• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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.
4 
5 #ifndef CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H__
6 #define CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H__
7 #pragma once
8 
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/task.h"
12 #include "base/threading/thread.h"
13 #include "printing/page_number.h"
14 #include "printing/printing_context.h"
15 #include "ui/gfx/native_widget_types.h"
16 
17 class DictionaryValue;
18 
19 namespace printing {
20 
21 class PrintedDocument;
22 class PrintedPage;
23 class PrintJob;
24 class PrintJobWorkerOwner;
25 
26 // Worker thread code. All this code, except for the constructor, is executed in
27 // the worker thread. It manages the PrintingContext, which can be blocking
28 // and/or run a message loop. This is the object that generates most
29 // NOTIFY_PRINT_JOB_EVENT notifications, but they are generated through a
30 // NotificationTask task to be executed from the right thread, the UI thread.
31 // PrintJob always outlives its worker instance.
32 class PrintJobWorker : public base::Thread {
33  public:
34   explicit PrintJobWorker(PrintJobWorkerOwner* owner);
35   ~PrintJobWorker();
36 
37   void SetNewOwner(PrintJobWorkerOwner* new_owner);
38 
39   // Initializes the print settings. If |ask_user_for_settings| is true, a
40   // Print... dialog box will be shown to ask the user his preference.
41   void GetSettings(bool ask_user_for_settings,
42                    gfx::NativeView parent_view,
43                    int document_page_count,
44                    bool has_selection,
45                    bool use_overlays);
46 
47   // Set the new print settings. This function takes ownership of
48   // |new_settings|.
49   void SetSettings(const DictionaryValue* const new_settings);
50 
51   // Starts the printing loop. Every pages are printed as soon as the data is
52   // available. Makes sure the new_document is the right one.
53   void StartPrinting(PrintedDocument* new_document);
54 
55   // Updates the printed document.
56   void OnDocumentChanged(PrintedDocument* new_document);
57 
58   // Unqueues waiting pages. Called when PrintJob receives a
59   // NOTIFY_PRINTED_DOCUMENT_UPDATED notification. It's time to look again if
60   // the next page can be printed.
61   void OnNewPage();
62 
63   // This is the only function that can be called in a thread.
64   void Cancel();
65 
66  protected:
67   // Retrieves the context for testing only.
printing_context()68   PrintingContext* printing_context() { return printing_context_.get(); }
69 
70  private:
71   // The shared NotificationService service can only be accessed from the UI
72   // thread, so this class encloses the necessary information to send the
73   // notification from the right thread. Most NOTIFY_PRINT_JOB_EVENT
74   // notifications are sent this way, except USER_INIT_DONE, USER_INIT_CANCELED
75   // and DEFAULT_INIT_DONE. These three are sent through PrintJob::InitDone().
76   class NotificationTask;
77   friend struct RunnableMethodTraits<PrintJobWorker>;
78 
79   // Renders a page in the printer.
80   void SpoolPage(PrintedPage& page);
81 
82   // Closes the job since spooling is done.
83   void OnDocumentDone();
84 
85   // Discards the current document, the current page and cancels the printing
86   // context.
87   void OnFailure();
88 
89   // Asks the user for print settings. Must be called on the UI thread.
90   // Required on Mac and Linux. Windows can display UI from non-main threads,
91   // but sticks with this for consistency.
92   void GetSettingsWithUI(gfx::NativeView parent_view,
93                          int document_page_count,
94                          bool has_selection);
95 
96   // The callback used by PrintingContext::GetSettingsWithUI() to notify this
97   // object that the print settings are set.  This is needed in order to bounce
98   // back into the IO thread for GetSettingsDone().
99   void GetSettingsWithUIDone(PrintingContext::Result result);
100 
101   // Called on the UI thread to update the print settings. This function takes
102   // the ownership of |new_settings|.
103   void UpdatePrintSettings(const DictionaryValue* const new_settings);
104 
105   // Reports settings back to owner_.
106   void GetSettingsDone(PrintingContext::Result result);
107 
108   // Use the default settings. When using GTK+ or Mac, this can still end up
109   // displaying a dialog. So this needs to happen from the UI thread on these
110   // systems.
111   void UseDefaultSettings();
112 
113   // Information about the printer setting.
114   scoped_ptr<PrintingContext> printing_context_;
115 
116   // The printed document. Only has read-only access.
117   scoped_refptr<PrintedDocument> document_;
118 
119   // The print job owning this worker thread. It is guaranteed to outlive this
120   // object.
121   PrintJobWorkerOwner* owner_;
122 
123   // Current page number to print.
124   PageNumber page_number_;
125 
126   DISALLOW_COPY_AND_ASSIGN(PrintJobWorker);
127 };
128 
129 }  // namespace printing
130 
131 template <>
132 struct RunnableMethodTraits<printing::PrintJobWorker> {
133   void RetainCallee(printing::PrintJobWorker* obj);
134   void ReleaseCallee(printing::PrintJobWorker* obj);
135  private:
136   scoped_refptr<printing::PrintJobWorkerOwner> owner_;
137 };
138 
139 #endif  // CHROME_BROWSER_PRINTING_PRINT_JOB_WORKER_H__
140