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