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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
// Copyright (c) 2010 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/pepper_plugin_instance.h"
#include "base/logging.h"
#include "base/scoped_ptr.h"
#include "base/utf_string_conversions.h"
#include "gfx/rect.h"
#include "third_party/ppapi/c/pp_instance.h"
#include "third_party/ppapi/c/pp_event.h"
#include "third_party/ppapi/c/pp_rect.h"
#include "third_party/ppapi/c/pp_resource.h"
#include "third_party/ppapi/c/pp_var.h"
#include "third_party/ppapi/c/ppb_find.h"
#include "third_party/ppapi/c/ppb_instance.h"
#include "third_party/ppapi/c/ppp_find.h"
#include "third_party/ppapi/c/ppp_instance.h"
#include "third_party/ppapi/c/ppp_zoom.h"
#include "third_party/WebKit/WebKit/chromium/public/WebCursorInfo.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/WebKit/chromium/public/WebElement.h"
#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/WebKit/chromium/public/WebInputEvent.h"
#include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h"
#include "third_party/WebKit/WebKit/chromium/public/WebRect.h"
#include "webkit/glue/plugins/pepper_device_context_2d.h"
#include "webkit/glue/plugins/pepper_event_conversion.h"
#include "webkit/glue/plugins/pepper_image_data.h"
#include "webkit/glue/plugins/pepper_plugin_delegate.h"
#include "webkit/glue/plugins/pepper_plugin_module.h"
#include "webkit/glue/plugins/pepper_string.h"
#include "webkit/glue/plugins/pepper_url_loader.h"
#include "webkit/glue/plugins/pepper_var.h"
using WebKit::WebCanvas;
using WebKit::WebCursorInfo;
using WebKit::WebFrame;
using WebKit::WebInputEvent;
using WebKit::WebPluginContainer;
namespace pepper {
namespace {
#define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, np_name) \
COMPILE_ASSERT(int(WebCursorInfo::webkit_name) == int(np_name), \
mismatching_enums)
COMPILE_ASSERT_MATCHING_ENUM(TypePointer, PP_CURSORTYPE_POINTER);
COMPILE_ASSERT_MATCHING_ENUM(TypeCross, PP_CURSORTYPE_CROSS);
COMPILE_ASSERT_MATCHING_ENUM(TypeHand, PP_CURSORTYPE_HAND);
COMPILE_ASSERT_MATCHING_ENUM(TypeIBeam, PP_CURSORTYPE_IBEAM);
COMPILE_ASSERT_MATCHING_ENUM(TypeWait, PP_CURSORTYPE_WAIT);
COMPILE_ASSERT_MATCHING_ENUM(TypeHelp, PP_CURSORTYPE_HELP);
COMPILE_ASSERT_MATCHING_ENUM(TypeEastResize, PP_CURSORTYPE_EASTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthResize, PP_CURSORTYPE_NORTHRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthEastResize,
PP_CURSORTYPE_NORTHEASTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthWestResize,
PP_CURSORTYPE_NORTHWESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthResize, PP_CURSORTYPE_SOUTHRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthEastResize,
PP_CURSORTYPE_SOUTHEASTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthWestResize,
PP_CURSORTYPE_SOUTHWESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeWestResize, PP_CURSORTYPE_WESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthSouthResize,
PP_CURSORTYPE_NORTHSOUTHRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeEastWestResize, PP_CURSORTYPE_EASTWESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthEastSouthWestResize,
PP_CURSORTYPE_NORTHEASTSOUTHWESTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthWestSouthEastResize,
PP_CURSORTYPE_NORTHWESTSOUTHEASTRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeColumnResize, PP_CURSORTYPE_COLUMNRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeRowResize, PP_CURSORTYPE_ROWRESIZE);
COMPILE_ASSERT_MATCHING_ENUM(TypeMiddlePanning, PP_CURSORTYPE_MIDDLEPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeEastPanning, PP_CURSORTYPE_EASTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthPanning, PP_CURSORTYPE_NORTHPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthEastPanning,
PP_CURSORTYPE_NORTHEASTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeNorthWestPanning,
PP_CURSORTYPE_NORTHWESTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthPanning, PP_CURSORTYPE_SOUTHPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthEastPanning,
PP_CURSORTYPE_SOUTHEASTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeSouthWestPanning,
PP_CURSORTYPE_SOUTHWESTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeWestPanning, PP_CURSORTYPE_WESTPANNING);
COMPILE_ASSERT_MATCHING_ENUM(TypeMove, PP_CURSORTYPE_MOVE);
COMPILE_ASSERT_MATCHING_ENUM(TypeVerticalText, PP_CURSORTYPE_VERTICALTEXT);
COMPILE_ASSERT_MATCHING_ENUM(TypeCell, PP_CURSORTYPE_CELL);
COMPILE_ASSERT_MATCHING_ENUM(TypeContextMenu, PP_CURSORTYPE_CONTEXTMENU);
COMPILE_ASSERT_MATCHING_ENUM(TypeAlias, PP_CURSORTYPE_ALIAS);
COMPILE_ASSERT_MATCHING_ENUM(TypeProgress, PP_CURSORTYPE_PROGRESS);
COMPILE_ASSERT_MATCHING_ENUM(TypeNoDrop, PP_CURSORTYPE_NODROP);
COMPILE_ASSERT_MATCHING_ENUM(TypeCopy, PP_CURSORTYPE_COPY);
COMPILE_ASSERT_MATCHING_ENUM(TypeNone, PP_CURSORTYPE_NONE);
COMPILE_ASSERT_MATCHING_ENUM(TypeNotAllowed, PP_CURSORTYPE_NOTALLOWED);
COMPILE_ASSERT_MATCHING_ENUM(TypeZoomIn, PP_CURSORTYPE_ZOOMIN);
COMPILE_ASSERT_MATCHING_ENUM(TypeZoomOut, PP_CURSORTYPE_ZOOMOUT);
COMPILE_ASSERT_MATCHING_ENUM(TypeCustom, PP_CURSORTYPE_CUSTOM);
void RectToPPRect(const gfx::Rect& input, PP_Rect* output) {
*output = PP_MakeRectFromXYWH(input.x(), input.y(),
input.width(), input.height());
}
PP_Var GetWindowObject(PP_Instance instance_id) {
PluginInstance* instance = PluginInstance::FromPPInstance(instance_id);
if (!instance)
return PP_MakeVoid();
return instance->GetWindowObject();
}
PP_Var GetOwnerElementObject(PP_Instance instance_id) {
PluginInstance* instance = PluginInstance::FromPPInstance(instance_id);
if (!instance)
return PP_MakeVoid();
return instance->GetOwnerElementObject();
}
bool BindGraphicsDeviceContext(PP_Instance instance_id, PP_Resource device_id) {
PluginInstance* instance = PluginInstance::FromPPInstance(instance_id);
if (!instance)
return false;
return instance->BindGraphicsDeviceContext(device_id);
}
bool IsFullFrame(PP_Instance instance_id) {
PluginInstance* instance = PluginInstance::FromPPInstance(instance_id);
if (!instance)
return false;
return instance->full_frame();
}
bool SetCursor(PP_Instance instance_id,
PP_CursorType type,
PP_Resource custom_image_id,
const PP_Point* hot_spot) {
PluginInstance* instance = PluginInstance::FromPPInstance(instance_id);
if (!instance)
return false;
scoped_refptr<ImageData> custom_image(
Resource::GetAs<ImageData>(custom_image_id));
if (custom_image.get()) {
// TODO: implement custom cursors.
NOTIMPLEMENTED();
return false;
}
return instance->SetCursor(type);
}
const PPB_Instance ppb_instance = {
&GetWindowObject,
&GetOwnerElementObject,
&BindGraphicsDeviceContext,
&IsFullFrame,
&SetCursor,
};
void NumberOfFindResultsChanged(PP_Instance instance_id,
int32_t total,
bool final_result) {
PluginInstance* instance = PluginInstance::FromPPInstance(instance_id);
if (!instance)
return;
DCHECK_NE(instance->find_identifier(), -1);
instance->delegate()->DidChangeNumberOfFindResults(
instance->find_identifier(), total, final_result);
}
void SelectedFindResultChanged(PP_Instance instance_id,
int32_t index) {
PluginInstance* instance = PluginInstance::FromPPInstance(instance_id);
if (!instance)
return;
DCHECK_NE(instance->find_identifier(), -1);
instance->delegate()->DidChangeSelectedFindResult(
instance->find_identifier(), index);
}
const PPB_Find ppb_find = {
&NumberOfFindResultsChanged,
&SelectedFindResultChanged,
};
} // namespace
PluginInstance::PluginInstance(PluginDelegate* delegate,
PluginModule* module,
const PPP_Instance* instance_interface)
: delegate_(delegate),
module_(module),
instance_interface_(instance_interface),
container_(NULL),
full_frame_(false),
find_identifier_(-1),
plugin_find_interface_(NULL),
plugin_zoom_interface_(NULL) {
DCHECK(delegate);
module_->InstanceCreated(this);
delegate_->InstanceCreated(this);
}
PluginInstance::~PluginInstance() {
delegate_->InstanceDeleted(this);
module_->InstanceDeleted(this);
}
// static
const PPB_Instance* PluginInstance::GetInterface() {
return &ppb_instance;
}
// static
PluginInstance* PluginInstance::FromPPInstance(PP_Instance instance) {
return reinterpret_cast<PluginInstance*>(instance);
}
// static
const PPB_Find* PluginInstance::GetFindInterface() {
return &ppb_find;
}
PP_Instance PluginInstance::GetPPInstance() {
return reinterpret_cast<intptr_t>(this);
}
void PluginInstance::Paint(WebCanvas* canvas,
const gfx::Rect& plugin_rect,
const gfx::Rect& paint_rect) {
if (device_context_2d_)
device_context_2d_->Paint(canvas, plugin_rect, paint_rect);
}
void PluginInstance::InvalidateRect(const gfx::Rect& rect) {
if (!container_ || position_.IsEmpty())
return; // Nothing to do.
if (rect.IsEmpty())
container_->invalidate();
else
container_->invalidateRect(rect);
}
PP_Var PluginInstance::GetWindowObject() {
if (!container_)
return PP_MakeVoid();
WebFrame* frame = container_->element().document().frame();
if (!frame)
return PP_MakeVoid();
return NPObjectToPPVar(frame->windowObject());
}
PP_Var PluginInstance::GetOwnerElementObject() {
if (!container_)
return PP_MakeVoid();
return NPObjectToPPVar(container_->scriptableObjectForElement());
}
bool PluginInstance::BindGraphicsDeviceContext(PP_Resource device_id) {
if (!device_id) {
// Special-case clearing the current device.
if (device_context_2d_) {
device_context_2d_->BindToInstance(NULL);
device_context_2d_ = NULL;
InvalidateRect(gfx::Rect());
}
return true;
}
scoped_refptr<DeviceContext2D> device_2d =
Resource::GetAs<DeviceContext2D>(device_id);
if (device_2d) {
if (!device_2d->BindToInstance(this))
return false; // Can't bind to more than one instance.
device_context_2d_ = device_2d;
// BindToInstance will have invalidated the plugin if necessary.
}
return true;
}
bool PluginInstance::SetCursor(PP_CursorType type) {
cursor_.reset(new WebCursorInfo(static_cast<WebCursorInfo::Type>(type)));
return true;
}
void PluginInstance::Delete() {
instance_interface_->Delete(GetPPInstance());
container_ = NULL;
}
bool PluginInstance::Initialize(WebPluginContainer* container,
const std::vector<std::string>& arg_names,
const std::vector<std::string>& arg_values,
bool full_frame) {
container_ = container;
full_frame_ = full_frame;
if (!instance_interface_->New(GetPPInstance()))
return false;
size_t argc = 0;
scoped_array<const char*> argn(new const char*[arg_names.size()]);
scoped_array<const char*> argv(new const char*[arg_names.size()]);
for (size_t i = 0; i < arg_names.size(); ++i) {
argn[argc] = arg_names[i].c_str();
argv[argc] = arg_values[i].c_str();
argc++;
}
return instance_interface_->Initialize(GetPPInstance(),
argc, argn.get(), argv.get());
}
bool PluginInstance::HandleDocumentLoad(URLLoader* loader) {
return instance_interface_->HandleDocumentLoad(GetPPInstance(),
loader->GetResource());
}
bool PluginInstance::HandleInputEvent(const WebKit::WebInputEvent& event,
WebCursorInfo* cursor_info) {
scoped_ptr<PP_Event> pp_event(CreatePP_Event(event));
if (!pp_event.get())
return false;
bool rv = instance_interface_->HandleEvent(GetPPInstance(), pp_event.get());
if (cursor_.get())
*cursor_info = *cursor_;
return rv;
}
PP_Var PluginInstance::GetInstanceObject() {
return instance_interface_->GetInstanceObject(GetPPInstance());
}
void PluginInstance::ViewChanged(const gfx::Rect& position,
const gfx::Rect& clip) {
position_ = position;
if (clip.IsEmpty()) {
// WebKit can give weird (x,y) positions for empty clip rects (since the
// position technically doesn't matter). But we want to make these
// consistent since this is given to the plugin, so force everything to 0
// in the "everything is clipped" case.
clip_ = gfx::Rect();
} else {
clip_ = clip;
}
PP_Rect pp_position, pp_clip;
RectToPPRect(position_, &pp_position);
RectToPPRect(clip_, &pp_clip);
instance_interface_->ViewChanged(GetPPInstance(), &pp_position, &pp_clip);
}
void PluginInstance::ViewInitiatedPaint() {
if (device_context_2d_)
device_context_2d_->ViewInitiatedPaint();
}
void PluginInstance::ViewFlushedPaint() {
if (device_context_2d_)
device_context_2d_->ViewFlushedPaint();
}
string16 PluginInstance::GetSelectedText(bool html) {
PP_Var rv = instance_interface_->GetSelectedText(GetPPInstance(), html);
String* string = GetString(rv);
if (!string)
return string16();
return UTF8ToUTF16(string->value());
}
void PluginInstance::Zoom(float factor, bool text_only) {
if (!LoadZoomInterface())
return;
plugin_zoom_interface_->Zoom(GetPPInstance(), factor, text_only);
}
bool PluginInstance::StartFind(const string16& search_text,
bool case_sensitive,
int identifier) {
if (!LoadFindInterface())
return false;
find_identifier_ = identifier;
return plugin_find_interface_->StartFind(
GetPPInstance(),
UTF16ToUTF8(search_text.c_str()).c_str(),
case_sensitive);
}
void PluginInstance::SelectFindResult(bool forward) {
if (LoadFindInterface())
plugin_find_interface_->SelectFindResult(GetPPInstance(), forward);
}
void PluginInstance::StopFind() {
if (!LoadFindInterface())
return;
find_identifier_ = -1;
plugin_find_interface_->StopFind(GetPPInstance());
}
bool PluginInstance::LoadFindInterface() {
if (!plugin_find_interface_) {
plugin_find_interface_ =
reinterpret_cast<const PPP_Find*>(module_->GetPluginInterface(
PPP_FIND_INTERFACE));
}
return !!plugin_find_interface_;
}
bool PluginInstance::LoadZoomInterface() {
if (!plugin_zoom_interface_) {
plugin_zoom_interface_ =
reinterpret_cast<const PPP_Zoom*>(module_->GetPluginInterface(
PPP_ZOOM_INTERFACE));
}
return !!plugin_zoom_interface_;
}
} // namespace pepper
|