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

Side by Side Diff: components/pdf/renderer/ppb_pdf_impl.cc

Issue 477263003: pdf: Create a separate component for using the pdf pepper plugin. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 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 "chrome/renderer/pepper/ppb_pdf_impl.h" 5 #include "components/pdf/renderer/ppb_pdf_impl.h"
6 6
7 #include "base/files/scoped_file.h" 7 #include "base/files/scoped_file.h"
8 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "base/numerics/safe_conversions.h" 9 #include "base/numerics/safe_conversions.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 #include "chrome/common/render_messages.h" 12 #include "components/pdf/common/pdf_messages.h"
13 #include "chrome/renderer/printing/print_web_view_helper.h"
14 #include "content/app/resources/grit/content_resources.h" 13 #include "content/app/resources/grit/content_resources.h"
15 #include "content/app/strings/grit/content_strings.h" 14 #include "content/app/strings/grit/content_strings.h"
16 #include "content/public/common/child_process_sandbox_support_linux.h" 15 #include "content/public/common/child_process_sandbox_support_linux.h"
17 #include "content/public/common/referrer.h" 16 #include "content/public/common/referrer.h"
18 #include "content/public/renderer/pepper_plugin_instance.h" 17 #include "content/public/renderer/pepper_plugin_instance.h"
19 #include "content/public/renderer/render_thread.h" 18 #include "content/public/renderer/render_thread.h"
20 #include "content/public/renderer/render_view.h" 19 #include "content/public/renderer/render_view.h"
21 #include "ppapi/c/pp_resource.h" 20 #include "ppapi/c/pp_resource.h"
22 #include "ppapi/c/private/ppb_pdf.h" 21 #include "ppapi/c/private/ppb_pdf.h"
23 #include "ppapi/c/trusted/ppb_browser_font_trusted.h" 22 #include "ppapi/c/trusted/ppb_browser_font_trusted.h"
24 #include "ppapi/shared_impl/ppapi_globals.h" 23 #include "ppapi/shared_impl/ppapi_globals.h"
25 #include "ppapi/shared_impl/resource.h" 24 #include "ppapi/shared_impl/resource.h"
26 #include "ppapi/shared_impl/resource_tracker.h" 25 #include "ppapi/shared_impl/resource_tracker.h"
27 #include "ppapi/shared_impl/var.h" 26 #include "ppapi/shared_impl/var.h"
28 #include "third_party/WebKit/public/web/WebDocument.h" 27 #include "third_party/WebKit/public/web/WebDocument.h"
29 #include "third_party/WebKit/public/web/WebElement.h" 28 #include "third_party/WebKit/public/web/WebElement.h"
30 #include "third_party/WebKit/public/web/WebLocalFrame.h" 29 #include "third_party/WebKit/public/web/WebLocalFrame.h"
31 #include "third_party/WebKit/public/web/WebPluginContainer.h" 30 #include "third_party/WebKit/public/web/WebPluginContainer.h"
32 #include "third_party/WebKit/public/web/WebView.h" 31 #include "third_party/WebKit/public/web/WebView.h"
33 #include "third_party/icu/source/i18n/unicode/usearch.h" 32 #include "third_party/icu/source/i18n/unicode/usearch.h"
34 #include "third_party/skia/include/core/SkBitmap.h" 33 #include "third_party/skia/include/core/SkBitmap.h"
35 #include "ui/base/l10n/l10n_util.h" 34 #include "ui/base/l10n/l10n_util.h"
36 #include "ui/base/resource/resource_bundle.h" 35 #include "ui/base/resource/resource_bundle.h"
37 36
37 namespace pdf {
38 namespace { 38 namespace {
39 39
40 PPB_PDF_Impl::PrintDelegate* print_delegate = NULL;
Lei Zhang 2014/08/26 22:41:31 g_print_delegate
sadrul 2014/08/27 00:09:52 Done.
41
40 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) 42 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
41 class PrivateFontFile : public ppapi::Resource { 43 class PrivateFontFile : public ppapi::Resource {
42 public: 44 public:
43 PrivateFontFile(PP_Instance instance, int fd) 45 PrivateFontFile(PP_Instance instance, int fd)
44 : Resource(ppapi::OBJECT_IS_IMPL, instance), fd_(fd) {} 46 : Resource(ppapi::OBJECT_IS_IMPL, instance), fd_(fd) {}
45 47
46 bool GetFontTable(uint32_t table, void* output, uint32_t* output_length) { 48 bool GetFontTable(uint32_t table, void* output, uint32_t* output_length) {
47 size_t temp_size = static_cast<size_t>(*output_length); 49 size_t temp_size = static_cast<size_t>(*output_length);
48 bool rv = content::GetFontTable( 50 bool rv = content::GetFontTable(fd_.get(),
49 fd_.get(), table, 0 /* offset */, static_cast<uint8_t*>(output), 51 table,
50 &temp_size); 52 0 /* offset */,
53 static_cast<uint8_t*>(output),
54 &temp_size);
51 *output_length = base::checked_cast<uint32_t>(temp_size); 55 *output_length = base::checked_cast<uint32_t>(temp_size);
52 return rv; 56 return rv;
53 } 57 }
54 58
55 protected: 59 protected:
56 virtual ~PrivateFontFile() {} 60 virtual ~PrivateFontFile() {}
57 61
58 private: 62 private:
59 base::ScopedFD fd_; 63 base::ScopedFD fd_;
60 }; 64 };
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_4, IDR_PDF_PROGRESS_BAR_4}, 114 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_4, IDR_PDF_PROGRESS_BAR_4},
111 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_5, IDR_PDF_PROGRESS_BAR_5}, 115 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_5, IDR_PDF_PROGRESS_BAR_5},
112 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_6, IDR_PDF_PROGRESS_BAR_6}, 116 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_6, IDR_PDF_PROGRESS_BAR_6},
113 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_7, IDR_PDF_PROGRESS_BAR_7}, 117 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_7, IDR_PDF_PROGRESS_BAR_7},
114 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_8, IDR_PDF_PROGRESS_BAR_8}, 118 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_8, IDR_PDF_PROGRESS_BAR_8},
115 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_BACKGROUND, 119 {PP_RESOURCEIMAGE_PDF_PROGRESS_BAR_BACKGROUND,
116 IDR_PDF_PROGRESS_BAR_BACKGROUND}, 120 IDR_PDF_PROGRESS_BAR_BACKGROUND},
117 {PP_RESOURCEIMAGE_PDF_PAGE_INDICATOR_BACKGROUND, 121 {PP_RESOURCEIMAGE_PDF_PAGE_INDICATOR_BACKGROUND,
118 IDR_PDF_PAGE_INDICATOR_BACKGROUND}, 122 IDR_PDF_PAGE_INDICATOR_BACKGROUND},
119 {PP_RESOURCEIMAGE_PDF_PAGE_DROPSHADOW, IDR_PDF_PAGE_DROPSHADOW}, 123 {PP_RESOURCEIMAGE_PDF_PAGE_DROPSHADOW, IDR_PDF_PAGE_DROPSHADOW},
120 {PP_RESOURCEIMAGE_PDF_PAN_SCROLL_ICON, IDR_PAN_SCROLL_ICON}, }; 124 {PP_RESOURCEIMAGE_PDF_PAN_SCROLL_ICON, IDR_PAN_SCROLL_ICON},
121 125 };
122 #if defined(ENABLE_FULL_PRINTING)
123
124 blink::WebElement GetWebElement(PP_Instance instance_id) {
125 content::PepperPluginInstance* instance =
126 content::PepperPluginInstance::Get(instance_id);
127 if (!instance)
128 return blink::WebElement();
129 return instance->GetContainer()->element();
130 }
131
132 printing::PrintWebViewHelper* GetPrintWebViewHelper(
133 const blink::WebElement& element) {
134 if (element.isNull())
135 return NULL;
136 blink::WebView* view = element.document().frame()->view();
137 content::RenderView* render_view = content::RenderView::FromWebView(view);
138 return printing::PrintWebViewHelper::Get(render_view);
139 }
140
141 bool IsPrintingEnabled(PP_Instance instance_id) {
142 blink::WebElement element = GetWebElement(instance_id);
143 printing::PrintWebViewHelper* helper = GetPrintWebViewHelper(element);
144 return helper && helper->IsPrintingEnabled();
145 }
146
147 #else // ENABLE_FULL_PRINTING
148
149 bool IsPrintingEnabled(PP_Instance instance_id) { return false; }
150
151 #endif // ENABLE_FULL_PRINTING
152 126
153 PP_Var GetLocalizedString(PP_Instance instance_id, 127 PP_Var GetLocalizedString(PP_Instance instance_id,
154 PP_ResourceString string_id) { 128 PP_ResourceString string_id) {
155 content::PepperPluginInstance* instance = 129 content::PepperPluginInstance* instance =
156 content::PepperPluginInstance::Get(instance_id); 130 content::PepperPluginInstance::Get(instance_id);
157 if (!instance) 131 if (!instance)
158 return PP_MakeUndefined(); 132 return PP_MakeUndefined();
159 133
160 std::string rv; 134 std::string rv;
161 if (string_id == PP_RESOURCESTRING_PDFGETPASSWORD) { 135 if (string_id == PP_RESOURCESTRING_PDFGETPASSWORD) {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 if (!instance) 267 if (!instance)
294 return; 268 return;
295 instance->GetRenderView()->DidStopLoading(); 269 instance->GetRenderView()->DidStopLoading();
296 } 270 }
297 271
298 void SetContentRestriction(PP_Instance instance_id, int restrictions) { 272 void SetContentRestriction(PP_Instance instance_id, int restrictions) {
299 content::PepperPluginInstance* instance = 273 content::PepperPluginInstance* instance =
300 content::PepperPluginInstance::Get(instance_id); 274 content::PepperPluginInstance::Get(instance_id);
301 if (!instance) 275 if (!instance)
302 return; 276 return;
303 instance->GetRenderView()->Send( 277 instance->GetRenderView()->Send(new PDFHostMsg_PDFUpdateContentRestrictions(
304 new ChromeViewHostMsg_PDFUpdateContentRestrictions( 278 instance->GetRenderView()->GetRoutingID(), restrictions));
305 instance->GetRenderView()->GetRoutingID(), restrictions));
306 } 279 }
307 280
308 void HistogramPDFPageCount(PP_Instance instance, int count) { 281 void HistogramPDFPageCount(PP_Instance instance, int count) {
309 UMA_HISTOGRAM_COUNTS_10000("PDF.PageCount", count); 282 UMA_HISTOGRAM_COUNTS_10000("PDF.PageCount", count);
310 } 283 }
311 284
312 void UserMetricsRecordAction(PP_Instance instance, PP_Var action) { 285 void UserMetricsRecordAction(PP_Instance instance, PP_Var action) {
313 scoped_refptr<ppapi::StringVar> action_str( 286 scoped_refptr<ppapi::StringVar> action_str(
314 ppapi::StringVar::FromPPVar(action)); 287 ppapi::StringVar::FromPPVar(action));
315 if (action_str) 288 if (action_str)
316 content::RenderThread::Get()->RecordComputedAction(action_str->value()); 289 content::RenderThread::Get()->RecordComputedAction(action_str->value());
317 } 290 }
318 291
319 void HasUnsupportedFeature(PP_Instance instance_id) { 292 void HasUnsupportedFeature(PP_Instance instance_id) {
320 content::PepperPluginInstance* instance = 293 content::PepperPluginInstance* instance =
321 content::PepperPluginInstance::Get(instance_id); 294 content::PepperPluginInstance::Get(instance_id);
322 if (!instance) 295 if (!instance)
323 return; 296 return;
324 297
325 // Only want to show an info bar if the pdf is the whole tab. 298 // Only want to show an info bar if the pdf is the whole tab.
326 if (!instance->IsFullPagePlugin()) 299 if (!instance->IsFullPagePlugin())
327 return; 300 return;
328 301
329 blink::WebView* view = 302 blink::WebView* view =
330 instance->GetContainer()->element().document().frame()->view(); 303 instance->GetContainer()->element().document().frame()->view();
331 content::RenderView* render_view = content::RenderView::FromWebView(view); 304 content::RenderView* render_view = content::RenderView::FromWebView(view);
332 render_view->Send(new ChromeViewHostMsg_PDFHasUnsupportedFeature( 305 render_view->Send(
333 render_view->GetRoutingID())); 306 new PDFHostMsg_PDFHasUnsupportedFeature(render_view->GetRoutingID()));
334 } 307 }
335 308
336 void SaveAs(PP_Instance instance_id) { 309 void SaveAs(PP_Instance instance_id) {
337 content::PepperPluginInstance* instance = 310 content::PepperPluginInstance* instance =
338 content::PepperPluginInstance::Get(instance_id); 311 content::PepperPluginInstance::Get(instance_id);
339 if (!instance) 312 if (!instance)
340 return; 313 return;
341 GURL url = instance->GetPluginURL(); 314 GURL url = instance->GetPluginURL();
342 315
343 content::RenderView* render_view = instance->GetRenderView(); 316 content::RenderView* render_view = instance->GetRenderView();
344 blink::WebLocalFrame* frame = 317 blink::WebLocalFrame* frame =
345 render_view->GetWebView()->mainFrame()->toWebLocalFrame(); 318 render_view->GetWebView()->mainFrame()->toWebLocalFrame();
346 content::Referrer referrer(frame->document().url(), 319 content::Referrer referrer(frame->document().url(),
347 frame->document().referrerPolicy()); 320 frame->document().referrerPolicy());
348 render_view->Send(new ChromeViewHostMsg_PDFSaveURLAs( 321 render_view->Send(
349 render_view->GetRoutingID(), url, referrer)); 322 new PDFHostMsg_PDFSaveURLAs(render_view->GetRoutingID(), url, referrer));
350 } 323 }
351 324
352 PP_Bool IsFeatureEnabled(PP_Instance instance, PP_PDFFeature feature) { 325 PP_Bool IsFeatureEnabled(PP_Instance instance, PP_PDFFeature feature) {
353 switch (feature) { 326 switch (feature) {
354 case PP_PDFFEATURE_HIDPI: 327 case PP_PDFFEATURE_HIDPI:
355 return PP_TRUE; 328 return PP_TRUE;
356 case PP_PDFFEATURE_PRINTING: 329 case PP_PDFFEATURE_PRINTING:
357 return IsPrintingEnabled(instance) ? PP_TRUE : PP_FALSE; 330 return (print_delegate && print_delegate->IsPrintingEnabled(instance))
331 ? PP_TRUE
332 : PP_FALSE;
358 } 333 }
359 return PP_FALSE; 334 return PP_FALSE;
360 } 335 }
361 336
362 PP_Resource GetResourceImageForScale(PP_Instance instance_id, 337 PP_Resource GetResourceImageForScale(PP_Instance instance_id,
363 PP_ResourceImage image_id, 338 PP_ResourceImage image_id,
364 float scale) { 339 float scale) {
365 int res_id = 0; 340 int res_id = 0;
366 for (size_t i = 0; i < arraysize(kResourceImageMap); ++i) { 341 for (size_t i = 0; i < arraysize(kResourceImageMap); ++i) {
367 if (kResourceImageMap[i].pp_id == image_id) { 342 if (kResourceImageMap[i].pp_id == image_id) {
(...skipping 27 matching lines...) Expand all
395 PP_Var ModalPromptForPassword(PP_Instance instance_id, PP_Var message) { 370 PP_Var ModalPromptForPassword(PP_Instance instance_id, PP_Var message) {
396 content::PepperPluginInstance* instance = 371 content::PepperPluginInstance* instance =
397 content::PepperPluginInstance::Get(instance_id); 372 content::PepperPluginInstance::Get(instance_id);
398 if (!instance) 373 if (!instance)
399 return PP_MakeUndefined(); 374 return PP_MakeUndefined();
400 375
401 std::string actual_value; 376 std::string actual_value;
402 scoped_refptr<ppapi::StringVar> message_string( 377 scoped_refptr<ppapi::StringVar> message_string(
403 ppapi::StringVar::FromPPVar(message)); 378 ppapi::StringVar::FromPPVar(message));
404 379
405 IPC::SyncMessage* msg = new ChromeViewHostMsg_PDFModalPromptForPassword( 380 IPC::SyncMessage* msg = new PDFHostMsg_PDFModalPromptForPassword(
406 instance->GetRenderView()->GetRoutingID(), 381 instance->GetRenderView()->GetRoutingID(),
407 message_string->value(), 382 message_string->value(),
408 &actual_value); 383 &actual_value);
409 msg->EnableMessagePumping(); 384 msg->EnableMessagePumping();
410 instance->GetRenderView()->Send(msg); 385 instance->GetRenderView()->Send(msg);
411 386
412 return ppapi::StringVar::StringToPPVar(actual_value); 387 return ppapi::StringVar::StringToPPVar(actual_value);
413 } 388 }
414 389
415 PP_Bool IsOutOfProcess(PP_Instance instance_id) { return PP_FALSE; } 390 PP_Bool IsOutOfProcess(PP_Instance instance_id) {
391 return PP_FALSE;
392 }
416 393
417 void SetSelectedText(PP_Instance instance_id, const char* selected_text) { 394 void SetSelectedText(PP_Instance instance_id, const char* selected_text) {
418 // This function is intended for out of process PDF plugin. 395 // This function is intended for out of process PDF plugin.
419 } 396 }
420 397
421 void SetLinkUnderCursor(PP_Instance instance_id, const char* url) { 398 void SetLinkUnderCursor(PP_Instance instance_id, const char* url) {
422 content::PepperPluginInstance* instance = 399 content::PepperPluginInstance* instance =
423 content::PepperPluginInstance::Get(instance_id); 400 content::PepperPluginInstance::Get(instance_id);
424 if (!instance) 401 if (!instance)
425 return; 402 return;
(...skipping 18 matching lines...) Expand all
444 &GetResourceImageForScale, // 421 &GetResourceImageForScale, //
445 &ModalPromptForPassword, // 422 &ModalPromptForPassword, //
446 &IsOutOfProcess, // 423 &IsOutOfProcess, //
447 &SetSelectedText, // 424 &SetSelectedText, //
448 &SetLinkUnderCursor, // 425 &SetLinkUnderCursor, //
449 }; 426 };
450 427
451 } // namespace 428 } // namespace
452 429
453 // static 430 // static
454 const PPB_PDF* PPB_PDF_Impl::GetInterface() { return &ppb_pdf; } 431 const PPB_PDF* PPB_PDF_Impl::GetInterface() {
432 return &ppb_pdf;
433 }
455 434
456 // static 435 // static
457 void PPB_PDF_Impl::InvokePrintingForInstance(PP_Instance instance_id) { 436 void PPB_PDF_Impl::InvokePrintingForInstance(PP_Instance instance_id) {
458 #if defined(ENABLE_FULL_PRINTING) 437 if (print_delegate)
459 blink::WebElement element = GetWebElement(instance_id); 438 print_delegate->Print(instance_id);
460 printing::PrintWebViewHelper* helper = GetPrintWebViewHelper(element);
461 if (helper)
462 helper->PrintNode(element);
463 #endif // ENABLE_FULL_PRINTING
464 } 439 }
440
441 void PPB_PDF_Impl::SetPrintDelegate(PPB_PDF_Impl::PrintDelegate* delegate) {
442 CHECK(!print_delegate) << "There should only be a single PrintDelegate.";
443 print_delegate = delegate;
444 }
445
446 } // namespace pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698