15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/weak_ptr.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/browser/browser_thread.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "printing/page_number.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "printing/print_job_constants.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "printing/printing_context.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace printing {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrintJob;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrintJobWorkerOwner;
2503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class PrintedDocument;
2603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class PrintedPage;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Worker thread code. It manages the PrintingContext, which can be blocking
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and/or run a message loop. This is the object that generates most
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTIFY_PRINT_JOB_EVENT notifications, but they are generated through a
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NotificationTask task to be executed from the right thread, the UI thread.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PrintJob always outlives its worker instance.
3303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class PrintJobWorker {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  PrintJobWorker(int render_process_id,
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 int render_view_id,
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 PrintJobWorkerOwner* owner);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~PrintJobWorker();
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetNewOwner(PrintJobWorkerOwner* new_owner);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes the print settings. If |ask_user_for_settings| is true, a
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Print... dialog box will be shown to ask the user his preference.
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void GetSettings(
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      bool ask_user_for_settings,
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      int document_page_count,
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      bool has_selection,
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      MarginType margin_type);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Set the new print settings.
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void SetSettings(scoped_ptr<base::DictionaryValue> new_settings);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Starts the printing loop. Every pages are printed as soon as the data is
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // available. Makes sure the new_document is the right one.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StartPrinting(PrintedDocument* new_document);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates the printed document.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDocumentChanged(PrintedDocument* new_document);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Dequeues waiting pages. Called when PrintJob receives a
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the next page can be printed.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnNewPage();
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is the only function that can be called in a thread.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Cancel();
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Returns true if the thread has been started, and not yet stopped.
6903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  bool IsRunning() const;
7003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
7103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Posts the given task to be run.
7203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  bool PostTask(const tracked_objects::Location& from_here,
7303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                const base::Closure& task);
7403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
7503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Signals the thread to exit in the near future.
7603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  void StopSoon();
7703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Signals the thread to exit and returns once the thread has exited.
7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  void Stop();
8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
8103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Starts the thread.
8203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  bool Start();
8303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Retrieves the context for testing only.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrintingContext* printing_context() { return printing_context_.get(); }
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The shared NotificationService service can only be accessed from the UI
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thread, so this class encloses the necessary information to send the
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class NotificationTask;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Renders a page in the printer.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SpoolPage(PrintedPage* page);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Closes the job since spooling is done.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDocumentDone();
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Discards the current document, the current page and cancels the printing
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // context.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnFailure();
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Asks the user for print settings. Must be called on the UI thread.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Required on Mac and Linux. Windows can display UI from non-main threads,
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // but sticks with this for consistency.
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void GetSettingsWithUI(
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      int document_page_count,
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      bool has_selection);
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The callback used by PrintingContext::GetSettingsWithUI() to notify this
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object that the print settings are set.  This is needed in order to bounce
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // back into the IO thread for GetSettingsDone().
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetSettingsWithUIDone(PrintingContext::Result result);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Called on the UI thread to update the print settings.
11903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  void UpdatePrintSettings(scoped_ptr<base::DictionaryValue> new_settings);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reports settings back to owner_.
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetSettingsDone(PrintingContext::Result result);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use the default settings. When using GTK+ or Mac, this can still end up
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // displaying a dialog. So this needs to happen from the UI thread on these
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // systems.
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UseDefaultSettings();
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Printing context delegate.
1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_ptr<PrintingContext::Delegate> printing_context_delegate_;
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Information about the printer setting.
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<PrintingContext> printing_context_;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The printed document. Only has read-only access.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PrintedDocument> document_;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The print job owning this worker thread. It is guaranteed to outlive this
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object.
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrintJobWorkerOwner* owner_;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Current page number to print.
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PageNumber page_number_;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Thread to run worker tasks.
14603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  base::Thread thread_;
14703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
14803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Tread-safe pointer to task runner of the |thread_|.
14903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_refptr<base::SequencedTaskRunner> task_runner_;
15003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used to generate a WeakPtr for callbacks.
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::WeakPtrFactory<PrintJobWorker> weak_factory_;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace printing
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
160