| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_preview_message_handler.h" | 5 #include "chrome/browser/printing/print_preview_message_handler.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/shared_memory.h" | 12 #include "base/shared_memory.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/printing/print_job_manager.h" | 14 #include "chrome/browser/printing/print_job_manager.h" |
| 15 #include "chrome/browser/printing/print_preview_tab_controller.h" | 15 #include "chrome/browser/printing/print_preview_tab_controller.h" |
| 16 #include "chrome/browser/printing/print_view_manager.h" | 16 #include "chrome/browser/printing/print_view_manager.h" |
| 17 #include "chrome/browser/printing/printer_query.h" | 17 #include "chrome/browser/printing/printer_query.h" |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 19 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 19 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 20 #include "chrome/common/print_messages.h" | 20 #include "chrome/common/print_messages.h" |
| 21 #include "content/browser/renderer_host/render_view_host.h" | 21 #include "content/browser/renderer_host/render_view_host.h" |
| 22 #include "content/browser/webui/web_ui.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 24 #include "printing/page_size_margins.h" | 25 #include "printing/page_size_margins.h" |
| 25 #include "printing/print_job_constants.h" | 26 #include "printing/print_job_constants.h" |
| 26 | 27 |
| 27 using content::BrowserThread; | 28 using content::BrowserThread; |
| 28 using content::NavigationController; | 29 using content::NavigationController; |
| 29 using content::WebContents; | 30 using content::WebContents; |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // Always need to stop the worker. | 91 // Always need to stop the worker. |
| 91 StopWorker(document_cookie); | 92 StopWorker(document_cookie); |
| 92 | 93 |
| 93 // Inform the print preview tab of the failure. | 94 // Inform the print preview tab of the failure. |
| 94 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 95 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 95 // User might have closed it already. | 96 // User might have closed it already. |
| 96 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) | 97 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 97 return NULL; | 98 return NULL; |
| 98 | 99 |
| 99 return static_cast<PrintPreviewUI*>( | 100 return static_cast<PrintPreviewUI*>( |
| 100 print_preview_tab->web_contents()->GetWebUI()); | 101 print_preview_tab->web_contents()->GetWebUI()->GetController()); |
| 101 } | 102 } |
| 102 | 103 |
| 103 void PrintPreviewMessageHandler::OnRequestPrintPreview( | 104 void PrintPreviewMessageHandler::OnRequestPrintPreview( |
| 104 bool source_is_modifiable) { | 105 bool source_is_modifiable) { |
| 105 PrintPreviewTabController::PrintPreview(tab_contents_wrapper()); | 106 PrintPreviewTabController::PrintPreview(tab_contents_wrapper()); |
| 106 PrintPreviewUI::SetSourceIsModifiable(GetPrintPreviewTab(), | 107 PrintPreviewUI::SetSourceIsModifiable(GetPrintPreviewTab(), |
| 107 source_is_modifiable); | 108 source_is_modifiable); |
| 108 } | 109 } |
| 109 | 110 |
| 110 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount( | 111 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount( |
| 111 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { | 112 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { |
| 112 if (params.page_count <= 0) { | 113 if (params.page_count <= 0) { |
| 113 NOTREACHED(); | 114 NOTREACHED(); |
| 114 return; | 115 return; |
| 115 } | 116 } |
| 116 | 117 |
| 117 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 118 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 118 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) | 119 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 119 return; | 120 return; |
| 120 | 121 |
| 121 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 122 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 122 print_preview_tab->web_contents()->GetWebUI()); | 123 print_preview_tab->web_contents()->GetWebUI()->GetController()); |
| 123 | 124 |
| 124 if (!params.is_modifiable || params.clear_preview_data) | 125 if (!params.is_modifiable || params.clear_preview_data) |
| 125 print_preview_ui->ClearAllPreviewData(); | 126 print_preview_ui->ClearAllPreviewData(); |
| 126 | 127 |
| 127 print_preview_ui->OnDidGetPreviewPageCount(params); | 128 print_preview_ui->OnDidGetPreviewPageCount(params); |
| 128 } | 129 } |
| 129 | 130 |
| 130 void PrintPreviewMessageHandler::OnDidPreviewPage( | 131 void PrintPreviewMessageHandler::OnDidPreviewPage( |
| 131 const PrintHostMsg_DidPreviewPage_Params& params) { | 132 const PrintHostMsg_DidPreviewPage_Params& params) { |
| 132 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 133 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 133 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) | 134 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 134 return; | 135 return; |
| 135 | 136 |
| 136 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 137 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 137 print_preview_tab->web_contents()->GetWebUI()); | 138 print_preview_tab->web_contents()->GetWebUI()->GetController()); |
| 138 int page_number = params.page_number; | 139 int page_number = params.page_number; |
| 139 if (page_number >= FIRST_PAGE_INDEX && params.data_size) { | 140 if (page_number >= FIRST_PAGE_INDEX && params.data_size) { |
| 140 RefCountedBytes* data_bytes = | 141 RefCountedBytes* data_bytes = |
| 141 GetDataFromHandle(params.metafile_data_handle, params.data_size); | 142 GetDataFromHandle(params.metafile_data_handle, params.data_size); |
| 142 DCHECK(data_bytes); | 143 DCHECK(data_bytes); |
| 143 | 144 |
| 144 print_preview_ui->SetPrintPreviewDataForIndex(page_number, data_bytes); | 145 print_preview_ui->SetPrintPreviewDataForIndex(page_number, data_bytes); |
| 145 print_preview_ui->OnDidPreviewPage(page_number, params.preview_request_id); | 146 print_preview_ui->OnDidPreviewPage(page_number, params.preview_request_id); |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 149 void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( | 150 void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( |
| 150 const PrintHostMsg_DidPreviewDocument_Params& params) { | 151 const PrintHostMsg_DidPreviewDocument_Params& params) { |
| 151 // Always try to stop the worker. | 152 // Always try to stop the worker. |
| 152 StopWorker(params.document_cookie); | 153 StopWorker(params.document_cookie); |
| 153 | 154 |
| 154 if (params.expected_pages_count <= 0) { | 155 if (params.expected_pages_count <= 0) { |
| 155 NOTREACHED(); | 156 NOTREACHED(); |
| 156 return; | 157 return; |
| 157 } | 158 } |
| 158 | 159 |
| 159 // Get the print preview tab. | 160 // Get the print preview tab. |
| 160 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 161 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 161 // User might have closed it already. | 162 // User might have closed it already. |
| 162 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) | 163 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 163 return; | 164 return; |
| 164 | 165 |
| 165 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 166 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 166 print_preview_tab->web_contents()->GetWebUI()); | 167 print_preview_tab->web_contents()->GetWebUI()->GetController()); |
| 167 | 168 |
| 168 if (params.reuse_existing_data) { | 169 if (params.reuse_existing_data) { |
| 169 // Need to match normal rendering where we are expected to send this. | 170 // Need to match normal rendering where we are expected to send this. |
| 170 PrintHostMsg_DidGetPreviewPageCount_Params temp_params; | 171 PrintHostMsg_DidGetPreviewPageCount_Params temp_params; |
| 171 temp_params.page_count = params.expected_pages_count; | 172 temp_params.page_count = params.expected_pages_count; |
| 172 temp_params.document_cookie = params.document_cookie; | 173 temp_params.document_cookie = params.document_cookie; |
| 173 temp_params.is_modifiable = params.modifiable; | 174 temp_params.is_modifiable = params.modifiable; |
| 174 temp_params.preview_request_id = params.preview_request_id; | 175 temp_params.preview_request_id = params.preview_request_id; |
| 175 print_preview_ui->OnDidGetPreviewPageCount(temp_params); | 176 print_preview_ui->OnDidGetPreviewPageCount(temp_params); |
| 176 print_preview_ui->OnReusePreviewData(params.preview_request_id); | 177 print_preview_ui->OnReusePreviewData(params.preview_request_id); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 199 } | 200 } |
| 200 | 201 |
| 201 void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout( | 202 void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout( |
| 202 const PageSizeMargins& page_layout_in_points, | 203 const PageSizeMargins& page_layout_in_points, |
| 203 bool has_custom_page_size_style) { | 204 bool has_custom_page_size_style) { |
| 204 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 205 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 205 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) | 206 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 206 return; | 207 return; |
| 207 | 208 |
| 208 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 209 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 209 print_preview_tab->web_contents()->GetWebUI()); | 210 print_preview_tab->web_contents()->GetWebUI()->GetController()); |
| 210 print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points, | 211 print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points, |
| 211 has_custom_page_size_style); | 212 has_custom_page_size_style); |
| 212 } | 213 } |
| 213 | 214 |
| 214 void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) { | 215 void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) { |
| 215 // Always need to stop the worker. | 216 // Always need to stop the worker. |
| 216 StopWorker(document_cookie); | 217 StopWorker(document_cookie); |
| 217 } | 218 } |
| 218 | 219 |
| 219 void PrintPreviewMessageHandler::OnInvalidPrinterSettings(int document_cookie) { | 220 void PrintPreviewMessageHandler::OnInvalidPrinterSettings(int document_cookie) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 return; | 261 return; |
| 261 } | 262 } |
| 262 // If |tab| is navigating and it has a print preview tab, notify |tab| to | 263 // If |tab| is navigating and it has a print preview tab, notify |tab| to |
| 263 // consider print preview done so it unfreezes the renderer in the case of | 264 // consider print preview done so it unfreezes the renderer in the case of |
| 264 // window.print(). | 265 // window.print(). |
| 265 if (preview_tab) | 266 if (preview_tab) |
| 266 tab->print_view_manager()->PrintPreviewDone(); | 267 tab->print_view_manager()->PrintPreviewDone(); |
| 267 } | 268 } |
| 268 | 269 |
| 269 } // namespace printing | 270 } // namespace printing |
| OLD | NEW |