summaryrefslogtreecommitdiffstats
path: root/dom/plugins/base/nsJSNPRuntime.h
blob: c1e877f85959375542bb6cf29ab28062b93259f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsJSNPRuntime_h_
#define nsJSNPRuntime_h_

#include "nscore.h"
#include "npapi.h"
#include "npruntime.h"
#include "PLDHashTable.h"
#include "js/RootingAPI.h"

class nsJSNPRuntime {
 public:
  static void OnPluginDestroy(NPP npp);
  static void OnPluginDestroyPending(NPP npp);
};

class nsJSObjWrapperKey {
 public:
  nsJSObjWrapperKey(JSObject* obj, NPP npp) : mJSObj(obj), mNpp(npp) {}

  bool operator==(const nsJSObjWrapperKey& other) const {
    return mJSObj == other.mJSObj && mNpp == other.mNpp;
  }
  bool operator!=(const nsJSObjWrapperKey& other) const {
    return !(*this == other);
  }

  void trace(JSTracer* trc) {
    JS::TraceEdge(trc, &mJSObj, "nsJSObjWrapperKey");
  }

  nsJSObjWrapperKey(nsJSObjWrapperKey&& other) = default;
  nsJSObjWrapperKey& operator=(nsJSObjWrapperKey&& other) = default;

  JS::Heap<JSObject*> mJSObj;
  NPP mNpp;
};

class nsJSObjWrapper : public NPObject {
 public:
  JS::Heap<JSObject*> mJSObj;
  // Because mJSObj might be a cross-compartment wrapper, we can't use it to
  // enter the target realm. We use this global instead (it's always
  // same-compartment with mJSObj).
  JS::Heap<JSObject*> mJSObjGlobal;
  const NPP mNpp;
  bool mDestroyPending;

  static NPObject* GetNewOrUsed(NPP npp, JS::Handle<JSObject*> obj,
                                JS::Handle<JSObject*> objGlobal);

  void trace(JSTracer* trc) {
    JS::TraceEdge(trc, &mJSObj, "nsJSObjWrapper::mJSObj");
    JS::TraceEdge(trc, &mJSObjGlobal, "nsJSObjWrapper::mJSObjGlobal");
  }

 protected:
  explicit nsJSObjWrapper(NPP npp);
  ~nsJSObjWrapper();

  static NPObject* NP_Allocate(NPP npp, NPClass* aClass);
  static void NP_Deallocate(NPObject* obj);
  static void NP_Invalidate(NPObject* obj);
  static bool NP_HasMethod(NPObject*, NPIdentifier identifier);
  static bool NP_Invoke(NPObject* obj, NPIdentifier method,
                        const NPVariant* args, uint32_t argCount,
                        NPVariant* result);
  static bool NP_InvokeDefault(NPObject* obj, const NPVariant* args,
                               uint32_t argCount, NPVariant* result);
  static bool NP_HasProperty(NPObject* obj, NPIdentifier property);
  static bool NP_GetProperty(NPObject* obj, NPIdentifier property,
                             NPVariant* result);
  static bool NP_SetProperty(NPObject* obj, NPIdentifier property,
                             const NPVariant* value);
  static bool NP_RemoveProperty(NPObject* obj, NPIdentifier property);
  static bool NP_Enumerate(NPObject* npobj, NPIdentifier** identifier,
                           uint32_t* count);
  static bool NP_Construct(NPObject* obj, const NPVariant* args,
                           uint32_t argCount, NPVariant* result);

 public:
  static NPClass sJSObjWrapperNPClass;
};

class nsNPObjWrapper {
 public:
  static bool IsWrapper(JSObject* obj);
  static void OnDestroy(NPObject* npobj);
  static JSObject* GetNewOrUsed(NPP npp, JSContext* cx, NPObject* npobj);
};

bool JSValToNPVariant(NPP npp, JSContext* cx, const JS::Value& val,
                      NPVariant* variant);

#endif  // nsJSNPRuntime_h_