| 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 <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 base::Bind(callback, result))); | 109 base::Bind(callback, result))); |
| 110 } | 110 } |
| 111 | 111 |
| 112 } // namespace | 112 } // namespace |
| 113 | 113 |
| 114 PrintJobWorker::PrintJobWorker(int render_process_id, | 114 PrintJobWorker::PrintJobWorker(int render_process_id, |
| 115 int render_frame_id, | 115 int render_frame_id, |
| 116 PrintJobWorkerOwner* owner) | 116 PrintJobWorkerOwner* owner) |
| 117 : owner_(owner), thread_("Printing_Worker"), weak_factory_(this) { | 117 : owner_(owner), thread_("Printing_Worker"), weak_factory_(this) { |
| 118 // The object is created in the IO thread. | 118 // The object is created in the IO thread. |
| 119 DCHECK(owner_->RunsTasksOnCurrentThread()); | 119 DCHECK(owner_->RunsTasksInCurrentSequence()); |
| 120 | 120 |
| 121 printing_context_delegate_ = base::MakeUnique<PrintingContextDelegate>( | 121 printing_context_delegate_ = base::MakeUnique<PrintingContextDelegate>( |
| 122 render_process_id, render_frame_id); | 122 render_process_id, render_frame_id); |
| 123 printing_context_ = PrintingContext::Create(printing_context_delegate_.get()); | 123 printing_context_ = PrintingContext::Create(printing_context_delegate_.get()); |
| 124 } | 124 } |
| 125 | 125 |
| 126 PrintJobWorker::~PrintJobWorker() { | 126 PrintJobWorker::~PrintJobWorker() { |
| 127 // The object is normally deleted in the UI thread, but when the user | 127 // The object is normally deleted in the UI thread, but when the user |
| 128 // cancels printing or in the case of print preview, the worker is destroyed | 128 // cancels printing or in the case of print preview, the worker is destroyed |
| 129 // on the I/O thread. | 129 // on the I/O thread. |
| 130 DCHECK(owner_->RunsTasksOnCurrentThread()); | 130 DCHECK(owner_->RunsTasksInCurrentSequence()); |
| 131 Stop(); | 131 Stop(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { | 134 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { |
| 135 DCHECK(page_number_ == PageNumber::npos()); | 135 DCHECK(page_number_ == PageNumber::npos()); |
| 136 owner_ = new_owner; | 136 owner_ = new_owner; |
| 137 } | 137 } |
| 138 | 138 |
| 139 void PrintJobWorker::GetSettings(bool ask_user_for_settings, | 139 void PrintJobWorker::GetSettings(bool ask_user_for_settings, |
| 140 int document_page_count, | 140 int document_page_count, |
| 141 bool has_selection, | 141 bool has_selection, |
| 142 MarginType margin_type, | 142 MarginType margin_type, |
| 143 bool is_scripted, | 143 bool is_scripted, |
| 144 bool is_modifiable) { | 144 bool is_modifiable) { |
| 145 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 145 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 146 DCHECK_EQ(page_number_, PageNumber::npos()); | 146 DCHECK_EQ(page_number_, PageNumber::npos()); |
| 147 | 147 |
| 148 // Recursive task processing is needed for the dialog in case it needs to be | 148 // Recursive task processing is needed for the dialog in case it needs to be |
| 149 // destroyed by a task. | 149 // destroyed by a task. |
| 150 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed | 150 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed |
| 151 // on the thread where the PrintDlgEx is called, and definitely both calls | 151 // on the thread where the PrintDlgEx is called, and definitely both calls |
| 152 // should happen on the same thread. See http://crbug.com/73466 | 152 // should happen on the same thread. See http://crbug.com/73466 |
| 153 // MessageLoop::current()->SetNestableTasksAllowed(true); | 153 // MessageLoop::current()->SetNestableTasksAllowed(true); |
| 154 printing_context_->set_margin_type(margin_type); | 154 printing_context_->set_margin_type(margin_type); |
| 155 printing_context_->set_is_modifiable(is_modifiable); | 155 printing_context_->set_is_modifiable(is_modifiable); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 167 BrowserThread::PostTask( | 167 BrowserThread::PostTask( |
| 168 BrowserThread::UI, FROM_HERE, | 168 BrowserThread::UI, FROM_HERE, |
| 169 base::BindOnce(&HoldRefCallback, make_scoped_refptr(owner_), | 169 base::BindOnce(&HoldRefCallback, make_scoped_refptr(owner_), |
| 170 base::Bind(&PrintJobWorker::UseDefaultSettings, | 170 base::Bind(&PrintJobWorker::UseDefaultSettings, |
| 171 base::Unretained(this)))); | 171 base::Unretained(this)))); |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 | 174 |
| 175 void PrintJobWorker::SetSettings( | 175 void PrintJobWorker::SetSettings( |
| 176 std::unique_ptr<base::DictionaryValue> new_settings) { | 176 std::unique_ptr<base::DictionaryValue> new_settings) { |
| 177 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 177 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 178 | 178 |
| 179 BrowserThread::PostTask( | 179 BrowserThread::PostTask( |
| 180 BrowserThread::UI, FROM_HERE, | 180 BrowserThread::UI, FROM_HERE, |
| 181 base::BindOnce( | 181 base::BindOnce( |
| 182 &HoldRefCallback, make_scoped_refptr(owner_), | 182 &HoldRefCallback, make_scoped_refptr(owner_), |
| 183 base::Bind(&PrintJobWorker::UpdatePrintSettings, | 183 base::Bind(&PrintJobWorker::UpdatePrintSettings, |
| 184 base::Unretained(this), base::Passed(&new_settings)))); | 184 base::Unretained(this), base::Passed(&new_settings)))); |
| 185 } | 185 } |
| 186 | 186 |
| 187 void PrintJobWorker::UpdatePrintSettings( | 187 void PrintJobWorker::UpdatePrintSettings( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 base::Bind(&PrintJobWorker::GetSettingsDone, | 240 base::Bind(&PrintJobWorker::GetSettingsDone, |
| 241 weak_factory_.GetWeakPtr()))); | 241 weak_factory_.GetWeakPtr()))); |
| 242 } | 242 } |
| 243 | 243 |
| 244 void PrintJobWorker::UseDefaultSettings() { | 244 void PrintJobWorker::UseDefaultSettings() { |
| 245 PrintingContext::Result result = printing_context_->UseDefaultSettings(); | 245 PrintingContext::Result result = printing_context_->UseDefaultSettings(); |
| 246 GetSettingsDone(result); | 246 GetSettingsDone(result); |
| 247 } | 247 } |
| 248 | 248 |
| 249 void PrintJobWorker::StartPrinting(PrintedDocument* new_document) { | 249 void PrintJobWorker::StartPrinting(PrintedDocument* new_document) { |
| 250 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 250 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 251 DCHECK_EQ(page_number_, PageNumber::npos()); | 251 DCHECK_EQ(page_number_, PageNumber::npos()); |
| 252 DCHECK_EQ(document_.get(), new_document); | 252 DCHECK_EQ(document_.get(), new_document); |
| 253 DCHECK(document_.get()); | 253 DCHECK(document_.get()); |
| 254 | 254 |
| 255 if (!document_.get() || page_number_ != PageNumber::npos() || | 255 if (!document_.get() || page_number_ != PageNumber::npos() || |
| 256 document_.get() != new_document) { | 256 document_.get() != new_document) { |
| 257 return; | 257 return; |
| 258 } | 258 } |
| 259 | 259 |
| 260 base::string16 document_name = SimplifyDocumentTitle(document_->name()); | 260 base::string16 document_name = SimplifyDocumentTitle(document_->name()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 272 // Try to print already cached data. It may already have been generated for | 272 // Try to print already cached data. It may already have been generated for |
| 273 // the print preview. | 273 // the print preview. |
| 274 OnNewPage(); | 274 OnNewPage(); |
| 275 // Don't touch this anymore since the instance could be destroyed. It happens | 275 // Don't touch this anymore since the instance could be destroyed. It happens |
| 276 // if all the pages are printed a one sweep and the client doesn't have a | 276 // if all the pages are printed a one sweep and the client doesn't have a |
| 277 // handle to us anymore. There's a timing issue involved between the worker | 277 // handle to us anymore. There's a timing issue involved between the worker |
| 278 // thread and the UI thread. Take no chance. | 278 // thread and the UI thread. Take no chance. |
| 279 } | 279 } |
| 280 | 280 |
| 281 void PrintJobWorker::OnDocumentChanged(PrintedDocument* new_document) { | 281 void PrintJobWorker::OnDocumentChanged(PrintedDocument* new_document) { |
| 282 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 282 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 283 DCHECK_EQ(page_number_, PageNumber::npos()); | 283 DCHECK_EQ(page_number_, PageNumber::npos()); |
| 284 | 284 |
| 285 if (page_number_ != PageNumber::npos()) | 285 if (page_number_ != PageNumber::npos()) |
| 286 return; | 286 return; |
| 287 | 287 |
| 288 document_ = new_document; | 288 document_ = new_document; |
| 289 } | 289 } |
| 290 | 290 |
| 291 void PrintJobWorker::OnNewPage() { | 291 void PrintJobWorker::OnNewPage() { |
| 292 if (!document_.get()) // Spurious message. | 292 if (!document_.get()) // Spurious message. |
| 293 return; | 293 return; |
| 294 | 294 |
| 295 // message_loop() could return NULL when the print job is cancelled. | 295 // message_loop() could return NULL when the print job is cancelled. |
| 296 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 296 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 297 | 297 |
| 298 if (page_number_ == PageNumber::npos()) { | 298 if (page_number_ == PageNumber::npos()) { |
| 299 // Find first page to print. | 299 // Find first page to print. |
| 300 int page_count = document_->page_count(); | 300 int page_count = document_->page_count(); |
| 301 if (!page_count) { | 301 if (!page_count) { |
| 302 // We still don't know how many pages the document contains. We can't | 302 // We still don't know how many pages the document contains. We can't |
| 303 // start to print the document yet since the header/footer may refer to | 303 // start to print the document yet since the header/footer may refer to |
| 304 // the document's page count. | 304 // the document's page count. |
| 305 return; | 305 return; |
| 306 } | 306 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 thread_.Stop(); | 358 thread_.Stop(); |
| 359 } | 359 } |
| 360 | 360 |
| 361 bool PrintJobWorker::Start() { | 361 bool PrintJobWorker::Start() { |
| 362 bool result = thread_.Start(); | 362 bool result = thread_.Start(); |
| 363 task_runner_ = thread_.task_runner(); | 363 task_runner_ = thread_.task_runner(); |
| 364 return result; | 364 return result; |
| 365 } | 365 } |
| 366 | 366 |
| 367 void PrintJobWorker::OnDocumentDone() { | 367 void PrintJobWorker::OnDocumentDone() { |
| 368 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 368 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 369 DCHECK_EQ(page_number_, PageNumber::npos()); | 369 DCHECK_EQ(page_number_, PageNumber::npos()); |
| 370 DCHECK(document_.get()); | 370 DCHECK(document_.get()); |
| 371 | 371 |
| 372 int job_id = printing_context_->job_id(); | 372 int job_id = printing_context_->job_id(); |
| 373 if (printing_context_->DocumentDone() != PrintingContext::OK) { | 373 if (printing_context_->DocumentDone() != PrintingContext::OK) { |
| 374 OnFailure(); | 374 OnFailure(); |
| 375 return; | 375 return; |
| 376 } | 376 } |
| 377 | 377 |
| 378 owner_->PostTask(FROM_HERE, | 378 owner_->PostTask(FROM_HERE, |
| 379 base::Bind(&NotificationCallback, base::RetainedRef(owner_), | 379 base::Bind(&NotificationCallback, base::RetainedRef(owner_), |
| 380 JobEventDetails::DOC_DONE, job_id, | 380 JobEventDetails::DOC_DONE, job_id, |
| 381 base::RetainedRef(document_), nullptr)); | 381 base::RetainedRef(document_), nullptr)); |
| 382 | 382 |
| 383 // Makes sure the variables are reinitialized. | 383 // Makes sure the variables are reinitialized. |
| 384 document_ = NULL; | 384 document_ = NULL; |
| 385 } | 385 } |
| 386 | 386 |
| 387 void PrintJobWorker::SpoolPage(PrintedPage* page) { | 387 void PrintJobWorker::SpoolPage(PrintedPage* page) { |
| 388 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 388 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 389 DCHECK_NE(page_number_, PageNumber::npos()); | 389 DCHECK_NE(page_number_, PageNumber::npos()); |
| 390 | 390 |
| 391 // Signal everyone that the page is about to be printed. | 391 // Signal everyone that the page is about to be printed. |
| 392 owner_->PostTask( | 392 owner_->PostTask( |
| 393 FROM_HERE, | 393 FROM_HERE, |
| 394 base::Bind(&NotificationCallback, base::RetainedRef(owner_), | 394 base::Bind(&NotificationCallback, base::RetainedRef(owner_), |
| 395 JobEventDetails::NEW_PAGE, printing_context_->job_id(), | 395 JobEventDetails::NEW_PAGE, printing_context_->job_id(), |
| 396 base::RetainedRef(document_), base::RetainedRef(page))); | 396 base::RetainedRef(document_), base::RetainedRef(page))); |
| 397 | 397 |
| 398 // Preprocess. | 398 // Preprocess. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 416 | 416 |
| 417 // Signal everyone that the page is printed. | 417 // Signal everyone that the page is printed. |
| 418 owner_->PostTask( | 418 owner_->PostTask( |
| 419 FROM_HERE, | 419 FROM_HERE, |
| 420 base::Bind(&NotificationCallback, base::RetainedRef(owner_), | 420 base::Bind(&NotificationCallback, base::RetainedRef(owner_), |
| 421 JobEventDetails::PAGE_DONE, printing_context_->job_id(), | 421 JobEventDetails::PAGE_DONE, printing_context_->job_id(), |
| 422 base::RetainedRef(document_), base::RetainedRef(page))); | 422 base::RetainedRef(document_), base::RetainedRef(page))); |
| 423 } | 423 } |
| 424 | 424 |
| 425 void PrintJobWorker::OnFailure() { | 425 void PrintJobWorker::OnFailure() { |
| 426 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 426 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 427 | 427 |
| 428 // We may loose our last reference by broadcasting the FAILED event. | 428 // We may loose our last reference by broadcasting the FAILED event. |
| 429 scoped_refptr<PrintJobWorkerOwner> handle(owner_); | 429 scoped_refptr<PrintJobWorkerOwner> handle(owner_); |
| 430 | 430 |
| 431 owner_->PostTask(FROM_HERE, | 431 owner_->PostTask(FROM_HERE, |
| 432 base::Bind(&NotificationCallback, base::RetainedRef(owner_), | 432 base::Bind(&NotificationCallback, base::RetainedRef(owner_), |
| 433 JobEventDetails::FAILED, 0, | 433 JobEventDetails::FAILED, 0, |
| 434 base::RetainedRef(document_), nullptr)); | 434 base::RetainedRef(document_), nullptr)); |
| 435 Cancel(); | 435 Cancel(); |
| 436 | 436 |
| 437 // Makes sure the variables are reinitialized. | 437 // Makes sure the variables are reinitialized. |
| 438 document_ = NULL; | 438 document_ = NULL; |
| 439 page_number_ = PageNumber::npos(); | 439 page_number_ = PageNumber::npos(); |
| 440 } | 440 } |
| 441 | 441 |
| 442 } // namespace printing | 442 } // namespace printing |
| OLD | NEW |