AsmJSCache.h 5.0 KB

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