| 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 "chrome/renderer/pepper_widget.h" | |
| 6 | |
| 7 #include "base/hash_tables.h" | |
| 8 #include "base/lazy_instance.h" | |
| 9 #include "chrome/renderer/pepper_scrollbar_widget.h" | |
| 10 #include "chrome/renderer/webplugin_delegate_pepper.h" | |
| 11 #include "skia/ext/platform_canvas.h" | |
| 12 #include "webkit/plugins/npapi/plugin_instance.h" | |
| 13 #include "webkit/plugins/npapi/webplugin.h" | |
| 14 #include "webkit/plugins/npapi/webplugin_delegate.h" | |
| 15 | |
| 16 #if defined(OS_WIN) | |
| 17 #include "base/win/windows_version.h" | |
| 18 #endif | |
| 19 | |
| 20 static int g_next_id; | |
| 21 typedef base::hash_map<int, PepperWidget*> WidgetMap; | |
| 22 static base::LazyInstance<WidgetMap> g_widgets(base::LINKER_INITIALIZED); | |
| 23 | |
| 24 NPError NPCreateWidget(NPP instance, | |
| 25 NPWidgetType type, | |
| 26 void* params, | |
| 27 NPWidgetID* id) { | |
| 28 PepperWidget* widget; | |
| 29 switch (type) { | |
| 30 case NPWidgetTypeScrollbar: | |
| 31 widget = new PepperScrollbarWidget( | |
| 32 *static_cast<NPScrollbarCreateParams*>(params)); | |
| 33 break; | |
| 34 default: | |
| 35 return NPERR_INVALID_PARAM; | |
| 36 } | |
| 37 | |
| 38 *id = ++g_next_id; | |
| 39 widget->Init(instance, *id); | |
| 40 return NPERR_NO_ERROR; | |
| 41 } | |
| 42 | |
| 43 NPError NPDestroyWidget(NPP instance, NPWidgetID id) { | |
| 44 WidgetMap::iterator iter = g_widgets.Get().find(id); | |
| 45 if (iter == g_widgets.Get().end()) | |
| 46 return NPERR_INVALID_PARAM; | |
| 47 | |
| 48 iter->second->Destroy(); | |
| 49 return NPERR_NO_ERROR; | |
| 50 } | |
| 51 | |
| 52 NPError NPPaintWidget(NPP instance, | |
| 53 NPWidgetID id, | |
| 54 NPDeviceContext2D* context, | |
| 55 NPRect* dirty) { | |
| 56 WidgetMap::iterator iter = g_widgets.Get().find(id); | |
| 57 if (iter == g_widgets.Get().end()) | |
| 58 return NPERR_INVALID_PARAM; | |
| 59 | |
| 60 webkit::npapi::PluginInstance* plugin = | |
| 61 static_cast<webkit::npapi::PluginInstance*>(instance->ndata); | |
| 62 WebPluginDelegatePepper* delegate = | |
| 63 static_cast<WebPluginDelegatePepper*>(plugin->webplugin()->delegate()); | |
| 64 Graphics2DDeviceContext* gdc = delegate->GetGraphicsContext(context); | |
| 65 iter->second->Paint(gdc, *dirty); | |
| 66 | |
| 67 #if defined(OS_WIN) | |
| 68 if (base::win::GetVersion() == base::win::VERSION_XP) { | |
| 69 gdc->canvas()->getTopPlatformDevice().makeOpaque( | |
| 70 dirty->left, dirty->top, dirty->right - dirty->left, | |
| 71 dirty->bottom - dirty->top); | |
| 72 } | |
| 73 #endif | |
| 74 return NPERR_NO_ERROR; | |
| 75 } | |
| 76 | |
| 77 bool NPHandleWidgetEvent(NPP instance, NPWidgetID id, NPPepperEvent* event) { | |
| 78 WidgetMap::iterator iter = g_widgets.Get().find(id); | |
| 79 if (iter == g_widgets.Get().end()) | |
| 80 return false; | |
| 81 | |
| 82 return iter->second->HandleEvent(*event); | |
| 83 } | |
| 84 | |
| 85 NPError NPGetWidgetProperty(NPP instance, | |
| 86 NPWidgetID id, | |
| 87 NPWidgetProperty property, | |
| 88 void* value) { | |
| 89 WidgetMap::iterator iter = g_widgets.Get().find(id); | |
| 90 if (iter == g_widgets.Get().end()) | |
| 91 return NPERR_INVALID_PARAM; | |
| 92 | |
| 93 iter->second->GetProperty(property, value); | |
| 94 return NPERR_NO_ERROR; | |
| 95 } | |
| 96 | |
| 97 NPError NPSetWidgetProperty(NPP instance, | |
| 98 NPWidgetID id, | |
| 99 NPWidgetProperty property, | |
| 100 void* value) { | |
| 101 WidgetMap::iterator iter = g_widgets.Get().find(id); | |
| 102 if (iter == g_widgets.Get().end()) | |
| 103 return NPERR_INVALID_PARAM; | |
| 104 | |
| 105 iter->second->SetProperty(property, value); | |
| 106 return NPERR_NO_ERROR; | |
| 107 } | |
| 108 | |
| 109 NPWidgetExtensions g_widget_extensions = { | |
| 110 NPCreateWidget, | |
| 111 NPDestroyWidget, | |
| 112 NPPaintWidget, | |
| 113 NPHandleWidgetEvent, | |
| 114 NPGetWidgetProperty, | |
| 115 NPSetWidgetProperty | |
| 116 }; | |
| 117 | |
| 118 // static | |
| 119 NPWidgetExtensions* PepperWidget::GetWidgetExtensions() { | |
| 120 return &g_widget_extensions; | |
| 121 } | |
| 122 | |
| 123 PepperWidget::PepperWidget() : instance_(NULL), id_(0) { | |
| 124 } | |
| 125 | |
| 126 PepperWidget::~PepperWidget() { | |
| 127 if (id_) | |
| 128 g_widgets.Get().erase(id_); | |
| 129 } | |
| 130 | |
| 131 void PepperWidget::Init(NPP instance, int id) { | |
| 132 instance_ = instance; | |
| 133 id_ = id; | |
| 134 g_widgets.Get()[id] = this; | |
| 135 } | |
| 136 | |
| 137 void PepperWidget::WidgetPropertyChanged(NPWidgetProperty property) { | |
| 138 webkit::npapi::PluginInstance* instance = | |
| 139 static_cast<webkit::npapi::PluginInstance*>(instance_->ndata); | |
| 140 NPPExtensions* extensions = NULL; | |
| 141 instance->NPP_GetValue(NPPVPepperExtensions, &extensions); | |
| 142 if (!extensions) | |
| 143 return; | |
| 144 | |
| 145 extensions->widgetPropertyChanged(instance_, id_, property); | |
| 146 } | |
| OLD | NEW |