summaryrefslogtreecommitdiffstats
path: root/webkit/glue/plugins/test/plugin_client.cc
blob: 83583409799f60d153581db1e8888cd3fc7e32b7 (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
// Copyright (c) 2009 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 "webkit/glue/plugins/test/plugin_client.h"

#include "base/string_util.h"
#include "webkit/glue/plugins/test/plugin_test.h"
#include "webkit/glue/plugins/test/plugin_test_factory.h"

namespace NPAPIClient {

NPNetscapeFuncs* PluginClient::host_functions_;

NPError PluginClient::GetEntryPoints(NPPluginFuncs* pFuncs) {
  if (pFuncs == NULL)
    return NPERR_INVALID_FUNCTABLE_ERROR;

  if (pFuncs->size < sizeof(NPPluginFuncs))
    return NPERR_INVALID_FUNCTABLE_ERROR;

  pFuncs->version       = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
  pFuncs->newp          = NPP_New;
  pFuncs->destroy       = NPP_Destroy;
  pFuncs->setwindow     = NPP_SetWindow;
  pFuncs->newstream     = NPP_NewStream;
  pFuncs->destroystream = NPP_DestroyStream;
  pFuncs->asfile        = NPP_StreamAsFile;
  pFuncs->writeready    = NPP_WriteReady;
  pFuncs->write         = NPP_Write;
  pFuncs->print         = NPP_Print;
  pFuncs->event         = NPP_HandleEvent;
  pFuncs->urlnotify     = NPP_URLNotify;
  pFuncs->getvalue      = NPP_GetValue;
  pFuncs->setvalue      = NPP_SetValue;
  pFuncs->javaClass     = NULL;
  pFuncs->urlredirectnotify = NPP_URLRedirectNotify;

  return NPERR_NO_ERROR;
}

NPError PluginClient::Initialize(NPNetscapeFuncs* pFuncs) {
  if (pFuncs == NULL) {
    return NPERR_INVALID_FUNCTABLE_ERROR;
  }

  if (static_cast<unsigned char>((pFuncs->version >> 8) & 0xff) >
      NP_VERSION_MAJOR) {
    return NPERR_INCOMPATIBLE_VERSION_ERROR;
  }

#if defined(OS_WIN)
  // Check if we should crash.
  HANDLE crash_event = CreateEvent(NULL, TRUE, FALSE, L"TestPluginCrashOnInit");
  if (WaitForSingleObject(crash_event, 0) == WAIT_OBJECT_0) {
    int *zero = NULL;
    *zero = 0;
  }
  CloseHandle(crash_event);
#endif

  host_functions_ = pFuncs;

  return NPERR_NO_ERROR;
}

NPError PluginClient::Shutdown() {
  return NPERR_NO_ERROR;
}

} // namespace NPAPIClient

extern "C" {
NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode,
                int16 argc, char* argn[], char* argv[], NPSavedData* saved) {
  if (instance == NULL)
    return NPERR_INVALID_INSTANCE_ERROR;

  // We look at the test name requested via the plugin arguments.  We match
  // that against a given test and try to instantiate it.

  // lookup the name parameter
  std::string test_name;
  for (int name_index = 0; name_index < argc; name_index++) {
    if (base::strcasecmp(argn[name_index], "name") == 0) {
      test_name = argv[name_index];
      break;
    }
  }
  if (test_name.empty())
    return NPERR_GENERIC_ERROR;  // no name found

  NPAPIClient::PluginTest* new_test = NPAPIClient::CreatePluginTest(test_name,
      instance, NPAPIClient::PluginClient::HostFunctions());
  if (new_test == NULL) {
    // If we don't have a test case for this, create a
    // generic one which basically never fails.
    LOG(WARNING) << "Unknown test name '" << test_name
                 << "'; using default test.";
    new_test = new NPAPIClient::PluginTest(instance,
        NPAPIClient::PluginClient::HostFunctions());
  }

  NPError ret = new_test->New(mode, argc, (const char**)argn,
      (const char**)argv, saved);
  if ((ret == NPERR_NO_ERROR) && new_test->IsWindowless()) {
    NPAPIClient::PluginClient::HostFunctions()->setvalue(
          instance, NPPVpluginWindowBool, NULL);
  }

  return ret;
}

NPError NPP_Destroy(NPP instance, NPSavedData** save) {
  if (instance == NULL)
    return NPERR_INVALID_INSTANCE_ERROR;

  NPAPIClient::PluginTest* plugin =
      reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);

  NPError rv = plugin->Destroy();
  delete plugin;
  return rv;
}

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

  NPAPIClient::PluginTest* plugin =
      reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);

  return plugin->SetWindow(pNPWindow);
}

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

  NPAPIClient::PluginTest* plugin =
      reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);

  return plugin->NewStream(type, stream, seekable, stype);
}

int32 NPP_WriteReady(NPP instance, NPStream *stream) {
  if (instance == NULL)
    return NPERR_INVALID_INSTANCE_ERROR;

  NPAPIClient::PluginTest* plugin =
      reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);

  return plugin->WriteReady(stream);
}

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

  NPAPIClient::PluginTest* plugin =
      reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);

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

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

  NPAPIClient::PluginTest* plugin =
      reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);

  return plugin->DestroyStream(stream, reason);
}

void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname) {
  if (instance == NULL)
    return;

  NPAPIClient::PluginTest* plugin =
      reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);

  return plugin->StreamAsFile(stream, fname);
}

void NPP_Print(NPP instance, NPPrint* printInfo) {
  if (instance == NULL)
    return;

  // XXXMB - do work here.
}

void NPP_URLNotify(NPP instance, const char* url, NPReason reason,
                   void* notifyData) {
  if (instance == NULL)
    return;

  NPAPIClient::PluginTest* plugin =
      reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);

  return plugin->URLNotify(url, reason, notifyData);
}

NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) {
  if (instance == NULL)
    return NPERR_INVALID_INSTANCE_ERROR;

  // XXXMB - do work here.
  return NPERR_GENERIC_ERROR;
}

NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value) {
  if (instance == NULL)
    return NPERR_INVALID_INSTANCE_ERROR;

  // XXXMB - do work here.
  return NPERR_GENERIC_ERROR;
}

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

  NPAPIClient::PluginTest* plugin =
      reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);

  return plugin->HandleEvent(event);
}

void NPP_URLRedirectNotify(NPP instance, const char* url, int32_t status,
                           void* notify_data) {
  if (instance) {
    NPAPIClient::PluginTest* plugin =
        reinterpret_cast<NPAPIClient::PluginTest*>(instance->pdata);
    plugin->URLRedirectNotify(url, status, notify_data);
  }
}
} // extern "C"