| 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_view_manager.h" | 5 #include "chrome/browser/printing/print_view_manager.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 #include "grit/generated_resources.h" | 38 #include "grit/generated_resources.h" |
| 39 #include "printing/metafile.h" | 39 #include "printing/metafile.h" |
| 40 #include "printing/metafile_impl.h" | 40 #include "printing/metafile_impl.h" |
| 41 #include "printing/print_destination_interface.h" | 41 #include "printing/print_destination_interface.h" |
| 42 #include "printing/printed_document.h" | 42 #include "printing/printed_document.h" |
| 43 #include "ui/base/l10n/l10n_util.h" | 43 #include "ui/base/l10n/l10n_util.h" |
| 44 | 44 |
| 45 using base::TimeDelta; | 45 using base::TimeDelta; |
| 46 using content::BrowserThread; | 46 using content::BrowserThread; |
| 47 | 47 |
| 48 int printing::PrintViewManager::kUserDataKey; |
| 49 |
| 48 namespace { | 50 namespace { |
| 49 | 51 |
| 50 // Keeps track of pending scripted print preview closures. | 52 // Keeps track of pending scripted print preview closures. |
| 51 // No locking, only access on the UI thread. | 53 // No locking, only access on the UI thread. |
| 52 typedef std::map<content::RenderProcessHost*, base::Closure> | 54 typedef std::map<content::RenderProcessHost*, base::Closure> |
| 53 ScriptedPrintPreviewClosureMap; | 55 ScriptedPrintPreviewClosureMap; |
| 54 static base::LazyInstance<ScriptedPrintPreviewClosureMap> | 56 static base::LazyInstance<ScriptedPrintPreviewClosureMap> |
| 55 g_scripted_print_preview_closure_map = LAZY_INSTANCE_INITIALIZER; | 57 g_scripted_print_preview_closure_map = LAZY_INSTANCE_INITIALIZER; |
| 56 | 58 |
| 57 // Limits memory usage by raster to 64 MiB. | 59 // Limits memory usage by raster to 64 MiB. |
| 58 const int kMaxRasterSizeInPixels = 16*1024*1024; | 60 const int kMaxRasterSizeInPixels = 16*1024*1024; |
| 59 | 61 |
| 60 } // namespace | 62 } // namespace |
| 61 | 63 |
| 62 namespace printing { | 64 namespace printing { |
| 63 | 65 |
| 64 PrintViewManager::PrintViewManager(TabContents* tab) | 66 PrintViewManager::PrintViewManager(content::WebContents* web_contents) |
| 65 : content::WebContentsObserver(tab->web_contents()), | 67 : content::WebContentsObserver(web_contents), |
| 66 tab_(tab), | |
| 67 number_pages_(0), | 68 number_pages_(0), |
| 68 printing_succeeded_(false), | 69 printing_succeeded_(false), |
| 69 inside_inner_message_loop_(false), | 70 inside_inner_message_loop_(false), |
| 70 observer_(NULL), | 71 observer_(NULL), |
| 71 cookie_(0), | 72 cookie_(0), |
| 72 print_preview_state_(NOT_PREVIEWING), | 73 print_preview_state_(NOT_PREVIEWING), |
| 73 scripted_print_preview_rph_(NULL), | 74 scripted_print_preview_rph_(NULL), |
| 74 tab_content_blocked_(false) { | 75 tab_content_blocked_(false) { |
| 75 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 76 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 76 expecting_first_page_ = true; | 77 expecting_first_page_ = true; |
| 77 #endif | 78 #endif |
| 78 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, | 79 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, |
| 79 content::Source<TabContents>(tab)); | 80 content::Source<content::WebContents>(web_contents)); |
| 81 Profile* profile = |
| 82 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 80 printing_enabled_.Init(prefs::kPrintingEnabled, | 83 printing_enabled_.Init(prefs::kPrintingEnabled, |
| 81 tab->profile()->GetPrefs(), | 84 profile->GetPrefs(), |
| 82 this); | 85 this); |
| 83 } | 86 } |
| 84 | 87 |
| 85 PrintViewManager::~PrintViewManager() { | 88 PrintViewManager::~PrintViewManager() { |
| 86 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); | 89 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); |
| 87 ReleasePrinterQuery(); | 90 ReleasePrinterQuery(); |
| 88 DisconnectFromCurrentPrintJob(); | 91 DisconnectFromCurrentPrintJob(); |
| 89 } | 92 } |
| 90 | 93 |
| 91 bool PrintViewManager::PrintNow() { | 94 bool PrintViewManager::PrintNow() { |
| 92 return PrintNowInternal(new PrintMsg_PrintPages(routing_id())); | 95 return PrintNowInternal(new PrintMsg_PrintPages(routing_id())); |
| 93 } | 96 } |
| 94 | 97 |
| 95 bool PrintViewManager::PrintForSystemDialogNow() { | 98 bool PrintViewManager::PrintForSystemDialogNow() { |
| 96 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id())); | 99 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id())); |
| 97 } | 100 } |
| 98 | 101 |
| 99 bool PrintViewManager::AdvancedPrintNow() { | 102 bool PrintViewManager::AdvancedPrintNow() { |
| 100 PrintPreviewTabController* tab_controller = | 103 PrintPreviewTabController* tab_controller = |
| 101 PrintPreviewTabController::GetInstance(); | 104 PrintPreviewTabController::GetInstance(); |
| 102 if (!tab_controller) | 105 if (!tab_controller) |
| 103 return false; | 106 return false; |
| 104 TabContents* print_preview_tab = tab_controller->GetPrintPreviewForTab(tab_); | 107 TabContents* print_preview_tab = tab_controller->GetPrintPreviewForTab( |
| 108 TabContents::FromWebContents(web_contents())); |
| 105 if (print_preview_tab) { | 109 if (print_preview_tab) { |
| 106 // Preview tab exist for current tab or current tab is preview tab. | 110 // Preview tab exist for current tab or current tab is preview tab. |
| 107 if (!print_preview_tab->web_contents()->GetWebUI()) | 111 if (!print_preview_tab->web_contents()->GetWebUI()) |
| 108 return false; | 112 return false; |
| 109 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 113 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 110 print_preview_tab->web_contents()->GetWebUI()->GetController()); | 114 print_preview_tab->web_contents()->GetWebUI()->GetController()); |
| 111 print_preview_ui->OnShowSystemDialog(); | 115 print_preview_ui->OnShowSystemDialog(); |
| 112 return true; | 116 return true; |
| 113 } else { | 117 } else { |
| 114 return PrintNow(); | 118 return PrintNow(); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 ShouldQuitFromInnerMessageLoop(); | 291 ShouldQuitFromInnerMessageLoop(); |
| 288 } | 292 } |
| 289 | 293 |
| 290 void PrintViewManager::OnPrintingFailed(int cookie) { | 294 void PrintViewManager::OnPrintingFailed(int cookie) { |
| 291 if (cookie != cookie_) { | 295 if (cookie != cookie_) { |
| 292 NOTREACHED(); | 296 NOTREACHED(); |
| 293 return; | 297 return; |
| 294 } | 298 } |
| 295 | 299 |
| 296 chrome::ShowPrintErrorDialog( | 300 chrome::ShowPrintErrorDialog( |
| 297 tab_->web_contents()->GetView()->GetTopLevelNativeWindow()); | 301 web_contents()->GetView()->GetTopLevelNativeWindow()); |
| 298 | 302 |
| 299 ReleasePrinterQuery(); | 303 ReleasePrinterQuery(); |
| 300 | 304 |
| 301 content::NotificationService::current()->Notify( | 305 content::NotificationService::current()->Notify( |
| 302 chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 306 chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 303 content::Source<TabContents>(tab_), | 307 content::Source<TabContents>( |
| 308 TabContents::FromWebContents(web_contents())), |
| 304 content::NotificationService::NoDetails()); | 309 content::NotificationService::NoDetails()); |
| 305 } | 310 } |
| 306 | 311 |
| 307 void PrintViewManager::OnScriptedPrintPreview(bool source_is_modifiable, | 312 void PrintViewManager::OnScriptedPrintPreview(bool source_is_modifiable, |
| 308 IPC::Message* reply_msg) { | 313 IPC::Message* reply_msg) { |
| 309 BrowserThread::CurrentlyOn(BrowserThread::UI); | 314 BrowserThread::CurrentlyOn(BrowserThread::UI); |
| 310 ScriptedPrintPreviewClosureMap& map = | 315 ScriptedPrintPreviewClosureMap& map = |
| 311 g_scripted_print_preview_closure_map.Get(); | 316 g_scripted_print_preview_closure_map.Get(); |
| 312 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost(); | 317 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost(); |
| 313 | 318 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 333 } | 338 } |
| 334 | 339 |
| 335 print_preview_state_ = SCRIPTED_PREVIEW; | 340 print_preview_state_ = SCRIPTED_PREVIEW; |
| 336 base::Closure callback = | 341 base::Closure callback = |
| 337 base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, | 342 base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, |
| 338 base::Unretained(this), | 343 base::Unretained(this), |
| 339 reply_msg); | 344 reply_msg); |
| 340 map[rph] = callback; | 345 map[rph] = callback; |
| 341 scripted_print_preview_rph_ = rph; | 346 scripted_print_preview_rph_ = rph; |
| 342 | 347 |
| 343 tab_controller->PrintPreview(tab_); | 348 TabContents* tab_contents = TabContents::FromWebContents(web_contents()); |
| 349 tab_controller->PrintPreview(tab_contents); |
| 344 PrintPreviewUI::SetSourceIsModifiable( | 350 PrintPreviewUI::SetSourceIsModifiable( |
| 345 tab_controller->GetPrintPreviewForTab(tab_), | 351 tab_controller->GetPrintPreviewForTab(tab_contents), |
| 346 source_is_modifiable); | 352 source_is_modifiable); |
| 347 } | 353 } |
| 348 | 354 |
| 349 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { | 355 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { |
| 350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 351 Send(reply_msg); | 357 Send(reply_msg); |
| 352 } | 358 } |
| 353 | 359 |
| 354 void PrintViewManager::DidStartLoading( | 360 void PrintViewManager::DidStartLoading( |
| 355 content::RenderViewHost* render_view_host) { | 361 content::RenderViewHost* render_view_host) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 } | 404 } |
| 399 | 405 |
| 400 void PrintViewManager::OnNotifyPrintJobEvent( | 406 void PrintViewManager::OnNotifyPrintJobEvent( |
| 401 const JobEventDetails& event_details) { | 407 const JobEventDetails& event_details) { |
| 402 switch (event_details.type()) { | 408 switch (event_details.type()) { |
| 403 case JobEventDetails::FAILED: { | 409 case JobEventDetails::FAILED: { |
| 404 TerminatePrintJob(true); | 410 TerminatePrintJob(true); |
| 405 | 411 |
| 406 content::NotificationService::current()->Notify( | 412 content::NotificationService::current()->Notify( |
| 407 chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 413 chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 408 content::Source<TabContents>(tab_), | 414 content::Source<TabContents>( |
| 415 TabContents::FromWebContents(web_contents())), |
| 409 content::NotificationService::NoDetails()); | 416 content::NotificationService::NoDetails()); |
| 410 break; | 417 break; |
| 411 } | 418 } |
| 412 case JobEventDetails::USER_INIT_DONE: | 419 case JobEventDetails::USER_INIT_DONE: |
| 413 case JobEventDetails::DEFAULT_INIT_DONE: | 420 case JobEventDetails::DEFAULT_INIT_DONE: |
| 414 case JobEventDetails::USER_INIT_CANCELED: { | 421 case JobEventDetails::USER_INIT_CANCELED: { |
| 415 NOTREACHED(); | 422 NOTREACHED(); |
| 416 break; | 423 break; |
| 417 } | 424 } |
| 418 case JobEventDetails::ALL_PAGES_REQUESTED: { | 425 case JobEventDetails::ALL_PAGES_REQUESTED: { |
| 419 ShouldQuitFromInnerMessageLoop(); | 426 ShouldQuitFromInnerMessageLoop(); |
| 420 break; | 427 break; |
| 421 } | 428 } |
| 422 case JobEventDetails::NEW_DOC: | 429 case JobEventDetails::NEW_DOC: |
| 423 case JobEventDetails::NEW_PAGE: | 430 case JobEventDetails::NEW_PAGE: |
| 424 case JobEventDetails::PAGE_DONE: | 431 case JobEventDetails::PAGE_DONE: |
| 425 case JobEventDetails::DOC_DONE: { | 432 case JobEventDetails::DOC_DONE: { |
| 426 // Don't care about the actual printing process. | 433 // Don't care about the actual printing process. |
| 427 break; | 434 break; |
| 428 } | 435 } |
| 429 case JobEventDetails::JOB_DONE: { | 436 case JobEventDetails::JOB_DONE: { |
| 430 // Printing is done, we don't need it anymore. | 437 // Printing is done, we don't need it anymore. |
| 431 // print_job_->is_job_pending() may still be true, depending on the order | 438 // print_job_->is_job_pending() may still be true, depending on the order |
| 432 // of object registration. | 439 // of object registration. |
| 433 printing_succeeded_ = true; | 440 printing_succeeded_ = true; |
| 434 ReleasePrintJob(); | 441 ReleasePrintJob(); |
| 435 | 442 |
| 436 content::NotificationService::current()->Notify( | 443 content::NotificationService::current()->Notify( |
| 437 chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 444 chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 438 content::Source<TabContents>(tab_), | 445 content::Source<TabContents>( |
| 446 TabContents::FromWebContents(web_contents())), |
| 439 content::NotificationService::NoDetails()); | 447 content::NotificationService::NoDetails()); |
| 440 break; | 448 break; |
| 441 } | 449 } |
| 442 default: { | 450 default: { |
| 443 NOTREACHED(); | 451 NOTREACHED(); |
| 444 break; | 452 break; |
| 445 } | 453 } |
| 446 } | 454 } |
| 447 } | 455 } |
| 448 | 456 |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 scoped_refptr<printing::PrinterQuery> printer_query; | 683 scoped_refptr<printing::PrinterQuery> printer_query; |
| 676 print_job_manager->PopPrinterQuery(cookie, &printer_query); | 684 print_job_manager->PopPrinterQuery(cookie, &printer_query); |
| 677 if (!printer_query.get()) | 685 if (!printer_query.get()) |
| 678 return; | 686 return; |
| 679 BrowserThread::PostTask( | 687 BrowserThread::PostTask( |
| 680 BrowserThread::IO, FROM_HERE, | 688 BrowserThread::IO, FROM_HERE, |
| 681 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); | 689 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); |
| 682 } | 690 } |
| 683 | 691 |
| 684 } // namespace printing | 692 } // namespace printing |
| OLD | NEW |