Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: webkit/plugins/ppapi/ppb_scrollbar_impl.cc

Issue 20165002: Move webkit/plugins/ppapi to content/renderer/pepper. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/plugins/ppapi/ppb_scrollbar_impl.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "ppapi/c/dev/ppp_scrollbar_dev.h"
11 #include "ppapi/thunk/thunk.h"
12 #include "skia/ext/platform_canvas.h"
13 #include "third_party/WebKit/public/platform/WebCanvas.h"
14 #include "third_party/WebKit/public/platform/WebRect.h"
15 #include "third_party/WebKit/public/platform/WebVector.h"
16 #include "third_party/WebKit/public/web/WebInputEvent.h"
17 #include "third_party/WebKit/public/web/WebPluginScrollbar.h"
18 #include "webkit/glue/webkit_glue.h"
19 #include "webkit/plugins/ppapi/common.h"
20 #include "webkit/plugins/ppapi/event_conversion.h"
21 #include "webkit/plugins/ppapi/plugin_module.h"
22 #include "webkit/plugins/ppapi/ppapi_plugin_instance_impl.h"
23 #include "webkit/plugins/ppapi/ppb_image_data_impl.h"
24 #include "webkit/plugins/ppapi/resource_helper.h"
25
26 #if defined(OS_WIN)
27 #include "base/win/windows_version.h"
28 #endif
29
30 using ppapi::thunk::PPB_Scrollbar_API;
31 using WebKit::WebInputEvent;
32 using WebKit::WebRect;
33 using WebKit::WebScrollbar;
34 using WebKit::WebPluginScrollbar;
35
36 namespace webkit {
37 namespace ppapi {
38
39 // static
40 PP_Resource PPB_Scrollbar_Impl::Create(PP_Instance instance,
41 bool vertical) {
42 scoped_refptr<PPB_Scrollbar_Impl> scrollbar(
43 new PPB_Scrollbar_Impl(instance));
44 scrollbar->Init(vertical);
45 return scrollbar->GetReference();
46 }
47
48 PPB_Scrollbar_Impl::PPB_Scrollbar_Impl(PP_Instance instance)
49 : PPB_Widget_Impl(instance),
50 weak_ptr_factory_(this) {
51 }
52
53 PPB_Scrollbar_Impl::~PPB_Scrollbar_Impl() {
54 }
55
56 void PPB_Scrollbar_Impl::Init(bool vertical) {
57 PluginInstanceImpl* plugin_instance = ResourceHelper::GetPluginInstance(this);
58 if (!plugin_instance)
59 return;
60 scrollbar_.reset(WebPluginScrollbar::createForPlugin(
61 vertical ? WebScrollbar::Vertical : WebScrollbar::Horizontal,
62 ResourceHelper::GetPluginInstance(this)->container(),
63 static_cast<WebKit::WebPluginScrollbarClient*>(this)));
64 }
65
66 PPB_Scrollbar_API* PPB_Scrollbar_Impl::AsPPB_Scrollbar_API() {
67 return this;
68 }
69
70 void PPB_Scrollbar_Impl::InstanceWasDeleted() {
71 scrollbar_.reset();
72 }
73
74 uint32_t PPB_Scrollbar_Impl::GetThickness() {
75 return WebPluginScrollbar::defaultThickness();
76 }
77
78 bool PPB_Scrollbar_Impl::IsOverlay() {
79 return scrollbar_->isOverlay();
80 }
81
82 uint32_t PPB_Scrollbar_Impl::GetValue() {
83 return scrollbar_->value();
84 }
85
86 void PPB_Scrollbar_Impl::SetValue(uint32_t value) {
87 if (scrollbar_)
88 scrollbar_->setValue(value);
89 }
90
91 void PPB_Scrollbar_Impl::SetDocumentSize(uint32_t size) {
92 if (scrollbar_)
93 scrollbar_->setDocumentSize(size);
94 }
95
96 void PPB_Scrollbar_Impl::SetTickMarks(const PP_Rect* tick_marks,
97 uint32_t count) {
98 if (!scrollbar_)
99 return;
100 tickmarks_.resize(count);
101 for (uint32 i = 0; i < count; ++i) {
102 tickmarks_[i] = WebRect(tick_marks[i].point.x,
103 tick_marks[i].point.y,
104 tick_marks[i].size.width,
105 tick_marks[i].size.height);;
106 }
107 PP_Rect rect = location();
108 Invalidate(&rect);
109 }
110
111 void PPB_Scrollbar_Impl::ScrollBy(PP_ScrollBy_Dev unit, int32_t multiplier) {
112 if (!scrollbar_)
113 return;
114
115 WebScrollbar::ScrollDirection direction = multiplier >= 0 ?
116 WebScrollbar::ScrollForward : WebScrollbar::ScrollBackward;
117 float fmultiplier = 1.0;
118
119 WebScrollbar::ScrollGranularity granularity;
120 if (unit == PP_SCROLLBY_LINE) {
121 granularity = WebScrollbar::ScrollByLine;
122 } else if (unit == PP_SCROLLBY_PAGE) {
123 granularity = WebScrollbar::ScrollByPage;
124 } else if (unit == PP_SCROLLBY_DOCUMENT) {
125 granularity = WebScrollbar::ScrollByDocument;
126 } else {
127 granularity = WebScrollbar::ScrollByPixel;
128 fmultiplier = static_cast<float>(multiplier);
129 if (fmultiplier < 0)
130 fmultiplier *= -1;
131 }
132 scrollbar_->scroll(direction, granularity, fmultiplier);
133 }
134
135 PP_Bool PPB_Scrollbar_Impl::PaintInternal(const gfx::Rect& rect,
136 PPB_ImageData_Impl* image) {
137 ImageDataAutoMapper mapper(image);
138 skia::PlatformCanvas* canvas = image->GetPlatformCanvas();
139 if (!canvas || !scrollbar_)
140 return PP_FALSE;
141 canvas->save();
142 canvas->scale(scale(), scale());
143 scrollbar_->paint(canvas, rect);
144 canvas->restore();
145
146 #if defined(OS_WIN)
147 if (base::win::GetVersion() == base::win::VERSION_XP)
148 skia::MakeOpaque(canvas, rect.x(), rect.y(), rect.width(), rect.height());
149 #endif
150
151 return PP_TRUE;
152 }
153
154 PP_Bool PPB_Scrollbar_Impl::HandleEventInternal(
155 const ::ppapi::InputEventData& data) {
156 scoped_ptr<WebInputEvent> web_input_event(CreateWebInputEvent(data));
157 if (!web_input_event.get() || !scrollbar_)
158 return PP_FALSE;
159
160 return PP_FromBool(scrollbar_->handleInputEvent(*web_input_event.get()));
161 }
162
163 void PPB_Scrollbar_Impl::SetLocationInternal(const PP_Rect* location) {
164 if (!scrollbar_)
165 return;
166 scrollbar_->setLocation(WebRect(location->point.x,
167 location->point.y,
168 location->size.width,
169 location->size.height));
170 }
171
172 void PPB_Scrollbar_Impl::valueChanged(WebKit::WebPluginScrollbar* scrollbar) {
173 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this);
174 if (!plugin_module)
175 return;
176
177 const PPP_Scrollbar_Dev* ppp_scrollbar =
178 static_cast<const PPP_Scrollbar_Dev*>(plugin_module->GetPluginInterface(
179 PPP_SCROLLBAR_DEV_INTERFACE));
180 if (!ppp_scrollbar) {
181 // Try the old version. This is ok because the old interface is a subset of
182 // the new one, and ValueChanged didn't change.
183 ppp_scrollbar =
184 static_cast<const PPP_Scrollbar_Dev*>(plugin_module->GetPluginInterface(
185 PPP_SCROLLBAR_DEV_INTERFACE_0_2));
186 if (!ppp_scrollbar)
187 return;
188 }
189 ppp_scrollbar->ValueChanged(pp_instance(), pp_resource(),
190 scrollbar_->value());
191 }
192
193 void PPB_Scrollbar_Impl::overlayChanged(WebPluginScrollbar* scrollbar) {
194 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this);
195 if (!plugin_module)
196 return;
197
198 const PPP_Scrollbar_Dev* ppp_scrollbar =
199 static_cast<const PPP_Scrollbar_Dev*>(plugin_module->GetPluginInterface(
200 PPP_SCROLLBAR_DEV_INTERFACE));
201 if (!ppp_scrollbar)
202 return;
203 ppp_scrollbar->OverlayChanged(pp_instance(), pp_resource(),
204 PP_FromBool(IsOverlay()));
205 }
206
207 void PPB_Scrollbar_Impl::invalidateScrollbarRect(
208 WebKit::WebPluginScrollbar* scrollbar,
209 const WebKit::WebRect& rect) {
210 gfx::Rect gfx_rect(rect.x,
211 rect.y,
212 rect.width,
213 rect.height);
214 dirty_.Union(gfx_rect);
215 // Can't call into the client to tell them about the invalidate right away,
216 // since the PPB_Scrollbar_Impl code is still in the middle of updating its
217 // internal state.
218 // Note: we use a WeakPtrFactory here so that a lingering callback can not
219 // modify the lifetime of this object. Otherwise, WebKit::WebPluginScrollbar
220 // could outlive WebKit::WebPluginContainer, which is against its contract.
221 base::MessageLoop::current()->PostTask(
222 FROM_HERE,
223 base::Bind(&PPB_Scrollbar_Impl::NotifyInvalidate,
224 weak_ptr_factory_.GetWeakPtr()));
225 }
226
227 void PPB_Scrollbar_Impl::getTickmarks(
228 WebKit::WebPluginScrollbar* scrollbar,
229 WebKit::WebVector<WebKit::WebRect>* tick_marks) const {
230 if (tickmarks_.empty()) {
231 WebRect* rects = NULL;
232 tick_marks->assign(rects, 0);
233 } else {
234 tick_marks->assign(&tickmarks_[0], tickmarks_.size());
235 }
236 }
237
238 void PPB_Scrollbar_Impl::NotifyInvalidate() {
239 if (dirty_.IsEmpty())
240 return;
241 PP_Rect pp_rect;
242 pp_rect.point.x = dirty_.x();
243 pp_rect.point.y = dirty_.y();
244 pp_rect.size.width = dirty_.width();
245 pp_rect.size.height = dirty_.height();
246 dirty_ = gfx::Rect();
247 Invalidate(&pp_rect);
248 }
249
250 } // namespace ppapi
251 } // namespace webkit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698