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