OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/glue/plugins/pepper_plugin_instance.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/scoped_ptr.h" |
| 9 #include "gfx/rect.h" |
| 10 #include "third_party/ppapi/c/pp_instance.h" |
| 11 #include "third_party/ppapi/c/pp_event.h" |
| 12 #include "third_party/ppapi/c/pp_rect.h" |
| 13 #include "third_party/ppapi/c/pp_resource.h" |
| 14 #include "third_party/ppapi/c/ppb_instance.h" |
| 15 #include "third_party/ppapi/c/ppp_instance.h" |
| 16 #include "third_party/WebKit/WebKit/chromium/public/WebInputEvent.h" |
| 17 #include "webkit/glue/plugins/pepper_device_context_2d.h" |
| 18 #include "webkit/glue/plugins/pepper_plugin_module.h" |
| 19 #include "webkit/glue/plugins/pepper_resource_tracker.h" |
| 20 |
| 21 using WebKit::WebInputEvent; |
| 22 |
| 23 namespace pepper { |
| 24 |
| 25 namespace { |
| 26 |
| 27 void RectToPPRect(const gfx::Rect& input, PP_Rect* output) { |
| 28 output->left = input.x(); |
| 29 output->top = input.y(); |
| 30 output->right = input.right(); |
| 31 output->bottom = input.bottom(); |
| 32 } |
| 33 |
| 34 PP_Event_Type ConvertEventTypes(WebInputEvent::Type wetype) { |
| 35 switch (wetype) { |
| 36 case WebInputEvent::MouseDown: |
| 37 return PP_Event_Type_MouseDown; |
| 38 case WebInputEvent::MouseUp: |
| 39 return PP_Event_Type_MouseUp; |
| 40 case WebInputEvent::MouseMove: |
| 41 return PP_Event_Type_MouseMove; |
| 42 case WebInputEvent::MouseEnter: |
| 43 return PP_Event_Type_MouseEnter; |
| 44 case WebInputEvent::MouseLeave: |
| 45 return PP_Event_Type_MouseLeave; |
| 46 case WebInputEvent::MouseWheel: |
| 47 return PP_Event_Type_MouseWheel; |
| 48 case WebInputEvent::RawKeyDown: |
| 49 return PP_Event_Type_RawKeyDown; |
| 50 case WebInputEvent::KeyDown: |
| 51 return PP_Event_Type_KeyDown; |
| 52 case WebInputEvent::KeyUp: |
| 53 return PP_Event_Type_KeyUp; |
| 54 case WebInputEvent::Char: |
| 55 return PP_Event_Type_Char; |
| 56 case WebInputEvent::Undefined: |
| 57 default: |
| 58 return PP_Event_Type_Undefined; |
| 59 } |
| 60 } |
| 61 |
| 62 void BuildKeyEvent(const WebInputEvent* event, PP_Event* pp_event) { |
| 63 const WebKit::WebKeyboardEvent* key_event = |
| 64 reinterpret_cast<const WebKit::WebKeyboardEvent*>(event); |
| 65 pp_event->u.key.modifier = key_event->modifiers; |
| 66 pp_event->u.key.normalizedKeyCode = key_event->windowsKeyCode; |
| 67 } |
| 68 |
| 69 void BuildCharEvent(const WebInputEvent* event, PP_Event* pp_event) { |
| 70 const WebKit::WebKeyboardEvent* key_event = |
| 71 reinterpret_cast<const WebKit::WebKeyboardEvent*>(event); |
| 72 pp_event->u.character.modifier = key_event->modifiers; |
| 73 // For consistency, check that the sizes of the texts agree. |
| 74 DCHECK(sizeof(pp_event->u.character.text) == sizeof(key_event->text)); |
| 75 DCHECK(sizeof(pp_event->u.character.unmodifiedText) == |
| 76 sizeof(key_event->unmodifiedText)); |
| 77 for (size_t i = 0; i < WebKit::WebKeyboardEvent::textLengthCap; ++i) { |
| 78 pp_event->u.character.text[i] = key_event->text[i]; |
| 79 pp_event->u.character.unmodifiedText[i] = key_event->unmodifiedText[i]; |
| 80 } |
| 81 } |
| 82 |
| 83 void BuildMouseEvent(const WebInputEvent* event, PP_Event* pp_event) { |
| 84 const WebKit::WebMouseEvent* mouse_event = |
| 85 reinterpret_cast<const WebKit::WebMouseEvent*>(event); |
| 86 pp_event->u.mouse.modifier = mouse_event->modifiers; |
| 87 pp_event->u.mouse.button = mouse_event->button; |
| 88 pp_event->u.mouse.x = mouse_event->x; |
| 89 pp_event->u.mouse.y = mouse_event->y; |
| 90 pp_event->u.mouse.clickCount = mouse_event->clickCount; |
| 91 } |
| 92 |
| 93 void BuildMouseWheelEvent(const WebInputEvent* event, PP_Event* pp_event) { |
| 94 const WebKit::WebMouseWheelEvent* mouse_wheel_event = |
| 95 reinterpret_cast<const WebKit::WebMouseWheelEvent*>(event); |
| 96 pp_event->u.wheel.modifier = mouse_wheel_event->modifiers; |
| 97 pp_event->u.wheel.deltaX = mouse_wheel_event->deltaX; |
| 98 pp_event->u.wheel.deltaY = mouse_wheel_event->deltaY; |
| 99 pp_event->u.wheel.wheelTicksX = mouse_wheel_event->wheelTicksX; |
| 100 pp_event->u.wheel.wheelTicksY = mouse_wheel_event->wheelTicksY; |
| 101 pp_event->u.wheel.scrollByPage = mouse_wheel_event->scrollByPage; |
| 102 } |
| 103 |
| 104 bool BindGraphicsDeviceContext(PP_Instance instance_id, PP_Resource device_id) { |
| 105 PluginInstance* instance = PluginInstance::FromPPInstance(instance_id); |
| 106 if (!instance) |
| 107 return false; |
| 108 return instance->BindGraphicsDeviceContext(device_id); |
| 109 } |
| 110 |
| 111 const PPB_Instance ppb_instance = { |
| 112 &BindGraphicsDeviceContext, |
| 113 }; |
| 114 |
| 115 } // namespace |
| 116 |
| 117 PluginInstance::PluginInstance(PluginDelegate* delegate, |
| 118 PluginModule* module, |
| 119 const PPP_Instance* instance_interface) |
| 120 : delegate_(delegate), |
| 121 module_(module), |
| 122 instance_interface_(instance_interface) { |
| 123 DCHECK(delegate); |
| 124 module_->InstanceCreated(this); |
| 125 } |
| 126 |
| 127 PluginInstance::~PluginInstance() { |
| 128 module_->InstanceDeleted(this); |
| 129 } |
| 130 |
| 131 // static |
| 132 const PPB_Instance* PluginInstance::GetInterface() { |
| 133 return &ppb_instance; |
| 134 } |
| 135 |
| 136 // static |
| 137 PluginInstance* PluginInstance::FromPPInstance(PP_Instance instance) { |
| 138 return reinterpret_cast<PluginInstance*>(instance.id); |
| 139 } |
| 140 |
| 141 PP_Instance PluginInstance::GetPPInstance() { |
| 142 PP_Instance ret; |
| 143 ret.id = reinterpret_cast<intptr_t>(this); |
| 144 return ret; |
| 145 } |
| 146 |
| 147 void PluginInstance::Paint(WebKit::WebCanvas* canvas, |
| 148 const gfx::Rect& plugin_rect, |
| 149 const gfx::Rect& paint_rect) { |
| 150 if (device_context_2d_) |
| 151 device_context_2d_->Paint(canvas, plugin_rect, paint_rect); |
| 152 } |
| 153 |
| 154 bool PluginInstance::BindGraphicsDeviceContext(PP_Resource device_id) { |
| 155 scoped_refptr<Resource> device_resource = |
| 156 ResourceTracker::Get()->GetResource(device_id); |
| 157 if (!device_resource.get()) |
| 158 return false; |
| 159 |
| 160 DeviceContext2D* device_2d = device_resource->AsDeviceContext2D(); |
| 161 if (device_2d) { |
| 162 device_context_2d_ = device_2d; |
| 163 // TODO(brettw) repaint the plugin. |
| 164 } |
| 165 |
| 166 return true; |
| 167 } |
| 168 |
| 169 void PluginInstance::Delete() { |
| 170 instance_interface_->Delete(GetPPInstance()); |
| 171 } |
| 172 |
| 173 bool PluginInstance::Initialize(const std::vector<std::string>& arg_names, |
| 174 const std::vector<std::string>& arg_values) { |
| 175 if (!instance_interface_->New(GetPPInstance())) |
| 176 return false; |
| 177 |
| 178 size_t argc = 0; |
| 179 scoped_array<const char*> argn(new const char*[arg_names.size()]); |
| 180 scoped_array<const char*> argv(new const char*[arg_names.size()]); |
| 181 for (size_t i = 0; i < arg_names.size(); ++i) { |
| 182 argn[argc] = arg_names[i].c_str(); |
| 183 argv[argc] = arg_values[i].c_str(); |
| 184 argc++; |
| 185 } |
| 186 |
| 187 return instance_interface_->Initialize(GetPPInstance(), |
| 188 argc, argn.get(), argv.get()); |
| 189 } |
| 190 |
| 191 bool PluginInstance::HandleInputEvent(const WebKit::WebInputEvent& event, |
| 192 WebKit::WebCursorInfo* cursor_info) { |
| 193 PP_Event pp_event; |
| 194 |
| 195 pp_event.type = ConvertEventTypes(event.type); |
| 196 pp_event.size = sizeof(pp_event); |
| 197 pp_event.time_stamp_seconds = event.timeStampSeconds; |
| 198 switch (pp_event.type) { |
| 199 case PP_Event_Type_Undefined: |
| 200 return false; |
| 201 case PP_Event_Type_MouseDown: |
| 202 case PP_Event_Type_MouseUp: |
| 203 case PP_Event_Type_MouseMove: |
| 204 case PP_Event_Type_MouseEnter: |
| 205 case PP_Event_Type_MouseLeave: |
| 206 BuildMouseEvent(&event, &pp_event); |
| 207 break; |
| 208 case PP_Event_Type_MouseWheel: |
| 209 BuildMouseWheelEvent(&event, &pp_event); |
| 210 break; |
| 211 case PP_Event_Type_RawKeyDown: |
| 212 case PP_Event_Type_KeyDown: |
| 213 case PP_Event_Type_KeyUp: |
| 214 BuildKeyEvent(&event, &pp_event); |
| 215 break; |
| 216 case PP_Event_Type_Char: |
| 217 BuildCharEvent(&event, &pp_event); |
| 218 break; |
| 219 } |
| 220 return instance_interface_->HandleEvent(GetPPInstance(), &pp_event); |
| 221 } |
| 222 |
| 223 void PluginInstance::ViewChanged(const gfx::Rect& position, |
| 224 const gfx::Rect& clip) { |
| 225 PP_Rect pp_position, pp_clip; |
| 226 RectToPPRect(position, &pp_position); |
| 227 RectToPPRect(clip, &pp_clip); |
| 228 instance_interface_->ViewChanged(GetPPInstance(), &pp_position, &pp_clip); |
| 229 } |
| 230 |
| 231 } // namespace pepper |
OLD | NEW |