blob: 89280fabf437ffe67d437a0aa096ca4738fe7208 [file] [log] [blame]
jat@google.com134be542009-08-03 15:30:11 +00001// Copyright 2007, Google Inc.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
5//
6// 1. Redistributions of source code must retain the above copyright notice,
7// this list of conditions and the following disclaimer.
8// 2. Redistributions in binary form must reproduce the above copyright notice,
9// this list of conditions and the following disclaimer in the documentation
10// and/or other materials provided with the distribution.
11// 3. Neither the name of Google Inc. nor the names of its contributors may be
12// used to endorse or promote products derived from this software without
13// specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// Modified from gears/base/npapi/plugin.h
27
28#ifndef _H_NPObjectWrapper
29#define _H_NPObjectWrapper
30
31#include <cstdio>
32#include "mozincludes.h"
33
34// This is a base class for the bridge between the JavaScript engine and the plugin.
35template<class Wrapper>
36class NPObjectWrapper : public NPObject {
37 public:
38 // NPClass callbacks.
39 static NPObject* Allocate(NPP npp, NPClass *npclass) {
40 Wrapper* obj = new Wrapper(npp);
41 return obj;
42 }
43
44 static void Deallocate(NPObject *npobj) {
45 delete static_cast<Wrapper*>(npobj);
46 }
47
48 static bool Enumeration(NPObject *npobj, NPIdentifier** value,
49 uint32_t *count) {
50 Wrapper* obj = static_cast<Wrapper*>(npobj);
51 return obj->enumeration(value, count);
52 }
53
54 static bool HasMethod(NPObject *npobj, NPIdentifier name) {
55 Wrapper* obj = static_cast<Wrapper*>(npobj);
56 return obj->hasMethod(name);
57 }
58
59 static bool Invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args,
60 uint32_t num_args, NPVariant *result) {
61 Wrapper* obj = static_cast<Wrapper*>(npobj);
62 return obj->invoke(name, args, num_args, result);
63 }
64
65 static bool InvokeDefault(NPObject *npobj, const NPVariant *args,
66 uint32_t num_args, NPVariant *result) {
67 Wrapper* obj = static_cast<Wrapper*>(npobj);
68 return obj->invokeDefault(args, num_args, result);
69 }
70
71 static bool HasProperty(NPObject *npobj, NPIdentifier name) {
72 Wrapper* obj = static_cast<Wrapper*>(npobj);
73 return obj->hasProperty(name);
74 }
75
76 static bool GetProperty(NPObject *npobj, NPIdentifier name,
77 NPVariant *result) {
78 Wrapper* obj = static_cast<Wrapper*>(npobj);
79 return obj->getProperty(name, result);
80 }
81
82 static bool SetProperty(NPObject *npobj, NPIdentifier name,
83 const NPVariant *value) {
84 Wrapper* obj = static_cast<Wrapper*>(npobj);
85 return obj->setProperty(name, value);
86 }
87
88 virtual ~NPObjectWrapper() {}
89
90 /**
91 * *value must be memory allocated with NPN_MemAlloc, as the caller will call NPN_MemFree.
92 */
93 virtual bool enumeration(NPIdentifier** value, uint32_t* count) {
94 return false;
95 }
96
97 /**
98 * Caller must release the result value when it no longer needs it,
99 * so implementation must return an extra refcount.
100 */
101 virtual bool getProperty(NPIdentifier name, NPVariant *result) {
102 return false;
103 }
104
105 virtual bool hasMethod(NPIdentifier name) {
106 return false;
107 }
108
109 virtual bool hasProperty(NPIdentifier name) {
110 return false;
111 }
112
113 /**
114 * Caller must release the result value when it no longer needs it,
115 * so implementation must return an extra refcount.
116 */
117 virtual bool invoke(NPIdentifier name, const NPVariant *args,
118 uint32_t num_args, NPVariant *result) {
119 return false;
120 }
121
122 /**
123 * Caller must release the result value when it no longer needs it,
124 * so implementation must return an extra refcount.
125 */
126 virtual bool invokeDefault(const NPVariant *args, uint32_t num_args,
127 NPVariant *result) {
128 return false;
129 }
130
131 virtual bool setProperty(NPIdentifier name, const NPVariant *value) {
132 return false;
133 }
134
135 protected:
136 NPObjectWrapper(NPP instance) : npp(instance) {}
137
138 public:
139 const NPP getNPP() const {
140 return npp;
141 }
142
143 private:
144 NPP npp;
145 DISALLOW_EVIL_CONSTRUCTORS(NPObjectWrapper);
146};
147
148// Get the NPClass for a NPObject wrapper (the type must derive from NPObjectWrapper).
149template<class Wrapper>
150NPClass* GetNPClass() {
151 static NPClass plugin_class = {
152 NP_CLASS_STRUCT_VERSION,
153 NPObjectWrapper<Wrapper>::Allocate,
154 NPObjectWrapper<Wrapper>::Deallocate,
155 NULL, // Invalidate,
156 NPObjectWrapper<Wrapper>::HasMethod,
157 NPObjectWrapper<Wrapper>::Invoke,
158 NPObjectWrapper<Wrapper>::InvokeDefault,
159 NPObjectWrapper<Wrapper>::HasProperty,
160 NPObjectWrapper<Wrapper>::GetProperty,
161 NPObjectWrapper<Wrapper>::SetProperty,
162 NULL, // RemoveProperty,
163 NPObjectWrapper<Wrapper>::Enumeration,
164 };
165
166 return &plugin_class;
167}
168
169#endif // GEARS_BASE_NPAPI_PLUGIN_H__