Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/PluginPRLibrary.h"
8 #include "nsNPAPIPluginInstance.h"
10 // Some plugins on Windows, notably Quake Live, implement NP_Initialize using
11 // cdecl instead of the documented stdcall. In order to work around this,
12 // we force the caller to use a frame pointer.
13 #if defined(XP_WIN) && defined(_M_IX86)
14 #include <malloc.h>
16 // gNotOptimized exists so that the compiler will not optimize the alloca
17 // below.
18 static int gNotOptimized;
19 #define CALLING_CONVENTION_HACK void* foo = _alloca(gNotOptimized);
20 #else
21 #define CALLING_CONVENTION_HACK
22 #endif
24 #ifdef MOZ_WIDGET_ANDROID
25 #include "AndroidBridge.h"
26 #include "android_npapi.h"
27 #include <android/log.h>
28 #undef ALOG
29 #define ALOG(args...) __android_log_print(ANDROID_LOG_INFO, "GeckoJavaEnv", ## args)
30 #endif
32 using namespace mozilla::layers;
34 namespace mozilla {
35 #ifdef MOZ_WIDGET_ANDROID
36 nsresult
37 PluginPRLibrary::NP_Initialize(NPNetscapeFuncs* bFuncs,
38 NPPluginFuncs* pFuncs, NPError* error)
39 {
40 JNIEnv* env = GetJNIForThread();
42 mozilla::AutoLocalJNIFrame jniFrame(env);
44 if (mNP_Initialize) {
45 *error = mNP_Initialize(bFuncs, pFuncs, env);
46 } else {
47 NP_InitializeFunc pfNP_Initialize = (NP_InitializeFunc)
48 PR_FindFunctionSymbol(mLibrary, "NP_Initialize");
49 if (!pfNP_Initialize)
50 return NS_ERROR_FAILURE;
51 *error = pfNP_Initialize(bFuncs, pFuncs, env);
52 }
54 // Save pointers to functions that get called through PluginLibrary itself.
55 mNPP_New = pFuncs->newp;
56 mNPP_ClearSiteData = pFuncs->clearsitedata;
57 mNPP_GetSitesWithData = pFuncs->getsiteswithdata;
58 return NS_OK;
59 }
60 #elif defined(MOZ_WIDGET_GONK)
61 nsresult
62 PluginPRLibrary::NP_Initialize(NPNetscapeFuncs* bFuncs, NPError* error)
63 {
64 return NS_OK;
65 }
66 #elif defined(XP_UNIX) && !defined(XP_MACOSX)
67 nsresult
68 PluginPRLibrary::NP_Initialize(NPNetscapeFuncs* bFuncs,
69 NPPluginFuncs* pFuncs, NPError* error)
70 {
71 if (mNP_Initialize) {
72 *error = mNP_Initialize(bFuncs, pFuncs);
73 } else {
74 NP_InitializeFunc pfNP_Initialize = (NP_InitializeFunc)
75 PR_FindFunctionSymbol(mLibrary, "NP_Initialize");
76 if (!pfNP_Initialize)
77 return NS_ERROR_FAILURE;
78 *error = pfNP_Initialize(bFuncs, pFuncs);
79 }
82 // Save pointers to functions that get called through PluginLibrary itself.
83 mNPP_New = pFuncs->newp;
84 mNPP_ClearSiteData = pFuncs->clearsitedata;
85 mNPP_GetSitesWithData = pFuncs->getsiteswithdata;
86 return NS_OK;
87 }
88 #else
89 nsresult
90 PluginPRLibrary::NP_Initialize(NPNetscapeFuncs* bFuncs, NPError* error)
91 {
92 CALLING_CONVENTION_HACK
94 if (mNP_Initialize) {
95 *error = mNP_Initialize(bFuncs);
96 } else {
97 NP_InitializeFunc pfNP_Initialize = (NP_InitializeFunc)
98 PR_FindFunctionSymbol(mLibrary, "NP_Initialize");
99 if (!pfNP_Initialize)
100 return NS_ERROR_FAILURE;
101 *error = pfNP_Initialize(bFuncs);
102 }
104 return NS_OK;
105 }
106 #endif
108 nsresult
109 PluginPRLibrary::NP_Shutdown(NPError* error)
110 {
111 CALLING_CONVENTION_HACK
113 if (mNP_Shutdown) {
114 *error = mNP_Shutdown();
115 } else {
116 NP_ShutdownFunc pfNP_Shutdown = (NP_ShutdownFunc)
117 PR_FindFunctionSymbol(mLibrary, "NP_Shutdown");
118 if (!pfNP_Shutdown)
119 return NS_ERROR_FAILURE;
120 *error = pfNP_Shutdown();
121 }
123 return NS_OK;
124 }
126 nsresult
127 PluginPRLibrary::NP_GetMIMEDescription(const char** mimeDesc)
128 {
129 CALLING_CONVENTION_HACK
131 if (mNP_GetMIMEDescription) {
132 *mimeDesc = mNP_GetMIMEDescription();
133 }
134 else {
135 NP_GetMIMEDescriptionFunc pfNP_GetMIMEDescription =
136 (NP_GetMIMEDescriptionFunc)
137 PR_FindFunctionSymbol(mLibrary, "NP_GetMIMEDescription");
138 if (!pfNP_GetMIMEDescription) {
139 *mimeDesc = "";
140 return NS_ERROR_FAILURE;
141 }
142 *mimeDesc = pfNP_GetMIMEDescription();
143 }
145 return NS_OK;
146 }
148 nsresult
149 PluginPRLibrary::NP_GetValue(void *future, NPPVariable aVariable,
150 void *aValue, NPError* error)
151 {
152 #if defined(XP_UNIX) && !defined(XP_MACOSX)
153 if (mNP_GetValue) {
154 *error = mNP_GetValue(future, aVariable, aValue);
155 } else {
156 NP_GetValueFunc pfNP_GetValue = (NP_GetValueFunc)PR_FindFunctionSymbol(mLibrary, "NP_GetValue");
157 if (!pfNP_GetValue)
158 return NS_ERROR_FAILURE;
159 *error = pfNP_GetValue(future, aVariable, aValue);
160 }
161 return NS_OK;
162 #else
163 return NS_ERROR_NOT_IMPLEMENTED;
164 #endif
165 }
167 #if defined(XP_WIN) || defined(XP_MACOSX)
168 nsresult
169 PluginPRLibrary::NP_GetEntryPoints(NPPluginFuncs* pFuncs, NPError* error)
170 {
171 CALLING_CONVENTION_HACK
173 if (mNP_GetEntryPoints) {
174 *error = mNP_GetEntryPoints(pFuncs);
175 } else {
176 NP_GetEntryPointsFunc pfNP_GetEntryPoints = (NP_GetEntryPointsFunc)
177 PR_FindFunctionSymbol(mLibrary, "NP_GetEntryPoints");
178 if (!pfNP_GetEntryPoints)
179 return NS_ERROR_FAILURE;
180 *error = pfNP_GetEntryPoints(pFuncs);
181 }
183 // Save pointers to functions that get called through PluginLibrary itself.
184 mNPP_New = pFuncs->newp;
185 mNPP_ClearSiteData = pFuncs->clearsitedata;
186 mNPP_GetSitesWithData = pFuncs->getsiteswithdata;
187 return NS_OK;
188 }
189 #endif
191 nsresult
192 PluginPRLibrary::NPP_New(NPMIMEType pluginType, NPP instance,
193 uint16_t mode, int16_t argc, char* argn[],
194 char* argv[], NPSavedData* saved,
195 NPError* error)
196 {
197 if (!mNPP_New)
198 return NS_ERROR_FAILURE;
200 MAIN_THREAD_JNI_REF_GUARD;
201 *error = mNPP_New(pluginType, instance, mode, argc, argn, argv, saved);
202 return NS_OK;
203 }
205 nsresult
206 PluginPRLibrary::NPP_ClearSiteData(const char* site, uint64_t flags,
207 uint64_t maxAge)
208 {
209 if (!mNPP_ClearSiteData) {
210 return NS_ERROR_NOT_AVAILABLE;
211 }
213 MAIN_THREAD_JNI_REF_GUARD;
214 NPError result = mNPP_ClearSiteData(site, flags, maxAge);
216 switch (result) {
217 case NPERR_NO_ERROR:
218 return NS_OK;
219 case NPERR_TIME_RANGE_NOT_SUPPORTED:
220 return NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED;
221 case NPERR_MALFORMED_SITE:
222 return NS_ERROR_INVALID_ARG;
223 default:
224 return NS_ERROR_FAILURE;
225 }
226 }
228 nsresult
229 PluginPRLibrary::NPP_GetSitesWithData(InfallibleTArray<nsCString>& result)
230 {
231 if (!mNPP_GetSitesWithData) {
232 return NS_ERROR_NOT_AVAILABLE;
233 }
235 result.Clear();
237 MAIN_THREAD_JNI_REF_GUARD;
238 char** sites = mNPP_GetSitesWithData();
239 if (!sites) {
240 return NS_OK;
241 }
243 char** iterator = sites;
244 while (*iterator) {
245 result.AppendElement(*iterator);
246 NS_Free(*iterator);
247 ++iterator;
248 }
249 NS_Free(sites);
251 return NS_OK;
252 }
254 nsresult
255 PluginPRLibrary::AsyncSetWindow(NPP instance, NPWindow* window)
256 {
257 nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
258 NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
259 return NS_ERROR_NOT_IMPLEMENTED;
260 }
262 nsresult
263 PluginPRLibrary::GetImageContainer(NPP instance, ImageContainer** aContainer)
264 {
265 return NS_ERROR_NOT_IMPLEMENTED;
266 }
268 #if defined(XP_MACOSX)
269 nsresult
270 PluginPRLibrary::IsRemoteDrawingCoreAnimation(NPP instance, bool *aDrawing)
271 {
272 nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
273 NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
274 *aDrawing = false;
275 return NS_OK;
276 }
277 nsresult
278 PluginPRLibrary::ContentsScaleFactorChanged(NPP instance, double aContentsScaleFactor)
279 {
280 nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
281 NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
282 return NS_OK;
283 }
284 #endif
286 nsresult
287 PluginPRLibrary::GetImageSize(NPP instance, nsIntSize* aSize)
288 {
289 return NS_ERROR_NOT_IMPLEMENTED;
290 }
292 nsresult
293 PluginPRLibrary::SetBackgroundUnknown(NPP instance)
294 {
295 nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
296 NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
297 NS_ERROR("Unexpected use of async APIs for in-process plugin.");
298 return NS_ERROR_NOT_IMPLEMENTED;
299 }
301 nsresult
302 PluginPRLibrary::BeginUpdateBackground(NPP instance,
303 const nsIntRect&, gfxContext** aCtx)
304 {
305 nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
306 NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
307 NS_ERROR("Unexpected use of async APIs for in-process plugin.");
308 *aCtx = nullptr;
309 return NS_OK;
310 }
312 nsresult
313 PluginPRLibrary::EndUpdateBackground(NPP instance,
314 gfxContext*, const nsIntRect&)
315 {
316 NS_RUNTIMEABORT("This should never be called");
317 return NS_ERROR_NOT_AVAILABLE;
318 }
320 } // namespace mozilla