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 "content/public/browser/browser_thread.h"
     13 #include "printing/page_number.h"
     14 #include "printing/print_job_constants.h"
     15 #include "printing/printing_context.h"
     16 
     17 namespace base {
     18 class DictionaryValue;
     19 }
     20 
     21 namespace printing {
     22 
     23 class PrintJob;
     24 class PrintJobWorkerOwner;
     25 class PrintedDocument;
     26 class PrintedPage;
     27 
     28 // Worker thread code. It manages the PrintingContext, which can be blocking
     29 // and/or run a message loop. This is the object that generates most
     30 // NOTIFY_PRINT_JOB_EVENT notifications, but they are generated through a
     31 // NotificationTask task to be executed from the right thread, the UI thread.
     32 // PrintJob always outlives its worker instance.
     33 class PrintJobWorker {
     34  public:
     35   PrintJobWorker(int render_process_id,
     36                  int render_view_id,
     37                  PrintJobWorkerOwner* owner);
     38   virtual ~PrintJobWorker();
     39 
     40   void SetNewOwner(PrintJobWorkerOwner* new_owner);
     41 
     42   // Initializes the print settings. If |ask_user_for_settings| is true, a
     43   // Print... dialog box will be shown to ask the user his preference.
     44   void GetSettings(
     45       bool ask_user_for_settings,
     46       int document_page_count,
     47       bool has_selection,
     48       MarginType margin_type);
     49 
     50   // Set the new print settings.
     51   void SetSettings(scoped_ptr<base::DictionaryValue> new_settings);
     52 
     53   // Starts the printing loop. Every pages are printed as soon as the data is
     54   // available. Makes sure the new_document is the right one.
     55   void StartPrinting(PrintedDocument* new_document);
     56 
     57   // Updates the printed document.
     58   void OnDocumentChanged(PrintedDocument* new_document);
     59 
     60   // Dequeues waiting pages. Called when PrintJob receives a
     61   // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
     62   // the next page can be printed.
     63   void OnNewPage();
     64 
     65   // This is the only function that can be called in a thread.
     66   void Cancel();
     67 
     68   // Returns true if the thread has been started, and not yet stopped.
     69   bool IsRunning() const;
     70 
     71   // Posts the given task to be run.
     72   bool PostTask(const tracked_objects::Location& from_here,
     73                 const base::Closure& task);
     74 
     75   // Signals the thread to exit in the near future.
     76   void StopSoon();
     77 
     78   // Signals the thread to exit and returns once the thread has exited.
     79   void Stop();
     80 
     81   // Starts the thread.
     82   bool Start();
     83 
     84  protected:
     85   // Retrieves the context for testing only.
     86   PrintingContext* printing_context() { return printing_context_.get(); }
     87 
     88  private:
     89   // The shared NotificationService service can only be accessed from the UI
     90   // thread, so this class encloses the necessary information to send the
     91   // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
     92   // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
     93   // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
     94   class NotificationTask;
     95 
     96   // Renders a page in the printer.
     97   void SpoolPage(PrintedPage* page);
     98 
     99   // Closes the job since spooling is done.
    100   void OnDocumentDone();
    101 
    102   // Discards the current document, the current page and cancels the printing
    103   // context.
    104   void OnFailure();
    105 
    106   // Asks the user for print settings. Must be called on the UI thread.
    107   // Required on Mac and Linux. Windows can display UI from non-main threads,
    108   // but sticks with this for consistency.
    109   void GetSettingsWithUI(
    110       int document_page_count,
    111       bool has_selection);
    112 
    113   // The callback used by PrintingContext::GetSettingsWithUI() to notify this
    114   // object that the print settings are set.  This is needed in order to bounce
    115   // back into the IO thread for GetSettingsDone().
    116   void GetSettingsWithUIDone(PrintingContext::Result result);
    117 
    118   // Called on the UI thread to update the print settings.
    119   void UpdatePrintSettings(scoped_ptr<base::DictionaryValue> new_settings);
    120 
    121   // Reports settings back to owner_.
    122   void GetSettingsDone(PrintingContext::Result result);
    123 
    124   // Use the default settings. When using GTK+ or Mac, this can still end up
    125   // displaying a dialog. So this needs to happen from the UI thread on these
    126   // systems.
    127   void UseDefaultSettings();
    128 
    129   // Printing context delegate.
    130   scoped_ptr<PrintingContext::Delegate> printing_context_delegate_;
    131 
    132   // Information about the printer setting.
    133   scoped_ptr<PrintingContext> printing_context_;
    134 
    135   // The printed document. Only has read-only access.
    136   scoped_refptr<PrintedDocument> document_;
    137 
    138   // The print job owning this worker thread. It is guaranteed to outlive this
    139   // object.
    140   PrintJobWorkerOwner* owner_;
    141 
    142   // Current page number to print.
    143   PageNumber page_number_;
    144 
    145   // Thread to run worker tasks.
    146   base::Thread thread_;
    147 
    148   // Tread-safe pointer to task runner of the |thread_|.
    149   scoped_refptr<base::SequencedTaskRunner> task_runner_;
    150 
    151   // Used to generate a WeakPtr for callbacks.
    152   base::WeakPtrFactory<PrintJobWorker> weak_factory_;
    153 
    154   DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
    155 };
    156 
    157 }  // namespace printing
    158 
    159 #endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
    160