Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / js / xpconnect / loader / ScriptCacheActors.cpp
blob9b44f0ffe63d24418c3fd94bff674b41684637b7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/ScriptPreloader.h"
8 #include "ScriptPreloader-inl.h"
9 #include "mozilla/loader/ScriptCacheActors.h"
11 #include "mozilla/dom/ContentParent.h"
13 namespace mozilla {
14 namespace loader {
16 void ScriptCacheChild::Init(const Maybe<FileDescriptor>& cacheFile,
17 bool wantCacheData) {
18 mWantCacheData = wantCacheData;
20 auto& cache = ScriptPreloader::GetChildSingleton();
21 Unused << cache.InitCache(cacheFile, this);
23 if (!wantCacheData) {
24 // If the parent process isn't expecting any cache data from us, we're
25 // done.
26 Send__delete__(this, AutoTArray<ScriptData, 0>());
30 // Finalize the script cache for the content process, and send back data about
31 // any scripts executed up to this point.
32 void ScriptCacheChild::SendScriptsAndFinalize(
33 ScriptPreloader::ScriptHash& scripts) {
34 MOZ_ASSERT(mWantCacheData);
36 AutoSafeJSAPI jsapi;
38 auto matcher = ScriptPreloader::Match<ScriptPreloader::ScriptStatus::Saved>();
40 nsTArray<ScriptData> dataArray;
41 for (auto& script : IterHash(scripts, matcher)) {
42 if (!script->mSize && !script->XDREncode(jsapi.cx())) {
43 continue;
46 auto data = dataArray.AppendElement();
48 data->url() = script->mURL;
49 data->cachePath() = script->mCachePath;
50 data->loadTime() = script->mLoadTime;
52 if (script->HasBuffer()) {
53 auto& xdrData = script->Buffer();
54 data->xdrData().AppendElements(xdrData.begin(), xdrData.length());
55 script->FreeData();
59 Send__delete__(this, dataArray);
62 void ScriptCacheChild::ActorDestroy(ActorDestroyReason aWhy) {
63 auto& cache = ScriptPreloader::GetChildSingleton();
64 cache.mChildActor = nullptr;
67 IPCResult ScriptCacheParent::Recv__delete__(nsTArray<ScriptData>&& scripts) {
68 if (!mWantCacheData && scripts.Length()) {
69 return IPC_FAIL(this, "UnexpectedScriptData");
72 // We don't want any more data from the process at this point.
73 mWantCacheData = false;
75 // Merge the child's script data with the parent's.
76 auto parent = static_cast<dom::ContentParent*>(Manager());
77 auto processType =
78 ScriptPreloader::GetChildProcessType(parent->GetRemoteType());
80 auto& cache = ScriptPreloader::GetChildSingleton();
81 for (auto& script : scripts) {
82 cache.NoteStencil(script.url(), script.cachePath(), processType,
83 std::move(script.xdrData()), script.loadTime());
86 return IPC_OK();
89 void ScriptCacheParent::ActorDestroy(ActorDestroyReason aWhy) {}
91 } // namespace loader
92 } // namespace mozilla