license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H__ |
| 6 | #define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H__ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
| 9 | #include "base/task.h" |
| 10 | #include "base/thread.h" |
[email protected] | 5c7293a | 2010-03-17 06:40:57 | [diff] [blame] | 11 | #include "gfx/native_widget_types.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 12 | #include "printing/page_number.h" |
| 13 | #include "printing/printing_context.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | |
| 15 | namespace printing { |
| 16 | |
| 17 | class PrintedDocument; |
| 18 | class PrintedPage; |
| 19 | class PrintJob; |
| 20 | class PrintJobWorkerOwner; |
| 21 | |
| 22 | // Worker thread code. All this code, except for the constructor, is executed in |
| 23 | // the worker thread. It manages the PrintingContext, which can be blocking |
| 24 | // and/or run a message loop. This is the object that generates most |
| 25 | // NOTIFY_PRINT_JOB_EVENT notifications, but they are generated through a |
| 26 | // NotificationTask task to be executed from the right thread, the UI thread. |
| 27 | // PrintJob always outlives its worker instance. |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 28 | class PrintJobWorker : public base::Thread { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | public: |
[email protected] | 718fb43 | 2009-02-05 19:20:36 | [diff] [blame] | 30 | explicit PrintJobWorker(PrintJobWorkerOwner* owner); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | ~PrintJobWorker(); |
| 32 | |
| 33 | void SetNewOwner(PrintJobWorkerOwner* new_owner); |
| 34 | |
| 35 | // Initializes the print settings. If |ask_user_for_settings| is true, a |
| 36 | // Print... dialog box will be shown to ask the user his preference. |
| 37 | void GetSettings(bool ask_user_for_settings, |
[email protected] | fc790462 | 2010-05-12 19:26:40 | [diff] [blame] | 38 | gfx::NativeView parent_view, |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 39 | int document_page_count, |
[email protected] | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 40 | bool has_selection, |
| 41 | bool use_overlays); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
| 43 | // Starts the printing loop. Every pages are printed as soon as the data is |
| 44 | // available. Makes sure the new_document is the right one. |
| 45 | void StartPrinting(PrintedDocument* new_document); |
| 46 | |
| 47 | // Updates the printed document. |
| 48 | void OnDocumentChanged(PrintedDocument* new_document); |
| 49 | |
| 50 | // Unqueues waiting pages. Called when PrintJob receives a |
| 51 | // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if |
| 52 | // the next page can be printed. |
| 53 | void OnNewPage(); |
| 54 | |
| 55 | // This is the only function that can be called in a thread. |
| 56 | void Cancel(); |
| 57 | |
| 58 | // Cancels the Print... dialog box if shown, noop otherwise. |
| 59 | void DismissDialog(); |
| 60 | |
[email protected] | 718fb43 | 2009-02-05 19:20:36 | [diff] [blame] | 61 | protected: |
| 62 | // Retrieves the context for testing only. |
| 63 | PrintingContext& printing_context() { return printing_context_; } |
| 64 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | private: |
| 66 | // The shared NotificationService service can only be accessed from the UI |
| 67 | // thread, so this class encloses the necessary information to send the |
| 68 | // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT |
| 69 | // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED |
| 70 | // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone(). |
| 71 | class NotificationTask; |
| 72 | friend struct RunnableMethodTraits<PrintJobWorker>; |
| 73 | |
| 74 | // Renders a page in the printer. |
| 75 | void SpoolPage(PrintedPage& page); |
| 76 | |
| 77 | // Closes the job since spooling is done. |
| 78 | void OnDocumentDone(); |
| 79 | |
| 80 | // Discards the current document, the current page and cancels the printing |
| 81 | // context. |
| 82 | void OnFailure(); |
| 83 | |
[email protected] | b23aee0f | 2009-10-13 22:54:12 | [diff] [blame] | 84 | #if defined(OS_MACOSX) |
| 85 | // Asks the user for print settings. Must be called on the UI thread. |
| 86 | // Mac-only since Windows can display UI from non-main threads. |
[email protected] | fc790462 | 2010-05-12 19:26:40 | [diff] [blame] | 87 | void GetSettingsWithUI(gfx::NativeView parent_view, |
[email protected] | b23aee0f | 2009-10-13 22:54:12 | [diff] [blame] | 88 | int document_page_count, |
| 89 | bool has_selection); |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame^] | 90 | |
| 91 | // The callback used by PrintingContext::GetSettingsWithUI() to notify this |
| 92 | // object that the print settings are set. This is needed in order to bounce |
| 93 | // back into the IO thread for GetSettingsDone(). |
| 94 | void GetSettingsWithUIDone(PrintingContext::Result result); |
[email protected] | b23aee0f | 2009-10-13 22:54:12 | [diff] [blame] | 95 | #endif |
| 96 | |
| 97 | // Reports settings back to owner_. |
| 98 | void GetSettingsDone(PrintingContext::Result result); |
| 99 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | // Information about the printer setting. |
| 101 | PrintingContext printing_context_; |
| 102 | |
| 103 | // The printed document. Only has read-only access. |
| 104 | scoped_refptr<PrintedDocument> document_; |
| 105 | |
| 106 | // The print job owning this worker thread. It is guaranteed to outlive this |
| 107 | // object. |
| 108 | PrintJobWorkerOwner* owner_; |
| 109 | |
| 110 | // Current page number to print. |
| 111 | PageNumber page_number_; |
| 112 | |
[email protected] | 5930cb6 | 2009-12-08 02:04:22 | [diff] [blame] | 113 | DISALLOW_COPY_AND_ASSIGN(PrintJobWorker); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // namespace printing |
| 117 | |
| 118 | template <> |
| 119 | struct RunnableMethodTraits<printing::PrintJobWorker> { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | void RetainCallee(printing::PrintJobWorker* obj); |
| 121 | void ReleaseCallee(printing::PrintJobWorker* obj); |
| 122 | private: |
| 123 | scoped_refptr<printing::PrintJobWorkerOwner> owner_; |
| 124 | }; |
| 125 | |
| 126 | #endif // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H__ |