| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "content/test/plugin/plugin_client.h" | |
| 6 | |
| 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" | |
| 11 | |
| 12 namespace NPAPIClient { | |
| 13 | |
| 14 class NPWithProperty : public NPObject { | |
| 15 public: | |
| 16 NPWithProperty() : NPObject() {} | |
| 17 | |
| 18 static NPObject* Allocate(NPP npp, NPClass* npclass) { | |
| 19 return new NPWithProperty(); | |
| 20 } | |
| 21 | |
| 22 static void Deallocate(NPObject* npobject) { | |
| 23 delete static_cast<NPWithProperty*>(npobject); | |
| 24 } | |
| 25 | |
| 26 static bool HasProperty(NPObject* npobject, NPIdentifier name) { | |
| 27 return (name == PluginClient::HostFunctions()-> | |
| 28 getstringidentifier("loadedProperty")); | |
| 29 } | |
| 30 | |
| 31 static bool GetProperty(NPObject* npobject, | |
| 32 NPIdentifier name, | |
| 33 NPVariant* result) { | |
| 34 if (name == PluginClient::HostFunctions()-> | |
| 35 getstringidentifier("loadedProperty")) { | |
| 36 BOOLEAN_TO_NPVARIANT(true, *result); | |
| 37 return true; | |
| 38 } | |
| 39 return false; | |
| 40 } | |
| 41 }; | |
| 42 | |
| 43 static NPClass* GetNPClass() { | |
| 44 static NPClass plugin_class = { | |
| 45 NP_CLASS_STRUCT_VERSION, | |
| 46 NPWithProperty::Allocate, | |
| 47 NPWithProperty::Deallocate, | |
| 48 NULL, // Invalidate | |
| 49 NULL, // HasMethod | |
| 50 NULL, // Invoke | |
| 51 NULL, // InvokeDefault | |
| 52 NPWithProperty::HasProperty, | |
| 53 NPWithProperty::GetProperty, | |
| 54 NULL, // SetProperty | |
| 55 NULL, // RemoveProperty | |
| 56 }; | |
| 57 return &plugin_class; | |
| 58 } | |
| 59 | |
| 60 NPNetscapeFuncs* PluginClient::host_functions_; | |
| 61 | |
| 62 NPError PluginClient::GetEntryPoints(NPPluginFuncs* pFuncs) { | |
| 63 if (pFuncs == NULL) | |
| 64 return NPERR_INVALID_FUNCTABLE_ERROR; | |
| 65 | |
| 66 if (pFuncs->size < sizeof(NPPluginFuncs)) | |
| 67 return NPERR_INVALID_FUNCTABLE_ERROR; | |
| 68 | |
| 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; | |
| 86 | |
| 87 return NPERR_NO_ERROR; | |
| 88 } | |
| 89 | |
| 90 NPError PluginClient::Initialize(NPNetscapeFuncs* pFuncs) { | |
| 91 if (pFuncs == NULL) { | |
| 92 return NPERR_INVALID_FUNCTABLE_ERROR; | |
| 93 } | |
| 94 | |
| 95 if (static_cast<unsigned char>((pFuncs->version >> 8) & 0xff) > | |
| 96 NP_VERSION_MAJOR) { | |
| 97 return NPERR_INCOMPATIBLE_VERSION_ERROR; | |
| 98 } | |
| 99 | |
| 100 #if defined(OS_WIN) | |
| 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) { | |
| 104 int *zero = NULL; | |
| 105 *zero = 0; | |
| 106 } | |
| 107 CloseHandle(crash_event); | |
| 108 #endif | |
| 109 | |
| 110 host_functions_ = pFuncs; | |
| 111 | |
| 112 return NPERR_NO_ERROR; | |
| 113 } | |
| 114 | |
| 115 NPError PluginClient::Shutdown() { | |
| 116 return NPERR_NO_ERROR; | |
| 117 } | |
| 118 | |
| 119 } // namespace NPAPIClient | |
| 120 | |
| 121 extern "C" { | |
| 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; | |
| 126 | |
| 127 NPAPIClient::PluginTest* new_test = NULL; | |
| 128 if (mode == NP_FULL) { | |
| 129 new_test = new::NPAPIClient::ExecuteStreamJavaScript( | |
| 130 instance, NPAPIClient::PluginClient::HostFunctions()); | |
| 131 } else { | |
| 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. | |
| 134 | |
| 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]; | |
| 140 break; | |
| 141 } | |
| 142 } | |
| 143 if (test_name.empty()) | |
| 144 return NPERR_GENERIC_ERROR; // no name found | |
| 145 | |
| 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()); | |
| 155 } | |
| 156 } | |
| 157 | |
| 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; | |
| 166 #endif | |
| 167 | |
| 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); | |
| 173 } | |
| 174 | |
| 175 return ret; | |
| 176 } | |
| 177 | |
| 178 NPError NPP_Destroy(NPP instance, NPSavedData** save) { | |
| 179 if (instance == NULL) | |
| 180 return NPERR_INVALID_INSTANCE_ERROR; | |
| 181 | |
| 182 NPAPIClient::PluginTest* plugin = | |
| 183 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 184 | |
| 185 NPError rv = plugin->Destroy(); | |
| 186 delete plugin; | |
| 187 return rv; | |
| 188 } | |
| 189 | |
| 190 NPError NPP_SetWindow(NPP instance, NPWindow* pNPWindow) { | |
| 191 if (instance == NULL) | |
| 192 return NPERR_INVALID_INSTANCE_ERROR; | |
| 193 | |
| 194 NPAPIClient::PluginTest* plugin = | |
| 195 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 196 | |
| 197 return plugin->SetWindow(pNPWindow); | |
| 198 } | |
| 199 | |
| 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; | |
| 204 | |
| 205 NPAPIClient::PluginTest* plugin = | |
| 206 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 207 | |
| 208 return plugin->NewStream(type, stream, seekable, stype); | |
| 209 } | |
| 210 | |
| 211 int32 NPP_WriteReady(NPP instance, NPStream *stream) { | |
| 212 if (instance == NULL) | |
| 213 return NPERR_INVALID_INSTANCE_ERROR; | |
| 214 | |
| 215 NPAPIClient::PluginTest* plugin = | |
| 216 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 217 | |
| 218 return plugin->WriteReady(stream); | |
| 219 } | |
| 220 | |
| 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; | |
| 225 | |
| 226 NPAPIClient::PluginTest* plugin = | |
| 227 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 228 | |
| 229 return plugin->Write(stream, offset, len, buffer); | |
| 230 } | |
| 231 | |
| 232 NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPError reason) { | |
| 233 if (instance == NULL) | |
| 234 return NPERR_INVALID_INSTANCE_ERROR; | |
| 235 | |
| 236 NPAPIClient::PluginTest* plugin = | |
| 237 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 238 | |
| 239 return plugin->DestroyStream(stream, reason); | |
| 240 } | |
| 241 | |
| 242 void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname) { | |
| 243 if (instance == NULL) | |
| 244 return; | |
| 245 | |
| 246 NPAPIClient::PluginTest* plugin = | |
| 247 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 248 | |
| 249 return plugin->StreamAsFile(stream, fname); | |
| 250 } | |
| 251 | |
| 252 void NPP_Print(NPP instance, NPPrint* printInfo) { | |
| 253 if (instance == NULL) | |
| 254 return; | |
| 255 | |
| 256 // XXXMB - do work here. | |
| 257 } | |
| 258 | |
| 259 void NPP_URLNotify(NPP instance, const char* url, NPReason reason, | |
| 260 void* notifyData) { | |
| 261 if (instance == NULL) | |
| 262 return; | |
| 263 | |
| 264 NPAPIClient::PluginTest* plugin = | |
| 265 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 266 | |
| 267 return plugin->URLNotify(url, reason, notifyData); | |
| 268 } | |
| 269 | |
| 270 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) { | |
| 271 if (instance == NULL) | |
| 272 return NPERR_INVALID_INSTANCE_ERROR; | |
| 273 | |
| 274 if (variable == NPPVpluginNeedsXEmbed) { | |
| 275 *static_cast<NPBool*>(value) = 1; | |
| 276 return NPERR_NO_ERROR; | |
| 277 } | |
| 278 | |
| 279 if (variable == NPPVpluginScriptableNPObject) { | |
| 280 *(NPObject**)value = | |
| 281 NPAPIClient::PluginClient::HostFunctions()->createobject( | |
| 282 instance, NPAPIClient::GetNPClass()); | |
| 283 return NPERR_NO_ERROR; | |
| 284 } | |
| 285 | |
| 286 // XXXMB - do work here. | |
| 287 return NPERR_GENERIC_ERROR; | |
| 288 } | |
| 289 | |
| 290 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value) { | |
| 291 if (instance == NULL) | |
| 292 return NPERR_INVALID_INSTANCE_ERROR; | |
| 293 | |
| 294 // XXXMB - do work here. | |
| 295 return NPERR_GENERIC_ERROR; | |
| 296 } | |
| 297 | |
| 298 int16 NPP_HandleEvent(NPP instance, void* event) { | |
| 299 if (instance == NULL) | |
| 300 return 0; | |
| 301 | |
| 302 NPAPIClient::PluginTest* plugin = | |
| 303 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 304 | |
| 305 return plugin->HandleEvent(event); | |
| 306 } | |
| 307 | |
| 308 void NPP_URLRedirectNotify(NPP instance, const char* url, int32_t status, | |
| 309 void* notify_data) { | |
| 310 if (instance) { | |
| 311 NPAPIClient::PluginTest* plugin = | |
| 312 reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata); | |
| 313 plugin->URLRedirectNotify(url, status, notify_data); | |
| 314 } | |
| 315 } | |
| 316 | |
| 317 NPError NPP_ClearSiteData(const char* site, | |
| 318 uint64 flags, | |
| 319 uint64 max_age) { | |
| 320 VLOG(0) << "NPP_ClearSiteData called"; | |
| 321 return NPERR_NO_ERROR; | |
| 322 } | |
| 323 } // extern "C" | |
| OLD | NEW |