ncval-stubout: Handle multiple files
[nativeclient.git] / npapi_plugin / npp_launcher.cc
blob31bf1bfecac7a792513ee3d185f11467d77cbf4a
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /**
34 * @file
35 * Defines the basic API for the nacl browser plugin
37 * http://www.mozilla.org/projects/plugins/
38 * http://www.ssuet.edu.pk/taimoor/books/0-7897-1063-3/ch11.htm
40 * NOTE: This is mostly a wrapper for module specific functions defined
41 * elsewhere
42 * TODO: give some more details
45 #include <stdio.h>
47 #include <new>
49 #include "native_client/npapi_plugin/npapi_bridge/npmodule.h"
50 #include "native_client/npapi_plugin/srpc/srpc.h"
51 #include "native_client/service_runtime/expiration.h"
52 #include "native_client/service_runtime/nrd_xfer_lib/nrd_all_modules.h"
53 #include "native_client/tools/npapi_runtime/nacl_npapi.h"
55 #define EXPAND(x) x
56 #define ENQUOTE(x) #x
57 #define EMBED(x) EXPAND(ENQUOTE(x))
59 const char kPluginName[] = "Native Client Plugin";
60 const char kPluginDescription[] = "Native Client Plugin was built on "
61 __DATE__ " at " __TIME__
62 #if defined(EXPIRATION_CHECK)
63 " and expires on "
64 EMBED(EXPIRATION_MONTH) "/"
65 EMBED(EXPIRATION_DAY) "/"
66 EMBED(EXPIRATION_YEAR)
67 " (mm/dd/yyyy)"
68 #endif
69 "";
71 char* NPP_GetMIMEDescription() {
72 return const_cast<char*>("application/x-nacl-srpc:nexe,nacl:NativeClient"
73 " Simple RPC module;"
74 "application/x-nacl-npapi:nexe,nacl:NativeClient"
75 " NPAPI bridge module");
76 // ";application/x-nacl-webidl:nexe,nacl:NativeClient Web IDL module"
79 // Invoked from NP_Initialize()
80 NPError NPP_Initialize() {
81 printf("NPP_Initialize\n");
82 if (NaClHasExpired()) {
83 return NPERR_INCOMPATIBLE_VERSION_ERROR;
85 NaClNrdAllModulesInit();
86 return NPERR_NO_ERROR;
89 // Invoked from NP_Shutdown()
90 void NPP_Shutdown() {
91 NaClNrdAllModulesFini();
92 printf("NPP_Shutdown\n");
95 NPError NPP_New(NPMIMEType plugin_type, NPP instance, uint16_t mode,
96 int16_t argc, char* argn[], char* argv[],
97 NPSavedData* saved) {
98 printf("NPP_New '%s'\n", plugin_type);
99 if (NaClHasExpired()) {
100 return NPERR_INCOMPATIBLE_VERSION_ERROR;
102 if (instance == NULL) {
103 return NPERR_INVALID_INSTANCE_ERROR;
105 if (strcmp(plugin_type, "application/x-nacl-npapi") == 0) {
106 instance->pdata = static_cast<nacl::NPInstance*>(
107 new(std::nothrow) nacl::NPModule(instance, argc, argn, argv));
108 } else if (strcmp(plugin_type, "application/x-nacl-srpc") == 0) {
109 instance->pdata = static_cast<nacl::NPInstance*>(
110 new(std::nothrow) nacl::SRPC_Plugin(instance, argc, argn, argv));
112 if (instance->pdata == NULL) {
113 return NPERR_OUT_OF_MEMORY_ERROR;
115 return NPERR_NO_ERROR;
118 NPError NPP_Destroy(NPP instance, NPSavedData** save) {
119 printf("NPP_Destroy\n");
120 if (instance == NULL) {
121 return NPERR_INVALID_INSTANCE_ERROR;
123 nacl::NPInstance* module = static_cast<nacl::NPInstance*>(instance->pdata);
124 if (module != NULL) {
125 instance->pdata = NULL;
126 NPError nperr = module->Destroy(save); // module must delete itself.
127 return nperr;
129 return NPERR_NO_ERROR;
132 NPError NPP_SetWindow(NPP instance, NPWindow* window) {
133 printf("NPP_SetWindow\n");
134 if (instance == NULL) {
135 return NPERR_INVALID_INSTANCE_ERROR;
137 if (window == NULL) {
138 return NPERR_GENERIC_ERROR;
140 nacl::NPInstance* module = static_cast<nacl::NPInstance*>(instance->pdata);
141 if (module != NULL) {
142 return module->SetWindow(window);
144 return NPERR_GENERIC_ERROR;
147 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) {
148 if (variable == NPPVpluginNameString) {
149 *static_cast<const char**>(value) = kPluginName;
150 return NPERR_NO_ERROR;
152 if (variable == NPPVpluginDescriptionString) {
153 *static_cast<const char**>(value) = kPluginDescription;
154 return NPERR_NO_ERROR;
156 if (instance == NULL) {
157 return NPERR_INVALID_INSTANCE_ERROR;
159 nacl::NPInstance* module = static_cast<nacl::NPInstance*>(instance->pdata);
160 if (module != NULL) {
161 return module->GetValue(variable, value);
163 return NPERR_GENERIC_ERROR;
166 int16_t NPP_HandleEvent(NPP instance, void* event) {
167 if (instance == NULL) {
168 return NPERR_INVALID_INSTANCE_ERROR;
170 nacl::NPInstance* module = static_cast<nacl::NPInstance*>(instance->pdata);
171 if (module != NULL) {
172 return module->HandleEvent(event);
174 return 0;
177 NPObject* NPP_GetScriptableInstance(NPP instance) {
178 printf("NPP_GetScriptableInstance\n");
179 if (instance == NULL) {
180 return NULL;
182 nacl::NPInstance* module = static_cast<nacl::NPInstance*>(instance->pdata);
183 if (module != NULL) {
184 return module->GetScriptableInstance();
186 return NULL;
189 NPError NPP_NewStream(NPP instance, NPMIMEType type,
190 NPStream* stream, NPBool seekable,
191 uint16_t* stype) {
192 printf("NPP_NewStream\n");
193 if (instance == NULL) {
194 return NPERR_INVALID_INSTANCE_ERROR;
196 nacl::NPInstance* module = static_cast<nacl::NPInstance*>(instance->pdata);
197 if (module != NULL) {
198 return module->NewStream(type, stream, seekable, stype);
200 return NPERR_GENERIC_ERROR;
203 void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* filename) {
204 printf("NPP_StreamAsFile: %s\n", filename);
205 if (instance == NULL) {
206 return;
208 nacl::NPInstance* module = static_cast<nacl::NPInstance*>(instance->pdata);
209 if (module != NULL) {
210 #if NACL_OSX
211 if (filename && filename[0] != '/') {
212 // The filename we were given is a "classic" pathname, which needs
213 // to be converted to a posix pathname.
214 size_t len = strlen(filename);
216 // Pascal strings are limited to no more than 255 bytes.
217 if (len < 256) {
218 FSSpec spec;
219 unsigned char pascal_pathname[256];
221 // Make a pascal string out of a C string.
222 memmove(pascal_pathname + 1, filename, len);
223 pascal_pathname[0] = static_cast<unsigned char>(len);
224 if (FSMakeFSSpec(0, 0, pascal_pathname, &spec) == noErr) {
225 FSRef ref;
226 if (FSpMakeFSRef(&spec, &ref) == noErr) {
227 static unsigned char posix_path[1024];
229 if (FSRefMakePath(&ref, posix_path, sizeof(posix_path)) == noErr) {
230 filename = reinterpret_cast<char*>(posix_path);
231 } else {
232 filename = NULL;
234 module->StreamAsFile(stream, filename);
235 FSDeleteObject(&ref);
236 return;
240 filename = NULL;
242 #endif // NACL_OSX
243 module->StreamAsFile(stream, filename);
247 // Note Safari running on OS X removes the loaded file at this point.
248 // To keep the file, it must be opened in NPP_StreamAsFile().
249 NPError NPP_DestroyStream(NPP instance, NPStream *stream, NPError reason) {
250 printf("NPP_DestroyStream %d\n", reason);
251 if (instance == NULL) {
252 return NPERR_INVALID_INSTANCE_ERROR;
254 nacl::NPInstance* module = static_cast<nacl::NPInstance*>(instance->pdata);
255 if (module != NULL) {
256 return module->DestroyStream(stream, reason);
258 return NPERR_GENERIC_ERROR;
261 void NPP_URLNotify(NPP instance, const char* url, NPReason reason,
262 void* notify_data) {
263 printf("NPP_URLNotify(%s)\n", url);
264 if (instance == NULL) {
265 return;
267 nacl::NPInstance* module = static_cast<nacl::NPInstance*>(instance->pdata);
268 if (module != NULL) {
269 module->URLNotify(url, reason, notify_data);