michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sw=4 et tw=80: michael@0: * 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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_jsipc_JavaScriptShared_h__ michael@0: #define mozilla_jsipc_JavaScriptShared_h__ michael@0: michael@0: #include "mozilla/dom/DOMTypes.h" michael@0: #include "mozilla/jsipc/PJavaScript.h" michael@0: #include "nsJSUtils.h" michael@0: #include "nsFrameMessageManager.h" michael@0: michael@0: namespace mozilla { michael@0: namespace jsipc { michael@0: michael@0: typedef uint64_t ObjectId; michael@0: michael@0: class JavaScriptShared; michael@0: michael@0: class CpowIdHolder : public CpowHolder michael@0: { michael@0: public: michael@0: CpowIdHolder(JavaScriptShared *js, const InfallibleTArray &cpows) michael@0: : js_(js), michael@0: cpows_(cpows) michael@0: { michael@0: } michael@0: michael@0: bool ToObject(JSContext *cx, JS::MutableHandleObject objp); michael@0: michael@0: private: michael@0: JavaScriptShared *js_; michael@0: const InfallibleTArray &cpows_; michael@0: }; michael@0: michael@0: // Map ids -> JSObjects michael@0: class ObjectStore michael@0: { michael@0: typedef js::DefaultHasher TableKeyHasher; michael@0: michael@0: typedef js::HashMap, TableKeyHasher, js::SystemAllocPolicy> ObjectTable; michael@0: michael@0: public: michael@0: ObjectStore(); michael@0: michael@0: bool init(); michael@0: void trace(JSTracer *trc); michael@0: michael@0: bool add(ObjectId id, JSObject *obj); michael@0: JSObject *find(ObjectId id); michael@0: void remove(ObjectId id); michael@0: michael@0: private: michael@0: ObjectTable table_; michael@0: }; michael@0: michael@0: // Map JSObjects -> ids michael@0: class ObjectIdCache michael@0: { michael@0: typedef js::PointerHasher Hasher; michael@0: typedef js::HashMap ObjectIdTable; michael@0: michael@0: public: michael@0: ObjectIdCache(); michael@0: ~ObjectIdCache(); michael@0: michael@0: bool init(); michael@0: void trace(JSTracer *trc); michael@0: michael@0: bool add(JSContext *cx, JSObject *obj, ObjectId id); michael@0: ObjectId find(JSObject *obj); michael@0: void remove(JSObject *obj); michael@0: michael@0: private: michael@0: static void keyMarkCallback(JSTracer *trc, JSObject *key, void *data); michael@0: michael@0: ObjectIdTable *table_; michael@0: }; michael@0: michael@0: class JavaScriptShared michael@0: { michael@0: public: michael@0: bool init(); michael@0: michael@0: static const uint32_t OBJECT_EXTRA_BITS = 1; michael@0: static const uint32_t OBJECT_IS_CALLABLE = (1 << 0); michael@0: michael@0: bool Unwrap(JSContext *cx, const InfallibleTArray &aCpows, JS::MutableHandleObject objp); michael@0: bool Wrap(JSContext *cx, JS::HandleObject aObj, InfallibleTArray *outCpows); michael@0: michael@0: protected: michael@0: bool toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to); michael@0: bool toValue(JSContext *cx, const JSVariant &from, JS::MutableHandleValue to); michael@0: bool fromDescriptor(JSContext *cx, JS::Handle desc, PPropertyDescriptor *out); michael@0: bool toDescriptor(JSContext *cx, const PPropertyDescriptor &in, michael@0: JS::MutableHandle out); michael@0: bool convertIdToGeckoString(JSContext *cx, JS::HandleId id, nsString *to); michael@0: bool convertGeckoStringToId(JSContext *cx, const nsString &from, JS::MutableHandleId id); michael@0: michael@0: bool toValue(JSContext *cx, const JSVariant &from, jsval *to) { michael@0: JS::RootedValue v(cx); michael@0: if (!toValue(cx, from, &v)) michael@0: return false; michael@0: *to = v; michael@0: return true; michael@0: } michael@0: michael@0: virtual bool makeId(JSContext *cx, JSObject *obj, ObjectId *idp) = 0; michael@0: virtual JSObject *unwrap(JSContext *cx, ObjectId id) = 0; michael@0: michael@0: bool unwrap(JSContext *cx, ObjectId id, JS::MutableHandle objp) { michael@0: if (!id) { michael@0: objp.set(nullptr); michael@0: return true; michael@0: } michael@0: michael@0: objp.set(unwrap(cx, id)); michael@0: return bool(objp.get()); michael@0: } michael@0: michael@0: static void ConvertID(const nsID &from, JSIID *to); michael@0: static void ConvertID(const JSIID &from, nsID *to); michael@0: michael@0: JSObject *findObject(uint32_t objId) { michael@0: return objects_.find(objId); michael@0: } michael@0: michael@0: protected: michael@0: ObjectStore objects_; michael@0: }; michael@0: michael@0: // Use 47 at most, to be safe, since jsval privates are encoded as doubles. michael@0: static const uint64_t MAX_CPOW_IDS = (uint64_t(1) << 47) - 1; michael@0: michael@0: } // namespace jsipc michael@0: } // namespace mozilla michael@0: michael@0: #endif