| 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/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/timer.h" | 12 #include "base/timer.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/printing/print_error_dialog.h" | 15 #include "chrome/browser/printing/print_error_dialog.h" |
| 16 #include "chrome/browser/printing/print_job.h" | 16 #include "chrome/browser/printing/print_job.h" |
| 17 #include "chrome/browser/printing/print_job_manager.h" | 17 #include "chrome/browser/printing/print_job_manager.h" |
| 18 #include "chrome/browser/printing/print_preview_tab_controller.h" | 18 #include "chrome/browser/printing/print_preview_tab_controller.h" |
| 19 #include "chrome/browser/printing/print_view_manager_observer.h" | 19 #include "chrome/browser/printing/print_view_manager_observer.h" |
| 20 #include "chrome/browser/printing/printer_query.h" | 20 #include "chrome/browser/printing/printer_query.h" |
| 21 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 22 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 22 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 23 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 23 #include "chrome/common/chrome_notification_types.h" | 24 #include "chrome/common/chrome_notification_types.h" |
| 25 #include "chrome/common/pref_names.h" |
| 24 #include "chrome/common/print_messages.h" | 26 #include "chrome/common/print_messages.h" |
| 25 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/notification_details.h" | 28 #include "content/public/browser/notification_details.h" |
| 27 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
| 28 #include "content/public/browser/notification_source.h" | 30 #include "content/public/browser/notification_source.h" |
| 29 #include "content/public/browser/render_view_host.h" | 31 #include "content/public/browser/render_view_host.h" |
| 30 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/browser/web_contents_view.h" | 33 #include "content/public/browser/web_contents_view.h" |
| 32 #include "grit/generated_resources.h" | 34 #include "grit/generated_resources.h" |
| 33 #include "printing/metafile.h" | 35 #include "printing/metafile.h" |
| 34 #include "printing/metafile_impl.h" | 36 #include "printing/metafile_impl.h" |
| 35 #include "printing/print_destination_interface.h" | 37 #include "printing/print_destination_interface.h" |
| 36 #include "printing/printed_document.h" | 38 #include "printing/printed_document.h" |
| 37 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
| 38 | 40 |
| 39 using base::TimeDelta; | 41 using base::TimeDelta; |
| 40 using content::BrowserThread; | 42 using content::BrowserThread; |
| 41 using content::RenderViewHost; | |
| 42 | 43 |
| 43 namespace { | 44 namespace { |
| 44 | 45 |
| 45 // Keeps track of pending scripted print preview closures. | 46 // Keeps track of pending scripted print preview closures. |
| 46 // No locking, only access on the UI thread. | 47 // No locking, only access on the UI thread. |
| 47 typedef std::map<content::RenderProcessHost*, base::Closure> | 48 typedef std::map<content::RenderProcessHost*, base::Closure> |
| 48 ScriptedPrintPreviewClosureMap; | 49 ScriptedPrintPreviewClosureMap; |
| 49 static base::LazyInstance<ScriptedPrintPreviewClosureMap> | 50 static base::LazyInstance<ScriptedPrintPreviewClosureMap> |
| 50 g_scripted_print_preview_closure_map = LAZY_INSTANCE_INITIALIZER; | 51 g_scripted_print_preview_closure_map = LAZY_INSTANCE_INITIALIZER; |
| 51 | 52 |
| 52 } // namespace | 53 } // namespace |
| 53 | 54 |
| 54 namespace printing { | 55 namespace printing { |
| 55 | 56 |
| 56 PrintViewManager::PrintViewManager(TabContents* tab) | 57 PrintViewManager::PrintViewManager(TabContents* tab) |
| 57 : content::WebContentsObserver(tab->web_contents()), | 58 : content::WebContentsObserver(tab->web_contents()), |
| 58 tab_(tab), | 59 tab_(tab), |
| 59 number_pages_(0), | 60 number_pages_(0), |
| 60 printing_succeeded_(false), | 61 printing_succeeded_(false), |
| 61 inside_inner_message_loop_(false), | 62 inside_inner_message_loop_(false), |
| 62 observer_(NULL), | 63 observer_(NULL), |
| 63 cookie_(0), | 64 cookie_(0), |
| 64 print_preview_state_(NOT_PREVIEWING), | 65 print_preview_state_(NOT_PREVIEWING), |
| 65 scripted_print_preview_rph_(NULL) { | 66 scripted_print_preview_rph_(NULL), |
| 67 tab_content_blocked_(false) { |
| 66 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 68 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 67 expecting_first_page_ = true; | 69 expecting_first_page_ = true; |
| 68 #endif | 70 #endif |
| 69 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, | 71 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, |
| 70 content::Source<TabContents>(tab)); | 72 content::Source<TabContents>(tab)); |
| 73 printing_enabled_.Init(prefs::kPrintingEnabled, |
| 74 tab->profile()->GetPrefs(), |
| 75 this); |
| 71 } | 76 } |
| 72 | 77 |
| 73 PrintViewManager::~PrintViewManager() { | 78 PrintViewManager::~PrintViewManager() { |
| 74 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); | 79 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); |
| 75 ReleasePrinterQuery(); | 80 ReleasePrinterQuery(); |
| 76 DisconnectFromCurrentPrintJob(); | 81 DisconnectFromCurrentPrintJob(); |
| 77 } | 82 } |
| 78 | 83 |
| 79 bool PrintViewManager::PrintNow() { | 84 bool PrintViewManager::PrintNow() { |
| 80 return PrintNowInternal(new PrintMsg_PrintPages(routing_id())); | 85 return PrintNowInternal(new PrintMsg_PrintPages(routing_id())); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 ScriptedPrintPreviewClosureMap::iterator it = | 142 ScriptedPrintPreviewClosureMap::iterator it = |
| 138 map.find(scripted_print_preview_rph_); | 143 map.find(scripted_print_preview_rph_); |
| 139 CHECK(it != map.end()); | 144 CHECK(it != map.end()); |
| 140 it->second.Run(); | 145 it->second.Run(); |
| 141 map.erase(scripted_print_preview_rph_); | 146 map.erase(scripted_print_preview_rph_); |
| 142 scripted_print_preview_rph_ = NULL; | 147 scripted_print_preview_rph_ = NULL; |
| 143 } | 148 } |
| 144 print_preview_state_ = NOT_PREVIEWING; | 149 print_preview_state_ = NOT_PREVIEWING; |
| 145 } | 150 } |
| 146 | 151 |
| 147 void PrintViewManager::SetScriptedPrintingBlocked(bool blocked) { | 152 void PrintViewManager::UpdateScriptedPrintingBlocked() { |
| 148 Send(new PrintMsg_SetScriptedPrintingBlocked(routing_id(), blocked)); | 153 Send(new PrintMsg_SetScriptedPrintingBlocked( |
| 154 routing_id(), |
| 155 !printing_enabled_.GetValue() || tab_content_blocked_)); |
| 149 } | 156 } |
| 150 | 157 |
| 151 void PrintViewManager::set_observer(PrintViewManagerObserver* observer) { | 158 void PrintViewManager::set_observer(PrintViewManagerObserver* observer) { |
| 152 DCHECK(!observer || !observer_); | 159 DCHECK(!observer || !observer_); |
| 153 observer_ = observer; | 160 observer_ = observer; |
| 154 } | 161 } |
| 155 | 162 |
| 156 void PrintViewManager::StopNavigation() { | 163 void PrintViewManager::StopNavigation() { |
| 157 // Cancel the current job, wait for the worker to finish. | 164 // Cancel the current job, wait for the worker to finish. |
| 158 TerminatePrintJob(true); | 165 TerminatePrintJob(true); |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 PrintPreviewUI::SetSourceIsModifiable( | 320 PrintPreviewUI::SetSourceIsModifiable( |
| 314 tab_controller->GetPrintPreviewForTab(tab_), | 321 tab_controller->GetPrintPreviewForTab(tab_), |
| 315 source_is_modifiable); | 322 source_is_modifiable); |
| 316 } | 323 } |
| 317 | 324 |
| 318 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { | 325 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { |
| 319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 320 Send(reply_msg); | 327 Send(reply_msg); |
| 321 } | 328 } |
| 322 | 329 |
| 330 void PrintViewManager::DidStartLoading( |
| 331 content::RenderViewHost* render_view_host) { |
| 332 UpdateScriptedPrintingBlocked(); |
| 333 } |
| 334 |
| 323 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { | 335 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { |
| 324 bool handled = true; | 336 bool handled = true; |
| 325 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) | 337 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) |
| 326 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount, | 338 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount, |
| 327 OnDidGetPrintedPagesCount) | 339 OnDidGetPrintedPagesCount) |
| 328 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie, | 340 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie, |
| 329 OnDidGetDocumentCookie) | 341 OnDidGetDocumentCookie) |
| 330 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) | 342 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) |
| 331 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) | 343 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) |
| 332 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed) | 344 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed) |
| 333 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrintPreview, | 345 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrintPreview, |
| 334 OnScriptedPrintPreview) | 346 OnScriptedPrintPreview) |
| 335 IPC_MESSAGE_UNHANDLED(handled = false) | 347 IPC_MESSAGE_UNHANDLED(handled = false) |
| 336 IPC_END_MESSAGE_MAP() | 348 IPC_END_MESSAGE_MAP() |
| 337 return handled; | 349 return handled; |
| 338 } | 350 } |
| 339 | 351 |
| 340 void PrintViewManager::Observe(int type, | 352 void PrintViewManager::Observe(int type, |
| 341 const content::NotificationSource& source, | 353 const content::NotificationSource& source, |
| 342 const content::NotificationDetails& details) { | 354 const content::NotificationDetails& details) { |
| 343 switch (type) { | 355 switch (type) { |
| 344 case chrome::NOTIFICATION_PRINT_JOB_EVENT: { | 356 case chrome::NOTIFICATION_PRINT_JOB_EVENT: { |
| 345 OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr()); | 357 OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr()); |
| 346 break; | 358 break; |
| 347 } | 359 } |
| 360 case chrome::NOTIFICATION_PREF_CHANGED: { |
| 361 UpdateScriptedPrintingBlocked(); |
| 362 break; |
| 363 } |
| 348 case chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED: { | 364 case chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED: { |
| 349 SetScriptedPrintingBlocked(*content::Details<const bool>(details).ptr()); | 365 tab_content_blocked_ = *content::Details<const bool>(details).ptr(); |
| 366 UpdateScriptedPrintingBlocked(); |
| 350 break; | 367 break; |
| 351 } | 368 } |
| 352 default: { | 369 default: { |
| 353 NOTREACHED(); | 370 NOTREACHED(); |
| 354 break; | 371 break; |
| 355 } | 372 } |
| 356 } | 373 } |
| 357 } | 374 } |
| 358 | 375 |
| 359 void PrintViewManager::OnNotifyPrintJobEvent( | 376 void PrintViewManager::OnNotifyPrintJobEvent( |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 scoped_refptr<printing::PrinterQuery> printer_query; | 651 scoped_refptr<printing::PrinterQuery> printer_query; |
| 635 print_job_manager->PopPrinterQuery(cookie, &printer_query); | 652 print_job_manager->PopPrinterQuery(cookie, &printer_query); |
| 636 if (!printer_query.get()) | 653 if (!printer_query.get()) |
| 637 return; | 654 return; |
| 638 BrowserThread::PostTask( | 655 BrowserThread::PostTask( |
| 639 BrowserThread::IO, FROM_HERE, | 656 BrowserThread::IO, FROM_HERE, |
| 640 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); | 657 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); |
| 641 } | 658 } |
| 642 | 659 |
| 643 } // namespace printing | 660 } // namespace printing |
| OLD | NEW |