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

Side by Side Diff: chrome/renderer/pepper/pepper_pdf_host.cc

Issue 505243003: pdf: Move PepperPDFHost into the pdf component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pdf-component
Patch Set: tot-merge Created 6 years, 3 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
« no previous file with comments | « chrome/renderer/pepper/pepper_pdf_host.h ('k') | components/pdf.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "chrome/renderer/pepper/pepper_pdf_host.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/renderer/printing/print_web_view_helper.h"
9 #include "components/pdf/common/pdf_messages.h"
10 #include "content/app/resources/grit/content_resources.h"
11 #include "content/app/strings/grit/content_strings.h"
12 #include "content/public/common/referrer.h"
13 #include "content/public/renderer/pepper_plugin_instance.h"
14 #include "content/public/renderer/render_thread.h"
15 #include "content/public/renderer/render_view.h"
16 #include "content/public/renderer/renderer_ppapi_host.h"
17 #include "ppapi/host/dispatch_host_message.h"
18 #include "ppapi/host/host_message_context.h"
19 #include "ppapi/host/ppapi_host.h"
20 #include "ppapi/proxy/host_dispatcher.h"
21 #include "ppapi/proxy/ppapi_messages.h"
22 #include "ppapi/proxy/ppb_image_data_proxy.h"
23 #include "ppapi/shared_impl/ppb_image_data_shared.h"
24 #include "ppapi/shared_impl/scoped_pp_resource.h"
25 #include "ppapi/thunk/enter.h"
26 #include "ppapi/thunk/ppb_image_data_api.h"
27 #include "skia/ext/platform_canvas.h"
28 #include "third_party/WebKit/public/web/WebDocument.h"
29 #include "third_party/WebKit/public/web/WebElement.h"
30 #include "third_party/WebKit/public/web/WebLocalFrame.h"
31 #include "third_party/WebKit/public/web/WebPluginContainer.h"
32 #include "third_party/WebKit/public/web/WebView.h"
33 #include "third_party/skia/include/core/SkBitmap.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/layout.h"
36 #include "ui/base/resource/resource_bundle.h"
37 #include "ui/gfx/image/image_skia.h"
38 #include "ui/gfx/image/image_skia_rep.h"
39 #include "ui/gfx/point.h"
40
41 namespace {
42
43 struct ResourceImageInfo {
44 PP_ResourceImage pp_id;
45 int res_id;
46 };
47
48 const ResourceImageInfo kResourceImageMap[] = {
49 {PP_RESOURCEIMAGE_PDF_BUTTON_FTP, IDR_PDF_BUTTON_FTP},
50 {PP_RESOURCEIMAGE_PDF_BUTTON_FTP_HOVER, IDR_PDF_BUTTON_FTP_HOVER},
51 {PP_RESOURCEIMAGE_PDF_BUTTON_FTP_PRESSED, IDR_PDF_BUTTON_FTP_PRESSED},
52 {PP_RESOURCEIMAGE_PDF_BUTTON_FTW, IDR_PDF_BUTTON_FTW},
53 {PP_RESOURCEIMAGE_PDF_BUTTON_FTW_HOVER, IDR_PDF_BUTTON_FTW_HOVER},
54 {PP_RESOURCEIMAGE_PDF_BUTTON_FTW_PRESSED, IDR_PDF_BUTTON_FTW_PRESSED},
55 {PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMIN_END, IDR_PDF_BUTTON_ZOOMIN_END},
56 {PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMIN_END_HOVER,
57 IDR_PDF_BUTTON_ZOOMIN_END_HOVER},
58 {PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMIN_END_PRESSED,
59 IDR_PDF_BUTTON_ZOOMIN_END_PRESSED},
60 {PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMIN, IDR_PDF_BUTTON_ZOOMIN},
61 {PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMIN_HOVER, IDR_PDF_BUTTON_ZOOMIN_HOVER},
62 {PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMIN_PRESSED, IDR_PDF_BUTTON_ZOOMIN_PRESSED},
63 {PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMOUT, IDR_PDF_BUTTON_ZOOMOUT},
64 {PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMOUT_HOVER, IDR_PDF_BUTTON_ZOOMOUT_HOVER},
65 {PP_RESOURCEIMAGE_PDF_BUTTON_ZOOMOUT_PRESSED,
66 IDR_PDF_BUTTON_ZOOMOUT_PRESSED},
67 {PP_RESOURCEIMAGE_PDF_BUTTON_SAVE, IDR_PDF_BUTTON_SAVE},
68 {PP_RESOURCEIMAGE_PDF_BUTTON_SAVE_HOVER, IDR_PDF_BUTTON_SAVE_HOVER},
69 {PP_RESOURCEIMAGE_PDF_BUTTON_SAVE_PRESSED, IDR_PDF_BUTTON_SAVE_PRESSED},
70 {PP_RESOURCEIMAGE_PDF_BUTTON_PRINT, IDR_PDF_BUTTON_PRINT},
71 {PP_RESOURCEIMAGE_PDF_BUTTON_PRINT_HOVER, IDR_PDF_BUTTON_PRINT_HOVER},
72 {PP_RESOURCEIMAGE_PDF_BUTTON_PRINT_PRESSED, IDR_PDF_BUTTON_PRINT_PRESSED},
73 {PP_RESOURCEIMAGE_PDF_BUTTON_PRINT_DISABLED, IDR_PDF_BUTTON_PRINT_DISABLED},
74 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_0, IDR_PDF_THUMBNAIL_0},
75 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_1, IDR_PDF_THUMBNAIL_1},
76 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_2, IDR_PDF_THUMBNAIL_2},
77 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_3, IDR_PDF_THUMBNAIL_3},
78 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_4, IDR_PDF_THUMBNAIL_4},
79 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_5, IDR_PDF_THUMBNAIL_5},
80 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_6, IDR_PDF_THUMBNAIL_6},
81 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_7, IDR_PDF_THUMBNAIL_7},
82 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_8, IDR_PDF_THUMBNAIL_8},
83 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_9, IDR_PDF_THUMBNAIL_9},
84 {PP_RESOURCEIMAGE_PDF_BUTTON_THUMBNAIL_NUM_BACKGROUND,
85 IDR_PDF_THUMBNAIL_NUM_BACKGROUND},
86 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_0, IDR_PDF_PROGRESS_BAR_0},
87 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_1, IDR_PDF_PROGRESS_BAR_1},
88 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_2, IDR_PDF_PROGRESS_BAR_2},
89 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_3, IDR_PDF_PROGRESS_BAR_3},
90 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_4, IDR_PDF_PROGRESS_BAR_4},
91 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_5, IDR_PDF_PROGRESS_BAR_5},
92 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_6, IDR_PDF_PROGRESS_BAR_6},
93 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_7, IDR_PDF_PROGRESS_BAR_7},
94 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_8, IDR_PDF_PROGRESS_BAR_8},
95 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_BACKGROUND,
96 IDR_PDF_PROGRESS_BAR_BACKGROUND},
97 {PP_RESOURCEIMAGE_PDF_PAGE_INDICATOR_BACKGROUND,
98 IDR_PDF_PAGE_INDICATOR_BACKGROUND},
99 {PP_RESOURCEIMAGE_PDF_PAGE_DROPSHADOW, IDR_PDF_PAGE_DROPSHADOW},
100 {PP_RESOURCEIMAGE_PDF_PAN_SCROLL_ICON, IDR_PAN_SCROLL_ICON}, };
101
102 } // namespace
103
104 PepperPDFHost::PepperPDFHost(content::RendererPpapiHost* host,
105 PP_Instance instance,
106 PP_Resource resource)
107 : ppapi::host::ResourceHost(host->GetPpapiHost(), instance, resource),
108 host_(host) {}
109
110 PepperPDFHost::~PepperPDFHost() {}
111
112 int32_t PepperPDFHost::OnResourceMessageReceived(
113 const IPC::Message& msg,
114 ppapi::host::HostMessageContext* context) {
115 PPAPI_BEGIN_MESSAGE_MAP(PepperPDFHost, msg)
116 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_GetLocalizedString,
117 OnHostMsgGetLocalizedString)
118 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStartLoading,
119 OnHostMsgDidStartLoading)
120 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_DidStopLoading,
121 OnHostMsgDidStopLoading)
122 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_UserMetricsRecordAction,
123 OnHostMsgUserMetricsRecordAction)
124 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_HasUnsupportedFeature,
125 OnHostMsgHasUnsupportedFeature)
126 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_Print, OnHostMsgPrint)
127 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_PDF_SaveAs,
128 OnHostMsgSaveAs)
129 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_GetResourceImage,
130 OnHostMsgGetResourceImage)
131 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetSelectedText,
132 OnHostMsgSetSelectedText)
133 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_PDF_SetLinkUnderCursor,
134 OnHostMsgSetLinkUnderCursor)
135 PPAPI_END_MESSAGE_MAP()
136 return PP_ERROR_FAILED;
137 }
138
139 int32_t PepperPDFHost::OnHostMsgGetLocalizedString(
140 ppapi::host::HostMessageContext* context,
141 PP_ResourceString string_id) {
142 std::string rv;
143 if (string_id == PP_RESOURCESTRING_PDFGETPASSWORD) {
144 rv = base::UTF16ToUTF8(l10n_util::GetStringUTF16(IDS_PDF_NEED_PASSWORD));
145 } else if (string_id == PP_RESOURCESTRING_PDFLOADING) {
146 rv = base::UTF16ToUTF8(l10n_util::GetStringUTF16(IDS_PDF_PAGE_LOADING));
147 } else if (string_id == PP_RESOURCESTRING_PDFLOAD_FAILED) {
148 rv = base::UTF16ToUTF8(l10n_util::GetStringUTF16(IDS_PDF_PAGE_LOAD_FAILED));
149 } else if (string_id == PP_RESOURCESTRING_PDFPROGRESSLOADING) {
150 rv = base::UTF16ToUTF8(l10n_util::GetStringUTF16(IDS_PDF_PROGRESS_LOADING));
151 } else {
152 NOTREACHED();
153 return PP_ERROR_FAILED;
154 }
155
156 context->reply_msg = PpapiPluginMsg_PDF_GetLocalizedStringReply(rv);
157 return PP_OK;
158 }
159
160 int32_t PepperPDFHost::OnHostMsgDidStartLoading(
161 ppapi::host::HostMessageContext* context) {
162 content::PepperPluginInstance* instance =
163 host_->GetPluginInstance(pp_instance());
164 if (!instance)
165 return PP_ERROR_FAILED;
166 instance->GetRenderView()->DidStartLoading();
167 return PP_OK;
168 }
169
170 int32_t PepperPDFHost::OnHostMsgDidStopLoading(
171 ppapi::host::HostMessageContext* context) {
172 content::PepperPluginInstance* instance =
173 host_->GetPluginInstance(pp_instance());
174 if (!instance)
175 return PP_ERROR_FAILED;
176 instance->GetRenderView()->DidStopLoading();
177 return PP_OK;
178 }
179
180 int32_t PepperPDFHost::OnHostMsgSetContentRestriction(
181 ppapi::host::HostMessageContext* context,
182 int restrictions) {
183 content::PepperPluginInstance* instance =
184 host_->GetPluginInstance(pp_instance());
185 if (!instance)
186 return PP_ERROR_FAILED;
187 instance->GetRenderView()->Send(new PDFHostMsg_PDFUpdateContentRestrictions(
188 instance->GetRenderView()->GetRoutingID(), restrictions));
189 return PP_OK;
190 }
191
192 int32_t PepperPDFHost::OnHostMsgUserMetricsRecordAction(
193 ppapi::host::HostMessageContext* context,
194 const std::string& action) {
195 if (action.empty())
196 return PP_ERROR_FAILED;
197 content::RenderThread::Get()->RecordComputedAction(action);
198 return PP_OK;
199 }
200
201 int32_t PepperPDFHost::OnHostMsgHasUnsupportedFeature(
202 ppapi::host::HostMessageContext* context) {
203 content::PepperPluginInstance* instance =
204 host_->GetPluginInstance(pp_instance());
205 if (!instance)
206 return PP_ERROR_FAILED;
207
208 blink::WebView* view =
209 instance->GetContainer()->element().document().frame()->view();
210 content::RenderView* render_view = content::RenderView::FromWebView(view);
211 render_view->Send(
212 new PDFHostMsg_PDFHasUnsupportedFeature(render_view->GetRoutingID()));
213 return PP_OK;
214 }
215
216 int32_t PepperPDFHost::OnHostMsgPrint(
217 ppapi::host::HostMessageContext* context) {
218 #if defined(ENABLE_FULL_PRINTING)
219 content::PepperPluginInstance* instance =
220 host_->GetPluginInstance(pp_instance());
221 if (!instance)
222 return PP_ERROR_FAILED;
223
224 blink::WebElement element = instance->GetContainer()->element();
225 blink::WebView* view = element.document().frame()->view();
226 content::RenderView* render_view = content::RenderView::FromWebView(view);
227
228 using printing::PrintWebViewHelper;
229 PrintWebViewHelper* print_view_helper = PrintWebViewHelper::Get(render_view);
230 if (print_view_helper) {
231 print_view_helper->PrintNode(element);
232 return PP_OK;
233 }
234 #endif
235 return PP_ERROR_FAILED;
236 }
237
238 int32_t PepperPDFHost::OnHostMsgSaveAs(
239 ppapi::host::HostMessageContext* context) {
240 content::PepperPluginInstance* instance =
241 host_->GetPluginInstance(pp_instance());
242 if (!instance)
243 return PP_ERROR_FAILED;
244 GURL url = instance->GetPluginURL();
245 content::RenderView* render_view = instance->GetRenderView();
246 blink::WebLocalFrame* frame =
247 render_view->GetWebView()->mainFrame()->toWebLocalFrame();
248 content::Referrer referrer(frame->document().url(),
249 frame->document().referrerPolicy());
250 render_view->Send(
251 new PDFHostMsg_PDFSaveURLAs(render_view->GetRoutingID(), url, referrer));
252 return PP_OK;
253 }
254
255 int32_t PepperPDFHost::OnHostMsgGetResourceImage(
256 ppapi::host::HostMessageContext* context,
257 PP_ResourceImage image_id,
258 float scale) {
259 int res_id = 0;
260 for (size_t i = 0; i < arraysize(kResourceImageMap); ++i) {
261 if (kResourceImageMap[i].pp_id == image_id) {
262 res_id = kResourceImageMap[i].res_id;
263 break;
264 }
265 }
266 if (res_id == 0)
267 return PP_ERROR_FAILED;
268
269 gfx::ImageSkia* res_image_skia =
270 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(res_id);
271
272 if (!res_image_skia)
273 return PP_ERROR_FAILED;
274
275 gfx::ImageSkiaRep image_skia_rep = res_image_skia->GetRepresentation(scale);
276
277 if (image_skia_rep.is_null() || image_skia_rep.scale() != scale)
278 return PP_ERROR_FAILED;
279
280 PP_Size pp_size;
281 pp_size.width = image_skia_rep.pixel_width();
282 pp_size.height = image_skia_rep.pixel_height();
283
284 ppapi::HostResource host_resource;
285 PP_ImageDataDesc image_data_desc;
286 IPC::PlatformFileForTransit image_handle;
287 uint32_t byte_count = 0;
288 bool success =
289 CreateImageData(pp_instance(),
290 ppapi::PPB_ImageData_Shared::GetNativeImageDataFormat(),
291 pp_size,
292 image_skia_rep.sk_bitmap(),
293 &host_resource,
294 &image_data_desc,
295 &image_handle,
296 &byte_count);
297 ppapi::ScopedPPResource image_data_resource(
298 ppapi::ScopedPPResource::PassRef(), host_resource.host_resource());
299 if (!success)
300 return PP_ERROR_FAILED;
301
302 ppapi::host::ReplyMessageContext reply_context =
303 context->MakeReplyMessageContext();
304 ppapi::proxy::SerializedHandle serialized_handle;
305 serialized_handle.set_shmem(image_handle, byte_count);
306 reply_context.params.AppendHandle(serialized_handle);
307 SendReply(
308 reply_context,
309 PpapiPluginMsg_PDF_GetResourceImageReply(host_resource, image_data_desc));
310
311 // Keep a reference to the resource only if the function succeeds.
312 image_data_resource.Release();
313
314 return PP_OK_COMPLETIONPENDING;
315 }
316
317 int32_t PepperPDFHost::OnHostMsgSetSelectedText(
318 ppapi::host::HostMessageContext* context,
319 const base::string16& selected_text) {
320 content::PepperPluginInstance* instance =
321 host_->GetPluginInstance(pp_instance());
322 if (!instance)
323 return PP_ERROR_FAILED;
324 instance->SetSelectedText(selected_text);
325 return PP_OK;
326 }
327
328 int32_t PepperPDFHost::OnHostMsgSetLinkUnderCursor(
329 ppapi::host::HostMessageContext* context,
330 const std::string& url) {
331 content::PepperPluginInstance* instance =
332 host_->GetPluginInstance(pp_instance());
333 if (!instance)
334 return PP_ERROR_FAILED;
335 instance->SetLinkUnderCursor(url);
336 return PP_OK;
337 }
338
339 // TODO(raymes): This function is mainly copied from ppb_image_data_proxy.cc.
340 // It's a mess and needs to be fixed in several ways but this is better done
341 // when we refactor PPB_ImageData. On success, the image handle will be
342 // non-null.
343 bool PepperPDFHost::CreateImageData(
344 PP_Instance instance,
345 PP_ImageDataFormat format,
346 const PP_Size& size,
347 const SkBitmap& pixels_to_write,
348 ppapi::HostResource* result,
349 PP_ImageDataDesc* out_image_data_desc,
350 IPC::PlatformFileForTransit* out_image_handle,
351 uint32_t* out_byte_count) {
352 PP_Resource resource = ppapi::proxy::PPB_ImageData_Proxy::CreateImageData(
353 instance,
354 ppapi::PPB_ImageData_Shared::SIMPLE,
355 format,
356 size,
357 false /* init_to_zero */,
358 out_image_data_desc,
359 out_image_handle,
360 out_byte_count);
361 if (!resource)
362 return false;
363
364 result->SetHostResource(instance, resource);
365
366 // Write the image to the resource shared memory.
367 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API>
368 enter_resource(resource, false);
369 if (enter_resource.failed())
370 return false;
371
372 ppapi::thunk::PPB_ImageData_API* image_data =
373 static_cast<ppapi::thunk::PPB_ImageData_API*>(enter_resource.object());
374 SkCanvas* canvas = image_data->GetCanvas();
375 bool needs_unmapping = false;
376 if (!canvas) {
377 needs_unmapping = true;
378 image_data->Map();
379 canvas = image_data->GetCanvas();
380 if (!canvas)
381 return false; // Failure mapping.
382 }
383
384 const SkBitmap* bitmap = &skia::GetTopDevice(*canvas)->accessBitmap(false);
385 pixels_to_write.copyPixelsTo(
386 bitmap->getPixels(), bitmap->getSize(), bitmap->rowBytes());
387
388 if (needs_unmapping)
389 image_data->Unmap();
390
391 return true;
392 }
OLDNEW
« no previous file with comments | « chrome/renderer/pepper/pepper_pdf_host.h ('k') | components/pdf.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698