| 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_ui.h" | 19 #include "chrome/browser/ui/webui/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/tab_contents/tab_contents.h" | |
| 23 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/web_contents.h" |
| 24 #include "printing/page_size_margins.h" | 24 #include "printing/page_size_margins.h" |
| 25 #include "printing/print_job_constants.h" | 25 #include "printing/print_job_constants.h" |
| 26 | 26 |
| 27 using content::BrowserThread; | 27 using content::BrowserThread; |
| 28 using content::NavigationController; | 28 using content::NavigationController; |
| 29 using content::WebContents; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 void StopWorker(int document_cookie) { | 33 void StopWorker(int document_cookie) { |
| 33 if (document_cookie <= 0) | 34 if (document_cookie <= 0) |
| 34 return; | 35 return; |
| 35 | 36 |
| 36 printing::PrintJobManager* print_job_manager = | 37 printing::PrintJobManager* print_job_manager = |
| 37 g_browser_process->print_job_manager(); | 38 g_browser_process->print_job_manager(); |
| 38 scoped_refptr<printing::PrinterQuery> printer_query; | 39 scoped_refptr<printing::PrinterQuery> printer_query; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 57 std::vector<unsigned char> data(data_size); | 58 std::vector<unsigned char> data(data_size); |
| 58 memcpy(&data[0], preview_data, data_size); | 59 memcpy(&data[0], preview_data, data_size); |
| 59 return RefCountedBytes::TakeVector(&data); | 60 return RefCountedBytes::TakeVector(&data); |
| 60 } | 61 } |
| 61 | 62 |
| 62 } // namespace | 63 } // namespace |
| 63 | 64 |
| 64 namespace printing { | 65 namespace printing { |
| 65 | 66 |
| 66 PrintPreviewMessageHandler::PrintPreviewMessageHandler( | 67 PrintPreviewMessageHandler::PrintPreviewMessageHandler( |
| 67 TabContents* tab_contents) | 68 WebContents* web_contents) |
| 68 : content::WebContentsObserver(tab_contents) { | 69 : content::WebContentsObserver(web_contents) { |
| 69 DCHECK(tab_contents); | 70 DCHECK(web_contents); |
| 70 } | 71 } |
| 71 | 72 |
| 72 PrintPreviewMessageHandler::~PrintPreviewMessageHandler() { | 73 PrintPreviewMessageHandler::~PrintPreviewMessageHandler() { |
| 73 } | 74 } |
| 74 | 75 |
| 75 TabContentsWrapper* PrintPreviewMessageHandler::GetPrintPreviewTab() { | 76 TabContentsWrapper* PrintPreviewMessageHandler::GetPrintPreviewTab() { |
| 76 PrintPreviewTabController* tab_controller = | 77 PrintPreviewTabController* tab_controller = |
| 77 PrintPreviewTabController::GetInstance(); | 78 PrintPreviewTabController::GetInstance(); |
| 78 if (!tab_controller) | 79 if (!tab_controller) |
| 79 return NULL; | 80 return NULL; |
| 80 | 81 |
| 81 return tab_controller->GetPrintPreviewForTab(tab_contents_wrapper()); | 82 return tab_controller->GetPrintPreviewForTab(tab_contents_wrapper()); |
| 82 } | 83 } |
| 83 | 84 |
| 84 TabContentsWrapper* PrintPreviewMessageHandler::tab_contents_wrapper() { | 85 TabContentsWrapper* PrintPreviewMessageHandler::tab_contents_wrapper() { |
| 85 return TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); | 86 return TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); |
| 86 } | 87 } |
| 87 | 88 |
| 88 PrintPreviewUI* PrintPreviewMessageHandler::OnFailure(int document_cookie) { | 89 PrintPreviewUI* PrintPreviewMessageHandler::OnFailure(int document_cookie) { |
| 89 // Always need to stop the worker. | 90 // Always need to stop the worker. |
| 90 StopWorker(document_cookie); | 91 StopWorker(document_cookie); |
| 91 | 92 |
| 92 // Inform the print preview tab of the failure. | 93 // Inform the print preview tab of the failure. |
| 93 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 94 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 94 // User might have closed it already. | 95 // User might have closed it already. |
| 95 if (!print_preview_tab || !print_preview_tab->tab_contents()->GetWebUI()) | 96 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 96 return NULL; | 97 return NULL; |
| 97 | 98 |
| 98 return static_cast<PrintPreviewUI*>( | 99 return static_cast<PrintPreviewUI*>( |
| 99 print_preview_tab->tab_contents()->GetWebUI()); | 100 print_preview_tab->web_contents()->GetWebUI()); |
| 100 } | 101 } |
| 101 | 102 |
| 102 void PrintPreviewMessageHandler::OnRequestPrintPreview( | 103 void PrintPreviewMessageHandler::OnRequestPrintPreview( |
| 103 bool source_is_modifiable) { | 104 bool source_is_modifiable) { |
| 104 PrintPreviewTabController::PrintPreview(tab_contents_wrapper()); | 105 PrintPreviewTabController::PrintPreview(tab_contents_wrapper()); |
| 105 PrintPreviewUI::SetSourceIsModifiable(GetPrintPreviewTab(), | 106 PrintPreviewUI::SetSourceIsModifiable(GetPrintPreviewTab(), |
| 106 source_is_modifiable); | 107 source_is_modifiable); |
| 107 } | 108 } |
| 108 | 109 |
| 109 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount( | 110 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount( |
| 110 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { | 111 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { |
| 111 if (params.page_count <= 0) { | 112 if (params.page_count <= 0) { |
| 112 NOTREACHED(); | 113 NOTREACHED(); |
| 113 return; | 114 return; |
| 114 } | 115 } |
| 115 | 116 |
| 116 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 117 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 117 if (!print_preview_tab || !print_preview_tab->tab_contents()->GetWebUI()) | 118 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 118 return; | 119 return; |
| 119 | 120 |
| 120 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 121 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 121 print_preview_tab->tab_contents()->GetWebUI()); | 122 print_preview_tab->web_contents()->GetWebUI()); |
| 122 | 123 |
| 123 if (!params.is_modifiable || params.clear_preview_data) | 124 if (!params.is_modifiable || params.clear_preview_data) |
| 124 print_preview_ui->ClearAllPreviewData(); | 125 print_preview_ui->ClearAllPreviewData(); |
| 125 | 126 |
| 126 print_preview_ui->OnDidGetPreviewPageCount(params); | 127 print_preview_ui->OnDidGetPreviewPageCount(params); |
| 127 } | 128 } |
| 128 | 129 |
| 129 void PrintPreviewMessageHandler::OnDidPreviewPage( | 130 void PrintPreviewMessageHandler::OnDidPreviewPage( |
| 130 const PrintHostMsg_DidPreviewPage_Params& params) { | 131 const PrintHostMsg_DidPreviewPage_Params& params) { |
| 131 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 132 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 132 if (!print_preview_tab || !print_preview_tab->tab_contents()->GetWebUI()) | 133 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 133 return; | 134 return; |
| 134 | 135 |
| 135 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 136 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 136 print_preview_tab->tab_contents()->GetWebUI()); | 137 print_preview_tab->web_contents()->GetWebUI()); |
| 137 int page_number = params.page_number; | 138 int page_number = params.page_number; |
| 138 if (page_number >= FIRST_PAGE_INDEX && params.data_size) { | 139 if (page_number >= FIRST_PAGE_INDEX && params.data_size) { |
| 139 RefCountedBytes* data_bytes = | 140 RefCountedBytes* data_bytes = |
| 140 GetDataFromHandle(params.metafile_data_handle, params.data_size); | 141 GetDataFromHandle(params.metafile_data_handle, params.data_size); |
| 141 DCHECK(data_bytes); | 142 DCHECK(data_bytes); |
| 142 | 143 |
| 143 print_preview_ui->SetPrintPreviewDataForIndex(page_number, data_bytes); | 144 print_preview_ui->SetPrintPreviewDataForIndex(page_number, data_bytes); |
| 144 print_preview_ui->OnDidPreviewPage(page_number, params.preview_request_id); | 145 print_preview_ui->OnDidPreviewPage(page_number, params.preview_request_id); |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 | 148 |
| 148 void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( | 149 void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( |
| 149 const PrintHostMsg_DidPreviewDocument_Params& params) { | 150 const PrintHostMsg_DidPreviewDocument_Params& params) { |
| 150 // Always try to stop the worker. | 151 // Always try to stop the worker. |
| 151 StopWorker(params.document_cookie); | 152 StopWorker(params.document_cookie); |
| 152 | 153 |
| 153 if (params.expected_pages_count <= 0) { | 154 if (params.expected_pages_count <= 0) { |
| 154 NOTREACHED(); | 155 NOTREACHED(); |
| 155 return; | 156 return; |
| 156 } | 157 } |
| 157 | 158 |
| 158 // Get the print preview tab. | 159 // Get the print preview tab. |
| 159 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 160 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 160 // User might have closed it already. | 161 // User might have closed it already. |
| 161 if (!print_preview_tab || !print_preview_tab->tab_contents()->GetWebUI()) | 162 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 162 return; | 163 return; |
| 163 | 164 |
| 164 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 165 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 165 print_preview_tab->tab_contents()->GetWebUI()); | 166 print_preview_tab->web_contents()->GetWebUI()); |
| 166 | 167 |
| 167 if (params.reuse_existing_data) { | 168 if (params.reuse_existing_data) { |
| 168 // Need to match normal rendering where we are expected to send this. | 169 // Need to match normal rendering where we are expected to send this. |
| 169 PrintHostMsg_DidGetPreviewPageCount_Params temp_params; | 170 PrintHostMsg_DidGetPreviewPageCount_Params temp_params; |
| 170 temp_params.page_count = params.expected_pages_count; | 171 temp_params.page_count = params.expected_pages_count; |
| 171 temp_params.document_cookie = params.document_cookie; | 172 temp_params.document_cookie = params.document_cookie; |
| 172 temp_params.is_modifiable = params.modifiable; | 173 temp_params.is_modifiable = params.modifiable; |
| 173 temp_params.preview_request_id = params.preview_request_id; | 174 temp_params.preview_request_id = params.preview_request_id; |
| 174 print_preview_ui->OnDidGetPreviewPageCount(temp_params); | 175 print_preview_ui->OnDidGetPreviewPageCount(temp_params); |
| 175 print_preview_ui->OnReusePreviewData(params.preview_request_id); | 176 print_preview_ui->OnReusePreviewData(params.preview_request_id); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 193 void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) { | 194 void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) { |
| 194 PrintPreviewUI* print_preview_ui = OnFailure(document_cookie); | 195 PrintPreviewUI* print_preview_ui = OnFailure(document_cookie); |
| 195 if (!print_preview_ui) | 196 if (!print_preview_ui) |
| 196 return; | 197 return; |
| 197 print_preview_ui->OnPrintPreviewFailed(); | 198 print_preview_ui->OnPrintPreviewFailed(); |
| 198 } | 199 } |
| 199 | 200 |
| 200 void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout( | 201 void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout( |
| 201 const PageSizeMargins& page_layout_in_points) { | 202 const PageSizeMargins& page_layout_in_points) { |
| 202 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); | 203 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 203 if (!print_preview_tab || !print_preview_tab->tab_contents()->GetWebUI()) | 204 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
| 204 return; | 205 return; |
| 205 | 206 |
| 206 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 207 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 207 print_preview_tab->tab_contents()->GetWebUI()); | 208 print_preview_tab->web_contents()->GetWebUI()); |
| 208 print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points); | 209 print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points); |
| 209 } | 210 } |
| 210 | 211 |
| 211 void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) { | 212 void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) { |
| 212 // Always need to stop the worker. | 213 // Always need to stop the worker. |
| 213 StopWorker(document_cookie); | 214 StopWorker(document_cookie); |
| 214 } | 215 } |
| 215 | 216 |
| 216 void PrintPreviewMessageHandler::OnInvalidPrinterSettings(int document_cookie) { | 217 void PrintPreviewMessageHandler::OnInvalidPrinterSettings(int document_cookie) { |
| 217 PrintPreviewUI* print_preview_ui = OnFailure(document_cookie); | 218 PrintPreviewUI* print_preview_ui = OnFailure(document_cookie); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 return; | 258 return; |
| 258 } | 259 } |
| 259 // If |tab| is navigating and it has a print preview tab, notify |tab| to | 260 // If |tab| is navigating and it has a print preview tab, notify |tab| to |
| 260 // consider print preview done so it unfreezes the renderer in the case of | 261 // consider print preview done so it unfreezes the renderer in the case of |
| 261 // window.print(). | 262 // window.print(). |
| 262 if (preview_tab) | 263 if (preview_tab) |
| 263 tab->print_view_manager()->PrintPreviewDone(); | 264 tab->print_view_manager()->PrintPreviewDone(); |
| 264 } | 265 } |
| 265 | 266 |
| 266 } // namespace printing | 267 } // namespace printing |
| OLD | NEW |