OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/printing/print_job_worker.h" | 5 #include "chrome/browser/printing/print_job_worker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/printing/print_job.h" | 15 #include "chrome/browser/printing/print_job.h" |
16 #include "chrome/browser/printing/printing_ui_web_contents_observer.h" | 16 #include "chrome/browser/printing/printing_ui_web_contents_observer.h" |
17 #include "chrome/grit/generated_resources.h" | 17 #include "chrome/grit/generated_resources.h" |
18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
20 #include "content/public/browser/render_view_host.h" | |
21 #include "content/public/browser/web_contents.h" | |
22 #include "printing/print_job_constants.h" | 20 #include "printing/print_job_constants.h" |
23 #include "printing/printed_document.h" | 21 #include "printing/printed_document.h" |
24 #include "printing/printed_page.h" | 22 #include "printing/printed_page.h" |
25 #include "printing/printing_utils.h" | 23 #include "printing/printing_utils.h" |
26 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
27 | 25 |
28 using content::BrowserThread; | 26 using content::BrowserThread; |
29 | 27 |
30 namespace printing { | 28 namespace printing { |
31 | 29 |
32 namespace { | 30 namespace { |
33 | 31 |
34 // Helper function to ensure |owner| is valid until at least |callback| returns. | 32 // Helper function to ensure |owner| is valid until at least |callback| returns. |
35 void HoldRefCallback(const scoped_refptr<printing::PrintJobWorkerOwner>& owner, | 33 void HoldRefCallback(const scoped_refptr<printing::PrintJobWorkerOwner>& owner, |
36 const base::Closure& callback) { | 34 const base::Closure& callback) { |
37 callback.Run(); | 35 callback.Run(); |
38 } | 36 } |
39 | 37 |
40 class PrintingContextDelegate : public PrintingContext::Delegate { | 38 } // namespace |
41 public: | |
42 PrintingContextDelegate(int render_process_id, int render_view_id); | |
43 virtual ~PrintingContextDelegate(); | |
44 | |
45 virtual gfx::NativeView GetParentView() OVERRIDE; | |
46 virtual std::string GetAppLocale() OVERRIDE; | |
47 | |
48 private: | |
49 void InitOnUiThread(int render_process_id, int render_view_id); | |
50 | |
51 scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer_; | |
52 base::WeakPtrFactory<PrintingContextDelegate> weak_ptr_factory_; | |
53 }; | |
54 | |
55 PrintingContextDelegate::PrintingContextDelegate(int render_process_id, | |
56 int render_view_id) | |
57 : weak_ptr_factory_(this) { | |
58 InitOnUiThread(render_process_id, render_view_id); | |
59 } | |
60 | |
61 PrintingContextDelegate::~PrintingContextDelegate() { | |
62 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
63 } | |
64 | |
65 gfx::NativeView PrintingContextDelegate::GetParentView() { | |
66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
67 if (!web_contents_observer_) | |
68 return NULL; | |
69 return web_contents_observer_->GetParentView(); | |
70 } | |
71 | |
72 std::string PrintingContextDelegate::GetAppLocale() { | |
73 return g_browser_process->GetApplicationLocale(); | |
74 } | |
75 | |
76 void PrintingContextDelegate::InitOnUiThread(int render_process_id, | |
77 int render_view_id) { | |
78 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
79 // All data initialized here should be accessed on UI thread. Because object | |
80 // is being constructed now, anything that is going to access | |
81 // PrintingContextDelegate on UI thread would be scheduled after the tasks | |
82 // below. | |
83 BrowserThread::PostTask(BrowserThread::UI, | |
84 FROM_HERE, | |
85 base::Bind(&PrintingContextDelegate::InitOnUiThread, | |
86 weak_ptr_factory_.GetWeakPtr(), | |
87 render_process_id, | |
88 render_view_id)); | |
89 return; | |
90 } | |
91 content::RenderViewHost* view = | |
92 content::RenderViewHost::FromID(render_process_id, render_view_id); | |
93 if (!view) | |
94 return; | |
95 content::WebContents* wc = content::WebContents::FromRenderViewHost(view); | |
96 if (!wc) | |
97 return; | |
98 web_contents_observer_.reset(new PrintingUIWebContentsObserver(wc)); | |
99 } | |
100 | 39 |
101 void NotificationCallback(PrintJobWorkerOwner* print_job, | 40 void NotificationCallback(PrintJobWorkerOwner* print_job, |
102 JobEventDetails::Type detail_type, | 41 JobEventDetails::Type detail_type, |
103 PrintedDocument* document, | 42 PrintedDocument* document, |
104 PrintedPage* page) { | 43 PrintedPage* page) { |
105 JobEventDetails* details = new JobEventDetails(detail_type, document, page); | 44 JobEventDetails* details = new JobEventDetails(detail_type, document, page); |
106 content::NotificationService::current()->Notify( | 45 content::NotificationService::current()->Notify( |
107 chrome::NOTIFICATION_PRINT_JOB_EVENT, | 46 chrome::NOTIFICATION_PRINT_JOB_EVENT, |
108 // We know that is is a PrintJob object in this circumstance. | 47 // We know that is is a PrintJob object in this circumstance. |
109 content::Source<PrintJob>(static_cast<PrintJob*>(print_job)), | 48 content::Source<PrintJob>(static_cast<PrintJob*>(print_job)), |
110 content::Details<JobEventDetails>(details)); | 49 content::Details<JobEventDetails>(details)); |
111 } | 50 } |
112 | 51 |
113 } // namespace | 52 PrintJobWorker::PrintJobWorker(PrintJobWorkerOwner* owner) |
114 | |
115 PrintJobWorker::PrintJobWorker(int render_process_id, | |
116 int render_view_id, | |
117 PrintJobWorkerOwner* owner) | |
118 : owner_(owner), thread_("Printing_Worker"), weak_factory_(this) { | 53 : owner_(owner), thread_("Printing_Worker"), weak_factory_(this) { |
119 // The object is created in the IO thread. | 54 // The object is created in the IO thread. |
120 DCHECK(owner_->RunsTasksOnCurrentThread()); | 55 DCHECK(owner_->RunsTasksOnCurrentThread()); |
121 | 56 |
122 printing_context_delegate_.reset( | 57 printing_context_.reset(PrintingContext::Create( |
123 new PrintingContextDelegate(render_process_id, render_view_id)); | 58 g_browser_process->GetApplicationLocale())); |
124 printing_context_ = PrintingContext::Create(printing_context_delegate_.get()); | |
125 } | 59 } |
126 | 60 |
127 PrintJobWorker::~PrintJobWorker() { | 61 PrintJobWorker::~PrintJobWorker() { |
128 // The object is normally deleted in the UI thread, but when the user | 62 // The object is normally deleted in the UI thread, but when the user |
129 // cancels printing or in the case of print preview, the worker is destroyed | 63 // cancels printing or in the case of print preview, the worker is destroyed |
130 // on the I/O thread. | 64 // on the I/O thread. |
131 DCHECK(owner_->RunsTasksOnCurrentThread()); | 65 DCHECK(owner_->RunsTasksOnCurrentThread()); |
132 Stop(); | 66 Stop(); |
133 } | 67 } |
134 | 68 |
135 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { | 69 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { |
136 DCHECK(page_number_ == PageNumber::npos()); | 70 DCHECK(page_number_ == PageNumber::npos()); |
137 owner_ = new_owner; | 71 owner_ = new_owner; |
138 } | 72 } |
139 | 73 |
140 void PrintJobWorker::SetPrintDestination( | 74 void PrintJobWorker::SetPrintDestination( |
141 PrintDestinationInterface* destination) { | 75 PrintDestinationInterface* destination) { |
142 destination_ = destination; | 76 destination_ = destination; |
143 } | 77 } |
144 | 78 |
145 void PrintJobWorker::GetSettings( | 79 void PrintJobWorker::GetSettings( |
146 bool ask_user_for_settings, | 80 bool ask_user_for_settings, |
| 81 scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer, |
147 int document_page_count, | 82 int document_page_count, |
148 bool has_selection, | 83 bool has_selection, |
149 MarginType margin_type) { | 84 MarginType margin_type) { |
150 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 85 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
151 DCHECK_EQ(page_number_, PageNumber::npos()); | 86 DCHECK_EQ(page_number_, PageNumber::npos()); |
152 | 87 |
153 // Recursive task processing is needed for the dialog in case it needs to be | 88 // Recursive task processing is needed for the dialog in case it needs to be |
154 // destroyed by a task. | 89 // destroyed by a task. |
155 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed | 90 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed |
156 // on the thread where the PrintDlgEx is called, and definitely both calls | 91 // on the thread where the PrintDlgEx is called, and definitely both calls |
157 // should happen on the same thread. See http://crbug.com/73466 | 92 // should happen on the same thread. See http://crbug.com/73466 |
158 // MessageLoop::current()->SetNestableTasksAllowed(true); | 93 // MessageLoop::current()->SetNestableTasksAllowed(true); |
159 printing_context_->set_margin_type(margin_type); | 94 printing_context_->set_margin_type(margin_type); |
160 | 95 |
161 // When we delegate to a destination, we don't ask the user for settings. | 96 // When we delegate to a destination, we don't ask the user for settings. |
162 // TODO(mad): Ask the destination for settings. | 97 // TODO(mad): Ask the destination for settings. |
163 if (ask_user_for_settings && destination_.get() == NULL) { | 98 if (ask_user_for_settings && destination_.get() == NULL) { |
164 BrowserThread::PostTask( | 99 BrowserThread::PostTask( |
165 BrowserThread::UI, FROM_HERE, | 100 BrowserThread::UI, FROM_HERE, |
166 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 101 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
167 base::Bind(&PrintJobWorker::GetSettingsWithUI, | 102 base::Bind(&PrintJobWorker::GetSettingsWithUI, |
168 base::Unretained(this), | 103 base::Unretained(this), |
| 104 base::Passed(&web_contents_observer), |
169 document_page_count, | 105 document_page_count, |
170 has_selection))); | 106 has_selection))); |
171 } else { | 107 } else { |
| 108 BrowserThread::DeleteSoon( |
| 109 BrowserThread::UI, FROM_HERE, web_contents_observer.release()); |
172 BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
173 BrowserThread::UI, FROM_HERE, | 111 BrowserThread::UI, FROM_HERE, |
174 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 112 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
175 base::Bind(&PrintJobWorker::UseDefaultSettings, | 113 base::Bind(&PrintJobWorker::UseDefaultSettings, |
176 base::Unretained(this)))); | 114 base::Unretained(this)))); |
177 } | 115 } |
178 } | 116 } |
179 | 117 |
180 void PrintJobWorker::SetSettings( | 118 void PrintJobWorker::SetSettings( |
181 scoped_ptr<base::DictionaryValue> new_settings) { | 119 scoped_ptr<base::DictionaryValue> new_settings) { |
182 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 120 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
183 | 121 |
184 BrowserThread::PostTask( | 122 BrowserThread::PostTask( |
185 BrowserThread::UI, | 123 BrowserThread::UI, |
186 FROM_HERE, | 124 FROM_HERE, |
187 base::Bind(&HoldRefCallback, | 125 base::Bind(&HoldRefCallback, |
188 make_scoped_refptr(owner_), | 126 make_scoped_refptr(owner_), |
189 base::Bind(&PrintJobWorker::UpdatePrintSettings, | 127 base::Bind(&PrintJobWorker::UpdatePrintSettings, |
190 base::Unretained(this), | 128 base::Unretained(this), |
191 base::Passed(&new_settings)))); | 129 base::Passed(&new_settings)))); |
192 } | 130 } |
193 | 131 |
194 void PrintJobWorker::UpdatePrintSettings( | 132 void PrintJobWorker::UpdatePrintSettings( |
195 scoped_ptr<base::DictionaryValue> new_settings) { | 133 scoped_ptr<base::DictionaryValue> new_settings) { |
196 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
197 PrintingContext::Result result = | 134 PrintingContext::Result result = |
198 printing_context_->UpdatePrintSettings(*new_settings); | 135 printing_context_->UpdatePrintSettings(*new_settings); |
199 GetSettingsDone(result); | 136 GetSettingsDone(result); |
200 } | 137 } |
201 | 138 |
202 void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) { | 139 void PrintJobWorker::GetSettingsDone(PrintingContext::Result result) { |
203 // Most PrintingContext functions may start a message loop and process | 140 // Most PrintingContext functions may start a message loop and process |
204 // message recursively, so disable recursive task processing. | 141 // message recursively, so disable recursive task processing. |
205 // TODO(thestig): See above comment. SetNestableTasksAllowed(false) needs to | 142 // TODO(thestig): See above comment. SetNestableTasksAllowed(false) needs to |
206 // be called on the same thread as the previous call. See | 143 // be called on the same thread as the previous call. See |
207 // http://crbug.com/73466 | 144 // http://crbug.com/73466 |
208 // MessageLoop::current()->SetNestableTasksAllowed(false); | 145 // MessageLoop::current()->SetNestableTasksAllowed(false); |
209 | 146 |
210 // We can't use OnFailure() here since owner_ may not support notifications. | 147 // We can't use OnFailure() here since owner_ may not support notifications. |
211 | 148 |
212 // PrintJob will create the new PrintedDocument. | 149 // PrintJob will create the new PrintedDocument. |
213 owner_->PostTask(FROM_HERE, | 150 owner_->PostTask(FROM_HERE, |
214 base::Bind(&PrintJobWorkerOwner::GetSettingsDone, | 151 base::Bind(&PrintJobWorkerOwner::GetSettingsDone, |
215 make_scoped_refptr(owner_), | 152 make_scoped_refptr(owner_), |
216 printing_context_->settings(), | 153 printing_context_->settings(), |
217 result)); | 154 result)); |
218 } | 155 } |
219 | 156 |
220 void PrintJobWorker::GetSettingsWithUI( | 157 void PrintJobWorker::GetSettingsWithUI( |
| 158 scoped_ptr<PrintingUIWebContentsObserver> web_contents_observer, |
221 int document_page_count, | 159 int document_page_count, |
222 bool has_selection) { | 160 bool has_selection) { |
223 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 161 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 162 |
| 163 gfx::NativeView parent_view = web_contents_observer->GetParentView(); |
| 164 if (!parent_view) { |
| 165 GetSettingsWithUIDone(printing::PrintingContext::FAILED); |
| 166 return; |
| 167 } |
224 printing_context_->AskUserForSettings( | 168 printing_context_->AskUserForSettings( |
225 document_page_count, | 169 parent_view, document_page_count, has_selection, |
226 has_selection, | |
227 base::Bind(&PrintJobWorker::GetSettingsWithUIDone, | 170 base::Bind(&PrintJobWorker::GetSettingsWithUIDone, |
228 base::Unretained(this))); | 171 base::Unretained(this))); |
229 } | 172 } |
230 | 173 |
231 void PrintJobWorker::GetSettingsWithUIDone(PrintingContext::Result result) { | 174 void PrintJobWorker::GetSettingsWithUIDone(PrintingContext::Result result) { |
232 PostTask(FROM_HERE, | 175 PostTask(FROM_HERE, |
233 base::Bind(&HoldRefCallback, | 176 base::Bind(&HoldRefCallback, |
234 make_scoped_refptr(owner_), | 177 make_scoped_refptr(owner_), |
235 base::Bind(&PrintJobWorker::GetSettingsDone, | 178 base::Bind(&PrintJobWorker::GetSettingsDone, |
236 base::Unretained(this), | 179 base::Unretained(this), |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 309 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
367 DCHECK_EQ(page_number_, PageNumber::npos()); | 310 DCHECK_EQ(page_number_, PageNumber::npos()); |
368 DCHECK(document_.get()); | 311 DCHECK(document_.get()); |
369 | 312 |
370 if (printing_context_->DocumentDone() != PrintingContext::OK) { | 313 if (printing_context_->DocumentDone() != PrintingContext::OK) { |
371 OnFailure(); | 314 OnFailure(); |
372 return; | 315 return; |
373 } | 316 } |
374 | 317 |
375 owner_->PostTask(FROM_HERE, | 318 owner_->PostTask(FROM_HERE, |
376 base::Bind(&NotificationCallback, | 319 base::Bind(NotificationCallback, |
377 make_scoped_refptr(owner_), | 320 make_scoped_refptr(owner_), |
378 JobEventDetails::DOC_DONE, | 321 JobEventDetails::DOC_DONE, |
379 document_, | 322 document_, |
380 scoped_refptr<PrintedPage>())); | 323 scoped_refptr<PrintedPage>())); |
381 | 324 |
382 // Makes sure the variables are reinitialized. | 325 // Makes sure the variables are reinitialized. |
383 document_ = NULL; | 326 document_ = NULL; |
384 } | 327 } |
385 | 328 |
386 void PrintJobWorker::SpoolPage(PrintedPage* page) { | 329 void PrintJobWorker::SpoolPage(PrintedPage* page) { |
387 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 330 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
388 DCHECK_NE(page_number_, PageNumber::npos()); | 331 DCHECK_NE(page_number_, PageNumber::npos()); |
389 | 332 |
390 // Signal everyone that the page is about to be printed. | 333 // Signal everyone that the page is about to be printed. |
391 owner_->PostTask(FROM_HERE, | 334 owner_->PostTask(FROM_HERE, |
392 base::Bind(&NotificationCallback, | 335 base::Bind(NotificationCallback, |
393 make_scoped_refptr(owner_), | 336 make_scoped_refptr(owner_), |
394 JobEventDetails::NEW_PAGE, | 337 JobEventDetails::NEW_PAGE, |
395 document_, | 338 document_, |
396 make_scoped_refptr(page))); | 339 make_scoped_refptr(page))); |
397 | 340 |
398 // Preprocess. | 341 // Preprocess. |
399 if (printing_context_->NewPage() != PrintingContext::OK) { | 342 if (printing_context_->NewPage() != PrintingContext::OK) { |
400 OnFailure(); | 343 OnFailure(); |
401 return; | 344 return; |
402 } | 345 } |
(...skipping 18 matching lines...) Expand all Loading... |
421 #endif | 364 #endif |
422 | 365 |
423 // Postprocess. | 366 // Postprocess. |
424 if (printing_context_->PageDone() != PrintingContext::OK) { | 367 if (printing_context_->PageDone() != PrintingContext::OK) { |
425 OnFailure(); | 368 OnFailure(); |
426 return; | 369 return; |
427 } | 370 } |
428 | 371 |
429 // Signal everyone that the page is printed. | 372 // Signal everyone that the page is printed. |
430 owner_->PostTask(FROM_HERE, | 373 owner_->PostTask(FROM_HERE, |
431 base::Bind(&NotificationCallback, | 374 base::Bind(NotificationCallback, |
432 make_scoped_refptr(owner_), | 375 make_scoped_refptr(owner_), |
433 JobEventDetails::PAGE_DONE, | 376 JobEventDetails::PAGE_DONE, |
434 document_, | 377 document_, |
435 make_scoped_refptr(page))); | 378 make_scoped_refptr(page))); |
436 } | 379 } |
437 | 380 |
438 void PrintJobWorker::OnFailure() { | 381 void PrintJobWorker::OnFailure() { |
439 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 382 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
440 | 383 |
441 // We may loose our last reference by broadcasting the FAILED event. | 384 // We may loose our last reference by broadcasting the FAILED event. |
442 scoped_refptr<PrintJobWorkerOwner> handle(owner_); | 385 scoped_refptr<PrintJobWorkerOwner> handle(owner_); |
443 | 386 |
444 owner_->PostTask(FROM_HERE, | 387 owner_->PostTask(FROM_HERE, |
445 base::Bind(&NotificationCallback, | 388 base::Bind(NotificationCallback, |
446 make_scoped_refptr(owner_), | 389 make_scoped_refptr(owner_), |
447 JobEventDetails::FAILED, | 390 JobEventDetails::FAILED, |
448 document_, | 391 document_, |
449 scoped_refptr<PrintedPage>())); | 392 scoped_refptr<PrintedPage>())); |
450 Cancel(); | 393 Cancel(); |
451 | 394 |
452 // Makes sure the variables are reinitialized. | 395 // Makes sure the variables are reinitialized. |
453 document_ = NULL; | 396 document_ = NULL; |
454 page_number_ = PageNumber::npos(); | 397 page_number_ = PageNumber::npos(); |
455 } | 398 } |
456 | 399 |
457 } // namespace printing | 400 } // namespace printing |
OLD | NEW |