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
|
// 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 "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/ppb_instance.h"
#include "third_party/ppapi/c/ppp_instance.h"
#include "third_party/WebKit/WebKit/chromium/public/WebInputEvent.h"
#include "webkit/glue/plugins/pepper_device_context_2d.h"
#include "webkit/glue/plugins/pepper_plugin_module.h"
#include "webkit/glue/plugins/pepper_resource_tracker.h"
using WebKit::WebInputEvent;
namespace pepper {
namespace {
void RectToPPRect(const gfx::Rect& input, PP_Rect* output) {
output->left = input.x();
output->top = input.y();
output->right = input.right();
output->bottom = input.bottom();
}
PP_Event_Type ConvertEventTypes(WebInputEvent::Type wetype) {
switch (wetype) {
case WebInputEvent::MouseDown:
return PP_Event_Type_MouseDown;
case WebInputEvent::MouseUp:
return PP_Event_Type_MouseUp;
case WebInputEvent::MouseMove:
return PP_Event_Type_MouseMove;
case WebInputEvent::MouseEnter:
return PP_Event_Type_MouseEnter;
case WebInputEvent::MouseLeave:
return PP_Event_Type_MouseLeave;
case WebInputEvent::MouseWheel:
return PP_Event_Type_MouseWheel;
case WebInputEvent::RawKeyDown:
return PP_Event_Type_RawKeyDown;
case WebInputEvent::KeyDown:
return PP_Event_Type_KeyDown;
case WebInputEvent::KeyUp:
return PP_Event_Type_KeyUp;
case WebInputEvent::Char:
return PP_Event_Type_Char;
case WebInputEvent::Undefined:
default:
return PP_Event_Type_Undefined;
}
}
void BuildKeyEvent(const WebInputEvent* event, PP_Event* pp_event) {
const WebKit::WebKeyboardEvent* key_event =
reinterpret_cast<const WebKit::WebKeyboardEvent*>(event);
pp_event->u.key.modifier = key_event->modifiers;
pp_event->u.key.normalizedKeyCode = key_event->windowsKeyCode;
}
void BuildCharEvent(const WebInputEvent* event, PP_Event* pp_event) {
const WebKit::WebKeyboardEvent* key_event =
reinterpret_cast<const WebKit::WebKeyboardEvent*>(event);
pp_event->u.character.modifier = key_event->modifiers;
// For consistency, check that the sizes of the texts agree.
DCHECK(sizeof(pp_event->u.character.text) == sizeof(key_event->text));
DCHECK(sizeof(pp_event->u.character.unmodifiedText) ==
sizeof(key_event->unmodifiedText));
for (size_t i = 0; i < WebKit::WebKeyboardEvent::textLengthCap; ++i) {
pp_event->u.character.text[i] = key_event->text[i];
pp_event->u.character.unmodifiedText[i] = key_event->unmodifiedText[i];
}
}
void BuildMouseEvent(const WebInputEvent* event, PP_Event* pp_event) {
const WebKit::WebMouseEvent* mouse_event =
reinterpret_cast<const WebKit::WebMouseEvent*>(event);
pp_event->u.mouse.modifier = mouse_event->modifiers;
pp_event->u.mouse.button = mouse_event->button;
pp_event->u.mouse.x = mouse_event->x;
pp_event->u.mouse.y = mouse_event->y;
pp_event->u.mouse.clickCount = mouse_event->clickCount;
}
void BuildMouseWheelEvent(const WebInputEvent* event, PP_Event* pp_event) {
const WebKit::WebMouseWheelEvent* mouse_wheel_event =
reinterpret_cast<const WebKit::WebMouseWheelEvent*>(event);
pp_event->u.wheel.modifier = mouse_wheel_event->modifiers;
pp_event->u.wheel.deltaX = mouse_wheel_event->deltaX;
pp_event->u.wheel.deltaY = mouse_wheel_event->deltaY;
pp_event->u.wheel.wheelTicksX = mouse_wheel_event->wheelTicksX;
pp_event->u.wheel.wheelTicksY = mouse_wheel_event->wheelTicksY;
pp_event->u.wheel.scrollByPage = mouse_wheel_event->scrollByPage;
}
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);
}
const PPB_Instance ppb_instance = {
&BindGraphicsDeviceContext,
};
} // namespace
PluginInstance::PluginInstance(PluginDelegate* delegate,
PluginModule* module,
const PPP_Instance* instance_interface)
: delegate_(delegate),
module_(module),
instance_interface_(instance_interface) {
DCHECK(delegate);
module_->InstanceCreated(this);
}
PluginInstance::~PluginInstance() {
module_->InstanceDeleted(this);
}
// static
const PPB_Instance* PluginInstance::GetInterface() {
return &ppb_instance;
}
// static
PluginInstance* PluginInstance::FromPPInstance(PP_Instance instance) {
return reinterpret_cast<PluginInstance*>(instance.id);
}
PP_Instance PluginInstance::GetPPInstance() {
PP_Instance ret;
ret.id = reinterpret_cast<intptr_t>(this);
return ret;
}
void PluginInstance::Paint(WebKit::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);
}
bool PluginInstance::BindGraphicsDeviceContext(PP_Resource device_id) {
scoped_refptr<Resource> device_resource =
ResourceTracker::Get()->GetResource(device_id);
if (!device_resource.get())
return false;
DeviceContext2D* device_2d = device_resource->AsDeviceContext2D();
if (device_2d) {
device_context_2d_ = device_2d;
// TODO(brettw) repaint the plugin.
}
return true;
}
void PluginInstance::Delete() {
instance_interface_->Delete(GetPPInstance());
}
bool PluginInstance::Initialize(const std::vector<std::string>& arg_names,
const std::vector<std::string>& arg_values) {
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::HandleInputEvent(const WebKit::WebInputEvent& event,
WebKit::WebCursorInfo* cursor_info) {
PP_Event pp_event;
pp_event.type = ConvertEventTypes(event.type);
pp_event.size = sizeof(pp_event);
pp_event.time_stamp_seconds = event.timeStampSeconds;
switch (pp_event.type) {
case PP_Event_Type_Undefined:
return false;
case PP_Event_Type_MouseDown:
case PP_Event_Type_MouseUp:
case PP_Event_Type_MouseMove:
case PP_Event_Type_MouseEnter:
case PP_Event_Type_MouseLeave:
BuildMouseEvent(&event, &pp_event);
break;
case PP_Event_Type_MouseWheel:
BuildMouseWheelEvent(&event, &pp_event);
break;
case PP_Event_Type_RawKeyDown:
case PP_Event_Type_KeyDown:
case PP_Event_Type_KeyUp:
BuildKeyEvent(&event, &pp_event);
break;
case PP_Event_Type_Char:
BuildCharEvent(&event, &pp_event);
break;
}
return instance_interface_->HandleEvent(GetPPInstance(), &pp_event);
}
void PluginInstance::ViewChanged(const gfx::Rect& position,
const gfx::Rect& clip) {
PP_Rect pp_position, pp_clip;
RectToPPRect(position, &pp_position);
RectToPPRect(clip, &pp_clip);
instance_interface_->ViewChanged(GetPPInstance(), &pp_position, &pp_clip);
}
} // namespace pepper
|