Bug 1037715 - Implement .searchParams on Location, r=bz, r=ehsan

This commit is contained in:
Andrea Marchesini 2014-08-07 17:45:21 -07:00
parent 7fd3612d75
commit 190b960292
18 changed files with 327 additions and 23 deletions

View File

@ -596,9 +596,10 @@ Link::SetSearchParams(URLSearchParams& aSearchParams)
}
void
Link::URLSearchParamsUpdated()
Link::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
{
MOZ_ASSERT(mSearchParams);
MOZ_ASSERT(mSearchParams == aSearchParams);
nsString search;
mSearchParams->Serialize(search);

View File

@ -115,7 +115,7 @@ public:
bool ElementHasHref() const;
// URLSearchParamsObserver
void URLSearchParamsUpdated() MOZ_OVERRIDE;
void URLSearchParamsUpdated(URLSearchParams* aSearchParams) MOZ_OVERRIDE;
protected:
virtual ~Link();

View File

@ -192,6 +192,7 @@
#include "nsIWidget.h"
#include "mozilla/dom/EncodingUtils.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/URLSearchParams.h"
static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
@ -1932,6 +1933,24 @@ nsDocShell::SetCurrentURI(nsIURI *aURI, nsIRequest *aRequest,
mLSHE->GetIsSubFrame(&isSubFrame);
}
// nsDocShell owns a URLSearchParams that is used by
// window.location.searchParams to be in sync with the current location.
if (!mURLSearchParams) {
mURLSearchParams = new URLSearchParams();
}
nsAutoCString search;
nsCOMPtr<nsIURL> url(do_QueryInterface(mCurrentURI));
if (url) {
nsresult rv = url->GetQuery(search);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to get the query from a nsIURL.");
}
}
mURLSearchParams->ParseInput(search, nullptr);
if (!isSubFrame && !isRoot) {
/*
* We don't want to send OnLocationChange notifications when
@ -5348,6 +5367,11 @@ nsDocShell::Destroy()
mParentWidget = nullptr;
mCurrentURI = nullptr;
if (mURLSearchParams) {
mURLSearchParams->RemoveObservers();
mURLSearchParams = nullptr;
}
if (mScriptGlobal) {
mScriptGlobal->DetachFromDocShell();
mScriptGlobal = nullptr;
@ -13187,3 +13211,9 @@ nsDocShell::GetOpenedRemote()
nsCOMPtr<nsITabParent> openedRemote(do_QueryReferent(mOpenedRemote));
return openedRemote;
}
URLSearchParams*
nsDocShell::GetURLSearchParams()
{
return mURLSearchParams;
}

View File

@ -50,6 +50,7 @@
namespace mozilla {
namespace dom {
class EventTarget;
class URLSearchParams;
}
}
@ -763,6 +764,9 @@ protected:
nsCOMPtr<nsIChannel> mFailedChannel;
uint32_t mFailedLoadType;
// window.location.searchParams is updated in sync with this object.
nsRefPtr<mozilla::dom::URLSearchParams> mURLSearchParams;
// Set in DoURILoad when either the LOAD_RELOAD_ALLOW_MIXED_CONTENT flag or
// the LOAD_NORMAL_ALLOW_MIXED_CONTENT flag is set.
// Checked in nsMixedContentBlocker, to see if the channels match.

View File

@ -11,6 +11,13 @@
#include "js/TypeDecls.h"
class nsPresContext;
class nsIPresShell;
namespace mozilla {
namespace dom {
class URLSearchParams;
}
}
%}
/**
@ -19,6 +26,7 @@ class nsIPresShell;
[ptr] native nsPresContext(nsPresContext);
[ptr] native nsIPresShell(nsIPresShell);
[ptr] native URLSearchParams(mozilla::dom::URLSearchParams);
interface nsIURI;
interface nsIChannel;
@ -46,7 +54,7 @@ interface nsITabParent;
typedef unsigned long nsLoadFlags;
[scriptable, builtinclass, uuid(e5fe5c76-e511-4da3-9709-f8294b8dc5ce)]
[scriptable, builtinclass, uuid(8ccc5a61-e053-4851-ab00-dadab0dffd7f)]
interface nsIDocShell : nsIDocShellTreeItem
{
/**
@ -982,4 +990,7 @@ interface nsIDocShell : nsIDocShellTreeItem
*/
[noscript,notxpcom,nostdcall] void setOpenedRemote(in nsITabParent aOpenedRemote);
[noscript,notxpcom,nostdcall] nsITabParent getOpenedRemote();
// URLSearchParams for the window.location is owned by the docShell.
[noscript,notxpcom] URLSearchParams getURLSearchParams();
};

View File

@ -341,9 +341,10 @@ URL::SetHost(const nsAString& aHost, ErrorResult& aRv)
}
void
URL::URLSearchParamsUpdated()
URL::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
{
MOZ_ASSERT(mSearchParams);
MOZ_ASSERT(mSearchParams == aSearchParams);
nsAutoString search;
mSearchParams->Serialize(search);

View File

@ -121,7 +121,7 @@ public:
}
// URLSearchParamsObserver
void URLSearchParamsUpdated() MOZ_OVERRIDE;
void URLSearchParamsUpdated(URLSearchParams* aSearchParams) MOZ_OVERRIDE;
private:
nsIURI* GetURI() const

View File

@ -237,6 +237,12 @@ URLSearchParams::RemoveObserver(URLSearchParamsObserver* aObserver)
mObservers.RemoveElement(aObserver);
}
void
URLSearchParams::RemoveObservers()
{
mObservers.Clear();
}
void
URLSearchParams::Get(const nsAString& aName, nsString& aRetval)
{
@ -387,7 +393,7 @@ URLSearchParams::NotifyObservers(URLSearchParamsObserver* aExceptObserver)
{
for (uint32_t i = 0; i < mObservers.Length(); ++i) {
if (mObservers[i] != aExceptObserver) {
mObservers[i]->URLSearchParamsUpdated();
mObservers[i]->URLSearchParamsUpdated(this);
}
}
}

View File

@ -18,12 +18,14 @@
namespace mozilla {
namespace dom {
class URLSearchParams;
class URLSearchParamsObserver : public nsISupports
{
public:
virtual ~URLSearchParamsObserver() {}
virtual void URLSearchParamsUpdated() = 0;
virtual void URLSearchParamsUpdated(URLSearchParams* aFromThis) = 0;
};
class URLSearchParams MOZ_FINAL : public nsISupports,
@ -59,6 +61,7 @@ public:
void AddObserver(URLSearchParamsObserver* aObserver);
void RemoveObserver(URLSearchParamsObserver* aObserver);
void RemoveObservers();
void Serialize(nsAString& aValue) const;

View File

@ -68,16 +68,33 @@ nsLocation::nsLocation(nsPIDOMWindow* aWindow, nsIDocShell *aDocShell)
nsLocation::~nsLocation()
{
RemoveURLSearchParams();
}
// QueryInterface implementation for nsLocation
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsLocation)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsIDOMLocation)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMLocation)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsLocation, mInnerWindow)
NS_IMPL_CYCLE_COLLECTION_CLASS(nsLocation)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsLocation)
tmp->RemoveURLSearchParams();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInnerWindow);
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsLocation)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSearchParams)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInnerWindow)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsLocation)
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsLocation)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsLocation)
@ -858,6 +875,17 @@ nsLocation::GetSearch(nsAString& aSearch)
NS_IMETHODIMP
nsLocation::SetSearch(const nsAString& aSearch)
{
nsresult rv = SetSearchInternal(aSearch);
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
nsresult
nsLocation::SetSearchInternal(const nsAString& aSearch)
{
if (!CallerSubsumes())
return NS_ERROR_DOM_SECURITY_ERR;
@ -1036,3 +1064,119 @@ nsLocation::WrapObject(JSContext* aCx)
{
return LocationBinding::Wrap(aCx, this);
}
URLSearchParams*
nsLocation::GetDocShellSearchParams()
{
nsCOMPtr<nsIDocShell> docShell = GetDocShell();
if (!docShell) {
return nullptr;
}
return docShell->GetURLSearchParams();
}
URLSearchParams*
nsLocation::SearchParams()
{
if (!mSearchParams) {
// We must register this object to the URLSearchParams of the docshell in
// order to receive updates.
nsRefPtr<URLSearchParams> searchParams = GetDocShellSearchParams();
if (searchParams) {
searchParams->AddObserver(this);
}
mSearchParams = new URLSearchParams();
mSearchParams->AddObserver(this);
UpdateURLSearchParams();
}
return mSearchParams;
}
void
nsLocation::SetSearchParams(URLSearchParams& aSearchParams)
{
if (mSearchParams) {
mSearchParams->RemoveObserver(this);
}
// the observer will be cleared using the cycle collector.
mSearchParams = &aSearchParams;
mSearchParams->AddObserver(this);
nsAutoString search;
mSearchParams->Serialize(search);
SetSearchInternal(search);
// We don't need to inform the docShell about this new SearchParams because
// setting the new value the docShell will refresh its value automatically.
}
void
nsLocation::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
{
MOZ_ASSERT(mSearchParams);
// This change comes from content.
if (aSearchParams == mSearchParams) {
nsAutoString search;
mSearchParams->Serialize(search);
SetSearchInternal(search);
return;
}
// This change comes from the docShell.
#ifdef DEBUG
{
nsRefPtr<URLSearchParams> searchParams = GetDocShellSearchParams();
MOZ_ASSERT(searchParams);
MOZ_ASSERT(aSearchParams == searchParams);
}
#endif
nsAutoString search;
aSearchParams->Serialize(search);
mSearchParams->ParseInput(NS_ConvertUTF16toUTF8(search), this);
}
void
nsLocation::UpdateURLSearchParams()
{
if (!mSearchParams) {
return;
}
nsAutoCString search;
nsCOMPtr<nsIURI> uri;
nsresult rv = GetURI(getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(!uri)) {
return;
}
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
if (url) {
nsresult rv = url->GetQuery(search);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to get the query from a nsIURL.");
}
}
mSearchParams->ParseInput(search, this);
}
void
nsLocation::RemoveURLSearchParams()
{
if (mSearchParams) {
mSearchParams->RemoveObserver(this);
mSearchParams = nullptr;
nsRefPtr<URLSearchParams> docShellSearchParams = GetDocShellSearchParams();
if (docShellSearchParams) {
docShellSearchParams->RemoveObserver(this);
}
}
}

View File

@ -14,6 +14,7 @@
#include "nsCycleCollectionParticipant.h"
#include "js/TypeDecls.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/URLSearchParams.h"
#include "nsPIDOMWindow.h"
class nsIURI;
@ -26,6 +27,7 @@ class nsIDocShellLoadInfo;
class nsLocation MOZ_FINAL : public nsIDOMLocation
, public nsWrapperCache
, public mozilla::dom::URLSearchParamsObserver
{
typedef mozilla::ErrorResult ErrorResult;
@ -33,7 +35,8 @@ public:
nsLocation(nsPIDOMWindow* aWindow, nsIDocShell *aDocShell);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsLocation)
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsLocation,
nsIDOMLocation)
void SetDocShell(nsIDocShell *aDocShell);
nsIDocShell *GetDocShell();
@ -118,6 +121,11 @@ public:
{
aError = SetSearch(aSeach);
}
mozilla::dom::URLSearchParams* SearchParams();
void SetSearchParams(mozilla::dom::URLSearchParams& aSearchParams);
void GetHash(nsAString& aHash, ErrorResult& aError)
{
aError = GetHash(aHash);
@ -136,9 +144,18 @@ public:
}
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
// URLSearchParamsObserver
void URLSearchParamsUpdated(mozilla::dom::URLSearchParams* aSearchParams) MOZ_OVERRIDE;
protected:
virtual ~nsLocation();
nsresult SetSearchInternal(const nsAString& aSearch);
void UpdateURLSearchParams();
void RemoveURLSearchParams();
mozilla::dom::URLSearchParams* GetDocShellSearchParams();
// In the case of jar: uris, we sometimes want the place the jar was
// fetched from as the URI instead of the jar: uri itself. Pass in
// true for aGetInnermostURI when that's the case.
@ -156,6 +173,7 @@ protected:
nsString mCachedHash;
nsCOMPtr<nsPIDOMWindow> mInnerWindow;
nsRefPtr<mozilla::dom::URLSearchParams> mSearchParams;
nsWeakPtr mDocShell;
};

View File

@ -47,6 +47,7 @@ skip-if = buildapp == 'mulet' || buildapp == 'b2g' || toolkit == 'android' || e1
[test_history_state_null.html]
[test_Image_constructor.html]
[test_innersize_scrollport.html]
[test_location_searchParams.html]
[test_messageChannel.html]
[test_messageChannel_cloning.html]
[test_messageChannel_pingpong.html]

View File

@ -0,0 +1,89 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1037715
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1037715</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1037715">Mozilla Bug 1037715</a>
<iframe id="a"></iframe>
<script type="application/javascript">
var l;
var iframe = document.getElementById('a');
function onload0() {
iframe.removeEventListener('load', onload0);
l = iframe.contentWindow.location;
is(l.searchParams.get('a'), 'test0', 'l.searchParams value is ok');
info('changing location from JS...');
iframe.addEventListener('load', onload1);
iframe.contentWindow.location.href = 'file_empty.html?a=test1';
}
function onload1() {
iframe.removeEventListener('load', onload1);
var ll = iframe.contentWindow.location;
is(ll.searchParams.get('a'), 'test1', 'location.searchParams value is ok');
is(l.searchParams.get('a'), 'test1', 'l.searchParams value is ok');
isnot(ll.searchParams, l.searchParams, '2 different objects.');
info('changing location using l.searchParams...');
iframe.addEventListener('load', onload2);
l.searchParams.set('a', 'test2');
}
function onload2() {
iframe.removeEventListener('load', onload2);
var ll = iframe.contentWindow.location;
is(ll.searchParams.get('a'), 'test2', 'location.searchParams value is ok');
is(l.searchParams.get('a'), 'test2', 'l.searchParams value is ok');
isnot(ll.searchParams, l.searchParams, '2 different objects.');
info('changing iframe.src...');
iframe.addEventListener('load', onload3);
l.search = 'a=test3';
}
function onload3() {
iframe.removeEventListener('load', onload3);
var ll = iframe.contentWindow.location;
is(ll.searchParams.get('a'), 'test3', 'location.searchParams value is ok');
is(l.searchParams.get('a'), 'test3', 'l.searchParams value is ok');
isnot(ll.searchParams, l.searchParams, '2 different objects.');
info('changing iframe.src...');
iframe.addEventListener('load', onload4);
iframe.src = 'file_empty.html?a=test4';
}
function onload4() {
iframe.removeEventListener('load', onload4);
var ll = iframe.contentWindow.location;
is(ll.searchParams.get('a'), 'test4', 'location.searchParams value is ok');
is(l.searchParams.get('a'), 'test4', 'l.searchParams value is ok');
isnot(ll.searchParams, l.searchParams, '2 different objects.');
SimpleTest.finish();
}
iframe.addEventListener('load', onload0);
iframe.src = "file_empty.html?a=test0";
SimpleTest.waitForExplicitFinish();
</script>
</body>
</html>

View File

@ -22,4 +22,4 @@ interface Location {
void reload(optional boolean forceget = false);
};
// No support for .searchParams on Location yet. See bug 1037715.
Location implements URLUtilsNoSearchParams;
Location implements URLUtils;

View File

@ -15,7 +15,7 @@
[NoInterfaceObject,
Exposed=(Window, Worker)]
interface URLUtilsNoSearchParams {
interface URLUtils {
// Bug 824857: no support for stringifier attributes yet.
// stringifier attribute DOMString href;
[Throws, CrossOriginWritable=Location]
@ -39,7 +39,9 @@ interface URLUtilsNoSearchParams {
attribute DOMString pathname;
[Throws]
attribute DOMString search;
// searchParams should go here once Location implements it. See bug 1037715.
attribute URLSearchParams searchParams;
[Throws]
attribute DOMString hash;
@ -47,10 +49,3 @@ interface URLUtilsNoSearchParams {
[Throws]
stringifier;
};
[NoInterfaceObject,
Exposed=(Window, Worker)]
interface URLUtils : URLUtilsNoSearchParams
{
attribute URLSearchParams searchParams;
};

View File

@ -894,9 +894,10 @@ URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aUrl)
}
void
URL::URLSearchParamsUpdated()
URL::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
{
MOZ_ASSERT(mSearchParams);
MOZ_ASSERT(mSearchParams == aSearchParams);
nsString search;
mSearchParams->Serialize(search);

View File

@ -119,7 +119,7 @@ public:
}
// IURLSearchParamsObserver
void URLSearchParamsUpdated() MOZ_OVERRIDE;
void URLSearchParamsUpdated(URLSearchParams* aSearchParams) MOZ_OVERRIDE;
private:
URLProxy* GetURLProxy() const

View File

@ -118,7 +118,7 @@ Link::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
}
void
Link::URLSearchParamsUpdated()
Link::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
{
NS_NOTREACHED("Unexpected call to Link::URLSearchParamsUpdated");
}