michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_dom_asmjscache_asmjscache_h michael@0: #define mozilla_dom_asmjscache_asmjscache_h michael@0: michael@0: #include "ipc/IPCMessageUtils.h" michael@0: #include "js/TypeDecls.h" michael@0: #include "js/Vector.h" michael@0: #include "jsapi.h" michael@0: michael@0: class nsIPrincipal; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: namespace quota { michael@0: class Client; michael@0: } michael@0: michael@0: namespace asmjscache { michael@0: michael@0: class PAsmJSCacheEntryChild; michael@0: class PAsmJSCacheEntryParent; michael@0: michael@0: enum OpenMode michael@0: { michael@0: eOpenForRead, michael@0: eOpenForWrite, michael@0: NUM_OPEN_MODES michael@0: }; michael@0: michael@0: // Each origin stores a fixed size (kNumEntries) LRU cache of compiled asm.js michael@0: // modules. Each compiled asm.js module is stored in a separate file with one michael@0: // extra metadata file that stores the LRU cache and enough information for a michael@0: // client to pick which cached module's file to open. michael@0: struct Metadata michael@0: { michael@0: static const unsigned kNumEntries = 16; michael@0: static const unsigned kLastEntry = kNumEntries - 1; michael@0: michael@0: struct Entry michael@0: { michael@0: uint32_t mFastHash; michael@0: uint32_t mNumChars; michael@0: uint32_t mFullHash; michael@0: unsigned mModuleIndex; michael@0: michael@0: void clear() { michael@0: mFastHash = -1; michael@0: mNumChars = -1; michael@0: mFullHash = -1; michael@0: } michael@0: }; michael@0: michael@0: Entry mEntries[kNumEntries]; michael@0: }; michael@0: michael@0: // Parameters specific to opening a cache entry for writing michael@0: struct WriteParams michael@0: { michael@0: int64_t mSize; michael@0: int64_t mFastHash; michael@0: int64_t mNumChars; michael@0: int64_t mFullHash; michael@0: bool mInstalled; michael@0: michael@0: WriteParams() michael@0: : mSize(0), michael@0: mFastHash(0), michael@0: mNumChars(0), michael@0: mFullHash(0), michael@0: mInstalled(false) michael@0: { } michael@0: }; michael@0: michael@0: // Parameters specific to opening a cache entry for reading michael@0: struct ReadParams michael@0: { michael@0: const jschar* mBegin; michael@0: const jschar* mLimit; michael@0: michael@0: ReadParams() michael@0: : mBegin(nullptr), michael@0: mLimit(nullptr) michael@0: { } michael@0: }; michael@0: michael@0: // Implementation of AsmJSCacheOps, installed for the main JSRuntime by michael@0: // nsJSEnvironment.cpp and DOM Worker JSRuntimes in RuntimeService.cpp. michael@0: // michael@0: // The Open* functions cannot be called directly from AsmJSCacheOps: they take michael@0: // an nsIPrincipal as the first argument instead of a Handle. The michael@0: // caller must map the object to an nsIPrincipal. michael@0: // michael@0: // These methods may be called off the main thread and guarantee not to michael@0: // access the given aPrincipal except on the main thread. In exchange, the michael@0: // caller must ensure the given principal is alive from when OpenEntryForX is michael@0: // called to when CloseEntryForX returns. michael@0: michael@0: bool michael@0: OpenEntryForRead(nsIPrincipal* aPrincipal, michael@0: const jschar* aBegin, michael@0: const jschar* aLimit, michael@0: size_t* aSize, michael@0: const uint8_t** aMemory, michael@0: intptr_t *aHandle); michael@0: void michael@0: CloseEntryForRead(JS::Handle aGlobal, michael@0: size_t aSize, michael@0: const uint8_t* aMemory, michael@0: intptr_t aHandle); michael@0: bool michael@0: OpenEntryForWrite(nsIPrincipal* aPrincipal, michael@0: bool aInstalled, michael@0: const jschar* aBegin, michael@0: const jschar* aEnd, michael@0: size_t aSize, michael@0: uint8_t** aMemory, michael@0: intptr_t* aHandle); michael@0: void michael@0: CloseEntryForWrite(JS::Handle aGlobal, michael@0: size_t aSize, michael@0: uint8_t* aMemory, michael@0: intptr_t aHandle); michael@0: michael@0: bool michael@0: GetBuildId(JS::BuildIdCharVector* aBuildId); michael@0: michael@0: // Called from QuotaManager.cpp: michael@0: michael@0: quota::Client* michael@0: CreateClient(); michael@0: michael@0: // Called from ipc/ContentParent.cpp: michael@0: michael@0: PAsmJSCacheEntryParent* michael@0: AllocEntryParent(OpenMode aOpenMode, WriteParams aWriteParams, michael@0: nsIPrincipal* aPrincipal); michael@0: michael@0: void michael@0: DeallocEntryParent(PAsmJSCacheEntryParent* aActor); michael@0: michael@0: // Called from ipc/ContentChild.cpp: michael@0: michael@0: void michael@0: DeallocEntryChild(PAsmJSCacheEntryChild* aActor); michael@0: michael@0: } // namespace asmjscache michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: namespace IPC { michael@0: michael@0: template <> michael@0: struct ParamTraits : michael@0: public ContiguousEnumSerializer michael@0: { }; michael@0: michael@0: template <> michael@0: struct ParamTraits michael@0: { michael@0: typedef mozilla::dom::asmjscache::Metadata paramType; michael@0: static void Write(Message* aMsg, const paramType& aParam); michael@0: static bool Read(const Message* aMsg, void** aIter, paramType* aResult); michael@0: static void Log(const paramType& aParam, std::wstring* aLog); michael@0: }; michael@0: michael@0: template <> michael@0: struct ParamTraits michael@0: { michael@0: typedef mozilla::dom::asmjscache::WriteParams paramType; michael@0: static void Write(Message* aMsg, const paramType& aParam); michael@0: static bool Read(const Message* aMsg, void** aIter, paramType* aResult); michael@0: static void Log(const paramType& aParam, std::wstring* aLog); michael@0: }; michael@0: michael@0: } // namespace IPC michael@0: michael@0: #endif // mozilla_dom_asmjscache_asmjscache_h