1 // Copyright (c) 2012 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 "content/test/plugin/plugin_client.h"
7 #include "base/strings/string_util.h"
8 #include "content/test/plugin/plugin_execute_stream_javascript.h"
9 #include "content/test/plugin/plugin_test.h"
10 #include "content/test/plugin/plugin_test_factory.h"
12 namespace NPAPIClient
{
14 class NPWithProperty
: public NPObject
{
16 NPWithProperty() : NPObject() {}
18 static NPObject
* Allocate(NPP npp
, NPClass
* npclass
) {
19 return new NPWithProperty();
22 static void Deallocate(NPObject
* npobject
) {
23 delete static_cast<NPWithProperty
*>(npobject
);
26 static bool HasProperty(NPObject
* npobject
, NPIdentifier name
) {
27 return (name
== PluginClient::HostFunctions()->
28 getstringidentifier("loadedProperty"));
31 static bool GetProperty(NPObject
* npobject
,
34 if (name
== PluginClient::HostFunctions()->
35 getstringidentifier("loadedProperty")) {
36 BOOLEAN_TO_NPVARIANT(true, *result
);
43 static NPClass
* GetNPClass() {
44 static NPClass plugin_class
= {
45 NP_CLASS_STRUCT_VERSION
,
46 NPWithProperty::Allocate
,
47 NPWithProperty::Deallocate
,
51 NULL
, // InvokeDefault
52 NPWithProperty::HasProperty
,
53 NPWithProperty::GetProperty
,
55 NULL
, // RemoveProperty
60 NPNetscapeFuncs
* PluginClient::host_functions_
;
62 NPError
PluginClient::GetEntryPoints(NPPluginFuncs
* pFuncs
) {
64 return NPERR_INVALID_FUNCTABLE_ERROR
;
66 if (pFuncs
->size
< sizeof(NPPluginFuncs
))
67 return NPERR_INVALID_FUNCTABLE_ERROR
;
69 pFuncs
->version
= (NP_VERSION_MAJOR
<< 8) | NP_VERSION_MINOR
;
70 pFuncs
->newp
= NPP_New
;
71 pFuncs
->destroy
= NPP_Destroy
;
72 pFuncs
->setwindow
= NPP_SetWindow
;
73 pFuncs
->newstream
= NPP_NewStream
;
74 pFuncs
->destroystream
= NPP_DestroyStream
;
75 pFuncs
->asfile
= NPP_StreamAsFile
;
76 pFuncs
->writeready
= NPP_WriteReady
;
77 pFuncs
->write
= NPP_Write
;
78 pFuncs
->print
= NPP_Print
;
79 pFuncs
->event
= NPP_HandleEvent
;
80 pFuncs
->urlnotify
= NPP_URLNotify
;
81 pFuncs
->getvalue
= NPP_GetValue
;
82 pFuncs
->setvalue
= NPP_SetValue
;
83 pFuncs
->javaClass
= NULL
;
84 pFuncs
->urlredirectnotify
= NPP_URLRedirectNotify
;
85 pFuncs
->clearsitedata
= NPP_ClearSiteData
;
87 return NPERR_NO_ERROR
;
90 NPError
PluginClient::Initialize(NPNetscapeFuncs
* pFuncs
) {
92 return NPERR_INVALID_FUNCTABLE_ERROR
;
95 if (static_cast<unsigned char>((pFuncs
->version
>> 8) & 0xff) >
97 return NPERR_INCOMPATIBLE_VERSION_ERROR
;
101 // Check if we should crash.
102 HANDLE crash_event
= CreateEvent(NULL
, TRUE
, FALSE
, L
"TestPluginCrashOnInit");
103 if (WaitForSingleObject(crash_event
, 0) == WAIT_OBJECT_0
) {
107 CloseHandle(crash_event
);
110 host_functions_
= pFuncs
;
112 return NPERR_NO_ERROR
;
115 NPError
PluginClient::Shutdown() {
116 return NPERR_NO_ERROR
;
119 } // namespace NPAPIClient
122 NPError
NPP_New(NPMIMEType pluginType
, NPP instance
, uint16 mode
,
123 int16 argc
, char* argn
[], char* argv
[], NPSavedData
* saved
) {
124 if (instance
== NULL
)
125 return NPERR_INVALID_INSTANCE_ERROR
;
127 NPAPIClient::PluginTest
* new_test
= NULL
;
128 if (mode
== NP_FULL
) {
129 new_test
= new::NPAPIClient::ExecuteStreamJavaScript(
130 instance
, NPAPIClient::PluginClient::HostFunctions());
132 // We look at the test name requested via the plugin arguments. We match
133 // that against a given test and try to instantiate it.
135 // lookup the name parameter
136 std::string test_name
;
137 for (int name_index
= 0; name_index
< argc
; name_index
++) {
138 if (base::strcasecmp(argn
[name_index
], "name") == 0) {
139 test_name
= argv
[name_index
];
143 if (test_name
.empty())
144 return NPERR_GENERIC_ERROR
; // no name found
146 new_test
= NPAPIClient::CreatePluginTest(test_name
,
147 instance
, NPAPIClient::PluginClient::HostFunctions());
148 if (new_test
== NULL
) {
149 // If we don't have a test case for this, create a
150 // generic one which basically never fails.
151 LOG(WARNING
) << "Unknown test name '" << test_name
152 << "'; using default test.";
153 new_test
= new NPAPIClient::PluginTest(instance
,
154 NPAPIClient::PluginClient::HostFunctions());
158 #if defined(OS_MACOSX)
159 // Set modern drawing and event models.
160 NPError drawing_ret
= NPAPIClient::PluginClient::HostFunctions()->setvalue(
161 instance
, NPPVpluginDrawingModel
, (void*)NPDrawingModelCoreGraphics
);
162 NPError event_ret
= NPAPIClient::PluginClient::HostFunctions()->setvalue(
163 instance
, NPPVpluginEventModel
, (void*)NPEventModelCocoa
);
164 if (drawing_ret
!= NPERR_NO_ERROR
|| event_ret
!= NPERR_NO_ERROR
)
165 return NPERR_INCOMPATIBLE_VERSION_ERROR
;
168 NPError ret
= new_test
->New(mode
, argc
, (const char**)argn
,
169 (const char**)argv
, saved
);
170 if ((ret
== NPERR_NO_ERROR
) && new_test
->IsWindowless()) {
171 NPAPIClient::PluginClient::HostFunctions()->setvalue(
172 instance
, NPPVpluginWindowBool
, NULL
);
178 NPError
NPP_Destroy(NPP instance
, NPSavedData
** save
) {
179 if (instance
== NULL
)
180 return NPERR_INVALID_INSTANCE_ERROR
;
182 NPAPIClient::PluginTest
* plugin
=
183 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
185 NPError rv
= plugin
->Destroy();
190 NPError
NPP_SetWindow(NPP instance
, NPWindow
* pNPWindow
) {
191 if (instance
== NULL
)
192 return NPERR_INVALID_INSTANCE_ERROR
;
194 NPAPIClient::PluginTest
* plugin
=
195 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
197 return plugin
->SetWindow(pNPWindow
);
200 NPError
NPP_NewStream(NPP instance
, NPMIMEType type
,
201 NPStream
* stream
, NPBool seekable
, uint16
* stype
) {
202 if (instance
== NULL
)
203 return NPERR_INVALID_INSTANCE_ERROR
;
205 NPAPIClient::PluginTest
* plugin
=
206 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
208 return plugin
->NewStream(type
, stream
, seekable
, stype
);
211 int32
NPP_WriteReady(NPP instance
, NPStream
*stream
) {
212 if (instance
== NULL
)
213 return NPERR_INVALID_INSTANCE_ERROR
;
215 NPAPIClient::PluginTest
* plugin
=
216 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
218 return plugin
->WriteReady(stream
);
221 int32
NPP_Write(NPP instance
, NPStream
*stream
, int32 offset
,
222 int32 len
, void *buffer
) {
223 if (instance
== NULL
)
224 return NPERR_INVALID_INSTANCE_ERROR
;
226 NPAPIClient::PluginTest
* plugin
=
227 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
229 return plugin
->Write(stream
, offset
, len
, buffer
);
232 NPError
NPP_DestroyStream(NPP instance
, NPStream
*stream
, NPError reason
) {
233 if (instance
== NULL
)
234 return NPERR_INVALID_INSTANCE_ERROR
;
236 NPAPIClient::PluginTest
* plugin
=
237 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
239 return plugin
->DestroyStream(stream
, reason
);
242 void NPP_StreamAsFile(NPP instance
, NPStream
* stream
, const char* fname
) {
243 if (instance
== NULL
)
246 NPAPIClient::PluginTest
* plugin
=
247 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
249 return plugin
->StreamAsFile(stream
, fname
);
252 void NPP_Print(NPP instance
, NPPrint
* printInfo
) {
253 if (instance
== NULL
)
256 // XXXMB - do work here.
259 void NPP_URLNotify(NPP instance
, const char* url
, NPReason reason
,
261 if (instance
== NULL
)
264 NPAPIClient::PluginTest
* plugin
=
265 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
267 return plugin
->URLNotify(url
, reason
, notifyData
);
270 NPError
NPP_GetValue(NPP instance
, NPPVariable variable
, void *value
) {
271 if (instance
== NULL
)
272 return NPERR_INVALID_INSTANCE_ERROR
;
274 if (variable
== NPPVpluginNeedsXEmbed
) {
275 *static_cast<NPBool
*>(value
) = 1;
276 return NPERR_NO_ERROR
;
279 if (variable
== NPPVpluginScriptableNPObject
) {
281 NPAPIClient::PluginClient::HostFunctions()->createobject(
282 instance
, NPAPIClient::GetNPClass());
283 return NPERR_NO_ERROR
;
286 // XXXMB - do work here.
287 return NPERR_GENERIC_ERROR
;
290 NPError
NPP_SetValue(NPP instance
, NPNVariable variable
, void *value
) {
291 if (instance
== NULL
)
292 return NPERR_INVALID_INSTANCE_ERROR
;
294 // XXXMB - do work here.
295 return NPERR_GENERIC_ERROR
;
298 int16
NPP_HandleEvent(NPP instance
, void* event
) {
299 if (instance
== NULL
)
302 NPAPIClient::PluginTest
* plugin
=
303 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
305 return plugin
->HandleEvent(event
);
308 void NPP_URLRedirectNotify(NPP instance
, const char* url
, int32_t status
,
311 NPAPIClient::PluginTest
* plugin
=
312 reinterpret_cast<NPAPIClient::PluginTest
*>(instance
->pdata
);
313 plugin
->URLRedirectNotify(url
, status
, notify_data
);
317 NPError
NPP_ClearSiteData(const char* site
,
320 VLOG(0) << "NPP_ClearSiteData called";
321 return NPERR_NO_ERROR
;