| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 // on the I/O thread. | 62 // on the I/O thread. |
| 63 DCHECK_EQ(owner_->message_loop(), MessageLoop::current()); | 63 DCHECK_EQ(owner_->message_loop(), MessageLoop::current()); |
| 64 Stop(); | 64 Stop(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { | 67 void PrintJobWorker::SetNewOwner(PrintJobWorkerOwner* new_owner) { |
| 68 DCHECK(page_number_ == PageNumber::npos()); | 68 DCHECK(page_number_ == PageNumber::npos()); |
| 69 owner_ = new_owner; | 69 owner_ = new_owner; |
| 70 } | 70 } |
| 71 | 71 |
| 72 void PrintJobWorker::SetPrintDestination( |
| 73 PrintDestinationInterface* destination) { |
| 74 destination_ = destination; |
| 75 } |
| 76 |
| 72 void PrintJobWorker::GetSettings(bool ask_user_for_settings, | 77 void PrintJobWorker::GetSettings(bool ask_user_for_settings, |
| 73 gfx::NativeView parent_view, | 78 gfx::NativeView parent_view, |
| 74 int document_page_count, | 79 int document_page_count, |
| 75 bool has_selection, | 80 bool has_selection, |
| 76 MarginType margin_type) { | 81 MarginType margin_type) { |
| 77 DCHECK_EQ(message_loop(), MessageLoop::current()); | 82 DCHECK_EQ(message_loop(), MessageLoop::current()); |
| 78 DCHECK_EQ(page_number_, PageNumber::npos()); | 83 DCHECK_EQ(page_number_, PageNumber::npos()); |
| 79 | 84 |
| 80 // Recursive task processing is needed for the dialog in case it needs to be | 85 // Recursive task processing is needed for the dialog in case it needs to be |
| 81 // destroyed by a task. | 86 // destroyed by a task. |
| 82 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed | 87 // TODO(thestig): This code is wrong. SetNestableTasksAllowed(true) is needed |
| 83 // on the thread where the PrintDlgEx is called, and definitely both calls | 88 // on the thread where the PrintDlgEx is called, and definitely both calls |
| 84 // should happen on the same thread. See http://crbug.com/73466 | 89 // should happen on the same thread. See http://crbug.com/73466 |
| 85 // MessageLoop::current()->SetNestableTasksAllowed(true); | 90 // MessageLoop::current()->SetNestableTasksAllowed(true); |
| 86 printing_context_->set_margin_type(margin_type); | 91 printing_context_->set_margin_type(margin_type); |
| 87 | 92 |
| 88 if (ask_user_for_settings) { | 93 // When we delegate to a destination, we don't ask the user for settings. |
| 94 // TODO(mad): Ask the destination for settings. |
| 95 if (ask_user_for_settings && destination_.get() == NULL) { |
| 89 BrowserThread::PostTask( | 96 BrowserThread::PostTask( |
| 90 BrowserThread::UI, FROM_HERE, | 97 BrowserThread::UI, FROM_HERE, |
| 91 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 98 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
| 92 base::Bind(&PrintJobWorker::GetSettingsWithUI, | 99 base::Bind(&PrintJobWorker::GetSettingsWithUI, |
| 93 base::Unretained(this), parent_view, | 100 base::Unretained(this), parent_view, |
| 94 document_page_count, has_selection))); | 101 document_page_count, has_selection))); |
| 95 } else { | 102 } else { |
| 96 BrowserThread::PostTask( | 103 BrowserThread::PostTask( |
| 97 BrowserThread::UI, FROM_HERE, | 104 BrowserThread::UI, FROM_HERE, |
| 98 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), | 105 base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 // Find first page to print. | 241 // Find first page to print. |
| 235 int page_count = document_->page_count(); | 242 int page_count = document_->page_count(); |
| 236 if (!page_count) { | 243 if (!page_count) { |
| 237 // We still don't know how many pages the document contains. We can't | 244 // We still don't know how many pages the document contains. We can't |
| 238 // start to print the document yet since the header/footer may refer to | 245 // start to print the document yet since the header/footer may refer to |
| 239 // the document's page count. | 246 // the document's page count. |
| 240 return; | 247 return; |
| 241 } | 248 } |
| 242 // We have enough information to initialize page_number_. | 249 // We have enough information to initialize page_number_. |
| 243 page_number_.Init(document_->settings(), page_count); | 250 page_number_.Init(document_->settings(), page_count); |
| 251 if (destination_.get() != NULL) |
| 252 destination_->SetPageCount(page_count); |
| 244 } | 253 } |
| 245 DCHECK_NE(page_number_, PageNumber::npos()); | 254 DCHECK_NE(page_number_, PageNumber::npos()); |
| 246 | 255 |
| 247 while (true) { | 256 while (true) { |
| 248 // Is the page available? | 257 // Is the page available? |
| 249 scoped_refptr<PrintedPage> page; | 258 scoped_refptr<PrintedPage> page; |
| 250 if (!document_->GetPage(page_number_.ToInt(), &page)) { | 259 if (!document_->GetPage(page_number_.ToInt(), &page)) { |
| 251 // We need to wait for the page to be available. | 260 // We need to wait for the page to be available. |
| 252 MessageLoop::current()->PostDelayedTask( | 261 MessageLoop::current()->PostDelayedTask( |
| 253 FROM_HERE, | 262 FROM_HERE, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 FROM_HERE, base::Bind(NotificationCallback, make_scoped_refptr(owner_), | 310 FROM_HERE, base::Bind(NotificationCallback, make_scoped_refptr(owner_), |
| 302 JobEventDetails::NEW_PAGE, document_, | 311 JobEventDetails::NEW_PAGE, document_, |
| 303 make_scoped_refptr(page))); | 312 make_scoped_refptr(page))); |
| 304 | 313 |
| 305 // Preprocess. | 314 // Preprocess. |
| 306 if (printing_context_->NewPage() != PrintingContext::OK) { | 315 if (printing_context_->NewPage() != PrintingContext::OK) { |
| 307 OnFailure(); | 316 OnFailure(); |
| 308 return; | 317 return; |
| 309 } | 318 } |
| 310 | 319 |
| 320 if (destination_.get() != NULL) { |
| 321 std::vector<uint8> metabytes(page->metafile()->GetDataSize()); |
| 322 bool success = page->metafile()->GetData( |
| 323 reinterpret_cast<void*>(&metabytes[0]), metabytes.size()); |
| 324 DCHECK(success) << "Failed to get metafile data."; |
| 325 destination_->SetPageContent( |
| 326 page->page_number(), |
| 327 reinterpret_cast<void*>(&metabytes[0]), |
| 328 metabytes.size()); |
| 329 return; |
| 330 } |
| 331 |
| 311 // Actual printing. | 332 // Actual printing. |
| 312 #if defined(OS_WIN) || defined(OS_MACOSX) | 333 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 313 document_->RenderPrintedPage(*page, printing_context_->context()); | 334 document_->RenderPrintedPage(*page, printing_context_->context()); |
| 314 #elif defined(OS_POSIX) | 335 #elif defined(OS_POSIX) |
| 315 document_->RenderPrintedPage(*page, printing_context_.get()); | 336 document_->RenderPrintedPage(*page, printing_context_.get()); |
| 316 #endif | 337 #endif |
| 317 | 338 |
| 318 // Postprocess. | 339 // Postprocess. |
| 319 if (printing_context_->PageDone() != PrintingContext::OK) { | 340 if (printing_context_->PageDone() != PrintingContext::OK) { |
| 320 OnFailure(); | 341 OnFailure(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 340 JobEventDetails::FAILED, document_, | 361 JobEventDetails::FAILED, document_, |
| 341 scoped_refptr<PrintedPage>())); | 362 scoped_refptr<PrintedPage>())); |
| 342 Cancel(); | 363 Cancel(); |
| 343 | 364 |
| 344 // Makes sure the variables are reinitialized. | 365 // Makes sure the variables are reinitialized. |
| 345 document_ = NULL; | 366 document_ = NULL; |
| 346 page_number_ = PageNumber::npos(); | 367 page_number_ = PageNumber::npos(); |
| 347 } | 368 } |
| 348 | 369 |
| 349 } // namespace printing | 370 } // namespace printing |
| OLD | NEW |