js/ipc/JavaScriptShared.h

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

mercurial