| 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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 | 62 |
| 63 PrintPreviewMessageHandler::PrintPreviewMessageHandler( | 63 PrintPreviewMessageHandler::PrintPreviewMessageHandler( |
| 64 TabContents* tab_contents) | 64 TabContents* tab_contents) |
| 65 : TabContentsObserver(tab_contents) { | 65 : TabContentsObserver(tab_contents) { |
| 66 DCHECK(tab_contents); | 66 DCHECK(tab_contents); |
| 67 } | 67 } |
| 68 | 68 |
| 69 PrintPreviewMessageHandler::~PrintPreviewMessageHandler() { | 69 PrintPreviewMessageHandler::~PrintPreviewMessageHandler() { |
| 70 } | 70 } |
| 71 | 71 |
| 72 TabContents* PrintPreviewMessageHandler::GetPrintPreviewTab() { | 72 TabContentsWrapper* PrintPreviewMessageHandler::GetPrintPreviewTab() { |
| 73 printing::PrintPreviewTabController* tab_controller = | 73 PrintPreviewTabController* tab_controller = |
| 74 printing::PrintPreviewTabController::GetInstance(); | 74 PrintPreviewTabController::GetInstance(); |
| 75 if (!tab_controller) | 75 if (!tab_controller) |
| 76 return NULL; | 76 return NULL; |
| 77 | 77 |
| 78 return tab_controller->GetPrintPreviewForTab(tab_contents()); | 78 return tab_controller->GetPrintPreviewForTab(tab_contents_wrapper()); |
| 79 } |
| 80 |
| 81 TabContentsWrapper* PrintPreviewMessageHandler::tab_contents_wrapper() { |
| 82 return TabContentsWrapper::GetCurrentWrapperForContents(tab_contents()); |
| 79 } | 83 } |
| 80 | 84 |
| 81 void PrintPreviewMessageHandler::OnRequestPrintPreview() { | 85 void PrintPreviewMessageHandler::OnRequestPrintPreview() { |
| 82 PrintPreviewTabController::PrintPreview(tab_contents()); | 86 PrintPreviewTabController::PrintPreview(tab_contents_wrapper()); |
| 83 } | 87 } |
| 84 | 88 |
| 85 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount( | 89 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount( |
| 86 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { | 90 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { |
| 87 if (params.page_count <= 0) { | 91 if (params.page_count <= 0) { |
| 88 NOTREACHED(); | 92 NOTREACHED(); |
| 89 return; | 93 return; |
| 90 } | 94 } |
| 91 | 95 |
| 92 TabContents* print_preview_tab = GetPrintPreviewTab(); | 96 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 93 if (!print_preview_tab || !print_preview_tab->web_ui()) | 97 if (!print_preview_tab || !print_preview_tab->web_ui()) |
| 94 return; | 98 return; |
| 95 | 99 |
| 96 PrintPreviewUI* print_preview_ui = | 100 PrintPreviewUI* print_preview_ui = |
| 97 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 101 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
| 98 print_preview_ui->OnDidGetPreviewPageCount(params); | 102 print_preview_ui->OnDidGetPreviewPageCount(params); |
| 99 } | 103 } |
| 100 | 104 |
| 101 void PrintPreviewMessageHandler::OnDidPreviewPage( | 105 void PrintPreviewMessageHandler::OnDidPreviewPage( |
| 102 const PrintHostMsg_DidPreviewPage_Params& params) { | 106 const PrintHostMsg_DidPreviewPage_Params& params) { |
| 103 TabContents* print_preview_tab = GetPrintPreviewTab(); | 107 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 104 if (!print_preview_tab || !print_preview_tab->web_ui()) | 108 if (!print_preview_tab || !print_preview_tab->web_ui()) |
| 105 return; | 109 return; |
| 106 | 110 |
| 107 PrintPreviewUI* print_preview_ui = | 111 PrintPreviewUI* print_preview_ui = |
| 108 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 112 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
| 109 int page_number = params.page_number; | 113 int page_number = params.page_number; |
| 110 | 114 |
| 111 if (page_number == FIRST_PAGE_INDEX) | 115 if (page_number == FIRST_PAGE_INDEX) |
| 112 print_preview_ui->ClearAllPreviewData(); | 116 print_preview_ui->ClearAllPreviewData(); |
| 113 | 117 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 125 const PrintHostMsg_DidPreviewDocument_Params& params) { | 129 const PrintHostMsg_DidPreviewDocument_Params& params) { |
| 126 // Always try to stop the worker. | 130 // Always try to stop the worker. |
| 127 StopWorker(params.document_cookie); | 131 StopWorker(params.document_cookie); |
| 128 | 132 |
| 129 if (params.expected_pages_count <= 0) { | 133 if (params.expected_pages_count <= 0) { |
| 130 NOTREACHED(); | 134 NOTREACHED(); |
| 131 return; | 135 return; |
| 132 } | 136 } |
| 133 | 137 |
| 134 // Get the print preview tab. | 138 // Get the print preview tab. |
| 135 TabContents* print_preview_tab = GetPrintPreviewTab(); | 139 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 136 // User might have closed it already. | 140 // User might have closed it already. |
| 137 if (!print_preview_tab) | 141 if (!print_preview_tab || !print_preview_tab->web_ui()) |
| 138 return; | 142 return; |
| 139 | 143 |
| 140 PrintPreviewUI* print_preview_ui = | 144 PrintPreviewUI* print_preview_ui = |
| 141 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 145 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
| 142 | 146 |
| 143 TabContentsWrapper* wrapper = | |
| 144 TabContentsWrapper::GetCurrentWrapperForContents(print_preview_tab); | |
| 145 | |
| 146 if (params.reuse_existing_data) { | 147 if (params.reuse_existing_data) { |
| 147 // Need to match normal rendering where we are expected to send this. | 148 // Need to match normal rendering where we are expected to send this. |
| 148 PrintHostMsg_DidGetPreviewPageCount_Params temp_params; | 149 PrintHostMsg_DidGetPreviewPageCount_Params temp_params; |
| 149 temp_params.page_count = params.expected_pages_count; | 150 temp_params.page_count = params.expected_pages_count; |
| 150 temp_params.document_cookie = params.document_cookie; | 151 temp_params.document_cookie = params.document_cookie; |
| 151 temp_params.is_modifiable = params.modifiable; | 152 temp_params.is_modifiable = params.modifiable; |
| 152 temp_params.preview_request_id = params.preview_request_id; | 153 temp_params.preview_request_id = params.preview_request_id; |
| 153 print_preview_ui->OnDidGetPreviewPageCount(temp_params); | 154 print_preview_ui->OnDidGetPreviewPageCount(temp_params); |
| 154 print_preview_ui->OnReusePreviewData(params.preview_request_id); | 155 print_preview_ui->OnReusePreviewData(params.preview_request_id); |
| 155 return; | 156 return; |
| 156 } | 157 } |
| 157 | 158 |
| 158 wrapper->print_view_manager()->OverrideTitle(tab_contents()); | 159 print_preview_tab->print_view_manager()->OverrideTitle(tab_contents()); |
| 159 | 160 |
| 160 // TODO(joth): This seems like a good match for using RefCountedStaticMemory | 161 // TODO(joth): This seems like a good match for using RefCountedStaticMemory |
| 161 // to avoid the memory copy, but the SetPrintPreviewData call chain below | 162 // to avoid the memory copy, but the SetPrintPreviewData call chain below |
| 162 // needs updating to accept the RefCountedMemory* base class. | 163 // needs updating to accept the RefCountedMemory* base class. |
| 163 RefCountedBytes* data_bytes = | 164 RefCountedBytes* data_bytes = |
| 164 GetDataFromHandle(params.metafile_data_handle, params.data_size); | 165 GetDataFromHandle(params.metafile_data_handle, params.data_size); |
| 165 if (!data_bytes) | 166 if (!data_bytes) |
| 166 return; | 167 return; |
| 167 | 168 |
| 168 print_preview_ui->SetPrintPreviewDataForIndex(COMPLETE_PREVIEW_DOCUMENT_INDEX, | 169 print_preview_ui->SetPrintPreviewDataForIndex(COMPLETE_PREVIEW_DOCUMENT_INDEX, |
| 169 data_bytes); | 170 data_bytes); |
| 170 print_preview_ui->OnPreviewDataIsAvailable( | 171 print_preview_ui->OnPreviewDataIsAvailable( |
| 171 params.expected_pages_count, params.preview_request_id); | 172 params.expected_pages_count, params.preview_request_id); |
| 172 } | 173 } |
| 173 | 174 |
| 174 void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) { | 175 void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) { |
| 175 // Always need to stop the worker. | 176 // Always need to stop the worker. |
| 176 StopWorker(document_cookie); | 177 StopWorker(document_cookie); |
| 177 | 178 |
| 178 // Inform the print preview tab of the failure. | 179 // Inform the print preview tab of the failure. |
| 179 TabContents* print_preview_tab = GetPrintPreviewTab(); | 180 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 180 // User might have closed it already. | 181 // User might have closed it already. |
| 181 if (!print_preview_tab) | 182 if (!print_preview_tab || !print_preview_tab->web_ui()) |
| 182 return; | 183 return; |
| 183 | 184 |
| 184 TabContentsWrapper* wrapper = | |
| 185 TabContentsWrapper::GetCurrentWrapperForContents(print_preview_tab); | |
| 186 | |
| 187 if (g_browser_process->background_printing_manager()-> | 185 if (g_browser_process->background_printing_manager()-> |
| 188 HasTabContents(wrapper)) { | 186 HasTabContents(print_preview_tab)) { |
| 189 // Preview tab was hidden to serve the print request. | 187 // Preview tab was hidden to serve the print request. |
| 190 delete wrapper; | 188 delete print_preview_tab; |
| 191 } else { | 189 } else { |
| 192 PrintPreviewUI* print_preview_ui = | 190 PrintPreviewUI* print_preview_ui = |
| 193 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 191 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
| 194 print_preview_ui->OnPrintPreviewFailed(); | 192 print_preview_ui->OnPrintPreviewFailed(); |
| 195 } | 193 } |
| 196 } | 194 } |
| 197 | 195 |
| 198 void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout( | 196 void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout( |
| 199 const PageSizeMargins& page_layout_in_points) { | 197 const PageSizeMargins& page_layout_in_points) { |
| 200 TabContents* print_preview_tab = GetPrintPreviewTab(); | 198 TabContentsWrapper* print_preview_tab = GetPrintPreviewTab(); |
| 201 if (!print_preview_tab) | 199 if (!print_preview_tab || !print_preview_tab->web_ui()) |
| 202 return; | 200 return; |
| 203 | 201 |
| 204 PrintPreviewUI* print_preview_ui = | 202 PrintPreviewUI* print_preview_ui = |
| 205 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 203 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
| 206 print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points); | 204 print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points); |
| 207 } | 205 } |
| 208 | 206 |
| 209 void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) { | 207 void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) { |
| 210 // Always need to stop the worker. | 208 // Always need to stop the worker. |
| 211 StopWorker(document_cookie); | 209 StopWorker(document_cookie); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 229 OnDidGetDefaultPageLayout) | 227 OnDidGetDefaultPageLayout) |
| 230 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled, | 228 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled, |
| 231 OnPrintPreviewCancelled) | 229 OnPrintPreviewCancelled) |
| 232 IPC_MESSAGE_UNHANDLED(handled = false) | 230 IPC_MESSAGE_UNHANDLED(handled = false) |
| 233 IPC_END_MESSAGE_MAP() | 231 IPC_END_MESSAGE_MAP() |
| 234 return handled; | 232 return handled; |
| 235 } | 233 } |
| 236 | 234 |
| 237 void PrintPreviewMessageHandler::DidStartLoading() { | 235 void PrintPreviewMessageHandler::DidStartLoading() { |
| 238 if (tab_contents()->delegate() && | 236 if (tab_contents()->delegate() && |
| 239 printing::PrintPreviewTabController::IsPrintPreviewTab(tab_contents())) { | 237 PrintPreviewTabController::IsPrintPreviewTab(tab_contents_wrapper())) { |
| 240 tab_contents()->SetContentRestrictions(CONTENT_RESTRICTION_PRINT); | 238 tab_contents()->SetContentRestrictions(CONTENT_RESTRICTION_PRINT); |
| 241 } | 239 } |
| 242 } | 240 } |
| 243 | 241 |
| 244 } // namespace printing | 242 } // namespace printing |
| OLD | NEW |