Home | History | Annotate | Download | only in printing
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
      6 #define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
      7 
      8 #include "base/memory/ref_counted.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/threading/thread.h"
     12 #include "printing/page_number.h"
     13 #include "printing/print_destination_interface.h"
     14 #include "printing/printing_context.h"
     15 #include "printing/print_job_constants.h"
     16 
     17 class PrintingUIWebContentsObserver;
     18 
     19 namespace base {
     20 class DictionaryValue;
     21 }
     22 
     23 namespace printing {
     24 
     25 class PrintedDocument;
     26 class PrintedPage;
     27 class PrintJob;
     28 class PrintJobWorkerOwner;
     29 
     30 // Worker thread code. It manages the PrintingContext, which can be blocking
     31 // and/or run a message loop. This is the object that generates most
     32 // NOTIFY_PRINT_JOB_EVENT notifications, but they are generated through a
     33 // NotificationTask task to be executed from the right thread, the UI thread.
     34 // PrintJob always outlives its worker instance.
     35 class PrintJobWorker : public base::Thread {
     36  public:
     37   explicit PrintJobWorker(PrintJobWorkerOwner* owner);
     38   virtual ~PrintJobWorker();
     39 
     40   void SetNewOwner(PrintJobWorkerOwner* new_owner);
     41 
     42   // Set a destination for print.
     43   // This supercedes the document's rendering destination.
     44   void SetPrintDestination(PrintDestinationInterface* destination);
     45 
     46   // Initializes the print settings. If |ask_user_for_settings| is true, a
     47   // Print... dialog box will be shown to ask the user his preference.
     48   void GetSettings(
     49       bool ask_user_for_settings,
     50       scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
     51       int document_page_count,
     52       bool has_selection,
     53       MarginType margin_type);
     54 
     55   // Set the new print settings. This function takes ownership of
     56   // |new_settings|.
     57   void SetSettings(const base::DictionaryValue* const new_settings);
     58 
     59   // Starts the printing loop. Every pages are printed as soon as the data is
     60   // available. Makes sure the new_document is the right one.
     61   void StartPrinting(PrintedDocument* new_document);
     62 
     63   // Updates the printed document.
     64   void OnDocumentChanged(PrintedDocument* new_document);
     65 
     66   // Dequeues waiting pages. Called when PrintJob receives a
     67   // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
     68   // the next page can be printed.
     69   void OnNewPage();
     70 
     71   // This is the only function that can be called in a thread.
     72   void Cancel();
     73 
     74  protected:
     75   // Retrieves the context for testing only.
     76   PrintingContext* printing_context() { return printing_context_.get(); }
     77 
     78  private:
     79   // The shared NotificationService service can only be accessed from the UI
     80   // thread, so this class encloses the necessary information to send the
     81   // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
     82   // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
     83   // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
     84   class NotificationTask;
     85 
     86   // Renders a page in the printer.
     87   void SpoolPage(PrintedPage* page);
     88 
     89   // Closes the job since spooling is done.
     90   void OnDocumentDone();
     91 
     92   // Discards the current document, the current page and cancels the printing
     93   // context.
     94   void OnFailure();
     95 
     96   // Asks the user for print settings. Must be called on the UI thread.
     97   // Required on Mac and Linux. Windows can display UI from non-main threads,
     98   // but sticks with this for consistency.
     99   void GetSettingsWithUI(
    100       scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
    101       int document_page_count,
    102       bool has_selection);
    103 
    104   // The callback used by PrintingContext::GetSettingsWithUI() to notify this
    105   // object that the print settings are set.  This is needed in order to bounce
    106   // back into the IO thread for GetSettingsDone().
    107   void GetSettingsWithUIDone(PrintingContext::Result result);
    108 
    109   // Called on the UI thread to update the print settings. This function takes
    110   // the ownership of |new_settings|.
    111   void UpdatePrintSettings(const base::DictionaryValue* const new_settings);
    112 
    113   // Reports settings back to owner_.
    114   void GetSettingsDone(PrintingContext::Result result);
    115 
    116   // Use the default settings. When using GTK+ or Mac, this can still end up
    117   // displaying a dialog. So this needs to happen from the UI thread on these
    118   // systems.
    119   void UseDefaultSettings();
    120 
    121   // Information about the printer setting.
    122   scoped_ptr<PrintingContext> printing_context_;
    123 
    124   // The printed document. Only has read-only access.
    125   scoped_refptr<PrintedDocument> document_;
    126 
    127   // The print destination, may be NULL.
    128   scoped_refptr<PrintDestinationInterface> destination_;
    129 
    130   // The print job owning this worker thread. It is guaranteed to outlive this
    131   // object.
    132   PrintJobWorkerOwner* owner_;
    133 
    134   // Current page number to print.
    135   PageNumber page_number_;
    136 
    137   // Used to generate a WeakPtr for callbacks.
    138   base::WeakPtrFactory<PrintJobWorker> weak_factory_;
    139 
    140   DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
    141 };
    142 
    143 }  // namespace printing
    144 
    145 #endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
    146