1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=8 et :
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla Plugin App.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Josh Aas <josh@mozilla.com>
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "mozilla/PluginPRLibrary.h"
40 : // Some plugins on Windows, notably Quake Live, implement NP_Initialize using
41 : // cdecl instead of the documented stdcall. In order to work around this,
42 : // we force the caller to use a frame pointer.
43 : #if defined(XP_WIN) && defined(_M_IX86)
44 : #include <malloc.h>
45 :
46 : // gNotOptimized exists so that the compiler will not optimize the alloca
47 : // below.
48 : static int gNotOptimized;
49 : #define CALLING_CONVENTION_HACK void* foo = _alloca(gNotOptimized);
50 : #else
51 : #define CALLING_CONVENTION_HACK
52 : #endif
53 :
54 : #ifdef MOZ_WIDGET_ANDROID
55 : #include "AndroidBridge.h"
56 : #include "android_npapi.h"
57 : #include <android/log.h>
58 : #define ALOG(args...) __android_log_print(ANDROID_LOG_INFO, "GeckoJavaEnv", ## args)
59 : #endif
60 :
61 : namespace mozilla {
62 : #ifdef MOZ_WIDGET_ANDROID
63 : nsresult
64 : PluginPRLibrary::NP_Initialize(NPNetscapeFuncs* bFuncs,
65 : NPPluginFuncs* pFuncs, NPError* error)
66 : {
67 : JNIEnv* env = GetJNIForThread();
68 : if (!env)
69 : return NS_ERROR_FAILURE;
70 :
71 : if (mNP_Initialize) {
72 : *error = mNP_Initialize(bFuncs, pFuncs, env);
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, env);
79 : }
80 :
81 : // Save pointers to functions that get called through PluginLibrary itself.
82 : mNPP_New = pFuncs->newp;
83 : mNPP_GetValue = pFuncs->getvalue;
84 : mNPP_ClearSiteData = pFuncs->clearsitedata;
85 : mNPP_GetSitesWithData = pFuncs->getsiteswithdata;
86 : return NS_OK;
87 : }
88 : #elif defined(MOZ_WIDGET_GONK)
89 : nsresult
90 : PluginPRLibrary::NP_Initialize(NPNetscapeFuncs* bFuncs, NPError* error)
91 : {
92 : return NS_OK;
93 : }
94 : #elif defined(XP_UNIX) && !defined(XP_MACOSX)
95 : nsresult
96 0 : PluginPRLibrary::NP_Initialize(NPNetscapeFuncs* bFuncs,
97 : NPPluginFuncs* pFuncs, NPError* error)
98 : {
99 0 : if (mNP_Initialize) {
100 0 : *error = mNP_Initialize(bFuncs, pFuncs);
101 : } else {
102 : NP_InitializeFunc pfNP_Initialize = (NP_InitializeFunc)
103 0 : PR_FindFunctionSymbol(mLibrary, "NP_Initialize");
104 0 : if (!pfNP_Initialize)
105 0 : return NS_ERROR_FAILURE;
106 0 : *error = pfNP_Initialize(bFuncs, pFuncs);
107 : }
108 :
109 :
110 : // Save pointers to functions that get called through PluginLibrary itself.
111 0 : mNPP_New = pFuncs->newp;
112 0 : mNPP_ClearSiteData = pFuncs->clearsitedata;
113 0 : mNPP_GetSitesWithData = pFuncs->getsiteswithdata;
114 0 : return NS_OK;
115 : }
116 : #else
117 : nsresult
118 : PluginPRLibrary::NP_Initialize(NPNetscapeFuncs* bFuncs, NPError* error)
119 : {
120 : CALLING_CONVENTION_HACK
121 :
122 : if (mNP_Initialize) {
123 : *error = mNP_Initialize(bFuncs);
124 : } else {
125 : NP_InitializeFunc pfNP_Initialize = (NP_InitializeFunc)
126 : PR_FindFunctionSymbol(mLibrary, "NP_Initialize");
127 : if (!pfNP_Initialize)
128 : return NS_ERROR_FAILURE;
129 : *error = pfNP_Initialize(bFuncs);
130 : }
131 :
132 : return NS_OK;
133 : }
134 : #endif
135 :
136 : nsresult
137 0 : PluginPRLibrary::NP_Shutdown(NPError* error)
138 : {
139 : CALLING_CONVENTION_HACK
140 :
141 0 : if (mNP_Shutdown) {
142 0 : *error = mNP_Shutdown();
143 : } else {
144 : NP_ShutdownFunc pfNP_Shutdown = (NP_ShutdownFunc)
145 0 : PR_FindFunctionSymbol(mLibrary, "NP_Shutdown");
146 0 : if (!pfNP_Shutdown)
147 0 : return NS_ERROR_FAILURE;
148 0 : *error = pfNP_Shutdown();
149 : }
150 :
151 0 : return NS_OK;
152 : }
153 :
154 : nsresult
155 0 : PluginPRLibrary::NP_GetMIMEDescription(const char** mimeDesc)
156 : {
157 : CALLING_CONVENTION_HACK
158 :
159 0 : if (mNP_GetMIMEDescription) {
160 0 : *mimeDesc = mNP_GetMIMEDescription();
161 : }
162 : else {
163 : NP_GetMIMEDescriptionFunc pfNP_GetMIMEDescription =
164 : (NP_GetMIMEDescriptionFunc)
165 0 : PR_FindFunctionSymbol(mLibrary, "NP_GetMIMEDescription");
166 0 : if (!pfNP_GetMIMEDescription) {
167 0 : *mimeDesc = "";
168 0 : return NS_ERROR_FAILURE;
169 : }
170 0 : *mimeDesc = pfNP_GetMIMEDescription();
171 : }
172 :
173 0 : return NS_OK;
174 : }
175 :
176 : nsresult
177 0 : PluginPRLibrary::NP_GetValue(void *future, NPPVariable aVariable,
178 : void *aValue, NPError* error)
179 : {
180 : #if defined(XP_UNIX) && !defined(XP_MACOSX)
181 0 : if (mNP_GetValue) {
182 0 : *error = mNP_GetValue(future, aVariable, aValue);
183 : } else {
184 0 : NP_GetValueFunc pfNP_GetValue = (NP_GetValueFunc)PR_FindFunctionSymbol(mLibrary, "NP_GetValue");
185 0 : if (!pfNP_GetValue)
186 0 : return NS_ERROR_FAILURE;
187 0 : *error = pfNP_GetValue(future, aVariable, aValue);
188 : }
189 0 : return NS_OK;
190 : #else
191 : return NS_ERROR_NOT_IMPLEMENTED;
192 : #endif
193 : }
194 :
195 : #if defined(XP_WIN) || defined(XP_MACOSX) || defined(XP_OS2)
196 : nsresult
197 : PluginPRLibrary::NP_GetEntryPoints(NPPluginFuncs* pFuncs, NPError* error)
198 : {
199 : CALLING_CONVENTION_HACK
200 :
201 : if (mNP_GetEntryPoints) {
202 : *error = mNP_GetEntryPoints(pFuncs);
203 : } else {
204 : NP_GetEntryPointsFunc pfNP_GetEntryPoints = (NP_GetEntryPointsFunc)
205 : PR_FindFunctionSymbol(mLibrary, "NP_GetEntryPoints");
206 : if (!pfNP_GetEntryPoints)
207 : return NS_ERROR_FAILURE;
208 : *error = pfNP_GetEntryPoints(pFuncs);
209 : }
210 :
211 : // Save pointers to functions that get called through PluginLibrary itself.
212 : mNPP_New = pFuncs->newp;
213 : mNPP_ClearSiteData = pFuncs->clearsitedata;
214 : mNPP_GetSitesWithData = pFuncs->getsiteswithdata;
215 : return NS_OK;
216 : }
217 : #endif
218 :
219 : nsresult
220 0 : PluginPRLibrary::NPP_New(NPMIMEType pluginType, NPP instance,
221 : uint16_t mode, int16_t argc, char* argn[],
222 : char* argv[], NPSavedData* saved,
223 : NPError* error)
224 : {
225 0 : if (!mNPP_New)
226 0 : return NS_ERROR_FAILURE;
227 0 : *error = mNPP_New(pluginType, instance, mode, argc, argn, argv, saved);
228 0 : return NS_OK;
229 : }
230 :
231 : nsresult
232 0 : PluginPRLibrary::NPP_ClearSiteData(const char* site, uint64_t flags,
233 : uint64_t maxAge)
234 : {
235 0 : if (!mNPP_ClearSiteData) {
236 0 : return NS_ERROR_NOT_AVAILABLE;
237 : }
238 :
239 0 : NPError result = mNPP_ClearSiteData(site, flags, maxAge);
240 :
241 0 : switch (result) {
242 : case NPERR_NO_ERROR:
243 0 : return NS_OK;
244 : case NPERR_TIME_RANGE_NOT_SUPPORTED:
245 0 : return NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED;
246 : case NPERR_MALFORMED_SITE:
247 0 : return NS_ERROR_INVALID_ARG;
248 : default:
249 0 : return NS_ERROR_FAILURE;
250 : }
251 : }
252 :
253 : nsresult
254 0 : PluginPRLibrary::NPP_GetSitesWithData(InfallibleTArray<nsCString>& result)
255 : {
256 0 : if (!mNPP_GetSitesWithData) {
257 0 : return NS_ERROR_NOT_AVAILABLE;
258 : }
259 :
260 0 : result.Clear();
261 :
262 0 : char** sites = mNPP_GetSitesWithData();
263 0 : if (!sites) {
264 0 : return NS_OK;
265 : }
266 :
267 0 : char** iterator = sites;
268 0 : while (*iterator) {
269 0 : result.AppendElement(*iterator);
270 0 : NS_Free(*iterator);
271 0 : ++iterator;
272 : }
273 0 : NS_Free(sites);
274 :
275 0 : return NS_OK;
276 : }
277 :
278 : nsresult
279 0 : PluginPRLibrary::AsyncSetWindow(NPP instance, NPWindow* window)
280 : {
281 0 : nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
282 0 : NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
283 0 : return NS_ERROR_NOT_IMPLEMENTED;
284 : }
285 :
286 : #if defined(MOZ_WIDGET_QT) && (MOZ_PLATFORM_MAEMO == 6)
287 : nsresult
288 : PluginPRLibrary::HandleGUIEvent(NPP instance, const nsGUIEvent& anEvent,
289 : bool* handled)
290 : {
291 : nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
292 : NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
293 : return NS_ERROR_NOT_IMPLEMENTED;
294 : }
295 : #endif
296 :
297 : nsresult
298 0 : PluginPRLibrary::GetImageContainer(NPP instance, ImageContainer** aContainer)
299 : {
300 0 : return NS_ERROR_NOT_IMPLEMENTED;
301 : }
302 :
303 : #if defined(XP_MACOSX)
304 : nsresult
305 : PluginPRLibrary::IsRemoteDrawingCoreAnimation(NPP instance, bool *aDrawing)
306 : {
307 : nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
308 : NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
309 : *aDrawing = false;
310 : return NS_OK;
311 : }
312 : #endif
313 :
314 : nsresult
315 0 : PluginPRLibrary::GetImageSize(NPP instance, nsIntSize* aSize)
316 : {
317 0 : return NS_ERROR_NOT_IMPLEMENTED;
318 : }
319 :
320 : nsresult
321 0 : PluginPRLibrary::SetBackgroundUnknown(NPP instance)
322 : {
323 0 : nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
324 0 : NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
325 0 : NS_ERROR("Unexpected use of async APIs for in-process plugin.");
326 0 : return NS_ERROR_NOT_IMPLEMENTED;
327 : }
328 :
329 : nsresult
330 0 : PluginPRLibrary::BeginUpdateBackground(NPP instance,
331 : const nsIntRect&, gfxContext** aCtx)
332 : {
333 0 : nsNPAPIPluginInstance* inst = (nsNPAPIPluginInstance*)instance->ndata;
334 0 : NS_ENSURE_TRUE(inst, NS_ERROR_NULL_POINTER);
335 0 : NS_ERROR("Unexpected use of async APIs for in-process plugin.");
336 0 : *aCtx = nsnull;
337 0 : return NS_OK;
338 : }
339 :
340 : nsresult
341 0 : PluginPRLibrary::EndUpdateBackground(NPP instance,
342 : gfxContext*, const nsIntRect&)
343 : {
344 0 : NS_RUNTIMEABORT("This should never be called");
345 0 : return NS_ERROR_NOT_AVAILABLE;
346 : }
347 :
348 : } // namespace mozilla
|