Chromium Code Reviews| 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 "ppapi/thunk/ppb_url_request_info_api.h" | |
|
yzshen1
2012/12/12 19:09:31
You don't need this.
raymes
2012/12/12 22:01:21
Done.
raymes
2012/12/12 22:01:21
Done.
| |
| 24 #include "skia/ext/platform_canvas.h" | |
| 25 #include "third_party/skia/include/core/SkCanvas.h" | |
| 26 #include "third_party/skia/include/core/SkMatrix.h" | |
| 27 #include "third_party/skia/include/core/SkPaint.h" | |
| 28 #include "third_party/skia/include/core/SkPoint.h" | |
| 29 #include "third_party/skia/include/core/SkTemplates.h" | |
| 30 #include "third_party/skia/include/core/SkTypeface.h" | |
| 31 #include "ui/gfx/rect.h" | |
| 32 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
| 33 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" | |
| 34 | |
| 35 using ppapi::thunk::EnterResourceNoLock; | |
| 36 using ppapi::thunk::PPB_ImageData_API; | |
| 37 using ppapi::thunk::PPB_URLRequestInfo_API; | |
|
yzshen1
2012/12/12 19:09:31
you don't need this.
raymes
2012/12/12 22:01:21
Done.
| |
| 16 | 38 |
| 17 namespace content { | 39 namespace content { |
| 18 | 40 |
| 19 PepperFlashRendererHost::PepperFlashRendererHost( | 41 PepperFlashRendererHost::PepperFlashRendererHost( |
| 20 RendererPpapiHost* host, | 42 RendererPpapiHost* host, |
| 21 PP_Instance instance, | 43 PP_Instance instance, |
| 22 PP_Resource resource) | 44 PP_Resource resource) |
| 23 : ResourceHost(host->GetPpapiHost(), instance, resource) { | 45 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 46 host_(host) { | |
| 24 } | 47 } |
| 25 | 48 |
| 26 PepperFlashRendererHost::~PepperFlashRendererHost() { | 49 PepperFlashRendererHost::~PepperFlashRendererHost() { |
| 27 } | 50 } |
| 28 | 51 |
| 29 int32_t PepperFlashRendererHost::OnResourceMessageReceived( | 52 int32_t PepperFlashRendererHost::OnResourceMessageReceived( |
| 30 const IPC::Message& msg, | 53 const IPC::Message& msg, |
| 31 ppapi::host::HostMessageContext* context) { | 54 ppapi::host::HostMessageContext* context) { |
| 32 IPC_BEGIN_MESSAGE_MAP(PepperFlashRendererHost, msg) | 55 IPC_BEGIN_MESSAGE_MAP(PepperFlashRendererHost, msg) |
| 33 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_GetProxyForURL, | 56 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_GetProxyForURL, |
| 34 OnMsgGetProxyForURL); | 57 OnMsgGetProxyForURL); |
| 58 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_SetInstanceAlwaysOnTop, | |
| 59 OnMsgSetInstanceAlwaysOnTop); | |
| 60 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_DrawGlyphs, | |
| 61 OnMsgDrawGlyphs); | |
| 62 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_Navigate, | |
| 63 OnMsgNavigate); | |
| 64 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_Flash_IsRectTopmost, | |
| 65 OnMsgIsRectTopmost); | |
| 35 IPC_END_MESSAGE_MAP() | 66 IPC_END_MESSAGE_MAP() |
| 36 return PP_ERROR_FAILED; | 67 return PP_ERROR_FAILED; |
| 37 } | 68 } |
| 38 | 69 |
| 39 int32_t PepperFlashRendererHost::OnMsgGetProxyForURL( | 70 int32_t PepperFlashRendererHost::OnMsgGetProxyForURL( |
| 40 ppapi::host::HostMessageContext* host_context, | 71 ppapi::host::HostMessageContext* host_context, |
| 41 const std::string& url) { | 72 const std::string& url) { |
| 42 GURL gurl(url); | 73 GURL gurl(url); |
| 43 if (!gurl.is_valid()) | 74 if (!gurl.is_valid()) |
| 44 return PP_ERROR_FAILED; | 75 return PP_ERROR_FAILED; |
| 45 bool result; | 76 bool result; |
| 46 std::string proxy; | 77 std::string proxy; |
| 47 RenderThreadImpl::current()->Send( | 78 RenderThreadImpl::current()->Send( |
| 48 new ViewHostMsg_ResolveProxy(gurl, &result, &proxy)); | 79 new ViewHostMsg_ResolveProxy(gurl, &result, &proxy)); |
| 49 if (!result) | 80 if (!result) |
| 50 return PP_ERROR_FAILED; | 81 return PP_ERROR_FAILED; |
| 51 host_context->reply_msg = PpapiPluginMsg_Flash_GetProxyForURLReply(proxy); | 82 host_context->reply_msg = PpapiPluginMsg_Flash_GetProxyForURLReply(proxy); |
| 52 return PP_OK; | 83 return PP_OK; |
| 53 } | 84 } |
| 54 | 85 |
| 86 int32_t PepperFlashRendererHost::OnMsgSetInstanceAlwaysOnTop( | |
| 87 ppapi::host::HostMessageContext* host_context, | |
| 88 bool on_top) { | |
| 89 webkit::ppapi::PluginInstance* plugin_instance = | |
| 90 host_->GetPluginInstance(pp_instance()); | |
| 91 plugin_instance->set_always_on_top(on_top); | |
|
yzshen1
2012/12/12 19:09:31
|plugin_instance| may be NULL. (I don't remember w
raymes
2012/12/12 22:01:21
Done.
| |
| 92 return PP_OK; | |
| 93 } | |
| 94 | |
| 95 int32_t PepperFlashRendererHost::OnMsgDrawGlyphs( | |
| 96 ppapi::host::HostMessageContext* host_context, | |
| 97 ppapi::proxy::PPBFlash_DrawGlyphs_Params params) { | |
| 98 if (params.glyph_indices.size() != params.glyph_advances.size() || | |
| 99 params.glyph_indices.empty()) | |
| 100 return PP_ERROR_FAILED; | |
| 101 | |
| 102 EnterResourceNoLock<PPB_ImageData_API> enter( | |
| 103 params.image_data.host_resource(), true); | |
| 104 if (enter.failed()) | |
| 105 return PP_ERROR_FAILED; | |
| 106 webkit::ppapi::PPB_ImageData_Impl* image_resource = | |
| 107 static_cast<webkit::ppapi::PPB_ImageData_Impl*>(enter.object()); | |
| 108 | |
| 109 webkit::ppapi::ImageDataAutoMapper mapper(image_resource); | |
| 110 if (!mapper.is_valid()) | |
| 111 return PP_ERROR_FAILED; | |
| 112 | |
| 113 // Set up the typeface. | |
| 114 int style = SkTypeface::kNormal; | |
| 115 if (static_cast<PP_BrowserFont_Trusted_Weight>(params.font_desc.weight) >= | |
| 116 PP_BROWSERFONT_TRUSTED_WEIGHT_BOLD) | |
| 117 style |= SkTypeface::kBold; | |
| 118 if (params.font_desc.italic) | |
| 119 style |= SkTypeface::kItalic; | |
| 120 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef( | |
| 121 SkTypeface::CreateFromName(params.font_desc.face.c_str(), | |
| 122 static_cast<SkTypeface::Style>(style))); | |
| 123 if (!typeface) | |
| 124 return PP_ERROR_FAILED; | |
| 125 | |
| 126 // Set up the canvas. | |
| 127 SkCanvas* canvas = image_resource->GetPlatformCanvas(); | |
| 128 SkAutoCanvasRestore acr(canvas, true); | |
| 129 | |
| 130 // Clip is applied in pixels before the transform. | |
| 131 SkRect clip_rect = { | |
| 132 SkIntToScalar(params.clip.point.x), | |
| 133 SkIntToScalar(params.clip.point.y), | |
| 134 SkIntToScalar(params.clip.point.x + params.clip.size.width), | |
| 135 SkIntToScalar(params.clip.point.y + params.clip.size.height) | |
| 136 }; | |
| 137 canvas->clipRect(clip_rect); | |
| 138 | |
| 139 // Convert & set the matrix. | |
| 140 SkMatrix matrix; | |
| 141 matrix.set(SkMatrix::kMScaleX, SkFloatToScalar(params.transformation[0][0])); | |
| 142 matrix.set(SkMatrix::kMSkewX, SkFloatToScalar(params.transformation[0][1])); | |
| 143 matrix.set(SkMatrix::kMTransX, SkFloatToScalar(params.transformation[0][2])); | |
| 144 matrix.set(SkMatrix::kMSkewY, SkFloatToScalar(params.transformation[1][0])); | |
| 145 matrix.set(SkMatrix::kMScaleY, SkFloatToScalar(params.transformation[1][1])); | |
| 146 matrix.set(SkMatrix::kMTransY, SkFloatToScalar(params.transformation[1][2])); | |
| 147 matrix.set(SkMatrix::kMPersp0, SkFloatToScalar(params.transformation[2][0])); | |
| 148 matrix.set(SkMatrix::kMPersp1, SkFloatToScalar(params.transformation[2][1])); | |
| 149 matrix.set(SkMatrix::kMPersp2, SkFloatToScalar(params.transformation[2][2])); | |
| 150 canvas->concat(matrix); | |
| 151 | |
| 152 SkPaint paint; | |
| 153 paint.setColor(params.color); | |
| 154 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
| 155 paint.setAntiAlias(true); | |
| 156 paint.setHinting(SkPaint::kFull_Hinting); | |
| 157 paint.setTextSize(SkIntToScalar(params.font_desc.size)); | |
| 158 paint.setTypeface(typeface.get()); // Takes a ref and manages lifetime. | |
| 159 if (params.allow_subpixel_aa) { | |
| 160 paint.setSubpixelText(true); | |
| 161 paint.setLCDRenderText(true); | |
| 162 } | |
| 163 | |
| 164 SkScalar x = SkIntToScalar(params.position.x); | |
| 165 SkScalar y = SkIntToScalar(params.position.y); | |
| 166 | |
| 167 // Build up the skia advances. | |
| 168 size_t glyph_count = params.glyph_indices.size(); | |
| 169 if (glyph_count == 0) | |
| 170 return PP_OK; | |
| 171 std::vector<SkPoint> storage; | |
| 172 storage.resize(glyph_count); | |
| 173 SkPoint* sk_positions = &storage[0]; | |
| 174 for (uint32_t i = 0; i < glyph_count; i++) { | |
| 175 sk_positions[i].set(x, y); | |
| 176 x += SkFloatToScalar(params.glyph_advances[i].x); | |
| 177 y += SkFloatToScalar(params.glyph_advances[i].y); | |
| 178 } | |
| 179 | |
| 180 canvas->drawPosText(¶ms.glyph_indices[0], glyph_count * 2, sk_positions, | |
| 181 paint); | |
| 182 | |
| 183 return PP_OK; | |
| 184 } | |
| 185 | |
| 186 int32_t PepperFlashRendererHost::OnMsgNavigate( | |
| 187 ppapi::host::HostMessageContext* host_context, | |
| 188 const ppapi::URLRequestInfoData& data, | |
| 189 const std::string& target, | |
| 190 bool from_user_action) { | |
| 191 // We need to allow re-entrancy here, because this may call into Javascript | |
| 192 // (e.g. with a "javascript:" URL), or do things like navigate away from the | |
| 193 // page, either one of which will need to re-enter into the plugin. | |
| 194 // It is safe, because it is essentially equivalent to NPN_GetURL, where Flash | |
| 195 // would expect re-entrancy. | |
| 196 ppapi::proxy::HostDispatcher* host_dispatcher = | |
| 197 ppapi::proxy::HostDispatcher::GetForInstance(pp_instance()); | |
| 198 host_dispatcher->set_allow_plugin_reentrancy(); | |
| 199 | |
| 200 webkit::ppapi::PluginInstance* plugin_instance = | |
| 201 host_->GetPluginInstance(pp_instance()); | |
| 202 plugin_instance->Navigate(data, target.c_str(), from_user_action); | |
| 203 return PP_OK; | |
| 204 } | |
| 205 | |
| 206 int32_t PepperFlashRendererHost::OnMsgIsRectTopmost( | |
| 207 ppapi::host::HostMessageContext* host_context, | |
| 208 const PP_Rect& rect) { | |
| 209 webkit::ppapi::PluginInstance* plugin_instance = | |
| 210 host_->GetPluginInstance(pp_instance()); | |
| 211 if (plugin_instance->IsRectTopmost(gfx::Rect( | |
| 212 rect.point.x, rect.point.y,rect.size.width, rect.size.height))) | |
| 213 return PP_OK; | |
| 214 return PP_ERROR_FAILED; | |
| 215 } | |
| 216 | |
| 55 } // namespace content | 217 } // namespace content |
| OLD | NEW |