| 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" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 // on the I/O thread. | 104 // on the I/O thread. |
| 105 DCHECK(owner_->RunsTasksOnCurrentThread()); | 105 DCHECK(owner_->RunsTasksOnCurrentThread()); |
| 106 Stop(); | 106 Stop(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { | 109 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { |
| 110 DCHECK(page_number_ == PageNumber::npos()); | 110 DCHECK(page_number_ == PageNumber::npos()); |
| 111 owner_ = new_owner; | 111 owner_ = new_owner; |
| 112 } | 112 } |
| 113 | 113 |
| 114 void PrintJobWorker::SetPrintDestination( | |
| 115 PrintDestinationInterface* destination) { | |
| 116 destination_ = destination; | |
| 117 } | |
| 118 | |
| 119 void PrintJobWorker::GetSettings( | 114 void PrintJobWorker::GetSettings( |
| 120 bool ask_user_for_settings, | 115 bool ask_user_for_settings, |
| 121 int document_page_count, | 116 int document_page_count, |
| 122 bool has_selection, | 117 bool has_selection, |
| 123 MarginType margin_type) { | 118 MarginType margin_type) { |
| 124 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 119 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 125 DCHECK_EQ(page_number_, PageNumber::npos()); | 120 DCHECK_EQ(page_number_, PageNumber::npos()); |
| 126 | 121 |
| 127 // Recursive task processing is needed for the dialog in case it needs to be | 122 // Recursive task processing is needed for the dialog in case it needs to be |
| 128 // destroyed by a task. | 123 // destroyed by a task. |
| 129 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed | 124 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed |
| 130 // on the thread where the PrintDlgEx is called, and definitely both calls | 125 // on the thread where the PrintDlgEx is called, and definitely both calls |
| 131 // should happen on the same thread. See http://crbug.com/73466 | 126 // should happen on the same thread. See http://crbug.com/73466 |
| 132 // MessageLoop::current()->SetNestableTasksAllowed(true); | 127 // MessageLoop::current()->SetNestableTasksAllowed(true); |
| 133 printing_context_->set_margin_type(margin_type); | 128 printing_context_->set_margin_type(margin_type); |
| 134 | 129 |
| 135 // When we delegate to a destination, we don't ask the user for settings. | 130 // When we delegate to a destination, we don't ask the user for settings. |
| 136 // TODO(mad): Ask the destination for settings. | 131 // TODO(mad): Ask the destination for settings. |
| 137 if (ask_user_for_settings && destination_.get() == NULL) { | 132 if (ask_user_for_settings) { |
| 138 BrowserThread::PostTask( | 133 BrowserThread::PostTask( |
| 139 BrowserThread::UI, FROM_HERE, | 134 BrowserThread::UI, FROM_HERE, |
| 140 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 135 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
| 141 base::Bind(&PrintJobWorker::GetSettingsWithUI, | 136 base::Bind(&PrintJobWorker::GetSettingsWithUI, |
| 142 base::Unretained(this), | 137 base::Unretained(this), |
| 143 document_page_count, | 138 document_page_count, |
| 144 has_selection))); | 139 has_selection))); |
| 145 } else { | 140 } else { |
| 146 BrowserThread::PostTask( | 141 BrowserThread::PostTask( |
| 147 BrowserThread::UI, FROM_HERE, | 142 BrowserThread::UI, FROM_HERE, |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 // Find first page to print. | 265 // Find first page to print. |
| 271 int page_count = document_->page_count(); | 266 int page_count = document_->page_count(); |
| 272 if (!page_count) { | 267 if (!page_count) { |
| 273 // We still don't know how many pages the document contains. We can't | 268 // We still don't know how many pages the document contains. We can't |
| 274 // start to print the document yet since the header/footer may refer to | 269 // start to print the document yet since the header/footer may refer to |
| 275 // the document's page count. | 270 // the document's page count. |
| 276 return; | 271 return; |
| 277 } | 272 } |
| 278 // We have enough information to initialize page_number_. | 273 // We have enough information to initialize page_number_. |
| 279 page_number_.Init(document_->settings(), page_count); | 274 page_number_.Init(document_->settings(), page_count); |
| 280 if (destination_.get() != NULL) | |
| 281 destination_->SetPageCount(page_count); | |
| 282 } | 275 } |
| 283 DCHECK_NE(page_number_, PageNumber::npos()); | 276 DCHECK_NE(page_number_, PageNumber::npos()); |
| 284 | 277 |
| 285 while (true) { | 278 while (true) { |
| 286 // Is the page available? | 279 // Is the page available? |
| 287 scoped_refptr<PrintedPage> page = document_->GetPage(page_number_.ToInt()); | 280 scoped_refptr<PrintedPage> page = document_->GetPage(page_number_.ToInt()); |
| 288 if (!page.get()) { | 281 if (!page.get()) { |
| 289 // We need to wait for the page to be available. | 282 // We need to wait for the page to be available. |
| 290 base::MessageLoop::current()->PostDelayedTask( | 283 base::MessageLoop::current()->PostDelayedTask( |
| 291 FROM_HERE, | 284 FROM_HERE, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 JobEventDetails::NEW_PAGE, | 361 JobEventDetails::NEW_PAGE, |
| 369 document_, | 362 document_, |
| 370 make_scoped_refptr(page))); | 363 make_scoped_refptr(page))); |
| 371 | 364 |
| 372 // Preprocess. | 365 // Preprocess. |
| 373 if (printing_context_->NewPage() != PrintingContext::OK) { | 366 if (printing_context_->NewPage() != PrintingContext::OK) { |
| 374 OnFailure(); | 367 OnFailure(); |
| 375 return; | 368 return; |
| 376 } | 369 } |
| 377 | 370 |
| 378 if (destination_.get() != NULL) { | |
| 379 std::vector<uint8> metabytes(page->metafile()->GetDataSize()); | |
| 380 bool success = page->metafile()->GetData( | |
| 381 reinterpret_cast<void*>(&metabytes[0]), metabytes.size()); | |
| 382 DCHECK(success) << "Failed to get metafile data."; | |
| 383 destination_->SetPageContent( | |
| 384 page->page_number(), | |
| 385 reinterpret_cast<void*>(&metabytes[0]), | |
| 386 metabytes.size()); | |
| 387 return; | |
| 388 } | |
| 389 | |
| 390 // Actual printing. | 371 // Actual printing. |
| 391 #if defined(OS_WIN) || defined(OS_MACOSX) | 372 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 392 document_->RenderPrintedPage(*page, printing_context_->context()); | 373 document_->RenderPrintedPage(*page, printing_context_->context()); |
| 393 #elif defined(OS_POSIX) | 374 #elif defined(OS_POSIX) |
| 394 document_->RenderPrintedPage(*page, printing_context_.get()); | 375 document_->RenderPrintedPage(*page, printing_context_.get()); |
| 395 #endif | 376 #endif |
| 396 | 377 |
| 397 // Postprocess. | 378 // Postprocess. |
| 398 if (printing_context_->PageDone() != PrintingContext::OK) { | 379 if (printing_context_->PageDone() != PrintingContext::OK) { |
| 399 OnFailure(); | 380 OnFailure(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 422 document_, | 403 document_, |
| 423 scoped_refptr<PrintedPage>())); | 404 scoped_refptr<PrintedPage>())); |
| 424 Cancel(); | 405 Cancel(); |
| 425 | 406 |
| 426 // Makes sure the variables are reinitialized. | 407 // Makes sure the variables are reinitialized. |
| 427 document_ = NULL; | 408 document_ = NULL; |
| 428 page_number_ = PageNumber::npos(); | 409 page_number_ = PageNumber::npos(); |
| 429 } | 410 } |
| 430 | 411 |
| 431 } // namespace printing | 412 } // namespace printing |
| OLD | NEW |