OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/pepper/pepper_flash_renderer_host.h" | 5 #include "content/renderer/pepper/pepper_flash_renderer_host.h" |
6 | 6 |
7 #include <vector> | |
8 | |
7 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
8 #include "content/public/renderer/renderer_ppapi_host.h" | 10 #include "content/public/renderer/renderer_ppapi_host.h" |
9 #include "content/renderer/render_thread_impl.h" | 11 #include "content/renderer/render_thread_impl.h" |
10 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
11 #include "ipc/ipc_message_macros.h" | 13 #include "ipc/ipc_message_macros.h" |
12 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
15 #include "ppapi/c/trusted/ppb_browser_font_trusted.h" | |
13 #include "ppapi/host/dispatch_host_message.h" | 16 #include "ppapi/host/dispatch_host_message.h" |
17 #include "ppapi/proxy/host_dispatcher.h" | |
14 #include "ppapi/proxy/ppapi_messages.h" | 18 #include "ppapi/proxy/ppapi_messages.h" |
15 #include "ppapi/proxy/resource_message_params.h" | 19 #include "ppapi/proxy/resource_message_params.h" |
20 #include "ppapi/proxy/serialized_structs.h" | |
21 #include "ppapi/thunk/enter.h" | |
22 #include "ppapi/thunk/ppb_image_data_api.h" | |
23 #include "skia/ext/platform_canvas.h" | |
24 #include "third_party/skia/include/core/SkCanvas.h" | |
25 #include "third_party/skia/include/core/SkMatrix.h" | |
26 #include "third_party/skia/include/core/SkPaint.h" | |
27 #include "third_party/skia/include/core/SkPoint.h" | |
28 #include "third_party/skia/include/core/SkTemplates.h" | |
29 #include "third_party/skia/include/core/SkTypeface.h" | |
30 #include "ui/gfx/rect.h" | |
31 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
32 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" | |
33 | |
34 using ppapi::thunk::EnterResourceNoLock; | |
35 using ppapi::thunk::PPB_ImageData_API; | |
16 | 36 |
17 namespace content { | 37 namespace content { |
18 | 38 |
19 PepperFlashRendererHost::PepperFlashRendererHost( | 39 PepperFlashRendererHost::PepperFlashRendererHost( |
20 RendererPpapiHost* host, | 40 RendererPpapiHost* host, |
21 PP_Instance instance, | 41 PP_Instance instance, |
22 PP_Resource resource) | 42 PP_Resource resource) |
23 : ResourceHost(host->GetPpapiHost(), instance, resource) { | 43 : ResourceHost(host->GetPpapiHost(), instance, resource), |
44 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
45 host_(host) { | |
24 } | 46 } |
25 | 47 |
26 PepperFlashRendererHost::~PepperFlashRendererHost() { | 48 PepperFlashRendererHost::~PepperFlashRendererHost() { |
49 // This object may be destroyed in the middle of a sync message. If that is | |
yzshen1
2012/12/14 19:13:14
I guess we will need this for some other classes.
| |
50 // the case, make sure we respond to all the pending navigate calls. | |
51 for (size_t i = 0; i < navigate_replies_.size(); ++i) | |
yzshen1
2012/12/14 19:13:14
maybe sending in reverse order is more consistent?
raymes
2012/12/17 04:07:57
Done.
| |
52 SendReply(navigate_replies_[i], IPC::Message()); | |
27 } | 53 } |
28 | 54 |
29 int32_t PepperFlashRendererHost::OnResourceMessageReceived( | 55 int32_t PepperFlashRendererHost::OnResourceMessageReceived( |
30 const IPC::Message& msg, | 56 const IPC::Message& msg, |
31 ppapi::host::HostMessageContext* context) { | 57 ppapi::host::HostMessageContext* context) { |
32 IPC_BEGIN_MESSAGE_MAP(PepperFlashRendererHost, msg) | 58 IPC_BEGIN_MESSAGE_MAP(PepperFlashRendererHost, msg) |
33 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_GetProxyForURL, | 59 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_GetProxyForURL, |
34 OnMsgGetProxyForURL); | 60 OnMsgGetProxyForURL); |
61 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_SetInstanceAlwaysOnTop, | |
62 OnMsgSetInstanceAlwaysOnTop); | |
63 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_DrawGlyphs, | |
64 OnMsgDrawGlyphs); | |
65 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_Navigate, | |
66 OnMsgNavigate); | |
67 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_IsRectTopmost, | |
68 OnMsgIsRectTopmost); | |
35 IPC_END_MESSAGE_MAP() | 69 IPC_END_MESSAGE_MAP() |
36 return PP_ERROR_FAILED; | 70 return PP_ERROR_FAILED; |
37 } | 71 } |
38 | 72 |
39 int32_t PepperFlashRendererHost::OnMsgGetProxyForURL( | 73 int32_t PepperFlashRendererHost::OnMsgGetProxyForURL( |
40 ppapi::host::HostMessageContext* host_context, | 74 ppapi::host::HostMessageContext* host_context, |
41 const std::string& url) { | 75 const std::string& url) { |
42 GURL gurl(url); | 76 GURL gurl(url); |
43 if (!gurl.is_valid()) | 77 if (!gurl.is_valid()) |
44 return PP_ERROR_FAILED; | 78 return PP_ERROR_FAILED; |
45 bool result; | 79 bool result; |
46 std::string proxy; | 80 std::string proxy; |
47 RenderThreadImpl::current()->Send( | 81 RenderThreadImpl::current()->Send( |
48 new ViewHostMsg_ResolveProxy(gurl, &result, &proxy)); | 82 new ViewHostMsg_ResolveProxy(gurl, &result, &proxy)); |
49 if (!result) | 83 if (!result) |
50 return PP_ERROR_FAILED; | 84 return PP_ERROR_FAILED; |
51 host_context->reply_msg = PpapiPluginMsg_Flash_GetProxyForURLReply(proxy); | 85 host_context->reply_msg = PpapiPluginMsg_Flash_GetProxyForURLReply(proxy); |
52 return PP_OK; | 86 return PP_OK; |
53 } | 87 } |
54 | 88 |
89 int32_t PepperFlashRendererHost::OnMsgSetInstanceAlwaysOnTop( | |
90 ppapi::host::HostMessageContext* host_context, | |
91 bool on_top) { | |
92 webkit::ppapi::PluginInstance* plugin_instance = | |
93 host_->GetPluginInstance(pp_instance()); | |
94 if (plugin_instance) | |
95 plugin_instance->set_always_on_top(on_top); | |
96 // Since no reply is sent for this message, it doesn't make sense to return an | |
97 // error. | |
98 return PP_OK; | |
99 } | |
100 | |
101 int32_t PepperFlashRendererHost::OnMsgDrawGlyphs( | |
102 ppapi::host::HostMessageContext* host_context, | |
103 ppapi::proxy::PPBFlash_DrawGlyphs_Params params) { | |
104 if (params.glyph_indices.size() != params.glyph_advances.size() || | |
105 params.glyph_indices.empty()) | |
106 return PP_ERROR_FAILED; | |
107 | |
108 EnterResourceNoLock<PPB_ImageData_API> enter( | |
109 params.image_data.host_resource(), true); | |
110 if (enter.failed()) | |
111 return PP_ERROR_FAILED; | |
112 webkit::ppapi::PPB_ImageData_Impl* image_resource = | |
113 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter.object()); | |
114 | |
115 webkit::ppapi::ImageDataAutoMapper mapper(image_resource); | |
116 if (!mapper.is_valid()) | |
117 return PP_ERROR_FAILED; | |
118 | |
119 // Set up the typeface. | |
120 int style = SkTypeface::kNormal; | |
121 if (static_cast<PP_BrowserFont_Trusted_Weight>(params.font_desc.weight) >= | |
122 PP_BROWSERFONT_TRUSTED_WEIGHT_BOLD) | |
123 style |= SkTypeface::kBold; | |
124 if (params.font_desc.italic) | |
125 style |= SkTypeface::kItalic; | |
126 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef( | |
127 SkTypeface::CreateFromName(params.font_desc.face.c_str(), | |
128 static_cast<SkTypeface::Style>(style))); | |
129 if (!typeface) | |
130 return PP_ERROR_FAILED; | |
131 | |
132 // Set up the canvas. | |
133 SkCanvas* canvas = image_resource->GetPlatformCanvas(); | |
134 SkAutoCanvasRestore acr(canvas, true); | |
135 | |
136 // Clip is applied in pixels before the transform. | |
137 SkRect clip_rect = { | |
138 SkIntToScalar(params.clip.point.x), | |
139 SkIntToScalar(params.clip.point.y), | |
140 SkIntToScalar(params.clip.point.x + params.clip.size.width), | |
141 SkIntToScalar(params.clip.point.y + params.clip.size.height) | |
142 }; | |
143 canvas->clipRect(clip_rect); | |
144 | |
145 // Convert & set the matrix. | |
146 SkMatrix matrix; | |
147 matrix.set(SkMatrix::kMScaleX, SkFloatToScalar(params.transformation[0][0])); | |
148 matrix.set(SkMatrix::kMSkewX, SkFloatToScalar(params.transformation[0][1])); | |
149 matrix.set(SkMatrix::kMTransX, SkFloatToScalar(params.transformation[0][2])); | |
150 matrix.set(SkMatrix::kMSkewY, SkFloatToScalar(params.transformation[1][0])); | |
151 matrix.set(SkMatrix::kMScaleY, SkFloatToScalar(params.transformation[1][1])); | |
152 matrix.set(SkMatrix::kMTransY, SkFloatToScalar(params.transformation[1][2])); | |
153 matrix.set(SkMatrix::kMPersp0, SkFloatToScalar(params.transformation[2][0])); | |
154 matrix.set(SkMatrix::kMPersp1, SkFloatToScalar(params.transformation[2][1])); | |
155 matrix.set(SkMatrix::kMPersp2, SkFloatToScalar(params.transformation[2][2])); | |
156 canvas->concat(matrix); | |
157 | |
158 SkPaint paint; | |
159 paint.setColor(params.color); | |
160 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
161 paint.setAntiAlias(true); | |
162 paint.setHinting(SkPaint::kFull_Hinting); | |
163 paint.setTextSize(SkIntToScalar(params.font_desc.size)); | |
164 paint.setTypeface(typeface.get()); // Takes a ref and manages lifetime. | |
165 if (params.allow_subpixel_aa) { | |
166 paint.setSubpixelText(true); | |
167 paint.setLCDRenderText(true); | |
168 } | |
169 | |
170 SkScalar x = SkIntToScalar(params.position.x); | |
171 SkScalar y = SkIntToScalar(params.position.y); | |
172 | |
173 // Build up the skia advances. | |
174 size_t glyph_count = params.glyph_indices.size(); | |
175 if (glyph_count == 0) | |
176 return PP_OK; | |
177 std::vector<SkPoint> storage; | |
178 storage.resize(glyph_count); | |
179 SkPoint* sk_positions = &storage[0]; | |
180 for (uint32_t i = 0; i < glyph_count; i++) { | |
181 sk_positions[i].set(x, y); | |
182 x += SkFloatToScalar(params.glyph_advances[i].x); | |
183 y += SkFloatToScalar(params.glyph_advances[i].y); | |
184 } | |
185 | |
186 canvas->drawPosText(¶ms.glyph_indices[0], glyph_count * 2, sk_positions, | |
187 paint); | |
188 | |
189 return PP_OK; | |
190 } | |
191 | |
192 // CAUTION: This code is subtle because Navigate is a sync call which may | |
193 // cause re-entrancy or cause the instance to be destroyed. If the instance | |
194 // is destroyed we need to ensure that we respond to all outstanding sync | |
195 // messages so that the plugin process does not remain blocked. | |
196 int32_t PepperFlashRendererHost::OnMsgNavigate( | |
197 ppapi::host::HostMessageContext* host_context, | |
198 const ppapi::URLRequestInfoData& data, | |
199 const std::string& target, | |
200 bool from_user_action) { | |
201 // If our PluginInstance is already destroyed, just return a failure. | |
202 webkit::ppapi::PluginInstance* plugin_instance = | |
203 host_->GetPluginInstance(pp_instance()); | |
204 if (!plugin_instance) | |
205 return PP_ERROR_FAILED; | |
206 | |
207 // Navigate may call into Javascript (e.g. with a "javascript:" URL), | |
208 // or do things like navigate away from the page, either one of which will | |
209 // need to re-enter into the plugin. It is safe, because it is essentially | |
210 // equivalent to NPN_GetURL, where Flash would expect re-entrancy. | |
211 ppapi::proxy::HostDispatcher* host_dispatcher = | |
212 ppapi::proxy::HostDispatcher::GetForInstance(pp_instance()); | |
213 host_dispatcher->set_allow_plugin_reentrancy(); | |
214 | |
215 // Grab a weak pointer to ourselves on the stack so we can check if we are | |
216 // still alive. | |
217 base::WeakPtr<PepperFlashRendererHost> weak_ptr = weak_factory_.GetWeakPtr(); | |
218 // Keep track of reply contexts in case we are destroyed during a Navigate | |
219 // call. Even if we are destroyed, we still need to send these replies to | |
220 // unblock the plugin process. | |
221 navigate_replies_.push_back(host_context->MakeReplyMessageContext()); | |
222 plugin_instance->Navigate(data, target.c_str(), from_user_action); | |
223 // This object might have been destroyed by this point. If it is destroyed | |
224 // the reply will be sent in the destructor. Otherwise send the reply here. | |
225 if (weak_ptr) { | |
226 SendReply(navigate_replies_.back(), IPC::Message()); | |
227 navigate_replies_.pop_back(); | |
228 } | |
229 | |
230 // Return PP_OK_COMPLETIONPENDING so that no reply is automatically sent. | |
231 return PP_OK_COMPLETIONPENDING; | |
232 } | |
233 | |
234 int32_t PepperFlashRendererHost::OnMsgIsRectTopmost( | |
235 ppapi::host::HostMessageContext* host_context, | |
236 const PP_Rect& rect) { | |
237 webkit::ppapi::PluginInstance* plugin_instance = | |
238 host_->GetPluginInstance(pp_instance()); | |
239 if (plugin_instance && plugin_instance->IsRectTopmost( | |
240 gfx::Rect(rect.point.x, rect.point.y,rect.size.width, rect.size.height))) | |
241 return PP_OK; | |
242 return PP_ERROR_FAILED; | |
243 } | |
244 | |
55 } // namespace content | 245 } // namespace content |
OLD | NEW |