Bug 1137245 - ServiceWorkerManager should set WorkerPrivate::LoadInfo::mIndexedDBAllowed correctly. r=bent, bkelly

This commit is contained in:
Fernando Jimenez 2015-06-11 15:32:54 +02:00
Родитель 10fa76166d
Коммит dbb95a17ef
7 изменённых файлов: 157 добавлений и 7 удалений

Просмотреть файл

@ -367,15 +367,14 @@ IDBFactory::AllowedForWindowInternal(nsPIDOMWindow* aWindow,
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}
if (nsContentUtils::IsSystemPrincipal(principal)) {
principal.forget(aPrincipal);
return NS_OK;
bool isSystemPrincipal;
if (!AllowedForPrincipal(principal, &isSystemPrincipal)) {
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}
bool isNullPrincipal;
if (NS_WARN_IF(NS_FAILED(principal->GetIsNullPrincipal(&isNullPrincipal))) ||
isNullPrincipal) {
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
if (isSystemPrincipal) {
principal.forget(aPrincipal);
return NS_OK;
}
// Whitelist about:home, since it doesn't have a base domain it would not
@ -424,6 +423,36 @@ IDBFactory::AllowedForWindowInternal(nsPIDOMWindow* aWindow,
return NS_OK;
}
// static
bool
IDBFactory::AllowedForPrincipal(nsIPrincipal* aPrincipal,
bool* aIsSystemPrincipal)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aPrincipal);
if (NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate())) {
return false;
}
if (nsContentUtils::IsSystemPrincipal(aPrincipal)) {
if (aIsSystemPrincipal) {
*aIsSystemPrincipal = true;
}
return true;
} else if (aIsSystemPrincipal) {
*aIsSystemPrincipal = false;
}
bool isNullPrincipal;
if (NS_WARN_IF(NS_FAILED(aPrincipal->GetIsNullPrincipal(&isNullPrincipal))) ||
isNullPrincipal) {
return false;
}
return true;
}
#ifdef DEBUG
void

Просмотреть файл

@ -105,6 +105,10 @@ public:
static bool
AllowedForWindow(nsPIDOMWindow* aWindow);
static bool
AllowedForPrincipal(nsIPrincipal* aPrincipal,
bool* aIsSystemPrincipal = nullptr);
void
AssertIsOnOwningThread() const
#ifdef DEBUG

Просмотреть файл

@ -12,6 +12,8 @@ support-files =
file_app_isolation.js
helpers.js
leaving_page_iframe.html
service_worker.js
service_worker_client.html
third_party_iframe1.html
third_party_iframe2.html
unit/test_add_put.js
@ -377,3 +379,5 @@ skip-if = buildapp == 'b2g' || buildapp == 'mulet' || e10s || toolkit == 'androi
# The clearBrowserData tests are only supposed to run in the main process.
# They currently time out on android.
skip-if = buildapp == 'b2g' || buildapp == 'mulet' || e10s || toolkit == 'android'
[test_serviceworker.html]
skip-if = buildapp == 'b2g'

Просмотреть файл

@ -0,0 +1,10 @@
onmessage = function(e) {
self.clients.matchAll().then(function(res) {
if (!res.length) {
dump("Error: no clients are currently controlled.\n");
return;
}
res[0].postMessage(indexedDB ? { available: true } :
{ available: false });
});
};

Просмотреть файл

@ -0,0 +1,28 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>controlled page</title>
<script class="testbody" type="text/javascript">
if (!parent) {
info("service_worker_client.html should not be launched directly!");
}
window.onload = function() {
navigator.serviceWorker.onmessage = function(msg) {
// Forward messages coming from the service worker to the test page.
parent.postMessage(msg.data, "*");
};
navigator.serviceWorker.ready.then(function(swr) {
parent.postMessage("READY", "*");
});
}
</script>
</head>
<body>
</body>
</html>

Просмотреть файл

@ -0,0 +1,71 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1137245 - Allow IndexedDB usage in ServiceWorkers</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">
function simpleRegister() {
return navigator.serviceWorker.register("service_worker.js", {
scope: 'service_worker_client.html'
});
}
function testIndexedDBAvailable(sw) {
var p = new Promise(function(resolve, reject) {
window.onmessage = function(e) {
if (e.data === "READY") {
sw.active.postMessage("GO");
return;
}
if (!("available" in e.data)) {
ok(false, "Something went wrong");
reject();
return;
}
ok(e.data.available, "IndexedDB available in service worker.");
resolve();
}
});
var content = document.getElementById("content");
ok(content, "Parent exists.");
iframe = document.createElement("iframe");
iframe.setAttribute('src', "service_worker_client.html");
content.appendChild(iframe);
return p.then(() => content.removeChild(iframe));
}
function runTest() {
simpleRegister()
.then(testIndexedDBAvailable)
.then(SimpleTest.finish)
.catch(function(e) {
ok(false, "Some test failed with error " + e);
SimpleTest.finish();
});
}
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, runTest);
</script>
</pre>
</body>
</html>

Просмотреть файл

@ -34,6 +34,7 @@
#include "mozilla/dom/DOMError.h"
#include "mozilla/dom/ErrorEvent.h"
#include "mozilla/dom/Headers.h"
#include "mozilla/dom/indexedDB/IDBFactory.h"
#include "mozilla/dom/InternalHeaders.h"
#include "mozilla/dom/Navigator.h"
#include "mozilla/dom/PromiseNativeHandler.h"
@ -3540,6 +3541,9 @@ ServiceWorkerManager::CreateServiceWorker(nsIPrincipal* aPrincipal,
info.mPrincipal = aPrincipal;
info.mIndexedDBAllowed =
indexedDB::IDBFactory::AllowedForPrincipal(aPrincipal);
// NOTE: this defaults the SW load context to:
// - private browsing = false
// - content = true