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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
// 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.
#define PEPPER_APIS_ENABLED 1
#include "webkit/glue/plugins/webplugin_delegate_pepper_impl.h"
#include <string>
#include <vector>
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/process_util.h"
#include "base/scoped_ptr.h"
#include "base/stats_counters.h"
#include "base/string_util.h"
#include "webkit/api/public/WebInputEvent.h"
#include "webkit/glue/glue_util.h"
#include "webkit/glue/plugins/plugin_constants_win.h"
#include "webkit/glue/plugins/plugin_instance.h"
#include "webkit/glue/plugins/plugin_lib.h"
#include "webkit/glue/plugins/plugin_list.h"
#include "webkit/glue/plugins/plugin_stream_url.h"
#include "webkit/glue/webkit_glue.h"
using webkit_glue::WebPlugin;
using webkit_glue::WebPluginDelegate;
using webkit_glue::WebPluginResourceClient;
using WebKit::WebCursorInfo;
using WebKit::WebKeyboardEvent;
using WebKit::WebInputEvent;
using WebKit::WebMouseEvent;
using WebKit::WebMouseWheelEvent;
WebPluginDelegatePepperImpl* WebPluginDelegatePepperImpl::Create(
const FilePath& filename,
const std::string& mime_type,
gfx::PluginWindowHandle containing_view) {
scoped_refptr<NPAPI::PluginLib> plugin_lib =
NPAPI::PluginLib::CreatePluginLib(filename);
if (plugin_lib.get() == NULL)
return NULL;
NPError err = plugin_lib->NP_Initialize();
if (err != NPERR_NO_ERROR)
return NULL;
scoped_refptr<NPAPI::PluginInstance> instance =
plugin_lib->CreateInstance(mime_type);
return new WebPluginDelegatePepperImpl(containing_view, instance.get());
}
bool WebPluginDelegatePepperImpl::Initialize(
const GURL& url,
const std::vector<std::string>& arg_names,
const std::vector<std::string>& arg_values,
WebPlugin* plugin,
bool load_manually) {
plugin_ = plugin;
instance_->set_web_plugin(plugin_);
int argc = 0;
scoped_array<char*> argn(new char*[arg_names.size()]);
scoped_array<char*> argv(new char*[arg_names.size()]);
for (size_t i = 0; i < arg_names.size(); ++i) {
argn[argc] = const_cast<char*>(arg_names[i].c_str());
argv[argc] = const_cast<char*>(arg_values[i].c_str());
argc++;
}
bool start_result = instance_->Start(
url, argn.get(), argv.get(), argc, load_manually);
if (!start_result)
return false;
// For windowless plugins we should set the containing window handle
// as the instance window handle. This is what Safari does. Not having
// a valid window handle causes subtle bugs with plugins which retreive
// the window handle and validate the same. The window handle can be
// retreived via NPN_GetValue of NPNVnetscapeWindow.
instance_->set_window_handle(parent_);
plugin_url_ = url.spec();
return true;
}
void WebPluginDelegatePepperImpl::DestroyInstance() {
if (instance_ && (instance_->npp()->ndata != NULL)) {
// Shutdown all streams before destroying so that
// no streams are left "in progress". Need to do
// this before calling set_web_plugin(NULL) because the
// instance uses the helper to do the download.
instance_->CloseStreams();
window_.window = NULL;
instance_->NPP_SetWindow(&window_);
instance_->NPP_Destroy();
instance_->set_web_plugin(NULL);
instance_ = 0;
}
}
void WebPluginDelegatePepperImpl::UpdateGeometry(
const gfx::Rect& window_rect,
const gfx::Rect& clip_rect) {
WindowlessUpdateGeometry(window_rect, clip_rect);
}
NPObject* WebPluginDelegatePepperImpl::GetPluginScriptableObject() {
return instance_->GetPluginScriptableObject();
}
void WebPluginDelegatePepperImpl::DidFinishLoadWithReason(
const GURL& url,
NPReason reason,
intptr_t notify_data) {
instance()->DidFinishLoadWithReason(
url, reason, reinterpret_cast<void*>(notify_data));
}
int WebPluginDelegatePepperImpl::GetProcessId() {
// We are in process, so the plugin pid is this current process pid.
return base::GetCurrentProcId();
}
void WebPluginDelegatePepperImpl::SendJavaScriptStream(
const GURL& url,
const std::string& result,
bool success,
bool notify_needed,
intptr_t notify_data) {
instance()->SendJavaScriptStream(url, result, success, notify_needed,
notify_data);
}
void WebPluginDelegatePepperImpl::DidReceiveManualResponse(
const GURL& url, const std::string& mime_type,
const std::string& headers, uint32 expected_length, uint32 last_modified) {
instance()->DidReceiveManualResponse(url, mime_type, headers,
expected_length, last_modified);
}
void WebPluginDelegatePepperImpl::DidReceiveManualData(const char* buffer,
int length) {
instance()->DidReceiveManualData(buffer, length);
}
void WebPluginDelegatePepperImpl::DidFinishManualLoading() {
instance()->DidFinishManualLoading();
}
void WebPluginDelegatePepperImpl::DidManualLoadFail() {
instance()->DidManualLoadFail();
}
FilePath WebPluginDelegatePepperImpl::GetPluginPath() {
return instance()->plugin_lib()->plugin_info().path;
}
WebPluginResourceClient* WebPluginDelegatePepperImpl::CreateResourceClient(
int resource_id, const GURL& url, bool notify_needed,
intptr_t notify_data, intptr_t existing_stream) {
// Stream already exists. This typically happens for range requests
// initiated via NPN_RequestRead.
if (existing_stream) {
NPAPI::PluginStream* plugin_stream =
reinterpret_cast<NPAPI::PluginStream*>(existing_stream);
return plugin_stream->AsResourceClient();
}
std::string mime_type;
NPAPI::PluginStreamUrl *stream = instance()->CreateStream(
resource_id, url, mime_type, notify_needed,
reinterpret_cast<void*>(notify_data));
return stream;
}
bool WebPluginDelegatePepperImpl::IsPluginDelegateWindow(
gfx::NativeWindow window) {
return false;
}
bool WebPluginDelegatePepperImpl::GetPluginNameFromWindow(
gfx::NativeWindow window, std::wstring *plugin_name) {
return false;
}
bool WebPluginDelegatePepperImpl::IsDummyActivationWindow(
gfx::NativeWindow window) {
return false;
}
WebPluginDelegatePepperImpl::WebPluginDelegatePepperImpl(
gfx::PluginWindowHandle containing_view,
NPAPI::PluginInstance *instance)
: plugin_(NULL),
instance_(instance),
parent_(containing_view) {
memset(&window_, 0, sizeof(window_));
}
WebPluginDelegatePepperImpl::~WebPluginDelegatePepperImpl() {
DestroyInstance();
}
void WebPluginDelegatePepperImpl::PluginDestroyed() {
delete this;
}
void WebPluginDelegatePepperImpl::Paint(gfx::NativeDrawingContext context, const gfx::Rect& rect) {
NOTIMPLEMENTED();
}
void WebPluginDelegatePepperImpl::Print(gfx::NativeDrawingContext context) {
NOTIMPLEMENTED();
}
void WebPluginDelegatePepperImpl::InstallMissingPlugin() {
NOTIMPLEMENTED();
}
void WebPluginDelegatePepperImpl::WindowlessUpdateGeometry(
const gfx::Rect& window_rect,
const gfx::Rect& clip_rect) {
// Only resend to the instance if the geometry has changed.
if (window_rect == window_rect_ && clip_rect == clip_rect_)
return;
// We will inform the instance of this change when we call NPP_SetWindow.
clip_rect_ = clip_rect;
cutout_rects_.clear();
if (window_rect_ != window_rect) {
window_rect_ = window_rect;
WindowlessSetWindow(true);
// TODO(sehr): update the context here?
}
}
void WebPluginDelegatePepperImpl::WindowlessPaint(
gfx::NativeDrawingContext context,
const gfx::Rect& damage_rect) {
static StatsRate plugin_paint("Plugin.Paint");
StatsScope<StatsRate> scope(plugin_paint);
// TODO(sehr): save the context here?
}
void WebPluginDelegatePepperImpl::WindowlessSetWindow(bool force_set_window) {
if (!instance())
return;
if (window_rect_.IsEmpty()) // wait for geometry to be set.
return;
window_.clipRect.top = clip_rect_.y();
window_.clipRect.left = clip_rect_.x();
window_.clipRect.bottom = clip_rect_.y() + clip_rect_.height();
window_.clipRect.right = clip_rect_.x() + clip_rect_.width();
window_.height = window_rect_.height();
window_.width = window_rect_.width();
window_.x = window_rect_.x();
window_.y = window_rect_.y();
window_.type = NPWindowTypeDrawable;
NPError err = instance()->NPP_SetWindow(&window_);
DCHECK(err == NPERR_NO_ERROR);
}
void WebPluginDelegatePepperImpl::SetFocus() {
NPEvent npevent;
npevent.type = NPEventType_Focus;
npevent.size = sizeof(NPEvent);
// TODO(sehr): what timestamp should this have?
npevent.timeStampSeconds = 0.0;
// Currently this API only supports gaining focus.
npevent.u.focus.value = 1;
instance()->NPP_HandleEvent(&npevent);
}
// Anonymous namespace for functions converting WebInputEvents to NPAPI types.
namespace {
NPEventTypes ConvertEventTypes(WebInputEvent::Type wetype) {
switch (wetype) {
case WebInputEvent::MouseDown:
return NPEventType_MouseDown;
case WebInputEvent::MouseUp:
return NPEventType_MouseUp;
case WebInputEvent::MouseMove:
return NPEventType_MouseMove;
case WebInputEvent::MouseEnter:
return NPEventType_MouseEnter;
case WebInputEvent::MouseLeave:
return NPEventType_MouseLeave;
case WebInputEvent::MouseWheel:
return NPEventType_MouseWheel;
case WebInputEvent::RawKeyDown:
return NPEventType_RawKeyDown;
case WebInputEvent::KeyDown:
return NPEventType_KeyDown;
case WebInputEvent::KeyUp:
return NPEventType_KeyUp;
case WebInputEvent::Char:
return NPEventType_Char;
case WebInputEvent::Undefined:
default:
return NPEventType_Undefined;
}
}
void BuildKeyEvent(const WebInputEvent* event, NPEvent* npevent) {
const WebKeyboardEvent* key_event =
reinterpret_cast<const WebKeyboardEvent*>(event);
npevent->u.key.modifier = key_event->modifiers;
npevent->u.key.normalizedKeyCode = key_event->windowsKeyCode;
}
void BuildCharEvent(const WebInputEvent* event, NPEvent* npevent) {
const WebKeyboardEvent* key_event =
reinterpret_cast<const WebKeyboardEvent*>(event);
npevent->u.character.modifier = key_event->modifiers;
// For consistency, check that the sizes of the texts agree.
DCHECK(sizeof(npevent->u.character.text) == sizeof(key_event->text));
DCHECK(sizeof(npevent->u.character.unmodifiedText) ==
sizeof(key_event->unmodifiedText));
for (size_t i = 0; i < WebKeyboardEvent::textLengthCap; ++i) {
npevent->u.character.text[i] = key_event->text[i];
npevent->u.character.unmodifiedText[i] = key_event->unmodifiedText[i];
}
}
void BuildMouseEvent(const WebInputEvent* event, NPEvent* npevent) {
const WebMouseEvent* mouse_event =
reinterpret_cast<const WebMouseEvent*>(event);
npevent->u.mouse.modifier = mouse_event->modifiers;
npevent->u.mouse.button = mouse_event->button;
npevent->u.mouse.x = mouse_event->x;
npevent->u.mouse.y = mouse_event->y;
npevent->u.mouse.clickCount = mouse_event->clickCount;
}
void BuildMouseWheelEvent(const WebInputEvent* event, NPEvent* npevent) {
const WebMouseWheelEvent* mouse_wheel_event =
reinterpret_cast<const WebMouseWheelEvent*>(event);
npevent->u.wheel.modifier = mouse_wheel_event->modifiers;
npevent->u.wheel.deltaX = mouse_wheel_event->deltaX;
npevent->u.wheel.deltaY = mouse_wheel_event->deltaY;
npevent->u.wheel.wheelTicksX = mouse_wheel_event->wheelTicksX;
npevent->u.wheel.wheelTicksY = mouse_wheel_event->wheelTicksY;
npevent->u.wheel.scrollByPage = mouse_wheel_event->scrollByPage;
}
} // namespace
bool WebPluginDelegatePepperImpl::HandleInputEvent(const WebInputEvent& event,
WebCursorInfo* cursor_info) {
NPEvent npevent;
npevent.type = ConvertEventTypes(event.type);
npevent.size = sizeof(NPEvent);
npevent.timeStampSeconds = event.timeStampSeconds;
switch (npevent.type) {
case NPEventType_Undefined:
return false;
case NPEventType_MouseDown:
case NPEventType_MouseUp:
case NPEventType_MouseMove:
case NPEventType_MouseEnter:
case NPEventType_MouseLeave:
BuildMouseEvent(&event, &npevent);
break;
case NPEventType_MouseWheel:
BuildMouseWheelEvent(&event, &npevent);
break;
case NPEventType_RawKeyDown:
case NPEventType_KeyDown:
case NPEventType_KeyUp:
BuildKeyEvent(&event, &npevent);
case NPEventType_Char:
BuildCharEvent(&event, &npevent);
break;
case NPEventType_Minimize:
case NPEventType_Focus:
case NPEventType_Device:
NOTIMPLEMENTED();
break;
}
return instance()->NPP_HandleEvent(&npevent) != 0;
}
|