Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / printing / print_job_worker.h
blob97d8d81e72d0bd4a2cd7d4421e8c30727cc46eff
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 "chrome/browser/printing/printer_query.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "printing/page_number.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 // Initializes the print settings. If |ask_user_for_settings| is true, a
44 // Print... dialog box will be shown to ask the user his preference.
45 // |is_scripted| should be true for calls coming straight from window.print().
46 void GetSettings(
47 bool ask_user_for_settings,
48 int document_page_count,
49 bool has_selection,
50 MarginType margin_type,
51 bool is_scripted);
53 // Set the new print settings.
54 void SetSettings(scoped_ptr<base::DictionaryValue> new_settings);
56 // Starts the printing loop. Every pages are printed as soon as the data is
57 // available. Makes sure the new_document is the right one.
58 void StartPrinting(PrintedDocument* new_document);
60 // Updates the printed document.
61 void OnDocumentChanged(PrintedDocument* new_document);
63 // Dequeues waiting pages. Called when PrintJob receives a
64 // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
65 // the next page can be printed.
66 void OnNewPage();
68 // This is the only function that can be called in a thread.
69 void Cancel();
71 // Returns true if the thread has been started, and not yet stopped.
72 bool IsRunning() const;
74 // Posts the given task to be run.
75 bool PostTask(const tracked_objects::Location& from_here,
76 const base::Closure& task);
78 // Signals the thread to exit in the near future.
79 void StopSoon();
81 // Signals the thread to exit and returns once the thread has exited.
82 void Stop();
84 // Starts the thread.
85 bool Start();
87 protected:
88 // Retrieves the context for testing only.
89 PrintingContext* printing_context() { return printing_context_.get(); }
91 private:
92 // The shared NotificationService service can only be accessed from the UI
93 // thread, so this class encloses the necessary information to send the
94 // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
95 // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
96 // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
97 class NotificationTask;
99 // Renders a page in the printer.
100 void SpoolPage(PrintedPage* page);
102 // Closes the job since spooling is done.
103 void OnDocumentDone();
105 // Discards the current document, the current page and cancels the printing
106 // context.
107 void OnFailure();
109 // Asks the user for print settings. Must be called on the UI thread.
110 // Required on Mac and Linux. Windows can display UI from non-main threads,
111 // but sticks with this for consistency.
112 void GetSettingsWithUI(
113 int document_page_count,
114 bool has_selection,
115 bool is_scripted);
117 // The callback used by PrintingContext::GetSettingsWithUI() to notify this
118 // object that the print settings are set. This is needed in order to bounce
119 // back into the IO thread for GetSettingsDone().
120 void GetSettingsWithUIDone(PrintingContext::Result result);
122 // Called on the UI thread to update the print settings.
123 void UpdatePrintSettings(scoped_ptr<base::DictionaryValue> new_settings);
125 // Reports settings back to owner_.
126 void GetSettingsDone(PrintingContext::Result result);
128 // Use the default settings. When using GTK+ or Mac, this can still end up
129 // displaying a dialog. So this needs to happen from the UI thread on these
130 // systems.
131 void UseDefaultSettings();
133 // Printing context delegate.
134 scoped_ptr<PrintingContext::Delegate> printing_context_delegate_;
136 // Information about the printer setting.
137 scoped_ptr<PrintingContext> printing_context_;
139 // The printed document. Only has read-only access.
140 scoped_refptr<PrintedDocument> document_;
142 // The print job owning this worker thread. It is guaranteed to outlive this
143 // object.
144 PrintJobWorkerOwner* owner_;
146 // Current page number to print.
147 PageNumber page_number_;
149 // Thread to run worker tasks.
150 base::Thread thread_;
152 // Tread-safe pointer to task runner of the |thread_|.
153 scoped_refptr<base::SequencedTaskRunner> task_runner_;
155 // Used to generate a WeakPtr for callbacks.
156 base::WeakPtrFactory<PrintJobWorker> weak_factory_;
158 DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
161 } // namespace printing
163 #endif // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_