Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sw=4 et tw=80: |
michael@0 | 3 | * |
michael@0 | 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef mozilla_jsipc_JavaScriptShared_h__ |
michael@0 | 9 | #define mozilla_jsipc_JavaScriptShared_h__ |
michael@0 | 10 | |
michael@0 | 11 | #include "mozilla/dom/DOMTypes.h" |
michael@0 | 12 | #include "mozilla/jsipc/PJavaScript.h" |
michael@0 | 13 | #include "nsJSUtils.h" |
michael@0 | 14 | #include "nsFrameMessageManager.h" |
michael@0 | 15 | |
michael@0 | 16 | namespace mozilla { |
michael@0 | 17 | namespace jsipc { |
michael@0 | 18 | |
michael@0 | 19 | typedef uint64_t ObjectId; |
michael@0 | 20 | |
michael@0 | 21 | class JavaScriptShared; |
michael@0 | 22 | |
michael@0 | 23 | class CpowIdHolder : public CpowHolder |
michael@0 | 24 | { |
michael@0 | 25 | public: |
michael@0 | 26 | CpowIdHolder(JavaScriptShared *js, const InfallibleTArray<CpowEntry> &cpows) |
michael@0 | 27 | : js_(js), |
michael@0 | 28 | cpows_(cpows) |
michael@0 | 29 | { |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | bool ToObject(JSContext *cx, JS::MutableHandleObject objp); |
michael@0 | 33 | |
michael@0 | 34 | private: |
michael@0 | 35 | JavaScriptShared *js_; |
michael@0 | 36 | const InfallibleTArray<CpowEntry> &cpows_; |
michael@0 | 37 | }; |
michael@0 | 38 | |
michael@0 | 39 | // Map ids -> JSObjects |
michael@0 | 40 | class ObjectStore |
michael@0 | 41 | { |
michael@0 | 42 | typedef js::DefaultHasher<ObjectId> TableKeyHasher; |
michael@0 | 43 | |
michael@0 | 44 | typedef js::HashMap<ObjectId, JS::Heap<JSObject *>, TableKeyHasher, js::SystemAllocPolicy> ObjectTable; |
michael@0 | 45 | |
michael@0 | 46 | public: |
michael@0 | 47 | ObjectStore(); |
michael@0 | 48 | |
michael@0 | 49 | bool init(); |
michael@0 | 50 | void trace(JSTracer *trc); |
michael@0 | 51 | |
michael@0 | 52 | bool add(ObjectId id, JSObject *obj); |
michael@0 | 53 | JSObject *find(ObjectId id); |
michael@0 | 54 | void remove(ObjectId id); |
michael@0 | 55 | |
michael@0 | 56 | private: |
michael@0 | 57 | ObjectTable table_; |
michael@0 | 58 | }; |
michael@0 | 59 | |
michael@0 | 60 | // Map JSObjects -> ids |
michael@0 | 61 | class ObjectIdCache |
michael@0 | 62 | { |
michael@0 | 63 | typedef js::PointerHasher<JSObject *, 3> Hasher; |
michael@0 | 64 | typedef js::HashMap<JSObject *, ObjectId, Hasher, js::SystemAllocPolicy> ObjectIdTable; |
michael@0 | 65 | |
michael@0 | 66 | public: |
michael@0 | 67 | ObjectIdCache(); |
michael@0 | 68 | ~ObjectIdCache(); |
michael@0 | 69 | |
michael@0 | 70 | bool init(); |
michael@0 | 71 | void trace(JSTracer *trc); |
michael@0 | 72 | |
michael@0 | 73 | bool add(JSContext *cx, JSObject *obj, ObjectId id); |
michael@0 | 74 | ObjectId find(JSObject *obj); |
michael@0 | 75 | void remove(JSObject *obj); |
michael@0 | 76 | |
michael@0 | 77 | private: |
michael@0 | 78 | static void keyMarkCallback(JSTracer *trc, JSObject *key, void *data); |
michael@0 | 79 | |
michael@0 | 80 | ObjectIdTable *table_; |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | class JavaScriptShared |
michael@0 | 84 | { |
michael@0 | 85 | public: |
michael@0 | 86 | bool init(); |
michael@0 | 87 | |
michael@0 | 88 | static const uint32_t OBJECT_EXTRA_BITS = 1; |
michael@0 | 89 | static const uint32_t OBJECT_IS_CALLABLE = (1 << 0); |
michael@0 | 90 | |
michael@0 | 91 | bool Unwrap(JSContext *cx, const InfallibleTArray<CpowEntry> &aCpows, JS::MutableHandleObject objp); |
michael@0 | 92 | bool Wrap(JSContext *cx, JS::HandleObject aObj, InfallibleTArray<CpowEntry> *outCpows); |
michael@0 | 93 | |
michael@0 | 94 | protected: |
michael@0 | 95 | bool toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to); |
michael@0 | 96 | bool toValue(JSContext *cx, const JSVariant &from, JS::MutableHandleValue to); |
michael@0 | 97 | bool fromDescriptor(JSContext *cx, JS::Handle<JSPropertyDescriptor> desc, PPropertyDescriptor *out); |
michael@0 | 98 | bool toDescriptor(JSContext *cx, const PPropertyDescriptor &in, |
michael@0 | 99 | JS::MutableHandle<JSPropertyDescriptor> out); |
michael@0 | 100 | bool convertIdToGeckoString(JSContext *cx, JS::HandleId id, nsString *to); |
michael@0 | 101 | bool convertGeckoStringToId(JSContext *cx, const nsString &from, JS::MutableHandleId id); |
michael@0 | 102 | |
michael@0 | 103 | bool toValue(JSContext *cx, const JSVariant &from, jsval *to) { |
michael@0 | 104 | JS::RootedValue v(cx); |
michael@0 | 105 | if (!toValue(cx, from, &v)) |
michael@0 | 106 | return false; |
michael@0 | 107 | *to = v; |
michael@0 | 108 | return true; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | virtual bool makeId(JSContext *cx, JSObject *obj, ObjectId *idp) = 0; |
michael@0 | 112 | virtual JSObject *unwrap(JSContext *cx, ObjectId id) = 0; |
michael@0 | 113 | |
michael@0 | 114 | bool unwrap(JSContext *cx, ObjectId id, JS::MutableHandle<JSObject*> objp) { |
michael@0 | 115 | if (!id) { |
michael@0 | 116 | objp.set(nullptr); |
michael@0 | 117 | return true; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | objp.set(unwrap(cx, id)); |
michael@0 | 121 | return bool(objp.get()); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | static void ConvertID(const nsID &from, JSIID *to); |
michael@0 | 125 | static void ConvertID(const JSIID &from, nsID *to); |
michael@0 | 126 | |
michael@0 | 127 | JSObject *findObject(uint32_t objId) { |
michael@0 | 128 | return objects_.find(objId); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | protected: |
michael@0 | 132 | ObjectStore objects_; |
michael@0 | 133 | }; |
michael@0 | 134 | |
michael@0 | 135 | // Use 47 at most, to be safe, since jsval privates are encoded as doubles. |
michael@0 | 136 | static const uint64_t MAX_CPOW_IDS = (uint64_t(1) << 47) - 1; |
michael@0 | 137 | |
michael@0 | 138 | } // namespace jsipc |
michael@0 | 139 | } // namespace mozilla |
michael@0 | 140 | |
michael@0 | 141 | #endif |