JavaScriptShared.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  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. #ifndef mozilla_jsipc_JavaScriptShared_h__
  7. #define mozilla_jsipc_JavaScriptShared_h__
  8. #include "mozilla/dom/DOMTypes.h"
  9. #include "mozilla/jsipc/CrossProcessObjectWrappers.h"
  10. #include "mozilla/jsipc/PJavaScript.h"
  11. #include "js/GCHashTable.h"
  12. #include "nsJSUtils.h"
  13. namespace mozilla {
  14. namespace jsipc {
  15. class ObjectId {
  16. public:
  17. // Use 47 bits at most, to be safe, since jsval privates are encoded as
  18. // doubles. See bug 1065811 comment 12 for an explanation.
  19. static const size_t SERIAL_NUMBER_BITS = 47;
  20. static const size_t FLAG_BITS = 1;
  21. static const uint64_t SERIAL_NUMBER_MAX = (uint64_t(1) << SERIAL_NUMBER_BITS) - 1;
  22. explicit ObjectId(uint64_t serialNumber, bool hasXrayWaiver)
  23. : serialNumber_(serialNumber), hasXrayWaiver_(hasXrayWaiver)
  24. {
  25. if (MOZ_UNLIKELY(serialNumber == 0 || serialNumber > SERIAL_NUMBER_MAX))
  26. MOZ_CRASH("Bad CPOW Id");
  27. }
  28. bool operator==(const ObjectId& other) const {
  29. bool equal = serialNumber() == other.serialNumber();
  30. MOZ_ASSERT_IF(equal, hasXrayWaiver() == other.hasXrayWaiver());
  31. return equal;
  32. }
  33. bool isNull() { return !serialNumber_; }
  34. uint64_t serialNumber() const { return serialNumber_; }
  35. bool hasXrayWaiver() const { return hasXrayWaiver_; }
  36. uint64_t serialize() const {
  37. MOZ_ASSERT(serialNumber(), "Don't send a null ObjectId over IPC");
  38. return uint64_t((serialNumber() << FLAG_BITS) | ((hasXrayWaiver() ? 1 : 0) << 0));
  39. }
  40. static ObjectId nullId() { return ObjectId(); }
  41. static ObjectId deserialize(uint64_t data) {
  42. return ObjectId(data >> FLAG_BITS, data & 1);
  43. }
  44. // For use with StructGCPolicy.
  45. void trace(JSTracer*) const {}
  46. bool needsSweep() const { return false; }
  47. private:
  48. ObjectId() : serialNumber_(0), hasXrayWaiver_(false) {}
  49. uint64_t serialNumber_ : SERIAL_NUMBER_BITS;
  50. bool hasXrayWaiver_ : 1;
  51. };
  52. class JavaScriptShared;
  53. // DefaultHasher<T> requires that T coerce to an integral type. We could make
  54. // ObjectId do that, but doing so would weaken our type invariants, so we just
  55. // reimplement it manually.
  56. struct ObjectIdHasher
  57. {
  58. typedef ObjectId Lookup;
  59. static js::HashNumber hash(const Lookup& l) {
  60. return l.serialize();
  61. }
  62. static bool match(const ObjectId& k, const ObjectId& l) {
  63. return k == l;
  64. }
  65. static void rekey(ObjectId& k, const ObjectId& newKey) {
  66. k = newKey;
  67. }
  68. };
  69. // Map ids -> JSObjects
  70. class IdToObjectMap
  71. {
  72. typedef js::HashMap<ObjectId, JS::Heap<JSObject*>, ObjectIdHasher, js::SystemAllocPolicy> Table;
  73. public:
  74. IdToObjectMap();
  75. bool init();
  76. void trace(JSTracer* trc, uint64_t minimumId = 0);
  77. void sweep();
  78. bool add(ObjectId id, JSObject* obj);
  79. JSObject* find(ObjectId id);
  80. JSObject* findPreserveColor(ObjectId id);
  81. void remove(ObjectId id);
  82. void clear();
  83. bool empty() const;
  84. #ifdef DEBUG
  85. bool has(const ObjectId& id, const JSObject* obj) const;
  86. #endif
  87. private:
  88. Table table_;
  89. };
  90. // Map JSObjects -> ids
  91. class ObjectToIdMap
  92. {
  93. using Hasher = js::MovableCellHasher<JS::Heap<JSObject*>>;
  94. using Table = JS::GCHashMap<JS::Heap<JSObject*>, ObjectId, Hasher, js::SystemAllocPolicy>;
  95. public:
  96. bool init();
  97. void trace(JSTracer* trc);
  98. void sweep();
  99. bool add(JSContext* cx, JSObject* obj, ObjectId id);
  100. ObjectId find(JSObject* obj);
  101. void remove(JSObject* obj);
  102. void clear();
  103. private:
  104. Table table_;
  105. };
  106. class Logging;
  107. class JavaScriptShared : public CPOWManager
  108. {
  109. public:
  110. JavaScriptShared();
  111. virtual ~JavaScriptShared();
  112. bool init();
  113. void decref();
  114. void incref();
  115. bool Unwrap(JSContext* cx, const InfallibleTArray<CpowEntry>& aCpows, JS::MutableHandleObject objp);
  116. bool Wrap(JSContext* cx, JS::HandleObject aObj, InfallibleTArray<CpowEntry>* outCpows);
  117. protected:
  118. bool toVariant(JSContext* cx, JS::HandleValue from, JSVariant* to);
  119. bool fromVariant(JSContext* cx, const JSVariant& from, JS::MutableHandleValue to);
  120. bool toJSIDVariant(JSContext* cx, JS::HandleId from, JSIDVariant* to);
  121. bool fromJSIDVariant(JSContext* cx, const JSIDVariant& from, JS::MutableHandleId to);
  122. bool toSymbolVariant(JSContext* cx, JS::Symbol* sym, SymbolVariant* symVarp);
  123. JS::Symbol* fromSymbolVariant(JSContext* cx, const SymbolVariant& symVar);
  124. bool fromDescriptor(JSContext* cx, JS::Handle<JS::PropertyDescriptor> desc,
  125. PPropertyDescriptor* out);
  126. bool toDescriptor(JSContext* cx, const PPropertyDescriptor& in,
  127. JS::MutableHandle<JS::PropertyDescriptor> out);
  128. bool toObjectOrNullVariant(JSContext* cx, JSObject* obj, ObjectOrNullVariant* objVarp);
  129. JSObject* fromObjectOrNullVariant(JSContext* cx, const ObjectOrNullVariant& objVar);
  130. bool convertIdToGeckoString(JSContext* cx, JS::HandleId id, nsString* to);
  131. bool convertGeckoStringToId(JSContext* cx, const nsString& from, JS::MutableHandleId id);
  132. virtual bool toObjectVariant(JSContext* cx, JSObject* obj, ObjectVariant* objVarp) = 0;
  133. virtual JSObject* fromObjectVariant(JSContext* cx, const ObjectVariant& objVar) = 0;
  134. static void ConvertID(const nsID& from, JSIID* to);
  135. static void ConvertID(const JSIID& from, nsID* to);
  136. JSObject* findCPOWById(const ObjectId& objId) {
  137. return cpows_.find(objId);
  138. }
  139. JSObject* findObjectById(JSContext* cx, const ObjectId& objId);
  140. #ifdef DEBUG
  141. bool hasCPOW(const ObjectId& objId, const JSObject* obj) {
  142. return cpows_.has(objId, obj);
  143. }
  144. #endif
  145. static bool LoggingEnabled() { return sLoggingEnabled; }
  146. static bool StackLoggingEnabled() { return sStackLoggingEnabled; }
  147. friend class Logging;
  148. virtual bool isParent() = 0;
  149. virtual JSObject* scopeForTargetObjects() = 0;
  150. protected:
  151. uintptr_t refcount_;
  152. IdToObjectMap objects_;
  153. IdToObjectMap cpows_;
  154. uint64_t nextSerialNumber_;
  155. // nextCPOWNumber_ should be the value of nextSerialNumber_ in the other
  156. // process. The next new CPOW we get should have this serial number.
  157. uint64_t nextCPOWNumber_;
  158. // CPOW references can be weak, and any object we store in a map may be
  159. // GCed (at which point the CPOW will report itself "dead" to the owner).
  160. // This means that we don't want to store any js::Wrappers in the CPOW map,
  161. // because CPOW will die if the wrapper is GCed, even if the underlying
  162. // object is still alive.
  163. //
  164. // This presents a tricky situation for Xray waivers, since they're normally
  165. // represented as a special same-compartment wrapper. We have to strip them
  166. // off before putting them in the id-to-object and object-to-id maps, so we
  167. // need a way of distinguishing them at lookup-time.
  168. //
  169. // For the id-to-object map, we encode waiver-or-not information into the id
  170. // itself, which lets us do the right thing when accessing the object.
  171. //
  172. // For the object-to-id map, we just keep two maps, one for each type.
  173. ObjectToIdMap unwaivedObjectIds_;
  174. ObjectToIdMap waivedObjectIds_;
  175. ObjectToIdMap& objectIdMap(bool waiver) {
  176. return waiver ? waivedObjectIds_ : unwaivedObjectIds_;
  177. }
  178. static bool sLoggingInitialized;
  179. static bool sLoggingEnabled;
  180. static bool sStackLoggingEnabled;
  181. };
  182. } // namespace jsipc
  183. } // namespace mozilla
  184. #endif