• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/printing/print_preview_message_handler.h"
6 
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/printing/print_job_manager.h"
9 #include "chrome/browser/printing/print_preview_tab_controller.h"
10 #include "chrome/browser/printing/print_view_manager.h"
11 #include "chrome/browser/printing/printer_query.h"
12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
13 #include "chrome/browser/ui/webui/print_preview_handler.h"
14 #include "chrome/browser/ui/webui/print_preview_ui.h"
15 #include "chrome/browser/ui/webui/print_preview_ui_html_source.h"
16 #include "chrome/common/content_restriction.h"
17 #include "chrome/common/print_messages.h"
18 #include "content/browser/browser_thread.h"
19 #include "content/browser/renderer_host/render_view_host.h"
20 #include "content/browser/tab_contents/tab_contents.h"
21 
22 namespace printing {
23 
PrintPreviewMessageHandler(TabContents * tab_contents)24 PrintPreviewMessageHandler::PrintPreviewMessageHandler(
25     TabContents* tab_contents)
26     : TabContentsObserver(tab_contents) {
27   DCHECK(tab_contents);
28 }
29 
~PrintPreviewMessageHandler()30 PrintPreviewMessageHandler::~PrintPreviewMessageHandler() {
31 }
32 
GetPrintPreviewTab()33 TabContents* PrintPreviewMessageHandler::GetPrintPreviewTab() {
34   // Get/Create preview tab for initiator tab.
35   printing::PrintPreviewTabController* tab_controller =
36       printing::PrintPreviewTabController::GetInstance();
37   if (!tab_controller)
38     return NULL;
39   return tab_controller->GetPrintPreviewForTab(tab_contents());
40 }
41 
OnPagesReadyForPreview(const PrintHostMsg_DidPreviewDocument_Params & params)42 void PrintPreviewMessageHandler::OnPagesReadyForPreview(
43     const PrintHostMsg_DidPreviewDocument_Params& params) {
44   // Always need to stop the worker and send PrintMsg_PrintingDone.
45   PrintJobManager* print_job_manager = g_browser_process->print_job_manager();
46   scoped_refptr<printing::PrinterQuery> printer_query;
47   print_job_manager->PopPrinterQuery(params.document_cookie, &printer_query);
48   if (printer_query.get()) {
49     BrowserThread::PostTask(
50         BrowserThread::IO, FROM_HERE,
51         NewRunnableMethod(printer_query.get(),
52                           &printing::PrinterQuery::StopWorker));
53   }
54 
55   RenderViewHost* rvh = tab_contents()->render_view_host();
56   rvh->Send(new PrintMsg_PrintingDone(rvh->routing_id(),
57                                      params.document_cookie,
58                                      true));
59 
60   // Get the print preview tab.
61   TabContents* print_preview_tab = GetPrintPreviewTab();
62   // User might have closed it already.
63   if (!print_preview_tab)
64     return;
65 
66   base::SharedMemory* shared_buf =
67       new base::SharedMemory(params.metafile_data_handle, true);
68   if (!shared_buf->Map(params.data_size)) {
69     NOTREACHED();
70     delete shared_buf;
71     return;
72   }
73 
74   TabContentsWrapper* wrapper =
75       TabContentsWrapper::GetCurrentWrapperForContents(print_preview_tab);
76   wrapper->print_view_manager()->OverrideTitle(tab_contents());
77 
78   PrintPreviewUI* print_preview_ui =
79       static_cast<PrintPreviewUI*>(print_preview_tab->web_ui());
80   PrintPreviewUIHTMLSource* html_source = print_preview_ui->html_source();
81   html_source->SetPrintPreviewData(
82       std::make_pair(shared_buf, params.data_size));
83   print_preview_ui->PreviewDataIsAvailable(
84       params.expected_pages_count,
85       wrapper->print_view_manager()->RenderSourceName());
86 }
87 
OnPrintPreviewNodeUnderContextMenu()88 void PrintPreviewMessageHandler::OnPrintPreviewNodeUnderContextMenu() {
89   PrintPreviewTabController::PrintPreview(tab_contents());
90 }
91 
OnScriptInitiatedPrintPreview()92 void PrintPreviewMessageHandler::OnScriptInitiatedPrintPreview() {
93   PrintPreviewTabController::PrintPreview(tab_contents());
94 }
95 
OnMessageReceived(const IPC::Message & message)96 bool PrintPreviewMessageHandler::OnMessageReceived(
97     const IPC::Message& message) {
98   bool handled = true;
99   IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message)
100     IPC_MESSAGE_HANDLER(PrintHostMsg_PagesReadyForPreview,
101                         OnPagesReadyForPreview)
102     IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewNodeUnderContextMenu,
103                         OnPrintPreviewNodeUnderContextMenu)
104     IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptInitiatedPrintPreview,
105                         OnScriptInitiatedPrintPreview)
106     IPC_MESSAGE_UNHANDLED(handled = false)
107   IPC_END_MESSAGE_MAP()
108   return handled;
109 }
110 
DidStartLoading()111 void PrintPreviewMessageHandler::DidStartLoading() {
112   if (tab_contents()->delegate() &&
113       printing::PrintPreviewTabController::IsPrintPreviewTab(tab_contents())) {
114     tab_contents()->SetContentRestrictions(CONTENT_RESTRICTION_PRINT);
115   }
116 }
117 
118 }  // namespace printing
119