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.
20 NpVersion g_version
= {0};
22 NPN_GetURLProcPtr g_geturl
= NULL
;
23 NPN_PostURLProcPtr g_posturl
= NULL
;
24 NPN_RequestReadProcPtr g_requestread
= NULL
;
25 NPN_NewStreamProcPtr g_newstream
= NULL
;
26 NPN_WriteProcPtr g_write
= NULL
;
27 NPN_DestroyStreamProcPtr g_destroystream
= NULL
;
28 NPN_StatusProcPtr g_status
= NULL
;
29 NPN_UserAgentProcPtr g_useragent
= NULL
;
30 NPN_MemAllocProcPtr g_memalloc
= NULL
;
31 NPN_MemFreeProcPtr g_memfree
= NULL
;
32 NPN_MemFlushProcPtr g_memflush
= NULL
;
33 NPN_ReloadPluginsProcPtr g_reloadplugins
= NULL
;
34 NPN_GetJavaEnvProcPtr g_getJavaEnv
= NULL
;
35 NPN_GetJavaPeerProcPtr g_getJavaPeer
= NULL
;
36 NPN_GetURLNotifyProcPtr g_geturlnotify
= NULL
;
37 NPN_PostURLNotifyProcPtr g_posturlnotify
= NULL
;
38 NPN_GetValueProcPtr g_getvalue
= NULL
;
39 NPN_SetValueProcPtr g_setvalue
= NULL
;
40 NPN_InvalidateRectProcPtr g_invalidaterect
= NULL
;
41 NPN_InvalidateRegionProcPtr g_invalidateregion
= NULL
;
42 NPN_ForceRedrawProcPtr g_forceredraw
= NULL
;
43 NPN_GetStringIdentifierProcPtr g_getstringidentifier
= NULL
;
44 NPN_GetStringIdentifiersProcPtr g_getstringidentifiers
= NULL
;
45 NPN_GetIntIdentifierProcPtr g_getintidentifier
= NULL
;
46 NPN_IdentifierIsStringProcPtr g_identifierisstring
= NULL
;
47 NPN_UTF8FromIdentifierProcPtr g_utf8fromidentifier
= NULL
;
48 NPN_IntFromIdentifierProcPtr g_intfromidentifier
= NULL
;
49 NPN_CreateObjectProcPtr g_createobject
= NULL
;
50 NPN_RetainObjectProcPtr g_retainobject
= NULL
;
51 NPN_ReleaseObjectProcPtr g_releaseobject
= NULL
;
52 NPN_InvokeProcPtr g_invoke
= NULL
;
53 NPN_InvokeDefaultProcPtr g_invoke_default
= NULL
;
54 NPN_EvaluateProcPtr g_evaluate
= NULL
;
55 NPN_GetPropertyProcPtr g_getproperty
= NULL
;
56 NPN_SetPropertyProcPtr g_setproperty
= NULL
;
57 NPN_RemovePropertyProcPtr g_removeproperty
= NULL
;
58 NPN_HasPropertyProcPtr g_hasproperty
= NULL
;
59 NPN_HasMethodProcPtr g_hasmethod
= NULL
;
60 NPN_ReleaseVariantValueProcPtr g_releasevariantvalue
= NULL
;
61 NPN_SetExceptionProcPtr g_setexception
= NULL
;
62 NPN_PushPopupsEnabledStateProcPtr g_pushpopupsenabledstate
= NULL
;
63 NPN_PopPopupsEnabledStateProcPtr g_poppopupsenabledstate
= NULL
;
64 NPN_EnumerateProcPtr g_enumerate
= NULL
;
65 NPN_PluginThreadAsyncCallProcPtr g_pluginthreadasynccall
= NULL
;
66 NPN_ConstructProcPtr g_construct
= NULL
;
67 NPN_GetValueForURLPtr g_getvalueforurl
= NULL
;
68 NPN_SetValueForURLPtr g_setvalueforurl
= NULL
;
69 NPN_GetAuthenticationInfoPtr g_getauthenticationinfo
= NULL
;
71 // Must be called prior to calling any of the browser functions below.
72 void InitializeBrowserFunctions(NPNetscapeFuncs
* functions
) {
74 DCHECK(g_geturl
== NULL
|| g_geturl
== functions
->geturl
);
76 g_version
.version
= functions
->version
;
78 g_geturl
= functions
->geturl
;
79 g_posturl
= functions
->posturl
;
80 g_requestread
= functions
->requestread
;
81 g_newstream
= functions
->newstream
;
82 g_write
= functions
->write
;
83 g_destroystream
= functions
->destroystream
;
84 g_status
= functions
->status
;
85 g_useragent
= functions
->uagent
;
86 g_memalloc
= functions
->memalloc
;
87 g_memfree
= functions
->memfree
;
88 g_memflush
= functions
->memflush
;
89 g_reloadplugins
= functions
->reloadplugins
;
90 g_getJavaEnv
= functions
->getJavaEnv
;
91 g_getJavaPeer
= functions
->getJavaPeer
;
92 g_geturlnotify
= functions
->geturlnotify
;
93 g_posturlnotify
= functions
->posturlnotify
;
94 g_getvalue
= functions
->getvalue
;
95 g_setvalue
= functions
->setvalue
;
96 g_invalidaterect
= functions
->invalidaterect
;
97 g_invalidateregion
= functions
->invalidateregion
;
98 g_forceredraw
= functions
->forceredraw
;
99 g_getstringidentifier
= functions
->getstringidentifier
;
100 g_getstringidentifiers
= functions
->getstringidentifiers
;
101 g_getintidentifier
= functions
->getintidentifier
;
102 g_identifierisstring
= functions
->identifierisstring
;
103 g_utf8fromidentifier
= functions
->utf8fromidentifier
;
104 g_intfromidentifier
= functions
->intfromidentifier
;
105 g_createobject
= functions
->createobject
;
106 g_retainobject
= functions
->retainobject
;
107 g_releaseobject
= functions
->releaseobject
;
108 g_invoke
= functions
->invoke
;
109 g_invoke_default
= functions
->invokeDefault
;
110 g_evaluate
= functions
->evaluate
;
111 g_getproperty
= functions
->getproperty
;
112 g_setproperty
= functions
->setproperty
;
113 g_removeproperty
= functions
->removeproperty
;
114 g_hasproperty
= functions
->hasproperty
;
115 g_hasmethod
= functions
->hasmethod
;
116 g_releasevariantvalue
= functions
->releasevariantvalue
;
117 g_setexception
= functions
->setexception
;
118 g_pushpopupsenabledstate
= functions
->pushpopupsenabledstate
;
119 g_poppopupsenabledstate
= functions
->poppopupsenabledstate
;
120 g_enumerate
= functions
->enumerate
;
121 g_pluginthreadasynccall
= functions
->pluginthreadasynccall
;
122 g_construct
= functions
->construct
;
124 if (g_version
.v
.minor
>= NPVERS_HAS_URL_AND_AUTH_INFO
) {
125 g_getvalueforurl
= functions
->getvalueforurl
;
126 g_setvalueforurl
= functions
->setvalueforurl
;
127 g_getauthenticationinfo
= functions
->getauthenticationinfo
;
131 void UninitializeBrowserFunctions() {
132 g_version
.version
= 0;
134 // We skip doing this in the official build as it doesn't serve much purpose
135 // during shutdown. The reason for it being here in the other types of builds
136 // is to spot potential browser bugs whereby the browser leaves living objects
137 // in our DLL after shutdown has been called. In theory those objects could
138 // trigger a call to the browser functions after shutdown has been called
139 // and for non official builds we want that to simply crash.
140 // For official builds we leave the function pointers around since they
141 // continue to valid.
144 g_requestread
= NULL
;
147 g_destroystream
= NULL
;
153 g_reloadplugins
= NULL
;
155 g_getJavaPeer
= NULL
;
156 g_geturlnotify
= NULL
;
157 g_posturlnotify
= NULL
;
160 g_invalidaterect
= NULL
;
161 g_invalidateregion
= NULL
;
162 g_forceredraw
= NULL
;
163 g_getstringidentifier
= NULL
;
164 g_getstringidentifiers
= NULL
;
165 g_getintidentifier
= NULL
;
166 g_identifierisstring
= NULL
;
167 g_utf8fromidentifier
= NULL
;
168 g_intfromidentifier
= NULL
;
169 g_createobject
= NULL
;
170 g_retainobject
= NULL
;
171 g_releaseobject
= NULL
;
173 g_invoke_default
= NULL
;
175 g_getproperty
= NULL
;
176 g_setproperty
= NULL
;
177 g_removeproperty
= NULL
;
178 g_hasproperty
= NULL
;
180 g_releasevariantvalue
= NULL
;
181 g_setexception
= NULL
;
182 g_pushpopupsenabledstate
= NULL
;
183 g_poppopupsenabledstate
= NULL
;
185 g_pluginthreadasynccall
= NULL
;
187 g_getvalueforurl
= NULL
;
188 g_setvalueforurl
= NULL
;
189 g_getauthenticationinfo
= NULL
;
192 bool IsInitialized() {
193 // We only check one function for convenience.
194 return g_getvalue
!= NULL
;
197 // Function stubs for functions that the host browser implements.
198 uint8
VersionMinor() {
199 return g_version
.v
.minor
;
202 uint8
VersionMajor() {
203 return g_version
.v
.major
;
206 NPError
GetURL(NPP instance
, const char* URL
, const char* window
) {
207 DCHECK(IsInitialized()) << __FUNCTION__
;
208 return g_geturl(instance
, URL
, window
);
211 NPError
PostURL(NPP instance
, const char* URL
, const char* window
, uint32 len
,
212 const char* buf
, NPBool file
) {
213 DCHECK(IsInitialized()) << __FUNCTION__
;
214 return g_posturl(instance
, URL
, window
, len
, buf
, file
);
217 NPError
RequestRead(NPStream
* stream
, NPByteRange
* rangeList
) {
218 DCHECK(IsInitialized()) << __FUNCTION__
;
219 return g_requestread(stream
, rangeList
);
222 NPError
NewStream(NPP instance
, NPMIMEType type
, const char* window
,
224 DCHECK(IsInitialized()) << __FUNCTION__
;
225 return g_newstream(instance
, type
, window
, stream
);
228 int32
Write(NPP instance
, NPStream
* stream
, int32 len
, void* buffer
) {
229 DCHECK(IsInitialized()) << __FUNCTION__
;
230 return g_write(instance
, stream
, len
, buffer
);
233 NPError
DestroyStream(NPP instance
, NPStream
* stream
, NPReason reason
) {
234 DCHECK(IsInitialized()) << __FUNCTION__
;
235 return g_destroystream(instance
, stream
, reason
);
238 void Status(NPP instance
, const char* message
) {
239 DCHECK(IsInitialized()) << __FUNCTION__
;
240 return g_status(instance
, message
);
243 const char* UserAgent(NPP instance
) {
244 DCHECK(IsInitialized()) << __FUNCTION__
;
245 return g_useragent(instance
);
248 void* MemAlloc(uint32 size
) {
249 DCHECK(IsInitialized()) << __FUNCTION__
;
250 return g_memalloc(size
);
253 void MemFree(void* ptr
) {
254 DCHECK(IsInitialized()) << __FUNCTION__
;
255 return g_memfree(ptr
);
258 uint32
MemFlush(uint32 size
) {
259 DCHECK(IsInitialized()) << __FUNCTION__
;
260 return g_memflush(size
);
263 void ReloadPlugins(NPBool reloadPages
) {
264 DCHECK(IsInitialized()) << __FUNCTION__
;
265 return g_reloadplugins(reloadPages
);
269 DCHECK(IsInitialized()) << __FUNCTION__
;
270 return g_getJavaEnv();
273 void* GetJavaPeer(NPP instance
) {
274 DCHECK(IsInitialized()) << __FUNCTION__
;
275 return g_getJavaPeer(instance
);
278 NPError
GetURLNotify(NPP instance
, const char* URL
, const char* window
,
280 DCHECK(IsInitialized()) << __FUNCTION__
;
281 return g_geturlnotify(instance
, URL
, window
, notifyData
);
284 NPError
PostURLNotify(NPP instance
, const char* URL
, const char* window
,
285 uint32 len
, const char* buf
, NPBool file
,
287 DCHECK(IsInitialized()) << __FUNCTION__
;
288 return g_posturlnotify(instance
, URL
, window
, len
, buf
, file
, notifyData
);
291 NPError
GetValue(NPP instance
, NPNVariable variable
, void* ret_value
) {
292 DCHECK(IsInitialized()) << __FUNCTION__
;
293 return g_getvalue(instance
, variable
, ret_value
);
296 NPError
SetValue(NPP instance
, NPPVariable variable
, void* value
) {
297 DCHECK(IsInitialized()) << __FUNCTION__
;
298 return g_setvalue(instance
, variable
, value
);
301 void InvalidateRect(NPP instance
, NPRect
* rect
) {
302 DCHECK(IsInitialized()) << __FUNCTION__
;
303 return g_invalidaterect(instance
, rect
);
306 void InvalidateRegion(NPP instance
, NPRegion region
) {
307 DCHECK(IsInitialized()) << __FUNCTION__
;
308 return g_invalidateregion(instance
, region
);
311 void ForceRedraw(NPP instance
) {
312 DCHECK(IsInitialized()) << __FUNCTION__
;
313 return g_forceredraw(instance
);
316 void ReleaseVariantValue(NPVariant
* variant
) {
317 DCHECK(IsInitialized()) << __FUNCTION__
;
318 return g_releasevariantvalue(variant
);
321 NPIdentifier
GetStringIdentifier(const NPUTF8
* name
) {
322 DCHECK(IsInitialized()) << __FUNCTION__
;
323 return g_getstringidentifier(name
);
326 void GetStringIdentifiers(const NPUTF8
** names
, int nameCount
,
327 NPIdentifier
* identifiers
) {
328 DCHECK(IsInitialized()) << __FUNCTION__
;
329 return g_getstringidentifiers(names
, nameCount
, identifiers
);
332 NPIdentifier
GetIntIdentifier(int32_t intid
) {
333 DCHECK(IsInitialized()) << __FUNCTION__
;
334 return g_getintidentifier(intid
);
337 int32_t IntFromIdentifier(NPIdentifier identifier
) {
338 DCHECK(IsInitialized()) << __FUNCTION__
;
339 return g_intfromidentifier(identifier
);
342 bool IdentifierIsString(NPIdentifier identifier
) {
343 DCHECK(IsInitialized()) << __FUNCTION__
;
344 return g_identifierisstring(identifier
);
348 NPUTF8
* UTF8FromIdentifier(NPIdentifier identifier
) {
349 DCHECK(IsInitialized()) << __FUNCTION__
;
350 return g_utf8fromidentifier(identifier
);
354 NPObject
* CreateObject(NPP instance
, NPClass
* aClass
) {
355 DCHECK(IsInitialized()) << __FUNCTION__
;
356 return g_createobject(instance
, aClass
);
360 NPObject
* RetainObject(NPObject
* obj
) {
361 DCHECK(IsInitialized()) << __FUNCTION__
;
362 return g_retainobject(obj
);
366 void ReleaseObject(NPObject
* obj
) {
367 DCHECK(IsInitialized()) << __FUNCTION__
;
368 return g_releaseobject(obj
);
372 bool Invoke(NPP npp
, NPObject
* obj
, NPIdentifier methodName
,
373 const NPVariant
* args
, unsigned argCount
, NPVariant
* result
) {
374 DCHECK(IsInitialized()) << __FUNCTION__
;
375 return g_invoke(npp
, obj
, methodName
, args
, argCount
, result
);
378 bool InvokeDefault(NPP npp
, NPObject
* obj
, const NPVariant
* args
,
379 unsigned argCount
, NPVariant
* result
) {
380 DCHECK(IsInitialized()) << __FUNCTION__
;
381 return g_invoke_default(npp
, obj
, args
, argCount
, result
);
384 bool Evaluate(NPP npp
, NPObject
* obj
, NPString
* script
, NPVariant
* result
) {
385 DCHECK(IsInitialized()) << __FUNCTION__
;
386 return g_evaluate(npp
, obj
, script
, result
);
389 bool GetProperty(NPP npp
, NPObject
* obj
, NPIdentifier propertyName
,
391 DCHECK(IsInitialized()) << __FUNCTION__
;
392 return g_getproperty(npp
, obj
, propertyName
, result
);
395 bool SetProperty(NPP npp
, NPObject
* obj
, NPIdentifier propertyName
,
396 const NPVariant
* value
) {
397 DCHECK(IsInitialized()) << __FUNCTION__
;
398 return g_setproperty(npp
, obj
, propertyName
, value
);
401 bool HasProperty(NPP npp
, NPObject
* npobj
, NPIdentifier propertyName
) {
402 DCHECK(IsInitialized()) << __FUNCTION__
;
403 return g_hasproperty(npp
, npobj
, propertyName
);
406 bool HasMethod(NPP npp
, NPObject
* npobj
, NPIdentifier methodName
) {
407 DCHECK(IsInitialized()) << __FUNCTION__
;
408 return g_hasmethod(npp
, npobj
, methodName
);
411 bool RemoveProperty(NPP npp
, NPObject
* obj
, NPIdentifier propertyName
) {
412 DCHECK(IsInitialized()) << __FUNCTION__
;
413 return g_removeproperty(npp
, obj
, propertyName
);
416 void SetException(NPObject
* obj
, const NPUTF8
* message
) {
417 DCHECK(IsInitialized()) << __FUNCTION__
;
418 return g_setexception(obj
, message
);
421 void PushPopupsEnabledState(NPP npp
, NPBool enabled
) {
422 DCHECK(IsInitialized()) << __FUNCTION__
;
423 return g_pushpopupsenabledstate(npp
, enabled
);
426 void PopPopupsEnabledState(NPP npp
) {
427 DCHECK(IsInitialized()) << __FUNCTION__
;
428 return g_poppopupsenabledstate(npp
);
431 bool Enumerate(NPP npp
, NPObject
* obj
, NPIdentifier
** identifier
,
433 DCHECK(IsInitialized()) << __FUNCTION__
;
434 return g_enumerate(npp
, obj
, identifier
, count
);
437 void PluginThreadAsyncCall(NPP instance
, void (*func
)(void*), void* userData
) {
438 DCHECK(IsInitialized()) << __FUNCTION__
;
439 return g_pluginthreadasynccall(instance
, func
, userData
);
442 bool Construct(NPP npp
, NPObject
* obj
, const NPVariant
* args
, uint32_t argCount
,
444 DCHECK(IsInitialized()) << __FUNCTION__
;
445 return g_construct(npp
, obj
, args
, argCount
, result
);
448 NPError
GetValueForURL(NPP instance
, NPNURLVariable variable
, const char* url
,
449 char** value
, uint32
* len
) {
450 DCHECK(IsInitialized()) << __FUNCTION__
;
451 DCHECK(npapi::VersionMinor() >= NPVERS_HAS_URL_AND_AUTH_INFO
);
452 if (!g_getvalueforurl
) {
454 return NPERR_INCOMPATIBLE_VERSION_ERROR
;
457 return g_getvalueforurl(instance
, variable
, url
, value
, len
);
460 NPError
SetValueForURL(NPP instance
, NPNURLVariable variable
, const char* url
,
461 const char* value
, uint32 len
) {
462 DCHECK(IsInitialized()) << __FUNCTION__
;
463 DCHECK(npapi::VersionMinor() >= NPVERS_HAS_URL_AND_AUTH_INFO
);
464 if (!g_setvalueforurl
) {
466 return NPERR_INCOMPATIBLE_VERSION_ERROR
;
469 return g_setvalueforurl(instance
, variable
, url
, value
, len
);
472 NPError
GetAuthenticationInfo(NPP instance
, const char* protocol
,
473 const char* host
, int32 port
, const char* scheme
,
474 const char *realm
, char** username
, uint32
* ulen
,
475 char** password
, uint32
* plen
) {
476 DCHECK(IsInitialized()) << __FUNCTION__
;
477 DCHECK(npapi::VersionMinor() >= NPVERS_HAS_URL_AND_AUTH_INFO
);
478 if (g_getauthenticationinfo
) {
480 return NPERR_INCOMPATIBLE_VERSION_ERROR
;
483 return g_getauthenticationinfo(instance
, protocol
, host
, port
, scheme
,
484 realm
, username
, ulen
, password
, plen
);
487 std::string
StringFromIdentifier(NPIdentifier identifier
) {
489 NPUTF8
* utf8
= UTF8FromIdentifier(identifier
);
499 void AllocateStringVariant(const std::string
& str
, NPVariant
* var
) {
502 int len
= str
.length();
503 NPUTF8
* buffer
= reinterpret_cast<NPUTF8
*>(npapi::MemAlloc(len
+ 1));
506 memcpy(buffer
, str
.c_str(), len
);
507 STRINGN_TO_NPVARIANT(buffer
, len
, *var
);
509 NULL_TO_NPVARIANT(*var
);