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_job_constants.h"
15 #include "printing/printing_context.h"
18 class DictionaryValue
;
24 class PrintJobWorkerOwner
;
25 class PrintedDocument
;
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
{
35 PrintJobWorker(int render_process_id
,
37 PrintJobWorkerOwner
* owner
);
38 virtual ~PrintJobWorker();
40 void SetNewOwner(PrintJobWorkerOwner
* new_owner
);
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.
45 bool ask_user_for_settings
,
46 int document_page_count
,
48 MarginType margin_type
);
50 // Set the new print settings.
51 void SetSettings(scoped_ptr
<base::DictionaryValue
> new_settings
);
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
);
57 // Updates the printed document.
58 void OnDocumentChanged(PrintedDocument
* new_document
);
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.
65 // This is the only function that can be called in a thread.
68 // Returns true if the thread has been started, and not yet stopped.
69 bool IsRunning() const;
71 // Posts the given task to be run.
72 bool PostTask(const tracked_objects::Location
& from_here
,
73 const base::Closure
& task
);
75 // Signals the thread to exit in the near future.
78 // Signals the thread to exit and returns once the thread has exited.
85 // Retrieves the context for testing only.
86 PrintingContext
* printing_context() { return printing_context_
.get(); }
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
;
96 // Renders a page in the printer.
97 void SpoolPage(PrintedPage
* page
);
99 // Closes the job since spooling is done.
100 void OnDocumentDone();
102 // Discards the current document, the current page and cancels the printing
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
,
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
);
118 // Called on the UI thread to update the print settings.
119 void UpdatePrintSettings(scoped_ptr
<base::DictionaryValue
> new_settings
);
121 // Reports settings back to owner_.
122 void GetSettingsDone(PrintingContext::Result result
);
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
127 void UseDefaultSettings();
129 // Printing context delegate.
130 scoped_ptr
<PrintingContext::Delegate
> printing_context_delegate_
;
132 // Information about the printer setting.
133 scoped_ptr
<PrintingContext
> printing_context_
;
135 // The printed document. Only has read-only access.
136 scoped_refptr
<PrintedDocument
> document_
;
138 // The print job owning this worker thread. It is guaranteed to outlive this
140 PrintJobWorkerOwner
* owner_
;
142 // Current page number to print.
143 PageNumber page_number_
;
145 // Thread to run worker tasks.
146 base::Thread thread_
;
148 // Tread-safe pointer to task runner of the |thread_|.
149 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
151 // Used to generate a WeakPtr for callbacks.
152 base::WeakPtrFactory
<PrintJobWorker
> weak_factory_
;
154 DISALLOW_COPY_AND_ASSIGN(PrintJobWorker
);
157 } // namespace printing
159 #endif // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_