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 "printing/page_number.h"
13 #include "printing/print_destination_interface.h"
14 #include "printing/printing_context.h"
15 #include "printing/print_job_constants.h"
17 class PrintingUIWebContentsObserver;
20 class DictionaryValue;
25 class PrintedDocument;
28 class PrintJobWorkerOwner;
30 // Worker thread code. It manages the PrintingContext, which can be blocking
31 // and/or run a message loop. This is the object that generates most
32 // NOTIFY_PRINT_JOB_EVENT notifications, but they are generated through a
33 // NotificationTask task to be executed from the right thread, the UI thread.
34 // PrintJob always outlives its worker instance.
35 class PrintJobWorker : public base::Thread {
37 explicit PrintJobWorker(PrintJobWorkerOwner* owner);
38 virtual ~PrintJobWorker();
40 void SetNewOwner(PrintJobWorkerOwner* new_owner);
42 // Set a destination for print.
43 // This supercedes the document's rendering destination.
44 void SetPrintDestination(PrintDestinationInterface* destination);
46 // Initializes the print settings. If |ask_user_for_settings| is true, a
47 // Print... dialog box will be shown to ask the user his preference.
49 bool ask_user_for_settings,
50 scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
51 int document_page_count,
53 MarginType margin_type);
55 // Set the new print settings. This function takes ownership of
57 void SetSettings(const base::DictionaryValue* const new_settings);
59 // Starts the printing loop. Every pages are printed as soon as the data is
60 // available. Makes sure the new_document is the right one.
61 void StartPrinting(PrintedDocument* new_document);
63 // Updates the printed document.
64 void OnDocumentChanged(PrintedDocument* new_document);
66 // Dequeues waiting pages. Called when PrintJob receives a
67 // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
68 // the next page can be printed.
71 // This is the only function that can be called in a thread.
75 // Retrieves the context for testing only.
76 PrintingContext* printing_context() { return printing_context_.get(); }
79 // The shared NotificationService service can only be accessed from the UI
80 // thread, so this class encloses the necessary information to send the
81 // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
82 // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
83 // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
84 class NotificationTask;
86 // Renders a page in the printer.
87 void SpoolPage(PrintedPage* page);
89 // Closes the job since spooling is done.
90 void OnDocumentDone();
92 // Discards the current document, the current page and cancels the printing
96 // Asks the user for print settings. Must be called on the UI thread.
97 // Required on Mac and Linux. Windows can display UI from non-main threads,
98 // but sticks with this for consistency.
99 void GetSettingsWithUI(
100 scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer,
101 int document_page_count,
104 // The callback used by PrintingContext::GetSettingsWithUI() to notify this
105 // object that the print settings are set. This is needed in order to bounce
106 // back into the IO thread for GetSettingsDone().
107 void GetSettingsWithUIDone(PrintingContext::Result result);
109 // Called on the UI thread to update the print settings. This function takes
110 // the ownership of |new_settings|.
111 void UpdatePrintSettings(const base::DictionaryValue* const new_settings);
113 // Reports settings back to owner_.
114 void GetSettingsDone(PrintingContext::Result result);
116 // Use the default settings. When using GTK+ or Mac, this can still end up
117 // displaying a dialog. So this needs to happen from the UI thread on these
119 void UseDefaultSettings();
121 // Information about the printer setting.
122 scoped_ptr<PrintingContext> printing_context_;
124 // The printed document. Only has read-only access.
125 scoped_refptr<PrintedDocument> document_;
127 // The print destination, may be NULL.
128 scoped_refptr<PrintDestinationInterface> destination_;
130 // The print job owning this worker thread. It is guaranteed to outlive this
132 PrintJobWorkerOwner* owner_;
134 // Current page number to print.
135 PageNumber page_number_;
137 // Used to generate a WeakPtr for callbacks.
138 base::WeakPtrFactory<PrintJobWorker> weak_factory_;
140 DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
143 } // namespace printing
145 #endif // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H_