Browse Source

chore: backport printing changes (#15023, #15143) to 4-0-x (#15976)

* refactor: move printing out of chromium_src (#15023)

* remove printing related things from chromium_src

* chore: add printing build flag and patch

* fix: include PrintingService on other platforms too

* fix: printing_handler is only needed on Windows

* fix: format BUILD.gn properly

* fix: rename printing build flag to avoid conflict with chromium

* fix: place previously missed printing calls behind build flag

* fix: accidentally renamed flag in patch file

* fix: don't include all printing strings

* fix: allow ShowItemInFolder and OpenItem to block, fixing a DCHECK crash

* fix: make things compile, some changes got lost while rebasing

* fix: remove rogue line from BUILD.gn

* chore: update patch description

* style: lint fix

* chore: use chromium printing buildflag, move node related stuff out of patch

* revert: remove ScopedAllowBlockingForTesting call

* fix: fix my rebase blooper

* fix: re-add header lost during rebase, update patch

* fix: add <map> include, tweak the patch a bit

* revert: remove rogue diff from patch

* fix: clean up after rebase

* refactor: printing implementation (#15143)

* refactor: basic printing

* move build files to chromium_src/BUILD.gn
* remove dependency on chrome prerender sources

* spec: move printing specs behind feature flag

* build: register pdf compositor service

* chore: cleanup api usage from //chrome
Ben Gotow 6 years ago
parent
commit
f6fa88ac23
66 changed files with 1551 additions and 7534 deletions
  1. 22 3
      BUILD.gn
  2. 6 1
      atom/browser/api/atom_api_app.cc
  3. 51 50
      atom/browser/api/atom_api_web_contents.cc
  4. 13 10
      atom/browser/api/atom_api_web_contents.h
  5. 21 4
      atom/browser/atom_browser_client.cc
  6. 6 0
      atom/browser/atom_browser_context.cc
  7. 2 2
      atom/browser/atom_browser_main_parts.cc
  8. 11 3
      atom/browser/common_web_contents_delegate.cc
  9. 197 0
      atom/browser/printing/print_preview_message_handler.cc
  10. 74 0
      atom/browser/printing/print_preview_message_handler.h
  11. 6 0
      atom/common/api/features.cc
  12. 0 3
      atom/common/common_message_generator.h
  13. 0 1
      atom/renderer/atom_sandboxed_renderer_client.cc
  14. 37 0
      atom/renderer/printing/print_render_frame_helper_delegate.cc
  15. 31 0
      atom/renderer/printing/print_render_frame_helper_delegate.h
  16. 10 2
      atom/renderer/renderer_client_base.cc
  17. 73 9
      atom/utility/atom_content_utility_client.cc
  18. 9 4
      atom/utility/atom_content_utility_client.h
  19. 2 0
      build/args/all.gn
  20. 44 1
      chromium_src/BUILD.gn
  21. 0 31
      chromium_src/chrome/browser/browser_process.cc
  22. 0 43
      chromium_src/chrome/browser/browser_process.h
  23. 225 0
      chromium_src/chrome/browser/browser_process_impl.cc
  24. 110 0
      chromium_src/chrome/browser/browser_process_impl.h
  25. 0 684
      chromium_src/chrome/browser/printing/pdf_to_emf_converter.cc
  26. 0 43
      chromium_src/chrome/browser/printing/pdf_to_emf_converter.h
  27. 0 468
      chromium_src/chrome/browser/printing/print_job.cc
  28. 0 246
      chromium_src/chrome/browser/printing/print_job.h
  29. 0 151
      chromium_src/chrome/browser/printing/print_job_manager.cc
  30. 0 100
      chromium_src/chrome/browser/printing/print_job_manager.h
  31. 0 558
      chromium_src/chrome/browser/printing/print_job_worker.cc
  32. 0 174
      chromium_src/chrome/browser/printing/print_job_worker.h
  33. 0 27
      chromium_src/chrome/browser/printing/print_job_worker_owner.cc
  34. 0 65
      chromium_src/chrome/browser/printing/print_job_worker_owner.h
  35. 0 142
      chromium_src/chrome/browser/printing/print_preview_message_handler.cc
  36. 0 60
      chromium_src/chrome/browser/printing/print_preview_message_handler.h
  37. 0 508
      chromium_src/chrome/browser/printing/print_view_manager_base.cc
  38. 0 171
      chromium_src/chrome/browser/printing/print_view_manager_base.h
  39. 0 46
      chromium_src/chrome/browser/printing/print_view_manager_basic.cc
  40. 0 57
      chromium_src/chrome/browser/printing/print_view_manager_basic.h
  41. 0 23
      chromium_src/chrome/browser/printing/print_view_manager_observer.h
  42. 0 152
      chromium_src/chrome/browser/printing/printer_query.cc
  43. 0 111
      chromium_src/chrome/browser/printing/printer_query.h
  44. 0 328
      chromium_src/chrome/browser/printing/printing_message_filter.cc
  45. 0 97
      chromium_src/chrome/browser/printing/printing_message_filter.h
  46. 0 19
      chromium_src/chrome/browser/printing/printing_ui_web_contents_observer.cc
  47. 0 24
      chromium_src/chrome/browser/printing/printing_ui_web_contents_observer.h
  48. 0 113
      chromium_src/chrome/common/chrome_utility_printing_messages.h
  49. 0 66
      chromium_src/chrome/common/print_messages.cc
  50. 0 314
      chromium_src/chrome/common/print_messages.h
  51. 0 1425
      chromium_src/chrome/renderer/printing/print_web_view_helper.cc
  52. 0 396
      chromium_src/chrome/renderer/printing/print_web_view_helper.h
  53. 0 142
      chromium_src/chrome/renderer/printing/print_web_view_helper_linux.cc
  54. 0 127
      chromium_src/chrome/renderer/printing/print_web_view_helper_mac.mm
  55. 0 207
      chromium_src/chrome/renderer/printing/print_web_view_helper_pdf_win.cc
  56. 0 176
      chromium_src/chrome/utility/printing_handler_win.cc
  57. 0 52
      chromium_src/chrome/utility/printing_handler_win.h
  58. 0 21
      chromium_src/chrome/utility/utility_message_handler.h
  59. 16 0
      electron_strings.grdp
  60. 4 35
      filenames.gni
  61. 25 1
      lib/browser/api/web-contents.js
  62. 3 1
      manifests/electron_content_browser_manifest_overlay.json
  63. 1 0
      patches/common/chromium/.patches
  64. 494 0
      patches/common/chromium/printing.patch
  65. 0 37
      spec/api-browser-window-spec.js
  66. 58 0
      spec/api-web-contents-spec.js

+ 22 - 3
BUILD.gn

@@ -207,11 +207,11 @@ static_library("electron_lib") {
     "native_mate",
     "//base",
     "//base:i18n",
+    "//chrome/app/resources:platform_locale_settings",
     "//components/certificate_transparency",
     "//components/net_log",
     "//components/network_session_configurator/common",
     "//components/prefs",
-    "//components/printing/common",
     "//components/viz/service",
     "//content/public/app:both",
     "//content/public/child",
@@ -222,7 +222,6 @@ static_library("electron_lib") {
     "//ppapi/host",
     "//ppapi/proxy",
     "//ppapi/shared_impl",
-    "//printing",
     "//services/proxy_resolver:lib",
     "//skia",
     "//third_party/blink/public:blink",
@@ -367,6 +366,7 @@ static_library("electron_lib") {
       # Disable warnings for g_settings_list_schemas.
       "GLIB_DISABLE_DEPRECATION_WARNINGS",
     ]
+
     sources += filenames.lib_sources_nss
   }
   if (is_win) {
@@ -437,6 +437,15 @@ static_library("electron_lib") {
     ]
   }
 
+  if (enable_basic_printing) {
+    sources += [
+      "atom/browser/printing/print_preview_message_handler.cc",
+      "atom/browser/printing/print_preview_message_handler.h",
+      "atom/renderer/printing/print_render_frame_helper_delegate.cc",
+      "atom/renderer/printing/print_render_frame_helper_delegate.h",
+    ]
+  }
+
   if (enable_pepper_flash) {
     deps += [ "components/pepper_flash" ]
   }
@@ -831,7 +840,6 @@ group("chromium_unittests") {
     "//mojo:mojo_unittests",
     "//net:net_unittests",
     "//ppapi:ppapi_unittests",
-    "//printing:printing_unittests",
     "//skia:skia_unittests",
     "//sql:sql_unittests",
     "//storage:storage_unittests",
@@ -849,6 +857,10 @@ group("chromium_unittests") {
     "//v8/test/unittests:unittests",
   ]
 
+  if (enable_basic_printing) {
+    deps += [ "//printing:printing_unittests" ]
+  }
+
   if (is_linux) {
     deps += [
       "//net:disk_cache_memory_test",
@@ -991,6 +1003,13 @@ group("electron_content_manifest_overlays") {
 service_manifest("electron_content_packaged_services_manifest_overlay") {
   source = "//electron/manifests/electron_content_packaged_services_manifest_overlay.json"
   packaged_services = [ "//services/proxy_resolver:proxy_resolver_manifest" ]
+
+  if (enable_basic_printing) {
+    packaged_services += [
+      "//chrome/services/printing:manifest",
+      "//components/services/pdf_compositor:pdf_compositor_manifest",
+    ]
+  }
 }
 
 service_manifest("electron_content_browser_manifest_overlay") {

+ 6 - 1
atom/browser/api/atom_api_app.cc

@@ -23,7 +23,6 @@
 #include "atom/common/native_mate_converters/net_converter.h"
 #include "atom/common/native_mate_converters/network_converter.h"
 #include "atom/common/native_mate_converters/value_converter.h"
-#include "atom/common/node_includes.h"
 #include "atom/common/options_switches.h"
 #include "base/command_line.h"
 #include "base/environment.h"
@@ -54,6 +53,12 @@
 #include "ui/base/l10n/l10n_util.h"
 #include "ui/gfx/image/image.h"
 
+// clang-format off
+// This header should be declared at the end to avoid
+// redefinition errors.
+#include "atom/common/node_includes.h"  // NOLINT(build/include_alpha)
+// clang-format on
+
 #if defined(OS_WIN)
 #include "atom/browser/ui/win/jump_list.h"
 #include "base/strings/utf_string_conversions.h"

+ 51 - 50
atom/browser/api/atom_api_web_contents.cc

@@ -52,8 +52,6 @@
 #include "brightray/browser/inspectable_web_contents.h"
 #include "brightray/browser/inspectable_web_contents_view.h"
 #include "chrome/browser/browser_process.h"
-#include "chrome/browser/printing/print_preview_message_handler.h"
-#include "chrome/browser/printing/print_view_manager_basic.h"
 #include "chrome/browser/ssl/security_state_tab_helper.h"
 #include "content/browser/frame_host/frame_tree_node.h"
 #include "content/browser/frame_host/render_frame_host_manager.h"
@@ -102,34 +100,16 @@
 #include "ui/gfx/font_render_params.h"
 #endif
 
-#include "atom/common/node_includes.h"
-
-namespace {
+#if BUILDFLAG(ENABLE_PRINTING)
+#include "chrome/browser/printing/print_view_manager_basic.h"
+#include "components/printing/common/print_messages.h"
+#endif
 
-struct PrintSettings {
-  bool silent;
-  bool print_background;
-  base::string16 device_name;
-};
-}  // namespace
+#include "atom/common/node_includes.h"
 
 namespace mate {
 
-template <>
-struct Converter<PrintSettings> {
-  static bool FromV8(v8::Isolate* isolate,
-                     v8::Local<v8::Value> val,
-                     PrintSettings* out) {
-    mate::Dictionary dict;
-    if (!ConvertFromV8(isolate, val, &dict))
-      return false;
-    dict.Get("silent", &(out->silent));
-    dict.Get("printBackground", &(out->print_background));
-    dict.Get("deviceName", &(out->device_name));
-    return true;
-  }
-};
-
+#if BUILDFLAG(ENABLE_PRINTING)
 template <>
 struct Converter<printing::PrinterBasicInfo> {
   static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
@@ -143,6 +123,7 @@ struct Converter<printing::PrinterBasicInfo> {
     return dict.GetHandle();
   }
 };
+#endif
 
 template <>
 struct Converter<WindowOpenDisposition> {
@@ -1472,40 +1453,58 @@ bool WebContents::IsDOMReady() const {
   return is_dom_ready_;
 }
 
+#if BUILDFLAG(ENABLE_PRINTING)
 void WebContents::Print(mate::Arguments* args) {
-  PrintSettings settings = {false, false, base::string16()};
-  if (args->Length() >= 1 && !args->GetNext(&settings)) {
-    args->ThrowError();
+  bool silent, print_background = false;
+  base::string16 device_name;
+  mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
+  base::DictionaryValue settings;
+  if (args->Length() >= 1 && !args->GetNext(&options)) {
+    args->ThrowError("Invalid print settings specified");
     return;
   }
-  auto* print_view_manager_basic_ptr =
-      printing::PrintViewManagerBasic::FromWebContents(web_contents());
-  if (args->Length() == 2) {
-    base::Callback<void(bool)> callback;
-    if (!args->GetNext(&callback)) {
-      args->ThrowError();
-      return;
-    }
-    print_view_manager_basic_ptr->SetCallback(callback);
+  printing::CompletionCallback callback;
+  if (args->Length() == 2 && !args->GetNext(&callback)) {
+    args->ThrowError("Invalid optional callback provided");
+    return;
+  }
+  options.Get("silent", &silent);
+  options.Get("printBackground", &print_background);
+  if (options.Get("deviceName", &device_name) && !device_name.empty()) {
+    settings.SetString(printing::kSettingDeviceName, device_name);
   }
-  print_view_manager_basic_ptr->PrintNow(
-      web_contents()->GetMainFrame(), settings.silent,
-      settings.print_background, settings.device_name);
+  auto* print_view_manager =
+      printing::PrintViewManagerBasic::FromWebContents(web_contents());
+  auto* focused_frame = web_contents()->GetFocusedFrame();
+  auto* rfh = focused_frame && focused_frame->HasSelection()
+                  ? focused_frame
+                  : web_contents()->GetMainFrame();
+  print_view_manager->PrintNow(
+      rfh,
+      std::make_unique<PrintMsg_PrintPages>(rfh->GetRoutingID(), silent,
+                                            print_background, settings),
+      std::move(callback));
 }
 
 std::vector<printing::PrinterBasicInfo> WebContents::GetPrinterList() {
   std::vector<printing::PrinterBasicInfo> printers;
-  auto print_backend = printing::PrintBackend::CreateInstance(nullptr);
-  base::ThreadRestrictions::ScopedAllowIO allow_io;
-  print_backend->EnumeratePrinters(&printers);
+  {
+    // TODO(deepak1556): Deprecate this api in favor of an
+    // async version and post a non blocing task call.
+    base::ThreadRestrictions::ScopedAllowIO allow_io;
+    auto print_backend = printing::PrintBackend::CreateInstance(nullptr);
+    print_backend->EnumeratePrinters(&printers);
+  }
   return printers;
 }
 
-void WebContents::PrintToPDF(const base::DictionaryValue& setting,
-                             const PrintToPDFCallback& callback) {
-  printing::PrintPreviewMessageHandler::FromWebContents(web_contents())
-      ->PrintToPDF(setting, callback);
+void WebContents::PrintToPDF(
+    const base::DictionaryValue& settings,
+    const PrintPreviewMessageHandler::PrintToPDFCallback& callback) {
+  PrintPreviewMessageHandler::FromWebContents(web_contents())
+      ->PrintToPDF(settings, callback);
 }
+#endif
 
 void WebContents::AddWorkSpace(mate::Arguments* args,
                                const base::FilePath& path) {
@@ -2125,9 +2124,11 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("unregisterServiceWorker",
                  &WebContents::UnregisterServiceWorker)
       .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker)
-      .SetMethod("print", &WebContents::Print)
-      .SetMethod("getPrinters", &WebContents::GetPrinterList)
+#if BUILDFLAG(ENABLE_PRINTING)
+      .SetMethod("_print", &WebContents::Print)
+      .SetMethod("_getPrinters", &WebContents::GetPrinterList)
       .SetMethod("_printToPDF", &WebContents::PrintToPDF)
+#endif
       .SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
       .SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
       .SetMethod("showDefinitionForSelection",

+ 13 - 10
atom/browser/api/atom_api_web_contents.h

@@ -22,9 +22,14 @@
 #include "content/public/common/favicon_url.h"
 #include "electron/buildflags/buildflags.h"
 #include "native_mate/handle.h"
-#include "printing/backend/print_backend.h"
+#include "printing/buildflags/buildflags.h"
 #include "ui/gfx/image/image.h"
 
+#if BUILDFLAG(ENABLE_PRINTING)
+#include "atom/browser/printing/print_preview_message_handler.h"
+#include "printing/backend/print_backend.h"
+#endif
+
 namespace blink {
 struct WebDeviceEmulationParams;
 }
@@ -78,10 +83,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
     OFF_SCREEN,       // Used for offscreen rendering
   };
 
-  // For node.js callback function type: function(error, buffer)
-  using PrintToPDFCallback =
-      base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>;
-
   // Create from an existing WebContents.
   static mate::Handle<WebContents> CreateFrom(
       v8::Isolate* isolate,
@@ -155,15 +156,18 @@ class WebContents : public mate::TrackableObject<WebContents>,
   bool IsAudioMuted();
   bool IsCurrentlyAudible();
   bool IsDOMReady() const;
-  void Print(mate::Arguments* args);
-  std::vector<printing::PrinterBasicInfo> GetPrinterList();
   void SetEmbedder(const WebContents* embedder);
   void SetDevToolsWebContents(const WebContents* devtools);
   v8::Local<v8::Value> GetNativeView() const;
 
+#if BUILDFLAG(ENABLE_PRINTING)
+  void Print(mate::Arguments* args);
+  std::vector<printing::PrinterBasicInfo> GetPrinterList();
   // Print current page as PDF.
-  void PrintToPDF(const base::DictionaryValue& setting,
-                  const PrintToPDFCallback& callback);
+  void PrintToPDF(
+      const base::DictionaryValue& settings,
+      const PrintPreviewMessageHandler::PrintToPDFCallback& callback);
+#endif
 
   // DevTools workspace api.
   void AddWorkSpace(mate::Arguments* args, const base::FilePath& path);
@@ -485,7 +489,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
 
   std::unique_ptr<AtomJavaScriptDialogManager> dialog_manager_;
   std::unique_ptr<WebViewGuestDelegate> guest_delegate_;
-
   std::unique_ptr<FrameSubscriber> frame_subscriber_;
 
   // The host webcontents that may contain this webcontents.

+ 21 - 4
atom/browser/atom_browser_client.cc

@@ -39,8 +39,6 @@
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
-#include "chrome/browser/printing/printing_message_filter.h"
-#include "chrome/browser/speech/tts_message_filter.h"
 #include "components/net_log/chrome_net_log.h"
 #include "content/public/browser/browser_ppapi_host.h"
 #include "content/public/browser/client_certificate_delegate.h"
@@ -60,6 +58,7 @@
 #include "net/base/escape.h"
 #include "net/ssl/ssl_cert_request_info.h"
 #include "ppapi/host/ppapi_host.h"
+#include "printing/buildflags/buildflags.h"
 #include "services/device/public/cpp/geolocation/location_provider.h"
 #include "services/network/public/cpp/resource_request_body.h"
 #include "services/proxy_resolver/public/mojom/proxy_resolver.mojom.h"
@@ -85,6 +84,12 @@
 #include "atom/browser/fake_location_provider.h"
 #endif  // BUILDFLAG(OVERRIDE_LOCATION_PROVIDER)
 
+#if BUILDFLAG(ENABLE_PRINTING)
+#include "chrome/browser/printing/printing_message_filter.h"
+#include "chrome/services/printing/public/mojom/constants.mojom.h"
+#include "components/services/pdf_compositor/public/interfaces/pdf_compositor.mojom.h"
+#endif  // BUILDFLAG(ENABLE_PRINTING)
+
 using content::BrowserThread;
 
 namespace atom {
@@ -212,8 +217,10 @@ void AtomBrowserClient::RenderProcessWillLaunch(
   if (IsProcessObserved(process_id))
     return;
 
-  host->AddFilter(new printing::PrintingMessageFilter(process_id));
-  host->AddFilter(new TtsMessageFilter(process_id, host->GetBrowserContext()));
+#if BUILDFLAG(ENABLE_PRINTING)
+  host->AddFilter(new printing::PrintingMessageFilter(
+      process_id, host->GetBrowserContext()));
+#endif
 
   ProcessPreferences prefs;
   auto* web_preferences =
@@ -537,6 +544,16 @@ void AtomBrowserClient::RegisterOutOfProcessServices(
   (*services)[proxy_resolver::mojom::kProxyResolverServiceName] =
       base::BindRepeating(&l10n_util::GetStringUTF16,
                           IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME);
+
+#if BUILDFLAG(ENABLE_PRINTING)
+  (*services)[printing::mojom::kServiceName] =
+      base::BindRepeating(&l10n_util::GetStringUTF16,
+                          IDS_UTILITY_PROCESS_PDF_COMPOSITOR_SERVICE_NAME);
+
+  (*services)[printing::mojom::kChromePrintingServiceName] =
+      base::BindRepeating(&l10n_util::GetStringUTF16,
+                          IDS_UTILITY_PROCESS_PRINTING_SERVICE_NAME);
+#endif
 }
 
 std::unique_ptr<base::Value> AtomBrowserClient::GetServiceManifestOverlay(

+ 6 - 0
atom/browser/atom_browser_context.cc

@@ -32,6 +32,7 @@
 #include "brightray/common/application_info.h"
 #include "chrome/common/chrome_paths.h"
 #include "chrome/common/pref_names.h"
+#include "components/keyed_service/content/browser_context_dependency_manager.h"
 #include "components/prefs/json_pref_store.h"
 #include "components/prefs/pref_registry_simple.h"
 #include "components/prefs/pref_service.h"
@@ -119,6 +120,8 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
   proxy_config_monitor_ = std::make_unique<ProxyConfigMonitor>(prefs_.get());
   io_handle_ = new URLRequestContextGetter::Handle(weak_factory_.GetWeakPtr());
   cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);
+
+  BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);
 }
 
 AtomBrowserContext::~AtomBrowserContext() {
@@ -126,6 +129,9 @@ AtomBrowserContext::~AtomBrowserContext() {
   NotifyWillBeDestroyed(this);
   ShutdownStoragePartitions();
   io_handle_->ShutdownOnUIThread();
+  // Notify any keyed services of browser context destruction.
+  BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
+      this);
 }
 
 void AtomBrowserContext::InitPrefs() {

+ 2 - 2
atom/browser/atom_browser_main_parts.cc

@@ -19,7 +19,7 @@
 #include "atom/common/node_bindings.h"
 #include "base/command_line.h"
 #include "base/threading/thread_task_runner_handle.h"
-#include "chrome/browser/browser_process.h"
+#include "chrome/browser/browser_process_impl.h"
 #include "chrome/browser/icon_manager.h"
 #include "chrome/browser/net/chrome_net_log_helper.h"
 #include "components/net_log/chrome_net_log.h"
@@ -68,7 +68,7 @@ AtomBrowserMainParts* AtomBrowserMainParts::self_ = nullptr;
 
 AtomBrowserMainParts::AtomBrowserMainParts(
     const content::MainFunctionParams& params)
-    : fake_browser_process_(new BrowserProcess),
+    : fake_browser_process_(new BrowserProcessImpl),
       browser_(new Browser),
       node_bindings_(NodeBindings::Create(NodeBindings::BROWSER)),
       atom_bindings_(new AtomBindings(uv_default_loop())),

+ 11 - 3
atom/browser/common_web_contents_delegate.cc

@@ -21,8 +21,6 @@
 #include "base/json/json_reader.h"
 #include "base/task_scheduler/post_task.h"
 #include "base/threading/sequenced_task_runner_handle.h"
-#include "chrome/browser/printing/print_preview_message_handler.h"
-#include "chrome/browser/printing/print_view_manager_basic.h"
 #include "chrome/browser/ssl/security_state_tab_helper.h"
 #include "chrome/browser/ui/browser_dialogs.h"
 #include "chrome/common/pref_names.h"
@@ -38,12 +36,19 @@
 #include "content/public/browser/render_widget_host.h"
 #include "content/public/browser/security_style_explanation.h"
 #include "content/public/browser/security_style_explanations.h"
+#include "printing/buildflags/buildflags.h"
 #include "storage/browser/fileapi/isolated_context.h"
 
 #if BUILDFLAG(ENABLE_OSR)
 #include "atom/browser/osr/osr_web_contents_view.h"
 #endif
 
+#if BUILDFLAG(ENABLE_PRINTING)
+#include "atom/browser/printing/print_preview_message_handler.h"
+#include "chrome/browser/printing/print_view_manager_basic.h"
+#include "components/printing/browser/print_manager_utils.h"
+#endif
+
 using content::BrowserThread;
 
 namespace atom {
@@ -173,8 +178,11 @@ void CommonWebContentsDelegate::InitWithWebContents(
   browser_context_ = browser_context;
   web_contents->SetDelegate(this);
 
+#if BUILDFLAG(ENABLE_PRINTING)
+  PrintPreviewMessageHandler::CreateForWebContents(web_contents);
   printing::PrintViewManagerBasic::CreateForWebContents(web_contents);
-  printing::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
+  printing::CreateCompositeClientIfNeeded(web_contents);
+#endif
 
   // Determien whether the WebContents is offscreen.
   auto* web_preferences = WebContentsPreferences::From(web_contents);

+ 197 - 0
atom/browser/printing/print_preview_message_handler.cc

@@ -0,0 +1,197 @@
+// Copyright (c) 2018 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/browser/printing/print_preview_message_handler.h"
+
+#include <memory>
+#include <utility>
+
+#include "base/bind.h"
+#include "base/memory/read_only_shared_memory_region.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/ref_counted_memory.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/printing/print_job_manager.h"
+#include "chrome/browser/printing/printer_query.h"
+#include "components/printing/browser/print_composite_client.h"
+#include "components/printing/browser/print_manager_utils.h"
+#include "components/printing/common/print_messages.h"
+#include "components/services/pdf_compositor/public/cpp/pdf_service_mojo_types.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/render_frame_host.h"
+#include "content/public/browser/web_contents.h"
+
+#include "atom/common/node_includes.h"
+
+using content::BrowserThread;
+
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::PrintPreviewMessageHandler);
+
+namespace atom {
+
+namespace {
+
+void StopWorker(int document_cookie) {
+  if (document_cookie <= 0)
+    return;
+  scoped_refptr<printing::PrintQueriesQueue> queue =
+      g_browser_process->print_job_manager()->queue();
+  scoped_refptr<printing::PrinterQuery> printer_query =
+      queue->PopPrinterQuery(document_cookie);
+  if (printer_query.get()) {
+    BrowserThread::PostTask(
+        BrowserThread::IO, FROM_HERE,
+        base::BindOnce(&printing::PrinterQuery::StopWorker, printer_query));
+  }
+}
+
+scoped_refptr<base::RefCountedMemory> GetDataFromHandle(
+    base::SharedMemoryHandle handle,
+    uint32_t data_size) {
+  auto shared_buf = std::make_unique<base::SharedMemory>(handle, true);
+  if (!shared_buf->Map(data_size)) {
+    return nullptr;
+  }
+
+  return base::MakeRefCounted<base::RefCountedSharedMemory>(
+      std::move(shared_buf), data_size);
+}
+
+}  // namespace
+
+PrintPreviewMessageHandler::PrintPreviewMessageHandler(
+    content::WebContents* web_contents)
+    : content::WebContentsObserver(web_contents), weak_ptr_factory_(this) {
+  DCHECK(web_contents);
+}
+
+PrintPreviewMessageHandler::~PrintPreviewMessageHandler() = default;
+
+bool PrintPreviewMessageHandler::OnMessageReceived(
+    const IPC::Message& message,
+    content::RenderFrameHost* render_frame_host) {
+  bool handled = true;
+  IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintPreviewMessageHandler, message,
+                                   render_frame_host)
+    IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting,
+                        OnMetafileReadyForPrinting)
+    IPC_MESSAGE_UNHANDLED(handled = false)
+  IPC_END_MESSAGE_MAP()
+  if (handled)
+    return true;
+
+  handled = true;
+  IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message)
+    IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, OnPrintPreviewFailed)
+    IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled,
+                        OnPrintPreviewCancelled)
+    IPC_MESSAGE_UNHANDLED(handled = false)
+  IPC_END_MESSAGE_MAP()
+  return handled;
+}
+
+void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
+    content::RenderFrameHost* render_frame_host,
+    const PrintHostMsg_DidPreviewDocument_Params& params,
+    const PrintHostMsg_PreviewIds& ids) {
+  // Always try to stop the worker.
+  StopWorker(params.document_cookie);
+
+  const PrintHostMsg_DidPrintContent_Params& content = params.content;
+  if (!content.metafile_data_handle.IsValid() ||
+      params.expected_pages_count <= 0) {
+    RunPrintToPDFCallback(ids.request_id, nullptr);
+    return;
+  }
+
+  if (printing::IsOopifEnabled()) {
+    auto* client =
+        printing::PrintCompositeClient::FromWebContents(web_contents());
+    DCHECK(client);
+    client->DoCompositeDocumentToPdf(
+        params.document_cookie, render_frame_host, content.metafile_data_handle,
+        content.data_size, content.subframe_content_info,
+        base::BindOnce(&PrintPreviewMessageHandler::OnCompositePdfDocumentDone,
+                       weak_ptr_factory_.GetWeakPtr(),
+                       params.expected_pages_count, ids));
+  } else {
+    RunPrintToPDFCallback(
+        ids.request_id,
+        GetDataFromHandle(content.metafile_data_handle, content.data_size));
+  }
+}
+
+void PrintPreviewMessageHandler::OnCompositePdfDocumentDone(
+    int page_number,
+    const PrintHostMsg_PreviewIds& ids,
+    printing::mojom::PdfCompositor::Status status,
+    base::ReadOnlySharedMemoryRegion region) {
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
+
+  if (status != printing::mojom::PdfCompositor::Status::SUCCESS) {
+    DLOG(ERROR) << "Compositing pdf failed with error " << status;
+    RunPrintToPDFCallback(ids.request_id, nullptr);
+    return;
+  }
+
+  RunPrintToPDFCallback(
+      ids.request_id,
+      base::RefCountedSharedMemoryMapping::CreateFromWholeRegion(region));
+}
+
+void PrintPreviewMessageHandler::OnPrintPreviewFailed(
+    int document_cookie,
+    const PrintHostMsg_PreviewIds& ids) {
+  StopWorker(document_cookie);
+
+  RunPrintToPDFCallback(ids.request_id, nullptr);
+}
+
+void PrintPreviewMessageHandler::OnPrintPreviewCancelled(
+    int document_cookie,
+    const PrintHostMsg_PreviewIds& ids) {
+  StopWorker(document_cookie);
+
+  RunPrintToPDFCallback(ids.request_id, nullptr);
+}
+
+void PrintPreviewMessageHandler::PrintToPDF(
+    const base::DictionaryValue& options,
+    const PrintToPDFCallback& callback) {
+  int request_id;
+  options.GetInteger(printing::kPreviewRequestID, &request_id);
+  print_to_pdf_callback_map_[request_id] = callback;
+
+  auto* focused_frame = web_contents()->GetFocusedFrame();
+  auto* rfh = focused_frame && focused_frame->HasSelection()
+                  ? focused_frame
+                  : web_contents()->GetMainFrame();
+  rfh->Send(new PrintMsg_PrintPreview(rfh->GetRoutingID(), options));
+}
+
+void PrintPreviewMessageHandler::RunPrintToPDFCallback(
+    int request_id,
+    scoped_refptr<base::RefCountedMemory> data_bytes) {
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
+
+  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+  v8::Locker locker(isolate);
+  v8::HandleScope handle_scope(isolate);
+  if (data_bytes && data_bytes->size()) {
+    v8::Local<v8::Value> buffer =
+        node::Buffer::Copy(isolate,
+                           reinterpret_cast<const char*>(data_bytes->front()),
+                           data_bytes->size())
+            .ToLocalChecked();
+    print_to_pdf_callback_map_[request_id].Run(v8::Null(isolate), buffer);
+  } else {
+    v8::Local<v8::String> error_message =
+        v8::String::NewFromUtf8(isolate, "Failed to generate PDF");
+    print_to_pdf_callback_map_[request_id].Run(
+        v8::Exception::Error(error_message), v8::Null(isolate));
+  }
+  print_to_pdf_callback_map_.erase(request_id);
+}
+
+}  // namespace atom

+ 74 - 0
atom/browser/printing/print_preview_message_handler.h

@@ -0,0 +1,74 @@
+// Copyright (c) 2018 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
+#define ATOM_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
+
+#include <map>
+
+#include "base/memory/ref_counted_memory.h"
+#include "base/memory/weak_ptr.h"
+#include "components/services/pdf_compositor/public/interfaces/pdf_compositor.mojom.h"
+#include "content/public/browser/web_contents_observer.h"
+#include "content/public/browser/web_contents_user_data.h"
+#include "v8/include/v8.h"
+
+struct PrintHostMsg_DidPreviewDocument_Params;
+struct PrintHostMsg_PreviewIds;
+
+namespace content {
+class RenderFrameHost;
+}
+
+namespace atom {
+
+// Manages the print preview handling for a WebContents.
+class PrintPreviewMessageHandler
+    : public content::WebContentsObserver,
+      public content::WebContentsUserData<PrintPreviewMessageHandler> {
+ public:
+  using PrintToPDFCallback =
+      base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>;
+
+  ~PrintPreviewMessageHandler() override;
+
+  void PrintToPDF(const base::DictionaryValue& options,
+                  const PrintToPDFCallback& callback);
+
+ protected:
+  // content::WebContentsObserver implementation.
+  bool OnMessageReceived(const IPC::Message& message,
+                         content::RenderFrameHost* render_frame_host) override;
+
+ private:
+  friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
+
+  explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
+
+  void OnMetafileReadyForPrinting(
+      content::RenderFrameHost* render_frame_host,
+      const PrintHostMsg_DidPreviewDocument_Params& params,
+      const PrintHostMsg_PreviewIds& ids);
+  void OnCompositePdfDocumentDone(int page_number,
+                                  const PrintHostMsg_PreviewIds& ids,
+                                  printing::mojom::PdfCompositor::Status status,
+                                  base::ReadOnlySharedMemoryRegion region);
+  void OnPrintPreviewFailed(int document_cookie,
+                            const PrintHostMsg_PreviewIds& ids);
+  void OnPrintPreviewCancelled(int document_cookie,
+                               const PrintHostMsg_PreviewIds& ids);
+  void RunPrintToPDFCallback(int request_id,
+                             scoped_refptr<base::RefCountedMemory> data_bytes);
+
+  using PrintToPDFCallbackMap = std::map<int, PrintToPDFCallback>;
+  PrintToPDFCallbackMap print_to_pdf_callback_map_;
+
+  base::WeakPtrFactory<PrintPreviewMessageHandler> weak_ptr_factory_;
+
+  DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
+};
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_

+ 6 - 0
atom/common/api/features.cc

@@ -4,6 +4,7 @@
 
 #include "electron/buildflags/buildflags.h"
 #include "native_mate/dictionary.h"
+#include "printing/buildflags/buildflags.h"
 // clang-format off
 #include "atom/common/node_includes.h"  // NOLINT(build/include_alpha)
 // clang-format on
@@ -30,6 +31,10 @@ bool IsViewApiEnabled() {
   return BUILDFLAG(ENABLE_VIEW_API);
 }
 
+bool IsPrintingEnabled() {
+  return BUILDFLAG(ENABLE_PRINTING);
+}
+
 void Initialize(v8::Local<v8::Object> exports,
                 v8::Local<v8::Value> unused,
                 v8::Local<v8::Context> context,
@@ -41,6 +46,7 @@ void Initialize(v8::Local<v8::Object> exports,
   dict.SetMethod("isFakeLocationProviderEnabled",
                  &IsFakeLocationProviderEnabled);
   dict.SetMethod("isViewApiEnabled", &IsViewApiEnabled);
+  dict.SetMethod("isPrintingEnabled", &IsPrintingEnabled);
 }
 
 }  // namespace

+ 0 - 3
atom/common/common_message_generator.h

@@ -5,6 +5,3 @@
 // Multiply-included file, no traditional include guard.
 
 #include "atom/common/api/api_messages.h"
-#include "chrome/common/chrome_utility_printing_messages.h"
-#include "chrome/common/print_messages.h"
-#include "chrome/common/tts_messages.h"

+ 0 - 1
atom/renderer/atom_sandboxed_renderer_client.cc

@@ -18,7 +18,6 @@
 #include "base/path_service.h"
 #include "base/process/process_handle.h"
 #include "brightray/common/application_info.h"
-#include "chrome/renderer/printing/print_web_view_helper.h"
 #include "content/public/renderer/render_frame.h"
 #include "native_mate/dictionary.h"
 #include "third_party/blink/public/web/blink.h"

+ 37 - 0
atom/renderer/printing/print_render_frame_helper_delegate.cc

@@ -0,0 +1,37 @@
+// Copyright (c) 2018 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/renderer/printing/print_render_frame_helper_delegate.h"
+
+#include "content/public/renderer/render_frame.h"
+#include "third_party/blink/public/web/web_element.h"
+#include "third_party/blink/public/web/web_local_frame.h"
+
+namespace atom {
+
+PrintRenderFrameHelperDelegate::PrintRenderFrameHelperDelegate() = default;
+
+PrintRenderFrameHelperDelegate::~PrintRenderFrameHelperDelegate() = default;
+
+bool PrintRenderFrameHelperDelegate::CancelPrerender(
+    content::RenderFrame* render_frame) {
+  return false;
+}
+
+// Return the PDF object element if |frame| is the out of process PDF extension.
+blink::WebElement PrintRenderFrameHelperDelegate::GetPdfElement(
+    blink::WebLocalFrame* frame) {
+  return blink::WebElement();
+}
+
+bool PrintRenderFrameHelperDelegate::IsPrintPreviewEnabled() {
+  return false;
+}
+
+bool PrintRenderFrameHelperDelegate::OverridePrint(
+    blink::WebLocalFrame* frame) {
+  return false;
+}
+
+}  // namespace atom

+ 31 - 0
atom/renderer/printing/print_render_frame_helper_delegate.h

@@ -0,0 +1,31 @@
+// Copyright (c) 2018 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_RENDERER_PRINTING_PRINT_RENDER_FRAME_HELPER_DELEGATE_H_
+#define ATOM_RENDERER_PRINTING_PRINT_RENDER_FRAME_HELPER_DELEGATE_H_
+
+#include "base/macros.h"
+#include "components/printing/renderer/print_render_frame_helper.h"
+
+namespace atom {
+
+class PrintRenderFrameHelperDelegate
+    : public printing::PrintRenderFrameHelper::Delegate {
+ public:
+  PrintRenderFrameHelperDelegate();
+  ~PrintRenderFrameHelperDelegate() override;
+
+ private:
+  // printing::PrintRenderFrameHelper::Delegate:
+  bool CancelPrerender(content::RenderFrame* render_frame) override;
+  blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) override;
+  bool IsPrintPreviewEnabled() override;
+  bool OverridePrint(blink::WebLocalFrame* frame) override;
+
+  DISALLOW_COPY_AND_ASSIGN(PrintRenderFrameHelperDelegate);
+};
+
+}  // namespace atom
+
+#endif  // ATOM_RENDERER_PRINTING_PRINT_RENDER_FRAME_HELPER_DELEGATE_H_

+ 10 - 2
atom/renderer/renderer_client_base.cc

@@ -19,7 +19,6 @@
 #include "base/command_line.h"
 #include "base/strings/string_split.h"
 #include "base/strings/stringprintf.h"
-#include "chrome/renderer/printing/print_web_view_helper.h"
 #include "chrome/renderer/tts_dispatcher.h"
 #include "content/public/common/content_constants.h"
 #include "content/public/common/content_switches.h"
@@ -27,6 +26,7 @@
 #include "content/public/renderer/render_view.h"
 #include "electron/buildflags/buildflags.h"
 #include "native_mate/dictionary.h"
+#include "printing/buildflags/buildflags.h"
 #include "third_party/blink/public/web/blink.h"
 #include "third_party/blink/public/web/web_custom_element.h"  // NOLINT(build/include_alpha)
 #include "third_party/blink/public/web/web_frame_widget.h"
@@ -51,6 +51,11 @@
 #include "chrome/renderer/pepper/pepper_helper.h"
 #endif  // BUILDFLAG(ENABLE_PEPPER_FLASH)
 
+#if BUILDFLAG(ENABLE_PRINTING)
+#include "atom/renderer/printing/print_render_frame_helper_delegate.h"
+#include "components/printing/renderer/print_render_frame_helper.h"
+#endif  // BUILDFLAG(ENABLE_PRINTING)
+
 namespace atom {
 
 namespace {
@@ -182,7 +187,10 @@ void RendererClientBase::RenderFrameCreated(
   new PepperHelper(render_frame);
 #endif
   new ContentSettingsObserver(render_frame);
-  new printing::PrintWebViewHelper(render_frame);
+#if BUILDFLAG(ENABLE_PRINTING)
+  new printing::PrintRenderFrameHelper(
+      render_frame, std::make_unique<atom::PrintRenderFrameHelperDelegate>());
+#endif
 
 #if BUILDFLAG(ENABLE_PDF_VIEWER)
   // Allow access to file scheme from pdf viewer.

+ 73 - 9
atom/utility/atom_content_utility_client.cc

@@ -4,30 +4,82 @@
 
 #include "atom/utility/atom_content_utility_client.h"
 
+#include <string>
+#include <utility>
+
+#include "base/command_line.h"
 #include "content/public/child/child_thread.h"
+#include "content/public/common/service_manager_connection.h"
+#include "content/public/common/simple_connection_filter.h"
 #include "services/proxy_resolver/proxy_resolver_service.h"
 #include "services/proxy_resolver/public/mojom/proxy_resolver.mojom.h"
+#include "services/service_manager/sandbox/switches.h"
+
+#if BUILDFLAG(ENABLE_PRINTING)
+#include "chrome/services/printing/printing_service.h"
+#include "chrome/services/printing/public/mojom/constants.mojom.h"
+#include "components/services/pdf_compositor/public/cpp/pdf_compositor_service_factory.h"
+#include "components/services/pdf_compositor/public/interfaces/pdf_compositor.mojom.h"
 
 #if defined(OS_WIN)
-#include "chrome/utility/printing_handler_win.h"
-#endif
+#include "chrome/services/printing/pdf_to_emf_converter_factory.h"
+#include "chrome/utility/printing_handler.h"
+#endif  // defined(OS_WIN)
+
+#endif  // BUILDFLAG(ENABLE_PRINTING)
 
 namespace atom {
 
-AtomContentUtilityClient::AtomContentUtilityClient() {
-#if defined(OS_WIN)
-  handlers_.push_back(std::make_unique<printing::PrintingHandlerWin>());
+AtomContentUtilityClient::AtomContentUtilityClient() : elevated_(false) {
+#if BUILDFLAG(ENABLE_PRINTING) && defined(OS_WIN)
+  printing_handler_ = std::make_unique<printing::PrintingHandler>();
 #endif
 }
 
 AtomContentUtilityClient::~AtomContentUtilityClient() {}
 
-bool AtomContentUtilityClient::OnMessageReceived(const IPC::Message& message) {
+// The guts of this came from the chromium implementation
+// https://cs.chromium.org/chromium/src/chrome/utility/
+// chrome_content_utility_client.cc?sq=package:chromium&dr=CSs&g=0&l=142
+void AtomContentUtilityClient::UtilityThreadStarted() {
 #if defined(OS_WIN)
-  for (const auto& handler : handlers_) {
-    if (handler->OnMessageReceived(message))
-      return true;
+  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
+  elevated_ = command_line->HasSwitch(
+      service_manager::switches::kNoSandboxAndElevatedPrivileges);
+#endif
+
+  content::ServiceManagerConnection* connection =
+      content::ChildThread::Get()->GetServiceManagerConnection();
+
+  // NOTE: Some utility process instances are not connected to the Service
+  // Manager. Nothing left to do in that case.
+  if (!connection)
+    return;
+
+  auto registry = std::make_unique<service_manager::BinderRegistry>();
+  // If our process runs with elevated privileges, only add elevated Mojo
+  // interfaces to the interface registry.
+  if (!elevated_) {
+#if BUILDFLAG(ENABLE_PRINTING) && defined(OS_WIN)
+    // TODO(crbug.com/798782): remove when the Cloud print chrome/service is
+    // removed.
+    registry->AddInterface(
+        base::BindRepeating(printing::PdfToEmfConverterFactory::Create),
+        base::ThreadTaskRunnerHandle::Get());
+#endif
   }
+
+  connection->AddConnectionFilter(
+      std::make_unique<content::SimpleConnectionFilter>(std::move(registry)));
+}
+
+bool AtomContentUtilityClient::OnMessageReceived(const IPC::Message& message) {
+  if (elevated_)
+    return false;
+
+#if BUILDFLAG(ENABLE_PRINTING) && defined(OS_WIN)
+  if (printing_handler_->OnMessageReceived(message))
+    return true;
 #endif
 
   return false;
@@ -41,6 +93,18 @@ void AtomContentUtilityClient::RegisterServices(StaticServiceMap* services) {
       base::BindRepeating(&proxy_resolver::ProxyResolverService::CreateService);
   services->emplace(proxy_resolver::mojom::kProxyResolverServiceName,
                     proxy_resolver_info);
+
+#if BUILDFLAG(ENABLE_PRINTING)
+  service_manager::EmbeddedServiceInfo pdf_compositor_info;
+  pdf_compositor_info.factory =
+      base::BindRepeating(&printing::CreatePdfCompositorService, std::string());
+  services->emplace(printing::mojom::kServiceName, pdf_compositor_info);
+
+  service_manager::EmbeddedServiceInfo printing_info;
+  printing_info.factory =
+      base::BindRepeating(&printing::PrintingService::CreateService);
+  services->emplace(printing::mojom::kChromePrintingServiceName, printing_info);
+#endif
 }
 
 }  // namespace atom

+ 9 - 4
atom/utility/atom_content_utility_client.h

@@ -10,8 +10,11 @@
 
 #include "base/compiler_specific.h"
 #include "content/public/utility/content_utility_client.h"
+#include "printing/buildflags/buildflags.h"
 
-class UtilityMessageHandler;
+#if BUILDFLAG(ENABLE_PRINTING) && defined(OS_WIN)
+#include "chrome/utility/printing_handler.h"
+#endif
 
 namespace atom {
 
@@ -20,15 +23,17 @@ class AtomContentUtilityClient : public content::ContentUtilityClient {
   AtomContentUtilityClient();
   ~AtomContentUtilityClient() override;
 
+  void UtilityThreadStarted() override;
   bool OnMessageReceived(const IPC::Message& message) override;
   void RegisterServices(StaticServiceMap* services) override;
 
  private:
-#if defined(OS_WIN)
-  typedef std::vector<std::unique_ptr<UtilityMessageHandler>> Handlers;
-  Handlers handlers_;
+#if BUILDFLAG(ENABLE_PRINTING) && defined(OS_WIN)
+  std::unique_ptr<printing::PrintingHandler> printing_handler_;
 #endif
 
+  bool elevated_;
+
   DISALLOW_COPY_AND_ASSIGN(AtomContentUtilityClient);
 };
 

+ 2 - 0
build/args/all.gn

@@ -11,4 +11,6 @@ enable_extensions = false
 proprietary_codecs = true
 ffmpeg_branding = "Chrome"
 
+enable_basic_printing = true
+
 is_cfi = false

+ 44 - 1
chromium_src/BUILD.gn

@@ -3,12 +3,16 @@
 # found in the LICENSE file.
 
 import("//electron/buildflags/buildflags.gni")
+import("//printing/buildflags/buildflags.gni")
+import("//third_party/widevine/cdm/widevine.gni")
 
 # Builds some of the chrome sources that Electron depends
 # on unconditionally.
 source_set("chrome") {
   visibility = [ "//electron:electron_lib" ]
   sources = [
+    "//chrome/browser/browser_process.cc",
+    "//chrome/browser/browser_process.h",
     "//chrome/browser/icon_loader.cc",
     "//chrome/browser/icon_loader.h",
     "//chrome/browser/icon_loader_mac.mm",
@@ -36,7 +40,7 @@ source_set("chrome") {
     "//content/public/browser",
   ]
   deps = [
-    "//chrome/common:constants",
+    "//chrome/common",
     "//components/proxy_config",
     "//components/security_state/content",
   ]
@@ -56,4 +60,43 @@ source_set("chrome") {
     ]
     deps += [ "//ui/snapshot" ]
   }
+
+  if (enable_basic_printing) {
+    sources += [
+      "//chrome/browser/printing/print_job.cc",
+      "//chrome/browser/printing/print_job.h",
+      "//chrome/browser/printing/print_job_manager.cc",
+      "//chrome/browser/printing/print_job_manager.h",
+      "//chrome/browser/printing/print_job_worker.cc",
+      "//chrome/browser/printing/print_job_worker.h",
+      "//chrome/browser/printing/print_view_manager_base.cc",
+      "//chrome/browser/printing/print_view_manager_base.h",
+      "//chrome/browser/printing/print_view_manager_basic.cc",
+      "//chrome/browser/printing/print_view_manager_basic.h",
+      "//chrome/browser/printing/printer_query.cc",
+      "//chrome/browser/printing/printer_query.h",
+      "//chrome/browser/printing/printing_message_filter.cc",
+      "//chrome/browser/printing/printing_message_filter.h",
+    ]
+    deps += [
+      "//chrome/services/printing:lib",
+      "//components/keyed_service/content",
+      "//components/printing/browser",
+      "//components/printing/common",
+      "//components/printing/renderer",
+      "//components/services/pdf_compositor",
+      "//components/services/pdf_compositor/public/cpp:factory",
+      "//components/services/pdf_compositor/public/interfaces",
+      "//printing",
+    ]
+
+    if (is_win) {
+      sources += [
+        "//chrome/browser/printing/pdf_to_emf_converter.cc",
+        "//chrome/browser/printing/pdf_to_emf_converter.h",
+        "//chrome/utility/printing_handler.cc",
+        "//chrome/utility/printing_handler.h",
+      ]
+    }
+  }
 }

+ 0 - 31
chromium_src/chrome/browser/browser_process.cc

@@ -1,31 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/browser_process.h"
-
-#include "chrome/browser/printing/print_job_manager.h"
-#include "ui/base/l10n/l10n_util.h"
-
-BrowserProcess* g_browser_process = NULL;
-
-BrowserProcess::BrowserProcess()
-    : print_job_manager_(new printing::PrintJobManager) {
-  g_browser_process = this;
-}
-
-BrowserProcess::~BrowserProcess() {
-  g_browser_process = NULL;
-}
-
-void BrowserProcess::SetApplicationLocale(const std::string& locale) {
-  locale_ = locale;
-}
-
-std::string BrowserProcess::GetApplicationLocale() {
-  return locale_;
-}
-
-printing::PrintJobManager* BrowserProcess::print_job_manager() {
-  return print_job_manager_.get();
-}

+ 0 - 43
chromium_src/chrome/browser/browser_process.h

@@ -1,43 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// This interface is for managing the global services of the application. Each
-// service is lazily created when requested the first time. The service getters
-// will return NULL if the service is not available, so callers must check for
-// this condition.
-
-#ifndef CHROME_BROWSER_BROWSER_PROCESS_H_
-#define CHROME_BROWSER_BROWSER_PROCESS_H_
-
-#include <memory>
-#include <string>
-
-#include "base/macros.h"
-
-namespace printing {
-class PrintJobManager;
-}
-
-// NOT THREAD SAFE, call only from the main thread.
-// These functions shouldn't return NULL unless otherwise noted.
-class BrowserProcess {
- public:
-  BrowserProcess();
-  ~BrowserProcess();
-
-  void SetApplicationLocale(const std::string& locale);
-  std::string GetApplicationLocale();
-
-  printing::PrintJobManager* print_job_manager();
-
- private:
-  std::unique_ptr<printing::PrintJobManager> print_job_manager_;
-  std::string locale_;
-
-  DISALLOW_COPY_AND_ASSIGN(BrowserProcess);
-};
-
-extern BrowserProcess* g_browser_process;
-
-#endif  // CHROME_BROWSER_BROWSER_PROCESS_H_

+ 225 - 0
chromium_src/chrome/browser/browser_process_impl.cc

@@ -0,0 +1,225 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/browser_process_impl.h"
+
+#include "ui/base/l10n/l10n_util.h"
+
+#if BUILDFLAG(ENABLE_PRINTING)
+#include "chrome/browser/printing/print_job_manager.h"
+#endif
+
+BrowserProcessImpl::BrowserProcessImpl() {
+  g_browser_process = this;
+}
+
+BrowserProcessImpl::~BrowserProcessImpl() {
+  g_browser_process = nullptr;
+}
+
+bool BrowserProcessImpl::IsShuttingDown() {
+  return false;
+}
+
+metrics_services_manager::MetricsServicesManager*
+BrowserProcessImpl::GetMetricsServicesManager() {
+  return nullptr;
+}
+
+metrics::MetricsService* BrowserProcessImpl::metrics_service() {
+  return nullptr;
+}
+
+rappor::RapporServiceImpl* BrowserProcessImpl::rappor_service() {
+  return nullptr;
+}
+
+ProfileManager* BrowserProcessImpl::profile_manager() {
+  return nullptr;
+}
+
+PrefService* BrowserProcessImpl::local_state() {
+  return nullptr;
+}
+
+net::URLRequestContextGetter* BrowserProcessImpl::system_request_context() {
+  return nullptr;
+}
+
+scoped_refptr<network::SharedURLLoaderFactory>
+BrowserProcessImpl::shared_url_loader_factory() {
+  return nullptr;
+}
+
+variations::VariationsService* BrowserProcessImpl::variations_service() {
+  return nullptr;
+}
+
+BrowserProcessPlatformPart* BrowserProcessImpl::platform_part() {
+  return nullptr;
+}
+
+extensions::EventRouterForwarder*
+BrowserProcessImpl::extension_event_router_forwarder() {
+  return nullptr;
+}
+
+NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
+  return nullptr;
+}
+
+NotificationPlatformBridge* BrowserProcessImpl::notification_platform_bridge() {
+  return nullptr;
+}
+
+IOThread* BrowserProcessImpl::io_thread() {
+  return nullptr;
+}
+
+SystemNetworkContextManager*
+BrowserProcessImpl::system_network_context_manager() {
+  return nullptr;
+}
+
+network::NetworkQualityTracker* BrowserProcessImpl::network_quality_tracker() {
+  return nullptr;
+}
+
+WatchDogThread* BrowserProcessImpl::watchdog_thread() {
+  return nullptr;
+}
+
+policy::ChromeBrowserPolicyConnector*
+BrowserProcessImpl::browser_policy_connector() {
+  return nullptr;
+}
+
+policy::PolicyService* BrowserProcessImpl::policy_service() {
+  return nullptr;
+}
+
+IconManager* BrowserProcessImpl::icon_manager() {
+  return nullptr;
+}
+
+GpuModeManager* BrowserProcessImpl::gpu_mode_manager() {
+  return nullptr;
+}
+
+printing::PrintPreviewDialogController*
+BrowserProcessImpl::print_preview_dialog_controller() {
+  return nullptr;
+}
+
+printing::BackgroundPrintingManager*
+BrowserProcessImpl::background_printing_manager() {
+  return nullptr;
+}
+
+IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
+  return nullptr;
+}
+
+DownloadStatusUpdater* BrowserProcessImpl::download_status_updater() {
+  return nullptr;
+}
+
+DownloadRequestLimiter* BrowserProcessImpl::download_request_limiter() {
+  return nullptr;
+}
+
+BackgroundModeManager* BrowserProcessImpl::background_mode_manager() {
+  return nullptr;
+}
+
+StatusTray* BrowserProcessImpl::status_tray() {
+  return nullptr;
+}
+
+safe_browsing::SafeBrowsingService*
+BrowserProcessImpl::safe_browsing_service() {
+  return nullptr;
+}
+
+safe_browsing::ClientSideDetectionService*
+BrowserProcessImpl::safe_browsing_detection_service() {
+  return nullptr;
+}
+
+subresource_filter::ContentRulesetService*
+BrowserProcessImpl::subresource_filter_ruleset_service() {
+  return nullptr;
+}
+
+optimization_guide::OptimizationGuideService*
+BrowserProcessImpl::optimization_guide_service() {
+  return nullptr;
+}
+
+net_log::ChromeNetLog* BrowserProcessImpl::net_log() {
+  return nullptr;
+}
+
+component_updater::ComponentUpdateService*
+BrowserProcessImpl::component_updater() {
+  return nullptr;
+}
+
+component_updater::SupervisedUserWhitelistInstaller*
+BrowserProcessImpl::supervised_user_whitelist_installer() {
+  return nullptr;
+}
+
+MediaFileSystemRegistry* BrowserProcessImpl::media_file_system_registry() {
+  return nullptr;
+}
+
+WebRtcLogUploader* BrowserProcessImpl::webrtc_log_uploader() {
+  return nullptr;
+}
+
+network_time::NetworkTimeTracker* BrowserProcessImpl::network_time_tracker() {
+  return nullptr;
+}
+
+gcm::GCMDriver* BrowserProcessImpl::gcm_driver() {
+  return nullptr;
+}
+
+resource_coordinator::TabManager* BrowserProcessImpl::GetTabManager() {
+  return nullptr;
+}
+
+shell_integration::DefaultWebClientState
+BrowserProcessImpl::CachedDefaultWebClientState() {
+  return shell_integration::UNKNOWN_DEFAULT;
+}
+
+prefs::InProcessPrefServiceFactory* BrowserProcessImpl::pref_service_factory()
+    const {
+  return nullptr;
+}
+
+content::NetworkConnectionTracker*
+BrowserProcessImpl::network_connection_tracker() {
+  return nullptr;
+}
+
+void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
+  locale_ = locale;
+}
+
+const std::string& BrowserProcessImpl::GetApplicationLocale() {
+  return locale_;
+}
+
+printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
+#if BUILDFLAG(ENABLE_PRINTING)
+  if (!print_job_manager_)
+    print_job_manager_.reset(new printing::PrintJobManager());
+  return print_job_manager_.get();
+#else
+  return nullptr;
+#endif
+}

+ 110 - 0
chromium_src/chrome/browser/browser_process_impl.h

@@ -0,0 +1,110 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This interface is for managing the global services of the application. Each
+// service is lazily created when requested the first time. The service getters
+// will return NULL if the service is not available, so callers must check for
+// this condition.
+
+#ifndef CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
+#define CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_
+
+#include <memory>
+#include <string>
+
+#include "base/macros.h"
+#include "chrome/browser/browser_process.h"
+#include "printing/buildflags/buildflags.h"
+#include "services/network/public/cpp/shared_url_loader_factory.h"
+
+namespace printing {
+class PrintJobManager;
+}
+
+// Empty definition for std::unique_ptr
+class BackgroundModeManager {};
+
+// NOT THREAD SAFE, call only from the main thread.
+// These functions shouldn't return NULL unless otherwise noted.
+class BrowserProcessImpl : public BrowserProcess {
+ public:
+  BrowserProcessImpl();
+  ~BrowserProcessImpl() override;
+
+  void ResourceDispatcherHostCreated() override {}
+  void EndSession() override {}
+  void FlushLocalStateAndReply(base::OnceClosure reply) override {}
+  bool IsShuttingDown() override;
+
+  metrics_services_manager::MetricsServicesManager* GetMetricsServicesManager()
+      override;
+  metrics::MetricsService* metrics_service() override;
+  rappor::RapporServiceImpl* rappor_service() override;
+  ProfileManager* profile_manager() override;
+  PrefService* local_state() override;
+  net::URLRequestContextGetter* system_request_context() override;
+  scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory()
+      override;
+  variations::VariationsService* variations_service() override;
+  BrowserProcessPlatformPart* platform_part() override;
+  extensions::EventRouterForwarder* extension_event_router_forwarder() override;
+  NotificationUIManager* notification_ui_manager() override;
+  NotificationPlatformBridge* notification_platform_bridge() override;
+  IOThread* io_thread() override;
+  SystemNetworkContextManager* system_network_context_manager() override;
+  network::NetworkQualityTracker* network_quality_tracker() override;
+  WatchDogThread* watchdog_thread() override;
+  policy::ChromeBrowserPolicyConnector* browser_policy_connector() override;
+  policy::PolicyService* policy_service() override;
+  IconManager* icon_manager() override;
+  GpuModeManager* gpu_mode_manager() override;
+  printing::PrintPreviewDialogController* print_preview_dialog_controller()
+      override;
+  printing::BackgroundPrintingManager* background_printing_manager() override;
+  IntranetRedirectDetector* intranet_redirect_detector() override;
+  DownloadStatusUpdater* download_status_updater() override;
+  DownloadRequestLimiter* download_request_limiter() override;
+  BackgroundModeManager* background_mode_manager() override;
+  StatusTray* status_tray() override;
+  safe_browsing::SafeBrowsingService* safe_browsing_service() override;
+  safe_browsing::ClientSideDetectionService* safe_browsing_detection_service()
+      override;
+  subresource_filter::ContentRulesetService*
+  subresource_filter_ruleset_service() override;
+  optimization_guide::OptimizationGuideService* optimization_guide_service()
+      override;
+  net_log::ChromeNetLog* net_log() override;
+  component_updater::ComponentUpdateService* component_updater() override;
+  component_updater::SupervisedUserWhitelistInstaller*
+  supervised_user_whitelist_installer() override;
+  MediaFileSystemRegistry* media_file_system_registry() override;
+  WebRtcLogUploader* webrtc_log_uploader() override;
+  network_time::NetworkTimeTracker* network_time_tracker() override;
+  gcm::GCMDriver* gcm_driver() override;
+  resource_coordinator::TabManager* GetTabManager() override;
+  shell_integration::DefaultWebClientState CachedDefaultWebClientState()
+      override;
+  prefs::InProcessPrefServiceFactory* pref_service_factory() const override;
+  content::NetworkConnectionTracker* network_connection_tracker() override;
+  void CreateDevToolsProtocolHandler() override {}
+  void CreateDevToolsAutoOpener() override {}
+  void set_background_mode_manager_for_test(
+      std::unique_ptr<BackgroundModeManager> manager) override {}
+#if (defined(OS_WIN) || defined(OS_LINUX))
+  void StartAutoupdateTimer() override {}
+#endif
+  void SetApplicationLocale(const std::string& locale) override;
+  const std::string& GetApplicationLocale() override;
+  printing::PrintJobManager* print_job_manager() override;
+
+ private:
+#if BUILDFLAG(ENABLE_PRINTING)
+  std::unique_ptr<printing::PrintJobManager> print_job_manager_;
+#endif
+  std::string locale_;
+
+  DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl);
+};
+
+#endif  // CHROME_BROWSER_BROWSER_PROCESS_IMPL_H_

+ 0 - 684
chromium_src/chrome/browser/printing/pdf_to_emf_converter.cc

@@ -1,684 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/pdf_to_emf_converter.h"
-
-#include <stdint.h>
-#include <windows.h>
-
-#include <memory>
-#include <queue>
-#include <utility>
-#include <vector>
-
-#include "base/files/file.h"
-#include "base/files/file_util.h"
-#include "base/files/scoped_temp_dir.h"
-#include "base/logging.h"
-#include "base/macros.h"
-#include "base/memory/ref_counted_delete_on_sequence.h"
-#include "base/sequenced_task_runner.h"
-#include "base/task_scheduler/post_task.h"
-#include "base/threading/sequenced_task_runner_handle.h"
-#include "base/threading/thread_restrictions.h"
-#include "base/threading/thread_task_runner_handle.h"
-#include "chrome/common/chrome_utility_printing_messages.h"
-#include "content/browser/utility_process_host.h"
-#include "content/browser/utility_process_host_client.h"
-#include "content/public/browser/browser_thread.h"
-#include "content/public/browser/child_process_data.h"
-#include "printing/emf_win.h"
-#include "printing/pdf_render_settings.h"
-#include "ui/base/l10n/l10n_util.h"
-
-using content::BrowserThread;
-
-namespace printing {
-
-namespace {
-
-void CloseFileOnBlockingTaskRunner(base::File temp_file) {
-  base::AssertBlockingAllowed();
-  temp_file.Close();
-}
-
-class PdfConverterImpl;
-
-// Allows to delete temporary directory after all temporary files created inside
-// are closed. Windows cannot delete directory with opened files. Directory is
-// used to store PDF and metafiles. PDF should be gone by the time utility
-// process exits. Metafiles should be gone when all LazyEmf destroyed.
-class RefCountedTempDir
-    : public base::RefCountedDeleteOnSequence<RefCountedTempDir> {
- public:
-  RefCountedTempDir()
-      : base::RefCountedDeleteOnSequence<RefCountedTempDir>(
-            base::SequencedTaskRunnerHandle::Get()) {
-    ignore_result(temp_dir_.CreateUniqueTempDir());
-  }
-
-  bool IsValid() const { return temp_dir_.IsValid(); }
-  const base::FilePath& GetPath() const { return temp_dir_.GetPath(); }
-
- private:
-  friend class base::RefCountedDeleteOnSequence<RefCountedTempDir>;
-  friend class base::DeleteHelper<RefCountedTempDir>;
-
-  ~RefCountedTempDir() {}
-
-  base::ScopedTempDir temp_dir_;
-  DISALLOW_COPY_AND_ASSIGN(RefCountedTempDir);
-};
-
-class TempFile {
- public:
-  explicit TempFile(base::File file)
-      : file_(std::move(file)),
-        blocking_task_runner_(base::SequencedTaskRunnerHandle::Get()) {
-    base::AssertBlockingAllowed();
-  }
-  ~TempFile() {
-    blocking_task_runner_->PostTask(
-        FROM_HERE, base::BindOnce(&CloseFileOnBlockingTaskRunner,
-                                  base::Passed(std::move(file_))));
-  }
-
-  base::File& file() { return file_; }
-
- private:
-  base::File file_;
-  const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
-
-  DISALLOW_COPY_AND_ASSIGN(TempFile);
-};
-
-using ScopedTempFile = std::unique_ptr<TempFile>;
-
-// Wrapper for Emf to keep only file handle in memory, and load actual data only
-// on playback. Emf::InitFromFile() can play metafile directly from disk, but it
-// can't open file handles. We need file handles to reliably delete temporary
-// files, and to efficiently interact with utility process.
-class LazyEmf : public MetafilePlayer {
- public:
-  LazyEmf(const scoped_refptr<RefCountedTempDir>& temp_dir, ScopedTempFile file)
-      : temp_dir_(temp_dir), file_(std::move(file)) {
-    CHECK(file_);
-  }
-  ~LazyEmf() override { Close(); }
-
- protected:
-  // MetafilePlayer:
-  bool SafePlayback(HDC hdc) const override;
-
-  void Close() const;
-  bool LoadEmf(Emf* emf) const;
-
- private:
-  mutable scoped_refptr<RefCountedTempDir> temp_dir_;
-  mutable ScopedTempFile file_;  // Mutable because of consts in base class.
-
-  bool GetDataAsVector(std::vector<char>* buffer) const override;
-  bool SaveTo(base::File* file) const override;
-
-  DISALLOW_COPY_AND_ASSIGN(LazyEmf);
-};
-
-// Postscript metafile subclass to override SafePlayback.
-class PostScriptMetaFile : public LazyEmf {
- public:
-  PostScriptMetaFile(const scoped_refptr<RefCountedTempDir>& temp_dir,
-                     ScopedTempFile file)
-      : LazyEmf(temp_dir, std::move(file)) {}
-  ~PostScriptMetaFile() override;
-
- private:
-  // MetafilePlayer:
-  bool SafePlayback(HDC hdc) const override;
-
-  DISALLOW_COPY_AND_ASSIGN(PostScriptMetaFile);
-};
-
-// Class for converting PDF to another format for printing (Emf, Postscript).
-// Class uses UI thread, IO thread and |blocking_task_runner_|.
-// Internal workflow is following:
-// 1. Create instance on the UI thread. (files_, settings_,)
-// 2. Create pdf file on |blocking_task_runner_|.
-// 3. Start utility process and start conversion on the IO thread.
-// 4. Utility process returns page count.
-// 5. For each page:
-//   1. Clients requests page with file handle to a temp file.
-//   2. Utility converts the page, save it to the file and reply.
-//
-// All these steps work sequentially, so no data should be accessed
-// simultaneously by several threads.
-class PdfConverterUtilityProcessHostClient
-    : public content::UtilityProcessHostClient {
- public:
-  PdfConverterUtilityProcessHostClient(
-      base::WeakPtr<PdfConverterImpl> converter,
-      const PdfRenderSettings& settings);
-
-  void Start(const scoped_refptr<base::RefCountedMemory>& data,
-             const PdfConverter::StartCallback& start_callback);
-
-  void GetPage(int page_number,
-               const PdfConverter::GetPageCallback& get_page_callback);
-
-  void Stop();
-
-  // UtilityProcessHostClient implementation.
-  void OnProcessCrashed(int exit_code) override;
-  void OnProcessLaunchFailed(int exit_code) override;
-
-  // Needs to be public to handle ChromeUtilityHostMsg_PreCacheFontCharacters
-  // sync message replies.
-  bool Send(IPC::Message* msg);
-
- private:
-  class GetPageCallbackData {
-   public:
-    GetPageCallbackData(int page_number, PdfConverter::GetPageCallback callback)
-        : page_number_(page_number), callback_(callback) {}
-
-    GetPageCallbackData(GetPageCallbackData&& other) {
-      *this = std::move(other);
-    }
-
-    GetPageCallbackData& operator=(GetPageCallbackData&& rhs) {
-      page_number_ = rhs.page_number_;
-      callback_ = rhs.callback_;
-      file_ = std::move(rhs.file_);
-      return *this;
-    }
-
-    int page_number() const { return page_number_; }
-    const PdfConverter::GetPageCallback& callback() const { return callback_; }
-    ScopedTempFile TakeFile() { return std::move(file_); }
-    void set_file(ScopedTempFile file) { file_ = std::move(file); }
-
-   private:
-    int page_number_;
-
-    PdfConverter::GetPageCallback callback_;
-    ScopedTempFile file_;
-
-    DISALLOW_COPY_AND_ASSIGN(GetPageCallbackData);
-  };
-
-  ~PdfConverterUtilityProcessHostClient() override;
-
-  bool OnMessageReceived(const IPC::Message& message) override;
-
-  // Helper functions: must be overridden by subclasses
-  // Set the process name
-  base::string16 GetName() const;
-  // Create a metafileplayer subclass file from a temporary file.
-  std::unique_ptr<MetafilePlayer> GetFileFromTemp(ScopedTempFile temp_file);
-  // Send the messages to Start, GetPage, and Stop.
-  void SendStartMessage(IPC::PlatformFileForTransit transit);
-  void SendGetPageMessage(int page_number, IPC::PlatformFileForTransit transit);
-  void SendStopMessage();
-
-  // Message handlers:
-  void OnPageCount(int page_count);
-  void OnPageDone(bool success, float scale_factor);
-
-  void OnFailed();
-  void OnTempPdfReady(ScopedTempFile pdf);
-  void OnTempFileReady(GetPageCallbackData* callback_data,
-                       ScopedTempFile temp_file);
-
-  // Additional message handler needed for Pdf to Emf
-  void OnPreCacheFontCharacters(const LOGFONT& log_font,
-                                const base::string16& characters);
-
-  scoped_refptr<RefCountedTempDir> temp_dir_;
-
-  // Used to suppress callbacks after PdfConverter is deleted.
-  base::WeakPtr<PdfConverterImpl> converter_;
-  PdfRenderSettings settings_;
-
-  // Document loaded callback.
-  PdfConverter::StartCallback start_callback_;
-
-  // Process host for IPC.
-  base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
-
-  // Queue of callbacks for GetPage() requests. Utility process should reply
-  // with PageDone in the same order as requests were received.
-  // Use containers that keeps element pointers valid after push() and pop().
-  using GetPageCallbacks = std::queue<GetPageCallbackData>;
-  GetPageCallbacks get_page_callbacks_;
-
-  const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
-
-  DISALLOW_COPY_AND_ASSIGN(PdfConverterUtilityProcessHostClient);
-};
-
-std::unique_ptr<MetafilePlayer>
-PdfConverterUtilityProcessHostClient::GetFileFromTemp(
-    ScopedTempFile temp_file) {
-  if (settings_.mode == PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2 ||
-      settings_.mode == PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3) {
-    return std::make_unique<PostScriptMetaFile>(temp_dir_,
-                                                std::move(temp_file));
-  }
-  return std::make_unique<LazyEmf>(temp_dir_, std::move(temp_file));
-}
-
-class PdfConverterImpl : public PdfConverter {
- public:
-  PdfConverterImpl();
-
-  ~PdfConverterImpl() override;
-
-  base::WeakPtr<PdfConverterImpl> GetWeakPtr() {
-    return weak_ptr_factory_.GetWeakPtr();
-  }
-
-  void Start(const scoped_refptr<base::RefCountedMemory>& data,
-             const PdfRenderSettings& conversion_settings,
-             const StartCallback& start_callback);
-
-  void GetPage(int page_number,
-               const GetPageCallback& get_page_callback) override;
-
-  // Helps to cancel callbacks if this object is destroyed.
-  void RunCallback(const base::Closure& callback);
-
-  void Start(
-      const scoped_refptr<PdfConverterUtilityProcessHostClient>& utility_client,
-      const scoped_refptr<base::RefCountedMemory>& data,
-      const StartCallback& start_callback);
-
- private:
-  scoped_refptr<PdfConverterUtilityProcessHostClient> utility_client_;
-
-  base::WeakPtrFactory<PdfConverterImpl> weak_ptr_factory_;
-
-  DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl);
-};
-
-ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) {
-  if (!temp_dir->get())
-    *temp_dir = base::MakeRefCounted<RefCountedTempDir>();
-  ScopedTempFile file;
-  if (!(*temp_dir)->IsValid())
-    return file;
-  base::FilePath path;
-  if (!base::CreateTemporaryFileInDir((*temp_dir)->GetPath(), &path)) {
-    PLOG(ERROR) << "Failed to create file in "
-                << (*temp_dir)->GetPath().value();
-    return file;
-  }
-  file = std::make_unique<TempFile>(base::File(
-      path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
-                base::File::FLAG_READ | base::File::FLAG_DELETE_ON_CLOSE |
-                base::File::FLAG_TEMPORARY));
-  if (!file->file().IsValid()) {
-    PLOG(ERROR) << "Failed to create " << path.value();
-    file.reset();
-  }
-  return file;
-}
-
-ScopedTempFile CreateTempPdfFile(
-    const scoped_refptr<base::RefCountedMemory>& data,
-    scoped_refptr<RefCountedTempDir>* temp_dir) {
-  ScopedTempFile pdf_file = CreateTempFile(temp_dir);
-  if (!pdf_file || static_cast<int>(data->size()) !=
-                       pdf_file->file().WriteAtCurrentPos(
-                           data->front_as<char>(), data->size())) {
-    pdf_file.reset();
-    return pdf_file;
-  }
-  pdf_file->file().Seek(base::File::FROM_BEGIN, 0);
-  return pdf_file;
-}
-
-bool LazyEmf::SafePlayback(HDC hdc) const {
-  Emf emf;
-  bool result = LoadEmf(&emf) && emf.SafePlayback(hdc);
-  // TODO(thestig): Fix destruction of metafiles. For some reasons
-  // instances of Emf are not deleted. https://crbug.com/260806
-  // It's known that the Emf going to be played just once to a printer. So just
-  // release |file_| here.
-  Close();
-  return result;
-}
-
-bool LazyEmf::GetDataAsVector(std::vector<char>* buffer) const {
-  NOTREACHED();
-  return false;
-}
-
-bool LazyEmf::SaveTo(base::File* file) const {
-  Emf emf;
-  return LoadEmf(&emf) && emf.SaveTo(file);
-}
-
-void LazyEmf::Close() const {
-  file_.reset();
-  temp_dir_ = nullptr;
-}
-
-bool LazyEmf::LoadEmf(Emf* emf) const {
-  file_->file().Seek(base::File::FROM_BEGIN, 0);
-  int64_t size = file_->file().GetLength();
-  if (size <= 0)
-    return false;
-  std::vector<char> data(size);
-  if (file_->file().ReadAtCurrentPos(data.data(), data.size()) != size)
-    return false;
-  return emf->InitFromData(data.data(), data.size());
-}
-
-PostScriptMetaFile::~PostScriptMetaFile() {}
-
-bool PostScriptMetaFile::SafePlayback(HDC hdc) const {
-  // TODO(thestig): Fix destruction of metafiles. For some reasons
-  // instances of Emf are not deleted. https://crbug.com/260806
-  // It's known that the Emf going to be played just once to a printer. So just
-  // release |file_| before returning.
-  Emf emf;
-  if (!LoadEmf(&emf)) {
-    Close();
-    return false;
-  }
-
-  {
-    // Ensure enumerator destruction before calling Close() below.
-    Emf::Enumerator emf_enum(emf, nullptr, nullptr);
-    for (const Emf::Record& record : emf_enum) {
-      auto* emf_record = record.record();
-      if (emf_record->iType != EMR_GDICOMMENT)
-        continue;
-
-      const EMRGDICOMMENT* comment =
-          reinterpret_cast<const EMRGDICOMMENT*>(emf_record);
-      const char* data = reinterpret_cast<const char*>(comment->Data);
-      const uint16_t* ptr = reinterpret_cast<const uint16_t*>(data);
-      int ret = ExtEscape(hdc, PASSTHROUGH, 2 + *ptr, data, 0, nullptr);
-      DCHECK_EQ(*ptr, ret);
-    }
-  }
-  Close();
-  return true;
-}
-
-PdfConverterUtilityProcessHostClient::PdfConverterUtilityProcessHostClient(
-    base::WeakPtr<PdfConverterImpl> converter,
-    const PdfRenderSettings& settings)
-    : converter_(converter),
-      settings_(settings),
-      blocking_task_runner_(base::CreateSequencedTaskRunnerWithTraits(
-          {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
-           base::TaskShutdownBehavior::BLOCK_SHUTDOWN})) {}
-
-PdfConverterUtilityProcessHostClient::~PdfConverterUtilityProcessHostClient() {}
-
-void PdfConverterUtilityProcessHostClient::Start(
-    const scoped_refptr<base::RefCountedMemory>& data,
-    const PdfConverter::StartCallback& start_callback) {
-  CHECK(false) << "Printing doesn't work yet.";
-  if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
-    BrowserThread::PostTask(
-        BrowserThread::IO, FROM_HERE,
-        base::Bind(&PdfConverterUtilityProcessHostClient::Start, this, data,
-                   start_callback));
-    return;
-  }
-
-  // Store callback before any OnFailed() call to make it called on failure.
-  start_callback_ = start_callback;
-
-  // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load
-  // gdiplus.dll, change how rendering happens, and not be able to correctly
-  // generate when sent to a metafile DC.
-  /*
-  utility_process_host_ = content::UtilityProcessHost::Create(
-                              this, base::ThreadTaskRunnerHandle::Get())
-                              ->AsWeakPtr();
-  */
-  utility_process_host_ = nullptr;
-  utility_process_host_->SetName(GetName());
-
-  base::PostTaskAndReplyWithResult(
-      blocking_task_runner_.get(), FROM_HERE,
-      base::Bind(&CreateTempPdfFile, data, &temp_dir_),
-      base::Bind(&PdfConverterUtilityProcessHostClient::OnTempPdfReady, this));
-}
-
-void PdfConverterUtilityProcessHostClient::OnTempPdfReady(ScopedTempFile pdf) {
-  DCHECK_CURRENTLY_ON(BrowserThread::IO);
-  if (!utility_process_host_ || !pdf)
-    return OnFailed();
-  // Should reply with OnPageCount().
-  SendStartMessage(
-      IPC::GetPlatformFileForTransit(pdf->file().GetPlatformFile(), false));
-}
-
-void PdfConverterUtilityProcessHostClient::OnPageCount(int page_count) {
-  DCHECK_CURRENTLY_ON(BrowserThread::IO);
-  if (start_callback_.is_null())
-    return OnFailed();
-  BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
-                          base::Bind(&PdfConverterImpl::RunCallback, converter_,
-                                     base::Bind(start_callback_, page_count)));
-  start_callback_.Reset();
-}
-
-void PdfConverterUtilityProcessHostClient::GetPage(
-    int page_number,
-    const PdfConverter::GetPageCallback& get_page_callback) {
-  if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
-    BrowserThread::PostTask(
-        BrowserThread::IO, FROM_HERE,
-        base::Bind(&PdfConverterUtilityProcessHostClient::GetPage, this,
-                   page_number, get_page_callback));
-    return;
-  }
-
-  // Store callback before any OnFailed() call to make it called on failure.
-  get_page_callbacks_.push(GetPageCallbackData(page_number, get_page_callback));
-
-  if (!utility_process_host_)
-    return OnFailed();
-
-  base::PostTaskAndReplyWithResult(
-      blocking_task_runner_.get(), FROM_HERE,
-      base::Bind(&CreateTempFile, &temp_dir_),
-      base::Bind(&PdfConverterUtilityProcessHostClient::OnTempFileReady, this,
-                 &get_page_callbacks_.back()));
-}
-
-void PdfConverterUtilityProcessHostClient::OnTempFileReady(
-    GetPageCallbackData* callback_data,
-    ScopedTempFile temp_file) {
-  DCHECK_CURRENTLY_ON(BrowserThread::IO);
-  if (!utility_process_host_ || !temp_file)
-    return OnFailed();
-  IPC::PlatformFileForTransit transit = IPC::GetPlatformFileForTransit(
-      temp_file->file().GetPlatformFile(), false);
-  callback_data->set_file(std::move(temp_file));
-  // Should reply with OnPageDone().
-  SendGetPageMessage(callback_data->page_number(), transit);
-}
-
-void PdfConverterUtilityProcessHostClient::OnPageDone(bool success,
-                                                      float scale_factor) {
-  DCHECK_CURRENTLY_ON(BrowserThread::IO);
-  if (get_page_callbacks_.empty())
-    return OnFailed();
-  GetPageCallbackData& data = get_page_callbacks_.front();
-  std::unique_ptr<MetafilePlayer> file;
-
-  if (success) {
-    ScopedTempFile temp_file = data.TakeFile();
-    if (!temp_file)  // Unexpected message from utility process.
-      return OnFailed();
-    file = GetFileFromTemp(std::move(temp_file));
-  }
-
-  BrowserThread::PostTask(
-      BrowserThread::UI, FROM_HERE,
-      base::Bind(&PdfConverterImpl::RunCallback, converter_,
-                 base::Bind(data.callback(), data.page_number(), scale_factor,
-                            base::Passed(&file))));
-  get_page_callbacks_.pop();
-}
-
-void PdfConverterUtilityProcessHostClient::Stop() {
-  if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
-    BrowserThread::PostTask(
-        BrowserThread::IO, FROM_HERE,
-        base::Bind(&PdfConverterUtilityProcessHostClient::Stop, this));
-    return;
-  }
-  SendStopMessage();
-}
-
-void PdfConverterUtilityProcessHostClient::OnProcessCrashed(int exit_code) {
-  OnFailed();
-}
-
-void PdfConverterUtilityProcessHostClient::OnProcessLaunchFailed(
-    int exit_code) {
-  OnFailed();
-}
-
-bool PdfConverterUtilityProcessHostClient::Send(IPC::Message* msg) {
-  if (utility_process_host_)
-#if 1
-    CHECK(false) << "Printing is broken.";
-#else
-    return utility_process_host_->Send(msg);
-#endif
-  delete msg;
-  return false;
-}
-
-void PdfConverterUtilityProcessHostClient::OnFailed() {
-  DCHECK_CURRENTLY_ON(BrowserThread::IO);
-  if (!start_callback_.is_null())
-    OnPageCount(0);
-  while (!get_page_callbacks_.empty())
-    OnPageDone(false, 0.0f);
-  utility_process_host_.reset();
-}
-
-void PdfConverterUtilityProcessHostClient::OnPreCacheFontCharacters(
-    const LOGFONT& font,
-    const base::string16& str) {
-  // TODO(scottmg): pdf/ppapi still require the renderer to be able to precache
-  // GDI fonts (http://crbug.com/383227), even when using DirectWrite.
-  // Eventually this shouldn't be added and should be moved to
-  // FontCacheDispatcher too. http://crbug.com/356346.
-
-  // First, comments from FontCacheDispatcher::OnPreCacheFont do apply here too.
-  // Except that for True Type fonts,
-  // GetTextMetrics will not load the font in memory.
-  // The only way windows seem to load properly, it is to create a similar
-  // device (like the one in which we print), then do an ExtTextOut,
-  // as we do in the printing thread, which is sandboxed.
-  HDC hdc = CreateEnhMetaFile(nullptr, nullptr, nullptr, nullptr);
-  HFONT font_handle = CreateFontIndirect(&font);
-  DCHECK(font_handle != nullptr);
-
-  HGDIOBJ old_font = SelectObject(hdc, font_handle);
-  DCHECK(old_font != nullptr);
-
-  ExtTextOut(hdc, 0, 0, ETO_GLYPH_INDEX, 0, str.c_str(), str.length(), nullptr);
-
-  SelectObject(hdc, old_font);
-  DeleteObject(font_handle);
-
-  HENHMETAFILE metafile = CloseEnhMetaFile(hdc);
-
-  if (metafile)
-    DeleteEnhMetaFile(metafile);
-}
-
-bool PdfConverterUtilityProcessHostClient::OnMessageReceived(
-    const IPC::Message& message) {
-  bool handled = true;
-  IPC_BEGIN_MESSAGE_MAP(PdfConverterUtilityProcessHostClient, message)
-    IPC_MESSAGE_HANDLER(
-        ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount, OnPageCount)
-    IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone,
-                        OnPageDone)
-#if 0
-    IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PreCacheFontCharacters,
-                        OnPreCacheFontCharacters)
-#endif
-    IPC_MESSAGE_UNHANDLED(handled = false)
-  IPC_END_MESSAGE_MAP()
-  return handled;
-}
-
-base::string16 PdfConverterUtilityProcessHostClient::GetName() const {
-  return L"ChromeUtilityProcessPDFConvertor";
-}
-
-void PdfConverterUtilityProcessHostClient::SendGetPageMessage(
-    int page_number,
-    IPC::PlatformFileForTransit transit) {
-  Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage(page_number,
-                                                              transit));
-}
-
-void PdfConverterUtilityProcessHostClient::SendStartMessage(
-    IPC::PlatformFileForTransit transit) {
-  Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(transit, settings_));
-}
-
-void PdfConverterUtilityProcessHostClient::SendStopMessage() {
-  Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop());
-}
-
-// Pdf Converter Impl and subclasses
-PdfConverterImpl::PdfConverterImpl() : weak_ptr_factory_(this) {}
-
-PdfConverterImpl::~PdfConverterImpl() {
-  if (utility_client_.get())
-    utility_client_->Stop();
-}
-
-void PdfConverterImpl::Start(
-    const scoped_refptr<PdfConverterUtilityProcessHostClient>& utility_client,
-    const scoped_refptr<base::RefCountedMemory>& data,
-    const StartCallback& start_callback) {
-  DCHECK(!utility_client_);
-  utility_client_ = utility_client;
-  utility_client_->Start(data, start_callback);
-}
-
-void PdfConverterImpl::GetPage(int page_number,
-                               const GetPageCallback& get_page_callback) {
-  utility_client_->GetPage(page_number, get_page_callback);
-}
-
-void PdfConverterImpl::RunCallback(const base::Closure& callback) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-  callback.Run();
-}
-
-}  // namespace
-
-PdfConverter::~PdfConverter() {}
-
-// static
-std::unique_ptr<PdfConverter> PdfConverter::StartPdfConverter(
-    const scoped_refptr<base::RefCountedMemory>& data,
-    const PdfRenderSettings& conversion_settings,
-    const StartCallback& start_callback) {
-  std::unique_ptr<PdfConverterImpl> converter =
-      std::make_unique<PdfConverterImpl>();
-  converter->Start(new PdfConverterUtilityProcessHostClient(
-                       converter->GetWeakPtr(), conversion_settings),
-                   data, start_callback);
-  return std::move(converter);
-}
-
-}  // namespace printing

+ 0 - 43
chromium_src/chrome/browser/printing/pdf_to_emf_converter.h

@@ -1,43 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PDF_TO_EMF_CONVERTER_H_
-#define CHROME_BROWSER_PRINTING_PDF_TO_EMF_CONVERTER_H_
-
-#include <memory>
-
-#include "base/callback.h"
-#include "base/memory/ref_counted_memory.h"
-
-namespace printing {
-
-class MetafilePlayer;
-struct PdfRenderSettings;
-
-class PdfConverter {
- public:
-  using StartCallback = base::Callback<void(int page_count)>;
-  using GetPageCallback =
-      base::Callback<void(int page_number,
-                          float scale_factor,
-                          std::unique_ptr<MetafilePlayer> file)>;
-  virtual ~PdfConverter();
-
-  // Starts conversion of PDF provided as |data|. Calls |start_callback|
-  // with positive |page_count|. |page_count| is 0 if initialization failed.
-  static std::unique_ptr<PdfConverter> StartPdfConverter(
-      const scoped_refptr<base::RefCountedMemory>& data,
-      const PdfRenderSettings& conversion_settings,
-      const StartCallback& start_callback);
-
-  // Requests conversion of the page. |page_number| is 0-base page number in
-  // PDF provided in Start() call.
-  // Calls |get_page_callback| after conversion. |emf| of callback in not NULL
-  // if conversion succeeded.
-  virtual void GetPage(int page_number,
-                       const GetPageCallback& get_page_callback) = 0;
-};
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PDF_TO_EMF_CONVERTER_H_

+ 0 - 468
chromium_src/chrome/browser/printing/print_job.cc

@@ -1,468 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/print_job.h"
-
-#include <memory>
-#include <utility>
-
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/location.h"
-#include "base/message_loop/message_loop.h"
-#include "base/run_loop.h"
-#include "base/single_thread_task_runner.h"
-#include "base/task_scheduler/post_task.h"
-#include "base/threading/thread_restrictions.h"
-#include "base/threading/thread_task_runner_handle.h"
-#include "build/build_config.h"
-#include "chrome/browser/chrome_notification_types.h"
-#include "chrome/browser/printing/print_job_worker.h"
-#include "content/public/browser/notification_service.h"
-#include "printing/printed_document.h"
-
-#if defined(OS_WIN)
-#include "chrome/browser/printing/pdf_to_emf_converter.h"
-#include "printing/metafile.h"
-#include "printing/pdf_render_settings.h"
-#endif
-
-using base::TimeDelta;
-
-namespace printing {
-
-namespace {
-
-// Helper function to ensure |owner| is valid until at least |callback| returns.
-void HoldRefCallback(const scoped_refptr<PrintJobWorkerOwner>& owner,
-                     const base::Closure& callback) {
-  callback.Run();
-}
-
-}  // namespace
-
-PrintJob::PrintJob()
-    : is_job_pending_(false), is_canceling_(false), quit_factory_(this) {
-  DCHECK(base::MessageLoopForUI::IsCurrent());
-}
-
-PrintJob::~PrintJob() {
-  // The job should be finished (or at least canceled) when it is destroyed.
-  DCHECK(!is_job_pending_);
-  DCHECK(!is_canceling_);
-  DCHECK(!worker_ || !worker_->IsRunning());
-  DCHECK(RunsTasksInCurrentSequence());
-}
-
-void PrintJob::Initialize(PrintJobWorkerOwner* job,
-                          const base::string16& name,
-                          int page_count) {
-  DCHECK(!worker_);
-  DCHECK(!is_job_pending_);
-  DCHECK(!is_canceling_);
-  DCHECK(!document_.get());
-  worker_ = job->DetachWorker(this);
-  settings_ = job->settings();
-
-  PrintedDocument* new_doc =
-      new PrintedDocument(settings_, name, job->cookie());
-  new_doc->set_page_count(page_count);
-  UpdatePrintedDocument(new_doc);
-
-  // Don't forget to register to our own messages.
-  registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
-                 content::Source<PrintJob>(this));
-}
-
-void PrintJob::Observe(int type,
-                       const content::NotificationSource& source,
-                       const content::NotificationDetails& details) {
-  DCHECK(RunsTasksInCurrentSequence());
-  DCHECK_EQ(chrome::NOTIFICATION_PRINT_JOB_EVENT, type);
-
-  OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr());
-}
-
-void PrintJob::GetSettingsDone(const PrintSettings& new_settings,
-                               PrintingContext::Result result) {
-  NOTREACHED();
-}
-
-std::unique_ptr<PrintJobWorker> PrintJob::DetachWorker(
-    PrintJobWorkerOwner* new_owner) {
-  NOTREACHED();
-  return nullptr;
-}
-
-const PrintSettings& PrintJob::settings() const {
-  return settings_;
-}
-
-int PrintJob::cookie() const {
-  // Always use an invalid cookie in this case.
-  if (!document_.get())
-    return 0;
-  return document_->cookie();
-}
-
-void PrintJob::StartPrinting() {
-  DCHECK(RunsTasksInCurrentSequence());
-  if (!worker_->IsRunning() || is_job_pending_) {
-    NOTREACHED();
-    return;
-  }
-
-  // Real work is done in PrintJobWorker::StartPrinting().
-  worker_->PostTask(FROM_HERE,
-                    base::Bind(&HoldRefCallback, WrapRefCounted(this),
-                               base::Bind(&PrintJobWorker::StartPrinting,
-                                          base::Unretained(worker_.get()),
-                                          base::RetainedRef(document_))));
-  // Set the flag right now.
-  is_job_pending_ = true;
-
-  // Tell everyone!
-  scoped_refptr<JobEventDetails> details(
-      new JobEventDetails(JobEventDetails::NEW_DOC, 0, document_.get()));
-  content::NotificationService::current()->Notify(
-      chrome::NOTIFICATION_PRINT_JOB_EVENT, content::Source<PrintJob>(this),
-      content::Details<JobEventDetails>(details.get()));
-}
-
-void PrintJob::Stop() {
-  DCHECK(RunsTasksInCurrentSequence());
-
-  if (quit_factory_.HasWeakPtrs()) {
-    // In case we're running a nested message loop to wait for a job to finish,
-    // and we finished before the timeout, quit the nested loop right away.
-    Quit();
-    quit_factory_.InvalidateWeakPtrs();
-  }
-
-  // Be sure to live long enough.
-  scoped_refptr<PrintJob> handle(this);
-
-  if (worker_->IsRunning()) {
-    ControlledWorkerShutdown();
-  } else {
-    // Flush the cached document.
-    is_job_pending_ = false;
-    UpdatePrintedDocument(nullptr);
-  }
-}
-
-void PrintJob::Cancel() {
-  if (is_canceling_)
-    return;
-  is_canceling_ = true;
-
-  // Be sure to live long enough.
-  scoped_refptr<PrintJob> handle(this);
-
-  DCHECK(RunsTasksInCurrentSequence());
-  if (worker_ && worker_->IsRunning()) {
-    // Call this right now so it renders the context invalid. Do not use
-    // InvokeLater since it would take too much time.
-    worker_->Cancel();
-  }
-  // Make sure a Cancel() is broadcast.
-  scoped_refptr<JobEventDetails> details(
-      new JobEventDetails(JobEventDetails::FAILED, 0, nullptr));
-  content::NotificationService::current()->Notify(
-      chrome::NOTIFICATION_PRINT_JOB_EVENT, content::Source<PrintJob>(this),
-      content::Details<JobEventDetails>(details.get()));
-  Stop();
-  is_canceling_ = false;
-}
-
-bool PrintJob::FlushJob(base::TimeDelta timeout) {
-  // Make sure the object outlive this message loop.
-  scoped_refptr<PrintJob> handle(this);
-
-  base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
-      FROM_HERE, base::Bind(&PrintJob::Quit, quit_factory_.GetWeakPtr()),
-      timeout);
-
-  base::MessageLoop::ScopedNestableTaskAllower allow;
-  base::RunLoop().Run();
-
-  return true;
-}
-
-bool PrintJob::is_job_pending() const {
-  return is_job_pending_;
-}
-
-PrintedDocument* PrintJob::document() const {
-  return document_.get();
-}
-
-#if defined(OS_WIN)
-class PrintJob::PdfConversionState {
- public:
-  PdfConversionState(gfx::Size page_size, gfx::Rect content_area)
-      : page_count_(0),
-        current_page_(0),
-        pages_in_progress_(0),
-        page_size_(page_size),
-        content_area_(content_area) {}
-
-  void Start(const scoped_refptr<base::RefCountedMemory>& data,
-             const PdfRenderSettings& conversion_settings,
-             const PdfConverter::StartCallback& start_callback) {
-    converter_ = PdfConverter::StartPdfConverter(data, conversion_settings,
-                                                 start_callback);
-  }
-
-  void GetMorePages(const PdfConverter::GetPageCallback& get_page_callback) {
-    const int kMaxNumberOfTempFilesPerDocument = 3;
-    while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument &&
-           current_page_ < page_count_) {
-      ++pages_in_progress_;
-      converter_->GetPage(current_page_++, get_page_callback);
-    }
-  }
-
-  void OnPageProcessed(const PdfConverter::GetPageCallback& get_page_callback) {
-    --pages_in_progress_;
-    GetMorePages(get_page_callback);
-    // Release converter if we don't need this any more.
-    if (!pages_in_progress_ && current_page_ >= page_count_)
-      converter_.reset();
-  }
-
-  void set_page_count(int page_count) { page_count_ = page_count; }
-  gfx::Size page_size() const { return page_size_; }
-  gfx::Rect content_area() const { return content_area_; }
-
- private:
-  int page_count_;
-  int current_page_;
-  int pages_in_progress_;
-  gfx::Size page_size_;
-  gfx::Rect content_area_;
-  std::unique_ptr<PdfConverter> converter_;
-};
-
-void PrintJob::AppendPrintedPage(int page_number) {
-  pdf_page_mapping_.push_back(page_number);
-}
-
-void PrintJob::StartPdfToEmfConversion(
-    const scoped_refptr<base::RefCountedMemory>& bytes,
-    const gfx::Size& page_size,
-    const gfx::Rect& content_area,
-    bool print_text_with_gdi) {
-  DCHECK(!pdf_conversion_state_);
-  pdf_conversion_state_ =
-      std::make_unique<PdfConversionState>(page_size, content_area);
-  const int kPrinterDpi = settings().dpi();
-  PdfRenderSettings settings(
-      content_area, gfx::Point(0, 0), gfx::Size(kPrinterDpi, kPrinterDpi),
-      /*autorotate=*/true, settings_.color() == COLOR,
-      print_text_with_gdi ? PdfRenderSettings::Mode::GDI_TEXT
-                          : PdfRenderSettings::Mode::NORMAL);
-  pdf_conversion_state_->Start(
-      bytes, settings, base::Bind(&PrintJob::OnPdfConversionStarted, this));
-}
-
-void PrintJob::OnPdfConversionStarted(int page_count) {
-  if (page_count <= 0) {
-    pdf_conversion_state_.reset();
-    Cancel();
-    return;
-  }
-  pdf_conversion_state_->set_page_count(page_count);
-  pdf_conversion_state_->GetMorePages(
-      base::Bind(&PrintJob::OnPdfPageConverted, this));
-}
-
-void PrintJob::OnPdfPageConverted(int page_number,
-                                  float scale_factor,
-                                  std::unique_ptr<MetafilePlayer> metafile) {
-  DCHECK(pdf_conversion_state_);
-  if (!document_.get() || !metafile || page_number < 0 ||
-      static_cast<size_t>(page_number) >= pdf_page_mapping_.size()) {
-    pdf_conversion_state_.reset();
-    Cancel();
-    return;
-  }
-
-  // Update the rendered document. It will send notifications to the listener.
-  document_->SetPage(pdf_page_mapping_[page_number], std::move(metafile),
-                     scale_factor, pdf_conversion_state_->page_size(),
-                     pdf_conversion_state_->content_area());
-
-  pdf_conversion_state_->GetMorePages(
-      base::Bind(&PrintJob::OnPdfPageConverted, this));
-}
-
-void PrintJob::StartPdfToPostScriptConversion(
-    const scoped_refptr<base::RefCountedMemory>& bytes,
-    const gfx::Rect& content_area,
-    const gfx::Point& physical_offsets,
-    bool ps_level2) {
-  DCHECK(!pdf_conversion_state_);
-  pdf_conversion_state_ =
-      std::make_unique<PdfConversionState>(gfx::Size(), gfx::Rect());
-  const int kPrinterDpi = settings().dpi();
-  PdfRenderSettings settings(
-      content_area, physical_offsets, gfx::Size(kPrinterDpi, kPrinterDpi),
-      true /* autorotate? */, settings_.color() == COLOR,
-      ps_level2 ? PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2
-                : PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3);
-  pdf_conversion_state_->Start(
-      bytes, settings, base::Bind(&PrintJob::OnPdfConversionStarted, this));
-}
-
-#endif  // defined(OS_WIN)
-
-void PrintJob::UpdatePrintedDocument(PrintedDocument* new_document) {
-  if (document_.get() == new_document)
-    return;
-
-  document_ = new_document;
-
-  if (document_.get())
-    settings_ = document_->settings();
-
-  if (worker_) {
-    DCHECK(!is_job_pending_);
-    // Sync the document with the worker.
-    worker_->PostTask(FROM_HERE,
-                      base::Bind(&HoldRefCallback, WrapRefCounted(this),
-                                 base::Bind(&PrintJobWorker::OnDocumentChanged,
-                                            base::Unretained(worker_.get()),
-                                            base::RetainedRef(document_))));
-  }
-}
-
-void PrintJob::OnNotifyPrintJobEvent(const JobEventDetails& event_details) {
-  switch (event_details.type()) {
-    case JobEventDetails::FAILED: {
-      settings_.Clear();
-      // No need to cancel since the worker already canceled itself.
-      Stop();
-      break;
-    }
-    case JobEventDetails::USER_INIT_DONE:
-    case JobEventDetails::DEFAULT_INIT_DONE:
-    case JobEventDetails::USER_INIT_CANCELED: {
-      DCHECK_EQ(event_details.document(), document_.get());
-      break;
-    }
-    case JobEventDetails::NEW_DOC:
-    case JobEventDetails::NEW_PAGE:
-    case JobEventDetails::JOB_DONE:
-    case JobEventDetails::ALL_PAGES_REQUESTED: {
-      // Don't care.
-      break;
-    }
-    case JobEventDetails::DOC_DONE: {
-      // This will call Stop() and broadcast a JOB_DONE message.
-      base::ThreadTaskRunnerHandle::Get()->PostTask(
-          FROM_HERE, base::Bind(&PrintJob::OnDocumentDone, this));
-      break;
-    }
-    case JobEventDetails::PAGE_DONE:
-#if defined(OS_WIN)
-      if (pdf_conversion_state_) {
-        pdf_conversion_state_->OnPageProcessed(
-            base::Bind(&PrintJob::OnPdfPageConverted, this));
-      }
-#endif  // defined(OS_WIN)
-      break;
-    default: {
-      NOTREACHED();
-      break;
-    }
-  }
-}
-
-void PrintJob::OnDocumentDone() {
-  // Be sure to live long enough. The instance could be destroyed by the
-  // JOB_DONE broadcast.
-  scoped_refptr<PrintJob> handle(this);
-
-  // Stop the worker thread.
-  Stop();
-
-  scoped_refptr<JobEventDetails> details(
-      new JobEventDetails(JobEventDetails::JOB_DONE, 0, document_.get()));
-  content::NotificationService::current()->Notify(
-      chrome::NOTIFICATION_PRINT_JOB_EVENT, content::Source<PrintJob>(this),
-      content::Details<JobEventDetails>(details.get()));
-}
-
-void PrintJob::ControlledWorkerShutdown() {
-  DCHECK(RunsTasksInCurrentSequence());
-
-  // The deadlock this code works around is specific to window messaging on
-  // Windows, so we aren't likely to need it on any other platforms.
-#if defined(OS_WIN)
-  // We could easily get into a deadlock case if worker_->Stop() is used; the
-  // printer driver created a window as a child of the browser window. By
-  // canceling the job, the printer driver initiated dialog box is destroyed,
-  // which sends a blocking message to its parent window. If the browser window
-  // thread is not processing messages, a deadlock occurs.
-  //
-  // This function ensures that the dialog box will be destroyed in a timely
-  // manner by the mere fact that the thread will terminate. So the potential
-  // deadlock is eliminated.
-  worker_->StopSoon();
-
-  // Delay shutdown until the worker terminates.  We want this code path
-  // to wait on the thread to quit before continuing.
-  if (worker_->IsRunning()) {
-    base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
-        FROM_HERE, base::Bind(&PrintJob::ControlledWorkerShutdown, this),
-        base::TimeDelta::FromMilliseconds(100));
-    return;
-  }
-#endif
-
-  // Now make sure the thread object is cleaned up. Do this on a worker
-  // thread because it may block.
-  base::PostTaskAndReply(
-      FROM_HERE,
-      base::Bind(&PrintJobWorker::Stop, base::Unretained(worker_.get())),
-      base::Bind(&PrintJob::HoldUntilStopIsCalled, this));
-
-  is_job_pending_ = false;
-  registrar_.RemoveAll();
-  UpdatePrintedDocument(nullptr);
-}
-
-void PrintJob::HoldUntilStopIsCalled() {}
-
-void PrintJob::Quit() {
-  base::RunLoop::QuitCurrentWhenIdleDeprecated();
-}
-
-// Takes settings_ ownership and will be deleted in the receiving thread.
-#if defined(OS_WIN)
-JobEventDetails::JobEventDetails(Type type,
-                                 int job_id,
-                                 PrintedDocument* document,
-                                 PrintedPage* page)
-    : document_(document), page_(page), type_(type), job_id_(job_id) {}
-#endif
-
-JobEventDetails::JobEventDetails(Type type,
-                                 int job_id,
-                                 PrintedDocument* document)
-    : document_(document), type_(type), job_id_(job_id) {}
-
-JobEventDetails::~JobEventDetails() {}
-
-PrintedDocument* JobEventDetails::document() const {
-  return document_.get();
-}
-
-#if defined(OS_WIN)
-PrintedPage* JobEventDetails::page() const {
-  return page_.get();
-}
-#endif
-}  // namespace printing

+ 0 - 246
chromium_src/chrome/browser/printing/print_job.h

@@ -1,246 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_H_
-#define CHROME_BROWSER_PRINTING_PRINT_JOB_H_
-
-#include <memory>
-#include <vector>
-
-#include "base/macros.h"
-#include "base/memory/weak_ptr.h"
-#include "build/build_config.h"
-#include "chrome/browser/printing/print_job_worker_owner.h"
-#include "content/public/browser/notification_observer.h"
-#include "content/public/browser/notification_registrar.h"
-
-#if defined(OS_WIN)
-#include "printing/printed_page_win.h"
-#endif
-
-namespace base {
-class RefCountedMemory;
-}
-
-namespace printing {
-
-class JobEventDetails;
-class MetafilePlayer;
-class PdfToEmfConverter;
-class PrintJobWorker;
-class PrintedDocument;
-#if defined(OS_WIN)
-class PrintedPage;
-#endif
-class PrinterQuery;
-
-// Manages the print work for a specific document. Talks to the printer through
-// PrintingContext through PrintJobWorker. Hides access to PrintingContext in a
-// worker thread so the caller never blocks. PrintJob will send notifications on
-// any state change. While printing, the PrintJobManager instance keeps a
-// reference to the job to be sure it is kept alive. All the code in this class
-// runs in the UI thread.
-class PrintJob : public PrintJobWorkerOwner,
-                 public content::NotificationObserver {
- public:
-  // Create a empty PrintJob. When initializing with this constructor,
-  // post-constructor initialization must be done with Initialize().
-  PrintJob();
-
-  // Grabs the ownership of the PrintJobWorker from another job, which is
-  // usually a PrinterQuery. Set the expected page count of the print job.
-  void Initialize(PrintJobWorkerOwner* job,
-                  const base::string16& name,
-                  int page_count);
-
-  // content::NotificationObserver implementation.
-  void Observe(int type,
-               const content::NotificationSource& source,
-               const content::NotificationDetails& details) override;
-
-  // PrintJobWorkerOwner implementation.
-  void GetSettingsDone(const PrintSettings& new_settings,
-                       PrintingContext::Result result) override;
-  std::unique_ptr<PrintJobWorker> DetachWorker(
-      PrintJobWorkerOwner* new_owner) override;
-  const PrintSettings& settings() const override;
-  int cookie() const override;
-
-  // Starts the actual printing. Signals the worker that it should begin to
-  // spool as soon as data is available.
-  void StartPrinting();
-
-  // Asks for the worker thread to finish its queued tasks and disconnects the
-  // delegate object. The PrintJobManager will remove its reference. This may
-  // have the side-effect of destroying the object if the caller doesn't have a
-  // handle to the object. Use PrintJob::is_stopped() to check whether the
-  // worker thread has actually stopped.
-  void Stop();
-
-  // Cancels printing job and stops the worker thread. Takes effect immediately.
-  void Cancel();
-
-  // Synchronously wait for the job to finish. It is mainly useful when the
-  // process is about to be shut down and we're waiting for the spooler to eat
-  // our data.
-  bool FlushJob(base::TimeDelta timeout);
-
-  // Returns true if the print job is pending, i.e. between a StartPrinting()
-  // and the end of the spooling.
-  bool is_job_pending() const;
-
-  // Access the current printed document. Warning: may be NULL.
-  PrintedDocument* document() const;
-
-#if defined(OS_WIN)
-  // Let the PrintJob know the 0-based |page_number| of a given printed page.
-  void AppendPrintedPage(int page_number);
-
-  void StartPdfToEmfConversion(
-      const scoped_refptr<base::RefCountedMemory>& bytes,
-      const gfx::Size& page_size,
-      const gfx::Rect& content_area,
-      bool print_text_with_gdi);
-
-  void StartPdfToPostScriptConversion(
-      const scoped_refptr<base::RefCountedMemory>& bytes,
-      const gfx::Rect& content_area,
-      const gfx::Point& physical_offset,
-      bool ps_level2);
-#endif  // defined(OS_WIN)
-
- protected:
-  ~PrintJob() override;
-
- private:
-  // Updates |document_| to a new instance.
-  void UpdatePrintedDocument(PrintedDocument* new_document);
-
-  // Processes a NOTIFY_PRINT_JOB_EVENT notification.
-  void OnNotifyPrintJobEvent(const JobEventDetails& event_details);
-
-  // Releases the worker thread by calling Stop(), then broadcasts a JOB_DONE
-  // notification.
-  void OnDocumentDone();
-
-  // Terminates the worker thread in a very controlled way, to work around any
-  // eventual deadlock.
-  void ControlledWorkerShutdown();
-
-  // Called at shutdown when running a nested message loop.
-  void Quit();
-
-  void HoldUntilStopIsCalled();
-
-#if defined(OS_WIN)
-  void OnPdfConversionStarted(int page_count);
-  void OnPdfPageConverted(int page_number,
-                          float scale_factor,
-                          std::unique_ptr<MetafilePlayer> emf);
-#endif  // defined(OS_WIN)
-
-  content::NotificationRegistrar registrar_;
-
-  // All the UI is done in a worker thread because many Win32 print functions
-  // are blocking and enters a message loop without your consent. There is one
-  // worker thread per print job.
-  std::unique_ptr<PrintJobWorker> worker_;
-
-  // Cache of the print context settings for access in the UI thread.
-  PrintSettings settings_;
-
-  // The printed document.
-  scoped_refptr<PrintedDocument> document_;
-
-  // Is the worker thread printing.
-  bool is_job_pending_;
-
-  // Is Canceling? If so, try to not cause recursion if on FAILED notification,
-  // the notified calls Cancel() again.
-  bool is_canceling_;
-
-#if defined(OS_WIN)
-  class PdfConversionState;
-  std::unique_ptr<PdfConversionState> pdf_conversion_state_;
-  std::vector<int> pdf_page_mapping_;
-#endif  // defined(OS_WIN)
-
-  // Used at shutdown so that we can quit a nested message loop.
-  base::WeakPtrFactory<PrintJob> quit_factory_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrintJob);
-};
-
-// Details for a NOTIFY_PRINT_JOB_EVENT notification. The members may be NULL.
-class JobEventDetails : public base::RefCountedThreadSafe<JobEventDetails> {
- public:
-  // Event type.
-  enum Type {
-    // Print... dialog box has been closed with OK button.
-    USER_INIT_DONE,
-
-    // Print... dialog box has been closed with CANCEL button.
-    USER_INIT_CANCELED,
-
-    // An automated initialization has been done, e.g. Init(false, NULL).
-    DEFAULT_INIT_DONE,
-
-    // A new document started printing.
-    NEW_DOC,
-
-    // A new page started printing.
-    NEW_PAGE,
-
-    // A page is done printing.
-    PAGE_DONE,
-
-    // A document is done printing. The worker thread is still alive. Warning:
-    // not a good moment to release the handle to PrintJob.
-    DOC_DONE,
-
-    // The worker thread is finished. A good moment to release the handle to
-    // PrintJob.
-    JOB_DONE,
-
-    // All missing pages have been requested.
-    ALL_PAGES_REQUESTED,
-
-    // An error occured. Printing is canceled.
-    FAILED,
-  };
-
-#if defined(OS_WIN)
-  JobEventDetails(Type type,
-                  int job_id,
-                  PrintedDocument* document,
-                  PrintedPage* page);
-#endif
-  JobEventDetails(Type type, int job_id, PrintedDocument* document);
-
-  // Getters.
-  PrintedDocument* document() const;
-#if defined(OS_WIN)
-  PrintedPage* page() const;
-#endif
-  Type type() const { return type_; }
-  int job_id() const { return job_id_; }
-
- private:
-  friend class base::RefCountedThreadSafe<JobEventDetails>;
-
-  ~JobEventDetails();
-
-  scoped_refptr<PrintedDocument> document_;
-#if defined(OS_WIN)
-  scoped_refptr<PrintedPage> page_;
-#endif
-  const Type type_;
-  int job_id_;
-
-  DISALLOW_COPY_AND_ASSIGN(JobEventDetails);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_H_

+ 0 - 151
chromium_src/chrome/browser/printing/print_job_manager.cc

@@ -1,151 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/print_job_manager.h"
-
-#include "chrome/browser/chrome_notification_types.h"
-#include "chrome/browser/printing/print_job.h"
-#include "chrome/browser/printing/printer_query.h"
-#include "content/public/browser/browser_thread.h"
-#include "content/public/browser/notification_service.h"
-#include "printing/printed_document.h"
-
-namespace printing {
-
-PrintQueriesQueue::PrintQueriesQueue() {}
-
-PrintQueriesQueue::~PrintQueriesQueue() {
-  base::AutoLock lock(lock_);
-  queued_queries_.clear();
-}
-
-void PrintQueriesQueue::QueuePrinterQuery(PrinterQuery* job) {
-  base::AutoLock lock(lock_);
-  DCHECK(job);
-  queued_queries_.push_back(WrapRefCounted(job));
-  DCHECK(job->is_valid());
-}
-
-scoped_refptr<PrinterQuery> PrintQueriesQueue::PopPrinterQuery(
-    int document_cookie) {
-  base::AutoLock lock(lock_);
-  for (PrinterQueries::iterator itr = queued_queries_.begin();
-       itr != queued_queries_.end(); ++itr) {
-    if ((*itr)->cookie() == document_cookie && !(*itr)->is_callback_pending()) {
-      scoped_refptr<PrinterQuery> current_query(*itr);
-      queued_queries_.erase(itr);
-      DCHECK(current_query->is_valid());
-      return current_query;
-    }
-  }
-  return NULL;
-}
-
-scoped_refptr<PrinterQuery> PrintQueriesQueue::CreatePrinterQuery(
-    int render_process_id,
-    int render_frame_id) {
-  return WrapRefCounted(new PrinterQuery(render_process_id, render_frame_id));
-}
-
-void PrintQueriesQueue::Shutdown() {
-  PrinterQueries queries_to_stop;
-  {
-    base::AutoLock lock(lock_);
-    queued_queries_.swap(queries_to_stop);
-  }
-  // Stop all pending queries, requests to generate print preview do not have
-  // corresponding PrintJob, so any pending preview requests are not covered
-  // by PrintJobManager::StopJobs and should be stopped explicitly.
-  for (PrinterQueries::iterator itr = queries_to_stop.begin();
-       itr != queries_to_stop.end(); ++itr) {
-    (*itr)->PostTask(FROM_HERE, base::Bind(&PrinterQuery::StopWorker, *itr));
-  }
-}
-
-PrintJobManager::PrintJobManager() : is_shutdown_(false) {
-  registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
-                 content::NotificationService::AllSources());
-}
-
-PrintJobManager::~PrintJobManager() {}
-
-scoped_refptr<PrintQueriesQueue> PrintJobManager::queue() {
-  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
-  if (!queue_.get())
-    queue_ = new PrintQueriesQueue();
-  return queue_;
-}
-
-void PrintJobManager::Shutdown() {
-  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
-  DCHECK(!is_shutdown_);
-  is_shutdown_ = true;
-  registrar_.RemoveAll();
-  StopJobs(true);
-  if (queue_.get())
-    queue_->Shutdown();
-  queue_ = NULL;
-}
-
-void PrintJobManager::StopJobs(bool wait_for_finish) {
-  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
-  // Copy the array since it can be modified in transit.
-  PrintJobs to_stop;
-  to_stop.swap(current_jobs_);
-
-  for (PrintJobs::const_iterator job = to_stop.begin(); job != to_stop.end();
-       ++job) {
-    // Wait for two minutes for the print job to be spooled.
-    if (wait_for_finish)
-      (*job)->FlushJob(base::TimeDelta::FromMinutes(2));
-    (*job)->Stop();
-  }
-}
-
-void PrintJobManager::Observe(int type,
-                              const content::NotificationSource& source,
-                              const content::NotificationDetails& details) {
-  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
-  DCHECK_EQ(chrome::NOTIFICATION_PRINT_JOB_EVENT, type);
-
-  OnPrintJobEvent(content::Source<PrintJob>(source).ptr(),
-                  *content::Details<JobEventDetails>(details).ptr());
-}
-
-void PrintJobManager::OnPrintJobEvent(PrintJob* print_job,
-                                      const JobEventDetails& event_details) {
-  switch (event_details.type()) {
-    case JobEventDetails::NEW_DOC: {
-      DCHECK(current_jobs_.end() == current_jobs_.find(print_job));
-      // Causes a AddRef().
-      current_jobs_.insert(print_job);
-      break;
-    }
-    case JobEventDetails::JOB_DONE: {
-      DCHECK(current_jobs_.end() != current_jobs_.find(print_job));
-      current_jobs_.erase(print_job);
-      break;
-    }
-    case JobEventDetails::FAILED: {
-      current_jobs_.erase(print_job);
-      break;
-    }
-    case JobEventDetails::USER_INIT_DONE:
-    case JobEventDetails::USER_INIT_CANCELED:
-    case JobEventDetails::DEFAULT_INIT_DONE:
-    case JobEventDetails::NEW_PAGE:
-    case JobEventDetails::PAGE_DONE:
-    case JobEventDetails::DOC_DONE:
-    case JobEventDetails::ALL_PAGES_REQUESTED: {
-      // Don't care.
-      break;
-    }
-    default: {
-      NOTREACHED();
-      break;
-    }
-  }
-}
-
-}  // namespace printing

+ 0 - 100
chromium_src/chrome/browser/printing/print_job_manager.h

@@ -1,100 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_
-#define CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_
-
-#include <memory>
-#include <set>
-#include <vector>
-
-#include "base/logging.h"
-#include "base/macros.h"
-#include "base/memory/ref_counted.h"
-#include "base/synchronization/lock.h"
-#include "content/public/browser/notification_observer.h"
-#include "content/public/browser/notification_registrar.h"
-
-namespace printing {
-
-class JobEventDetails;
-class PrintJob;
-class PrinterQuery;
-
-class PrintQueriesQueue : public base::RefCountedThreadSafe<PrintQueriesQueue> {
- public:
-  PrintQueriesQueue();
-
-  // Queues a semi-initialized worker thread. Can be called from any thread.
-  // Current use case is queuing from the I/O thread.
-  // TODO(maruel):  Have them vanish after a timeout (~5 minutes?)
-  void QueuePrinterQuery(PrinterQuery* job);
-
-  // Pops a queued PrintJobWorkerOwner object that was previously queued or
-  // create new one. Can be called from any thread.
-  scoped_refptr<PrinterQuery> PopPrinterQuery(int document_cookie);
-
-  // Creates new query.
-  scoped_refptr<PrinterQuery> CreatePrinterQuery(int render_process_id,
-                                                 int render_frame_id);
-
-  void Shutdown();
-
- private:
-  friend class base::RefCountedThreadSafe<PrintQueriesQueue>;
-  typedef std::vector<scoped_refptr<PrinterQuery>> PrinterQueries;
-
-  virtual ~PrintQueriesQueue();
-
-  // Used to serialize access to queued_workers_.
-  base::Lock lock_;
-
-  PrinterQueries queued_queries_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrintQueriesQueue);
-};
-
-class PrintJobManager : public content::NotificationObserver {
- public:
-  PrintJobManager();
-  ~PrintJobManager() override;
-
-  // On browser quit, we should wait to have the print job finished.
-  void Shutdown();
-
-  // content::NotificationObserver
-  void Observe(int type,
-               const content::NotificationSource& source,
-               const content::NotificationDetails& details) override;
-
-  // Returns queries queue. Never returns NULL. Must be called on Browser UI
-  // Thread. Reference could be stored and used from any thread.
-  scoped_refptr<PrintQueriesQueue> queue();
-
- private:
-  typedef std::set<scoped_refptr<PrintJob>> PrintJobs;
-
-  // Processes a NOTIFY_PRINT_JOB_EVENT notification.
-  void OnPrintJobEvent(PrintJob* print_job,
-                       const JobEventDetails& event_details);
-
-  // Stops all printing jobs. If wait_for_finish is true, tries to give jobs
-  // a chance to complete before stopping them.
-  void StopJobs(bool wait_for_finish);
-
-  content::NotificationRegistrar registrar_;
-
-  // Current print jobs that are active.
-  PrintJobs current_jobs_;
-
-  scoped_refptr<PrintQueriesQueue> queue_;
-
-  bool is_shutdown_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrintJobManager);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_MANAGER_H_

+ 0 - 558
chromium_src/chrome/browser/printing/print_job_worker.cc

@@ -1,558 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/print_job_worker.h"
-
-#include <memory>
-
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/callback.h"
-#include "base/compiler_specific.h"
-#include "base/location.h"
-#include "base/message_loop/message_loop.h"
-#include "base/single_thread_task_runner.h"
-#include "base/threading/thread_task_runner_handle.h"
-#include "base/values.h"
-#include "build/build_config.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/chrome_notification_types.h"
-#include "chrome/browser/printing/print_job.h"
-#include "content/public/browser/browser_thread.h"
-#include "content/public/browser/notification_service.h"
-#include "content/public/browser/render_frame_host.h"
-#include "content/public/browser/web_contents.h"
-#include "printing/print_job_constants.h"
-#include "printing/printed_document.h"
-#include "printing/printing_utils.h"
-#include "ui/base/l10n/l10n_util.h"
-
-#include <stddef.h>
-#include <algorithm>
-#include <cmath>
-#include <string>
-#include <utility>
-
-#include "base/strings/string_number_conversions.h"
-#include "base/strings/utf_string_conversions.h"
-#include "base/time/time.h"
-#include "printing/page_size_margins.h"
-#include "printing/print_job_constants.h"
-#include "printing/print_settings.h"
-#include "printing/units.h"
-
-#if defined(OS_WIN)
-#include "printing/printed_page_win.h"
-#endif
-
-using content::BrowserThread;
-
-namespace printing {
-
-namespace {
-
-// Helper function to ensure |owner| is valid until at least |callback| returns.
-void HoldRefCallback(const scoped_refptr<PrintJobWorkerOwner>& owner,
-                     base::OnceClosure callback) {
-  std::move(callback).Run();
-}
-
-void SetCustomMarginsToJobSettings(const PageSizeMargins& page_size_margins,
-                                   base::DictionaryValue* settings) {
-  std::unique_ptr<base::DictionaryValue> custom_margins(
-      new base::DictionaryValue());
-  custom_margins->SetDouble(kSettingMarginTop, page_size_margins.margin_top);
-  custom_margins->SetDouble(kSettingMarginBottom,
-                            page_size_margins.margin_bottom);
-  custom_margins->SetDouble(kSettingMarginLeft, page_size_margins.margin_left);
-  custom_margins->SetDouble(kSettingMarginRight,
-                            page_size_margins.margin_right);
-  settings->Set(kSettingMarginsCustom, std::move(custom_margins));
-}
-
-void PrintSettingsToJobSettings(const PrintSettings& settings,
-                                base::DictionaryValue* job_settings) {
-  // header footer
-  job_settings->SetBoolean(kSettingHeaderFooterEnabled,
-                           settings.display_header_footer());
-  job_settings->SetString(kSettingHeaderFooterTitle, settings.title());
-  job_settings->SetString(kSettingHeaderFooterURL, settings.url());
-
-  // bg
-  job_settings->SetBoolean(kSettingShouldPrintBackgrounds,
-                           settings.should_print_backgrounds());
-  job_settings->SetBoolean(kSettingShouldPrintSelectionOnly,
-                           settings.selection_only());
-
-  // margin
-  auto margin_type = settings.margin_type();
-  job_settings->SetInteger(kSettingMarginsType, settings.margin_type());
-  if (margin_type == CUSTOM_MARGINS) {
-    const auto& margins_in_points =
-        settings.requested_custom_margins_in_points();
-
-    PageSizeMargins page_size_margins;
-
-    page_size_margins.margin_top = margins_in_points.top;
-    page_size_margins.margin_bottom = margins_in_points.bottom;
-    page_size_margins.margin_left = margins_in_points.left;
-    page_size_margins.margin_right = margins_in_points.right;
-    SetCustomMarginsToJobSettings(page_size_margins, job_settings);
-  }
-  job_settings->SetInteger(kSettingPreviewPageCount, 1);
-
-  // range
-
-  if (!settings.ranges().empty()) {
-    auto page_range_array = std::make_unique<base::ListValue>();
-    job_settings->Set(kSettingPageRange, std::move(page_range_array));
-    for (size_t i = 0; i < settings.ranges().size(); ++i) {
-      std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
-      dict->SetInteger(kSettingPageRangeFrom, settings.ranges()[i].from + 1);
-      dict->SetInteger(kSettingPageRangeTo, settings.ranges()[i].to + 1);
-      page_range_array->Append(std::move(dict));
-    }
-  }
-
-  job_settings->SetBoolean(kSettingCollate, settings.collate());
-  job_settings->SetInteger(kSettingCopies, 1);
-  job_settings->SetInteger(kSettingColor, settings.color());
-  job_settings->SetInteger(kSettingDuplexMode, settings.duplex_mode());
-  job_settings->SetBoolean(kSettingLandscape, settings.landscape());
-  job_settings->SetString(kSettingDeviceName, settings.device_name());
-  job_settings->SetInteger(kSettingScaleFactor, 100);
-  job_settings->SetBoolean("rasterizePDF", false);
-
-  job_settings->SetInteger("dpi", settings.dpi());
-  job_settings->SetInteger("dpiHorizontal", 72);
-  job_settings->SetInteger("dpiVertical", 72);
-
-  job_settings->SetBoolean(kSettingPrintToPDF, false);
-  job_settings->SetBoolean(kSettingCloudPrintDialog, false);
-  job_settings->SetBoolean(kSettingPrintWithPrivet, false);
-  job_settings->SetBoolean(kSettingPrintWithExtension, false);
-
-  job_settings->SetBoolean(kSettingShowSystemDialog, false);
-  job_settings->SetInteger(kSettingPreviewPageCount, 1);
-}
-
-class PrintingContextDelegate : public PrintingContext::Delegate {
- public:
-  PrintingContextDelegate(int render_process_id, int render_frame_id);
-  ~PrintingContextDelegate() override;
-
-  gfx::NativeView GetParentView() override;
-  std::string GetAppLocale() override;
-
-  // Not exposed to PrintingContext::Delegate because of dependency issues.
-  content::WebContents* GetWebContents();
-
- private:
-  const int render_process_id_;
-  const int render_frame_id_;
-};
-
-PrintingContextDelegate::PrintingContextDelegate(int render_process_id,
-                                                 int render_frame_id)
-    : render_process_id_(render_process_id),
-      render_frame_id_(render_frame_id) {}
-
-PrintingContextDelegate::~PrintingContextDelegate() {}
-
-gfx::NativeView PrintingContextDelegate::GetParentView() {
-  content::WebContents* wc = GetWebContents();
-  return wc ? wc->GetNativeView() : nullptr;
-}
-
-content::WebContents* PrintingContextDelegate::GetWebContents() {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-  auto* rfh =
-      content::RenderFrameHost::FromID(render_process_id_, render_frame_id_);
-  return rfh ? content::WebContents::FromRenderFrameHost(rfh) : nullptr;
-}
-
-std::string PrintingContextDelegate::GetAppLocale() {
-  return g_browser_process->GetApplicationLocale();
-}
-
-void NotificationCallback(PrintJobWorkerOwner* print_job,
-                          JobEventDetails::Type detail_type,
-                          int job_id,
-                          PrintedDocument* document) {
-  JobEventDetails* details = new JobEventDetails(detail_type, job_id, document);
-  content::NotificationService::current()->Notify(
-      chrome::NOTIFICATION_PRINT_JOB_EVENT,
-      // We know that is is a PrintJob object in this circumstance.
-      content::Source<PrintJob>(static_cast<PrintJob*>(print_job)),
-      content::Details<JobEventDetails>(details));
-}
-
-void PostOnOwnerThread(const scoped_refptr<PrintJobWorkerOwner>& owner,
-                       PrintingContext::PrintSettingsCallback callback,
-                       PrintingContext::Result result) {
-  owner->PostTask(FROM_HERE,
-                  base::BindOnce(&HoldRefCallback, owner,
-                                 base::BindOnce(std::move(callback), result)));
-}
-
-#if defined(OS_WIN)
-void PageNotificationCallback(PrintJobWorkerOwner* print_job,
-                              JobEventDetails::Type detail_type,
-                              int job_id,
-                              PrintedDocument* document,
-                              PrintedPage* page) {
-  JobEventDetails* details =
-      new JobEventDetails(detail_type, job_id, document, page);
-  content::NotificationService::current()->Notify(
-      chrome::NOTIFICATION_PRINT_JOB_EVENT,
-      // We know that is is a PrintJob object in this circumstance.
-      content::Source<PrintJob>(static_cast<PrintJob*>(print_job)),
-      content::Details<JobEventDetails>(details));
-}
-#endif
-
-}  // namespace
-
-PrintJobWorker::PrintJobWorker(int render_process_id,
-                               int render_frame_id,
-                               PrintJobWorkerOwner* owner)
-    : owner_(owner), thread_("Printing_Worker"), weak_factory_(this) {
-  // The object is created in the IO thread.
-  DCHECK(owner_->RunsTasksInCurrentSequence());
-
-  printing_context_delegate_ = std::make_unique<PrintingContextDelegate>(
-      render_process_id, render_frame_id);
-  printing_context_ = PrintingContext::Create(printing_context_delegate_.get());
-}
-
-PrintJobWorker::~PrintJobWorker() {
-  // The object is normally deleted in the UI thread, but when the user
-  // cancels printing or in the case of print preview, the worker is destroyed
-  // on the I/O thread.
-  DCHECK(owner_->RunsTasksInCurrentSequence());
-  Stop();
-}
-
-void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) {
-  DCHECK(page_number_ == PageNumber::npos());
-  owner_ = new_owner;
-}
-
-void PrintJobWorker::GetSettings(bool ask_user_for_settings,
-                                 int document_page_count,
-                                 bool has_selection,
-                                 MarginType margin_type,
-                                 bool is_scripted,
-                                 bool is_modifiable,
-                                 const base::string16& device_name) {
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-  DCHECK_EQ(page_number_, PageNumber::npos());
-
-  // Recursive task processing is needed for the dialog in case it needs to be
-  // destroyed by a task.
-  // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed
-  // on the thread where the PrintDlgEx is called, and definitely both calls
-  // should happen on the same thread. See http://crbug.com/73466
-  // MessageLoop::current()->SetNestableTasksAllowed(true);
-  printing_context_->set_margin_type(margin_type);
-  printing_context_->set_is_modifiable(is_modifiable);
-
-  // When we delegate to a destination, we don't ask the user for settings.
-  // TODO(mad): Ask the destination for settings.
-  if (ask_user_for_settings) {
-    BrowserThread::PostTask(
-        BrowserThread::UI, FROM_HERE,
-        base::BindOnce(
-            &HoldRefCallback, WrapRefCounted(owner_),
-            base::BindOnce(&PrintJobWorker::GetSettingsWithUI,
-                           base::Unretained(this), document_page_count,
-                           has_selection, is_scripted)));
-  } else if (!device_name.empty()) {
-    BrowserThread::PostTask(
-        BrowserThread::UI, FROM_HERE,
-        base::BindOnce(&HoldRefCallback, WrapRefCounted(owner_),
-                       base::BindOnce(&PrintJobWorker::InitWithDeviceName,
-                                      base::Unretained(this), device_name)));
-  } else {
-    BrowserThread::PostTask(
-        BrowserThread::UI, FROM_HERE,
-        base::BindOnce(&HoldRefCallback, WrapRefCounted(owner_),
-                       base::BindOnce(&PrintJobWorker::UseDefaultSettings,
-                                      base::Unretained(this))));
-  }
-}
-
-void PrintJobWorker::SetSettings(
-    std::unique_ptr<base::DictionaryValue> new_settings) {
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-
-  BrowserThread::PostTask(
-      BrowserThread::UI, FROM_HERE,
-      base::BindOnce(
-          &HoldRefCallback, WrapRefCounted(owner_),
-          base::BindOnce(&PrintJobWorker::UpdatePrintSettings,
-                         base::Unretained(this), base::Passed(&new_settings))));
-}
-
-void PrintJobWorker::UpdatePrintSettings(
-    std::unique_ptr<base::DictionaryValue> new_settings) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-  PrintingContext::Result result =
-      printing_context_->UpdatePrintSettings(*new_settings);
-  GetSettingsDone(result);
-}
-
-void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) {
-  // Most PrintingContext functions may start a message loop and process
-  // message recursively, so disable recursive task processing.
-  // TODO(thestig): See above comment. SetNestableTasksAllowed(false) needs to
-  // be called on the same thread as the previous call.  See
-  // http://crbug.com/73466
-  // MessageLoop::current()->SetNestableTasksAllowed(false);
-
-  // We can't use OnFailure() here since owner_ may not support notifications.
-
-  // PrintJob will create the new PrintedDocument.
-  owner_->PostTask(
-      FROM_HERE,
-      base::Bind(&PrintJobWorkerOwner::GetSettingsDone, WrapRefCounted(owner_),
-                 printing_context_->settings(), result));
-}
-
-void PrintJobWorker::GetSettingsWithUI(int document_page_count,
-                                       bool has_selection,
-                                       bool is_scripted) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-
-  // weak_factory_ creates pointers valid only on owner_ thread.
-  printing_context_->AskUserForSettings(
-      document_page_count, has_selection, is_scripted,
-      base::BindOnce(&PostOnOwnerThread, WrapRefCounted(owner_),
-                     base::BindOnce(&PrintJobWorker::GetSettingsDone,
-                                    weak_factory_.GetWeakPtr())));
-}
-
-void PrintJobWorker::UseDefaultSettings() {
-  PrintingContext::Result result = printing_context_->UseDefaultSettings();
-  GetSettingsDone(result);
-}
-
-void PrintJobWorker::InitWithDeviceName(const base::string16& device_name) {
-  const auto& settings = printing_context_->settings();
-  std::unique_ptr<base::DictionaryValue> dic(new base::DictionaryValue);
-  PrintSettingsToJobSettings(settings, dic.get());
-  dic->SetString(kSettingDeviceName, device_name);
-  UpdatePrintSettings(std::move(dic));
-}
-
-void PrintJobWorker::StartPrinting(PrintedDocument* new_document) {
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-  DCHECK_EQ(page_number_, PageNumber::npos());
-  DCHECK_EQ(document_.get(), new_document);
-  DCHECK(document_.get());
-
-  if (!document_.get() || page_number_ != PageNumber::npos() ||
-      document_.get() != new_document) {
-    return;
-  }
-
-  base::string16 document_name =
-      printing::SimplifyDocumentTitle(document_->name());
-  PrintingContext::Result result =
-      printing_context_->NewDocument(document_name);
-  if (result != PrintingContext::OK) {
-    OnFailure();
-    return;
-  }
-
-  // Try to print already cached data. It may already have been generated for
-  // the print preview.
-  OnNewPage();
-  // Don't touch this anymore since the instance could be destroyed. It happens
-  // if all the pages are printed a one sweep and the client doesn't have a
-  // handle to us anymore. There's a timing issue involved between the worker
-  // thread and the UI thread. Take no chance.
-}
-
-void PrintJobWorker::OnDocumentChanged(PrintedDocument* new_document) {
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-  DCHECK_EQ(page_number_, PageNumber::npos());
-
-  if (page_number_ != PageNumber::npos())
-    return;
-
-  document_ = new_document;
-}
-
-void PrintJobWorker::PostWaitForPage() {
-  // We need to wait for the page to be available.
-  base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
-      FROM_HERE,
-      base::BindOnce(&PrintJobWorker::OnNewPage, weak_factory_.GetWeakPtr()),
-      base::TimeDelta::FromMilliseconds(500));
-}
-
-#if defined(OS_WIN)
-void PrintJobWorker::OnNewPage() {
-  if (!document_.get())  // Spurious message.
-    return;
-
-  // message_loop() could return NULL when the print job is cancelled.
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-
-  if (page_number_ == PageNumber::npos()) {
-    // Find first page to print.
-    int page_count = document_->page_count();
-    if (!page_count) {
-      // We still don't know how many pages the document contains. We can't
-      // start to print the document yet since the header/footer may refer to
-      // the document's page count.
-      return;
-    }
-    // We have enough information to initialize page_number_.
-    page_number_.Init(document_->settings(), page_count);
-  }
-
-  DCHECK_NE(page_number_, PageNumber::npos());
-  while (true) {
-    // Is the page available?
-    scoped_refptr<PrintedPage> page = document_->GetPage(page_number_.ToInt());
-    if (!page.get()) {
-      PostWaitForPage();
-      break;
-    }
-    // The page is there, print it.
-    SpoolPage(page.get());
-    ++page_number_;
-    if (page_number_ == PageNumber::npos()) {
-      OnDocumentDone();
-      // Don't touch this anymore since the instance could be destroyed.
-      break;
-    }
-  }
-}
-#else
-void PrintJobWorker::OnNewPage() {
-  if (!document_.get())  // Spurious message.
-    return;
-
-  // message_loop() could return NULL when the print job is cancelled.
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-
-  const MetafilePlayer* metafile = document_->GetMetafile();
-  if (!metafile) {
-    PostWaitForPage();
-    return;
-  }
-  SpoolJob();
-  // Don't touch this anymore since the instance could be destroyed.
-  OnDocumentDone();
-}
-#endif  // defined(OS_WIN)
-
-void PrintJobWorker::Cancel() {
-  // This is the only function that can be called from any thread.
-  printing_context_->Cancel();
-  // Cannot touch any member variable since we don't know in which thread
-  // context we run.
-}
-
-bool PrintJobWorker::IsRunning() const {
-  return thread_.IsRunning();
-}
-
-bool PrintJobWorker::PostTask(const base::Location& from_here,
-                              const base::Closure& task) {
-  if (task_runner_.get())
-    return task_runner_->PostTask(from_here, task);
-  return false;
-}
-
-void PrintJobWorker::StopSoon() {
-  thread_.StopSoon();
-}
-
-void PrintJobWorker::Stop() {
-  thread_.Stop();
-}
-
-bool PrintJobWorker::Start() {
-  bool result = thread_.Start();
-  task_runner_ = thread_.task_runner();
-  return result;
-}
-
-void PrintJobWorker::OnDocumentDone() {
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-  DCHECK_EQ(page_number_, PageNumber::npos());
-  DCHECK(document_.get());
-
-  int job_id = printing_context_->job_id();
-  if (printing_context_->DocumentDone() != PrintingContext::OK) {
-    OnFailure();
-    return;
-  }
-
-  owner_->PostTask(FROM_HERE,
-                   base::Bind(&NotificationCallback, base::RetainedRef(owner_),
-                              JobEventDetails::DOC_DONE, job_id,
-                              base::RetainedRef(document_)));
-
-  // Makes sure the variables are reinitialized.
-  document_ = NULL;
-}
-
-#if defined(OS_WIN)
-void PrintJobWorker::SpoolPage(PrintedPage* page) {
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-  DCHECK_NE(page_number_, PageNumber::npos());
-
-  // Preprocess.
-  if (printing_context_->NewPage() != PrintingContext::OK) {
-    OnFailure();
-    return;
-  }
-
-  // Actual printing.
-  document_->RenderPrintedPage(*page, printing_context_->context());
-
-  // Postprocess.
-  if (printing_context_->PageDone() != PrintingContext::OK) {
-    OnFailure();
-    return;
-  }
-
-  // Signal everyone that the page is printed.
-  owner_->PostTask(FROM_HERE,
-                   base::BindRepeating(
-                       &PageNotificationCallback, base::RetainedRef(owner_),
-                       JobEventDetails::PAGE_DONE, printing_context_->job_id(),
-                       base::RetainedRef(document_), base::RetainedRef(page)));
-}
-#else
-void PrintJobWorker::SpoolJob() {
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-  if (!document_->RenderPrintedDocument(printing_context_.get()))
-    OnFailure();
-}
-#endif
-
-void PrintJobWorker::OnFailure() {
-  DCHECK(task_runner_->RunsTasksInCurrentSequence());
-
-  // We may loose our last reference by broadcasting the FAILED event.
-  scoped_refptr<PrintJobWorkerOwner> handle(owner_);
-
-  owner_->PostTask(
-      FROM_HERE, base::BindRepeating(
-                     &NotificationCallback, base::RetainedRef(owner_),
-                     JobEventDetails::FAILED, 0, base::RetainedRef(document_)));
-  Cancel();
-
-  // Makes sure the variables are reinitialized.
-  document_ = NULL;
-  page_number_ = PageNumber::npos();
-}
-
-}  // namespace printing

+ 0 - 174
chromium_src/chrome/browser/printing/print_job_worker.h

@@ -1,174 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
-#define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
-
-#include <memory>
-
-#include "base/location.h"
-#include "base/macros.h"
-#include "base/memory/ref_counted.h"
-#include "base/memory/weak_ptr.h"
-#include "base/threading/thread.h"
-#include "build/build_config.h"
-#include "chrome/browser/printing/printer_query.h"
-#include "content/public/browser/browser_thread.h"
-#include "printing/page_number.h"
-#include "printing/print_job_constants.h"
-#include "printing/printing_context.h"
-
-namespace base {
-class DictionaryValue;
-}
-
-namespace printing {
-
-class PrintJob;
-class PrintJobWorkerOwner;
-class PrintedDocument;
-class PrintedPage;
-
-// Worker thread code. It manages the PrintingContext, which can be blocking
-// and/or run a message loop. This is the object that generates most
-// NOTIFY_PRINT_JOB_EVENT notifications, but they are generated through a
-// NotificationTask task to be executed from the right thread, the UI thread.
-// PrintJob always outlives its worker instance.
-class PrintJobWorker {
- public:
-  PrintJobWorker(int render_process_id,
-                 int render_frame_id,
-                 PrintJobWorkerOwner* owner);
-  virtual ~PrintJobWorker();
-
-  void SetNewOwner(PrintJobWorkerOwner* new_owner);
-
-  // Initializes the print settings. If |ask_user_for_settings| is true, a
-  // Print... dialog box will be shown to ask the user their preference.
-  // |is_scripted| should be true for calls coming straight from window.print().
-  // |is_modifiable| implies HTML and not other formats like PDF.
-  void GetSettings(bool ask_user_for_settings,
-                   int document_page_count,
-                   bool has_selection,
-                   MarginType margin_type,
-                   bool is_scripted,
-                   bool is_modifiable,
-                   const base::string16& device_name);
-
-  // Set the new print settings.
-  void SetSettings(std::unique_ptr<base::DictionaryValue> new_settings);
-
-  // Starts the printing loop. Every pages are printed as soon as the data is
-  // available. Makes sure the new_document is the right one.
-  void StartPrinting(PrintedDocument* new_document);
-
-  // Updates the printed document.
-  void OnDocumentChanged(PrintedDocument* new_document);
-
-  // Dequeues waiting pages. Called when PrintJob receives a
-  // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
-  // the next page can be printed.
-  void OnNewPage();
-
-  // This is the only function that can be called in a thread.
-  void Cancel();
-
-  // Returns true if the thread has been started, and not yet stopped.
-  bool IsRunning() const;
-
-  // Posts the given task to be run.
-  bool PostTask(const base::Location& from_here, const base::Closure& task);
-
-  // Signals the thread to exit in the near future.
-  void StopSoon();
-
-  // Signals the thread to exit and returns once the thread has exited.
-  void Stop();
-
-  // Starts the thread.
-  bool Start();
-
- protected:
-  // Retrieves the context for testing only.
-  PrintingContext* printing_context() { return printing_context_.get(); }
-
- private:
-  // The shared NotificationService service can only be accessed from the UI
-  // thread, so this class encloses the necessary information to send the
-  // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
-  // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
-  // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
-  class NotificationTask;
-
-  // Posts a task to call OnNewPage(). Used to wait for pages/document to be
-  // available.
-  void PostWaitForPage();
-
-#if defined(OS_WIN)
-  // Renders a page in the printer.
-  void SpoolPage(PrintedPage* page);
-#else
-  // Renders the document to the printer.
-  void SpoolJob();
-#endif
-
-  // Closes the job since spooling is done.
-  void OnDocumentDone();
-
-  // Discards the current document, the current page and cancels the printing
-  // context.
-  void OnFailure();
-
-  // Asks the user for print settings. Must be called on the UI thread.
-  // Required on Mac and Linux. Windows can display UI from non-main threads,
-  // but sticks with this for consistency.
-  void GetSettingsWithUI(int document_page_count,
-                         bool has_selection,
-                         bool is_scripted);
-
-  // Called on the UI thread to update the print settings.
-  void UpdatePrintSettings(std::unique_ptr<base::DictionaryValue> new_settings);
-
-  // Reports settings back to owner_.
-  void GetSettingsDone(PrintingContext::Result result);
-
-  // Use the default settings. When using GTK+ or Mac, this can still end up
-  // displaying a dialog. So this needs to happen from the UI thread on these
-  // systems.
-  void UseDefaultSettings();
-
-  // set the printer name
-  void InitWithDeviceName(const base::string16& device_name);
-
-  // Printing context delegate.
-  std::unique_ptr<PrintingContext::Delegate> printing_context_delegate_;
-
-  // Information about the printer setting.
-  std::unique_ptr<PrintingContext> printing_context_;
-
-  // The printed document. Only has read-only access.
-  scoped_refptr<PrintedDocument> document_;
-
-  // The print job owning this worker thread. It is guaranteed to outlive this
-  // object.
-  PrintJobWorkerOwner* owner_;
-
-  // Current page number to print.
-  PageNumber page_number_;
-
-  // Thread to run worker tasks.
-  base::Thread thread_;
-
-  // Tread-safe pointer to task runner of the |thread_|.
-  scoped_refptr<base::SequencedTaskRunner> task_runner_;
-
-  // Used to generate a WeakPtr for callbacks.
-  base::WeakPtrFactory<PrintJobWorker> weak_factory_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_

+ 0 - 27
chromium_src/chrome/browser/printing/print_job_worker_owner.cc

@@ -1,27 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/print_job_worker_owner.h"
-
-#include "base/location.h"
-#include "base/single_thread_task_runner.h"
-#include "base/threading/thread_task_runner_handle.h"
-
-namespace printing {
-
-PrintJobWorkerOwner::PrintJobWorkerOwner()
-    : task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
-
-PrintJobWorkerOwner::~PrintJobWorkerOwner() {}
-
-bool PrintJobWorkerOwner::RunsTasksInCurrentSequence() const {
-  return task_runner_->RunsTasksInCurrentSequence();
-}
-
-bool PrintJobWorkerOwner::PostTask(const base::Location& from_here,
-                                   base::OnceClosure task) {
-  return task_runner_->PostTask(from_here, std::move(task));
-}
-
-}  // namespace printing

+ 0 - 65
chromium_src/chrome/browser/printing/print_job_worker_owner.h

@@ -1,65 +0,0 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_OWNER_H__
-#define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_OWNER_H__
-
-#include <memory>
-
-#include "base/location.h"
-#include "base/memory/ref_counted.h"
-#include "printing/printing_context.h"
-
-namespace base {
-class MessageLoop;
-class SequencedTaskRunner;
-}  // namespace base
-
-namespace printing {
-
-class PrintJobWorker;
-class PrintSettings;
-
-class PrintJobWorkerOwner
-    : public base::RefCountedThreadSafe<PrintJobWorkerOwner> {
- public:
-  PrintJobWorkerOwner();
-
-  // Finishes the initialization began by PrintJobWorker::GetSettings().
-  // Creates a new PrintedDocument if necessary. Solely meant to be called by
-  // PrintJobWorker.
-  virtual void GetSettingsDone(const PrintSettings& new_settings,
-                               PrintingContext::Result result) = 0;
-
-  // Detach the PrintJobWorker associated to this object.
-  virtual std::unique_ptr<PrintJobWorker> DetachWorker(
-      PrintJobWorkerOwner* new_owner) = 0;
-
-  // Access the current settings.
-  virtual const PrintSettings& settings() const = 0;
-
-  // Cookie uniquely identifying the PrintedDocument and/or loaded settings.
-  virtual int cookie() const = 0;
-
-  // Returns true if the current thread is a thread on which a task
-  // may be run, and false if no task will be run on the current
-  // thread.
-  bool RunsTasksInCurrentSequence() const;
-
-  // Posts the given task to be run.
-  bool PostTask(const base::Location& from_here, base::OnceClosure task);
-
- protected:
-  friend class base::RefCountedThreadSafe<PrintJobWorkerOwner>;
-
-  virtual ~PrintJobWorkerOwner();
-
-  // Task runner reference. Used to send notifications in the right
-  // thread.
-  scoped_refptr<base::SequencedTaskRunner> task_runner_;
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_OWNER_H__

+ 0 - 142
chromium_src/chrome/browser/printing/print_preview_message_handler.cc

@@ -1,142 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/print_preview_message_handler.h"
-
-#include "base/bind.h"
-#include "base/memory/shared_memory.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/printing/print_job_manager.h"
-#include "chrome/browser/printing/printer_query.h"
-#include "chrome/common/print_messages.h"
-#include "content/public/browser/browser_thread.h"
-#include "content/public/browser/render_frame_host.h"
-#include "content/public/browser/render_view_host.h"
-#include "content/public/browser/web_contents.h"
-#include "printing/page_size_margins.h"
-#include "printing/pdf_metafile_skia.h"
-#include "printing/print_job_constants.h"
-
-#include "atom/common/node_includes.h"
-
-using content::BrowserThread;
-using content::WebContents;
-
-DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintPreviewMessageHandler);
-
-namespace {
-
-void StopWorker(int document_cookie) {
-  if (document_cookie <= 0)
-    return;
-  scoped_refptr<printing::PrintQueriesQueue> queue =
-      g_browser_process->print_job_manager()->queue();
-  scoped_refptr<printing::PrinterQuery> printer_query =
-      queue->PopPrinterQuery(document_cookie);
-  if (printer_query.get()) {
-    BrowserThread::PostTask(
-        BrowserThread::IO, FROM_HERE,
-        base::Bind(&printing::PrinterQuery::StopWorker, printer_query));
-  }
-}
-
-char* CopyPDFDataOnIOThread(
-    const PrintHostMsg_DidPreviewDocument_Params& params) {
-  DCHECK_CURRENTLY_ON(BrowserThread::IO);
-  std::unique_ptr<base::SharedMemory> shared_buf(
-      new base::SharedMemory(params.metafile_data_handle, true));
-  if (!shared_buf->Map(params.data_size))
-    return nullptr;
-  char* pdf_data = new char[params.data_size];
-  memcpy(pdf_data, shared_buf->memory(), params.data_size);
-  return pdf_data;
-}
-
-void FreeNodeBufferData(char* data, void* hint) {
-  delete[] data;
-}
-
-}  // namespace
-
-namespace printing {
-
-PrintPreviewMessageHandler::PrintPreviewMessageHandler(
-    WebContents* web_contents)
-    : content::WebContentsObserver(web_contents) {
-  DCHECK(web_contents);
-}
-
-PrintPreviewMessageHandler::~PrintPreviewMessageHandler() {}
-
-void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
-    const PrintHostMsg_DidPreviewDocument_Params& params) {
-  // Always try to stop the worker.
-  StopWorker(params.document_cookie);
-
-  if (params.expected_pages_count <= 0) {
-    NOTREACHED();
-    return;
-  }
-
-  BrowserThread::PostTaskAndReplyWithResult(
-      BrowserThread::IO, FROM_HERE, base::Bind(&CopyPDFDataOnIOThread, params),
-      base::Bind(&PrintPreviewMessageHandler::RunPrintToPDFCallback,
-                 base::Unretained(this), params.preview_request_id,
-                 params.data_size));
-}
-
-void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
-                                                      int request_id) {
-  StopWorker(document_cookie);
-  RunPrintToPDFCallback(request_id, 0, nullptr);
-}
-
-bool PrintPreviewMessageHandler::OnMessageReceived(
-    const IPC::Message& message,
-    content::RenderFrameHost* render_frame_host) {
-  bool handled = true;
-  IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting,
-                        OnMetafileReadyForPrinting)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, OnPrintPreviewFailed)
-    IPC_MESSAGE_UNHANDLED(handled = false)
-  IPC_END_MESSAGE_MAP()
-  return handled;
-}
-
-void PrintPreviewMessageHandler::PrintToPDF(
-    const base::DictionaryValue& options,
-    const atom::api::WebContents::PrintToPDFCallback& callback) {
-  int request_id;
-  options.GetInteger(printing::kPreviewRequestID, &request_id);
-  print_to_pdf_callback_map_[request_id] = callback;
-
-  content::RenderFrameHost* rfh = web_contents()->GetMainFrame();
-  rfh->Send(new PrintMsg_PrintPreview(rfh->GetRoutingID(), options));
-}
-
-void PrintPreviewMessageHandler::RunPrintToPDFCallback(int request_id,
-                                                       uint32_t data_size,
-                                                       char* data) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-
-  v8::Isolate* isolate = v8::Isolate::GetCurrent();
-  v8::Locker locker(isolate);
-  v8::HandleScope handle_scope(isolate);
-  if (data) {
-    v8::Local<v8::Value> buffer =
-        node::Buffer::New(isolate, data, static_cast<size_t>(data_size),
-                          &FreeNodeBufferData, nullptr)
-            .ToLocalChecked();
-    print_to_pdf_callback_map_[request_id].Run(v8::Null(isolate), buffer);
-  } else {
-    v8::Local<v8::String> error_message =
-        v8::String::NewFromUtf8(isolate, "Failed to generate PDF");
-    print_to_pdf_callback_map_[request_id].Run(
-        v8::Exception::Error(error_message), v8::Null(isolate));
-  }
-  print_to_pdf_callback_map_.erase(request_id);
-}
-
-}  // namespace printing

+ 0 - 60
chromium_src/chrome/browser/printing/print_preview_message_handler.h

@@ -1,60 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
-#define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
-
-#include <map>
-
-#include "atom/browser/api/atom_api_web_contents.h"
-#include "base/compiler_specific.h"
-#include "content/public/browser/web_contents_observer.h"
-#include "content/public/browser/web_contents_user_data.h"
-
-struct PrintHostMsg_DidPreviewDocument_Params;
-
-namespace content {
-class WebContents;
-}
-
-namespace printing {
-
-struct PageSizeMargins;
-
-// Manages the print preview handling for a WebContents.
-class PrintPreviewMessageHandler
-    : public content::WebContentsObserver,
-      public content::WebContentsUserData<PrintPreviewMessageHandler> {
- public:
-  ~PrintPreviewMessageHandler() override;
-
-  // content::WebContentsObserver implementation.
-  bool OnMessageReceived(const IPC::Message& message,
-                         content::RenderFrameHost* render_frame_host) override;
-
-  void PrintToPDF(const base::DictionaryValue& options,
-                  const atom::api::WebContents::PrintToPDFCallback& callback);
-
- private:
-  typedef std::map<int, atom::api::WebContents::PrintToPDFCallback>
-      PrintToPDFCallbackMap;
-
-  explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
-  friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
-
-  // Message handlers.
-  void OnMetafileReadyForPrinting(
-      const PrintHostMsg_DidPreviewDocument_Params& params);
-  void OnPrintPreviewFailed(int document_cookie, int request_id);
-
-  void RunPrintToPDFCallback(int request_id, uint32_t data_size, char* data);
-
-  PrintToPDFCallbackMap print_to_pdf_callback_map_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_

+ 0 - 508
chromium_src/chrome/browser/printing/print_view_manager_base.cc

@@ -1,508 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/print_view_manager_base.h"
-
-#include <memory>
-
-#include "base/bind.h"
-#include "base/memory/ref_counted_memory.h"
-#include "base/message_loop/message_loop.h"
-#include "base/run_loop.h"
-#include "base/strings/utf_string_conversions.h"
-#include "base/timer/timer.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/chrome_notification_types.h"
-#include "chrome/browser/printing/print_job.h"
-#include "chrome/browser/printing/print_job_manager.h"
-#include "chrome/browser/printing/printer_query.h"
-#include "chrome/browser/profiles/profile.h"
-#include "chrome/browser/ui/simple_message_box.h"
-#include "chrome/common/pref_names.h"
-#include "chrome/common/print_messages.h"
-#include "components/prefs/pref_service.h"
-#include "content/public/browser/browser_thread.h"
-#include "content/public/browser/notification_details.h"
-#include "content/public/browser/notification_service.h"
-#include "content/public/browser/notification_source.h"
-#include "content/public/browser/render_frame_host.h"
-#include "content/public/browser/render_view_host.h"
-#include "content/public/browser/web_contents.h"
-#include "printing/pdf_metafile_skia.h"
-#include "printing/printed_document.h"
-#include "ui/base/l10n/l10n_util.h"
-
-#if defined(ENABLE_FULL_PRINTING)
-#include "chrome/browser/printing/print_error_dialog.h"
-#endif
-
-using base::TimeDelta;
-using content::BrowserThread;
-
-namespace printing {
-
-namespace {}  // namespace
-
-PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
-    : content::WebContentsObserver(web_contents),
-      number_pages_(0),
-      printing_succeeded_(false),
-      inside_inner_message_loop_(false),
-      cookie_(0),
-      queue_(g_browser_process->print_job_manager()->queue()) {
-  DCHECK(queue_.get());
-  expecting_first_page_ = true;
-  printing_enabled_ = true;
-}
-
-PrintViewManagerBase::~PrintViewManagerBase() {
-  ReleasePrinterQuery();
-  DisconnectFromCurrentPrintJob();
-}
-
-#if !defined(DISABLE_BASIC_PRINTING)
-bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh,
-                                    bool silent,
-                                    bool print_background,
-                                    const base::string16& device_name) {
-  int32_t id = rfh->GetRoutingID();
-  return PrintNowInternal(rfh, std::make_unique<PrintMsg_PrintPages>(
-                                   id, silent, print_background, device_name));
-}
-#endif  // !DISABLE_BASIC_PRINTING
-
-void PrintViewManagerBase::NavigationStopped() {
-  // Cancel the current job, wait for the worker to finish.
-  TerminatePrintJob(true);
-}
-
-void PrintViewManagerBase::RenderProcessGone(base::TerminationStatus status) {
-  ReleasePrinterQuery();
-
-  if (!print_job_.get())
-    return;
-
-  scoped_refptr<PrintedDocument> document(print_job_->document());
-  if (document.get()) {
-    // If IsComplete() returns false, the document isn't completely rendered.
-    // Since our renderer is gone, there's nothing to do, cancel it. Otherwise,
-    // the print job may finish without problem.
-    TerminatePrintJob(!document->IsComplete());
-  }
-}
-
-base::string16 PrintViewManagerBase::RenderSourceName() {
-  base::string16 name(web_contents()->GetTitle());
-  return name;
-}
-
-void PrintViewManagerBase::OnDidGetPrintedPagesCount(int cookie,
-                                                     int number_pages) {
-  DCHECK_GT(cookie, 0);
-  DCHECK_GT(number_pages, 0);
-  number_pages_ = number_pages;
-  OpportunisticallyCreatePrintJob(cookie);
-}
-
-void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) {
-  cookie_ = cookie;
-}
-
-void PrintViewManagerBase::OnDidPrintPage(
-    const PrintHostMsg_DidPrintPage_Params& params) {
-// TODO(rbpotter): Remove this check once there are no more spurious
-// DidPrintPage messages.
-#if !defined(OS_WIN)
-  if (!expecting_first_page_)
-    return;
-#endif
-
-  if (!OpportunisticallyCreatePrintJob(params.document_cookie))
-    return;
-
-  PrintedDocument* document = print_job_->document();
-  if (!document || params.document_cookie != document->cookie()) {
-    // Out of sync. It may happen since we are completely asynchronous. Old
-    // spurious messages can be received if one of the processes is overloaded.
-    return;
-  }
-
-  const bool metafile_must_be_valid = expecting_first_page_;
-  expecting_first_page_ = false;
-
-  base::SharedMemory shared_buf(params.metafile_data_handle, true);
-  if (metafile_must_be_valid) {
-    if (!shared_buf.Map(params.data_size)) {
-      NOTREACHED() << "couldn't map";
-      web_contents()->Stop();
-      return;
-    }
-  }
-
-  auto metafile = std::make_unique<PdfMetafileSkia>();
-  if (metafile_must_be_valid) {
-    if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) {
-      NOTREACHED() << "Invalid metafile header";
-      web_contents()->Stop();
-      return;
-    }
-  }
-
-#if !defined(OS_WIN)
-  // Update the rendered document. It will send notifications to the listener.
-  document->SetDocument(std::move(metafile), params.page_size,
-                        params.content_area);
-
-  ShouldQuitFromInnerMessageLoop();
-#else
-  print_job_->AppendPrintedPage(params.page_number);
-  if (metafile_must_be_valid) {
-    bool print_text_with_gdi = document->settings().print_text_with_gdi() &&
-                               !document->settings().printer_is_xps();
-
-    scoped_refptr<base::RefCountedBytes> bytes = new base::RefCountedBytes(
-        reinterpret_cast<const unsigned char*>(shared_buf.memory()),
-        params.data_size);
-
-    document->DebugDumpData(bytes.get(), FILE_PATH_LITERAL(".pdf"));
-    print_job_->StartPdfToEmfConversion(
-        bytes, params.page_size, params.content_area, print_text_with_gdi);
-  }
-#endif  // !OS_WIN
-}
-
-void PrintViewManagerBase::OnPrintingFailed(int cookie) {
-  if (cookie != cookie_) {
-    NOTREACHED();
-    return;
-  }
-
-  ReleasePrinterQuery();
-
-  content::NotificationService::current()->Notify(
-      chrome::NOTIFICATION_PRINT_JOB_RELEASED,
-      content::Source<content::WebContents>(web_contents()),
-      content::NotificationService::NoDetails());
-}
-
-void PrintViewManagerBase::OnShowInvalidPrinterSettingsError() {
-  LOG(ERROR) << "Invalid printer settings";
-}
-
-bool PrintViewManagerBase::OnMessageReceived(
-    const IPC::Message& message,
-    content::RenderFrameHost* render_frame_host) {
-  bool handled = true;
-  IPC_BEGIN_MESSAGE_MAP(PrintViewManagerBase, message)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount,
-                        OnDidGetPrintedPagesCount)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie,
-                        OnDidGetDocumentCookie)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError,
-                        OnShowInvalidPrinterSettingsError);
-    IPC_MESSAGE_UNHANDLED(handled = false)
-  IPC_END_MESSAGE_MAP()
-  return handled;
-}
-
-void PrintViewManagerBase::Observe(
-    int type,
-    const content::NotificationSource& source,
-    const content::NotificationDetails& details) {
-  switch (type) {
-    case chrome::NOTIFICATION_PRINT_JOB_EVENT: {
-      OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr());
-      break;
-    }
-    default: {
-      NOTREACHED();
-      break;
-    }
-  }
-}
-
-void PrintViewManagerBase::OnNotifyPrintJobEvent(
-    const JobEventDetails& event_details) {
-  switch (event_details.type()) {
-    case JobEventDetails::FAILED: {
-      TerminatePrintJob(true);
-
-      content::NotificationService::current()->Notify(
-          chrome::NOTIFICATION_PRINT_JOB_RELEASED,
-          content::Source<content::WebContents>(web_contents()),
-          content::NotificationService::NoDetails());
-      break;
-    }
-    case JobEventDetails::USER_INIT_DONE:
-    case JobEventDetails::DEFAULT_INIT_DONE:
-    case JobEventDetails::USER_INIT_CANCELED: {
-      NOTREACHED();
-      break;
-    }
-    case JobEventDetails::ALL_PAGES_REQUESTED: {
-      ShouldQuitFromInnerMessageLoop();
-      break;
-    }
-    case JobEventDetails::NEW_DOC:
-    case JobEventDetails::NEW_PAGE:
-    case JobEventDetails::PAGE_DONE:
-    case JobEventDetails::DOC_DONE: {
-      // Don't care about the actual printing process.
-      break;
-    }
-    case JobEventDetails::JOB_DONE: {
-      // Printing is done, we don't need it anymore.
-      // print_job_->is_job_pending() may still be true, depending on the order
-      // of object registration.
-      printing_succeeded_ = true;
-      ReleasePrintJob();
-
-      content::NotificationService::current()->Notify(
-          chrome::NOTIFICATION_PRINT_JOB_RELEASED,
-          content::Source<content::WebContents>(web_contents()),
-          content::NotificationService::NoDetails());
-      break;
-    }
-    default: {
-      NOTREACHED();
-      break;
-    }
-  }
-}
-
-bool PrintViewManagerBase::RenderAllMissingPagesNow() {
-  if (!print_job_.get() || !print_job_->is_job_pending())
-    return false;
-
-  // We can't print if there is no renderer.
-  if (!web_contents() || !web_contents()->GetRenderViewHost() ||
-      !web_contents()->GetRenderViewHost()->IsRenderViewLive()) {
-    return false;
-  }
-
-  // Is the document already complete?
-  if (print_job_->document() && print_job_->document()->IsComplete()) {
-    printing_succeeded_ = true;
-    return true;
-  }
-
-  // WebContents is either dying or a second consecutive request to print
-  // happened before the first had time to finish. We need to render all the
-  // pages in an hurry if a print_job_ is still pending. No need to wait for it
-  // to actually spool the pages, only to have the renderer generate them. Run
-  // a message loop until we get our signal that the print job is satisfied.
-  // PrintJob will send a ALL_PAGES_REQUESTED after having received all the
-  // pages it needs. MessageLoop::current()->Quit() will be called as soon as
-  // print_job_->document()->IsComplete() is true on either ALL_PAGES_REQUESTED
-  // or in DidPrintPage(). The check is done in
-  // ShouldQuitFromInnerMessageLoop().
-  // BLOCKS until all the pages are received. (Need to enable recursive task)
-  if (!RunInnerMessageLoop()) {
-    // This function is always called from DisconnectFromCurrentPrintJob() so we
-    // know that the job will be stopped/canceled in any case.
-    return false;
-  }
-  return true;
-}
-
-void PrintViewManagerBase::ShouldQuitFromInnerMessageLoop() {
-  // Look at the reason.
-  DCHECK(print_job_->document());
-  if (print_job_->document() && print_job_->document()->IsComplete() &&
-      inside_inner_message_loop_) {
-    // We are in a message loop created by RenderAllMissingPagesNow. Quit from
-    // it.
-    base::RunLoop::QuitCurrentWhenIdleDeprecated();
-    inside_inner_message_loop_ = false;
-  }
-}
-
-bool PrintViewManagerBase::CreateNewPrintJob(PrintJobWorkerOwner* job) {
-  DCHECK(!inside_inner_message_loop_);
-
-  // Disconnect the current print_job_.
-  DisconnectFromCurrentPrintJob();
-
-  // We can't print if there is no renderer.
-  if (!web_contents()->GetRenderViewHost() ||
-      !web_contents()->GetRenderViewHost()->IsRenderViewLive()) {
-    return false;
-  }
-
-  // Ask the renderer to generate the print preview, create the print preview
-  // view and switch to it, initialize the printer and show the print dialog.
-  DCHECK(!print_job_.get());
-  DCHECK(job);
-  if (!job)
-    return false;
-
-  print_job_ = new PrintJob();
-  print_job_->Initialize(job, RenderSourceName(), number_pages_);
-  registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
-                 content::Source<PrintJob>(print_job_.get()));
-  printing_succeeded_ = false;
-  return true;
-}
-
-void PrintViewManagerBase::DisconnectFromCurrentPrintJob() {
-  // Make sure all the necessary rendered page are done. Don't bother with the
-  // return value.
-  bool result = RenderAllMissingPagesNow();
-
-  // Verify that assertion.
-  if (print_job_.get() && print_job_->document() &&
-      !print_job_->document()->IsComplete()) {
-    DCHECK(!result);
-    // That failed.
-    TerminatePrintJob(true);
-  } else {
-    // DO NOT wait for the job to finish.
-    ReleasePrintJob();
-  }
-  expecting_first_page_ = true;
-}
-
-void PrintViewManagerBase::PrintingDone(bool success) {
-  auto* host = web_contents()->GetRenderViewHost();
-  if (print_job_.get()) {
-    if (host)
-      host->Send(new PrintMsg_PrintingDone(host->GetRoutingID(), success));
-  }
-  if (!callback.is_null()) {
-    callback.Run(success && print_job_);
-  }
-}
-
-void PrintViewManagerBase::TerminatePrintJob(bool cancel) {
-  if (!print_job_.get())
-    return;
-
-  if (cancel) {
-    // We don't need the metafile data anymore because the printing is canceled.
-    print_job_->Cancel();
-    inside_inner_message_loop_ = false;
-  } else {
-    DCHECK(!inside_inner_message_loop_);
-    DCHECK(!print_job_->document() || print_job_->document()->IsComplete());
-
-    // WebContents is either dying or navigating elsewhere. We need to render
-    // all the pages in an hurry if a print job is still pending. This does the
-    // trick since it runs a blocking message loop:
-    print_job_->Stop();
-  }
-  ReleasePrintJob();
-}
-
-void PrintViewManagerBase::ReleasePrintJob() {
-  if (!print_job_.get())
-    return;
-
-  PrintingDone(printing_succeeded_);
-
-  registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
-                    content::Source<PrintJob>(print_job_.get()));
-  // Don't close the worker thread.
-  print_job_ = NULL;
-}
-
-bool PrintViewManagerBase::RunInnerMessageLoop() {
-  // This value may actually be too low:
-  //
-  // - If we're looping because of printer settings initialization, the premise
-  // here is that some poor users have their print server away on a VPN over a
-  // slow connection. In this situation, the simple fact of opening the printer
-  // can be dead slow. On the other side, we don't want to die infinitely for a
-  // real network error. Give the printer 60 seconds to comply.
-  //
-  // - If we're looping because of renderer page generation, the renderer could
-  // be CPU bound, the page overly complex/large or the system just
-  // memory-bound.
-  static const int kPrinterSettingsTimeout = 60000;
-  base::OneShotTimer quit_timer;
-  base::RunLoop run_loop;
-  quit_timer.Start(FROM_HERE,
-                   TimeDelta::FromMilliseconds(kPrinterSettingsTimeout),
-                   run_loop.QuitWhenIdleClosure());
-
-  inside_inner_message_loop_ = true;
-
-  // Need to enable recursive task.
-  {
-    base::MessageLoop::ScopedNestableTaskAllower allow;
-    base::RunLoop().Run();
-  }
-
-  bool success = true;
-  if (inside_inner_message_loop_) {
-    // Ok we timed out. That's sad.
-    inside_inner_message_loop_ = false;
-    success = false;
-  }
-
-  return success;
-}
-
-bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
-  if (print_job_.get())
-    return true;
-
-  if (!cookie) {
-    // Out of sync. It may happens since we are completely asynchronous. Old
-    // spurious message can happen if one of the processes is overloaded.
-    return false;
-  }
-
-  // The job was initiated by a script. Time to get the corresponding worker
-  // thread.
-  scoped_refptr<PrinterQuery> queued_query = queue_->PopPrinterQuery(cookie);
-  if (!queued_query.get()) {
-    NOTREACHED();
-    return false;
-  }
-
-  if (!CreateNewPrintJob(queued_query.get())) {
-    // Don't kill anything.
-    return false;
-  }
-
-  // Settings are already loaded. Go ahead. This will set
-  // print_job_->is_job_pending() to true.
-  print_job_->StartPrinting();
-  return true;
-}
-
-bool PrintViewManagerBase::PrintNowInternal(
-    content::RenderFrameHost* rfh,
-    std::unique_ptr<IPC::Message> message) {
-  // Don't print / print preview interstitials or crashed tabs.
-  if (web_contents()->ShowingInterstitialPage() || web_contents()->IsCrashed())
-    return false;
-  return rfh->Send(message.release());
-}
-
-void PrintViewManagerBase::ReleasePrinterQuery() {
-  if (!cookie_)
-    return;
-
-  int cookie = cookie_;
-  cookie_ = 0;
-
-  printing::PrintJobManager* print_job_manager =
-      g_browser_process->print_job_manager();
-  // May be NULL in tests.
-  if (!print_job_manager)
-    return;
-
-  scoped_refptr<printing::PrinterQuery> printer_query;
-  printer_query = queue_->PopPrinterQuery(cookie);
-  if (!printer_query.get())
-    return;
-  BrowserThread::PostTask(
-      BrowserThread::IO, FROM_HERE,
-      base::Bind(&PrinterQuery::StopWorker, printer_query.get()));
-}
-
-}  // namespace printing

+ 0 - 171
chromium_src/chrome/browser/printing/print_view_manager_base.h

@@ -1,171 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASE_H_
-#define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASE_H_
-
-#include <memory>
-
-#include "base/callback.h"
-#include "base/memory/ref_counted.h"
-#include "base/strings/string16.h"
-#include "components/prefs/pref_member.h"
-#include "content/public/browser/notification_observer.h"
-#include "content/public/browser/notification_registrar.h"
-#include "content/public/browser/web_contents_observer.h"
-#include "content/public/browser/web_contents_user_data.h"
-
-struct PrintHostMsg_DidPrintPage_Params;
-
-namespace content {
-class RenderViewHost;
-}
-
-namespace printing {
-
-class JobEventDetails;
-class MetafilePlayer;
-class PrintJob;
-class PrintJobWorkerOwner;
-class PrintQueriesQueue;
-
-// Base class for managing the print commands for a WebContents.
-class PrintViewManagerBase : public content::NotificationObserver,
-                             public content::WebContentsObserver {
- public:
-  ~PrintViewManagerBase() override;
-
-#if !defined(DISABLE_BASIC_PRINTING)
-  // Prints the current document immediately. Since the rendering is
-  // asynchronous, the actual printing will not be completed on the return of
-  // this function. Returns false if printing is impossible at the moment.
-  virtual bool PrintNow(content::RenderFrameHost* rfh,
-                        bool silent,
-                        bool print_background,
-                        const base::string16& device_name);
-#endif  // !DISABLE_BASIC_PRINTING
-
-  // PrintedPagesSource implementation.
-  base::string16 RenderSourceName();
-
-  void SetCallback(const base::Callback<void(bool)>& cb) { callback = cb; };
-
- protected:
-  explicit PrintViewManagerBase(content::WebContents* web_contents);
-
-  // Helper method for Print*Now().
-  bool PrintNowInternal(content::RenderFrameHost* rfh,
-                        std::unique_ptr<IPC::Message> message);
-
-  // Terminates or cancels the print job if one was pending.
-  void RenderProcessGone(base::TerminationStatus status) override;
-
-  // content::WebContentsObserver implementation.
-  bool OnMessageReceived(const IPC::Message& message,
-                         content::RenderFrameHost* render_frame_host) override;
-
-  // IPC Message handlers.
-  virtual void OnPrintingFailed(int cookie);
-
- private:
-  // content::NotificationObserver implementation.
-  void Observe(int type,
-               const content::NotificationSource& source,
-               const content::NotificationDetails& details) override;
-
-  // Cancels the print job.
-  void NavigationStopped() override;
-
-  // IPC Message handlers.
-  void OnDidGetPrintedPagesCount(int cookie, int number_pages);
-  void OnDidGetDocumentCookie(int cookie);
-  void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params);
-  void OnShowInvalidPrinterSettingsError();
-
-  // Processes a NOTIFY_PRINT_JOB_EVENT notification.
-  void OnNotifyPrintJobEvent(const JobEventDetails& event_details);
-
-  // Requests the RenderView to render all the missing pages for the print job.
-  // No-op if no print job is pending. Returns true if at least one page has
-  // been requested to the renderer.
-  bool RenderAllMissingPagesNow();
-
-  // Quits the current message loop if these conditions hold true: a document is
-  // loaded and is complete and waiting_for_pages_to_be_rendered_ is true. This
-  // function is called in DidPrintPage() or on ALL_PAGES_REQUESTED
-  // notification. The inner message loop is created was created by
-  // RenderAllMissingPagesNow().
-  void ShouldQuitFromInnerMessageLoop();
-
-  // Creates a new empty print job. It has no settings loaded. If there is
-  // currently a print job, safely disconnect from it. Returns false if it is
-  // impossible to safely disconnect from the current print job or it is
-  // impossible to create a new print job.
-  bool CreateNewPrintJob(PrintJobWorkerOwner* job);
-
-  // Makes sure the current print_job_ has all its data before continuing, and
-  // disconnect from it.
-  void DisconnectFromCurrentPrintJob();
-
-  // Notify that the printing is done.
-  void PrintingDone(bool success);
-
-  // Terminates the print job. No-op if no print job has been created. If
-  // |cancel| is true, cancel it instead of waiting for the job to finish. Will
-  // call ReleasePrintJob().
-  void TerminatePrintJob(bool cancel);
-
-  // Releases print_job_. Correctly deregisters from notifications. No-op if
-  // no print job has been created.
-  void ReleasePrintJob();
-
-  // Runs an inner message loop. It will set inside_inner_message_loop_ to true
-  // while the blocking inner message loop is running. This is useful in cases
-  // where the RenderView is about to be destroyed while a printing job isn't
-  // finished.
-  bool RunInnerMessageLoop();
-
-  // In the case of Scripted Printing, where the renderer is controlling the
-  // control flow, print_job_ is initialized whenever possible. No-op is
-  // print_job_ is initialized.
-  bool OpportunisticallyCreatePrintJob(int cookie);
-
-  // Release the PrinterQuery associated with our |cookie_|.
-  void ReleasePrinterQuery();
-
-  content::NotificationRegistrar registrar_;
-
-  // Manages the low-level talk to the printer.
-  scoped_refptr<PrintJob> print_job_;
-
-  // Number of pages to print in the print job.
-  int number_pages_;
-
-  // Indication of success of the print job.
-  bool printing_succeeded_;
-
-  // Running an inner message loop inside RenderAllMissingPagesNow(). This means
-  // we are _blocking_ until all the necessary pages have been rendered or the
-  // print settings are being loaded.
-  bool inside_inner_message_loop_;
-
-  // Set to true when OnDidPrintPage() should be expecting the first page.
-  bool expecting_first_page_;
-
-  // The document cookie of the current PrinterQuery.
-  int cookie_;
-
-  // Whether printing is enabled.
-  bool printing_enabled_;
-
-  scoped_refptr<printing::PrintQueriesQueue> queue_;
-
-  base::Callback<void(bool)> callback;
-
-  DISALLOW_COPY_AND_ASSIGN(PrintViewManagerBase);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASE_H_

+ 0 - 46
chromium_src/chrome/browser/printing/print_view_manager_basic.cc

@@ -1,46 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/print_view_manager_basic.h"
-
-#if defined(OS_ANDROID)
-#include "base/file_descriptor_posix.h"
-#include "chrome/common/print_messages.h"
-#include "printing/printing_context_android.h"
-#endif
-
-DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintViewManagerBasic);
-
-namespace printing {
-
-PrintViewManagerBasic::PrintViewManagerBasic(content::WebContents* web_contents)
-    : PrintViewManagerBase(web_contents) {}
-
-PrintViewManagerBasic::~PrintViewManagerBasic() {}
-
-#if defined(OS_ANDROID)
-void PrintViewManagerBasic::RenderProcessGone(base::TerminationStatus status) {
-  PrintingContextAndroid::PdfWritingDone(file_descriptor_.fd, false);
-  file_descriptor_ = base::FileDescriptor(-1, false);
-  PrintViewManagerBase::RenderProcessGone(status);
-}
-
-void PrintViewManagerBasic::OnPrintingFailed(int cookie) {
-  PrintingContextAndroid::PdfWritingDone(file_descriptor_.fd, false);
-  file_descriptor_ = base::FileDescriptor(-1, false);
-  PrintViewManagerBase::OnPrintingFailed(cookie);
-}
-
-bool PrintViewManagerBasic::OnMessageReceived(const IPC::Message& message) {
-  bool handled = true;
-  IPC_BEGIN_MESSAGE_MAP(PrintViewManagerBasic, message)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed)
-    IPC_MESSAGE_UNHANDLED(handled = false)
-  IPC_END_MESSAGE_MAP()
-
-  return handled ? true : PrintViewManagerBase::OnMessageReceived(message);
-}
-#endif
-
-}  // namespace printing

+ 0 - 57
chromium_src/chrome/browser/printing/print_view_manager_basic.h

@@ -1,57 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASIC_H_
-#define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASIC_H_
-
-#include "chrome/browser/printing/print_view_manager_base.h"
-#include "content/public/browser/web_contents_user_data.h"
-
-#if defined(OS_ANDROID)
-#include "base/file_descriptor_posix.h"
-#endif
-
-namespace printing {
-
-// Manages the print commands for a WebContents - basic version.
-class PrintViewManagerBasic
-    : public PrintViewManagerBase,
-      public content::WebContentsUserData<PrintViewManagerBasic> {
- public:
-  ~PrintViewManagerBasic() override;
-
-#if defined(OS_ANDROID)
-  // Sets the file descriptor into which the PDF will be written.
-  void set_file_descriptor(const base::FileDescriptor& file_descriptor) {
-    file_descriptor_ = file_descriptor;
-  }
-
-  // Gets the file descriptor into which the PDF will be written.
-  base::FileDescriptor file_descriptor() const { return file_descriptor_; }
-
-  // content::WebContentsObserver implementation.
-  // Terminates or cancels the print job if one was pending.
-  virtual void RenderProcessGone(base::TerminationStatus status) override;
-
-  // content::WebContentsObserver implementation.
-  virtual bool OnMessageReceived(const IPC::Message& message) override;
-#endif
-
- private:
-  explicit PrintViewManagerBasic(content::WebContents* web_contents);
-  friend class content::WebContentsUserData<PrintViewManagerBasic>;
-
-#if defined(OS_ANDROID)
-  virtual void OnPrintingFailed(int cookie) override;
-
-  // The file descriptor into which the PDF of the page will be written.
-  base::FileDescriptor file_descriptor_;
-#endif
-
-  DISALLOW_COPY_AND_ASSIGN(PrintViewManagerBasic);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASIC_H_

+ 0 - 23
chromium_src/chrome/browser/printing/print_view_manager_observer.h

@@ -1,23 +0,0 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_OBSERVER_H_
-#define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_OBSERVER_H_
-
-namespace printing {
-
-// An interface the PrintViewManager uses to notify an observer when the print
-// dialog is shown. Register the observer via PrintViewManager::set_observer.
-class PrintViewManagerObserver {
- public:
-  // Notifies the observer that the print dialog was shown.
-  virtual void OnPrintDialogShown() = 0;
-
- protected:
-  virtual ~PrintViewManagerObserver() {}
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_OBSERVER_H_

+ 0 - 152
chromium_src/chrome/browser/printing/printer_query.cc

@@ -1,152 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/printer_query.h"
-
-#include <memory>
-#include <utility>
-
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/message_loop/message_loop.h"
-#include "base/threading/thread_restrictions.h"
-#include "base/values.h"
-#include "chrome/browser/printing/print_job_worker.h"
-
-namespace printing {
-
-PrinterQuery::PrinterQuery(int render_process_id, int render_frame_id)
-    : worker_(new PrintJobWorker(render_process_id, render_frame_id, this)),
-      is_print_dialog_box_shown_(false),
-      cookie_(PrintSettings::NewCookie()),
-      last_status_(PrintingContext::FAILED) {
-  DCHECK(base::MessageLoopForIO::IsCurrent());
-}
-
-PrinterQuery::~PrinterQuery() {
-  // The job should be finished (or at least canceled) when it is destroyed.
-  DCHECK(!is_print_dialog_box_shown_);
-  // If this fires, it is that this pending printer context has leaked.
-  DCHECK(!worker_);
-}
-
-void PrinterQuery::GetSettingsDone(const PrintSettings& new_settings,
-                                   PrintingContext::Result result) {
-  is_print_dialog_box_shown_ = false;
-  last_status_ = result;
-  if (result != PrintingContext::FAILED) {
-    settings_ = new_settings;
-    cookie_ = PrintSettings::NewCookie();
-  } else {
-    // Failure.
-    cookie_ = 0;
-  }
-
-  if (!callback_.is_null()) {
-    // This may cause reentrancy like to call StopWorker().
-    callback_.Run();
-    callback_.Reset();
-  }
-}
-
-std::unique_ptr<PrintJobWorker> PrinterQuery::DetachWorker(
-    PrintJobWorkerOwner* new_owner) {
-  DCHECK(callback_.is_null());
-  DCHECK(worker_);
-
-  worker_->SetNewOwner(new_owner);
-  return std::move(worker_);
-}
-
-const PrintSettings& PrinterQuery::settings() const {
-  return settings_;
-}
-
-int PrinterQuery::cookie() const {
-  return cookie_;
-}
-
-void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings,
-                               int expected_page_count,
-                               bool has_selection,
-                               MarginType margin_type,
-                               bool is_scripted,
-                               bool is_modifiable,
-                               const base::Closure& callback) {
-  DCHECK(RunsTasksInCurrentSequence());
-  DCHECK(!is_print_dialog_box_shown_ || !is_scripted);
-
-  StartWorker(callback);
-
-  // Real work is done in PrintJobWorker::GetSettings().
-  is_print_dialog_box_shown_ =
-      ask_user_for_settings == GetSettingsAskParam::ASK_USER;
-  worker_->PostTask(
-      FROM_HERE,
-      base::Bind(&PrintJobWorker::GetSettings, base::Unretained(worker_.get()),
-                 is_print_dialog_box_shown_, expected_page_count, has_selection,
-                 margin_type, is_scripted, is_modifiable, base::string16()));
-}
-
-void PrinterQuery::GetSettings(GetSettingsAskParam ask_user_for_settings,
-                               int expected_page_count,
-                               bool has_selection,
-                               MarginType margin_type,
-                               bool is_scripted,
-                               bool is_modifiable,
-                               const base::string16& device_name,
-                               const base::Closure& callback) {
-  DCHECK(RunsTasksInCurrentSequence());
-  DCHECK(!is_print_dialog_box_shown_);
-  StartWorker(callback);
-
-  is_print_dialog_box_shown_ = false;
-  worker_->PostTask(
-      FROM_HERE,
-      base::Bind(&PrintJobWorker::GetSettings, base::Unretained(worker_.get()),
-                 is_print_dialog_box_shown_, expected_page_count, has_selection,
-                 margin_type, is_scripted, is_modifiable, device_name));
-}
-
-void PrinterQuery::SetSettings(
-    std::unique_ptr<base::DictionaryValue> new_settings,
-    const base::Closure& callback) {
-  StartWorker(callback);
-
-  worker_->PostTask(FROM_HERE, base::Bind(&PrintJobWorker::SetSettings,
-                                          base::Unretained(worker_.get()),
-                                          base::Passed(&new_settings)));
-}
-
-void PrinterQuery::StartWorker(const base::Closure& callback) {
-  DCHECK(callback_.is_null());
-  DCHECK(worker_);
-
-  // Lazily create the worker thread. There is one worker thread per print job.
-  if (!worker_->IsRunning())
-    worker_->Start();
-
-  callback_ = callback;
-}
-
-void PrinterQuery::StopWorker() {
-  if (worker_) {
-    // http://crbug.com/66082: We're blocking on the PrinterQuery's worker
-    // thread.  It's not clear to me if this may result in blocking the current
-    // thread for an unacceptable time.  We should probably fix it.
-    base::ThreadRestrictions::ScopedAllowIO allow_io;
-    worker_->Stop();
-    worker_.reset();
-  }
-}
-
-bool PrinterQuery::is_callback_pending() const {
-  return !callback_.is_null();
-}
-
-bool PrinterQuery::is_valid() const {
-  return !!worker_;
-}
-
-}  // namespace printing

+ 0 - 111
chromium_src/chrome/browser/printing/printer_query.h

@@ -1,111 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
-#define CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_
-
-#include <memory>
-
-#include "base/callback.h"
-#include "base/compiler_specific.h"
-#include "base/macros.h"
-#include "chrome/browser/printing/print_job_worker_owner.h"
-#include "printing/print_job_constants.h"
-
-namespace base {
-class DictionaryValue;
-}
-
-namespace printing {
-
-class PrintDestinationInterface;
-class PrintJobWorker;
-
-// Query the printer for settings.
-class PrinterQuery : public PrintJobWorkerOwner {
- public:
-  // GetSettings() UI parameter.
-  enum class GetSettingsAskParam {
-    DEFAULTS,
-    ASK_USER,
-  };
-
-  PrinterQuery(int render_process_id, int render_frame_id);
-
-  // PrintJobWorkerOwner implementation.
-  void GetSettingsDone(const PrintSettings& new_settings,
-                       PrintingContext::Result result) override;
-  std::unique_ptr<PrintJobWorker> DetachWorker(
-      PrintJobWorkerOwner* new_owner) override;
-  const PrintSettings& settings() const override;
-  int cookie() const override;
-
-  // Initializes the printing context. It is fine to call this function multiple
-  // times to reinitialize the settings. |web_contents_observer| can be queried
-  // to find the owner of the print setting dialog box. It is unused when
-  // |ask_for_user_settings| is DEFAULTS.
-  void GetSettings(GetSettingsAskParam ask_user_for_settings,
-                   int expected_page_count,
-                   bool has_selection,
-                   MarginType margin_type,
-                   bool is_scripted,
-                   bool is_modifiable,
-                   const base::Closure& callback);
-
-  void GetSettings(GetSettingsAskParam ask_user_for_settings,
-                   int expected_page_count,
-                   bool has_selection,
-                   MarginType margin_type,
-                   bool is_scripted,
-                   bool is_modifiable,
-                   const base::string16& device_name,
-                   const base::Closure& callback);
-
-  // Updates the current settings with |new_settings| dictionary values.
-  void SetSettings(std::unique_ptr<base::DictionaryValue> new_settings,
-                   const base::Closure& callback);
-
-  // Stops the worker thread since the client is done with this object.
-  void StopWorker();
-
-  // Returns true if a GetSettings() call is pending completion.
-  bool is_callback_pending() const;
-
-  PrintingContext::Result last_status() const { return last_status_; }
-
-  // Returns if a worker thread is still associated to this instance.
-  bool is_valid() const;
-
- private:
-  ~PrinterQuery() override;
-
-  // Lazy create the worker thread. There is one worker thread per print job.
-  void StartWorker(const base::Closure& callback);
-
-  // All the UI is done in a worker thread because many Win32 print functions
-  // are blocking and enters a message loop without your consent. There is one
-  // worker thread per print job.
-  std::unique_ptr<PrintJobWorker> worker_;
-
-  // Cache of the print context settings for access in the UI thread.
-  PrintSettings settings_;
-
-  // Is the Print... dialog box currently shown.
-  bool is_print_dialog_box_shown_;
-
-  // Cookie that make this instance unique.
-  int cookie_;
-
-  // Results from the last GetSettingsDone() callback.
-  PrintingContext::Result last_status_;
-
-  // Callback waiting to be run.
-  base::Closure callback_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrinterQuery);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINTER_QUERY_H_

+ 0 - 328
chromium_src/chrome/browser/printing/printing_message_filter.cc

@@ -1,328 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/printing_message_filter.h"
-
-#include <string>
-
-#include "base/bind.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/printing/print_job_manager.h"
-#include "chrome/browser/printing/printer_query.h"
-#include "chrome/browser/profiles/profile.h"
-#include "chrome/browser/profiles/profile_io_data.h"
-#include "chrome/common/print_messages.h"
-#include "content/public/browser/browser_thread.h"
-#include "content/public/browser/render_frame_host.h"
-#include "content/public/browser/web_contents.h"
-#include "content/public/common/child_process_host.h"
-#include "printing/buildflags/buildflags.h"
-
-#if defined(OS_ANDROID)
-#include "base/strings/string_number_conversions.h"
-#include "chrome/browser/printing/print_view_manager_basic.h"
-#include "printing/printing_context_android.h"
-#endif
-
-using content::BrowserThread;
-
-namespace printing {
-
-namespace {
-
-void RenderParamsFromPrintSettings(const PrintSettings& settings,
-                                   PrintMsg_Print_Params* params) {
-  params->page_size = settings.page_setup_device_units().physical_size();
-  params->content_size.SetSize(
-      settings.page_setup_device_units().content_area().width(),
-      settings.page_setup_device_units().content_area().height());
-  params->printable_area.SetRect(
-      settings.page_setup_device_units().printable_area().x(),
-      settings.page_setup_device_units().printable_area().y(),
-      settings.page_setup_device_units().printable_area().width(),
-      settings.page_setup_device_units().printable_area().height());
-  params->margin_top = settings.page_setup_device_units().content_area().y();
-  params->margin_left = settings.page_setup_device_units().content_area().x();
-  params->dpi = settings.dpi();
-  params->scale_factor = settings.scale_factor();
-  // Always use an invalid cookie.
-  params->document_cookie = 0;
-  params->selection_only = settings.selection_only();
-  params->supports_alpha_blend = settings.supports_alpha_blend();
-  params->should_print_backgrounds = settings.should_print_backgrounds();
-  params->display_header_footer = settings.display_header_footer();
-  params->title = settings.title();
-  params->url = settings.url();
-}
-
-#if defined(OS_ANDROID)
-content::WebContents* GetWebContentsForRenderFrame(int render_process_id,
-                                                   int render_frame_id) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-  content::RenderFrameHost* frame =
-      content::RenderFrameHost::FromID(render_process_id, render_frame_id);
-  return frame ? content::WebContents::FromRenderFrameHost(frame) : nullptr;
-}
-
-PrintViewManagerBasic* GetPrintManager(int render_process_id,
-                                       int render_frame_id) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-  content::WebContents* web_contents =
-      GetWebContentsForRenderFrame(render_process_id, render_frame_id);
-  return web_contents ? PrintViewManagerBasic::FromWebContents(web_contents)
-                      : nullptr;
-}
-#endif
-
-}  // namespace
-
-PrintingMessageFilter::PrintingMessageFilter(int render_process_id)
-    : BrowserMessageFilter(PrintMsgStart),
-      render_process_id_(render_process_id),
-      queue_(g_browser_process->print_job_manager()->queue()) {
-  DCHECK(queue_.get());
-}
-
-PrintingMessageFilter::~PrintingMessageFilter() {}
-
-void PrintingMessageFilter::OnDestruct() const {
-  BrowserThread::DeleteOnUIThread::Destruct(this);
-}
-
-void PrintingMessageFilter::OverrideThreadForMessage(
-    const IPC::Message& message,
-    BrowserThread::ID* thread) {
-#if defined(OS_ANDROID)
-  if (message.type() == PrintHostMsg_AllocateTempFileForPrinting::ID ||
-      message.type() == PrintHostMsg_TempFileForPrintingWritten::ID) {
-    *thread = BrowserThread::UI;
-  }
-#endif
-}
-
-bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) {
-  bool handled = true;
-  IPC_BEGIN_MESSAGE_MAP(PrintingMessageFilter, message)
-#if defined(OS_ANDROID)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting,
-                        OnAllocateTempFileForPrinting)
-    IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten,
-                        OnTempFileForPrintingWritten)
-#endif
-    IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings,
-                                    OnGetDefaultPrintSettings)
-    IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_InitSettingWithDeviceName,
-                                    OnInitSettingWithDeviceName)
-    IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint)
-    IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_UpdatePrintSettings,
-                                    OnUpdatePrintSettings)
-    IPC_MESSAGE_UNHANDLED(handled = false)
-  IPC_END_MESSAGE_MAP()
-  return handled;
-}
-
-#if defined(OS_ANDROID)
-void PrintingMessageFilter::OnAllocateTempFileForPrinting(
-    int render_frame_id,
-    base::FileDescriptor* temp_file_fd,
-    int* sequence_number) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-  PrintViewManagerBasic* print_view_manager =
-      GetPrintManager(render_process_id_, render_frame_id);
-  if (!print_view_manager)
-    return;
-
-  // The file descriptor is originally created in & passed from the Android
-  // side, and it will handle the closing.
-  temp_file_fd->fd = print_view_manager->file_descriptor().fd;
-  temp_file_fd->auto_close = false;
-}
-
-void PrintingMessageFilter::OnTempFileForPrintingWritten(int render_frame_id,
-                                                         int sequence_number) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-  PrintViewManagerBasic* print_view_manager =
-      GetPrintManager(render_process_id_, render_frame_id);
-  if (print_view_manager)
-    print_view_manager->PdfWritingDone(true);
-}
-#endif  // defined(OS_ANDROID)
-
-void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) {
-  DCHECK_CURRENTLY_ON(BrowserThread::IO);
-  scoped_refptr<PrinterQuery> printer_query;
-  if (false) {
-    // Reply with NULL query.
-    OnGetDefaultPrintSettingsReply(printer_query, reply_msg);
-    return;
-  }
-  printer_query = queue_->PopPrinterQuery(0);
-  if (!printer_query.get()) {
-    printer_query =
-        queue_->CreatePrinterQuery(render_process_id_, reply_msg->routing_id());
-  }
-
-  // Loads default settings. This is asynchronous, only the IPC message sender
-  // will hang until the settings are retrieved.
-  printer_query->GetSettings(
-      PrinterQuery::GetSettingsAskParam::DEFAULTS, 0, false, DEFAULT_MARGINS,
-      false, false,
-      base::Bind(&PrintingMessageFilter::OnGetDefaultPrintSettingsReply, this,
-                 printer_query, reply_msg));
-}
-
-void PrintingMessageFilter::OnInitSettingWithDeviceName(
-    const base::string16& device_name,
-    IPC::Message* reply_msg) {
-  DCHECK_CURRENTLY_ON(BrowserThread::IO);
-  scoped_refptr<PrinterQuery> printer_query;
-  printer_query = queue_->PopPrinterQuery(0);
-  if (!printer_query.get()) {
-    printer_query =
-        queue_->CreatePrinterQuery(render_process_id_, reply_msg->routing_id());
-  }
-
-  // Loads default settings. This is asynchronous, only the IPC message sender
-  // will hang until the settings are retrieved.
-  printer_query->GetSettings(
-      PrinterQuery::GetSettingsAskParam::DEFAULTS, 0, false, DEFAULT_MARGINS,
-      true, true, device_name,
-      base::Bind(&PrintingMessageFilter::OnGetDefaultPrintSettingsReply, this,
-                 printer_query, reply_msg));
-}
-
-void PrintingMessageFilter::OnGetDefaultPrintSettingsReply(
-    scoped_refptr<PrinterQuery> printer_query,
-    IPC::Message* reply_msg) {
-  PrintMsg_Print_Params params;
-  if (!printer_query.get() ||
-      printer_query->last_status() != PrintingContext::OK) {
-    params.Reset();
-  } else {
-    RenderParamsFromPrintSettings(printer_query->settings(), &params);
-    params.document_cookie = printer_query->cookie();
-  }
-  PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, params);
-  Send(reply_msg);
-  // If printing was enabled.
-  if (printer_query.get()) {
-    // If user hasn't cancelled.
-    if (printer_query->cookie() && printer_query->settings().dpi()) {
-      queue_->QueuePrinterQuery(printer_query.get());
-    } else {
-      printer_query->StopWorker();
-    }
-  }
-}
-
-void PrintingMessageFilter::OnScriptedPrint(
-    const PrintHostMsg_ScriptedPrint_Params& params,
-    IPC::Message* reply_msg) {
-  scoped_refptr<PrinterQuery> printer_query =
-      queue_->PopPrinterQuery(params.cookie);
-  if (!printer_query.get()) {
-    printer_query =
-        queue_->CreatePrinterQuery(render_process_id_, reply_msg->routing_id());
-  }
-  printer_query->GetSettings(
-      PrinterQuery::GetSettingsAskParam::ASK_USER, params.expected_pages_count,
-      params.has_selection, params.margin_type, true, true,
-      base::Bind(&PrintingMessageFilter::OnScriptedPrintReply, this,
-                 printer_query, reply_msg));
-}
-
-void PrintingMessageFilter::OnScriptedPrintReply(
-    scoped_refptr<PrinterQuery> printer_query,
-    IPC::Message* reply_msg) {
-  PrintMsg_PrintPages_Params params;
-#if defined(OS_ANDROID)
-  // We need to save the routing ID here because Send method below deletes the
-  // |reply_msg| before we can get the routing ID for the Android code.
-  int routing_id = reply_msg->routing_id();
-#endif
-  if (printer_query->last_status() != PrintingContext::OK ||
-      !printer_query->settings().dpi()) {
-    params.Reset();
-  } else {
-    RenderParamsFromPrintSettings(printer_query->settings(), &params.params);
-    params.params.document_cookie = printer_query->cookie();
-    params.pages = PageRange::GetPages(printer_query->settings().ranges());
-  }
-  PrintHostMsg_ScriptedPrint::WriteReplyParams(reply_msg, params);
-  Send(reply_msg);
-  if (params.params.dpi && params.params.document_cookie) {
-#if defined(OS_ANDROID)
-    int file_descriptor;
-    const base::string16& device_name = printer_query->settings().device_name();
-    if (base::StringToInt(device_name, &file_descriptor)) {
-      BrowserThread::PostTask(
-          BrowserThread::UI, FROM_HERE,
-          base::Bind(&PrintingMessageFilter::UpdateFileDescriptor, this,
-                     routing_id, file_descriptor));
-    }
-#endif
-    queue_->QueuePrinterQuery(printer_query.get());
-  } else {
-    printer_query->StopWorker();
-  }
-}
-
-#if defined(OS_ANDROID)
-void PrintingMessageFilter::UpdateFileDescriptor(int render_view_id, int fd) {
-  DCHECK_CURRENTLY_ON(BrowserThread::UI);
-  content::WebContents* wc = GetWebContentsForRenderView(render_view_id);
-  if (!wc)
-    return;
-  PrintViewManagerBasic* print_view_manager =
-      PrintViewManagerBasic::FromWebContents(wc);
-  print_view_manager->set_file_descriptor(base::FileDescriptor(fd, false));
-}
-#endif
-
-void PrintingMessageFilter::OnUpdatePrintSettings(
-    int document_cookie,
-    const base::DictionaryValue& job_settings,
-    IPC::Message* reply_msg) {
-  std::unique_ptr<base::DictionaryValue> new_settings(job_settings.DeepCopy());
-
-  scoped_refptr<PrinterQuery> printer_query;
-  printer_query = queue_->PopPrinterQuery(document_cookie);
-  if (!printer_query.get()) {
-    printer_query = queue_->CreatePrinterQuery(
-        content::ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE);
-  }
-  printer_query->SetSettings(
-      std::move(new_settings),
-      base::Bind(&PrintingMessageFilter::OnUpdatePrintSettingsReply, this,
-                 printer_query, reply_msg));
-}
-
-void PrintingMessageFilter::OnUpdatePrintSettingsReply(
-    scoped_refptr<PrinterQuery> printer_query,
-    IPC::Message* reply_msg) {
-  PrintMsg_PrintPages_Params params;
-  if (!printer_query.get() ||
-      printer_query->last_status() != PrintingContext::OK) {
-    params.Reset();
-  } else {
-    RenderParamsFromPrintSettings(printer_query->settings(), &params.params);
-    params.params.document_cookie = printer_query->cookie();
-    params.pages = PageRange::GetPages(printer_query->settings().ranges());
-  }
-  bool canceled = printer_query.get() &&
-                  (printer_query->last_status() == PrintingContext::CANCEL);
-  PrintHostMsg_UpdatePrintSettings::WriteReplyParams(reply_msg, params,
-                                                     canceled);
-  Send(reply_msg);
-  // If user hasn't cancelled.
-  if (printer_query.get()) {
-    if (printer_query->cookie() && printer_query->settings().dpi()) {
-      queue_->QueuePrinterQuery(printer_query.get());
-    } else {
-      printer_query->StopWorker();
-    }
-  }
-}
-
-}  // namespace printing

+ 0 - 97
chromium_src/chrome/browser/printing/printing_message_filter.h

@@ -1,97 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINTING_MESSAGE_FILTER_H_
-#define CHROME_BROWSER_PRINTING_PRINTING_MESSAGE_FILTER_H_
-
-#include <string>
-
-#include "base/compiler_specific.h"
-#include "content/public/browser/browser_message_filter.h"
-
-struct PrintHostMsg_ScriptedPrint_Params;
-
-namespace base {
-class DictionaryValue;
-class FilePath;
-}  // namespace base
-
-namespace content {
-class WebContents;
-}
-
-namespace printing {
-
-class PrintQueriesQueue;
-class PrinterQuery;
-
-// This class filters out incoming printing related IPC messages for the
-// renderer process on the IPC thread.
-class PrintingMessageFilter : public content::BrowserMessageFilter {
- public:
-  PrintingMessageFilter(int render_process_id);
-
-  // content::BrowserMessageFilter methods.
-  void OverrideThreadForMessage(const IPC::Message& message,
-                                content::BrowserThread::ID* thread) override;
-  bool OnMessageReceived(const IPC::Message& message) override;
-
- private:
-  friend class base::DeleteHelper<PrintingMessageFilter>;
-  friend class content::BrowserThread;
-
-  ~PrintingMessageFilter() override;
-
-  void OnDestruct() const override;
-
-#if defined(OS_ANDROID)
-  // Used to ask the browser allocate a temporary file for the renderer
-  // to fill in resulting PDF in renderer.
-  void OnAllocateTempFileForPrinting(int render_frame_id,
-                                     base::FileDescriptor* temp_file_fd,
-                                     int* sequence_number);
-  void OnTempFileForPrintingWritten(int render_frame_id, int sequence_number);
-
-  // Updates the file descriptor for the PrintViewManagerBasic of a given
-  // render_frame_id.
-  void UpdateFileDescriptor(int render_frame_id, int fd);
-#endif
-
-  // Get the default print setting.
-  void OnGetDefaultPrintSettings(IPC::Message* reply_msg);
-
-  // Set deviceName
-  void OnInitSettingWithDeviceName(const base::string16& device_name,
-                                   IPC::Message* reply_msg);
-
-  void OnGetDefaultPrintSettingsReply(scoped_refptr<PrinterQuery> printer_query,
-                                      IPC::Message* reply_msg);
-
-  // The renderer host have to show to the user the print dialog and returns
-  // the selected print settings. The task is handled by the print worker
-  // thread and the UI thread. The reply occurs on the IO thread.
-  void OnScriptedPrint(const PrintHostMsg_ScriptedPrint_Params& params,
-                       IPC::Message* reply_msg);
-  void OnScriptedPrintReply(scoped_refptr<PrinterQuery> printer_query,
-                            IPC::Message* reply_msg);
-
-  // Modify the current print settings based on |job_settings|. The task is
-  // handled by the print worker thread and the UI thread. The reply occurs on
-  // the IO thread.
-  void OnUpdatePrintSettings(int document_cookie,
-                             const base::DictionaryValue& job_settings,
-                             IPC::Message* reply_msg);
-  void OnUpdatePrintSettingsReply(scoped_refptr<PrinterQuery> printer_query,
-                                  IPC::Message* reply_msg);
-
-  const int render_process_id_;
-
-  scoped_refptr<PrintQueriesQueue> queue_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrintingMessageFilter);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_BROWSER_PRINTING_PRINTING_MESSAGE_FILTER_H_

+ 0 - 19
chromium_src/chrome/browser/printing/printing_ui_web_contents_observer.cc

@@ -1,19 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/printing/printing_ui_web_contents_observer.h"
-
-#include "content/public/browser/browser_thread.h"
-#include "content/public/browser/web_contents.h"
-
-PrintingUIWebContentsObserver::PrintingUIWebContentsObserver(
-    content::WebContents* web_contents)
-    : content::WebContentsObserver(web_contents) {
-  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
-}
-
-gfx::NativeView PrintingUIWebContentsObserver::GetParentView() {
-  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
-  return web_contents() ? web_contents()->GetNativeView() : NULL;
-}

+ 0 - 24
chromium_src/chrome/browser/printing/printing_ui_web_contents_observer.h

@@ -1,24 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_PRINTING_PRINTING_UI_WEB_CONTENTS_OBSERVER_H_
-#define CHROME_BROWSER_PRINTING_PRINTING_UI_WEB_CONTENTS_OBSERVER_H_
-
-#include "content/public/browser/web_contents_observer.h"
-#include "ui/gfx/native_widget_types.h"
-
-// Wrapper used to keep track of the lifetime of a WebContents.
-// Lives on the UI thread.
-class PrintingUIWebContentsObserver : public content::WebContentsObserver {
- public:
-  explicit PrintingUIWebContentsObserver(content::WebContents* web_contents);
-
-  // Return the parent NativeView of the observed WebContents.
-  gfx::NativeView GetParentView();
-
- private:
-  DISALLOW_COPY_AND_ASSIGN(PrintingUIWebContentsObserver);
-};
-
-#endif  // CHROME_BROWSER_PRINTING_PRINTING_UI_WEB_CONTENTS_OBSERVER_H_

+ 0 - 113
chromium_src/chrome/common/chrome_utility_printing_messages.h

@@ -1,113 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Multiply-included message file, so no include guard.
-
-#include <string>
-#include <vector>
-
-#include "base/strings/string16.h"
-#include "build/build_config.h"
-#include "ipc/ipc_message_macros.h"
-#include "ipc/ipc_param_traits.h"
-#include "ipc/ipc_platform_file.h"
-#include "printing/backend/print_backend.h"
-#include "printing/buildflags/buildflags.h"
-#include "printing/page_range.h"
-#include "printing/pdf_render_settings.h"
-#include "printing/pwg_raster_settings.h"
-
-#if defined(OS_WIN)
-#include <windows.h>
-#endif
-
-#define IPC_MESSAGE_START ChromeUtilityPrintingMsgStart
-
-IPC_ENUM_TRAITS_MAX_VALUE(printing::PdfRenderSettings::Mode,
-                          printing::PdfRenderSettings::Mode::LAST)
-
-IPC_STRUCT_TRAITS_BEGIN(printing::PdfRenderSettings)
-  IPC_STRUCT_TRAITS_MEMBER(area)
-  IPC_STRUCT_TRAITS_MEMBER(offsets)
-  IPC_STRUCT_TRAITS_MEMBER(dpi)
-  IPC_STRUCT_TRAITS_MEMBER(autorotate)
-  IPC_STRUCT_TRAITS_MEMBER(mode)
-IPC_STRUCT_TRAITS_END()
-
-IPC_STRUCT_TRAITS_BEGIN(printing::PrinterCapsAndDefaults)
-  IPC_STRUCT_TRAITS_MEMBER(printer_capabilities)
-  IPC_STRUCT_TRAITS_MEMBER(caps_mime_type)
-  IPC_STRUCT_TRAITS_MEMBER(printer_defaults)
-  IPC_STRUCT_TRAITS_MEMBER(defaults_mime_type)
-IPC_STRUCT_TRAITS_END()
-
-IPC_ENUM_TRAITS_MAX_VALUE(printing::ColorModel, printing::PROCESSCOLORMODEL_RGB)
-
-IPC_STRUCT_TRAITS_BEGIN(printing::PrinterSemanticCapsAndDefaults::Paper)
-  IPC_STRUCT_TRAITS_MEMBER(display_name)
-  IPC_STRUCT_TRAITS_MEMBER(vendor_id)
-  IPC_STRUCT_TRAITS_MEMBER(size_um)
-IPC_STRUCT_TRAITS_END()
-
-IPC_STRUCT_TRAITS_BEGIN(printing::PrinterSemanticCapsAndDefaults)
-  IPC_STRUCT_TRAITS_MEMBER(collate_capable)
-  IPC_STRUCT_TRAITS_MEMBER(collate_default)
-  IPC_STRUCT_TRAITS_MEMBER(copies_capable)
-  IPC_STRUCT_TRAITS_MEMBER(duplex_capable)
-  IPC_STRUCT_TRAITS_MEMBER(duplex_default)
-  IPC_STRUCT_TRAITS_MEMBER(color_changeable)
-  IPC_STRUCT_TRAITS_MEMBER(color_default)
-  IPC_STRUCT_TRAITS_MEMBER(color_model)
-  IPC_STRUCT_TRAITS_MEMBER(bw_model)
-  IPC_STRUCT_TRAITS_MEMBER(papers)
-  IPC_STRUCT_TRAITS_MEMBER(default_paper)
-  IPC_STRUCT_TRAITS_MEMBER(dpis)
-  IPC_STRUCT_TRAITS_MEMBER(default_dpi)
-IPC_STRUCT_TRAITS_END()
-
-IPC_ENUM_TRAITS_MAX_VALUE(printing::PwgRasterTransformType,
-                          printing::TRANSFORM_TYPE_LAST)
-
-IPC_STRUCT_TRAITS_BEGIN(printing::PwgRasterSettings)
-  IPC_STRUCT_TRAITS_MEMBER(odd_page_transform)
-  IPC_STRUCT_TRAITS_MEMBER(rotate_all_pages)
-  IPC_STRUCT_TRAITS_MEMBER(reverse_page_order)
-IPC_STRUCT_TRAITS_END()
-
-#if defined(OS_WIN)
-// Reply when the utility process loaded PDF. |page_count| is 0, if loading
-// failed.
-IPC_MESSAGE_CONTROL1(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount,
-                     int /* page_count */)
-
-// Reply when the utility process rendered the PDF page.
-IPC_MESSAGE_CONTROL2(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone,
-                     bool /* success */,
-                     float /* scale_factor */)
-
-// Request that the given font characters be loaded by the browser so it's
-// cached by the OS. Please see
-// PdfToEmfUtilityProcessHostClient::OnPreCacheFontCharacters for details.
-#if 0
-IPC_SYNC_MESSAGE_CONTROL2_0(ChromeUtilityHostMsg_PreCacheFontCharacters,
-                            LOGFONT /* font_data */,
-                            base::string16 /* characters */)
-#endif
-
-// Tell the utility process to start rendering the given PDF into a metafile.
-// Utility process would be alive until
-// ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop message.
-IPC_MESSAGE_CONTROL2(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
-                     IPC::PlatformFileForTransit /* input_file */,
-                     printing::PdfRenderSettings /* settings */)
-
-// Requests conversion of the next page.
-IPC_MESSAGE_CONTROL2(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage,
-                     int /* page_number */,
-                     IPC::PlatformFileForTransit /* output_file */)
-
-// Requests utility process to stop conversion and exit.
-IPC_MESSAGE_CONTROL0(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop)
-
-#endif  // OS_WIN

+ 0 - 66
chromium_src/chrome/common/print_messages.cc

@@ -1,66 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/common/print_messages.h"
-
-#include "base/strings/string16.h"
-#include "ui/gfx/geometry/size.h"
-
-PrintMsg_Print_Params::PrintMsg_Print_Params()
-    : page_size(),
-      content_size(),
-      printable_area(),
-      margin_top(0),
-      margin_left(0),
-      dpi(0),
-      scale_factor(1.0f),
-      document_cookie(0),
-      selection_only(false),
-      supports_alpha_blend(false),
-      preview_ui_id(-1),
-      preview_request_id(0),
-      is_first_request(false),
-      print_scaling_option(blink::kWebPrintScalingOptionSourceSize),
-      print_to_pdf(false),
-      display_header_footer(false),
-      title(),
-      url(),
-      should_print_backgrounds(false) {}
-
-PrintMsg_Print_Params::PrintMsg_Print_Params(const PrintMsg_Print_Params&) =
-    default;
-PrintMsg_Print_Params::~PrintMsg_Print_Params() = default;
-
-void PrintMsg_Print_Params::Reset() {
-  page_size = gfx::Size();
-  content_size = gfx::Size();
-  printable_area = gfx::Rect();
-  margin_top = 0;
-  margin_left = 0;
-  dpi = 0;
-  scale_factor = 1.0f;
-  document_cookie = 0;
-  selection_only = false;
-  supports_alpha_blend = false;
-  preview_ui_id = -1;
-  preview_request_id = 0;
-  is_first_request = false;
-  print_scaling_option = blink::kWebPrintScalingOptionSourceSize;
-  print_to_pdf = false;
-  display_header_footer = false;
-  title = base::string16();
-  url = base::string16();
-  should_print_backgrounds = false;
-}
-
-PrintMsg_PrintPages_Params::PrintMsg_PrintPages_Params() : pages() {}
-PrintMsg_PrintPages_Params::PrintMsg_PrintPages_Params(
-    const PrintMsg_PrintPages_Params&) = default;
-
-PrintMsg_PrintPages_Params::~PrintMsg_PrintPages_Params() = default;
-
-void PrintMsg_PrintPages_Params::Reset() {
-  params.Reset();
-  pages = std::vector<int>();
-}

+ 0 - 314
chromium_src/chrome/common/print_messages.h

@@ -1,314 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// IPC messages for printing.
-// Multiply-included message file, hence no include guard.
-
-#include <string>
-#include <vector>
-
-#include "base/memory/shared_memory.h"
-#include "base/values.h"
-#include "ipc/ipc_message_macros.h"
-#include "printing/page_size_margins.h"
-#include "printing/print_job_constants.h"
-#include "third_party/blink/public/web/web_print_scaling_option.h"
-#include "ui/gfx/geometry/rect.h"
-#include "ui/gfx/native_widget_types.h"
-
-#if defined(OS_WIN)
-#include "ipc/ipc_platform_file.h"
-#include "printing/backend/print_backend.h"
-#include "printing/page_range.h"
-#include "printing/pdf_render_settings.h"
-#endif
-
-#ifndef CHROME_COMMON_PRINT_MESSAGES_H_
-#define CHROME_COMMON_PRINT_MESSAGES_H_
-
-struct PrintMsg_Print_Params {
-  PrintMsg_Print_Params();
-  PrintMsg_Print_Params(const PrintMsg_Print_Params&);
-  ~PrintMsg_Print_Params();
-
-  // Resets the members of the struct to 0.
-  void Reset();
-
-  gfx::Size page_size;
-  gfx::Size content_size;
-  gfx::Rect printable_area;
-  int margin_top;
-  int margin_left;
-  double dpi;
-  double scale_factor;
-  bool rasterize_pdf;
-  int document_cookie;
-  bool selection_only;
-  bool supports_alpha_blend;
-  int32_t preview_ui_id;
-  int preview_request_id;
-  bool is_first_request;
-  blink::WebPrintScalingOption print_scaling_option;
-  bool print_to_pdf;
-  bool display_header_footer;
-  base::string16 title;
-  base::string16 url;
-  bool should_print_backgrounds;
-};
-
-struct PrintMsg_PrintPages_Params {
-  PrintMsg_PrintPages_Params();
-  PrintMsg_PrintPages_Params(const PrintMsg_PrintPages_Params&);
-  ~PrintMsg_PrintPages_Params();
-
-  // Resets the members of the struct to 0.
-  void Reset();
-
-  PrintMsg_Print_Params params;
-  std::vector<int> pages;
-};
-
-#endif  // CHROME_COMMON_PRINT_MESSAGES_H_
-
-#define IPC_MESSAGE_START PrintMsgStart
-
-IPC_ENUM_TRAITS_MAX_VALUE(printing::MarginType, printing::MARGIN_TYPE_LAST)
-IPC_ENUM_TRAITS_MIN_MAX_VALUE(printing::DuplexMode,
-                              printing::UNKNOWN_DUPLEX_MODE,
-                              printing::SHORT_EDGE)
-IPC_ENUM_TRAITS_MAX_VALUE(blink::WebPrintScalingOption,
-                          blink::kWebPrintScalingOptionLast)
-
-// Parameters for a render request.
-IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params)
-  // Physical size of the page, including non-printable margins,
-  // in pixels according to dpi.
-  IPC_STRUCT_TRAITS_MEMBER(page_size)
-
-  // In pixels according to dpi_x and dpi_y.
-  IPC_STRUCT_TRAITS_MEMBER(content_size)
-
-  // Physical printable area of the page in pixels according to dpi.
-  IPC_STRUCT_TRAITS_MEMBER(printable_area)
-
-  // The y-offset of the printable area, in pixels according to dpi.
-  IPC_STRUCT_TRAITS_MEMBER(margin_top)
-
-  // The x-offset of the printable area, in pixels according to dpi.
-  IPC_STRUCT_TRAITS_MEMBER(margin_left)
-
-  // Specifies dots per inch.
-  IPC_STRUCT_TRAITS_MEMBER(dpi)
-
-  // Specifies the scale factor in percent
-  IPC_STRUCT_TRAITS_MEMBER(scale_factor)
-
-  // Cookie for the document to ensure correctness.
-  IPC_STRUCT_TRAITS_MEMBER(document_cookie)
-
-  // Should only print currently selected text.
-  IPC_STRUCT_TRAITS_MEMBER(selection_only)
-
-  // Does the printer support alpha blending?
-  IPC_STRUCT_TRAITS_MEMBER(supports_alpha_blend)
-
-  // *** Parameters below are used only for print preview. ***
-
-  // The print preview ui associated with this request.
-  IPC_STRUCT_TRAITS_MEMBER(preview_ui_id)
-
-  // The id of the preview request.
-  IPC_STRUCT_TRAITS_MEMBER(preview_request_id)
-
-  // True if this is the first preview request.
-  IPC_STRUCT_TRAITS_MEMBER(is_first_request)
-
-  // Specifies the page scaling option for preview printing.
-  IPC_STRUCT_TRAITS_MEMBER(print_scaling_option)
-
-  // True if print to pdf is requested.
-  IPC_STRUCT_TRAITS_MEMBER(print_to_pdf)
-
-  // Specifies if the header and footer should be rendered.
-  IPC_STRUCT_TRAITS_MEMBER(display_header_footer)
-
-  // Title string to be printed as header if requested by the user.
-  IPC_STRUCT_TRAITS_MEMBER(title)
-
-  // URL string to be printed as footer if requested by the user.
-  IPC_STRUCT_TRAITS_MEMBER(url)
-
-  // True if print backgrounds is requested by the user.
-  IPC_STRUCT_TRAITS_MEMBER(should_print_backgrounds)
-IPC_STRUCT_TRAITS_END()
-
-IPC_STRUCT_BEGIN(PrintMsg_PrintPage_Params)
-  // Parameters to render the page as a printed page. It must always be the same
-  // value for all the document.
-  IPC_STRUCT_MEMBER(PrintMsg_Print_Params, params)
-
-  // The page number is the indicator of the square that should be rendered
-  // according to the layout specified in PrintMsg_Print_Params.
-  IPC_STRUCT_MEMBER(int, page_number)
-IPC_STRUCT_END()
-
-IPC_STRUCT_TRAITS_BEGIN(printing::PageSizeMargins)
-  IPC_STRUCT_TRAITS_MEMBER(content_width)
-  IPC_STRUCT_TRAITS_MEMBER(content_height)
-  IPC_STRUCT_TRAITS_MEMBER(margin_left)
-  IPC_STRUCT_TRAITS_MEMBER(margin_right)
-  IPC_STRUCT_TRAITS_MEMBER(margin_top)
-  IPC_STRUCT_TRAITS_MEMBER(margin_bottom)
-IPC_STRUCT_TRAITS_END()
-
-IPC_STRUCT_TRAITS_BEGIN(PrintMsg_PrintPages_Params)
-  // Parameters to render the page as a printed page. It must always be the same
-  // value for all the document.
-  IPC_STRUCT_TRAITS_MEMBER(params)
-
-  // If empty, this means a request to render all the printed pages.
-  IPC_STRUCT_TRAITS_MEMBER(pages)
-IPC_STRUCT_TRAITS_END()
-
-// Parameters to describe a rendered page.
-IPC_STRUCT_BEGIN(PrintHostMsg_DidPrintPage_Params)
-  // A shared memory handle to the EMF data. This data can be quite large so a
-  // memory map needs to be used.
-  IPC_STRUCT_MEMBER(base::SharedMemoryHandle, metafile_data_handle)
-
-  // Size of the metafile data.
-  IPC_STRUCT_MEMBER(uint32_t, data_size)
-
-  // Cookie for the document to ensure correctness.
-  IPC_STRUCT_MEMBER(int, document_cookie)
-
-  // Page number.
-  IPC_STRUCT_MEMBER(int, page_number)
-
-  // Shrink factor used to render this page.
-  IPC_STRUCT_MEMBER(double, actual_shrink)
-
-  // The size of the page the page author specified.
-  IPC_STRUCT_MEMBER(gfx::Size, page_size)
-
-  // The printable area the page author specified.
-  IPC_STRUCT_MEMBER(gfx::Rect, content_area)
-IPC_STRUCT_END()
-
-// Parameters for the IPC message ViewHostMsg_ScriptedPrint
-IPC_STRUCT_BEGIN(PrintHostMsg_ScriptedPrint_Params)
-  IPC_STRUCT_MEMBER(int, cookie)
-  IPC_STRUCT_MEMBER(int, expected_pages_count)
-  IPC_STRUCT_MEMBER(bool, has_selection)
-  IPC_STRUCT_MEMBER(printing::MarginType, margin_type)
-IPC_STRUCT_END()
-
-// Parameters to describe a rendered document.
-IPC_STRUCT_BEGIN(PrintHostMsg_DidPreviewDocument_Params)
-  // A shared memory handle to metafile data.
-  IPC_STRUCT_MEMBER(base::SharedMemoryHandle, metafile_data_handle)
-
-  // Size of metafile data.
-  IPC_STRUCT_MEMBER(uint32_t, data_size)
-
-  // Cookie for the document to ensure correctness.
-  IPC_STRUCT_MEMBER(int, document_cookie)
-
-  // Store the expected pages count.
-  IPC_STRUCT_MEMBER(int, expected_pages_count)
-
-  // Whether the preview can be modified.
-  IPC_STRUCT_MEMBER(bool, modifiable)
-
-  // The id of the preview request.
-  IPC_STRUCT_MEMBER(int, preview_request_id)
-IPC_STRUCT_END()
-
-// Messages sent from the browser to the renderer.
-
-// Tells the render view to switch the CSS to print media type, renders every
-// requested pages and switch back the CSS to display media type.
-IPC_MESSAGE_ROUTED3(PrintMsg_PrintPages,
-                    bool /* silent print */,
-                    bool /* print page's background */,
-                    base::string16 /* device name*/)
-
-// Tells the render view that printing is done so it can clean up.
-IPC_MESSAGE_ROUTED1(PrintMsg_PrintingDone, bool /* success */)
-
-// Tells the render view to switch the CSS to print media type, renders every
-// requested pages for print preview using the given |settings|. This gets
-// called multiple times as the user updates settings.
-IPC_MESSAGE_ROUTED1(PrintMsg_PrintPreview, base::DictionaryValue /* settings */)
-
-// Messages sent from the renderer to the browser.
-
-#if defined(OS_WIN)
-// Duplicates a shared memory handle from the renderer to the browser. Then
-// the renderer can flush the handle.
-IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_DuplicateSection,
-                           base::SharedMemoryHandle /* renderer handle */,
-                           base::SharedMemoryHandle /* browser handle */)
-#endif
-
-// Tells the browser that the renderer is done calculating the number of
-// rendered pages according to the specified settings.
-IPC_MESSAGE_ROUTED2(PrintHostMsg_DidGetPrintedPagesCount,
-                    int /* rendered document cookie */,
-                    int /* number of rendered pages */)
-
-// Sends the document cookie of the current printer query to the browser.
-IPC_MESSAGE_ROUTED1(PrintHostMsg_DidGetDocumentCookie,
-                    int /* rendered document cookie */)
-
-// Tells the browser that the print dialog has been shown.
-IPC_MESSAGE_ROUTED0(PrintHostMsg_DidShowPrintDialog)
-
-// Sends back to the browser the rendered "printed page" that was requested by
-// a ViewMsg_PrintPage message or from scripted printing. The memory handle in
-// this message is already valid in the browser process.
-IPC_MESSAGE_ROUTED1(PrintHostMsg_DidPrintPage,
-                    PrintHostMsg_DidPrintPage_Params /* page content */)
-
-// The renderer wants to know the default print settings.
-IPC_SYNC_MESSAGE_ROUTED0_1(PrintHostMsg_GetDefaultPrintSettings,
-                           PrintMsg_Print_Params /* default_settings */)
-
-// you can set the printer
-IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_InitSettingWithDeviceName,
-                           base::string16, /* device name */
-                           PrintMsg_Print_Params /* default_settings */)
-
-// The renderer wants to update the current print settings with new
-// |job_settings|.
-IPC_SYNC_MESSAGE_ROUTED2_2(PrintHostMsg_UpdatePrintSettings,
-                           int /* document_cookie */,
-                           base::DictionaryValue /* job_settings */,
-                           PrintMsg_PrintPages_Params /* current_settings */,
-                           bool /* canceled */)
-
-// It's the renderer that controls the printing process when it is generated
-// by javascript. This step is about showing UI to the user to select the
-// final print settings. The output parameter is the same as
-// ViewMsg_PrintPages which is executed implicitly.
-IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_ScriptedPrint,
-                           PrintHostMsg_ScriptedPrint_Params,
-                           PrintMsg_PrintPages_Params
-                           /* settings chosen by the user*/)
-
-// This is sent when there are invalid printer settings.
-IPC_MESSAGE_ROUTED0(PrintHostMsg_ShowInvalidPrinterSettingsError)
-
-// Tell the browser printing failed.
-IPC_MESSAGE_ROUTED1(PrintHostMsg_PrintingFailed, int /* document cookie */)
-
-// Sends back to the browser the complete rendered document (non-draft mode,
-// used for printing) that was requested by a PrintMsg_PrintPreview message.
-// The memory handle in this message is already valid in the browser process.
-IPC_MESSAGE_ROUTED1(PrintHostMsg_MetafileReadyForPrinting,
-                    PrintHostMsg_DidPreviewDocument_Params /* params */)
-
-IPC_MESSAGE_ROUTED2(PrintHostMsg_PrintPreviewFailed,
-                    int /* document cookie */,
-                    int /* request_id */);

+ 0 - 1425
chromium_src/chrome/renderer/printing/print_web_view_helper.cc

@@ -1,1425 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/renderer/printing/print_web_view_helper.h"
-
-#include <algorithm>
-#include <string>
-
-#include "base/auto_reset.h"
-#include "base/command_line.h"
-#include "base/json/json_writer.h"
-#include "base/logging.h"
-#include "base/message_loop/message_loop.h"
-#include "base/metrics/histogram_macros.h"
-#include "base/process/process_handle.h"
-#include "base/strings/string_number_conversions.h"
-#include "base/strings/stringprintf.h"
-#include "base/strings/utf_string_conversions.h"
-#include "chrome/common/print_messages.h"
-#include "content/public/common/web_preferences.h"
-#include "content/public/renderer/render_frame.h"
-#include "content/public/renderer/render_thread.h"
-#include "content/public/renderer/render_view.h"
-#include "net/base/escape.h"
-#include "printing/pdf_metafile_skia.h"
-#include "printing/units.h"
-#include "third_party/blink/public/mojom/page/page_visibility_state.mojom.h"
-#include "third_party/blink/public/platform/web_double_size.h"
-#include "third_party/blink/public/platform/web_size.h"
-#include "third_party/blink/public/platform/web_url_request.h"
-#include "third_party/blink/public/web/web_console_message.h"
-#include "third_party/blink/public/web/web_document.h"
-#include "third_party/blink/public/web/web_element.h"
-#include "third_party/blink/public/web/web_frame_widget.h"
-#include "third_party/blink/public/web/web_local_frame.h"
-#include "third_party/blink/public/web/web_local_frame_client.h"
-#include "third_party/blink/public/web/web_plugin.h"
-#include "third_party/blink/public/web/web_plugin_document.h"
-#include "third_party/blink/public/web/web_print_params.h"
-#include "third_party/blink/public/web/web_print_scaling_option.h"
-#include "third_party/blink/public/web/web_script_source.h"
-#include "third_party/blink/public/web/web_settings.h"
-#include "third_party/blink/public/web/web_view.h"
-#include "third_party/blink/public/web/web_view_client.h"
-#include "third_party/skia/include/core/SkCanvas.h"
-#include "ui/base/resource/resource_bundle.h"
-
-using content::WebPreferences;
-
-namespace printing {
-
-namespace {
-
-const double kMinDpi = 1.0;
-
-int GetDPI(const PrintMsg_Print_Params* print_params) {
-#if defined(OS_MACOSX)
-  // On the Mac, the printable area is in points, don't do any scaling based
-  // on dpi.
-  return kPointsPerInch;
-#else
-  return static_cast<int>(print_params->dpi);
-#endif  // defined(OS_MACOSX)
-}
-
-bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) {
-  return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() &&
-         !params.printable_area.IsEmpty() && params.document_cookie &&
-         params.dpi && params.margin_top >= 0 && params.margin_left >= 0 &&
-         params.dpi > kMinDpi && params.document_cookie != 0;
-}
-
-PrintMsg_Print_Params GetCssPrintParams(
-    blink::WebLocalFrame* frame,
-    int page_index,
-    const PrintMsg_Print_Params& page_params) {
-  PrintMsg_Print_Params page_css_params = page_params;
-  int dpi = GetDPI(&page_params);
-
-  blink::WebDoubleSize page_size_in_pixels(
-      ConvertUnitDouble(page_params.page_size.width(), dpi, kPixelsPerInch),
-      ConvertUnitDouble(page_params.page_size.height(), dpi, kPixelsPerInch));
-  int margin_top_in_pixels =
-      ConvertUnit(page_params.margin_top, dpi, kPixelsPerInch);
-  int margin_right_in_pixels = ConvertUnit(
-      page_params.page_size.width() - page_params.content_size.width() -
-          page_params.margin_left,
-      dpi, kPixelsPerInch);
-  int margin_bottom_in_pixels = ConvertUnit(
-      page_params.page_size.height() - page_params.content_size.height() -
-          page_params.margin_top,
-      dpi, kPixelsPerInch);
-  int margin_left_in_pixels =
-      ConvertUnit(page_params.margin_left, dpi, kPixelsPerInch);
-
-  if (frame) {
-    frame->PageSizeAndMarginsInPixels(
-        page_index, page_size_in_pixels, margin_top_in_pixels,
-        margin_right_in_pixels, margin_bottom_in_pixels, margin_left_in_pixels);
-  }
-
-  double new_content_width = page_size_in_pixels.Width() -
-                             margin_left_in_pixels - margin_right_in_pixels;
-  double new_content_height = page_size_in_pixels.Height() -
-                              margin_top_in_pixels - margin_bottom_in_pixels;
-
-  // Invalid page size and/or margins. We just use the default setting.
-  if (new_content_width < 1 || new_content_height < 1) {
-    CHECK(frame != NULL);
-    page_css_params = GetCssPrintParams(NULL, page_index, page_params);
-    return page_css_params;
-  }
-
-  page_css_params.page_size =
-      gfx::Size(ConvertUnit(page_size_in_pixels.Width(), kPixelsPerInch, dpi),
-                ConvertUnit(page_size_in_pixels.Height(), kPixelsPerInch, dpi));
-  page_css_params.content_size =
-      gfx::Size(ConvertUnit(new_content_width, kPixelsPerInch, dpi),
-                ConvertUnit(new_content_height, kPixelsPerInch, dpi));
-
-  page_css_params.margin_top =
-      ConvertUnit(margin_top_in_pixels, kPixelsPerInch, dpi);
-  page_css_params.margin_left =
-      ConvertUnit(margin_left_in_pixels, kPixelsPerInch, dpi);
-  return page_css_params;
-}
-
-double FitPrintParamsToPage(const PrintMsg_Print_Params& page_params,
-                            PrintMsg_Print_Params* params_to_fit) {
-  double content_width =
-      static_cast<double>(params_to_fit->content_size.width());
-  double content_height =
-      static_cast<double>(params_to_fit->content_size.height());
-  int default_page_size_height = page_params.page_size.height();
-  int default_page_size_width = page_params.page_size.width();
-  int css_page_size_height = params_to_fit->page_size.height();
-  int css_page_size_width = params_to_fit->page_size.width();
-
-  double scale_factor = 1.0f;
-  if (page_params.page_size == params_to_fit->page_size)
-    return scale_factor;
-
-  if (default_page_size_width < css_page_size_width ||
-      default_page_size_height < css_page_size_height) {
-    double ratio_width =
-        static_cast<double>(default_page_size_width) / css_page_size_width;
-    double ratio_height =
-        static_cast<double>(default_page_size_height) / css_page_size_height;
-    scale_factor = ratio_width < ratio_height ? ratio_width : ratio_height;
-    content_width *= scale_factor;
-    content_height *= scale_factor;
-  }
-  params_to_fit->margin_top = static_cast<int>(
-      (default_page_size_height - css_page_size_height * scale_factor) / 2 +
-      (params_to_fit->margin_top * scale_factor));
-  params_to_fit->margin_left = static_cast<int>(
-      (default_page_size_width - css_page_size_width * scale_factor) / 2 +
-      (params_to_fit->margin_left * scale_factor));
-  params_to_fit->content_size = gfx::Size(static_cast<int>(content_width),
-                                          static_cast<int>(content_height));
-  params_to_fit->page_size = page_params.page_size;
-  return scale_factor;
-}
-
-void CalculatePageLayoutFromPrintParams(
-    const PrintMsg_Print_Params& params,
-    PageSizeMargins* page_layout_in_points) {
-  int dpi = GetDPI(&params);
-  int content_width = params.content_size.width();
-  int content_height = params.content_size.height();
-
-  int margin_bottom =
-      params.page_size.height() - content_height - params.margin_top;
-  int margin_right =
-      params.page_size.width() - content_width - params.margin_left;
-
-  page_layout_in_points->content_width =
-      ConvertUnit(content_width, dpi, kPointsPerInch);
-  page_layout_in_points->content_height =
-      ConvertUnit(content_height, dpi, kPointsPerInch);
-  page_layout_in_points->margin_top =
-      ConvertUnit(params.margin_top, dpi, kPointsPerInch);
-  page_layout_in_points->margin_right =
-      ConvertUnit(margin_right, dpi, kPointsPerInch);
-  page_layout_in_points->margin_bottom =
-      ConvertUnit(margin_bottom, dpi, kPointsPerInch);
-  page_layout_in_points->margin_left =
-      ConvertUnit(params.margin_left, dpi, kPointsPerInch);
-}
-
-void EnsureOrientationMatches(const PrintMsg_Print_Params& css_params,
-                              PrintMsg_Print_Params* page_params) {
-  if ((page_params->page_size.width() > page_params->page_size.height()) ==
-      (css_params.page_size.width() > css_params.page_size.height())) {
-    return;
-  }
-
-  // Swap the |width| and |height| values.
-  page_params->page_size.SetSize(page_params->page_size.height(),
-                                 page_params->page_size.width());
-  page_params->content_size.SetSize(page_params->content_size.height(),
-                                    page_params->content_size.width());
-  page_params->printable_area.set_size(
-      gfx::Size(page_params->printable_area.height(),
-                page_params->printable_area.width()));
-}
-
-void ComputeWebKitPrintParamsInDesiredDpi(
-    const PrintMsg_Print_Params& print_params,
-    blink::WebPrintParams* webkit_print_params) {
-  int dpi = GetDPI(&print_params);
-  webkit_print_params->printer_dpi = dpi;
-  webkit_print_params->print_scaling_option = print_params.print_scaling_option;
-
-  webkit_print_params->print_content_area.width =
-      ConvertUnit(print_params.content_size.width(), dpi, kPointsPerInch);
-  webkit_print_params->print_content_area.height =
-      ConvertUnit(print_params.content_size.height(), dpi, kPointsPerInch);
-
-  webkit_print_params->printable_area.x =
-      ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch);
-  webkit_print_params->printable_area.y =
-      ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch);
-  webkit_print_params->printable_area.width =
-      ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch);
-  webkit_print_params->printable_area.height =
-      ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch);
-
-  webkit_print_params->paper_size.width =
-      ConvertUnit(print_params.page_size.width(), dpi, kPointsPerInch);
-  webkit_print_params->paper_size.height =
-      ConvertUnit(print_params.page_size.height(), dpi, kPointsPerInch);
-}
-
-blink::WebPlugin* GetPlugin(const blink::WebLocalFrame* frame) {
-  return frame->GetDocument().IsPluginDocument()
-             ? frame->GetDocument().To<blink::WebPluginDocument>().Plugin()
-             : nullptr;
-}
-
-bool PrintingNodeOrPdfFrame(const blink::WebLocalFrame* frame,
-                            const blink::WebNode& node) {
-  if (!node.IsNull())
-    return true;
-  blink::WebPlugin* plugin = GetPlugin(frame);
-  return plugin && plugin->SupportsPaginatedPrint();
-}
-
-MarginType GetMarginsForPdf(blink::WebLocalFrame* frame,
-                            const blink::WebNode& node) {
-  if (frame->IsPrintScalingDisabledForPlugin(node))
-    return NO_MARGINS;
-  else
-    return PRINTABLE_AREA_MARGINS;
-}
-
-PrintMsg_Print_Params CalculatePrintParamsForCss(
-    blink::WebLocalFrame* frame,
-    int page_index,
-    const PrintMsg_Print_Params& page_params,
-    bool ignore_css_margins,
-    bool fit_to_page,
-    double* scale_factor) {
-  PrintMsg_Print_Params css_params =
-      GetCssPrintParams(frame, page_index, page_params);
-
-  PrintMsg_Print_Params params = page_params;
-  EnsureOrientationMatches(css_params, &params);
-
-  if (ignore_css_margins && fit_to_page)
-    return params;
-
-  PrintMsg_Print_Params result_params = css_params;
-  if (ignore_css_margins) {
-    result_params.margin_top = params.margin_top;
-    result_params.margin_left = params.margin_left;
-
-    DCHECK(!fit_to_page);
-    // Since we are ignoring the margins, the css page size is no longer
-    // valid.
-    int default_margin_right = params.page_size.width() -
-                               params.content_size.width() - params.margin_left;
-    int default_margin_bottom = params.page_size.height() -
-                                params.content_size.height() -
-                                params.margin_top;
-    result_params.content_size =
-        gfx::Size(result_params.page_size.width() - result_params.margin_left -
-                      default_margin_right,
-                  result_params.page_size.height() - result_params.margin_top -
-                      default_margin_bottom);
-  }
-
-  if (fit_to_page) {
-    double factor = FitPrintParamsToPage(params, &result_params);
-    if (scale_factor)
-      *scale_factor = factor;
-  }
-  return result_params;
-}
-
-}  // namespace
-
-FrameReference::FrameReference(blink::WebLocalFrame* frame) {
-  Reset(frame);
-}
-
-FrameReference::FrameReference() {
-  Reset(NULL);
-}
-
-FrameReference::~FrameReference() {}
-
-void FrameReference::Reset(blink::WebLocalFrame* frame) {
-  if (frame) {
-    view_ = frame->View();
-    frame_ = frame;
-  } else {
-    view_ = NULL;
-    frame_ = NULL;
-  }
-}
-
-blink::WebLocalFrame* FrameReference::GetFrame() {
-  if (view_ == NULL || frame_ == NULL)
-    return NULL;
-  for (blink::WebFrame* frame = view_->MainFrame(); frame != NULL;
-       frame = frame->TraverseNext()) {
-    if (frame == frame_)
-      return frame_;
-  }
-  return NULL;
-}
-
-blink::WebView* FrameReference::view() {
-  return view_;
-}
-
-// static - Not anonymous so that platform implementations can use it.
-float PrintWebViewHelper::RenderPageContent(blink::WebLocalFrame* frame,
-                                            int page_number,
-                                            const gfx::Rect& canvas_area,
-                                            const gfx::Rect& content_area,
-                                            double scale_factor,
-                                            cc::PaintCanvas* canvas) {
-  cc::PaintCanvasAutoRestore auto_restore(canvas, true);
-  canvas->translate((content_area.x() - canvas_area.x()) / scale_factor,
-                    (content_area.y() - canvas_area.y()) / scale_factor);
-  return frame->PrintPage(page_number, canvas);
-}
-
-// Class that calls the Begin and End print functions on the frame and changes
-// the size of the view temporarily to support full page printing..
-class PrepareFrameAndViewForPrint : public blink::WebViewClient,
-                                    public blink::WebLocalFrameClient,
-                                    public blink::WebWidgetClient {
- public:
-  PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params,
-                              blink::WebLocalFrame* frame,
-                              const blink::WebNode& node,
-                              bool ignore_css_margins);
-  ~PrepareFrameAndViewForPrint() override;
-
-  // Optional. Replaces |frame_| with selection if needed. Will call |on_ready|
-  // when completed.
-  void CopySelectionIfNeeded(const WebPreferences& preferences,
-                             const base::Closure& on_ready);
-
-  // Prepares frame for printing.
-  void StartPrinting();
-
-  blink::WebWidgetClient* WidgetClient() override { return this; }
-
-  blink::WebLocalFrame* frame() { return frame_.GetFrame(); }
-
-  const blink::WebNode& node() const { return node_to_print_; }
-
-  int GetExpectedPageCount() const { return expected_pages_count_; }
-
-  void FinishPrinting();
-
-  bool IsLoadingSelection() {
-    // It's not selection if not |owns_web_view_|.
-    return owns_web_view_ && frame() && frame()->IsLoading();
-  }
-
- protected:
-  // blink::WebViewClient override:
-  void DidStopLoading() override;
-  bool AllowsBrokenNullLayerTreeView() const override;
-
-  // blink::WebLocalFrameClient:
-  blink::WebLocalFrame* CreateChildFrame(
-      blink::WebLocalFrame* parent,
-      blink::WebTreeScopeType scope,
-      const blink::WebString& name,
-      const blink::WebString& unique_name,
-      blink::WebSandboxFlags sandbox_flags,
-      const blink::ParsedFeaturePolicy& container_policy,
-      const blink::WebFrameOwnerProperties& frame_owner_properties) override;
-
- private:
-  void CallOnReady();
-  void ResizeForPrinting();
-  void RestoreSize();
-  void CopySelection(const WebPreferences& preferences);
-
-  FrameReference frame_;
-  blink::WebNode node_to_print_;
-  bool owns_web_view_;
-  blink::WebPrintParams web_print_params_;
-  gfx::Size prev_view_size_;
-  gfx::Size prev_scroll_offset_;
-  int expected_pages_count_;
-  base::Closure on_ready_;
-  bool should_print_backgrounds_;
-  bool should_print_selection_only_;
-  bool is_printing_started_;
-
-  base::WeakPtrFactory<PrepareFrameAndViewForPrint> weak_ptr_factory_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint);
-};
-
-PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
-    const PrintMsg_Print_Params& params,
-    blink::WebLocalFrame* frame,
-    const blink::WebNode& node,
-    bool ignore_css_margins)
-    : frame_(frame),
-      node_to_print_(node),
-      owns_web_view_(false),
-      expected_pages_count_(0),
-      should_print_backgrounds_(params.should_print_backgrounds),
-      should_print_selection_only_(params.selection_only),
-      is_printing_started_(false),
-      weak_ptr_factory_(this) {
-  PrintMsg_Print_Params print_params = params;
-  if (!should_print_selection_only_ ||
-      !PrintingNodeOrPdfFrame(frame, node_to_print_)) {
-    bool fit_to_page = ignore_css_margins &&
-                       print_params.print_scaling_option ==
-                           blink::kWebPrintScalingOptionFitToPrintableArea;
-    ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_);
-    frame->PrintBegin(web_print_params_, node_to_print_);
-    print_params = CalculatePrintParamsForCss(
-        frame, 0, print_params, ignore_css_margins, fit_to_page, NULL);
-    frame->PrintEnd();
-  }
-  ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_);
-}
-
-PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
-  FinishPrinting();
-}
-
-void PrepareFrameAndViewForPrint::ResizeForPrinting() {
-  // Layout page according to printer page size. Since WebKit shrinks the
-  // size of the page automatically (from 125% to 200%) we trick it to
-  // think the page is 125% larger so the size of the page is correct for
-  // minimum (default) scaling.
-  // This is important for sites that try to fill the page.
-  // The 1.25 value is |printingMinimumShrinkFactor|.
-  gfx::Size print_layout_size(web_print_params_.print_content_area.width,
-                              web_print_params_.print_content_area.height);
-  print_layout_size.set_height(
-      static_cast<int>(static_cast<double>(print_layout_size.height()) * 1.25));
-
-  if (!frame())
-    return;
-
-  // Backup size and offset if it's a local frame.
-  blink::WebView* web_view = frame_.view();
-  if (blink::WebFrame* web_frame = web_view->MainFrame()) {
-    if (web_frame->IsWebLocalFrame())
-      prev_scroll_offset_ = web_frame->ToWebLocalFrame()->GetScrollOffset();
-  }
-  prev_view_size_ = web_view->Size();
-
-  web_view->Resize(print_layout_size);
-}
-
-void PrepareFrameAndViewForPrint::StartPrinting() {
-  ResizeForPrinting();
-  blink::WebView* web_view = frame_.view();
-  web_view->GetSettings()->SetShouldPrintBackgrounds(should_print_backgrounds_);
-  expected_pages_count_ =
-      frame()->PrintBegin(web_print_params_, node_to_print_);
-  is_printing_started_ = true;
-}
-
-void PrepareFrameAndViewForPrint::CopySelectionIfNeeded(
-    const WebPreferences& preferences,
-    const base::Closure& on_ready) {
-  on_ready_ = on_ready;
-  if (should_print_selection_only_) {
-    CopySelection(preferences);
-  } else {
-    // Call immediately, async call crashes scripting printing.
-    CallOnReady();
-  }
-}
-
-void PrepareFrameAndViewForPrint::CopySelection(
-    const WebPreferences& preferences) {
-  ResizeForPrinting();
-  std::string url_str = "data:text/html;charset=utf-8,";
-  url_str.append(
-      net::EscapeQueryParamValue(frame()->SelectionAsMarkup().Utf8(), false));
-  RestoreSize();
-  // Create a new WebView with the same settings as the current display one.
-  // Except that we disable javascript (don't want any active content running
-  // on the page).
-  WebPreferences prefs = preferences;
-  prefs.javascript_enabled = false;
-
-  blink::WebView* web_view = blink::WebView::Create(
-      this, this, blink::mojom::PageVisibilityState::kVisible, nullptr);
-  owns_web_view_ = true;
-  content::RenderView::ApplyWebPreferences(prefs, web_view);
-  blink::WebLocalFrame* main_frame =
-      blink::WebLocalFrame::CreateMainFrame(web_view, this, nullptr, nullptr);
-  blink::WebFrameWidget::Create(this, main_frame);
-  frame_.Reset(web_view->MainFrame()->ToWebLocalFrame());
-  node_to_print_.Reset();
-
-  // When loading is done this will call DidStopLoading() and that will do the
-  // actual printing.
-  frame()->StartNavigation(blink::WebURLRequest(GURL(url_str)));
-}
-
-bool PrepareFrameAndViewForPrint::AllowsBrokenNullLayerTreeView() const {
-  return true;
-}
-
-void PrepareFrameAndViewForPrint::DidStopLoading() {
-  DCHECK(!on_ready_.is_null());
-  // Don't call callback here, because it can delete |this| and WebView that is
-  // called DidStopLoading.
-  base::ThreadTaskRunnerHandle::Get()->PostTask(
-      FROM_HERE, base::Bind(&PrepareFrameAndViewForPrint::CallOnReady,
-                            weak_ptr_factory_.GetWeakPtr()));
-}
-
-blink::WebLocalFrame* PrepareFrameAndViewForPrint::CreateChildFrame(
-    blink::WebLocalFrame* parent,
-    blink::WebTreeScopeType scope,
-    const blink::WebString& name,
-    const blink::WebString& unique_name,
-    blink::WebSandboxFlags sandbox_flags,
-    const blink::ParsedFeaturePolicy& container_policy,
-    const blink::WebFrameOwnerProperties& frame_owner_properties) {
-  blink::WebLocalFrame* frame = parent->CreateLocalChild(scope, this, nullptr);
-  return frame;
-}
-
-void PrepareFrameAndViewForPrint::CallOnReady() {
-  return on_ready_.Run();  // Can delete |this|.
-}
-
-void PrepareFrameAndViewForPrint::RestoreSize() {
-  if (!frame())
-    return;
-
-  blink::WebView* web_view = frame_.GetFrame()->View();
-  web_view->Resize(prev_view_size_);
-  if (blink::WebFrame* web_frame = web_view->MainFrame()) {
-    if (web_frame->IsWebLocalFrame())
-      web_frame->ToWebLocalFrame()->SetScrollOffset(prev_scroll_offset_);
-  }
-}
-
-void PrepareFrameAndViewForPrint::FinishPrinting() {
-  blink::WebLocalFrame* frame = frame_.GetFrame();
-  if (frame) {
-    blink::WebView* web_view = frame->View();
-    if (is_printing_started_) {
-      is_printing_started_ = false;
-      frame->PrintEnd();
-      if (!owns_web_view_) {
-        web_view->GetSettings()->SetShouldPrintBackgrounds(false);
-        RestoreSize();
-      }
-    }
-    if (owns_web_view_) {
-      DCHECK(!frame->IsLoading());
-      owns_web_view_ = false;
-      web_view->Close();
-    }
-  }
-  frame_.Reset(NULL);
-  on_ready_.Reset();
-}
-
-PrintWebViewHelper::PrintWebViewHelper(content::RenderFrame* render_frame)
-    : content::RenderFrameObserver(render_frame),
-      content::RenderFrameObserverTracker<PrintWebViewHelper>(render_frame),
-      reset_prep_frame_view_(false),
-      is_print_ready_metafile_sent_(false),
-      ignore_css_margins_(false),
-      is_scripted_printing_blocked_(false),
-      notify_browser_of_print_failure_(true),
-      print_for_preview_(false),
-      print_node_in_progress_(false),
-      is_loading_(false),
-      is_scripted_preview_delayed_(false),
-      ipc_nesting_level_(0),
-      weak_ptr_factory_(this) {}
-
-PrintWebViewHelper::~PrintWebViewHelper() {}
-
-// Prints |frame| which called window.print().
-void PrintWebViewHelper::ScriptedPrint(bool user_initiated) {
-  Print(render_frame()->GetWebFrame(), blink::WebNode());
-}
-
-bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) {
-  // The class is not designed to handle recursive messages. This is not
-  // expected during regular flow. However, during rendering of content for
-  // printing, lower level code may run nested message loop. E.g. PDF may has
-  // script to show message box http://crbug.com/502562. In that moment browser
-  // may receive updated printer capabilities and decide to restart print
-  // preview generation. When this happened message handling function may
-  // choose to ignore message or safely crash process.
-  ++ipc_nesting_level_;
-
-  bool handled = true;
-  IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message)
-    IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages)
-    IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
-    IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
-    IPC_MESSAGE_UNHANDLED(handled = false)
-  IPC_END_MESSAGE_MAP()
-
-  --ipc_nesting_level_;
-  return handled;
-}
-
-void PrintWebViewHelper::OnDestruct() {
-  delete this;
-}
-
-#if !defined(DISABLE_BASIC_PRINTING)
-void PrintWebViewHelper::OnPrintPages(bool silent,
-                                      bool print_background,
-                                      const base::string16& device_name) {
-  if (ipc_nesting_level_ > 1)
-    return;
-
-  blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
-  Print(frame, blink::WebNode(), silent, print_background, device_name);
-}
-#endif  // !DISABLE_BASIC_PRINTING
-
-void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout(
-    const PageSizeMargins& page_layout_in_points,
-    gfx::Size* page_size,
-    gfx::Rect* content_area) {
-  *page_size = gfx::Size(
-      page_layout_in_points.content_width + page_layout_in_points.margin_right +
-          page_layout_in_points.margin_left,
-      page_layout_in_points.content_height + page_layout_in_points.margin_top +
-          page_layout_in_points.margin_bottom);
-  *content_area = gfx::Rect(page_layout_in_points.margin_left,
-                            page_layout_in_points.margin_top,
-                            page_layout_in_points.content_width,
-                            page_layout_in_points.content_height);
-}
-
-void PrintWebViewHelper::UpdateFrameMarginsCssInfo(
-    const base::DictionaryValue& settings) {
-  int margins_type = 0;
-  if (!settings.GetInteger(kSettingMarginsType, &margins_type))
-    margins_type = DEFAULT_MARGINS;
-  ignore_css_margins_ = (margins_type != DEFAULT_MARGINS);
-}
-
-void PrintWebViewHelper::OnPrintingDone(bool success) {
-  notify_browser_of_print_failure_ = false;
-  if (!success)
-    LOG(ERROR) << "Failure in OnPrintingDone";
-  DidFinishPrinting(success ? OK : FAIL_PRINT);
-}
-
-void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) {
-  if (ipc_nesting_level_ > 1)
-    return;
-
-  blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
-
-  print_preview_context_.InitWithFrame(frame);
-  if (!print_preview_context_.source_frame()) {
-    DidFinishPrinting(FAIL_PREVIEW);
-    return;
-  }
-
-  if (!UpdatePrintSettings(print_preview_context_.source_frame(),
-                           print_preview_context_.source_node(), settings)) {
-    if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
-      DidFinishPrinting(INVALID_SETTINGS);
-    } else {
-      DidFinishPrinting(FAIL_PREVIEW);
-    }
-    return;
-  }
-  is_print_ready_metafile_sent_ = false;
-  PrepareFrameForPreviewDocument();
-}
-
-void PrintWebViewHelper::PrepareFrameForPreviewDocument() {
-  reset_prep_frame_view_ = false;
-
-  if (!print_pages_params_) {
-    DidFinishPrinting(FAIL_PREVIEW);
-    return;
-  }
-
-  // Don't reset loading frame or WebKit will fail assert. Just retry when
-  // current selection is loaded.
-  if (prep_frame_view_ && prep_frame_view_->IsLoadingSelection()) {
-    reset_prep_frame_view_ = true;
-    return;
-  }
-
-  const PrintMsg_Print_Params& print_params = print_pages_params_->params;
-  prep_frame_view_.reset(new PrepareFrameAndViewForPrint(
-      print_params, print_preview_context_.source_frame(),
-      print_preview_context_.source_node(), ignore_css_margins_));
-  prep_frame_view_->CopySelectionIfNeeded(
-      render_frame()->GetWebkitPreferences(),
-      base::Bind(&PrintWebViewHelper::OnFramePreparedForPreviewDocument,
-                 base::Unretained(this)));
-}
-
-void PrintWebViewHelper::OnFramePreparedForPreviewDocument() {
-  if (reset_prep_frame_view_) {
-    PrepareFrameForPreviewDocument();
-    return;
-  }
-  DidFinishPrinting(CreatePreviewDocument() ? OK : FAIL_PREVIEW);
-}
-
-bool PrintWebViewHelper::CreatePreviewDocument() {
-  if (!print_pages_params_)
-    return false;
-
-  const PrintMsg_Print_Params& print_params = print_pages_params_->params;
-  const std::vector<int>& pages = print_pages_params_->pages;
-
-  if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(),
-                                                    pages)) {
-    return false;
-  }
-
-  while (!print_preview_context_.IsFinalPageRendered()) {
-    int page_number = print_preview_context_.GetNextPageNumber();
-    DCHECK_GE(page_number, 0);
-    if (!RenderPreviewPage(page_number, print_params))
-      return false;
-
-    // We must call PrepareFrameAndViewForPrint::FinishPrinting() (by way of
-    // print_preview_context_.AllPagesRendered()) before calling
-    // FinalizePrintReadyDocument() when printing a PDF because the plugin
-    // code does not generate output until we call FinishPrinting().  We do not
-    // generate draft pages for PDFs, so IsFinalPageRendered() and
-    // IsLastPageOfPrintReadyMetafile() will be true in the same iteration of
-    // the loop.
-    if (print_preview_context_.IsFinalPageRendered())
-      print_preview_context_.AllPagesRendered();
-
-    if (print_preview_context_.IsLastPageOfPrintReadyMetafile()) {
-      DCHECK(print_preview_context_.IsModifiable() ||
-             print_preview_context_.IsFinalPageRendered());
-      if (!FinalizePrintReadyDocument())
-        return false;
-    }
-  }
-  print_preview_context_.Finished();
-  return true;
-}
-
-bool PrintWebViewHelper::FinalizePrintReadyDocument() {
-  DCHECK(!is_print_ready_metafile_sent_);
-  print_preview_context_.FinalizePrintReadyDocument();
-
-  PdfMetafileSkia* metafile = print_preview_context_.metafile();
-
-  PrintHostMsg_DidPreviewDocument_Params preview_params;
-
-  // Ask the browser to create the shared memory for us.
-  if (!CopyMetafileDataToSharedMem(*metafile,
-                                   &(preview_params.metafile_data_handle))) {
-    print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
-    return false;
-  }
-
-  preview_params.data_size = metafile->GetDataSize();
-  preview_params.document_cookie = print_pages_params_->params.document_cookie;
-  preview_params.expected_pages_count =
-      print_preview_context_.total_page_count();
-  preview_params.modifiable = print_preview_context_.IsModifiable();
-  preview_params.preview_request_id =
-      print_pages_params_->params.preview_request_id;
-
-  is_print_ready_metafile_sent_ = true;
-
-  Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), preview_params));
-  return true;
-}
-
-void PrintWebViewHelper::PrintNode(const blink::WebNode& node) {
-  if (node.IsNull() || !node.GetDocument().GetFrame()) {
-    // This can occur when the context menu refers to an invalid WebNode.
-    // See http://crbug.com/100890#c17 for a repro case.
-    return;
-  }
-
-  if (print_node_in_progress_) {
-    // This can happen as a result of processing sync messages when printing
-    // from ppapi plugins. It's a rare case, so its OK to just fail here.
-    // See http://crbug.com/159165.
-    return;
-  }
-
-  print_node_in_progress_ = true;
-  blink::WebNode duplicate_node(node);
-  Print(duplicate_node.GetDocument().GetFrame(), duplicate_node);
-
-  print_node_in_progress_ = false;
-}
-
-void PrintWebViewHelper::Print(blink::WebLocalFrame* frame,
-                               const blink::WebNode& node,
-                               bool silent,
-                               bool print_background,
-                               const base::string16& device_name) {
-  // If still not finished with earlier print request simply ignore.
-  if (prep_frame_view_)
-    return;
-
-  FrameReference frame_ref(frame);
-
-  int expected_page_count = 0;
-  if (!CalculateNumberOfPages(frame, node, &expected_page_count, device_name)) {
-    DidFinishPrinting(FAIL_PRINT_INIT);
-    return;  // Failed to init print page settings.
-  }
-
-  // Some full screen plugins can say they don't want to print.
-  if (!expected_page_count) {
-    DidFinishPrinting(FAIL_PRINT);
-    return;
-  }
-
-  // Ask the browser to show UI to retrieve the final print settings.
-  if (!silent && !GetPrintSettingsFromUser(frame_ref.GetFrame(), node,
-                                           expected_page_count)) {
-    DidFinishPrinting(OK);  // Release resources and fail silently.
-    return;
-  }
-
-  print_pages_params_->params.should_print_backgrounds = print_background;
-
-  // Render Pages for printing.
-  if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) {
-    DidFinishPrinting(FAIL_PRINT);
-  }
-}
-
-void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) {
-  switch (result) {
-    case OK:
-      break;
-
-    case FAIL_PRINT_INIT:
-      DCHECK(!notify_browser_of_print_failure_);
-      break;
-
-    case FAIL_PRINT:
-      if (notify_browser_of_print_failure_ && print_pages_params_) {
-        int cookie = print_pages_params_->params.document_cookie;
-        Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie));
-      }
-      break;
-
-    case FAIL_PREVIEW:
-    case INVALID_SETTINGS:
-      if (print_pages_params_) {
-        Send(new PrintHostMsg_PrintPreviewFailed(
-            routing_id(), print_pages_params_->params.document_cookie,
-            print_pages_params_->params.preview_request_id));
-      }
-      break;
-  }
-  prep_frame_view_.reset();
-  print_pages_params_.reset();
-  notify_browser_of_print_failure_ = true;
-}
-
-void PrintWebViewHelper::OnFramePreparedForPrintPages() {
-  PrintPages();
-  FinishFramePrinting();
-}
-
-void PrintWebViewHelper::PrintPages() {
-  if (!prep_frame_view_)  // Printing is already canceled or failed.
-    return;
-  prep_frame_view_->StartPrinting();
-
-  int page_count = prep_frame_view_->GetExpectedPageCount();
-  if (!page_count) {
-    LOG(ERROR) << "Can't print 0 pages.";
-    return DidFinishPrinting(FAIL_PRINT);
-  }
-
-  const PrintMsg_PrintPages_Params& params = *print_pages_params_;
-  const PrintMsg_Print_Params& print_params = params.params;
-
-#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
-  // TODO(vitalybuka): should be page_count or valid pages from params.pages.
-  // See http://crbug.com/161576
-  Send(new PrintHostMsg_DidGetPrintedPagesCount(
-      routing_id(), print_params.document_cookie, page_count));
-#endif  // !defined(OS_CHROMEOS)
-
-  if (!PrintPagesNative(prep_frame_view_->frame(), page_count)) {
-    LOG(ERROR) << "Printing failed.";
-    return DidFinishPrinting(FAIL_PRINT);
-  }
-}
-
-void PrintWebViewHelper::FinishFramePrinting() {
-  prep_frame_view_.reset();
-}
-
-#if defined(OS_MACOSX)
-bool PrintWebViewHelper::PrintPagesNative(blink::WebLocalFrame* frame,
-                                          int page_count) {
-  const PrintMsg_PrintPages_Params& params = *print_pages_params_;
-  const PrintMsg_Print_Params& print_params = params.params;
-
-  PrintMsg_PrintPage_Params page_params;
-  page_params.params = print_params;
-  if (params.pages.empty()) {
-    for (int i = 0; i < page_count; ++i) {
-      page_params.page_number = i;
-      PrintPageInternal(page_params, frame);
-    }
-  } else {
-    for (size_t i = 0; i < params.pages.size(); ++i) {
-      if (params.pages[i] >= page_count)
-        break;
-      page_params.page_number = params.pages[i];
-      PrintPageInternal(page_params, frame);
-    }
-  }
-  return true;
-}
-
-#endif  // OS_MACOSX
-
-// static - Not anonymous so that platform implementations can use it.
-void PrintWebViewHelper::ComputePageLayoutInPointsForCss(
-    blink::WebLocalFrame* frame,
-    int page_index,
-    const PrintMsg_Print_Params& page_params,
-    bool ignore_css_margins,
-    double* scale_factor,
-    PageSizeMargins* page_layout_in_points) {
-  PrintMsg_Print_Params params = CalculatePrintParamsForCss(
-      frame, page_index, page_params, ignore_css_margins,
-      page_params.print_scaling_option ==
-          blink::kWebPrintScalingOptionFitToPrintableArea,
-      scale_factor);
-  CalculatePageLayoutFromPrintParams(params, page_layout_in_points);
-}
-
-bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size,
-                                           const base::string16& device_name) {
-  PrintMsg_PrintPages_Params settings;
-  if (device_name.empty()) {
-    Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
-                                                  &settings.params));
-  } else {
-    Send(new PrintHostMsg_InitSettingWithDeviceName(routing_id(), device_name,
-                                                    &settings.params));
-  }
-  // Check if the printer returned any settings, if the settings is empty, we
-  // can safely assume there are no printer drivers configured. So we safely
-  // terminate.
-  bool result = true;
-  if (!PrintMsg_Print_Params_IsValid(settings.params))
-    result = false;
-
-  // Reset to default values.
-  ignore_css_margins_ = false;
-  settings.pages.clear();
-
-  settings.params.print_scaling_option =
-      blink::kWebPrintScalingOptionSourceSize;
-  if (fit_to_paper_size) {
-    settings.params.print_scaling_option =
-        blink::kWebPrintScalingOptionFitToPrintableArea;
-  }
-
-  SetPrintPagesParams(settings);
-  return result;
-}
-
-bool PrintWebViewHelper::CalculateNumberOfPages(
-    blink::WebLocalFrame* frame,
-    const blink::WebNode& node,
-    int* number_of_pages,
-    const base::string16& device_name) {
-  DCHECK(frame);
-  bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node));
-  if (!InitPrintSettings(fit_to_paper_size, device_name)) {
-    notify_browser_of_print_failure_ = false;
-    Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
-    return false;
-  }
-
-  const PrintMsg_Print_Params& params = print_pages_params_->params;
-  PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_);
-  prepare.StartPrinting();
-
-  *number_of_pages = prepare.GetExpectedPageCount();
-  return true;
-}
-
-bool PrintWebViewHelper::UpdatePrintSettings(
-    blink::WebLocalFrame* frame,
-    const blink::WebNode& node,
-    const base::DictionaryValue& passed_job_settings) {
-  const base::DictionaryValue* job_settings = &passed_job_settings;
-  base::DictionaryValue modified_job_settings;
-  if (job_settings->empty()) {
-    if (!print_for_preview_)
-      print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
-    return false;
-  }
-
-  bool source_is_html = true;
-  if (print_for_preview_) {
-    if (!job_settings->GetBoolean(kSettingPreviewModifiable, &source_is_html)) {
-      NOTREACHED();
-    }
-  } else {
-    source_is_html = !PrintingNodeOrPdfFrame(frame, node);
-  }
-
-  if (print_for_preview_ || !source_is_html) {
-    modified_job_settings.MergeDictionary(job_settings);
-    modified_job_settings.SetBoolean(kSettingHeaderFooterEnabled, false);
-    modified_job_settings.SetInteger(kSettingMarginsType, NO_MARGINS);
-    job_settings = &modified_job_settings;
-  }
-
-  // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
-  // possible.
-  int cookie =
-      print_pages_params_ ? print_pages_params_->params.document_cookie : 0;
-  PrintMsg_PrintPages_Params settings;
-  bool canceled = false;
-  Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), cookie, *job_settings,
-                                            &settings, &canceled));
-  if (canceled) {
-    notify_browser_of_print_failure_ = false;
-    return false;
-  }
-
-  if (!print_for_preview_) {
-    job_settings->GetInteger(kPreviewRequestID,
-                             &settings.params.preview_request_id);
-    settings.params.print_to_pdf = true;
-    UpdateFrameMarginsCssInfo(*job_settings);
-    settings.params.print_scaling_option =
-        blink::kWebPrintScalingOptionSourceSize;
-  }
-
-  SetPrintPagesParams(settings);
-
-  if (PrintMsg_Print_Params_IsValid(settings.params))
-    return true;
-
-  if (print_for_preview_)
-    Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
-  else
-    print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
-  return false;
-}
-
-bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebLocalFrame* frame,
-                                                  const blink::WebNode& node,
-                                                  int expected_pages_count) {
-  PrintHostMsg_ScriptedPrint_Params params;
-  PrintMsg_PrintPages_Params print_settings;
-
-  params.cookie = print_pages_params_->params.document_cookie;
-  params.has_selection = frame->HasSelection();
-  params.expected_pages_count = expected_pages_count;
-  MarginType margin_type = DEFAULT_MARGINS;
-  if (PrintingNodeOrPdfFrame(frame, node))
-    margin_type = GetMarginsForPdf(frame, node);
-  params.margin_type = margin_type;
-
-  // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the
-  // value before and restore it afterwards.
-  blink::WebPrintScalingOption scaling_option =
-      print_pages_params_->params.print_scaling_option;
-
-  print_pages_params_.reset();
-  IPC::SyncMessage* msg =
-      new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings);
-  msg->EnableMessagePumping();
-  Send(msg);
-  print_settings.params.print_scaling_option = scaling_option;
-  SetPrintPagesParams(print_settings);
-  return (print_settings.params.dpi && print_settings.params.document_cookie);
-}
-
-bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame,
-                                             const blink::WebNode& node) {
-  if (!frame || prep_frame_view_)
-    return false;
-  const PrintMsg_PrintPages_Params& params = *print_pages_params_;
-  const PrintMsg_Print_Params& print_params = params.params;
-  prep_frame_view_.reset(new PrepareFrameAndViewForPrint(
-      print_params, frame, node, ignore_css_margins_));
-  DCHECK(!print_pages_params_->params.selection_only ||
-         print_pages_params_->pages.empty());
-  prep_frame_view_->CopySelectionIfNeeded(
-      render_frame()->GetWebkitPreferences(),
-      base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages,
-                 base::Unretained(this)));
-  return true;
-}
-
-#if defined(OS_POSIX)
-bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
-    const PdfMetafileSkia& metafile,
-    base::SharedMemoryHandle* shared_mem_handle) {
-  uint32_t buf_size = metafile.GetDataSize();
-  if (buf_size == 0)
-    return false;
-
-  std::unique_ptr<base::SharedMemory> shared_buf(
-      content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(buf_size));
-  if (!shared_buf)
-    return false;
-
-  if (!shared_buf->Map(buf_size))
-    return false;
-
-  if (!metafile.GetData(shared_buf->memory(), buf_size))
-    return false;
-
-  *shared_mem_handle =
-      base::SharedMemory::DuplicateHandle(shared_buf->handle());
-  return true;
-}
-#endif  // defined(OS_POSIX)
-
-void PrintWebViewHelper::SetPrintPagesParams(
-    const PrintMsg_PrintPages_Params& settings) {
-  print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
-  Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
-                                             settings.params.document_cookie));
-}
-
-bool PrintWebViewHelper::PreviewPageRendered(int page_number,
-                                             PdfMetafileSkia* metafile) {
-  DCHECK_GE(page_number, FIRST_PAGE_INDEX);
-
-  // For non-modifiable files, |metafile| should be NULL, so do not bother
-  // sending a message. If we don't generate draft metafiles, |metafile| is
-  // NULL.
-  if (!print_preview_context_.IsModifiable() ||
-      !print_preview_context_.generate_draft_pages()) {
-    DCHECK(!metafile);
-    return true;
-  }
-
-  if (!metafile) {
-    NOTREACHED();
-    print_preview_context_.set_error(
-        PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE);
-    return false;
-  }
-
-  return true;
-}
-
-PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext()
-    : total_page_count_(0),
-      current_page_index_(0),
-      generate_draft_pages_(true),
-      print_ready_metafile_page_count_(0),
-      error_(PREVIEW_ERROR_NONE),
-      state_(UNINITIALIZED) {}
-
-PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() {}
-
-void PrintWebViewHelper::PrintPreviewContext::InitWithFrame(
-    blink::WebLocalFrame* web_frame) {
-  DCHECK(web_frame);
-  DCHECK(!IsRendering());
-  state_ = INITIALIZED;
-  source_frame_.Reset(web_frame);
-  source_node_.Reset();
-}
-
-void PrintWebViewHelper::PrintPreviewContext::InitWithNode(
-    const blink::WebNode& web_node) {
-  DCHECK(!web_node.IsNull());
-  DCHECK(web_node.GetDocument().GetFrame());
-  DCHECK(!IsRendering());
-  state_ = INITIALIZED;
-  source_frame_.Reset(web_node.GetDocument().GetFrame());
-  source_node_ = web_node;
-}
-
-void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() {
-  DCHECK_EQ(INITIALIZED, state_);
-  ClearContext();
-}
-
-bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument(
-    PrepareFrameAndViewForPrint* prepared_frame,
-    const std::vector<int>& pages) {
-  DCHECK_EQ(INITIALIZED, state_);
-  state_ = RENDERING;
-
-  // Need to make sure old object gets destroyed first.
-  prep_frame_view_.reset(prepared_frame);
-  prep_frame_view_->StartPrinting();
-
-  total_page_count_ = prep_frame_view_->GetExpectedPageCount();
-  if (total_page_count_ == 0) {
-    LOG(ERROR) << "CreatePreviewDocument got 0 page count";
-    set_error(PREVIEW_ERROR_ZERO_PAGES);
-    return false;
-  }
-
-  metafile_.reset(new PdfMetafileSkia());
-  CHECK(metafile_->Init());
-
-  current_page_index_ = 0;
-  pages_to_render_ = pages;
-  // Sort and make unique.
-  std::sort(pages_to_render_.begin(), pages_to_render_.end());
-  pages_to_render_.resize(
-      std::unique(pages_to_render_.begin(), pages_to_render_.end()) -
-      pages_to_render_.begin());
-  // Remove invalid pages.
-  pages_to_render_.resize(std::lower_bound(pages_to_render_.begin(),
-                                           pages_to_render_.end(),
-                                           total_page_count_) -
-                          pages_to_render_.begin());
-  print_ready_metafile_page_count_ = pages_to_render_.size();
-  if (pages_to_render_.empty()) {
-    print_ready_metafile_page_count_ = total_page_count_;
-    // Render all pages.
-    for (int i = 0; i < total_page_count_; ++i)
-      pages_to_render_.push_back(i);
-  } else if (generate_draft_pages_) {
-    int pages_index = 0;
-    for (int i = 0; i < total_page_count_; ++i) {
-      if (pages_index < print_ready_metafile_page_count_ &&
-          i == pages_to_render_[pages_index]) {
-        pages_index++;
-        continue;
-      }
-      pages_to_render_.push_back(i);
-    }
-  }
-
-  document_render_time_ = base::TimeDelta();
-  begin_time_ = base::TimeTicks::Now();
-
-  return true;
-}
-
-void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage(
-    const base::TimeDelta& page_time) {
-  DCHECK_EQ(RENDERING, state_);
-  document_render_time_ += page_time;
-  UMA_HISTOGRAM_TIMES("PrintPreview.RenderPDFPageTime", page_time);
-}
-
-void PrintWebViewHelper::PrintPreviewContext::AllPagesRendered() {
-  DCHECK_EQ(RENDERING, state_);
-  state_ = DONE;
-  prep_frame_view_->FinishPrinting();
-}
-
-void PrintWebViewHelper::PrintPreviewContext::FinalizePrintReadyDocument() {
-  DCHECK(IsRendering());
-
-  base::TimeTicks begin_time = base::TimeTicks::Now();
-  metafile_->FinishDocument();
-
-  if (print_ready_metafile_page_count_ <= 0) {
-    NOTREACHED();
-    return;
-  }
-
-  UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderToPDFTime",
-                             document_render_time_);
-  base::TimeDelta total_time =
-      (base::TimeTicks::Now() - begin_time) + document_render_time_;
-  UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTime",
-                             total_time);
-  UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage",
-                             total_time / pages_to_render_.size());
-}
-
-void PrintWebViewHelper::PrintPreviewContext::Finished() {
-  DCHECK_EQ(DONE, state_);
-  state_ = INITIALIZED;
-  ClearContext();
-}
-
-void PrintWebViewHelper::PrintPreviewContext::Failed(bool report_error) {
-  DCHECK(state_ == INITIALIZED || state_ == RENDERING);
-  state_ = INITIALIZED;
-  if (report_error) {
-    DCHECK_NE(PREVIEW_ERROR_NONE, error_);
-    UMA_HISTOGRAM_ENUMERATION("PrintPreview.RendererError", error_,
-                              PREVIEW_ERROR_LAST_ENUM);
-  }
-  ClearContext();
-}
-
-int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() {
-  DCHECK_EQ(RENDERING, state_);
-  if (IsFinalPageRendered())
-    return -1;
-  return pages_to_render_[current_page_index_++];
-}
-
-bool PrintWebViewHelper::PrintPreviewContext::IsRendering() const {
-  return state_ == RENDERING || state_ == DONE;
-}
-
-bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() {
-  // The only kind of node we can print right now is a PDF node.
-  return !PrintingNodeOrPdfFrame(source_frame(), source_node_);
-}
-
-bool PrintWebViewHelper::PrintPreviewContext::HasSelection() {
-  return IsModifiable() && source_frame()->HasSelection();
-}
-
-bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile()
-    const {
-  DCHECK(IsRendering());
-  return current_page_index_ == print_ready_metafile_page_count_;
-}
-
-bool PrintWebViewHelper::PrintPreviewContext::IsFinalPageRendered() const {
-  DCHECK(IsRendering());
-  return static_cast<size_t>(current_page_index_) == pages_to_render_.size();
-}
-
-void PrintWebViewHelper::PrintPreviewContext::set_generate_draft_pages(
-    bool generate_draft_pages) {
-  DCHECK_EQ(INITIALIZED, state_);
-  generate_draft_pages_ = generate_draft_pages;
-}
-
-void PrintWebViewHelper::PrintPreviewContext::set_error(
-    enum PrintPreviewErrorBuckets error) {
-  error_ = error;
-}
-
-blink::WebLocalFrame* PrintWebViewHelper::PrintPreviewContext::source_frame() {
-  DCHECK(state_ != UNINITIALIZED);
-  return source_frame_.GetFrame();
-}
-
-const blink::WebNode& PrintWebViewHelper::PrintPreviewContext::source_node()
-    const {
-  DCHECK(state_ != UNINITIALIZED);
-  return source_node_;
-}
-
-blink::WebLocalFrame*
-PrintWebViewHelper::PrintPreviewContext::prepared_frame() {
-  DCHECK(state_ != UNINITIALIZED);
-  return prep_frame_view_->frame();
-}
-
-const blink::WebNode& PrintWebViewHelper::PrintPreviewContext::prepared_node()
-    const {
-  DCHECK(state_ != UNINITIALIZED);
-  return prep_frame_view_->node();
-}
-
-int PrintWebViewHelper::PrintPreviewContext::total_page_count() const {
-  DCHECK(state_ != UNINITIALIZED);
-  return total_page_count_;
-}
-
-bool PrintWebViewHelper::PrintPreviewContext::generate_draft_pages() const {
-  return generate_draft_pages_;
-}
-
-PdfMetafileSkia* PrintWebViewHelper::PrintPreviewContext::metafile() {
-  DCHECK(IsRendering());
-  return metafile_.get();
-}
-
-int PrintWebViewHelper::PrintPreviewContext::last_error() const {
-  return error_;
-}
-
-void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
-  prep_frame_view_.reset();
-  metafile_.reset();
-  pages_to_render_.clear();
-  error_ = PREVIEW_ERROR_NONE;
-}
-
-}  // namespace printing

+ 0 - 396
chromium_src/chrome/renderer/printing/print_web_view_helper.h

@@ -1,396 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_
-#define CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_
-
-#include <memory>
-#include <vector>
-
-#include "base/callback.h"
-#include "base/gtest_prod_util.h"
-#include "base/memory/shared_memory.h"
-#include "base/memory/weak_ptr.h"
-#include "base/time/time.h"
-#include "content/public/renderer/render_frame_observer.h"
-#include "content/public/renderer/render_frame_observer_tracker.h"
-#include "printing/pdf_metafile_skia.h"
-#include "third_party/blink/public/web/web_local_frame.h"
-#include "third_party/blink/public/web/web_node.h"
-#include "third_party/blink/public/web/web_print_params.h"
-#include "ui/gfx/geometry/size.h"
-
-struct PrintMsg_Print_Params;
-struct PrintMsg_PrintPage_Params;
-struct PrintMsg_PrintPages_Params;
-struct PrintHostMsg_SetOptionsFromDocument_Params;
-
-namespace base {
-class DictionaryValue;
-}
-
-namespace blink {
-class WebView;
-}
-
-namespace cc {
-class PaintCanvas;
-}
-
-namespace printing {
-
-struct PageSizeMargins;
-class PrepareFrameAndViewForPrint;
-
-// Stores reference to frame using WebVew and unique name.
-// Workaround to modal dialog issue on Linux. crbug.com/236147.
-// If WebFrame someday supports WeakPtr, we should use it here.
-class FrameReference {
- public:
-  explicit FrameReference(blink::WebLocalFrame* frame);
-  FrameReference();
-  ~FrameReference();
-
-  void Reset(blink::WebLocalFrame* frame);
-
-  blink::WebLocalFrame* GetFrame();
-  blink::WebView* view();
-
- private:
-  blink::WebView* view_;
-  blink::WebLocalFrame* frame_;
-};
-
-// PrintWebViewHelper handles most of the printing grunt work for RenderView.
-// We plan on making print asynchronous and that will require copying the DOM
-// of the document and creating a new WebView with the contents.
-class PrintWebViewHelper
-    : public content::RenderFrameObserver,
-      public content::RenderFrameObserverTracker<PrintWebViewHelper> {
- public:
-  explicit PrintWebViewHelper(content::RenderFrame* render_frame);
-  ~PrintWebViewHelper() override;
-
-  void PrintNode(const blink::WebNode& node);
-
- private:
-  enum PrintingResult {
-    OK,
-    FAIL_PRINT_INIT,
-    FAIL_PRINT,
-    FAIL_PREVIEW,
-    INVALID_SETTINGS,
-  };
-
-  enum PrintPreviewErrorBuckets {
-    PREVIEW_ERROR_NONE,  // Always first.
-    PREVIEW_ERROR_BAD_SETTING,
-    PREVIEW_ERROR_METAFILE_COPY_FAILED,
-    PREVIEW_ERROR_METAFILE_INIT_FAILED_DEPRECATED,
-    PREVIEW_ERROR_ZERO_PAGES,
-    PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED_DEPRECATED,
-    PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE,
-    PREVIEW_ERROR_INVALID_PRINTER_SETTINGS,
-    PREVIEW_ERROR_LAST_ENUM  // Always last.
-  };
-
-  // RenderFrameObserver implementation.
-  bool OnMessageReceived(const IPC::Message& message) override;
-  void OnDestruct() override;
-  void ScriptedPrint(bool user_initiated) override;
-
-  // Message handlers ---------------------------------------------------------
-#if !defined(DISABLE_BASIC_PRINTING)
-  void OnPrintPages(bool silent,
-                    bool print_background,
-                    const base::string16& device_name);
-  void OnPrintingDone(bool success);
-#endif  // !DISABLE_BASIC_PRINTING
-  void OnPrintPreview(const base::DictionaryValue& settings);
-
-  // Get |page_size| and |content_area| information from
-  // |page_layout_in_points|.
-  void GetPageSizeAndContentAreaFromPageLayout(
-      const PageSizeMargins& page_layout_in_points,
-      gfx::Size* page_size,
-      gfx::Rect* content_area);
-
-  // Update |ignore_css_margins_| based on settings.
-  void UpdateFrameMarginsCssInfo(const base::DictionaryValue& settings);
-
-  // Prepare frame for creating preview document.
-  void PrepareFrameForPreviewDocument();
-
-  // Continue creating preview document.
-  void OnFramePreparedForPreviewDocument();
-
-  // Finalize the print ready preview document.
-  bool FinalizePrintReadyDocument();
-
-  // Renders a print preview page. |page_number| is 0-based.
-  // Returns true if print preview should continue, false on failure.
-  bool RenderPreviewPage(int page_number,
-                         const PrintMsg_Print_Params& print_params);
-
-  // Initialize the print preview document.
-  bool CreatePreviewDocument();
-
-  // Main printing code -------------------------------------------------------
-
-  void Print(blink::WebLocalFrame* frame,
-             const blink::WebNode& node,
-             bool silent = false,
-             bool print_background = false,
-             const base::string16& device_name = base::string16());
-
-  // Notification when printing is done - signal tear-down/free resources.
-  void DidFinishPrinting(PrintingResult result);
-
-  // Print Settings -----------------------------------------------------------
-
-  // Initialize print page settings with default settings.
-  // Used only for native printing workflow.
-  bool InitPrintSettings(bool fit_to_paper_size,
-                         const base::string16& device_name = base::string16());
-
-  // Calculate number of pages in source document.
-  bool CalculateNumberOfPages(
-      blink::WebLocalFrame* frame,
-      const blink::WebNode& node,
-      int* number_of_pages,
-      const base::string16& device_name = base::string16());
-
-  // Update the current print settings with new |passed_job_settings|.
-  // |passed_job_settings| dictionary contains print job details such as printer
-  // name, number of copies, page range, etc.
-  bool UpdatePrintSettings(blink::WebLocalFrame* frame,
-                           const blink::WebNode& node,
-                           const base::DictionaryValue& passed_job_settings);
-
-  // Get final print settings from the user.
-  // Return false if the user cancels or on error.
-  bool GetPrintSettingsFromUser(blink::WebLocalFrame* frame,
-                                const blink::WebNode& node,
-                                int expected_pages_count);
-
-  // Page Printing / Rendering ------------------------------------------------
-
-  void OnFramePreparedForPrintPages();
-  void PrintPages();
-  bool PrintPagesNative(blink::WebLocalFrame* frame, int page_count);
-  void FinishFramePrinting();
-
-  // Prints the page listed in |params|.
-#if defined(OS_LINUX) || defined(OS_ANDROID)
-  void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
-                         blink::WebLocalFrame* frame,
-                         PdfMetafileSkia* metafile);
-#elif defined(OS_WIN)
-  void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
-                         blink::WebLocalFrame* frame,
-                         PdfMetafileSkia* metafile,
-                         gfx::Size* page_size_in_dpi,
-                         gfx::Rect* content_area_in_dpi);
-#else
-  void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
-                         blink::WebLocalFrame* frame);
-#endif
-
-  // Render the frame for printing.
-  bool RenderPagesForPrint(blink::WebLocalFrame* frame,
-                           const blink::WebNode& node);
-
-  // Platform specific helper function for rendering page(s) to |metafile|.
-#if defined(OS_MACOSX)
-  void RenderPage(const PrintMsg_Print_Params& params,
-                  int page_number,
-                  blink::WebLocalFrame* frame,
-                  PdfMetafileSkia* metafile,
-                  gfx::Size* page_size,
-                  gfx::Rect* content_rect);
-#endif  // defined(OS_MACOSX)
-
-  // Renders page contents from |frame| to |content_area| of |canvas|.
-  // |page_number| is zero-based.
-  // When method is called, canvas should be setup to draw to |canvas_area|
-  // with |scale_factor|.
-  static float RenderPageContent(blink::WebLocalFrame* frame,
-                                 int page_number,
-                                 const gfx::Rect& canvas_area,
-                                 const gfx::Rect& content_area,
-                                 double scale_factor,
-                                 cc::PaintCanvas* canvas);
-
-  // Helper methods -----------------------------------------------------------
-
-  bool CopyMetafileDataToSharedMem(const PdfMetafileSkia& metafile,
-                                   base::SharedMemoryHandle* shared_mem_handle);
-
-  // Helper method to get page layout in points and fit to page if needed.
-  static void ComputePageLayoutInPointsForCss(
-      blink::WebLocalFrame* frame,
-      int page_index,
-      const PrintMsg_Print_Params& default_params,
-      bool ignore_css_margins,
-      double* scale_factor,
-      PageSizeMargins* page_layout_in_points);
-
-  // Script Initiated Printing ------------------------------------------------
-
-  // Notifies the browser a print preview page has been rendered.
-  // |page_number| is 0-based.
-  // For a valid |page_number| with modifiable content,
-  // |metafile| is the rendered page. Otherwise |metafile| is NULL.
-  // Returns true if print preview should continue, false on failure.
-  bool PreviewPageRendered(int page_number, PdfMetafileSkia* metafile);
-
-  void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings);
-
-  // WebView used only to print the selection.
-  std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_;
-  bool reset_prep_frame_view_;
-
-  std::unique_ptr<PrintMsg_PrintPages_Params> print_pages_params_;
-  bool is_print_ready_metafile_sent_;
-  bool ignore_css_margins_;
-
-  // Used for scripted initiated printing blocking.
-  bool is_scripted_printing_blocked_;
-
-  // Let the browser process know of a printing failure. Only set to false when
-  // the failure came from the browser in the first place.
-  bool notify_browser_of_print_failure_;
-
-  // True, when printing from print preview.
-  bool print_for_preview_;
-
-  // Keeps track of the state of print preview between messages.
-  // TODO(vitalybuka): Create PrintPreviewContext when needed and delete after
-  // use. Now it's interaction with various messages is confusing.
-  class PrintPreviewContext {
-   public:
-    PrintPreviewContext();
-    ~PrintPreviewContext();
-
-    // Initializes the print preview context. Need to be called to set
-    // the |web_frame| / |web_node| to generate the print preview for.
-    void InitWithFrame(blink::WebLocalFrame* web_frame);
-    void InitWithNode(const blink::WebNode& web_node);
-
-    // Does bookkeeping at the beginning of print preview.
-    void OnPrintPreview();
-
-    // Create the print preview document. |pages| is empty to print all pages.
-    // Takes ownership of |prepared_frame|.
-    bool CreatePreviewDocument(PrepareFrameAndViewForPrint* prepared_frame,
-                               const std::vector<int>& pages);
-
-    // Called after a page gets rendered. |page_time| is how long the
-    // rendering took.
-    void RenderedPreviewPage(const base::TimeDelta& page_time);
-
-    // Updates the print preview context when the required pages are rendered.
-    void AllPagesRendered();
-
-    // Finalizes the print ready preview document.
-    void FinalizePrintReadyDocument();
-
-    // Cleanup after print preview finishes.
-    void Finished();
-
-    // Cleanup after print preview fails.
-    void Failed(bool report_error);
-
-    // Helper functions
-    int GetNextPageNumber();
-    bool IsRendering() const;
-    bool IsModifiable();
-    bool HasSelection();
-    bool IsLastPageOfPrintReadyMetafile() const;
-    bool IsFinalPageRendered() const;
-
-    // Setters
-    void set_generate_draft_pages(bool generate_draft_pages);
-    void set_error(enum PrintPreviewErrorBuckets error);
-
-    // Getters
-    // Original frame for which preview was requested.
-    blink::WebLocalFrame* source_frame();
-    // Original node for which preview was requested.
-    const blink::WebNode& source_node() const;
-
-    // Frame to be use to render preview. May be the same as source_frame(), or
-    // generated from it, e.g. copy of selected block.
-    blink::WebLocalFrame* prepared_frame();
-    // Node to be use to render preview. May be the same as source_node(), or
-    // generated from it, e.g. copy of selected block.
-    const blink::WebNode& prepared_node() const;
-
-    int total_page_count() const;
-    bool generate_draft_pages() const;
-    PdfMetafileSkia* metafile();
-    int last_error() const;
-
-   private:
-    enum State {
-      UNINITIALIZED,  // Not ready to render.
-      INITIALIZED,    // Ready to render.
-      RENDERING,      // Rendering.
-      DONE            // Finished rendering.
-    };
-
-    // Reset some of the internal rendering context.
-    void ClearContext();
-
-    // Specifies what to render for print preview.
-    FrameReference source_frame_;
-    blink::WebNode source_node_;
-
-    std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_;
-    std::unique_ptr<PdfMetafileSkia> metafile_;
-
-    // Total page count in the renderer.
-    int total_page_count_;
-
-    // The current page to render.
-    int current_page_index_;
-
-    // List of page indices that need to be rendered.
-    std::vector<int> pages_to_render_;
-
-    // True, when draft pages needs to be generated.
-    bool generate_draft_pages_;
-
-    // Specifies the total number of pages in the print ready metafile.
-    int print_ready_metafile_page_count_;
-
-    base::TimeDelta document_render_time_;
-    base::TimeTicks begin_time_;
-
-    enum PrintPreviewErrorBuckets error_;
-
-    State state_;
-  };
-
-  bool print_node_in_progress_;
-  bool is_loading_;
-  bool is_scripted_preview_delayed_;
-  int ipc_nesting_level_;
-
-  PrintPreviewContext print_preview_context_;
-
-  // Used to fix a race condition where the source is a PDF and print preview
-  // hangs because RequestPrintPreview is called before DidStopLoading() is
-  // called. This is a store for the RequestPrintPreview() call and its
-  // parameters so that it can be invoked after DidStopLoading.
-  base::Closure on_stop_loading_closure_;
-
-  base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_

+ 0 - 142
chromium_src/chrome/renderer/printing/print_web_view_helper_linux.cc

@@ -1,142 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/renderer/printing/print_web_view_helper.h"
-
-#include <memory>
-
-#include "base/logging.h"
-#include "chrome/common/print_messages.h"
-#include "content/public/renderer/render_thread.h"
-#include "printing/metafile_skia_wrapper.h"
-#include "printing/page_size_margins.h"
-#include "printing/pdf_metafile_skia.h"
-#include "third_party/blink/public/web/web_local_frame.h"
-
-#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
-#include "base/process/process_handle.h"
-#else
-#include "base/file_descriptor_posix.h"
-#endif  // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
-
-namespace printing {
-
-using blink::WebLocalFrame;
-
-bool PrintWebViewHelper::RenderPreviewPage(
-    int page_number,
-    const PrintMsg_Print_Params& print_params) {
-  PrintMsg_PrintPage_Params page_params;
-  page_params.params = print_params;
-  page_params.page_number = page_number;
-  std::unique_ptr<PdfMetafileSkia> draft_metafile;
-  PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
-  if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) {
-    draft_metafile.reset(new PdfMetafileSkia());
-    initial_render_metafile = draft_metafile.get();
-  }
-
-  base::TimeTicks begin_time = base::TimeTicks::Now();
-  PrintPageInternal(page_params, print_preview_context_.prepared_frame(),
-                    initial_render_metafile);
-  print_preview_context_.RenderedPreviewPage(base::TimeTicks::Now() -
-                                             begin_time);
-  if (draft_metafile.get()) {
-    draft_metafile->FinishDocument();
-  } else if (print_preview_context_.IsModifiable() &&
-             print_preview_context_.generate_draft_pages()) {
-    DCHECK(!draft_metafile.get());
-    draft_metafile =
-        print_preview_context_.metafile()->GetMetafileForCurrentPage(
-            SkiaDocumentType::PDF);
-  }
-  return PreviewPageRendered(page_number, draft_metafile.get());
-}
-
-bool PrintWebViewHelper::PrintPagesNative(blink::WebLocalFrame* frame,
-                                          int page_count) {
-  PdfMetafileSkia metafile;
-  if (!metafile.Init())
-    return false;
-
-  const PrintMsg_PrintPages_Params& params = *print_pages_params_;
-  std::vector<int> printed_pages;
-
-  if (params.pages.empty()) {
-    for (int i = 0; i < page_count; ++i) {
-      printed_pages.push_back(i);
-    }
-  } else {
-    // TODO(vitalybuka): redesign to make more code cross platform.
-    for (size_t i = 0; i < params.pages.size(); ++i) {
-      if (params.pages[i] >= 0 && params.pages[i] < page_count) {
-        printed_pages.push_back(params.pages[i]);
-      }
-    }
-  }
-
-  if (printed_pages.empty())
-    return false;
-
-  PrintMsg_PrintPage_Params page_params;
-  page_params.params = params.params;
-  for (size_t i = 0; i < printed_pages.size(); ++i) {
-    page_params.page_number = printed_pages[i];
-    PrintPageInternal(page_params, frame, &metafile);
-  }
-
-  // blink::printEnd() for PDF should be called before metafile is closed.
-  FinishFramePrinting();
-
-  metafile.FinishDocument();
-
-  PrintHostMsg_DidPrintPage_Params printed_page_params;
-  if (!CopyMetafileDataToSharedMem(metafile,
-                                   &printed_page_params.metafile_data_handle)) {
-    return false;
-  }
-
-  printed_page_params.data_size = metafile.GetDataSize();
-  printed_page_params.document_cookie = params.params.document_cookie;
-
-  for (size_t i = 0; i < printed_pages.size(); ++i) {
-    printed_page_params.page_number = printed_pages[i];
-    Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params));
-    // Send the rest of the pages with an invalid metafile handle.
-    printed_page_params.metafile_data_handle.Release();
-  }
-  return true;
-}
-
-void PrintWebViewHelper::PrintPageInternal(
-    const PrintMsg_PrintPage_Params& params,
-    WebLocalFrame* frame,
-    PdfMetafileSkia* metafile) {
-  PageSizeMargins page_layout_in_points;
-  double scale_factor = 1.0f;
-  ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
-                                  ignore_css_margins_, &scale_factor,
-                                  &page_layout_in_points);
-  gfx::Size page_size;
-  gfx::Rect content_area;
-  GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
-                                          &content_area);
-  gfx::Rect canvas_area = content_area;
-
-  cc::PaintCanvas* canvas =
-      metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor);
-  if (!canvas)
-    return;
-
-  MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile);
-
-  RenderPageContent(frame, params.page_number, canvas_area, content_area,
-                    scale_factor, canvas);
-
-  // Done printing. Close the device context to retrieve the compiled metafile.
-  if (!metafile->FinishPage())
-    NOTREACHED() << "metafile failed";
-}
-
-}  // namespace printing

+ 0 - 127
chromium_src/chrome/renderer/printing/print_web_view_helper_mac.mm

@@ -1,127 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/renderer/printing/print_web_view_helper.h"
-
-#import <AppKit/AppKit.h>
-
-#include "base/logging.h"
-#include "base/mac/scoped_nsautorelease_pool.h"
-#include "base/metrics/histogram.h"
-#include "cc/paint/paint_canvas.h"
-#include "chrome/common/print_messages.h"
-#include "printing/metafile_skia_wrapper.h"
-#include "printing/page_size_margins.h"
-#include "third_party/blink/public/web/web_local_frame.h"
-
-namespace printing {
-
-using blink::WebLocalFrame;
-
-void PrintWebViewHelper::PrintPageInternal(
-    const PrintMsg_PrintPage_Params& params,
-    WebLocalFrame* frame) {
-  PdfMetafileSkia metafile;
-  CHECK(metafile.Init());
-
-  int page_number = params.page_number;
-  gfx::Size page_size_in_dpi;
-  gfx::Rect content_area_in_dpi;
-  RenderPage(print_pages_params_->params, page_number, frame, &metafile,
-             &page_size_in_dpi, &content_area_in_dpi);
-  metafile.FinishDocument();
-
-  PrintHostMsg_DidPrintPage_Params page_params;
-  page_params.data_size = metafile.GetDataSize();
-  page_params.page_number = page_number;
-  page_params.document_cookie = params.params.document_cookie;
-  page_params.page_size = page_size_in_dpi;
-  page_params.content_area = content_area_in_dpi;
-
-  // Ask the browser to create the shared memory for us.
-  if (!CopyMetafileDataToSharedMem(metafile,
-                                   &(page_params.metafile_data_handle))) {
-    // TODO(thestig): Fail and return false instead.
-    page_params.data_size = 0;
-  }
-
-  Send(new PrintHostMsg_DidPrintPage(routing_id(), page_params));
-}
-
-bool PrintWebViewHelper::RenderPreviewPage(
-    int page_number,
-    const PrintMsg_Print_Params& print_params) {
-  PrintMsg_Print_Params printParams = print_params;
-  std::unique_ptr<PdfMetafileSkia> draft_metafile;
-  PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
-
-  bool render_to_draft =
-      print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_;
-
-  if (render_to_draft) {
-    draft_metafile.reset(new PdfMetafileSkia());
-    CHECK(draft_metafile->Init());
-    initial_render_metafile = draft_metafile.get();
-  }
-
-  base::TimeTicks begin_time = base::TimeTicks::Now();
-  gfx::Size page_size;
-  RenderPage(printParams, page_number, print_preview_context_.prepared_frame(),
-             initial_render_metafile, &page_size, nullptr);
-  print_preview_context_.RenderedPreviewPage(base::TimeTicks::Now() -
-                                             begin_time);
-
-  if (draft_metafile.get()) {
-    draft_metafile->FinishDocument();
-  } else {
-    if (print_preview_context_.IsModifiable() &&
-        print_preview_context_.generate_draft_pages()) {
-      DCHECK(!draft_metafile.get());
-      draft_metafile =
-          print_preview_context_.metafile()->GetMetafileForCurrentPage(
-              SkiaDocumentType::PDF);
-    }
-  }
-  return PreviewPageRendered(page_number, draft_metafile.get());
-}
-
-void PrintWebViewHelper::RenderPage(const PrintMsg_Print_Params& params,
-                                    int page_number,
-                                    WebLocalFrame* frame,
-                                    PdfMetafileSkia* metafile,
-                                    gfx::Size* page_size,
-                                    gfx::Rect* content_rect) {
-  double scale_factor = 1.0f;
-  double webkit_shrink_factor = frame->GetPrintPageShrink(page_number);
-  PageSizeMargins page_layout_in_points;
-  gfx::Rect content_area;
-
-  ComputePageLayoutInPointsForCss(frame, page_number, params,
-                                  ignore_css_margins_, &scale_factor,
-                                  &page_layout_in_points);
-  GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, page_size,
-                                          &content_area);
-  if (content_rect)
-    *content_rect = content_area;
-
-  scale_factor *= webkit_shrink_factor;
-
-  gfx::Rect canvas_area = content_area;
-
-  {
-    cc::PaintCanvas* canvas = metafile->GetVectorCanvasForNewPage(
-        *page_size, canvas_area, scale_factor);
-    if (!canvas)
-      return;
-
-    MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile);
-    RenderPageContent(frame, page_number, canvas_area, content_area,
-                      scale_factor, static_cast<cc::PaintCanvas*>(canvas));
-  }
-
-  // Done printing. Close the device context to retrieve the compiled metafile.
-  metafile->FinishPage();
-}
-
-}  // namespace printing

+ 0 - 207
chromium_src/chrome/renderer/printing/print_web_view_helper_pdf_win.cc

@@ -1,207 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/renderer/printing/print_web_view_helper.h"
-
-#include <memory>
-
-#include "base/logging.h"
-#include "base/process/process_handle.h"
-#include "chrome/common/print_messages.h"
-#include "content/public/renderer/render_thread.h"
-#include "printing/metafile_skia_wrapper.h"
-#include "printing/page_size_margins.h"
-#include "printing/pdf_metafile_skia.h"
-#include "printing/units.h"
-#include "third_party/blink/public/web/web_local_frame.h"
-
-namespace printing {
-
-using blink::WebLocalFrame;
-
-bool PrintWebViewHelper::RenderPreviewPage(
-    int page_number,
-    const PrintMsg_Print_Params& print_params) {
-  PrintMsg_PrintPage_Params page_params;
-  page_params.params = print_params;
-  page_params.page_number = page_number;
-  std::unique_ptr<PdfMetafileSkia> draft_metafile;
-  PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
-  if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) {
-    draft_metafile.reset(new PdfMetafileSkia());
-    initial_render_metafile = draft_metafile.get();
-  }
-
-  base::TimeTicks begin_time = base::TimeTicks::Now();
-  PrintPageInternal(page_params, print_preview_context_.prepared_frame(),
-                    initial_render_metafile, NULL, NULL);
-  print_preview_context_.RenderedPreviewPage(base::TimeTicks::Now() -
-                                             begin_time);
-  if (draft_metafile.get()) {
-    draft_metafile->FinishDocument();
-  } else if (print_preview_context_.IsModifiable() &&
-             print_preview_context_.generate_draft_pages()) {
-    DCHECK(!draft_metafile.get());
-    draft_metafile =
-        print_preview_context_.metafile()->GetMetafileForCurrentPage(
-            SkiaDocumentType::PDF);
-  }
-  return PreviewPageRendered(page_number, draft_metafile.get());
-}
-
-bool PrintWebViewHelper::PrintPagesNative(blink::WebLocalFrame* frame,
-                                          int page_count) {
-  PdfMetafileSkia metafile;
-  if (!metafile.Init())
-    return false;
-
-  const PrintMsg_PrintPages_Params& params = *print_pages_params_;
-  std::vector<int> printed_pages;
-  if (params.pages.empty()) {
-    for (int i = 0; i < page_count; ++i) {
-      printed_pages.push_back(i);
-    }
-  } else {
-    // TODO(vitalybuka): redesign to make more code cross platform.
-    for (size_t i = 0; i < params.pages.size(); ++i) {
-      if (params.pages[i] >= 0 && params.pages[i] < page_count) {
-        printed_pages.push_back(params.pages[i]);
-      }
-    }
-  }
-  if (printed_pages.empty())
-    return false;
-
-  std::vector<gfx::Size> page_size_in_dpi(printed_pages.size());
-  std::vector<gfx::Rect> content_area_in_dpi(printed_pages.size());
-
-  PrintMsg_PrintPage_Params page_params;
-  page_params.params = params.params;
-  for (size_t i = 0; i < printed_pages.size(); ++i) {
-    page_params.page_number = printed_pages[i];
-    PrintPageInternal(page_params, frame, &metafile, &page_size_in_dpi[i],
-                      &content_area_in_dpi[i]);
-  }
-
-  // blink::printEnd() for PDF should be called before metafile is closed.
-  FinishFramePrinting();
-
-  metafile.FinishDocument();
-
-  PrintHostMsg_DidPrintPage_Params printed_page_params;
-  if (!CopyMetafileDataToSharedMem(metafile,
-                                   &printed_page_params.metafile_data_handle)) {
-    return false;
-  }
-
-  printed_page_params.content_area = params.params.printable_area;
-  printed_page_params.data_size = metafile.GetDataSize();
-  printed_page_params.document_cookie = params.params.document_cookie;
-  printed_page_params.page_size = params.params.page_size;
-
-  for (size_t i = 0; i < printed_pages.size(); ++i) {
-    printed_page_params.page_number = printed_pages[i];
-    printed_page_params.page_size = page_size_in_dpi[i];
-    printed_page_params.content_area = content_area_in_dpi[i];
-    Send(new PrintHostMsg_DidPrintPage(routing_id(), printed_page_params));
-    // Send the rest of the pages with an invalid metafile handle.
-    printed_page_params.metafile_data_handle.Close();
-    printed_page_params.metafile_data_handle = base::SharedMemoryHandle();
-  }
-  return true;
-}
-
-void PrintWebViewHelper::PrintPageInternal(
-    const PrintMsg_PrintPage_Params& params,
-    WebLocalFrame* frame,
-    PdfMetafileSkia* metafile,
-    gfx::Size* page_size_in_dpi,
-    gfx::Rect* content_area_in_dpi) {
-  PageSizeMargins page_layout_in_points;
-  double css_scale_factor = 1.0f;
-  ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
-                                  ignore_css_margins_, &css_scale_factor,
-                                  &page_layout_in_points);
-  gfx::Size page_size;
-  gfx::Rect content_area;
-  GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
-                                          &content_area);
-  int dpi = static_cast<int>(params.params.dpi);
-  // Calculate the actual page size and content area in dpi.
-  if (page_size_in_dpi) {
-    *page_size_in_dpi =
-        gfx::Size(static_cast<int>(ConvertUnitDouble(page_size.width(),
-                                                     kPointsPerInch, dpi)),
-                  static_cast<int>(ConvertUnitDouble(page_size.height(),
-                                                     kPointsPerInch, dpi)));
-  }
-
-  if (content_area_in_dpi) {
-    // Output PDF matches paper size and should be printer edge to edge.
-    *content_area_in_dpi =
-        gfx::Rect(0, 0, page_size_in_dpi->width(), page_size_in_dpi->height());
-  }
-
-  gfx::Rect canvas_area = content_area;
-#if 0
-      params.params.display_header_footer ? gfx::Rect(page_size) : content_area;
-#endif
-
-  float webkit_page_shrink_factor =
-      frame->GetPrintPageShrink(params.page_number);
-  float scale_factor = css_scale_factor * webkit_page_shrink_factor;
-
-  cc::PaintCanvas* canvas =
-      metafile->GetVectorCanvasForNewPage(page_size, canvas_area, scale_factor);
-  if (!canvas)
-    return;
-
-  MetafileSkiaWrapper::SetMetafileOnCanvas(canvas, metafile);
-
-#if 0
-  if (params.params.display_header_footer) {
-    // |page_number| is 0-based, so 1 is added.
-    PrintHeaderAndFooter(canvas.get(),
-                         params.page_number + 1,
-                         print_preview_context_.total_page_count(),
-                         *frame,
-                         scale_factor,
-                         page_layout_in_points,
-                         params.params);
-  }
-#endif
-
-  float webkit_scale_factor =
-      RenderPageContent(frame, params.page_number, canvas_area, content_area,
-                        scale_factor, canvas);
-  DCHECK_GT(webkit_scale_factor, 0.0f);
-  // Done printing. Close the device context to retrieve the compiled metafile.
-  if (!metafile->FinishPage())
-    NOTREACHED() << "metafile failed";
-}
-
-bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
-    const PdfMetafileSkia& metafile,
-    base::SharedMemoryHandle* shared_mem_handle) {
-  uint32_t buf_size = metafile.GetDataSize();
-  if (buf_size == 0)
-    return false;
-
-  std::unique_ptr<base::SharedMemory> shared_buf(
-      content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(buf_size));
-  if (!shared_buf)
-    return false;
-
-  if (!shared_buf->Map(buf_size))
-    return false;
-
-  if (!metafile.GetData(shared_buf->memory(), buf_size))
-    return false;
-
-  *shared_mem_handle =
-      base::SharedMemory::DuplicateHandle(shared_buf->handle());
-  return true;
-}
-
-}  // namespace printing

+ 0 - 176
chromium_src/chrome/utility/printing_handler_win.cc

@@ -1,176 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/utility/printing_handler_win.h"
-
-#include "base/files/file_util.h"
-#include "base/lazy_instance.h"
-#include "base/path_service.h"
-#include "base/scoped_native_library.h"
-#include "chrome/common/chrome_utility_printing_messages.h"
-#include "chrome/common/print_messages.h"
-#include "content/public/utility/utility_thread.h"
-#include "pdf/pdf.h"
-#include "printing/emf_win.h"
-#include "printing/page_range.h"
-#include "printing/pdf_render_settings.h"
-#include "ui/gfx/gdi_util.h"
-
-namespace printing {
-
-namespace {
-
-bool Send(IPC::Message* message) {
-  return content::UtilityThread::Get()->Send(message);
-}
-
-void ReleaseProcessIfNeeded() {
-  content::UtilityThread::Get()->ReleaseProcess();
-}
-
-void PreCacheFontCharacters(const LOGFONT* logfont,
-                            const wchar_t* text,
-                            size_t text_length) {
-#if 0
-  Send(new ChromeUtilityHostMsg_PreCacheFontCharacters(
-      *logfont, base::string16(text, text_length)));
-#endif
-}
-
-}  // namespace
-
-PrintingHandlerWin::PrintingHandlerWin() {
-  chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(PreCacheFontCharacters);
-}
-
-PrintingHandlerWin::~PrintingHandlerWin() {}
-
-bool PrintingHandlerWin::OnMessageReceived(const IPC::Message& message) {
-  bool handled = true;
-  IPC_BEGIN_MESSAGE_MAP(PrintingHandlerWin, message)
-    IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
-                        OnRenderPDFPagesToMetafile)
-    IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage,
-                        OnRenderPDFPagesToMetafileGetPage)
-    IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop,
-                        OnRenderPDFPagesToMetafileStop)
-    IPC_MESSAGE_UNHANDLED(handled = false)
-  IPC_END_MESSAGE_MAP()
-  return handled;
-}
-
-void PrintingHandlerWin::OnRenderPDFPagesToMetafile(
-    IPC::PlatformFileForTransit pdf_transit,
-    const PdfRenderSettings& settings) {
-  pdf_rendering_settings_ = settings;
-  chrome_pdf::SetPDFUseGDIPrinting(pdf_rendering_settings_.mode ==
-                                   PdfRenderSettings::Mode::GDI_TEXT);
-  int postscript_level;
-  switch (pdf_rendering_settings_.mode) {
-    case PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2:
-      postscript_level = chrome_pdf::PrintingMode::kPostScript2;
-      break;
-    case PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3:
-      postscript_level = chrome_pdf::PrintingMode::kPostScript3;
-      break;
-    default:
-      postscript_level =
-          chrome_pdf::PrintingMode::kEmf;  // Not using postscript.
-  }
-  chrome_pdf::SetPDFUsePrintMode(postscript_level);
-
-  base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit);
-  int page_count = LoadPDF(std::move(pdf_file));
-  Send(
-      new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount(page_count));
-}
-
-void PrintingHandlerWin::OnRenderPDFPagesToMetafileGetPage(
-    int page_number,
-    IPC::PlatformFileForTransit output_file) {
-  base::File emf_file = IPC::PlatformFileForTransitToFile(output_file);
-  float scale_factor = 1.0f;
-  bool postscript = pdf_rendering_settings_.mode ==
-                        PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2 ||
-                    pdf_rendering_settings_.mode ==
-                        PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3;
-  bool success = RenderPdfPageToMetafile(page_number, std::move(emf_file),
-                                         &scale_factor, postscript);
-  Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone(
-      success, scale_factor));
-}
-
-void PrintingHandlerWin::OnRenderPDFPagesToMetafileStop() {
-  ReleaseProcessIfNeeded();
-}
-
-int PrintingHandlerWin::LoadPDF(base::File pdf_file) {
-  int64_t length64 = pdf_file.GetLength();
-  if (length64 <= 0 || length64 > std::numeric_limits<int>::max())
-    return 0;
-  int length = static_cast<int>(length64);
-
-  pdf_data_.resize(length);
-  if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size()))
-    return 0;
-
-  int total_page_count = 0;
-  if (!chrome_pdf::GetPDFDocInfo(&pdf_data_.front(), pdf_data_.size(),
-                                 &total_page_count, nullptr)) {
-    return 0;
-  }
-  return total_page_count;
-}
-
-bool PrintingHandlerWin::RenderPdfPageToMetafile(int page_number,
-                                                 base::File output_file,
-                                                 float* scale_factor,
-                                                 bool postscript) {
-  Emf metafile;
-  metafile.Init();
-
-  // We need to scale down DC to fit an entire page into DC available area.
-  // Current metafile is based on screen DC and have current screen size.
-  // Writing outside of those boundaries will result in the cut-off output.
-  // On metafiles (this is the case here), scaling down will still record
-  // original coordinates and we'll be able to print in full resolution.
-  // Before playback we'll need to counter the scaling up that will happen
-  // in the service (print_system_win.cc).
-  //
-  // The postscript driver does not use the metafile size since it outputs
-  // postscript rather than a metafile. Instead it uses the printable area
-  // sent to RenderPDFPageToDC to determine the area to render. Therefore,
-  // don't scale the DC to match the metafile, and send the printer physical
-  // offsets to the driver.
-  if (!postscript) {
-    *scale_factor = gfx::CalculatePageScale(
-        metafile.context(), pdf_rendering_settings_.area.right(),
-        pdf_rendering_settings_.area.bottom());
-    gfx::ScaleDC(metafile.context(), *scale_factor);
-  }
-
-  // The underlying metafile is of type Emf and ignores the arguments passed
-  // to StartPage.
-  metafile.StartPage(gfx::Size(), gfx::Rect(), 1);
-  int offset_x = postscript ? pdf_rendering_settings_.offsets.x() : 0;
-  int offset_y = postscript ? pdf_rendering_settings_.offsets.y() : 0;
-
-  if (!chrome_pdf::RenderPDFPageToDC(
-          &pdf_data_.front(), pdf_data_.size(), page_number, metafile.context(),
-          pdf_rendering_settings_.dpi.width(),
-          pdf_rendering_settings_.dpi.height(),
-          pdf_rendering_settings_.area.x() - offset_x,
-          pdf_rendering_settings_.area.y() - offset_y,
-          pdf_rendering_settings_.area.width(),
-          pdf_rendering_settings_.area.height(), true, false, true, true,
-          pdf_rendering_settings_.use_color,
-          pdf_rendering_settings_.autorotate)) {
-    return false;
-  }
-  metafile.FinishPage();
-  metafile.FinishDocument();
-  return metafile.SaveTo(&output_file);
-}
-
-}  // namespace printing

+ 0 - 52
chromium_src/chrome/utility/printing_handler_win.h

@@ -1,52 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_UTILITY_PRINTING_HANDLER_WIN_H_
-#define CHROME_UTILITY_PRINTING_HANDLER_WIN_H_
-
-#include "base/compiler_specific.h"
-#include "base/macros.h"
-#include "chrome/utility/utility_message_handler.h"
-#include "ipc/ipc_platform_file.h"
-#include "printing/pdf_render_settings.h"
-
-namespace printing {
-
-struct PdfRenderSettings;
-struct PwgRasterSettings;
-struct PageRange;
-
-// Dispatches IPCs for printing.
-class PrintingHandlerWin : public UtilityMessageHandler {
- public:
-  PrintingHandlerWin();
-  ~PrintingHandlerWin() override;
-
-  // IPC::Listener:
-  bool OnMessageReceived(const IPC::Message& message) override;
-
- private:
-  // IPC message handlers.
-  void OnRenderPDFPagesToMetafile(IPC::PlatformFileForTransit pdf_transit,
-                                  const PdfRenderSettings& settings);
-  void OnRenderPDFPagesToMetafileGetPage(
-      int page_number,
-      IPC::PlatformFileForTransit output_file);
-  void OnRenderPDFPagesToMetafileStop();
-
-  int LoadPDF(base::File pdf_file);
-  bool RenderPdfPageToMetafile(int page_number,
-                               base::File output_file,
-                               float* scale_factor,
-                               bool postscript);
-
-  std::vector<char> pdf_data_;
-  PdfRenderSettings pdf_rendering_settings_;
-
-  DISALLOW_COPY_AND_ASSIGN(PrintingHandlerWin);
-};
-
-}  // namespace printing
-
-#endif  // CHROME_UTILITY_PRINTING_HANDLER_WIN_H_

+ 0 - 21
chromium_src/chrome/utility/utility_message_handler.h

@@ -1,21 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_UTILITY_UTILITY_MESSAGE_HANDLER_H_
-#define CHROME_UTILITY_UTILITY_MESSAGE_HANDLER_H_
-
-namespace IPC {
-class Message;
-}
-
-class UtilityMessageHandler {
- public:
-  virtual ~UtilityMessageHandler() {}
-
-  // Called when a message is received.  Returns true iff the message was
-  // handled.
-  virtual bool OnMessageReceived(const IPC::Message& message) = 0;
-};
-
-#endif  // CHROME_UTILITY_UTILITY_MESSAGE_HANDLER_H_

+ 16 - 0
electron_strings.grdp

@@ -5,6 +5,22 @@
     V8 Proxy Resolver
   </message>
 
+  <!-- PDF Compositor Service -->
+  <message name="IDS_UTILITY_PROCESS_PDF_COMPOSITOR_SERVICE_NAME" desc="The name of the utility process used for PDF compositing.">
+    PDF Compositor Service
+  </message>
+
+  <!-- Printing Service -->
+  <message name="IDS_UTILITY_PROCESS_PRINTING_SERVICE_NAME" desc="The name of the utility process used for printing conversions.">
+    Printing Service
+  </message>
+  <message name="IDS_PRINT_INVALID_PRINTER_SETTINGS" desc="Message to display when selected printer is not reachable or its settings are invalid.">
+    The selected printer is not available or not installed correctly. <ph name="BR">&lt;br&gt;</ph> Check your printer or try selecting another printer.
+  </message>
+  <message name="IDS_DEFAULT_PRINT_DOCUMENT_TITLE" desc="Default title for a print document">
+    Untitled Document
+  </message>
+
   <!-- Desktop Capturer API -->
   <message name="IDS_DESKTOP_MEDIA_PICKER_SINGLE_SCREEN_NAME" desc="Name for screens in the desktop media picker UI when there is only one monitor.">
     Entire screen

+ 4 - 35
filenames.gni

@@ -581,8 +581,8 @@ filenames = {
     "atom/renderer/web_worker_observer.h",
     "atom/utility/atom_content_utility_client.cc",
     "atom/utility/atom_content_utility_client.h",
-    "chromium_src/chrome/browser/browser_process.cc",
-    "chromium_src/chrome/browser/browser_process.h",
+    "chromium_src/chrome/browser/browser_process_impl.cc",
+    "chromium_src/chrome/browser/browser_process_impl.h",
     "chromium_src/chrome/browser/chrome_process_finder_win.cc",
     "chromium_src/chrome/browser/chrome_process_finder_win.h",
     "chromium_src/chrome/browser/chrome_notification_types.h",
@@ -594,25 +594,6 @@ filenames = {
     "chromium_src/chrome/browser/extensions/global_shortcut_listener_x11.h",
     "chromium_src/chrome/browser/extensions/global_shortcut_listener_win.cc",
     "chromium_src/chrome/browser/extensions/global_shortcut_listener_win.h",
-    "chromium_src/chrome/browser/printing/print_job.cc",
-    "chromium_src/chrome/browser/printing/print_job.h",
-    "chromium_src/chrome/browser/printing/print_job_manager.cc",
-    "chromium_src/chrome/browser/printing/print_job_manager.h",
-    "chromium_src/chrome/browser/printing/print_job_worker.cc",
-    "chromium_src/chrome/browser/printing/print_job_worker.h",
-    "chromium_src/chrome/browser/printing/print_job_worker_owner.cc",
-    "chromium_src/chrome/browser/printing/print_job_worker_owner.h",
-    "chromium_src/chrome/browser/printing/print_view_manager_base.cc",
-    "chromium_src/chrome/browser/printing/print_view_manager_base.h",
-    "chromium_src/chrome/browser/printing/print_view_manager_basic.cc",
-    "chromium_src/chrome/browser/printing/print_view_manager_basic.h",
-    "chromium_src/chrome/browser/printing/print_view_manager_observer.h",
-    "chromium_src/chrome/browser/printing/printer_query.cc",
-    "chromium_src/chrome/browser/printing/printer_query.h",
-    "chromium_src/chrome/browser/printing/printing_message_filter.cc",
-    "chromium_src/chrome/browser/printing/printing_message_filter.h",
-    "chromium_src/chrome/browser/printing/print_preview_message_handler.cc",
-    "chromium_src/chrome/browser/printing/print_preview_message_handler.h",
     "chromium_src/chrome/browser/process_singleton_posix.cc",
     "chromium_src/chrome/browser/process_singleton_win.cc",
     "chromium_src/chrome/browser/process_singleton.h",
@@ -632,21 +613,13 @@ filenames = {
     "chromium_src/chrome/browser/ui/views/color_chooser_aura.h",
     "chromium_src/chrome/browser/ui/views/frame/global_menu_bar_registrar_x11.cc",
     "chromium_src/chrome/browser/ui/views/frame/global_menu_bar_registrar_x11.h",
-    "chromium_src/chrome/common/print_messages.cc",
-    "chromium_src/chrome/common/print_messages.h",
     "chromium_src/chrome/common/tts_messages.h",
     "chromium_src/chrome/common/tts_utterance_request.cc",
     "chromium_src/chrome/common/tts_utterance_request.h",
-    "chromium_src/chrome/renderer/printing/print_web_view_helper.cc",
-    "chromium_src/chrome/renderer/printing/print_web_view_helper_linux.cc",
-    "chromium_src/chrome/renderer/printing/print_web_view_helper_mac.mm",
-    "chromium_src/chrome/renderer/printing/print_web_view_helper_pdf_win.cc",
-    "chromium_src/chrome/renderer/printing/print_web_view_helper.h",
-    "chromium_src/chrome/renderer/spellchecker/spellcheck_worditerator.cc",
-    "chromium_src/chrome/renderer/spellchecker/spellcheck_worditerator.h",
     "chromium_src/chrome/renderer/tts_dispatcher.cc",
     "chromium_src/chrome/renderer/tts_dispatcher.h",
-    "chromium_src/chrome/utility/utility_message_handler.h",
+    "chromium_src/chrome/renderer/spellchecker/spellcheck_worditerator.cc",
+    "chromium_src/chrome/renderer/spellchecker/spellcheck_worditerator.h",
     "chromium_src/library_loaders/libspeechd_loader.cc",
     "chromium_src/library_loaders/libspeechd.h",
   ]
@@ -660,10 +633,6 @@ filenames = {
     "chromium_src/chrome/browser/ui/views/color_chooser_dialog.cc",
     "chromium_src/chrome/browser/ui/views/color_chooser_dialog.h",
     "chromium_src/chrome/browser/ui/views/color_chooser_win.cc",
-    "chromium_src/chrome/browser/printing/pdf_to_emf_converter.cc",
-    "chromium_src/chrome/browser/printing/pdf_to_emf_converter.h",
-    "chromium_src/chrome/utility/printing_handler_win.cc",
-    "chromium_src/chrome/utility/printing_handler_win.h",
   ]
 
   app_sources = [

+ 25 - 1
lib/browser/api/web-contents.js

@@ -1,5 +1,6 @@
 'use strict'
 
+const features = process.atomBinding('features')
 const { EventEmitter } = require('events')
 const electron = require('electron')
 const path = require('path')
@@ -70,6 +71,7 @@ const defaultPrintingSetting = {
   marginsType: 0,
   isFirstRequest: false,
   requestID: getNextId(),
+  previewUIID: 0,
   previewModifiable: true,
   printToPDF: true,
   printWithCloudPrint: false,
@@ -249,7 +251,29 @@ WebContents.prototype.printToPDF = function (options, callback) {
     printingSetting.mediaSize = PDFPageSizes['A4']
   }
 
-  this._printToPDF(printingSetting, callback)
+  // Chromium expects this in a 0-100 range number, not as float
+  printingSetting.scaleFactor *= 100
+  if (features.isPrintingEnabled()) {
+    this._printToPDF(printingSetting, callback)
+  } else {
+    console.error('Error: Printing feature is disabled.')
+  }
+}
+
+WebContents.prototype.print = function (...args) {
+  if (features.isPrintingEnabled()) {
+    this._print(args)
+  } else {
+    console.error('Error: Printing feature is disabled.')
+  }
+}
+
+WebContents.prototype.getPrinters = function () {
+  if (features.isPrintingEnabled()) {
+    return this._getPrinters()
+  } else {
+    console.error('Error: Printing feature is disabled.')
+  }
 }
 
 WebContents.prototype.getZoomLevel = function (callback) {

+ 3 - 1
manifests/electron_content_browser_manifest_overlay.json

@@ -5,7 +5,9 @@
     "service_manager:connector": {
       "requires": {
         "device": [ "device:geolocation_control" ],
-        "proxy_resolver": [ "factory" ]
+        "proxy_resolver": [ "factory" ],
+        "chrome_printing": [ "converter" ],
+        "pdf_compositor": [ "compositor"]
       }
     }
   }

+ 1 - 0
patches/common/chromium/.patches

@@ -40,6 +40,7 @@ disable-redraw-lock.patch
 v8_context_snapshot_generator.patch
 boringssl_build_gn.patch
 pepper_flash.patch
+printing.patch
 no_cache_storage_check.patch
 blink_fix_prototype_assert.patch
 disable_scroll_begin_dcheck.patch

+ 494 - 0
patches/common/chromium/printing.patch

@@ -0,0 +1,494 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Heilig Benedek <[email protected]>
+Date: Thu, 18 Oct 2018 17:08:18 -0700
+Subject: printing.patch
+
+Add changeset that was previously applied to sources in chromium_src. The
+majority of changes originally come from these PRs:
+  * https://github.com/electron/electron/pull/1835
+  * https://github.com/electron/electron/pull/8596
+
+diff --git a/chrome/browser/printing/print_job_worker.cc b/chrome/browser/printing/print_job_worker.cc
+index 1065e808e621c087bde9320abe019f05292f977c..6cf33877d21a6bf06532f7f9d79804c744f1ad83 100644
+--- a/chrome/browser/printing/print_job_worker.cc
++++ b/chrome/browser/printing/print_job_worker.cc
+@@ -20,7 +20,7 @@
+ #include "chrome/browser/browser_process.h"
+ #include "chrome/browser/chrome_notification_types.h"
+ #include "chrome/browser/printing/print_job.h"
+-#include "chrome/grit/generated_resources.h"
++#include "electron/grit/electron_resources.h"
+ #include "content/public/browser/browser_thread.h"
+ #include "content/public/browser/notification_service.h"
+ #include "content/public/browser/render_frame_host.h"
+diff --git a/chrome/browser/printing/print_view_manager_base.cc b/chrome/browser/printing/print_view_manager_base.cc
+index 9b0c15b3c5016707788476295d53ab9d8a80e338..8b6580220fb0645a2335d625df8f0ceb211db88c 100644
+--- a/chrome/browser/printing/print_view_manager_base.cc
++++ b/chrome/browser/printing/print_view_manager_base.cc
+@@ -27,10 +27,7 @@
+ #include "chrome/browser/printing/print_view_manager_common.h"
+ #include "chrome/browser/printing/printer_query.h"
+ #include "chrome/browser/profiles/profile.h"
+-#include "chrome/browser/ui/simple_message_box.h"
+-#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
+ #include "chrome/common/pref_names.h"
+-#include "chrome/grit/generated_resources.h"
+ #include "components/prefs/pref_service.h"
+ #include "components/printing/browser/print_composite_client.h"
+ #include "components/printing/browser/print_manager_utils.h"
+@@ -45,6 +42,7 @@
+ #include "content/public/browser/render_process_host.h"
+ #include "content/public/browser/render_view_host.h"
+ #include "content/public/browser/web_contents.h"
++#include "electron/grit/electron_resources.h"
+ #include "mojo/public/cpp/system/buffer.h"
+ #include "printing/buildflags/buildflags.h"
+ #include "printing/pdf_metafile_skia.h"
+@@ -67,6 +65,8 @@ using PrintSettingsCallback =
+     base::OnceCallback<void(scoped_refptr<PrinterQuery>)>;
+ 
+ void ShowWarningMessageBox(const base::string16& message) {
++  LOG(ERROR) << "Invalid printer settings " << message;
++#if 0
+   // Runs always on the UI thread.
+   static bool is_dialog_shown = false;
+   if (is_dialog_shown)
+@@ -75,6 +75,7 @@ void ShowWarningMessageBox(const base::string16& message) {
+   base::AutoReset<bool> auto_reset(&is_dialog_shown, true);
+ 
+   chrome::ShowWarningMessageBox(nullptr, base::string16(), message);
++#endif
+ }
+ 
+ #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
+@@ -112,12 +113,14 @@ PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
+       queue_(g_browser_process->print_job_manager()->queue()),
+       weak_ptr_factory_(this) {
+   DCHECK(queue_);
++#if 0
+   Profile* profile =
+       Profile::FromBrowserContext(web_contents->GetBrowserContext());
+   printing_enabled_.Init(
+       prefs::kPrintingEnabled, profile->GetPrefs(),
+       base::Bind(&PrintViewManagerBase::UpdatePrintingEnabled,
+                  weak_ptr_factory_.GetWeakPtr()));
++#endif
+ }
+ 
+ PrintViewManagerBase::~PrintViewManagerBase() {
+@@ -125,12 +128,14 @@ PrintViewManagerBase::~PrintViewManagerBase() {
+   DisconnectFromCurrentPrintJob();
+ }
+ 
+-bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
++bool PrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh,
++                                    std::unique_ptr<IPC::Message> message,
++                                    CompletionCallback callback) {
+   DisconnectFromCurrentPrintJob();
+ 
+   SetPrintingRFH(rfh);
+-  int32_t id = rfh->GetRoutingID();
+-  return PrintNowInternal(rfh, std::make_unique<PrintMsg_PrintPages>(id));
++  callback_ = std::move(callback);
++  return PrintNowInternal(rfh, std::move(message));
+ }
+ 
+ #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
+@@ -247,9 +252,9 @@ void PrintViewManagerBase::StartLocalPrintJob(
+ void PrintViewManagerBase::UpdatePrintingEnabled() {
+   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+   // The Unretained() is safe because ForEachFrame() is synchronous.
+-  web_contents()->ForEachFrame(base::BindRepeating(
+-      &PrintViewManagerBase::SendPrintingEnabled, base::Unretained(this),
+-      printing_enabled_.GetValue()));
++  web_contents()->ForEachFrame(
++      base::BindRepeating(&PrintViewManagerBase::SendPrintingEnabled,
++                          base::Unretained(this), true));
+ }
+ 
+ void PrintViewManagerBase::NavigationStopped() {
+@@ -316,7 +321,7 @@ void PrintViewManagerBase::OnDidPrintDocument(
+   }
+ 
+   auto* client = PrintCompositeClient::FromWebContents(web_contents());
+-  if (IsOopifEnabled() && !PrintingPdfContent(render_frame_host)) {
++  if (IsOopifEnabled()) {
+     client->DoCompositeDocumentToPdf(
+         params.document_cookie, render_frame_host, content.metafile_data_handle,
+         content.data_size, content.subframe_content_info,
+@@ -342,7 +347,7 @@ void PrintViewManagerBase::OnPrintingFailed(int cookie) {
+   PrintManager::OnPrintingFailed(cookie);
+ 
+ #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
+-  ShowPrintErrorDialog();
++  // ShowPrintErrorDialog();
+ #endif
+ 
+   ReleasePrinterQuery();
+@@ -592,6 +597,9 @@ void PrintViewManagerBase::ReleasePrintJob() {
+   content::RenderFrameHost* rfh = printing_rfh_;
+   printing_rfh_ = nullptr;
+ 
++  if (!callback_.is_null())
++    std::move(callback_).Run(printing_succeeded_);
++
+   if (!print_job_)
+     return;
+ 
+diff --git a/chrome/browser/printing/print_view_manager_base.h b/chrome/browser/printing/print_view_manager_base.h
+index fe0e0b5b065cdcc4edd04665271db14b1bf935e3..be2b3ceb0bd582d499372374349bdd685a1994b6 100644
+--- a/chrome/browser/printing/print_view_manager_base.h
++++ b/chrome/browser/printing/print_view_manager_base.h
+@@ -38,6 +38,8 @@ class PrintJob;
+ class PrintQueriesQueue;
+ class PrinterQuery;
+ 
++using CompletionCallback = base::OnceCallback<void(bool)>;
++
+ // Base class for managing the print commands for a WebContents.
+ class PrintViewManagerBase : public content::NotificationObserver,
+                              public PrintManager {
+@@ -47,7 +49,9 @@ class PrintViewManagerBase : public content::NotificationObserver,
+   // Prints the current document immediately. Since the rendering is
+   // asynchronous, the actual printing will not be completed on the return of
+   // this function. Returns false if printing is impossible at the moment.
+-  virtual bool PrintNow(content::RenderFrameHost* rfh);
++  virtual bool PrintNow(content::RenderFrameHost* rfh,
++                        std::unique_ptr<IPC::Message> message,
++                        CompletionCallback callback);
+ 
+ #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
+   // Prints the document in |print_data| with settings specified in
+@@ -195,6 +199,9 @@ class PrintViewManagerBase : public content::NotificationObserver,
+   // The current RFH that is printing with a system printing dialog.
+   content::RenderFrameHost* printing_rfh_;
+ 
++  // Responded with success of the print job.
++  CompletionCallback callback_;
++
+   // Indication of success of the print job.
+   bool printing_succeeded_;
+ 
+diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc
+index 54866fdcdb64f4ffd2414c8637ffa2f8fb10c024..e1208906a6145b30c72a484a3b29f47279d75a0e 100644
+--- a/chrome/browser/printing/printing_message_filter.cc
++++ b/chrome/browser/printing/printing_message_filter.cc
+@@ -20,6 +20,7 @@
+ #include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
+ #include "components/printing/browser/print_manager_utils.h"
+ #include "components/printing/common/print_messages.h"
++#include "content/public/browser/browser_context.h"
+ #include "content/public/browser/render_frame_host.h"
+ #include "content/public/browser/web_contents.h"
+ #include "content/public/common/child_process_host.h"
+@@ -94,20 +95,23 @@ PrintViewManager* GetPrintViewManager(int render_process_id,
+ 
+ }  // namespace
+ 
+-PrintingMessageFilter::PrintingMessageFilter(int render_process_id,
+-                                             Profile* profile)
++PrintingMessageFilter::PrintingMessageFilter(
++    int render_process_id,
++    content::BrowserContext* browser_context)
+     : BrowserMessageFilter(PrintMsgStart),
+       render_process_id_(render_process_id),
+       queue_(g_browser_process->print_job_manager()->queue()) {
+   DCHECK(queue_.get());
+   printing_shutdown_notifier_ =
+       PrintingMessageFilterShutdownNotifierFactory::GetInstance()
+-          ->Get(profile)
++          ->Get(browser_context)
+           ->Subscribe(base::Bind(&PrintingMessageFilter::ShutdownOnUIThread,
+                                  base::Unretained(this)));
++#if 0
+   is_printing_enabled_.Init(prefs::kPrintingEnabled, profile->GetPrefs());
+   is_printing_enabled_.MoveToThread(
+       BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
++#endif
+ }
+ 
+ PrintingMessageFilter::~PrintingMessageFilter() {
+@@ -188,11 +192,13 @@ void PrintingMessageFilter::OnTempFileForPrintingWritten(int render_frame_id,
+ void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) {
+   DCHECK_CURRENTLY_ON(BrowserThread::IO);
+   scoped_refptr<PrinterQuery> printer_query;
++#if 0
+   if (!is_printing_enabled_.GetValue()) {
+     // Reply with NULL query.
+     OnGetDefaultPrintSettingsReply(printer_query, reply_msg);
+     return;
+   }
++#endif
+   printer_query = queue_->PopPrinterQuery(0);
+   if (!printer_query.get()) {
+     printer_query =
+@@ -301,11 +307,13 @@ void PrintingMessageFilter::OnUpdatePrintSettings(
+   std::unique_ptr<base::DictionaryValue> new_settings(job_settings.DeepCopy());
+ 
+   scoped_refptr<PrinterQuery> printer_query;
++#if 0
+   if (!is_printing_enabled_.GetValue()) {
+     // Reply with NULL query.
+     OnUpdatePrintSettingsReply(printer_query, reply_msg);
+     return;
+   }
++#endif
+   printer_query = queue_->PopPrinterQuery(document_cookie);
+   if (!printer_query.get()) {
+     printer_query = queue_->CreatePrinterQuery(
+@@ -364,7 +372,7 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply(
+ #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
+ void PrintingMessageFilter::OnCheckForCancel(const PrintHostMsg_PreviewIds& ids,
+                                              bool* cancel) {
+-  PrintPreviewUI::GetCurrentPrintPreviewStatus(ids, cancel);
++  // PrintPreviewUI::GetCurrentPrintPreviewStatus(ids, cancel);
+ }
+ #endif
+ 
+diff --git a/chrome/browser/printing/printing_message_filter.h b/chrome/browser/printing/printing_message_filter.h
+index a881a853bfb0b46d0e074b7e86121429a5a761a3..87efeab40574ac72e4dea33f3ff68919bff93839 100644
+--- a/chrome/browser/printing/printing_message_filter.h
++++ b/chrome/browser/printing/printing_message_filter.h
+@@ -27,6 +27,10 @@ class DictionaryValue;
+ #if defined(OS_ANDROID)
+ struct FileDescriptor;
+ #endif
++}  // namespace base
++
++namespace content {
++class BrowserContext;
+ }
+ 
+ namespace printing {
+@@ -38,7 +42,8 @@ class PrinterQuery;
+ // renderer process on the IPC thread.
+ class PrintingMessageFilter : public content::BrowserMessageFilter {
+  public:
+-  PrintingMessageFilter(int render_process_id, Profile* profile);
++  PrintingMessageFilter(int render_process_id,
++                        content::BrowserContext* browser_context);
+ 
+   // content::BrowserMessageFilter methods.
+   void OverrideThreadForMessage(const IPC::Message& message,
+@@ -73,6 +78,7 @@ class PrintingMessageFilter : public content::BrowserMessageFilter {
+ 
+   // Get the default print setting.
+   void OnGetDefaultPrintSettings(IPC::Message* reply_msg);
++
+   void OnGetDefaultPrintSettingsReply(scoped_refptr<PrinterQuery> printer_query,
+                                       IPC::Message* reply_msg);
+ 
+diff --git a/components/printing/common/print_messages.h b/components/printing/common/print_messages.h
+index d29bb6aedecd228e4bc02c84b86cce7151f33746..b489694b2701bddcfa95e187f5fcbf65d9a8ae8c 100644
+--- a/components/printing/common/print_messages.h
++++ b/components/printing/common/print_messages.h
+@@ -367,7 +367,10 @@ IPC_MESSAGE_ROUTED0(PrintMsg_PrintNodeUnderContextMenu)
+ #if BUILDFLAG(ENABLE_PRINTING)
+ // Tells the RenderFrame to switch the CSS to print media type, renders every
+ // requested pages and switch back the CSS to display media type.
+-IPC_MESSAGE_ROUTED0(PrintMsg_PrintPages)
++IPC_MESSAGE_ROUTED3(PrintMsg_PrintPages,
++                    bool /* silent print */,
++                    bool /* print page's background */,
++                    base::DictionaryValue /* settings */)
+ 
+ // Like PrintMsg_PrintPages, but using the print preview document's frame/node.
+ IPC_MESSAGE_ROUTED0(PrintMsg_PrintForSystemDialog)
+diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc
+index c7ea5cf76aa629b8feaf99ff6e289140de1d35bd..aee6e02b86a4cf99e263f5a26025a681e3f41463 100644
+--- a/components/printing/renderer/print_render_frame_helper.cc
++++ b/components/printing/renderer/print_render_frame_helper.cc
+@@ -1045,7 +1045,9 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
+     web_frame->DispatchBeforePrintEvent();
+     if (!weak_this)
+       return;
+-    Print(web_frame, blink::WebNode(), true /* is_scripted? */);
++    Print(web_frame, blink::WebNode(), true /* is_scripted? */,
++          false /* silent */, false /* print_background */,
++          base::DictionaryValue() /* new_settings */);
+     if (weak_this)
+       web_frame->DispatchAfterPrintEvent();
+   }
+@@ -1093,7 +1095,10 @@ void PrintRenderFrameHelper::OnDestruct() {
+   delete this;
+ }
+ 
+-void PrintRenderFrameHelper::OnPrintPages() {
++void PrintRenderFrameHelper::OnPrintPages(
++    bool silent,
++    bool print_background,
++    const base::DictionaryValue& settings) {
+   if (ipc_nesting_level_ > 1)
+     return;
+ 
+@@ -1106,7 +1111,7 @@ void PrintRenderFrameHelper::OnPrintPages() {
+   // If we are printing a PDF extension frame, find the plugin node and print
+   // that instead.
+   auto plugin = delegate_->GetPdfElement(frame);
+-  Print(frame, plugin, false /* is_scripted? */);
++  Print(frame, plugin, false, silent, print_background, settings);
+   if (weak_this)
+     frame->DispatchAfterPrintEvent();
+   // WARNING: |this| may be gone at this point. Do not do any more work here and
+@@ -1122,7 +1127,8 @@ void PrintRenderFrameHelper::OnPrintForSystemDialog() {
+     return;
+   }
+   auto weak_this = weak_ptr_factory_.GetWeakPtr();
+-  Print(frame, print_preview_context_.source_node(), false);
++  Print(frame, print_preview_context_.source_node(), false, false, false,
++        base::DictionaryValue());
+   if (weak_this)
+     frame->DispatchAfterPrintEvent();
+   // WARNING: |this| may be gone at this point. Do not do any more work here and
+@@ -1158,6 +1164,8 @@ void PrintRenderFrameHelper::OnPrintPreview(
+   if (ipc_nesting_level_ > 1)
+     return;
+ 
++  blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
++  print_preview_context_.InitWithFrame(frame);
+   print_preview_context_.OnPrintPreview();
+ 
+   UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent",
+@@ -1541,7 +1549,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
+ 
+     auto self = weak_ptr_factory_.GetWeakPtr();
+     Print(duplicate_node.GetDocument().GetFrame(), duplicate_node,
+-          false /* is_scripted? */);
++          false /* is_scripted? */, false /* silent */,
++          false /* print_background */,
++          base::DictionaryValue() /* new_settings */);
+     // Check if |this| is still valid.
+     if (!self)
+       return;
+@@ -1552,7 +1562,10 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
+ 
+ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
+                                    const blink::WebNode& node,
+-                                   bool is_scripted) {
++                                   bool is_scripted,
++                                   bool silent,
++                                   bool print_background,
++                                   const base::DictionaryValue& settings) {
+   // If still not finished with earlier print request simply ignore.
+   if (prep_frame_view_)
+     return;
+@@ -1560,7 +1573,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
+   FrameReference frame_ref(frame);
+ 
+   int expected_page_count = 0;
+-  if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
++  if (!CalculateNumberOfPages(frame, node, &expected_page_count, settings)) {
+     DidFinishPrinting(FAIL_PRINT_INIT);
+     return;  // Failed to init print page settings.
+   }
+@@ -1580,8 +1593,9 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
+ 
+     PrintMsg_PrintPages_Params print_settings;
+     auto self = weak_ptr_factory_.GetWeakPtr();
+-    GetPrintSettingsFromUser(frame_ref.GetFrame(), node, expected_page_count,
+-                             is_scripted, &print_settings);
++    if (!silent)
++      GetPrintSettingsFromUser(frame_ref.GetFrame(), node, expected_page_count,
++                               is_scripted, &print_settings);
+     // Check if |this| is still valid.
+     if (!self)
+       return;
+@@ -1591,6 +1605,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
+             ? blink::kWebPrintScalingOptionSourceSize
+             : scaling_option;
+     SetPrintPagesParams(print_settings);
++    print_settings.params.should_print_backgrounds = print_background;
+     if (print_settings.params.dpi.IsEmpty() ||
+         !print_settings.params.document_cookie) {
+       DidFinishPrinting(OK);  // Release resources and fail silently on failure.
+@@ -1778,10 +1793,24 @@ std::vector<int> PrintRenderFrameHelper::GetPrintedPages(
+   return printed_pages;
+ }
+ 
+-bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
++bool PrintRenderFrameHelper::InitPrintSettings(
++    bool fit_to_paper_size,
++    const base::DictionaryValue& new_settings) {
+   PrintMsg_PrintPages_Params settings;
+-  Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
+-                                                &settings.params));
++  if (new_settings.empty()) {
++    Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
++                                                  &settings.params));
++  } else {
++    // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
++    // possible.
++    int cookie =
++        print_pages_params_ ? print_pages_params_->params.document_cookie : 0;
++    bool canceled = false;
++    Send(new PrintHostMsg_UpdatePrintSettings(
++        routing_id(), cookie, new_settings, &settings, &canceled));
++    if (canceled)
++      return false;
++  }
+   // Check if the printer returned any settings, if the settings is empty, we
+   // can safely assume there are no printer drivers configured. So we safely
+   // terminate.
+@@ -1801,12 +1830,14 @@ bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
+   return result;
+ }
+ 
+-bool PrintRenderFrameHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame,
+-                                                    const blink::WebNode& node,
+-                                                    int* number_of_pages) {
++bool PrintRenderFrameHelper::CalculateNumberOfPages(
++    blink::WebLocalFrame* frame,
++    const blink::WebNode& node,
++    int* number_of_pages,
++    const base::DictionaryValue& settings) {
+   DCHECK(frame);
+   bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node));
+-  if (!InitPrintSettings(fit_to_paper_size)) {
++  if (!InitPrintSettings(fit_to_paper_size, settings)) {
+     notify_browser_of_print_failure_ = false;
+     Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
+     return false;
+diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
+index 6e2d7e1467b7bc179b1a0fc30dd656de612708cb..8b1516ce5e11d915efdf06cc75f334cd92ce7d99 100644
+--- a/components/printing/renderer/print_render_frame_helper.h
++++ b/components/printing/renderer/print_render_frame_helper.h
+@@ -186,7 +186,9 @@ class PrintRenderFrameHelper
+   bool OnMessageReceived(const IPC::Message& message) override;
+ 
+   // Message handlers ---------------------------------------------------------
+-  void OnPrintPages();
++  void OnPrintPages(bool silent,
++                    bool print_background,
++                    const base::DictionaryValue& settings);
+   void OnPrintForSystemDialog();
+ #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
+   void OnInitiatePrintPreview(bool has_selection);
+@@ -238,7 +240,10 @@ class PrintRenderFrameHelper
+   // WARNING: |this| may be gone after this method returns.
+   void Print(blink::WebLocalFrame* frame,
+              const blink::WebNode& node,
+-             bool is_scripted);
++             bool is_scripted,
++             bool silent,
++             bool print_background,
++             const base::DictionaryValue& settings);
+ 
+   // Notification when printing is done - signal tear-down/free resources.
+   void DidFinishPrinting(PrintingResult result);
+@@ -247,12 +252,14 @@ class PrintRenderFrameHelper
+ 
+   // Initialize print page settings with default settings.
+   // Used only for native printing workflow.
+-  bool InitPrintSettings(bool fit_to_paper_size);
++  bool InitPrintSettings(bool fit_to_paper_size,
++                         const base::DictionaryValue& settings);
+ 
+   // Calculate number of pages in source document.
+   bool CalculateNumberOfPages(blink::WebLocalFrame* frame,
+                               const blink::WebNode& node,
+-                              int* number_of_pages);
++                              int* number_of_pages,
++                              const base::DictionaryValue& settings);
+ 
+ #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
+   // Set options for print preset from source PDF document.

+ 0 - 37
spec/api-browser-window-spec.js

@@ -1720,43 +1720,6 @@ describe('BrowserWindow module', () => {
         })
       })
 
-      it('can get printer list', (done) => {
-        w.destroy()
-        w = new BrowserWindow({
-          show: false,
-          webPreferences: {
-            sandbox: true,
-            preload: preload
-          }
-        })
-        w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
-        w.webContents.once('did-finish-load', () => {
-          const printers = w.webContents.getPrinters()
-          assert.strictEqual(Array.isArray(printers), true)
-          done()
-        })
-      })
-
-      it('can print to PDF', (done) => {
-        w.destroy()
-        w = new BrowserWindow({
-          show: false,
-          webPreferences: {
-            sandbox: true,
-            preload: preload
-          }
-        })
-        w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
-        w.webContents.once('did-finish-load', () => {
-          w.webContents.printToPDF({}, function (error, data) {
-            assert.strictEqual(error, null)
-            assert.strictEqual(data instanceof Buffer, true)
-            assert.notStrictEqual(data.length, 0)
-            done()
-          })
-        })
-      })
-
       it('supports calling preventDefault on new-window events', (done) => {
         w.destroy()
         w = new BrowserWindow({

+ 58 - 0
spec/api-web-contents-spec.js

@@ -10,6 +10,7 @@ const { emittedOnce } = require('./events-helpers')
 const chai = require('chai')
 const dirtyChai = require('dirty-chai')
 
+const features = process.atomBinding('features')
 const { ipcRenderer, remote, clipboard } = require('electron')
 const { BrowserWindow, webContents, ipcMain, session } = remote
 const { expect } = chai
@@ -947,4 +948,61 @@ describe('webContents module', () => {
       done()
     })
   })
+
+  describe('getPrinterList()', () => {
+    before(function () {
+      if (!features.isPrintingEnabled()) {
+        return closeWindow(w).then(() => {
+          w = null
+          this.skip()
+        })
+      }
+    })
+
+    it('can get printer list', (done) => {
+      w.destroy()
+      w = new BrowserWindow({
+        show: false,
+        webPreferences: {
+          sandbox: true
+        }
+      })
+      w.webContents.once('did-finish-load', () => {
+        const printers = w.webContents.getPrinters()
+        assert.strictEqual(Array.isArray(printers), true)
+        done()
+      })
+      w.loadURL('about:blank')
+    })
+  })
+
+  describe('printToPDF()', () => {
+    before(function () {
+      if (!features.isPrintingEnabled()) {
+        return closeWindow(w).then(() => {
+          w = null
+          this.skip()
+        })
+      }
+    })
+
+    it('can print to PDF', (done) => {
+      w.destroy()
+      w = new BrowserWindow({
+        show: false,
+        webPreferences: {
+          sandbox: true
+        }
+      })
+      w.webContents.once('did-finish-load', () => {
+        w.webContents.printToPDF({}, function (error, data) {
+          assert.strictEqual(error, null)
+          assert.strictEqual(data instanceof Buffer, true)
+          assert.notStrictEqual(data.length, 0)
+          done()
+        })
+      })
+      w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
+    })
+  })
 })