js/ipc/JavaScriptShared.h

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

     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/. */
     8 #ifndef mozilla_jsipc_JavaScriptShared_h__
     9 #define mozilla_jsipc_JavaScriptShared_h__
    11 #include "mozilla/dom/DOMTypes.h"
    12 #include "mozilla/jsipc/PJavaScript.h"
    13 #include "nsJSUtils.h"
    14 #include "nsFrameMessageManager.h"
    16 namespace mozilla {
    17 namespace jsipc {
    19 typedef uint64_t ObjectId;
    21 class JavaScriptShared;
    23 class CpowIdHolder : public CpowHolder
    24 {
    25   public:
    26     CpowIdHolder(JavaScriptShared *js, const InfallibleTArray<CpowEntry> &cpows)
    27       : js_(js),
    28         cpows_(cpows)
    29     {
    30     }
    32     bool ToObject(JSContext *cx, JS::MutableHandleObject objp);
    34   private:
    35     JavaScriptShared *js_;
    36     const InfallibleTArray<CpowEntry> &cpows_;
    37 };
    39 // Map ids -> JSObjects
    40 class ObjectStore
    41 {
    42     typedef js::DefaultHasher<ObjectId> TableKeyHasher;
    44     typedef js::HashMap<ObjectId, JS::Heap<JSObject *>, TableKeyHasher, js::SystemAllocPolicy> ObjectTable;
    46   public:
    47     ObjectStore();
    49     bool init();
    50     void trace(JSTracer *trc);
    52     bool add(ObjectId id, JSObject *obj);
    53     JSObject *find(ObjectId id);
    54     void remove(ObjectId id);
    56   private:
    57     ObjectTable table_;
    58 };
    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;
    66   public:
    67     ObjectIdCache();
    68     ~ObjectIdCache();
    70     bool init();
    71     void trace(JSTracer *trc);
    73     bool add(JSContext *cx, JSObject *obj, ObjectId id);
    74     ObjectId find(JSObject *obj);
    75     void remove(JSObject *obj);
    77   private:
    78     static void keyMarkCallback(JSTracer *trc, JSObject *key, void *data);
    80     ObjectIdTable *table_;
    81 };
    83 class JavaScriptShared
    84 {
    85   public:
    86     bool init();
    88     static const uint32_t OBJECT_EXTRA_BITS  = 1;
    89     static const uint32_t OBJECT_IS_CALLABLE = (1 << 0);
    91     bool Unwrap(JSContext *cx, const InfallibleTArray<CpowEntry> &aCpows, JS::MutableHandleObject objp);
    92     bool Wrap(JSContext *cx, JS::HandleObject aObj, InfallibleTArray<CpowEntry> *outCpows);
    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);
   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     }
   111     virtual bool makeId(JSContext *cx, JSObject *obj, ObjectId *idp) = 0;
   112     virtual JSObject *unwrap(JSContext *cx, ObjectId id) = 0;
   114     bool unwrap(JSContext *cx, ObjectId id, JS::MutableHandle<JSObject*> objp) {
   115         if (!id) {
   116             objp.set(nullptr);
   117             return true;
   118         }
   120         objp.set(unwrap(cx, id));
   121         return bool(objp.get());
   122     }
   124     static void ConvertID(const nsID &from, JSIID *to);
   125     static void ConvertID(const JSIID &from, nsID *to);
   127     JSObject *findObject(uint32_t objId) {
   128         return objects_.find(objId);
   129     }
   131   protected:
   132     ObjectStore objects_;
   133 };
   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;
   138 } // namespace jsipc
   139 } // namespace mozilla
   141 #endif

mercurial