Bug 965970 - Add support to compile asm.js code at install/update time r=luke,bholley,marco

This commit is contained in:
Fabrice Desré 2014-04-17 22:03:03 -07:00
Родитель 7ae9022f62
Коммит b6c050f2c2
7 изменённых файлов: 338 добавлений и 16 удалений

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

@ -53,6 +53,7 @@
#include "mozilla/CORSMode.h"
#include "mozilla/Attributes.h"
#include "mozilla/unused.h"
#ifdef PR_LOGGING
static PRLogModuleInfo* gCspPRLog;
@ -814,11 +815,10 @@ NotifyOffThreadScriptLoadCompletedRunnable::Run()
static void
OffThreadScriptLoaderCallback(void *aToken, void *aCallbackData)
{
NotifyOffThreadScriptLoadCompletedRunnable* aRunnable =
static_cast<NotifyOffThreadScriptLoadCompletedRunnable*>(aCallbackData);
nsRefPtr<NotifyOffThreadScriptLoadCompletedRunnable> aRunnable =
dont_AddRef(static_cast<NotifyOffThreadScriptLoadCompletedRunnable*>(aCallbackData));
aRunnable->SetToken(aToken);
NS_DispatchToMainThread(aRunnable);
NS_RELEASE(aRunnable);
}
nsresult
@ -848,12 +848,9 @@ nsScriptLoader::AttemptAsyncScriptParse(nsScriptLoadRequest* aRequest)
return NS_ERROR_FAILURE;
}
NotifyOffThreadScriptLoadCompletedRunnable* runnable =
nsRefPtr<NotifyOffThreadScriptLoadCompletedRunnable> runnable =
new NotifyOffThreadScriptLoadCompletedRunnable(aRequest, this);
// This reference will be consumed by OffThreadScriptLoaderCallback.
NS_ADDREF(runnable);
if (!JS::CompileOffThread(cx, options,
aRequest->mScriptText.get(), aRequest->mScriptText.Length(),
OffThreadScriptLoaderCallback,
@ -863,6 +860,7 @@ nsScriptLoader::AttemptAsyncScriptParse(nsScriptLoadRequest* aRequest)
mDocument->BlockOnload();
unused << runnable.forget();
return NS_OK;
}

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

@ -0,0 +1,76 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const Cu = Components.utils;
const Ci = Components.interfaces;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Promise.jsm");
this.EXPORTED_SYMBOLS = ["ScriptPreloader"];
function debug(aMsg) {
//dump("--*-- ScriptPreloader: " + aMsg + "\n");
}
this.ScriptPreloader = {
#ifdef MOZ_B2G
_enabled: true,
#else
_enabled: false,
#endif
preload: function(aApp, aManifest) {
debug("Preloading " + aApp.origin);
let deferred = Promise.defer();
if (!this._enabled) {
deferred.resolve();
return deferred.promise;
}
if (aManifest.precompile &&
Array.isArray(aManifest.precompile) &&
aManifest.precompile.length > 0) {
let origin = Services.io.newURI(aApp.origin, null, null);
let toLoad = aManifest.precompile.length;
let principal =
Services.scriptSecurityManager
.getAppCodebasePrincipal(origin, aApp.localId, false);
aManifest.precompile.forEach((aPath) => {
let uri = Services.io.newURI(aPath, null, origin);
debug("Script to compile: " + uri.spec);
try {
Services.scriptloader.precompileScript(uri, principal,
(aSubject, aTopic, aData) => {
let uri = aSubject.QueryInterface(Ci.nsIURI);
debug("Done compiling " + uri.spec);
toLoad--;
if (toLoad == 0) {
deferred.resolve();
}
});
} catch (e) {
// Resolve the promise if precompileScript throws.
deferred.resolve();
}
});
} else {
// The precompile field is not an array, let the developer know.
// We don't want to have to enable debug for that to show up.
if (aManifest.precompile) {
Cu.reportError("ASM.JS compilation failed: the 'precompile' manifest " +
"property should be an array of script uris.\n");
}
deferred.resolve();
}
return deferred.promise;
}
}

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

@ -61,6 +61,9 @@ XPCOMUtils.defineLazyModuleGetter(this, "WebappOSUtils",
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ScriptPreloader",
"resource://gre/modules/ScriptPreloader.jsm");
#ifdef MOZ_WIDGET_GONK
XPCOMUtils.defineLazyGetter(this, "libcutils", function() {
Cu.import("resource://gre/modules/systemlibs.js");
@ -1489,7 +1492,9 @@ this.DOMApplicationRegistry = {
delete app.retryingDownload;
this._saveApps().then(() => {
// Update the asm.js scripts we need to compile.
ScriptPreloader.preload(app, aData)
.then(() => this._saveApps()).then(() => {
// Update the handlers and permissions for this app.
this.updateAppHandlers(aOldManifest, aData, app);
@ -2595,13 +2600,19 @@ onInstallSuccessAck: function onInstallSuccessAck(aManifestURL,
manifest: aManifest,
manifestURL: aNewApp.manifestURL
});
this.broadcastMessage("Webapps:FireEvent", {
eventType: ["downloadsuccess", "downloadapplied"],
manifestURL: aNewApp.manifestURL
});
if (aInstallSuccessCallback) {
aInstallSuccessCallback(aManifest, zipFile.path);
}
// Check if we have asm.js code to preload for this application.
ScriptPreloader.preload(aNewApp, aManifest)
.then(() => {
this.broadcastMessage("Webapps:FireEvent", {
eventType: ["downloadsuccess", "downloadapplied"],
manifestURL: aNewApp.manifestURL
});
if (aInstallSuccessCallback) {
aInstallSuccessCallback(aManifest, zipFile.path);
}
}
);
});
},

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

@ -36,6 +36,7 @@ EXTRA_JS_MODULES += [
EXTRA_PP_JS_MODULES += [
'AppsUtils.jsm',
'OperatorApps.jsm',
'ScriptPreloader.jsm',
'Webapps.jsm',
]

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

@ -4341,6 +4341,8 @@ JS::ReadOnlyCompileOptions::copyPODOptions(const ReadOnlyCompileOptions &rhs)
extraWarningsOption = rhs.extraWarningsOption;
werrorOption = rhs.werrorOption;
asmJSOption = rhs.asmJSOption;
forceAsync = rhs.forceAsync;
installedFile = rhs.installedFile;
sourcePolicy = rhs.sourcePolicy;
introductionType = rhs.introductionType;
introductionLineno = rhs.introductionLineno;

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

@ -6,7 +6,11 @@
#include "nsISupports.idl"
[scriptable, uuid(b21f1579-d994-4e99-a85d-a685140f3ec1)]
interface nsIURI;
interface nsIPrincipal;
interface nsIObserver;
[scriptable, uuid(19533e7b-f321-4ef1-bc59-6e812dc2a733)]
interface mozIJSSubScriptLoader : nsISupports
{
/**
@ -39,4 +43,22 @@ interface mozIJSSubScriptLoader : nsISupports
*/
[implicit_jscontext]
jsval loadSubScriptWithOptions(in AString url, in jsval options);
/*
* Compiles a JS script off the main thread and calls back the
* observer once it's done.
* The script will be cached in temporary or persistent storage depending
* on the principal used.
* We fire the notification callback in all cases - there is no fatal
* error there.
* @param uri the uri of the script to load.
* @param principal the principal from which we get the app id if any.
* @param observer this observer will be called once the script has
* been precompiled. The notification topic will be
* 'script-precompiled' and the subject the uri of the
* script as a nsIURI.
*/
void precompileScript(in nsIURI uri,
in nsIPrincipal principal,
in nsIObserver observer);
};

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

@ -18,6 +18,7 @@
#include "nsIFileURL.h"
#include "nsScriptLoader.h"
#include "nsIScriptSecurityManager.h"
#include "nsThreadUtils.h"
#include "jsapi.h"
#include "jsfriendapi.h"
@ -25,13 +26,16 @@
#include "nsJSPrincipals.h"
#include "xpcpublic.h" // For xpc::SystemErrorReporter
#include "xpcprivate.h" // For xpc::OptionsBase
#include "jswrapper.h"
#include "mozilla/scache/StartupCache.h"
#include "mozilla/scache/StartupCacheUtils.h"
#include "mozilla/unused.h"
using namespace mozilla::scache;
using namespace JS;
using namespace xpc;
using namespace mozilla;
class MOZ_STACK_CLASS LoadSubScriptOptions : public OptionsBase {
public:
@ -365,3 +369,211 @@ mozJSSubScriptLoader::DoLoadSubScriptWithOptions(const nsAString &url,
return NS_OK;
}
/**
* Let us compile scripts from a URI off the main thread.
*/
class ScriptPrecompiler : public nsIStreamLoaderObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSISTREAMLOADEROBSERVER
ScriptPrecompiler(nsIObserver* aObserver,
nsIPrincipal* aPrincipal,
nsIChannel* aChannel)
: mObserver(aObserver)
, mPrincipal(aPrincipal)
, mChannel(aChannel)
{}
virtual ~ScriptPrecompiler()
{}
static void OffThreadCallback(void *aToken, void *aData);
/* Sends the "done" notification back. Main thread only. */
void SendObserverNotification();
private:
nsRefPtr<nsIObserver> mObserver;
nsRefPtr<nsIPrincipal> mPrincipal;
nsRefPtr<nsIChannel> mChannel;
nsString mScript;
};
NS_IMPL_ISUPPORTS1(ScriptPrecompiler, nsIStreamLoaderObserver);
class NotifyPrecompilationCompleteRunnable : public nsRunnable
{
public:
NS_DECL_NSIRUNNABLE
NotifyPrecompilationCompleteRunnable(ScriptPrecompiler* aPrecompiler)
: mPrecompiler(aPrecompiler)
, mToken(nullptr)
{}
void SetToken(void* aToken) {
MOZ_ASSERT(aToken && !mToken);
mToken = aToken;
}
protected:
nsRefPtr<ScriptPrecompiler> mPrecompiler;
void* mToken;
};
/* RAII helper class to send observer notifications */
class AutoSendObserverNotification {
public:
AutoSendObserverNotification(ScriptPrecompiler* aPrecompiler)
: mPrecompiler(aPrecompiler)
{}
~AutoSendObserverNotification() {
if (mPrecompiler) {
mPrecompiler->SendObserverNotification();
}
}
void Disarm() {
mPrecompiler = nullptr;
}
private:
ScriptPrecompiler* mPrecompiler;
};
NS_IMETHODIMP
NotifyPrecompilationCompleteRunnable::Run(void)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mPrecompiler);
AutoSendObserverNotification notifier(mPrecompiler);
if (mToken) {
JSRuntime *rt = XPCJSRuntime::Get()->Runtime();
NS_ENSURE_TRUE(rt, NS_ERROR_FAILURE);
JS::FinishOffThreadScript(nullptr, rt, mToken);
}
return NS_OK;
}
NS_IMETHODIMP
ScriptPrecompiler::OnStreamComplete(nsIStreamLoader* aLoader,
nsISupports* aContext,
nsresult aStatus,
uint32_t aLength,
const uint8_t* aString)
{
AutoSendObserverNotification notifier(this);
// Just notify that we are done with this load.
NS_ENSURE_SUCCESS(aStatus, NS_OK);
// Convert data to jschar* and prepare to call CompileOffThread.
nsAutoString hintCharset;
nsresult rv =
nsScriptLoader::ConvertToUTF16(mChannel, aString, aLength,
hintCharset, nullptr, mScript);
NS_ENSURE_SUCCESS(rv, NS_OK);
// Our goal is to cache persistently the compiled script and to avoid quota
// checks. Since the caching mechanism decide the persistence type based on
// the principal, we create a new global with the app's principal.
// We then enter its compartment to compile with its principal.
AutoSafeJSContext cx;
RootedValue v(cx);
SandboxOptions sandboxOptions;
sandboxOptions.sandboxName.AssignASCII("asm.js precompilation");
sandboxOptions.invisibleToDebugger = true;
rv = CreateSandboxObject(cx, &v, mPrincipal, sandboxOptions);
NS_ENSURE_SUCCESS(rv, NS_OK);
JSAutoCompartment ac(cx, js::UncheckedUnwrap(&v.toObject()));
JS::CompileOptions options(cx, JSVERSION_DEFAULT);
options.setSourcePolicy(CompileOptions::NO_SOURCE);
options.forceAsync = true;
options.compileAndGo = true;
options.installedFile = true;
nsCOMPtr<nsIURI> uri;
mChannel->GetURI(getter_AddRefs(uri));
nsAutoCString spec;
uri->GetSpec(spec);
options.setFile(spec.get());
if (!JS::CanCompileOffThread(cx, options, mScript.Length())) {
NS_WARNING("Can't compile script off thread!");
return NS_OK;
}
nsRefPtr<NotifyPrecompilationCompleteRunnable> runnable =
new NotifyPrecompilationCompleteRunnable(this);
if (!JS::CompileOffThread(cx, options,
mScript.get(), mScript.Length(),
OffThreadCallback,
static_cast<void*>(runnable))) {
NS_WARNING("Failed to compile script off thread!");
return NS_OK;
}
unused << runnable.forget();
notifier.Disarm();
return NS_OK;
}
/* static */
void
ScriptPrecompiler::OffThreadCallback(void* aToken, void* aData)
{
nsRefPtr<NotifyPrecompilationCompleteRunnable> runnable =
dont_AddRef(static_cast<NotifyPrecompilationCompleteRunnable*>(aData));
runnable->SetToken(aToken);
NS_DispatchToMainThread(runnable);
}
void
ScriptPrecompiler::SendObserverNotification()
{
MOZ_ASSERT(mChannel && mObserver);
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIURI> uri;
mChannel->GetURI(getter_AddRefs(uri));
mObserver->Observe(uri, "script-precompiled", nullptr);
}
NS_IMETHODIMP
mozJSSubScriptLoader::PrecompileScript(nsIURI* aURI,
nsIPrincipal* aPrincipal,
nsIObserver *aObserver)
{
nsCOMPtr<nsIChannel> channel;
nsresult rv = NS_NewChannel(getter_AddRefs(channel),
aURI, nullptr, nullptr, nullptr,
nsIRequest::LOAD_NORMAL, nullptr);
NS_ENSURE_SUCCESS(rv, rv);
nsRefPtr<ScriptPrecompiler> loadObserver =
new ScriptPrecompiler(aObserver, aPrincipal, channel);
nsCOMPtr<nsIStreamLoader> loader;
rv = NS_NewStreamLoader(getter_AddRefs(loader), loadObserver);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStreamListener> listener = loader.get();
rv = channel->AsyncOpen(listener, nullptr);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}