blob: f79ca862e688728bb6568338460037075c2accb1 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
5#ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H__
6#define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H__
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit09911bf2008-07-26 23:55:298
9#include "base/task.h"
10#include "base/thread.h"
[email protected]5c7293a2010-03-17 06:40:5711#include "gfx/native_widget_types.h"
[email protected]8ff1d422009-07-07 21:31:3912#include "printing/page_number.h"
13#include "printing/printing_context.h"
initial.commit09911bf2008-07-26 23:55:2914
15namespace printing {
16
17class PrintedDocument;
18class PrintedPage;
19class PrintJob;
20class 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]ab820df2008-08-26 05:55:1028class PrintJobWorker : public base::Thread {
initial.commit09911bf2008-07-26 23:55:2929 public:
[email protected]718fb432009-02-05 19:20:3630 explicit PrintJobWorker(PrintJobWorkerOwner* owner);
initial.commit09911bf2008-07-26 23:55:2931 ~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]fc7904622010-05-12 19:26:4038 gfx::NativeView parent_view,
[email protected]c8ad40c2009-06-08 17:05:2139 int document_page_count,
[email protected]38bba4f2010-03-12 05:29:0740 bool has_selection,
41 bool use_overlays);
initial.commit09911bf2008-07-26 23:55:2942
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]718fb432009-02-05 19:20:3661 protected:
62 // Retrieves the context for testing only.
63 PrintingContext& printing_context() { return printing_context_; }
64
initial.commit09911bf2008-07-26 23:55:2965 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]b23aee0f2009-10-13 22:54:1284#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]fc7904622010-05-12 19:26:4087 void GetSettingsWithUI(gfx::NativeView parent_view,
[email protected]b23aee0f2009-10-13 22:54:1288 int document_page_count,
89 bool has_selection);
[email protected]b7191422010-09-21 19:18:0590
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]b23aee0f2009-10-13 22:54:1295#endif
96
97 // Reports settings back to owner_.
98 void GetSettingsDone(PrintingContext::Result result);
99
initial.commit09911bf2008-07-26 23:55:29100 // 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]5930cb62009-12-08 02:04:22113 DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
initial.commit09911bf2008-07-26 23:55:29114};
115
116} // namespace printing
117
118template <>
119struct RunnableMethodTraits<printing::PrintJobWorker> {
initial.commit09911bf2008-07-26 23:55:29120 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__