Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / printing / print_job_worker.h
blob02aac9aa678706a6a549edb5b057fde47af67324
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.
5 #ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
6 #define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_
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_destination_interface.h"
15 #include "printing/print_job_constants.h"
16 #include "printing/printing_context.h"
18 namespace base {
19 class DictionaryValue;
22 namespace printing {
24 class PrintJob;
25 class PrintJobWorkerOwner;
26 class PrintedDocument;
27 class PrintedPage;
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.
34 class PrintJobWorker {
35 public:
36 PrintJobWorker(int render_process_id,
37 int render_view_id,
38 PrintJobWorkerOwner* owner);
39 virtual ~PrintJobWorker();
41 void SetNewOwner(PrintJobWorkerOwner* new_owner);
43 // Set a destination for print.
44 // This supersedes the document's rendering destination.
45 void SetPrintDestination(PrintDestinationInterface* destination);
47 // Initializes the print settings. If |ask_user_for_settings| is true, a
48 // Print... dialog box will be shown to ask the user his preference.
49 void GetSettings(
50 bool ask_user_for_settings,
51 int document_page_count,
52 bool has_selection,
53 MarginType margin_type);
55 // Set the new print settings.
56 void SetSettings(scoped_ptr<base::DictionaryValue> new_settings);
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);
62 // Updates the printed document.
63 void OnDocumentChanged(PrintedDocument* new_document);
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();
70 // This is the only function that can be called in a thread.
71 void Cancel();
73 // Returns true if the thread has been started, and not yet stopped.
74 bool IsRunning() const;
76 // Posts the given task to be run.
77 bool PostTask(const tracked_objects::Location& from_here,
78 const base::Closure& task);
80 // Signals the thread to exit in the near future.
81 void StopSoon();
83 // Signals the thread to exit and returns once the thread has exited.
84 void Stop();
86 // Starts the thread.
87 bool Start();
89 protected:
90 // Retrieves the context for testing only.
91 PrintingContext* printing_context() { return printing_context_.get(); }
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;
101 // Renders a page in the printer.
102 void SpoolPage(PrintedPage* page);
104 // Closes the job since spooling is done.
105 void OnDocumentDone();
107 // Discards the current document, the current page and cancels the printing
108 // context.
109 void OnFailure();
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 int document_page_count,
116 bool has_selection);
118 // The callback used by PrintingContext::GetSettingsWithUI() to notify this
119 // object that the print settings are set. This is needed in order to bounce
120 // back into the IO thread for GetSettingsDone().
121 void GetSettingsWithUIDone(PrintingContext::Result result);
123 // Called on the UI thread to update the print settings.
124 void UpdatePrintSettings(scoped_ptr<base::DictionaryValue> new_settings);
126 // Reports settings back to owner_.
127 void GetSettingsDone(PrintingContext::Result result);
129 // Use the default settings. When using GTK+ or Mac, this can still end up
130 // displaying a dialog. So this needs to happen from the UI thread on these
131 // systems.
132 void UseDefaultSettings();
134 // Printing context delegate.
135 scoped_ptr<PrintingContext::Delegate> printing_context_delegate_;
137 // Information about the printer setting.
138 scoped_ptr<PrintingContext> printing_context_;
140 // The printed document. Only has read-only access.
141 scoped_refptr<PrintedDocument> document_;
143 // The print destination, may be NULL.
144 scoped_refptr<PrintDestinationInterface> destination_;
146 // The print job owning this worker thread. It is guaranteed to outlive this
147 // object.
148 PrintJobWorkerOwner* owner_;
150 // Current page number to print.
151 PageNumber page_number_;
153 // Thread to run worker tasks.
154 base::Thread thread_;
156 // Tread-safe pointer to task runner of the |thread_|.
157 scoped_refptr<base::SequencedTaskRunner> task_runner_;
159 // Used to generate a WeakPtr for callbacks.
160 base::WeakPtrFactory<PrintJobWorker> weak_factory_;
162 DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
165 } // namespace printing
167 #endif // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_