summaryrefslogtreecommitdiffstats
path: root/chrome/default_plugin/plugin_main.cc
blob: f7b15abb86c0dd784e9a7bac6f4f7371702d53f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/default_plugin/plugin_main.h"

#include "base/logging.h"
#include "base/string_util.h"
#include "chrome/default_plugin/plugin_impl.h"
#include "webkit/glue/webkit_glue.h"

namespace default_plugin {

#if defined(OS_WIN)
//
// Forward declare the linker-provided pseudo variable for the
// current module handle.
//
extern "C" IMAGE_DOS_HEADER __ImageBase;

// get the handle to the currently executing module
inline HMODULE GetCurrentModuleHandle() {
  return reinterpret_cast<HINSTANCE>(&__ImageBase);
}
#endif

// Initialized in NP_Initialize.
NPNetscapeFuncs* g_browser = NULL;

NPError API_CALL NP_GetEntryPoints(NPPluginFuncs* funcs) {
  // Be explicit about the namespace, because all internal plugins have
  // functions with these names and some might accidentally put them into the
  // global namespace. In that case, the linker might prefer the global one.
  funcs->version = 11;
  funcs->size = sizeof(*funcs);
  funcs->newp = default_plugin::NPP_New;
  funcs->destroy = default_plugin::NPP_Destroy;
  funcs->setwindow = default_plugin::NPP_SetWindow;
  funcs->newstream = default_plugin::NPP_NewStream;
  funcs->destroystream = default_plugin::NPP_DestroyStream;
  funcs->writeready = default_plugin::NPP_WriteReady;
  funcs->write = default_plugin::NPP_Write;
  funcs->asfile = NULL;
  funcs->print = NULL;
  funcs->event = default_plugin::NPP_HandleEvent;
  funcs->urlnotify = default_plugin::NPP_URLNotify;
#if defined(OS_POSIX) && !defined(OS_MACOSX)
  funcs->getvalue = default_plugin::NPP_GetValue;
#else
  funcs->getvalue = NULL;
#endif
  funcs->setvalue = NULL;
  return NPERR_NO_ERROR;
}

NPError API_CALL NP_Initialize(NPNetscapeFuncs* funcs) {
  g_browser = funcs;
  return 0;
}

#if defined(OS_POSIX) && !defined(OS_MACOSX)
NPError API_CALL NP_Initialize(NPNetscapeFuncs* funcs, NPPluginFuncs* p_funcs) {
  NPError err = NP_Initialize(funcs);
  if (err != NPERR_NO_ERROR)
    return err;
  return NP_GetEntryPoints(p_funcs);
}
#endif

NPError API_CALL NP_Shutdown(void) {
  g_browser = NULL;
  return NPERR_NO_ERROR;
}

namespace {
// This function is only invoked when the default plugin is invoked
// with a special mime type application/chromium-test-default-plugin
void SignalTestResult(NPP instance) {
  NPObject *window_obj = NULL;
  g_browser->getvalue(instance, NPNVWindowNPObject, &window_obj);
  if (!window_obj) {
    NOTREACHED();
    return;
  }

  std::string script = "javascript:onSuccess()";
  NPString script_string;
  script_string.UTF8Characters = script.c_str();
  script_string.UTF8Length =
      static_cast<unsigned int>(script.length());

  NPVariant result_var;
  g_browser->evaluate(instance, window_obj,
                      &script_string, &result_var);
  g_browser->releaseobject(window_obj);
}

}  // namespace CHROMIUM_DefaultPluginTest

bool NegotiateModels(NPP instance) {
#if defined(OS_MACOSX)
  NPError err;
  // Set drawing model to core graphics
  NPBool supportsCoreGraphics = false;
  err = g_browser->getvalue(instance,
                            NPNVsupportsCoreGraphicsBool,
                            &supportsCoreGraphics);
  if (err != NPERR_NO_ERROR || !supportsCoreGraphics) {
    NOTREACHED();
    return false;
  }
  err = g_browser->setvalue(instance,
                            NPPVpluginDrawingModel,
                            (void*)NPDrawingModelCoreGraphics);
  if (err != NPERR_NO_ERROR) {
    NOTREACHED();
    return false;
  }

  // Set event model to cocoa
  NPBool supportsCocoaEvents = false;
  err = g_browser->getvalue(instance,
                            NPNVsupportsCocoaBool,
                            &supportsCocoaEvents);
  if (err != NPERR_NO_ERROR || !supportsCocoaEvents) {
    NOTREACHED();
    return false;
  }
  err = g_browser->setvalue(instance,
                            NPPVpluginEventModel,
                            (void*)NPEventModelCocoa);
  if (err != NPERR_NO_ERROR) {
    NOTREACHED();
    return false;
  }
#elif defined(OS_POSIX)
  NPError err;
  // Check that chrome still supports xembed.
  NPBool supportsXEmbed = false;
  err = g_browser->getvalue(instance,
                            NPNVSupportsXEmbedBool,
                            &supportsXEmbed);
  if (err != NPERR_NO_ERROR || !supportsXEmbed) {
    NOTREACHED();
    return false;
  }

  // Check that the toolkit is still gtk2.
  NPNToolkitType toolkit;
  err = g_browser->getvalue(instance,
                            NPNVToolkit,
                            &toolkit);
  if (err != NPERR_NO_ERROR || toolkit != NPNVGtk2) {
    NOTREACHED();
    return false;
  }
#endif
  return true;
}

NPError NPP_New(NPMIMEType plugin_type, NPP instance, uint16_t mode,
                int16_t argc, char* argn[], char* argv[], NPSavedData* saved) {
  if (instance == NULL)
    return NPERR_INVALID_INSTANCE_ERROR;

  if (!NegotiateModels(instance))
    return NPERR_INCOMPATIBLE_VERSION_ERROR;

  PluginInstallerImpl* plugin_impl = new PluginInstallerImpl(mode);
  plugin_impl->Initialize(
#if defined(OS_WIN)
                          GetCurrentModuleHandle(),
#else
                          NULL,
#endif
                          instance, plugin_type, argc,
                          argn, argv);

  instance->pdata = reinterpret_cast<void*>(plugin_impl);

  if (!base::strcasecmp(plugin_type,
      "application/chromium-test-default-plugin")) {
    SignalTestResult(instance);
  }

  return NPERR_NO_ERROR;
}

NPError NPP_Destroy(NPP instance, NPSavedData** save) {
  PluginInstallerImpl* plugin_impl =
    reinterpret_cast<PluginInstallerImpl*>(instance->pdata);

  if (plugin_impl) {
    plugin_impl->Shutdown();
    delete plugin_impl;
  }

  return NPERR_NO_ERROR;
}

NPError NPP_SetWindow(NPP instance, NPWindow* window_info) {
  if (instance == NULL)
    return NPERR_INVALID_INSTANCE_ERROR;

  if (window_info == NULL) {
    NOTREACHED();
    return NPERR_GENERIC_ERROR;
  }

  PluginInstallerImpl* plugin_impl =
      reinterpret_cast<PluginInstallerImpl*>(instance->pdata);

  if (plugin_impl == NULL) {
    NOTREACHED();
    return NPERR_GENERIC_ERROR;
  }

  if (!plugin_impl->NPP_SetWindow(window_info)) {
    delete plugin_impl;
    return NPERR_GENERIC_ERROR;
  }

  return NPERR_NO_ERROR;
}

NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
                      NPBool seekable, uint16_t* stype) {
  if (instance == NULL)
    return NPERR_INVALID_INSTANCE_ERROR;

  PluginInstallerImpl* plugin_impl =
    reinterpret_cast<PluginInstallerImpl*>(instance->pdata);

  if (!plugin_impl) {
    NOTREACHED();
    return NPERR_INVALID_INSTANCE_ERROR;
  }

  plugin_impl->NewStream(stream);
  return NPERR_NO_ERROR;
}

NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason) {
  if (instance == NULL)
    return NPERR_INVALID_INSTANCE_ERROR;

  PluginInstallerImpl* plugin_impl =
      reinterpret_cast<PluginInstallerImpl*>(instance->pdata);

  if (!plugin_impl) {
    NOTREACHED();
    return NPERR_INVALID_INSTANCE_ERROR;
  }

  plugin_impl->DestroyStream(stream, reason);
  return NPERR_NO_ERROR;
}

int32_t NPP_WriteReady(NPP instance, NPStream* stream) {
  if (instance == NULL)
    return 0;

  PluginInstallerImpl* plugin_impl =
      reinterpret_cast<PluginInstallerImpl*>(instance->pdata);

  if (!plugin_impl) {
    NOTREACHED();
    return 0;
  }

  if (plugin_impl->WriteReady(stream))
    return 0x7FFFFFFF;
  return 0;
}

int32_t NPP_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len,
                void* buffer) {
  if (instance == NULL)
    return 0;

  PluginInstallerImpl* plugin_impl =
      reinterpret_cast<PluginInstallerImpl*>(instance->pdata);

  if (!plugin_impl) {
    NOTREACHED();
    return 0;
  }

  return plugin_impl->Write(stream, offset, len, buffer);
}

void NPP_URLNotify(NPP instance, const char* url, NPReason reason,
                   void* notifyData) {
  if (instance != NULL) {
    PluginInstallerImpl* plugin_impl =
        reinterpret_cast<PluginInstallerImpl*>(instance->pdata);

    if (!plugin_impl) {
      NOTREACHED();
      return;
    }

    plugin_impl->URLNotify(url, reason);
  }
}

#if defined(OS_POSIX) && !defined(OS_MACOSX)
NPError NPP_GetValue(NPP instance, NPPVariable variable, void* value) {
  switch (variable) {
    case NPPVpluginNeedsXEmbed:
      *static_cast<NPBool*>(value) = true;
      return NPERR_NO_ERROR;
    default:
      return NPERR_INVALID_PARAM;
  }
}
#endif

int16_t NPP_HandleEvent(NPP instance, void* event) {
  if (instance == NULL)
    return 0;

  PluginInstallerImpl* plugin_impl =
      reinterpret_cast<PluginInstallerImpl*>(instance->pdata);

  return plugin_impl->NPP_HandleEvent(event);
}

}   // default_plugin