Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim: set ts=8 sts=4 et sw=4 tw=99: */
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/. */
6 :
7 : #include "mozilla/ScriptPreloader.h"
8 : #include "ScriptPreloader-inl.h"
9 : #include "mozilla/loader/ScriptCacheActors.h"
10 :
11 : #include "mozilla/dom/ContentParent.h"
12 :
13 : namespace mozilla {
14 : namespace loader {
15 :
16 : void
17 2 : ScriptCacheChild::Init(const Maybe<FileDescriptor>& cacheFile, bool wantCacheData)
18 : {
19 2 : mWantCacheData = wantCacheData;
20 :
21 2 : auto& cache = ScriptPreloader::GetChildSingleton();
22 2 : Unused << cache.InitCache(cacheFile, this);
23 :
24 2 : if (!wantCacheData) {
25 : // If the parent process isn't expecting any cache data from us, we're
26 : // done.
27 1 : Send__delete__(this, AutoTArray<ScriptData, 0>());
28 : }
29 2 : }
30 :
31 : // Finalize the script cache for the content process, and send back data about
32 : // any scripts executed up to this point.
33 : void
34 1 : ScriptCacheChild::SendScriptsAndFinalize(ScriptPreloader::ScriptHash& scripts)
35 : {
36 1 : MOZ_ASSERT(mWantCacheData);
37 :
38 2 : AutoSafeJSAPI jsapi;
39 :
40 1 : auto matcher = ScriptPreloader::Match<ScriptPreloader::ScriptStatus::Saved>();
41 :
42 2 : nsTArray<ScriptData> dataArray;
43 43 : for (auto& script : IterHash(scripts, matcher)) {
44 42 : if (!script->mSize && !script->XDREncode(jsapi.cx())) {
45 0 : continue;
46 : }
47 :
48 42 : auto data = dataArray.AppendElement();
49 :
50 42 : data->url() = script->mURL;
51 42 : data->cachePath() = script->mCachePath;
52 42 : data->loadTime() = script->mLoadTime;
53 :
54 42 : if (script->HasBuffer()) {
55 18 : auto& xdrData = script->Buffer();
56 18 : data->xdrData().AppendElements(xdrData.begin(), xdrData.length());
57 18 : script->FreeData();
58 : }
59 : }
60 :
61 1 : Send__delete__(this, dataArray);
62 1 : }
63 :
64 : void
65 2 : ScriptCacheChild::ActorDestroy(ActorDestroyReason aWhy)
66 : {
67 2 : auto& cache = ScriptPreloader::GetChildSingleton();
68 2 : cache.mChildActor = nullptr;
69 2 : }
70 :
71 :
72 : IPCResult
73 1 : ScriptCacheParent::Recv__delete__(nsTArray<ScriptData>&& scripts)
74 : {
75 1 : if (!mWantCacheData && scripts.Length()) {
76 0 : return IPC_FAIL(this, "UnexpectedScriptData");
77 : }
78 :
79 : // We don't want any more data from the process at this point.
80 1 : mWantCacheData = false;
81 :
82 : // Merge the child's script data with the parent's.
83 1 : auto parent = static_cast<dom::ContentParent*>(Manager());
84 1 : auto processType = ScriptPreloader::GetChildProcessType(parent->GetRemoteType());
85 :
86 1 : auto& cache = ScriptPreloader::GetChildSingleton();
87 43 : for (auto& script : scripts) {
88 42 : cache.NoteScript(script.url(), script.cachePath(), processType,
89 84 : Move(script.xdrData()), script.loadTime());
90 : }
91 :
92 1 : return IPC_OK();
93 : }
94 :
95 : void
96 1 : ScriptCacheParent::ActorDestroy(ActorDestroyReason aWhy)
97 1 : {}
98 :
99 : } // namespace loader
100 : } // namespace mozilla
|