dom/asmjscache/AsmJSCache.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=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 file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_dom_asmjscache_asmjscache_h
     8 #define mozilla_dom_asmjscache_asmjscache_h
    10 #include "ipc/IPCMessageUtils.h"
    11 #include "js/TypeDecls.h"
    12 #include "js/Vector.h"
    13 #include "jsapi.h"
    15 class nsIPrincipal;
    17 namespace mozilla {
    18 namespace dom {
    20 namespace quota {
    21 class Client;
    22 }
    24 namespace asmjscache {
    26 class PAsmJSCacheEntryChild;
    27 class PAsmJSCacheEntryParent;
    29 enum OpenMode
    30 {
    31   eOpenForRead,
    32   eOpenForWrite,
    33   NUM_OPEN_MODES
    34 };
    36 // Each origin stores a fixed size (kNumEntries) LRU cache of compiled asm.js
    37 // modules. Each compiled asm.js module is stored in a separate file with one
    38 // extra metadata file that stores the LRU cache and enough information for a
    39 // client to pick which cached module's file to open.
    40 struct Metadata
    41 {
    42   static const unsigned kNumEntries = 16;
    43   static const unsigned kLastEntry = kNumEntries - 1;
    45   struct Entry
    46   {
    47     uint32_t mFastHash;
    48     uint32_t mNumChars;
    49     uint32_t mFullHash;
    50     unsigned mModuleIndex;
    52     void clear() {
    53       mFastHash = -1;
    54       mNumChars = -1;
    55       mFullHash = -1;
    56     }
    57   };
    59   Entry mEntries[kNumEntries];
    60 };
    62 // Parameters specific to opening a cache entry for writing
    63 struct WriteParams
    64 {
    65   int64_t mSize;
    66   int64_t mFastHash;
    67   int64_t mNumChars;
    68   int64_t mFullHash;
    69   bool mInstalled;
    71   WriteParams()
    72   : mSize(0),
    73     mFastHash(0),
    74     mNumChars(0),
    75     mFullHash(0),
    76     mInstalled(false)
    77   { }
    78 };
    80 // Parameters specific to opening a cache entry for reading
    81 struct ReadParams
    82 {
    83   const jschar* mBegin;
    84   const jschar* mLimit;
    86   ReadParams()
    87   : mBegin(nullptr),
    88     mLimit(nullptr)
    89   { }
    90 };
    92 // Implementation of AsmJSCacheOps, installed for the main JSRuntime by
    93 // nsJSEnvironment.cpp and DOM Worker JSRuntimes in RuntimeService.cpp.
    94 //
    95 // The Open* functions cannot be called directly from AsmJSCacheOps: they take
    96 // an nsIPrincipal as the first argument instead of a Handle<JSObject*>. The
    97 // caller must map the object to an nsIPrincipal.
    98 //
    99 // These methods may be called off the main thread and guarantee not to
   100 // access the given aPrincipal except on the main thread. In exchange, the
   101 // caller must ensure the given principal is alive from when OpenEntryForX is
   102 // called to when CloseEntryForX returns.
   104 bool
   105 OpenEntryForRead(nsIPrincipal* aPrincipal,
   106                  const jschar* aBegin,
   107                  const jschar* aLimit,
   108                  size_t* aSize,
   109                  const uint8_t** aMemory,
   110                  intptr_t *aHandle);
   111 void
   112 CloseEntryForRead(JS::Handle<JSObject*> aGlobal,
   113                   size_t aSize,
   114                   const uint8_t* aMemory,
   115                   intptr_t aHandle);
   116 bool
   117 OpenEntryForWrite(nsIPrincipal* aPrincipal,
   118                   bool aInstalled,
   119                   const jschar* aBegin,
   120                   const jschar* aEnd,
   121                   size_t aSize,
   122                   uint8_t** aMemory,
   123                   intptr_t* aHandle);
   124 void
   125 CloseEntryForWrite(JS::Handle<JSObject*> aGlobal,
   126                    size_t aSize,
   127                    uint8_t* aMemory,
   128                    intptr_t aHandle);
   130 bool
   131 GetBuildId(JS::BuildIdCharVector* aBuildId);
   133 // Called from QuotaManager.cpp:
   135 quota::Client*
   136 CreateClient();
   138 // Called from ipc/ContentParent.cpp:
   140 PAsmJSCacheEntryParent*
   141 AllocEntryParent(OpenMode aOpenMode, WriteParams aWriteParams,
   142                  nsIPrincipal* aPrincipal);
   144 void
   145 DeallocEntryParent(PAsmJSCacheEntryParent* aActor);
   147 // Called from ipc/ContentChild.cpp:
   149 void
   150 DeallocEntryChild(PAsmJSCacheEntryChild* aActor);
   152 } // namespace asmjscache
   153 } // namespace dom
   154 } // namespace mozilla
   156 namespace IPC {
   158 template <>
   159 struct ParamTraits<mozilla::dom::asmjscache::OpenMode> :
   160   public ContiguousEnumSerializer<mozilla::dom::asmjscache::OpenMode,
   161                                   mozilla::dom::asmjscache::eOpenForRead,
   162                                   mozilla::dom::asmjscache::NUM_OPEN_MODES>
   163 { };
   165 template <>
   166 struct ParamTraits<mozilla::dom::asmjscache::Metadata>
   167 {
   168   typedef mozilla::dom::asmjscache::Metadata paramType;
   169   static void Write(Message* aMsg, const paramType& aParam);
   170   static bool Read(const Message* aMsg, void** aIter, paramType* aResult);
   171   static void Log(const paramType& aParam, std::wstring* aLog);
   172 };
   174 template <>
   175 struct ParamTraits<mozilla::dom::asmjscache::WriteParams>
   176 {
   177   typedef mozilla::dom::asmjscache::WriteParams paramType;
   178   static void Write(Message* aMsg, const paramType& aParam);
   179   static bool Read(const Message* aMsg, void** aIter, paramType* aResult);
   180   static void Log(const paramType& aParam, std::wstring* aLog);
   181 };
   183 } // namespace IPC
   185 #endif  // mozilla_dom_asmjscache_asmjscache_h

mercurial