Bug 1057135 - ServiceWorkerManager::Register() uses entry global. r=ehsan

--HG--
extra : transplant_source : %B1%3F%EA%17%81%DB%D6%EC%8A%86%3C%EE%D7%A9x%C7%0B%CD2%A0
This commit is contained in:
Nikhil Marathe 2014-08-21 16:31:12 -07:00
parent 196068fbd3
commit e8762c026e
3 changed files with 48 additions and 35 deletions

View File

@ -8,13 +8,26 @@
interface nsIDocument;
interface nsIURI;
[uuid(9bec4abf-0f3d-4eca-8650-428f2eb04169)]
[uuid(c7132f91-c46c-4e01-b75a-43babb254d93)]
interface nsIServiceWorkerManager : nsISupports
{
// Returns a Promise
nsISupports register(in nsIDOMWindow aWindow, in DOMString aScope, in DOMString aScriptURI);
/**
* Registers a ServiceWorker with script loaded from `aScriptURI` to act as
* the ServiceWorker for aScope. Requires a valid entry settings object on
* the stack. This means you must call this from content code 'within'
* a window.
*
* Returns a Promise.
*/
nsISupports register(in DOMString aScope, in DOMString aScriptURI);
// Returns a Promise
/**
* Unregister an existing ServiceWorker registration for `aScope`. Requires
* a valid entry settings object on the stack. This means you must call this
* from content code 'within' a window.
*
* Returns a Promise.
*/
nsISupports unregister(in DOMString aScope);
// Returns a Promise

View File

@ -61,7 +61,7 @@ ServiceWorkerContainer::Register(const nsAString& aScriptURL,
return nullptr;
}
aRv = swm->Register(mWindow, aOptions.mScope, aScriptURL, getter_AddRefs(promise));
aRv = swm->Register(aOptions.mScope, aScriptURL, getter_AddRefs(promise));
if (aRv.Failed()) {
return nullptr;
}

View File

@ -544,31 +544,30 @@ public:
// If we return an error code here, the ServiceWorkerContainer will
// automatically reject the Promise.
NS_IMETHODIMP
ServiceWorkerManager::Register(nsIDOMWindow* aWindow, const nsAString& aScope,
ServiceWorkerManager::Register(const nsAString& aScope,
const nsAString& aScriptURL,
nsISupports** aPromise)
{
AssertIsOnMainThread();
MOZ_ASSERT(aWindow);
// XXXnsm Don't allow chrome callers for now, we don't support chrome
// ServiceWorkers.
MOZ_ASSERT(!nsContentUtils::IsCallerChrome());
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aWindow);
if (!window) {
nsCOMPtr<nsIGlobalObject> sgo = GetEntryGlobal();
if (!sgo) {
MOZ_ASSERT("Register() should only be called from a valid entry settings object!");
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIGlobalObject> sgo = do_QueryInterface(window);
ErrorResult result;
nsRefPtr<Promise> promise = Promise::Create(sgo, result);
if (result.Failed()) {
return result.ErrorCode();
}
nsCOMPtr<nsIURI> documentURI = window->GetDocumentURI();
if (!documentURI) {
nsCOMPtr<nsIDocument> doc = GetEntryDocument();
if (!doc) {
return NS_ERROR_FAILURE;
}
@ -578,52 +577,53 @@ ServiceWorkerManager::Register(nsIDOMWindow* aWindow, const nsAString& aScope,
// asynchronously. We aren't making any internal state changes in these
// checks, so ordering of multiple calls is not affected.
nsresult rv;
nsCOMPtr<nsIURI> documentURI = doc->GetBaseURI();
// FIXME(nsm): Bug 1003991. Disable check when devtools are open.
if (!Preferences::GetBool("dom.serviceWorkers.testing.enabled")) {
bool isHttps;
rv = documentURI->SchemeIs("https", &isHttps);
if (NS_FAILED(rv) || !isHttps) {
result = documentURI->SchemeIs("https", &isHttps);
if (result.Failed() || !isHttps) {
NS_WARNING("ServiceWorker registration from insecure websites is not allowed.");
return NS_ERROR_DOM_SECURITY_ERR;
}
}
nsCOMPtr<nsIPrincipal> documentPrincipal;
if (window->GetExtantDoc()) {
documentPrincipal = window->GetExtantDoc()->NodePrincipal();
} else {
documentPrincipal = do_CreateInstance("@mozilla.org/nullprincipal;1");
}
nsCOMPtr<nsIURI> scriptURI;
rv = NS_NewURI(getter_AddRefs(scriptURI), aScriptURL, nullptr, documentURI);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
result = NS_NewURI(getter_AddRefs(scriptURI), aScriptURL, nullptr, documentURI);
if (NS_WARN_IF(result.Failed())) {
return result.ErrorCode();
}
// Data URLs are not allowed.
rv = documentPrincipal->CheckMayLoad(scriptURI, true /* report */,
false /* allowIfInheritsPrincipal */);
if (NS_FAILED(rv)) {
nsCOMPtr<nsIPrincipal> documentPrincipal = doc->NodePrincipal();
result = documentPrincipal->CheckMayLoad(scriptURI, true /* report */,
false /* allowIfInheritsPrincipal */);
if (result.Failed()) {
return NS_ERROR_DOM_SECURITY_ERR;
}
nsCOMPtr<nsIURI> scopeURI;
rv = NS_NewURI(getter_AddRefs(scopeURI), aScope, nullptr, documentURI);
if (NS_WARN_IF(NS_FAILED(rv))) {
result = NS_NewURI(getter_AddRefs(scopeURI), aScope, nullptr, documentURI);
if (NS_WARN_IF(result.Failed())) {
return NS_ERROR_DOM_SECURITY_ERR;
}
rv = documentPrincipal->CheckMayLoad(scopeURI, true /* report */,
false /* allowIfInheritsPrinciple */);
if (NS_FAILED(rv)) {
result = documentPrincipal->CheckMayLoad(scopeURI, true /* report */,
false /* allowIfInheritsPrinciple */);
if (result.Failed()) {
return NS_ERROR_DOM_SECURITY_ERR;
}
nsCString cleanedScope;
rv = scopeURI->GetSpecIgnoringRef(cleanedScope);
if (NS_WARN_IF(NS_FAILED(rv))) {
result = scopeURI->GetSpecIgnoringRef(cleanedScope);
if (NS_WARN_IF(result.Failed())) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsPIDOMWindow> window = do_QueryObject(sgo);
if (!window) {
return NS_ERROR_FAILURE;
}