Chromium Code Reviews| 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/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 21 #include "printing/print_job_constants.h" | 21 #include "printing/print_job_constants.h" |
| 22 #include "printing/printed_document.h" | 22 #include "printing/printed_document.h" |
| 23 #include "printing/printed_page.h" | 23 #include "printing/printed_page.h" |
| 24 #include "printing/printing_utils.h" | 24 #include "printing/printing_utils.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 | 26 |
| 27 #if defined(OS_ANDROID) | |
| 28 #include "chrome/browser/android/tab_android.h" | |
| 29 #endif | |
| 30 | |
| 27 using content::BrowserThread; | 31 using content::BrowserThread; |
| 28 | 32 |
| 29 namespace printing { | 33 namespace printing { |
| 30 | 34 |
| 31 namespace { | 35 namespace { |
| 32 | 36 |
| 33 // Helper function to ensure |owner| is valid until at least |callback| returns. | 37 // Helper function to ensure |owner| is valid until at least |callback| returns. |
| 34 void HoldRefCallback(const scoped_refptr<printing::PrintJobWorkerOwner>& owner, | 38 void HoldRefCallback(const scoped_refptr<printing::PrintJobWorkerOwner>& owner, |
| 35 const base::Closure& callback) { | 39 const base::Closure& callback) { |
| 36 callback.Run(); | 40 callback.Run(); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 | 117 |
| 114 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { | 118 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { |
| 115 DCHECK(page_number_ == PageNumber::npos()); | 119 DCHECK(page_number_ == PageNumber::npos()); |
| 116 owner_ = new_owner; | 120 owner_ = new_owner; |
| 117 } | 121 } |
| 118 | 122 |
| 119 void PrintJobWorker::GetSettings( | 123 void PrintJobWorker::GetSettings( |
| 120 bool ask_user_for_settings, | 124 bool ask_user_for_settings, |
| 121 int document_page_count, | 125 int document_page_count, |
| 122 bool has_selection, | 126 bool has_selection, |
| 123 MarginType margin_type) { | 127 MarginType margin_type, |
| 128 bool is_scripted) { | |
| 124 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 129 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 125 DCHECK_EQ(page_number_, PageNumber::npos()); | 130 DCHECK_EQ(page_number_, PageNumber::npos()); |
| 126 | 131 |
| 127 // Recursive task processing is needed for the dialog in case it needs to be | 132 // Recursive task processing is needed for the dialog in case it needs to be |
| 128 // destroyed by a task. | 133 // destroyed by a task. |
| 129 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed | 134 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed |
| 130 // on the thread where the PrintDlgEx is called, and definitely both calls | 135 // on the thread where the PrintDlgEx is called, and definitely both calls |
| 131 // should happen on the same thread. See http://crbug.com/73466 | 136 // should happen on the same thread. See http://crbug.com/73466 |
| 132 // MessageLoop::current()->SetNestableTasksAllowed(true); | 137 // MessageLoop::current()->SetNestableTasksAllowed(true); |
| 133 printing_context_->set_margin_type(margin_type); | 138 printing_context_->set_margin_type(margin_type); |
| 134 | 139 |
| 135 // When we delegate to a destination, we don't ask the user for settings. | 140 // When we delegate to a destination, we don't ask the user for settings. |
| 136 // TODO(mad): Ask the destination for settings. | 141 // TODO(mad): Ask the destination for settings. |
| 137 if (ask_user_for_settings) { | 142 if (ask_user_for_settings) { |
| 138 BrowserThread::PostTask( | 143 BrowserThread::PostTask( |
| 139 BrowserThread::UI, FROM_HERE, | 144 BrowserThread::UI, FROM_HERE, |
| 140 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 145 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
| 141 base::Bind(&PrintJobWorker::GetSettingsWithUI, | 146 base::Bind(&PrintJobWorker::GetSettingsWithUI, |
| 142 base::Unretained(this), | 147 base::Unretained(this), |
| 143 document_page_count, | 148 document_page_count, |
| 144 has_selection))); | 149 has_selection, |
| 150 is_scripted))); | |
| 145 } else { | 151 } else { |
| 146 BrowserThread::PostTask( | 152 BrowserThread::PostTask( |
| 147 BrowserThread::UI, FROM_HERE, | 153 BrowserThread::UI, FROM_HERE, |
| 148 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 154 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
| 149 base::Bind(&PrintJobWorker::UseDefaultSettings, | 155 base::Bind(&PrintJobWorker::UseDefaultSettings, |
| 150 base::Unretained(this)))); | 156 base::Unretained(this)))); |
| 151 } | 157 } |
| 152 } | 158 } |
| 153 | 159 |
| 154 void PrintJobWorker::SetSettings( | 160 void PrintJobWorker::SetSettings( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 // PrintJob will create the new PrintedDocument. | 192 // PrintJob will create the new PrintedDocument. |
| 187 owner_->PostTask(FROM_HERE, | 193 owner_->PostTask(FROM_HERE, |
| 188 base::Bind(&PrintJobWorkerOwner::GetSettingsDone, | 194 base::Bind(&PrintJobWorkerOwner::GetSettingsDone, |
| 189 make_scoped_refptr(owner_), | 195 make_scoped_refptr(owner_), |
| 190 printing_context_->settings(), | 196 printing_context_->settings(), |
| 191 result)); | 197 result)); |
| 192 } | 198 } |
| 193 | 199 |
| 194 void PrintJobWorker::GetSettingsWithUI( | 200 void PrintJobWorker::GetSettingsWithUI( |
| 195 int document_page_count, | 201 int document_page_count, |
| 196 bool has_selection) { | 202 bool has_selection, |
| 203 bool is_scripted) { | |
| 197 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 204 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 205 | |
| 206 #if defined(OS_ANDROID) | |
| 207 if (is_scripted) { | |
| 208 PrintingContextDelegate* pcd = | |
| 209 static_cast<PrintingContextDelegate*>(printing_context_delegate_.get()); | |
| 210 content::WebContents* wc = pcd->GetWebContents(); | |
|
Bernhard Bauer
2014/12/15 09:38:02
Nit: Try not to use abbreviations. Maybe |contents
dgn
2014/12/15 10:27:05
Done.
| |
| 211 TabAndroid* tab = TabAndroid::FromWebContents(wc); | |
| 212 if (tab) { | |
| 213 // If fails or is not called, attempting to start a pending print does | |
|
Bernhard Bauer
2014/12/15 09:38:02
If what fails?
dgn
2014/12/15 10:27:06
If setPendingPrint fails (if there is a print in p
| |
| 214 // nothing and will notify that printing is finished. | |
| 215 tab->SetPendingPrint(); | |
| 216 } | |
| 217 } | |
| 218 #endif | |
| 219 | |
| 198 printing_context_->AskUserForSettings( | 220 printing_context_->AskUserForSettings( |
| 199 document_page_count, | 221 document_page_count, |
| 200 has_selection, | 222 has_selection, |
| 223 is_scripted, | |
| 201 base::Bind(&PrintJobWorker::GetSettingsWithUIDone, | 224 base::Bind(&PrintJobWorker::GetSettingsWithUIDone, |
| 202 base::Unretained(this))); | 225 base::Unretained(this))); |
| 203 } | 226 } |
| 204 | 227 |
| 205 void PrintJobWorker::GetSettingsWithUIDone(PrintingContext::Result result) { | 228 void PrintJobWorker::GetSettingsWithUIDone(PrintingContext::Result result) { |
| 206 PostTask(FROM_HERE, | 229 PostTask(FROM_HERE, |
| 207 base::Bind(&HoldRefCallback, | 230 base::Bind(&HoldRefCallback, |
| 208 make_scoped_refptr(owner_), | 231 make_scoped_refptr(owner_), |
| 209 base::Bind(&PrintJobWorker::GetSettingsDone, | 232 base::Bind(&PrintJobWorker::GetSettingsDone, |
| 210 base::Unretained(this), | 233 base::Unretained(this), |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 document_, | 431 document_, |
| 409 scoped_refptr<PrintedPage>())); | 432 scoped_refptr<PrintedPage>())); |
| 410 Cancel(); | 433 Cancel(); |
| 411 | 434 |
| 412 // Makes sure the variables are reinitialized. | 435 // Makes sure the variables are reinitialized. |
| 413 document_ = NULL; | 436 document_ = NULL; |
| 414 page_number_ = PageNumber::npos(); | 437 page_number_ = PageNumber::npos(); |
| 415 } | 438 } |
| 416 | 439 |
| 417 } // namespace printing | 440 } // namespace printing |
| OLD | NEW |