ScriptCacheActors.cpp (2956B)
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/. */ 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 ScriptCacheChild::Init(const Maybe<FileDescriptor>& cacheFile, 17 bool wantCacheData) { 18 AssertIsOnMainThread(); 19 mWantCacheData = wantCacheData; 20 21 auto& cache = ScriptPreloader::GetChildSingleton(); 22 (void)cache.InitCache(cacheFile, this); 23 24 if (!wantCacheData) { 25 // If the parent process isn't expecting any cache data from us, we're 26 // done. 27 Send__delete__(this, AutoTArray<ScriptData, 0>()); 28 } 29 } 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 ScriptCacheChild::SendScriptsAndFinalize( 34 ScriptPreloader::ScriptHash& scripts) { 35 MOZ_ASSERT(mWantCacheData); 36 37 auto matcher = ScriptPreloader::Match<ScriptPreloader::ScriptStatus::Saved>(); 38 39 JS::FrontendContext* fc = JS::NewFrontendContext(); 40 if (!fc) { 41 return; 42 } 43 44 nsTArray<ScriptData> dataArray; 45 for (auto& script : IterHash(scripts, matcher)) { 46 if (!script->mSize && !script->XDREncode(fc)) { 47 continue; 48 } 49 50 auto data = dataArray.AppendElement(); 51 52 data->url() = script->mURL; 53 data->cachePath() = script->mCachePath; 54 data->loadTime() = script->mLoadTime; 55 56 if (script->HasBuffer()) { 57 auto& xdrData = script->Buffer(); 58 data->xdrData().AppendElements(xdrData.begin(), xdrData.length()); 59 script->FreeData(); 60 } 61 } 62 63 JS::DestroyFrontendContext(fc); 64 65 Send__delete__(this, dataArray); 66 } 67 68 void ScriptCacheChild::ActorDestroy(ActorDestroyReason aWhy) { 69 auto& cache = ScriptPreloader::GetChildSingleton(); 70 cache.mChildActor = nullptr; 71 } 72 73 IPCResult ScriptCacheParent::Recv__delete__(nsTArray<ScriptData>&& scripts) { 74 if (!mWantCacheData && scripts.Length()) { 75 return IPC_FAIL(this, "UnexpectedScriptData"); 76 } 77 78 // We don't want any more data from the process at this point. 79 mWantCacheData = false; 80 81 // Merge the child's script data with the parent's. 82 auto parent = static_cast<dom::ContentParent*>(Manager()); 83 auto processType = 84 ScriptPreloader::GetChildProcessType(parent->GetRemoteType()); 85 86 auto& cache = ScriptPreloader::GetChildSingleton(); 87 for (auto& script : scripts) { 88 cache.NoteStencil(script.url(), script.cachePath(), processType, 89 std::move(script.xdrData()), script.loadTime()); 90 } 91 92 return IPC_OK(); 93 } 94 95 void ScriptCacheParent::ActorDestroy(ActorDestroyReason aWhy) {} 96 97 } // namespace loader 98 } // namespace mozilla