OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/pdf/renderer/pepper_pdf_host.h" | 5 #include "components/pdf/renderer/pepper_pdf_host.h" |
6 | 6 |
7 #include "components/pdf/common/pdf_messages.h" | 7 #include "components/pdf/common/pdf_messages.h" |
8 #include "components/pdf/renderer/pdf_resource_util.h" | 8 #include "components/pdf/renderer/pdf_resource_util.h" |
9 #include "components/pdf/renderer/ppb_pdf_impl.h" | |
10 #include "content/public/common/referrer.h" | 9 #include "content/public/common/referrer.h" |
11 #include "content/public/renderer/pepper_plugin_instance.h" | 10 #include "content/public/renderer/pepper_plugin_instance.h" |
12 #include "content/public/renderer/render_thread.h" | 11 #include "content/public/renderer/render_thread.h" |
13 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
14 #include "content/public/renderer/renderer_ppapi_host.h" | 13 #include "content/public/renderer/renderer_ppapi_host.h" |
15 #include "ppapi/host/dispatch_host_message.h" | 14 #include "ppapi/host/dispatch_host_message.h" |
16 #include "ppapi/host/host_message_context.h" | 15 #include "ppapi/host/host_message_context.h" |
17 #include "ppapi/host/ppapi_host.h" | 16 #include "ppapi/host/ppapi_host.h" |
18 #include "ppapi/proxy/host_dispatcher.h" | 17 #include "ppapi/proxy/host_dispatcher.h" |
19 #include "ppapi/proxy/ppapi_messages.h" | 18 #include "ppapi/proxy/ppapi_messages.h" |
20 #include "ppapi/proxy/ppb_image_data_proxy.h" | 19 #include "ppapi/proxy/ppb_image_data_proxy.h" |
21 #include "ppapi/shared_impl/ppb_image_data_shared.h" | 20 #include "ppapi/shared_impl/ppb_image_data_shared.h" |
22 #include "ppapi/shared_impl/scoped_pp_resource.h" | 21 #include "ppapi/shared_impl/scoped_pp_resource.h" |
23 #include "ppapi/thunk/enter.h" | 22 #include "ppapi/thunk/enter.h" |
24 #include "ppapi/thunk/ppb_image_data_api.h" | 23 #include "ppapi/thunk/ppb_image_data_api.h" |
25 #include "skia/ext/platform_canvas.h" | |
26 #include "third_party/WebKit/public/web/WebDocument.h" | 24 #include "third_party/WebKit/public/web/WebDocument.h" |
27 #include "third_party/WebKit/public/web/WebElement.h" | 25 #include "third_party/WebKit/public/web/WebElement.h" |
28 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
29 #include "third_party/WebKit/public/web/WebPluginContainer.h" | 27 #include "third_party/WebKit/public/web/WebPluginContainer.h" |
30 #include "third_party/WebKit/public/web/WebView.h" | 28 #include "third_party/WebKit/public/web/WebView.h" |
31 #include "third_party/skia/include/core/SkBitmap.h" | |
32 #include "ui/base/layout.h" | 29 #include "ui/base/layout.h" |
33 #include "ui/gfx/geometry/point.h" | 30 #include "ui/gfx/geometry/point.h" |
34 #include "ui/gfx/image/image_skia.h" | |
35 #include "ui/gfx/image/image_skia_rep.h" | |
36 | 31 |
37 namespace pdf { | 32 namespace pdf { |
38 | 33 |
| 34 namespace { |
| 35 // --single-process model may fail in CHECK(!g_print_client) if there exist |
| 36 // more than two RenderThreads, so here we use TLS for g_print_client. |
| 37 // See http://crbug.com/457580. |
| 38 base::LazyInstance<base::ThreadLocalPointer<PepperPDFHost::PrintClient>>::Leaky |
| 39 g_print_client_tls = LAZY_INSTANCE_INITIALIZER; |
| 40 |
| 41 } // namespace |
| 42 |
39 PepperPDFHost::PepperPDFHost(content::RendererPpapiHost* host, | 43 PepperPDFHost::PepperPDFHost(content::RendererPpapiHost* host, |
40 PP_Instance instance, | 44 PP_Instance instance, |
41 PP_Resource resource) | 45 PP_Resource resource) |
42 : ppapi::host::ResourceHost(host->GetPpapiHost(), instance, resource), | 46 : ppapi::host::ResourceHost(host->GetPpapiHost(), instance, resource), |
43 host_(host) {} | 47 host_(host) {} |
44 | 48 |
45 PepperPDFHost::~PepperPDFHost() {} | 49 PepperPDFHost::~PepperPDFHost() {} |
46 | 50 |
| 51 // static |
| 52 bool PepperPDFHost::InvokePrintingForInstance(PP_Instance instance_id) { |
| 53 return g_print_client_tls.Pointer()->Get() |
| 54 ? g_print_client_tls.Pointer()->Get()->Print(instance_id) |
| 55 : false; |
| 56 } |
| 57 |
| 58 // static |
| 59 void PepperPDFHost::SetPrintClient(PepperPDFHost::PrintClient* client) { |
| 60 CHECK(!g_print_client_tls.Pointer()->Get()) |
| 61 << "There should only be a single PrintClient for one RenderThread."; |
| 62 g_print_client_tls.Pointer()->Set(client); |
| 63 } |
| 64 |
47 int32_t PepperPDFHost::OnResourceMessageReceived( | 65 int32_t PepperPDFHost::OnResourceMessageReceived( |
48 const IPC::Message& msg, | 66 const IPC::Message& msg, |
49 ppapi::host::HostMessageContext* context) { | 67 ppapi::host::HostMessageContext* context) { |
50 PPAPI_BEGIN_MESSAGE_MAP(PepperPDFHost, msg) | 68 PPAPI_BEGIN_MESSAGE_MAP(PepperPDFHost, msg) |
51 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_GetLocalizedString, | 69 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_GetLocalizedString, |
52 OnHostMsgGetLocalizedString) | 70 OnHostMsgGetLocalizedString) |
53 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStartLoading, | 71 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStartLoading, |
54 OnHostMsgDidStartLoading) | 72 OnHostMsgDidStartLoading) |
55 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStopLoading, | 73 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStopLoading, |
56 OnHostMsgDidStopLoading) | 74 OnHostMsgDidStopLoading) |
57 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_UserMetricsRecordAction, | 75 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_UserMetricsRecordAction, |
58 OnHostMsgUserMetricsRecordAction) | 76 OnHostMsgUserMetricsRecordAction) |
59 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_HasUnsupportedFeature, | 77 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_HasUnsupportedFeature, |
60 OnHostMsgHasUnsupportedFeature) | 78 OnHostMsgHasUnsupportedFeature) |
61 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_Print, OnHostMsgPrint) | 79 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_Print, OnHostMsgPrint) |
62 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_SaveAs, | 80 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_SaveAs, |
63 OnHostMsgSaveAs) | 81 OnHostMsgSaveAs) |
64 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_GetResourceImage, | |
65 OnHostMsgGetResourceImage) | |
66 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetSelectedText, | 82 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetSelectedText, |
67 OnHostMsgSetSelectedText) | 83 OnHostMsgSetSelectedText) |
68 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetLinkUnderCursor, | 84 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetLinkUnderCursor, |
69 OnHostMsgSetLinkUnderCursor) | 85 OnHostMsgSetLinkUnderCursor) |
70 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetContentRestriction, | 86 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetContentRestriction, |
71 OnHostMsgSetContentRestriction) | 87 OnHostMsgSetContentRestriction) |
72 PPAPI_END_MESSAGE_MAP() | 88 PPAPI_END_MESSAGE_MAP() |
73 return PP_ERROR_FAILED; | 89 return PP_ERROR_FAILED; |
74 } | 90 } |
75 | 91 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 blink::WebView* view = | 148 blink::WebView* view = |
133 instance->GetContainer()->element().document().frame()->view(); | 149 instance->GetContainer()->element().document().frame()->view(); |
134 content::RenderView* render_view = content::RenderView::FromWebView(view); | 150 content::RenderView* render_view = content::RenderView::FromWebView(view); |
135 render_view->Send( | 151 render_view->Send( |
136 new PDFHostMsg_PDFHasUnsupportedFeature(render_view->GetRoutingID())); | 152 new PDFHostMsg_PDFHasUnsupportedFeature(render_view->GetRoutingID())); |
137 return PP_OK; | 153 return PP_OK; |
138 } | 154 } |
139 | 155 |
140 int32_t PepperPDFHost::OnHostMsgPrint( | 156 int32_t PepperPDFHost::OnHostMsgPrint( |
141 ppapi::host::HostMessageContext* context) { | 157 ppapi::host::HostMessageContext* context) { |
142 return PPB_PDF_Impl::InvokePrintingForInstance(pp_instance()) ? PP_OK : | 158 return InvokePrintingForInstance(pp_instance()) ? PP_OK : PP_ERROR_FAILED; |
143 PP_ERROR_FAILED; | |
144 } | 159 } |
145 | 160 |
146 int32_t PepperPDFHost::OnHostMsgSaveAs( | 161 int32_t PepperPDFHost::OnHostMsgSaveAs( |
147 ppapi::host::HostMessageContext* context) { | 162 ppapi::host::HostMessageContext* context) { |
148 content::PepperPluginInstance* instance = | 163 content::PepperPluginInstance* instance = |
149 host_->GetPluginInstance(pp_instance()); | 164 host_->GetPluginInstance(pp_instance()); |
150 if (!instance) | 165 if (!instance) |
151 return PP_ERROR_FAILED; | 166 return PP_ERROR_FAILED; |
152 GURL url = instance->GetPluginURL(); | 167 GURL url = instance->GetPluginURL(); |
153 content::RenderView* render_view = instance->GetRenderView(); | 168 content::RenderView* render_view = instance->GetRenderView(); |
154 blink::WebLocalFrame* frame = | 169 blink::WebLocalFrame* frame = |
155 render_view->GetWebView()->mainFrame()->toWebLocalFrame(); | 170 render_view->GetWebView()->mainFrame()->toWebLocalFrame(); |
156 content::Referrer referrer = content::Referrer::SanitizeForRequest( | 171 content::Referrer referrer = content::Referrer::SanitizeForRequest( |
157 url, content::Referrer(frame->document().url(), | 172 url, content::Referrer(frame->document().url(), |
158 frame->document().referrerPolicy())); | 173 frame->document().referrerPolicy())); |
159 render_view->Send( | 174 render_view->Send( |
160 new PDFHostMsg_PDFSaveURLAs(render_view->GetRoutingID(), url, referrer)); | 175 new PDFHostMsg_PDFSaveURLAs(render_view->GetRoutingID(), url, referrer)); |
161 return PP_OK; | 176 return PP_OK; |
162 } | 177 } |
163 | 178 |
164 int32_t PepperPDFHost::OnHostMsgGetResourceImage( | |
165 ppapi::host::HostMessageContext* context, | |
166 PP_ResourceImage image_id, | |
167 float scale) { | |
168 gfx::ImageSkia* res_image_skia = GetImageResource(image_id); | |
169 | |
170 if (!res_image_skia) | |
171 return PP_ERROR_FAILED; | |
172 | |
173 gfx::ImageSkiaRep image_skia_rep = res_image_skia->GetRepresentation(scale); | |
174 | |
175 if (image_skia_rep.is_null() || image_skia_rep.scale() != scale) | |
176 return PP_ERROR_FAILED; | |
177 | |
178 PP_Size pp_size; | |
179 pp_size.width = image_skia_rep.pixel_width(); | |
180 pp_size.height = image_skia_rep.pixel_height(); | |
181 | |
182 ppapi::HostResource host_resource; | |
183 PP_ImageDataDesc image_data_desc; | |
184 IPC::PlatformFileForTransit image_handle; | |
185 uint32_t byte_count = 0; | |
186 bool success = | |
187 CreateImageData(pp_instance(), | |
188 ppapi::PPB_ImageData_Shared::GetNativeImageDataFormat(), | |
189 pp_size, | |
190 image_skia_rep.sk_bitmap(), | |
191 &host_resource, | |
192 &image_data_desc, | |
193 &image_handle, | |
194 &byte_count); | |
195 ppapi::ScopedPPResource image_data_resource( | |
196 ppapi::ScopedPPResource::PassRef(), host_resource.host_resource()); | |
197 if (!success) | |
198 return PP_ERROR_FAILED; | |
199 | |
200 ppapi::host::ReplyMessageContext reply_context = | |
201 context->MakeReplyMessageContext(); | |
202 ppapi::proxy::SerializedHandle serialized_handle; | |
203 serialized_handle.set_shmem(image_handle, byte_count); | |
204 reply_context.params.AppendHandle(serialized_handle); | |
205 SendReply( | |
206 reply_context, | |
207 PpapiPluginMsg_PDF_GetResourceImageReply(host_resource, image_data_desc)); | |
208 | |
209 // Keep a reference to the resource only if the function succeeds. | |
210 image_data_resource.Release(); | |
211 | |
212 return PP_OK_COMPLETIONPENDING; | |
213 } | |
214 | |
215 int32_t PepperPDFHost::OnHostMsgSetSelectedText( | 179 int32_t PepperPDFHost::OnHostMsgSetSelectedText( |
216 ppapi::host::HostMessageContext* context, | 180 ppapi::host::HostMessageContext* context, |
217 const base::string16& selected_text) { | 181 const base::string16& selected_text) { |
218 content::PepperPluginInstance* instance = | 182 content::PepperPluginInstance* instance = |
219 host_->GetPluginInstance(pp_instance()); | 183 host_->GetPluginInstance(pp_instance()); |
220 if (!instance) | 184 if (!instance) |
221 return PP_ERROR_FAILED; | 185 return PP_ERROR_FAILED; |
222 instance->SetSelectedText(selected_text); | 186 instance->SetSelectedText(selected_text); |
223 return PP_OK; | 187 return PP_OK; |
224 } | 188 } |
225 | 189 |
226 int32_t PepperPDFHost::OnHostMsgSetLinkUnderCursor( | 190 int32_t PepperPDFHost::OnHostMsgSetLinkUnderCursor( |
227 ppapi::host::HostMessageContext* context, | 191 ppapi::host::HostMessageContext* context, |
228 const std::string& url) { | 192 const std::string& url) { |
229 content::PepperPluginInstance* instance = | 193 content::PepperPluginInstance* instance = |
230 host_->GetPluginInstance(pp_instance()); | 194 host_->GetPluginInstance(pp_instance()); |
231 if (!instance) | 195 if (!instance) |
232 return PP_ERROR_FAILED; | 196 return PP_ERROR_FAILED; |
233 instance->SetLinkUnderCursor(url); | 197 instance->SetLinkUnderCursor(url); |
234 return PP_OK; | 198 return PP_OK; |
235 } | 199 } |
236 | 200 |
237 // TODO(raymes): This function is mainly copied from ppb_image_data_proxy.cc. | |
238 // It's a mess and needs to be fixed in several ways but this is better done | |
239 // when we refactor PPB_ImageData. On success, the image handle will be | |
240 // non-null. | |
241 bool PepperPDFHost::CreateImageData( | |
242 PP_Instance instance, | |
243 PP_ImageDataFormat format, | |
244 const PP_Size& size, | |
245 const SkBitmap& pixels_to_write, | |
246 ppapi::HostResource* result, | |
247 PP_ImageDataDesc* out_image_data_desc, | |
248 IPC::PlatformFileForTransit* out_image_handle, | |
249 uint32_t* out_byte_count) { | |
250 PP_Resource resource = ppapi::proxy::PPB_ImageData_Proxy::CreateImageData( | |
251 instance, | |
252 ppapi::PPB_ImageData_Shared::SIMPLE, | |
253 format, | |
254 size, | |
255 false /* init_to_zero */, | |
256 out_image_data_desc, | |
257 out_image_handle, | |
258 out_byte_count); | |
259 if (!resource) | |
260 return false; | |
261 | |
262 result->SetHostResource(instance, resource); | |
263 | |
264 // Write the image to the resource shared memory. | |
265 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> | |
266 enter_resource(resource, false); | |
267 if (enter_resource.failed()) | |
268 return false; | |
269 | |
270 ppapi::thunk::PPB_ImageData_API* image_data = | |
271 static_cast<ppapi::thunk::PPB_ImageData_API*>(enter_resource.object()); | |
272 SkCanvas* canvas = image_data->GetCanvas(); | |
273 bool needs_unmapping = false; | |
274 if (!canvas) { | |
275 needs_unmapping = true; | |
276 image_data->Map(); | |
277 canvas = image_data->GetCanvas(); | |
278 if (!canvas) | |
279 return false; // Failure mapping. | |
280 } | |
281 | |
282 const SkBitmap* bitmap = &skia::GetTopDevice(*canvas)->accessBitmap(false); | |
283 pixels_to_write.copyPixelsTo( | |
284 bitmap->getPixels(), bitmap->getSize(), bitmap->rowBytes()); | |
285 | |
286 if (needs_unmapping) | |
287 image_data->Unmap(); | |
288 | |
289 return true; | |
290 } | |
291 | |
292 } // namespace pdf | 201 } // namespace pdf |
OLD | NEW |