print_view_manager_base.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASE_H_
  5. #define CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASE_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/strings/string16.h"
  10. #include "components/prefs/pref_member.h"
  11. #include "content/public/browser/notification_observer.h"
  12. #include "content/public/browser/notification_registrar.h"
  13. #include "content/public/browser/web_contents_observer.h"
  14. #include "content/public/browser/web_contents_user_data.h"
  15. struct PrintHostMsg_DidPrintPage_Params;
  16. namespace content {
  17. class RenderViewHost;
  18. }
  19. namespace printing {
  20. class JobEventDetails;
  21. class MetafilePlayer;
  22. class PrintJob;
  23. class PrintJobWorkerOwner;
  24. class PrintQueriesQueue;
  25. // Base class for managing the print commands for a WebContents.
  26. class PrintViewManagerBase : public content::NotificationObserver,
  27. public content::WebContentsObserver {
  28. public:
  29. ~PrintViewManagerBase() override;
  30. #if !defined(DISABLE_BASIC_PRINTING)
  31. // Prints the current document immediately. Since the rendering is
  32. // asynchronous, the actual printing will not be completed on the return of
  33. // this function. Returns false if printing is impossible at the moment.
  34. virtual bool PrintNow(content::RenderFrameHost* rfh,
  35. bool silent,
  36. bool print_background,
  37. const base::string16& device_name);
  38. #endif // !DISABLE_BASIC_PRINTING
  39. // PrintedPagesSource implementation.
  40. base::string16 RenderSourceName();
  41. void SetCallback(const base::Callback<void(bool)>& cb) { callback = cb; };
  42. protected:
  43. explicit PrintViewManagerBase(content::WebContents* web_contents);
  44. // Helper method for Print*Now().
  45. bool PrintNowInternal(content::RenderFrameHost* rfh,
  46. std::unique_ptr<IPC::Message> message);
  47. // Terminates or cancels the print job if one was pending.
  48. void RenderProcessGone(base::TerminationStatus status) override;
  49. // content::WebContentsObserver implementation.
  50. bool OnMessageReceived(const IPC::Message& message,
  51. content::RenderFrameHost* render_frame_host) override;
  52. // IPC Message handlers.
  53. virtual void OnPrintingFailed(int cookie);
  54. private:
  55. // content::NotificationObserver implementation.
  56. void Observe(int type,
  57. const content::NotificationSource& source,
  58. const content::NotificationDetails& details) override;
  59. // Cancels the print job.
  60. void NavigationStopped() override;
  61. // IPC Message handlers.
  62. void OnDidGetPrintedPagesCount(int cookie, int number_pages);
  63. void OnDidGetDocumentCookie(int cookie);
  64. void OnDidPrintPage(const PrintHostMsg_DidPrintPage_Params& params);
  65. void OnShowInvalidPrinterSettingsError();
  66. // Processes a NOTIFY_PRINT_JOB_EVENT notification.
  67. void OnNotifyPrintJobEvent(const JobEventDetails& event_details);
  68. // Requests the RenderView to render all the missing pages for the print job.
  69. // No-op if no print job is pending. Returns true if at least one page has
  70. // been requested to the renderer.
  71. bool RenderAllMissingPagesNow();
  72. // Quits the current message loop if these conditions hold true: a document is
  73. // loaded and is complete and waiting_for_pages_to_be_rendered_ is true. This
  74. // function is called in DidPrintPage() or on ALL_PAGES_REQUESTED
  75. // notification. The inner message loop is created was created by
  76. // RenderAllMissingPagesNow().
  77. void ShouldQuitFromInnerMessageLoop();
  78. // Creates a new empty print job. It has no settings loaded. If there is
  79. // currently a print job, safely disconnect from it. Returns false if it is
  80. // impossible to safely disconnect from the current print job or it is
  81. // impossible to create a new print job.
  82. bool CreateNewPrintJob(PrintJobWorkerOwner* job);
  83. // Makes sure the current print_job_ has all its data before continuing, and
  84. // disconnect from it.
  85. void DisconnectFromCurrentPrintJob();
  86. // Notify that the printing is done.
  87. void PrintingDone(bool success);
  88. // Terminates the print job. No-op if no print job has been created. If
  89. // |cancel| is true, cancel it instead of waiting for the job to finish. Will
  90. // call ReleasePrintJob().
  91. void TerminatePrintJob(bool cancel);
  92. // Releases print_job_. Correctly deregisters from notifications. No-op if
  93. // no print job has been created.
  94. void ReleasePrintJob();
  95. // Runs an inner message loop. It will set inside_inner_message_loop_ to true
  96. // while the blocking inner message loop is running. This is useful in cases
  97. // where the RenderView is about to be destroyed while a printing job isn't
  98. // finished.
  99. bool RunInnerMessageLoop();
  100. // In the case of Scripted Printing, where the renderer is controlling the
  101. // control flow, print_job_ is initialized whenever possible. No-op is
  102. // print_job_ is initialized.
  103. bool OpportunisticallyCreatePrintJob(int cookie);
  104. // Release the PrinterQuery associated with our |cookie_|.
  105. void ReleasePrinterQuery();
  106. content::NotificationRegistrar registrar_;
  107. // Manages the low-level talk to the printer.
  108. scoped_refptr<PrintJob> print_job_;
  109. // Number of pages to print in the print job.
  110. int number_pages_;
  111. // Indication of success of the print job.
  112. bool printing_succeeded_;
  113. // Running an inner message loop inside RenderAllMissingPagesNow(). This means
  114. // we are _blocking_ until all the necessary pages have been rendered or the
  115. // print settings are being loaded.
  116. bool inside_inner_message_loop_;
  117. #if !defined(OS_MACOSX)
  118. // Set to true when OnDidPrintPage() should be expecting the first page.
  119. bool expecting_first_page_;
  120. #endif // OS_MACOSX
  121. // The document cookie of the current PrinterQuery.
  122. int cookie_;
  123. // Whether printing is enabled.
  124. bool printing_enabled_;
  125. scoped_refptr<printing::PrintQueriesQueue> queue_;
  126. base::Callback<void(bool)> callback;
  127. DISALLOW_COPY_AND_ASSIGN(PrintViewManagerBase);
  128. };
  129. } // namespace printing
  130. #endif // CHROME_BROWSER_PRINTING_PRINT_VIEW_MANAGER_BASE_H_