1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome_frame/np_browser_functions.h"
7 #include "base/logging.h"
11 // global function pointers (within this namespace) for the NPN functions.
18 void set_version(uint16 version
) {
19 v
.minor
= version
& 0xFF;
20 v
.major
= version
>> 8;
23 uint16
version() const {
24 return v
.minor
| (v
.major
<< 8);
28 NpVersion g_version
= {0};
30 NPN_GetURLProcPtr g_geturl
= NULL
;
31 NPN_PostURLProcPtr g_posturl
= NULL
;
32 NPN_RequestReadProcPtr g_requestread
= NULL
;
33 NPN_NewStreamProcPtr g_newstream
= NULL
;
34 NPN_WriteProcPtr g_write
= NULL
;
35 NPN_DestroyStreamProcPtr g_destroystream
= NULL
;
36 NPN_StatusProcPtr g_status
= NULL
;
37 NPN_UserAgentProcPtr g_useragent
= NULL
;
38 NPN_MemAllocProcPtr g_memalloc
= NULL
;
39 NPN_MemFreeProcPtr g_memfree
= NULL
;
40 NPN_MemFlushProcPtr g_memflush
= NULL
;
41 NPN_ReloadPluginsProcPtr g_reloadplugins
= NULL
;
42 NPN_GetJavaEnvProcPtr g_getJavaEnv
= NULL
;
43 NPN_GetJavaPeerProcPtr g_getJavaPeer
= NULL
;
44 NPN_GetURLNotifyProcPtr g_geturlnotify
= NULL
;
45 NPN_PostURLNotifyProcPtr g_posturlnotify
= NULL
;
46 NPN_GetValueProcPtr g_getvalue
= NULL
;
47 NPN_SetValueProcPtr g_setvalue
= NULL
;
48 NPN_InvalidateRectProcPtr g_invalidaterect
= NULL
;
49 NPN_InvalidateRegionProcPtr g_invalidateregion
= NULL
;
50 NPN_ForceRedrawProcPtr g_forceredraw
= NULL
;
51 NPN_GetStringIdentifierProcPtr g_getstringidentifier
= NULL
;
52 NPN_GetStringIdentifiersProcPtr g_getstringidentifiers
= NULL
;
53 NPN_GetIntIdentifierProcPtr g_getintidentifier
= NULL
;
54 NPN_IdentifierIsStringProcPtr g_identifierisstring
= NULL
;
55 NPN_UTF8FromIdentifierProcPtr g_utf8fromidentifier
= NULL
;
56 NPN_IntFromIdentifierProcPtr g_intfromidentifier
= NULL
;
57 NPN_CreateObjectProcPtr g_createobject
= NULL
;
58 NPN_RetainObjectProcPtr g_retainobject
= NULL
;
59 NPN_ReleaseObjectProcPtr g_releaseobject
= NULL
;
60 NPN_InvokeProcPtr g_invoke
= NULL
;
61 NPN_InvokeDefaultProcPtr g_invoke_default
= NULL
;
62 NPN_EvaluateProcPtr g_evaluate
= NULL
;
63 NPN_GetPropertyProcPtr g_getproperty
= NULL
;
64 NPN_SetPropertyProcPtr g_setproperty
= NULL
;
65 NPN_RemovePropertyProcPtr g_removeproperty
= NULL
;
66 NPN_HasPropertyProcPtr g_hasproperty
= NULL
;
67 NPN_HasMethodProcPtr g_hasmethod
= NULL
;
68 NPN_ReleaseVariantValueProcPtr g_releasevariantvalue
= NULL
;
69 NPN_SetExceptionProcPtr g_setexception
= NULL
;
70 NPN_PushPopupsEnabledStateProcPtr g_pushpopupsenabledstate
= NULL
;
71 NPN_PopPopupsEnabledStateProcPtr g_poppopupsenabledstate
= NULL
;
72 NPN_EnumerateProcPtr g_enumerate
= NULL
;
73 NPN_PluginThreadAsyncCallProcPtr g_pluginthreadasynccall
= NULL
;
74 NPN_ConstructProcPtr g_construct
= NULL
;
75 NPN_GetValueForURLPtr g_getvalueforurl
= NULL
;
76 NPN_SetValueForURLPtr g_setvalueforurl
= NULL
;
77 NPN_GetAuthenticationInfoPtr g_getauthenticationinfo
= NULL
;
79 // Must be called prior to calling any of the browser functions below.
80 void InitializeBrowserFunctions(NPNetscapeFuncs
* functions
) {
82 DCHECK(g_geturl
== NULL
|| g_geturl
== functions
->geturl
);
84 g_version
.set_version(functions
->version
);
86 g_geturl
= functions
->geturl
;
87 g_posturl
= functions
->posturl
;
88 g_requestread
= functions
->requestread
;
89 g_newstream
= functions
->newstream
;
90 g_write
= functions
->write
;
91 g_destroystream
= functions
->destroystream
;
92 g_status
= functions
->status
;
93 g_useragent
= functions
->uagent
;
94 g_memalloc
= functions
->memalloc
;
95 g_memfree
= functions
->memfree
;
96 g_memflush
= functions
->memflush
;
97 g_reloadplugins
= functions
->reloadplugins
;
98 g_getJavaEnv
= functions
->getJavaEnv
;
99 g_getJavaPeer
= functions
->getJavaPeer
;
100 g_geturlnotify
= functions
->geturlnotify
;
101 g_posturlnotify
= functions
->posturlnotify
;
102 g_getvalue
= functions
->getvalue
;
103 g_setvalue
= functions
->setvalue
;
104 g_invalidaterect
= functions
->invalidaterect
;
105 g_invalidateregion
= functions
->invalidateregion
;
106 g_forceredraw
= functions
->forceredraw
;
107 g_getstringidentifier
= functions
->getstringidentifier
;
108 g_getstringidentifiers
= functions
->getstringidentifiers
;
109 g_getintidentifier
= functions
->getintidentifier
;
110 g_identifierisstring
= functions
->identifierisstring
;
111 g_utf8fromidentifier
= functions
->utf8fromidentifier
;
112 g_intfromidentifier
= functions
->intfromidentifier
;
113 g_createobject
= functions
->createobject
;
114 g_retainobject
= functions
->retainobject
;
115 g_releaseobject
= functions
->releaseobject
;
116 g_invoke
= functions
->invoke
;
117 g_invoke_default
= functions
->invokeDefault
;
118 g_evaluate
= functions
->evaluate
;
119 g_getproperty
= functions
->getproperty
;
120 g_setproperty
= functions
->setproperty
;
121 g_removeproperty
= functions
->removeproperty
;
122 g_hasproperty
= functions
->hasproperty
;
123 g_hasmethod
= functions
->hasmethod
;
124 g_releasevariantvalue
= functions
->releasevariantvalue
;
125 g_setexception
= functions
->setexception
;
126 g_pushpopupsenabledstate
= functions
->pushpopupsenabledstate
;
127 g_poppopupsenabledstate
= functions
->poppopupsenabledstate
;
128 g_enumerate
= functions
->enumerate
;
129 g_pluginthreadasynccall
= functions
->pluginthreadasynccall
;
130 g_construct
= functions
->construct
;
132 if (g_version
.v
.minor
>= NPVERS_HAS_URL_AND_AUTH_INFO
) {
133 g_getvalueforurl
= functions
->getvalueforurl
;
134 g_setvalueforurl
= functions
->setvalueforurl
;
135 g_getauthenticationinfo
= functions
->getauthenticationinfo
;
139 void UninitializeBrowserFunctions() {
140 g_version
.set_version(0);
142 // We skip doing this in the official build as it doesn't serve much purpose
143 // during shutdown. The reason for it being here in the other types of builds
144 // is to spot potential browser bugs whereby the browser leaves living objects
145 // in our DLL after shutdown has been called. In theory those objects could
146 // trigger a call to the browser functions after shutdown has been called
147 // and for non official builds we want that to simply crash.
148 // For official builds we leave the function pointers around since they
149 // continue to valid.
152 g_requestread
= NULL
;
155 g_destroystream
= NULL
;
161 g_reloadplugins
= NULL
;
163 g_getJavaPeer
= NULL
;
164 g_geturlnotify
= NULL
;
165 g_posturlnotify
= NULL
;
168 g_invalidaterect
= NULL
;
169 g_invalidateregion
= NULL
;
170 g_forceredraw
= NULL
;
171 g_getstringidentifier
= NULL
;
172 g_getstringidentifiers
= NULL
;
173 g_getintidentifier
= NULL
;
174 g_identifierisstring
= NULL
;
175 g_utf8fromidentifier
= NULL
;
176 g_intfromidentifier
= NULL
;
177 g_createobject
= NULL
;
178 g_retainobject
= NULL
;
179 g_releaseobject
= NULL
;
181 g_invoke_default
= NULL
;
183 g_getproperty
= NULL
;
184 g_setproperty
= NULL
;
185 g_removeproperty
= NULL
;
186 g_hasproperty
= NULL
;
188 g_releasevariantvalue
= NULL
;
189 g_setexception
= NULL
;
190 g_pushpopupsenabledstate
= NULL
;
191 g_poppopupsenabledstate
= NULL
;
193 g_pluginthreadasynccall
= NULL
;
195 g_getvalueforurl
= NULL
;
196 g_setvalueforurl
= NULL
;
197 g_getauthenticationinfo
= NULL
;
200 bool IsInitialized() {
201 // We only check one function for convenience.
202 return g_getvalue
!= NULL
;
205 // Function stubs for functions that the host browser implements.
206 uint8
VersionMinor() {
207 return g_version
.v
.minor
;
210 uint8
VersionMajor() {
211 return g_version
.v
.major
;
214 NPError
GetURL(NPP instance
, const char* URL
, const char* window
) {
215 DCHECK(IsInitialized()) << __FUNCTION__
;
216 return g_geturl(instance
, URL
, window
);
219 NPError
PostURL(NPP instance
, const char* URL
, const char* window
, uint32 len
,
220 const char* buf
, NPBool file
) {
221 DCHECK(IsInitialized()) << __FUNCTION__
;
222 return g_posturl(instance
, URL
, window
, len
, buf
, file
);
225 NPError
RequestRead(NPStream
* stream
, NPByteRange
* rangeList
) {
226 DCHECK(IsInitialized()) << __FUNCTION__
;
227 return g_requestread(stream
, rangeList
);
230 NPError
NewStream(NPP instance
, NPMIMEType type
, const char* window
,
232 DCHECK(IsInitialized()) << __FUNCTION__
;
233 return g_newstream(instance
, type
, window
, stream
);
236 int32
Write(NPP instance
, NPStream
* stream
, int32 len
, void* buffer
) {
237 DCHECK(IsInitialized()) << __FUNCTION__
;
238 return g_write(instance
, stream
, len
, buffer
);
241 NPError
DestroyStream(NPP instance
, NPStream
* stream
, NPReason reason
) {
242 DCHECK(IsInitialized()) << __FUNCTION__
;
243 return g_destroystream(instance
, stream
, reason
);
246 void Status(NPP instance
, const char* message
) {
247 DCHECK(IsInitialized()) << __FUNCTION__
;
248 return g_status(instance
, message
);
251 const char* UserAgent(NPP instance
) {
252 DCHECK(IsInitialized()) << __FUNCTION__
;
253 return g_useragent(instance
);
256 void* MemAlloc(uint32 size
) {
257 DCHECK(IsInitialized()) << __FUNCTION__
;
258 return g_memalloc(size
);
261 void MemFree(void* ptr
) {
262 DCHECK(IsInitialized()) << __FUNCTION__
;
263 return g_memfree(ptr
);
266 uint32
MemFlush(uint32 size
) {
267 DCHECK(IsInitialized()) << __FUNCTION__
;
268 return g_memflush(size
);
271 void ReloadPlugins(NPBool reloadPages
) {
272 DCHECK(IsInitialized()) << __FUNCTION__
;
273 return g_reloadplugins(reloadPages
);
277 DCHECK(IsInitialized()) << __FUNCTION__
;
278 return g_getJavaEnv();
281 void* GetJavaPeer(NPP instance
) {
282 DCHECK(IsInitialized()) << __FUNCTION__
;
283 return g_getJavaPeer(instance
);
286 NPError
GetURLNotify(NPP instance
, const char* URL
, const char* window
,
288 DCHECK(IsInitialized()) << __FUNCTION__
;
289 return g_geturlnotify(instance
, URL
, window
, notifyData
);
292 NPError
PostURLNotify(NPP instance
, const char* URL
, const char* window
,
293 uint32 len
, const char* buf
, NPBool file
,
295 DCHECK(IsInitialized()) << __FUNCTION__
;
296 return g_posturlnotify(instance
, URL
, window
, len
, buf
, file
, notifyData
);
299 NPError
GetValue(NPP instance
, NPNVariable variable
, void* ret_value
) {
300 DCHECK(IsInitialized()) << __FUNCTION__
;
301 return g_getvalue(instance
, variable
, ret_value
);
304 NPError
SetValue(NPP instance
, NPPVariable variable
, void* value
) {
305 DCHECK(IsInitialized()) << __FUNCTION__
;
306 return g_setvalue(instance
, variable
, value
);
309 void InvalidateRect(NPP instance
, NPRect
* rect
) {
310 DCHECK(IsInitialized()) << __FUNCTION__
;
311 return g_invalidaterect(instance
, rect
);
314 void InvalidateRegion(NPP instance
, NPRegion region
) {
315 DCHECK(IsInitialized()) << __FUNCTION__
;
316 return g_invalidateregion(instance
, region
);
319 void ForceRedraw(NPP instance
) {
320 DCHECK(IsInitialized()) << __FUNCTION__
;
321 return g_forceredraw(instance
);
324 void ReleaseVariantValue(NPVariant
* variant
) {
325 DCHECK(IsInitialized()) << __FUNCTION__
;
326 return g_releasevariantvalue(variant
);
329 NPIdentifier
GetStringIdentifier(const NPUTF8
* name
) {
330 DCHECK(IsInitialized()) << __FUNCTION__
;
331 return g_getstringidentifier(name
);
334 void GetStringIdentifiers(const NPUTF8
** names
, int nameCount
,
335 NPIdentifier
* identifiers
) {
336 DCHECK(IsInitialized()) << __FUNCTION__
;
337 return g_getstringidentifiers(names
, nameCount
, identifiers
);
340 NPIdentifier
GetIntIdentifier(int32_t intid
) {
341 DCHECK(IsInitialized()) << __FUNCTION__
;
342 return g_getintidentifier(intid
);
345 int32_t IntFromIdentifier(NPIdentifier identifier
) {
346 DCHECK(IsInitialized()) << __FUNCTION__
;
347 return g_intfromidentifier(identifier
);
350 bool IdentifierIsString(NPIdentifier identifier
) {
351 DCHECK(IsInitialized()) << __FUNCTION__
;
352 return g_identifierisstring(identifier
);
356 NPUTF8
* UTF8FromIdentifier(NPIdentifier identifier
) {
357 DCHECK(IsInitialized()) << __FUNCTION__
;
358 return g_utf8fromidentifier(identifier
);
362 NPObject
* CreateObject(NPP instance
, NPClass
* aClass
) {
363 DCHECK(IsInitialized()) << __FUNCTION__
;
364 return g_createobject(instance
, aClass
);
368 NPObject
* RetainObject(NPObject
* obj
) {
369 DCHECK(IsInitialized()) << __FUNCTION__
;
370 return g_retainobject(obj
);
374 void ReleaseObject(NPObject
* obj
) {
375 DCHECK(IsInitialized()) << __FUNCTION__
;
376 return g_releaseobject(obj
);
380 bool Invoke(NPP npp
, NPObject
* obj
, NPIdentifier methodName
,
381 const NPVariant
* args
, unsigned argCount
, NPVariant
* result
) {
382 DCHECK(IsInitialized()) << __FUNCTION__
;
383 return g_invoke(npp
, obj
, methodName
, args
, argCount
, result
);
386 bool InvokeDefault(NPP npp
, NPObject
* obj
, const NPVariant
* args
,
387 unsigned argCount
, NPVariant
* result
) {
388 DCHECK(IsInitialized()) << __FUNCTION__
;
389 return g_invoke_default(npp
, obj
, args
, argCount
, result
);
392 bool Evaluate(NPP npp
, NPObject
* obj
, NPString
* script
, NPVariant
* result
) {
393 DCHECK(IsInitialized()) << __FUNCTION__
;
394 return g_evaluate(npp
, obj
, script
, result
);
397 bool GetProperty(NPP npp
, NPObject
* obj
, NPIdentifier propertyName
,
399 DCHECK(IsInitialized()) << __FUNCTION__
;
400 return g_getproperty(npp
, obj
, propertyName
, result
);
403 bool SetProperty(NPP npp
, NPObject
* obj
, NPIdentifier propertyName
,
404 const NPVariant
* value
) {
405 DCHECK(IsInitialized()) << __FUNCTION__
;
406 return g_setproperty(npp
, obj
, propertyName
, value
);
409 bool HasProperty(NPP npp
, NPObject
* npobj
, NPIdentifier propertyName
) {
410 DCHECK(IsInitialized()) << __FUNCTION__
;
411 return g_hasproperty(npp
, npobj
, propertyName
);
414 bool HasMethod(NPP npp
, NPObject
* npobj
, NPIdentifier methodName
) {
415 DCHECK(IsInitialized()) << __FUNCTION__
;
416 return g_hasmethod(npp
, npobj
, methodName
);
419 bool RemoveProperty(NPP npp
, NPObject
* obj
, NPIdentifier propertyName
) {
420 DCHECK(IsInitialized()) << __FUNCTION__
;
421 return g_removeproperty(npp
, obj
, propertyName
);
424 void SetException(NPObject
* obj
, const NPUTF8
* message
) {
425 DCHECK(IsInitialized()) << __FUNCTION__
;
426 return g_setexception(obj
, message
);
429 void PushPopupsEnabledState(NPP npp
, NPBool enabled
) {
430 DCHECK(IsInitialized()) << __FUNCTION__
;
431 return g_pushpopupsenabledstate(npp
, enabled
);
434 void PopPopupsEnabledState(NPP npp
) {
435 DCHECK(IsInitialized()) << __FUNCTION__
;
436 return g_poppopupsenabledstate(npp
);
439 bool Enumerate(NPP npp
, NPObject
* obj
, NPIdentifier
** identifier
,
441 DCHECK(IsInitialized()) << __FUNCTION__
;
442 return g_enumerate(npp
, obj
, identifier
, count
);
445 void PluginThreadAsyncCall(NPP instance
, void (*func
)(void*), void* userData
) {
446 DCHECK(IsInitialized()) << __FUNCTION__
;
447 return g_pluginthreadasynccall(instance
, func
, userData
);
450 bool Construct(NPP npp
, NPObject
* obj
, const NPVariant
* args
, uint32_t argCount
,
452 DCHECK(IsInitialized()) << __FUNCTION__
;
453 return g_construct(npp
, obj
, args
, argCount
, result
);
456 NPError
GetValueForURL(NPP instance
, NPNURLVariable variable
, const char* url
,
457 char** value
, uint32
* len
) {
458 DCHECK(IsInitialized()) << __FUNCTION__
;
459 DCHECK(npapi::VersionMinor() >= NPVERS_HAS_URL_AND_AUTH_INFO
);
460 if (!g_getvalueforurl
) {
462 return NPERR_INCOMPATIBLE_VERSION_ERROR
;
465 return g_getvalueforurl(instance
, variable
, url
, value
, len
);
468 NPError
SetValueForURL(NPP instance
, NPNURLVariable variable
, const char* url
,
469 const char* value
, uint32 len
) {
470 DCHECK(IsInitialized()) << __FUNCTION__
;
471 DCHECK(npapi::VersionMinor() >= NPVERS_HAS_URL_AND_AUTH_INFO
);
472 if (!g_setvalueforurl
) {
474 return NPERR_INCOMPATIBLE_VERSION_ERROR
;
477 return g_setvalueforurl(instance
, variable
, url
, value
, len
);
480 NPError
GetAuthenticationInfo(NPP instance
, const char* protocol
,
481 const char* host
, int32 port
, const char* scheme
,
482 const char *realm
, char** username
, uint32
* ulen
,
483 char** password
, uint32
* plen
) {
484 DCHECK(IsInitialized()) << __FUNCTION__
;
485 DCHECK(npapi::VersionMinor() >= NPVERS_HAS_URL_AND_AUTH_INFO
);
486 if (g_getauthenticationinfo
) {
488 return NPERR_INCOMPATIBLE_VERSION_ERROR
;
491 return g_getauthenticationinfo(instance
, protocol
, host
, port
, scheme
,
492 realm
, username
, ulen
, password
, plen
);
495 std::string
StringFromIdentifier(NPIdentifier identifier
) {
497 NPUTF8
* utf8
= UTF8FromIdentifier(identifier
);
507 void AllocateStringVariant(const std::string
& str
, NPVariant
* var
) {
510 int len
= str
.length();
511 NPUTF8
* buffer
= reinterpret_cast<NPUTF8
*>(npapi::MemAlloc(len
+ 1));
514 memcpy(buffer
, str
.c_str(), len
);
515 STRINGN_TO_NPVARIANT(buffer
, len
, *var
);
517 NULL_TO_NPVARIANT(*var
);