| 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/android/tab_android.h" |
| 13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/printing/print_job.h" | 16 #include "chrome/browser/printing/print_job.h" |
| 16 #include "chrome/grit/generated_resources.h" | 17 #include "chrome/grit/generated_resources.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/render_view_host.h" | 20 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 21 #include "printing/print_job_constants.h" | 22 #include "printing/print_job_constants.h" |
| 22 #include "printing/printed_document.h" | 23 #include "printing/printed_document.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 37 } | 38 } |
| 38 | 39 |
| 39 class PrintingContextDelegate : public PrintingContext::Delegate { | 40 class PrintingContextDelegate : public PrintingContext::Delegate { |
| 40 public: | 41 public: |
| 41 PrintingContextDelegate(int render_process_id, int render_view_id); | 42 PrintingContextDelegate(int render_process_id, int render_view_id); |
| 42 ~PrintingContextDelegate() override; | 43 ~PrintingContextDelegate() override; |
| 43 | 44 |
| 44 gfx::NativeView GetParentView() override; | 45 gfx::NativeView GetParentView() override; |
| 45 std::string GetAppLocale() override; | 46 std::string GetAppLocale() override; |
| 46 | 47 |
| 48 void ShowSystemDialog() override; |
| 49 |
| 47 private: | 50 private: |
| 48 int render_process_id_; | 51 int render_process_id_; |
| 49 int render_view_id_; | 52 int render_view_id_; |
| 50 }; | 53 }; |
| 51 | 54 |
| 52 PrintingContextDelegate::PrintingContextDelegate(int render_process_id, | 55 PrintingContextDelegate::PrintingContextDelegate(int render_process_id, |
| 53 int render_view_id) | 56 int render_view_id) |
| 54 : render_process_id_(render_process_id), | 57 : render_process_id_(render_process_id), |
| 55 render_view_id_(render_view_id) { | 58 render_view_id_(render_view_id) { |
| 56 } | 59 } |
| 57 | 60 |
| 58 PrintingContextDelegate::~PrintingContextDelegate() { | 61 PrintingContextDelegate::~PrintingContextDelegate() { |
| 59 } | 62 } |
| 60 | 63 |
| 61 gfx::NativeView PrintingContextDelegate::GetParentView() { | 64 gfx::NativeView PrintingContextDelegate::GetParentView() { |
| 62 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 63 content::RenderViewHost* view = | 66 content::RenderViewHost* view = |
| 64 content::RenderViewHost::FromID(render_process_id_, render_view_id_); | 67 content::RenderViewHost::FromID(render_process_id_, render_view_id_); |
| 65 if (!view) | 68 if (!view) |
| 66 return NULL; | 69 return NULL; |
| 67 content::WebContents* wc = content::WebContents::FromRenderViewHost(view); | 70 content::WebContents* wc = content::WebContents::FromRenderViewHost(view); |
| 68 return wc ? wc->GetNativeView() : NULL; | 71 return wc ? wc->GetNativeView() : NULL; |
| 69 } | 72 } |
| 70 | 73 |
| 71 std::string PrintingContextDelegate::GetAppLocale() { | 74 std::string PrintingContextDelegate::GetAppLocale() { |
| 72 return g_browser_process->GetApplicationLocale(); | 75 return g_browser_process->GetApplicationLocale(); |
| 73 } | 76 } |
| 74 | 77 |
| 78 void PrintingContextDelegate::ShowSystemDialog() { |
| 79 LOG(INFO) << "DGN PrintingContextDelegate::ShowSystemDialog"; |
| 80 #if defined(OS_ANDROID) |
| 81 // Tab (Printable) needed to print. How to call it without the tab directly? |
| 82 // Removing the dependency on chrome would make it easier to move it to |
| 83 // printing_context_android |
| 84 content::RenderViewHost* view = content::RenderViewHost::FromID( |
| 85 render_process_id_, render_view_id_); |
| 86 DCHECK(view); |
| 87 content::WebContents* wc = content::WebContents::FromRenderViewHost(view); |
| 88 TabAndroid* tab = TabAndroid::FromWebContents(wc); |
| 89 if (tab) tab->ShowPrintDialog(); |
| 90 #endif |
| 91 } |
| 92 |
| 93 |
| 75 void NotificationCallback(PrintJobWorkerOwner* print_job, | 94 void NotificationCallback(PrintJobWorkerOwner* print_job, |
| 76 JobEventDetails::Type detail_type, | 95 JobEventDetails::Type detail_type, |
| 77 PrintedDocument* document, | 96 PrintedDocument* document, |
| 78 PrintedPage* page) { | 97 PrintedPage* page) { |
| 79 JobEventDetails* details = new JobEventDetails(detail_type, document, page); | 98 JobEventDetails* details = new JobEventDetails(detail_type, document, page); |
| 80 content::NotificationService::current()->Notify( | 99 content::NotificationService::current()->Notify( |
| 81 chrome::NOTIFICATION_PRINT_JOB_EVENT, | 100 chrome::NOTIFICATION_PRINT_JOB_EVENT, |
| 82 // We know that is is a PrintJob object in this circumstance. | 101 // We know that is is a PrintJob object in this circumstance. |
| 83 content::Source<PrintJob>(static_cast<PrintJob*>(print_job)), | 102 content::Source<PrintJob>(static_cast<PrintJob*>(print_job)), |
| 84 content::Details<JobEventDetails>(details)); | 103 content::Details<JobEventDetails>(details)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 105 DCHECK(owner_->RunsTasksOnCurrentThread()); | 124 DCHECK(owner_->RunsTasksOnCurrentThread()); |
| 106 Stop(); | 125 Stop(); |
| 107 } | 126 } |
| 108 | 127 |
| 109 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { | 128 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { |
| 110 DCHECK(page_number_ == PageNumber::npos()); | 129 DCHECK(page_number_ == PageNumber::npos()); |
| 111 owner_ = new_owner; | 130 owner_ = new_owner; |
| 112 } | 131 } |
| 113 | 132 |
| 114 void PrintJobWorker::GetSettings( | 133 void PrintJobWorker::GetSettings( |
| 115 bool ask_user_for_settings, | 134 GetSettingsAskParam ask_user_for_settings, |
| 116 int document_page_count, | 135 int document_page_count, |
| 117 bool has_selection, | 136 bool has_selection, |
| 118 MarginType margin_type) { | 137 MarginType margin_type) { |
| 119 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 138 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 120 DCHECK_EQ(page_number_, PageNumber::npos()); | 139 DCHECK_EQ(page_number_, PageNumber::npos()); |
| 121 | 140 |
| 141 LOG(INFO) << "DGN GetSettings - askParam: " << ask_user_for_settings; |
| 142 |
| 122 // Recursive task processing is needed for the dialog in case it needs to be | 143 // Recursive task processing is needed for the dialog in case it needs to be |
| 123 // destroyed by a task. | 144 // destroyed by a task. |
| 124 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed | 145 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed |
| 125 // on the thread where the PrintDlgEx is called, and definitely both calls | 146 // on the thread where the PrintDlgEx is called, and definitely both calls |
| 126 // should happen on the same thread. See http://crbug.com/73466 | 147 // should happen on the same thread. See http://crbug.com/73466 |
| 127 // MessageLoop::current()->SetNestableTasksAllowed(true); | 148 // MessageLoop::current()->SetNestableTasksAllowed(true); |
| 128 printing_context_->set_margin_type(margin_type); | 149 printing_context_->set_margin_type(margin_type); |
| 129 | 150 |
| 130 // When we delegate to a destination, we don't ask the user for settings. | 151 // When we delegate to a destination, we don't ask the user for settings. |
| 131 // TODO(mad): Ask the destination for settings. | 152 // TODO(mad): Ask the destination for settings. |
| 132 if (ask_user_for_settings) { | 153 if (ask_user_for_settings == GetSettingsAskParam::ASK_USER) { |
| 133 BrowserThread::PostTask( | 154 BrowserThread::PostTask( |
| 134 BrowserThread::UI, FROM_HERE, | 155 BrowserThread::UI, FROM_HERE, |
| 135 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 156 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
| 136 base::Bind(&PrintJobWorker::GetSettingsWithUI, | 157 base::Bind(&PrintJobWorker::GetSettingsWithUI, |
| 137 base::Unretained(this), | 158 base::Unretained(this), |
| 138 document_page_count, | 159 document_page_count, |
| 139 has_selection))); | 160 has_selection))); |
| 161 } else if (ask_user_for_settings == GetSettingsAskParam::SYSTEM_SPECIFIC) { |
| 162 LOG(INFO) << "DGN SYSTEM_SPECIFIC! "; |
| 163 BrowserThread::PostTask( |
| 164 BrowserThread::UI, FROM_HERE, |
| 165 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
| 166 base::Bind(&PrintJobWorker::ShowSystemDialog, |
| 167 base::Unretained(this)))); |
| 140 } else { | 168 } else { |
| 141 BrowserThread::PostTask( | 169 BrowserThread::PostTask( |
| 142 BrowserThread::UI, FROM_HERE, | 170 BrowserThread::UI, FROM_HERE, |
| 143 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 171 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
| 144 base::Bind(&PrintJobWorker::UseDefaultSettings, | 172 base::Bind(&PrintJobWorker::UseDefaultSettings, |
| 145 base::Unretained(this)))); | 173 base::Unretained(this)))); |
| 146 } | 174 } |
| 147 } | 175 } |
| 148 | 176 |
| 177 void PrintJobWorker::ShowSystemDialog() { |
| 178 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 179 LOG(INFO) << "DGN ShowSystemDialog"; |
| 180 |
| 181 if (printing_context_ == NULL) LOG(INFO) << "DGN printing_context_ is NULL"; |
| 182 |
| 183 printing_context_->SetPrintSettingsCallback( |
| 184 base::Bind(&PrintJobWorkerOwner::ShowSystemDialogDone, |
| 185 base::Unretained(owner_))); |
| 186 |
| 187 printing_context_delegate_->ShowSystemDialog(); |
| 188 } |
| 189 |
| 149 void PrintJobWorker::SetSettings( | 190 void PrintJobWorker::SetSettings( |
| 150 scoped_ptr<base::DictionaryValue> new_settings) { | 191 scoped_ptr<base::DictionaryValue> new_settings) { |
| 151 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 192 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 152 | 193 |
| 153 BrowserThread::PostTask( | 194 BrowserThread::PostTask( |
| 154 BrowserThread::UI, | 195 BrowserThread::UI, |
| 155 FROM_HERE, | 196 FROM_HERE, |
| 156 base::Bind(&HoldRefCallback, | 197 base::Bind(&HoldRefCallback, |
| 157 make_scoped_refptr(owner_), | 198 make_scoped_refptr(owner_), |
| 158 base::Bind(&PrintJobWorker::UpdatePrintSettings, | 199 base::Bind(&PrintJobWorker::UpdatePrintSettings, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 183 base::Bind(&PrintJobWorkerOwner::GetSettingsDone, | 224 base::Bind(&PrintJobWorkerOwner::GetSettingsDone, |
| 184 make_scoped_refptr(owner_), | 225 make_scoped_refptr(owner_), |
| 185 printing_context_->settings(), | 226 printing_context_->settings(), |
| 186 result)); | 227 result)); |
| 187 } | 228 } |
| 188 | 229 |
| 189 void PrintJobWorker::GetSettingsWithUI( | 230 void PrintJobWorker::GetSettingsWithUI( |
| 190 int document_page_count, | 231 int document_page_count, |
| 191 bool has_selection) { | 232 bool has_selection) { |
| 192 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 233 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 193 printing_context_->AskUserForSettings( | 234 printing_context_->AskUserForSettings( |
| 194 document_page_count, | 235 document_page_count, |
| 195 has_selection, | 236 has_selection, |
| 196 base::Bind(&PrintJobWorker::GetSettingsWithUIDone, | 237 base::Bind(&PrintJobWorker::GetSettingsWithUIDone, |
| 197 base::Unretained(this))); | 238 base::Unretained(this))); |
| 198 } | 239 } |
| 199 | 240 |
| 200 void PrintJobWorker::GetSettingsWithUIDone(PrintingContext::Result result) { | 241 void PrintJobWorker::GetSettingsWithUIDone(PrintingContext::Result result) { |
| 201 PostTask(FROM_HERE, | 242 PostTask(FROM_HERE, |
| 202 base::Bind(&HoldRefCallback, | 243 base::Bind(&HoldRefCallback, |
| 203 make_scoped_refptr(owner_), | 244 make_scoped_refptr(owner_), |
| 204 base::Bind(&PrintJobWorker::GetSettingsDone, | 245 base::Bind(&PrintJobWorker::GetSettingsDone, |
| 205 base::Unretained(this), | 246 base::Unretained(this), |
| 206 result))); | 247 result))); |
| 207 } | 248 } |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 document_, | 444 document_, |
| 404 scoped_refptr<PrintedPage>())); | 445 scoped_refptr<PrintedPage>())); |
| 405 Cancel(); | 446 Cancel(); |
| 406 | 447 |
| 407 // Makes sure the variables are reinitialized. | 448 // Makes sure the variables are reinitialized. |
| 408 document_ = NULL; | 449 document_ = NULL; |
| 409 page_number_ = PageNumber::npos(); | 450 page_number_ = PageNumber::npos(); |
| 410 } | 451 } |
| 411 | 452 |
| 412 } // namespace printing | 453 } // namespace printing |
| OLD | NEW |