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 #include "ui/gfx/native_widget_types.h"
     17 
     18 namespace base {
     19 class DictionaryValue;
     20 }
     21 
     22 namespace printing {
     23 
     24 class PrintedDocument;
     25 class PrintedPage;
     26 class PrintJob;
     27 class PrintJobWorkerOwner;
     28 
     29 // Worker thread code. All this code, except for the constructor, is executed in
     30 // the worker thread. 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(bool ask_user_for_settings,
     49                    gfx::NativeView parent_view,
     50                    int document_page_count,
     51                    bool has_selection,
     52                    MarginType margin_type);
     53 
     54   // Set the new print settings. This function takes ownership of
     55   // |new_settings|.
     56   void SetSettings(const base::DictionaryValue* const new_settings);
     57 
     58   // Starts the printing loop. Every pages are printed as soon as the data is
     59   // available. Makes sure the new_document is the right one.
     60   void StartPrinting(PrintedDocument* new_document);
     61 
     62   // Updates the printed document.
     63   void OnDocumentChanged(PrintedDocument* new_document);
     64 
     65   // Dequeues waiting pages. Called when PrintJob receives a
     66   // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
     67   // the next page can be printed.
     68   void OnNewPage();
     69 
     70   // This is the only function that can be called in a thread.
     71   void Cancel();
     72 
     73  protected:
     74   // Retrieves the context for testing only.
     75   PrintingContext* printing_context() { return printing_context_.get(); }
     76 
     77  private:
     78   // The shared NotificationService service can only be accessed from the UI
     79   // thread, so this class encloses the necessary information to send the
     80   // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
     81   // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
     82   // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
     83   class NotificationTask;
     84 
     85   // Renders a page in the printer.
     86   void SpoolPage(PrintedPage* page);
     87 
     88   // Closes the job since spooling is done.
     89   void OnDocumentDone();
     90 
     91   // Discards the current document, the current page and cancels the printing
     92   // context.
     93   void OnFailure();
     94 
     95   // Asks the user for print settings. Must be called on the UI thread.
     96   // Required on Mac and Linux. Windows can display UI from non-main threads,
     97   // but sticks with this for consistency.
     98   void GetSettingsWithUI(gfx::NativeView parent_view,
     99                          int document_page_count,
    100                          bool has_selection);
    101 
    102   // The callback used by PrintingContext::GetSettingsWithUI() to notify this
    103   // object that the print settings are set.  This is needed in order to bounce
    104   // back into the IO thread for GetSettingsDone().
    105   void GetSettingsWithUIDone(PrintingContext::Result result);
    106 
    107   // Called on the UI thread to update the print settings. This function takes
    108   // the ownership of |new_settings|.
    109   void UpdatePrintSettings(const base::DictionaryValue* const new_settings);
    110 
    111   // Reports settings back to owner_.
    112   void GetSettingsDone(PrintingContext::Result result);
    113 
    114   // Use the default settings. When using GTK+ or Mac, this can still end up
    115   // displaying a dialog. So this needs to happen from the UI thread on these
    116   // systems.
    117   void UseDefaultSettings();
    118 
    119   // Information about the printer setting.
    120   scoped_ptr<PrintingContext> printing_context_;
    121 
    122   // The printed document. Only has read-only access.
    123   scoped_refptr<PrintedDocument> document_;
    124 
    125   // The print destination, may be NULL.
    126   scoped_refptr<PrintDestinationInterface> destination_;
    127 
    128   // The print job owning this worker thread. It is guaranteed to outlive this
    129   // object.
    130   PrintJobWorkerOwner* owner_;
    131 
    132   // Current page number to print.
    133   PageNumber page_number_;
    134 
    135   // Used to generate a WeakPtr for callbacks.
    136   base::WeakPtrFactory<PrintJobWorker> weak_factory_;
    137 
    138   DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
    139 };
    140 
    141 }  // namespace printing
    142 
    143 #endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H__
    144