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/ui/webui/print_preview_ui.h" | 5 #include "chrome/browser/ui/webui/print_preview_ui.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/printing/background_printing_manager.h" |
14 #include "chrome/browser/printing/print_preview_data_service.h" | 16 #include "chrome/browser/printing/print_preview_data_service.h" |
15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 19 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
16 #include "chrome/browser/ui/webui/print_preview_data_source.h" | 20 #include "chrome/browser/ui/webui/print_preview_data_source.h" |
17 #include "chrome/browser/ui/webui/print_preview_handler.h" | 21 #include "chrome/browser/ui/webui/print_preview_handler.h" |
18 #include "chrome/common/print_messages.h" | 22 #include "chrome/common/print_messages.h" |
19 #include "content/browser/tab_contents/tab_contents.h" | 23 #include "content/browser/tab_contents/tab_contents.h" |
20 #include "printing/page_size_margins.h" | 24 #include "printing/page_size_margins.h" |
21 #include "printing/print_job_constants.h" | 25 #include "printing/print_job_constants.h" |
22 | 26 |
23 using printing::PageSizeMargins; | 27 using printing::PageSizeMargins; |
24 | 28 |
25 namespace { | 29 namespace { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 base::Lock lock_; | 64 base::Lock lock_; |
61 }; | 65 }; |
62 | 66 |
63 // Written to on the UI thread, read from any thread. | 67 // Written to on the UI thread, read from any thread. |
64 base::LazyInstance<PrintPreviewRequestIdMapWithLock> | 68 base::LazyInstance<PrintPreviewRequestIdMapWithLock> |
65 g_print_preview_request_id_map(base::LINKER_INITIALIZED); | 69 g_print_preview_request_id_map(base::LINKER_INITIALIZED); |
66 | 70 |
67 } // namespace | 71 } // namespace |
68 | 72 |
69 PrintPreviewUI::PrintPreviewUI(TabContents* contents) | 73 PrintPreviewUI::PrintPreviewUI(TabContents* contents) |
70 : ChromeWebUI(contents), | 74 : ConstrainedHtmlUI(contents), |
71 initial_preview_start_time_(base::TimeTicks::Now()) { | 75 initial_preview_start_time_(base::TimeTicks::Now()), |
| 76 tab_closed_(false) { |
72 // WebUI owns |handler_|. | 77 // WebUI owns |handler_|. |
73 handler_ = new PrintPreviewHandler(); | 78 handler_ = new PrintPreviewHandler(); |
74 AddMessageHandler(handler_->Attach(this)); | 79 AddMessageHandler(handler_->Attach(this)); |
75 | 80 |
76 // Set up the chrome://print/ data source. | 81 // Set up the chrome://print/ data source. |
77 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | 82 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); |
78 profile->GetChromeURLDataManager()->AddDataSource( | 83 profile->GetChromeURLDataManager()->AddDataSource( |
79 new PrintPreviewDataSource()); | 84 new PrintPreviewDataSource()); |
80 | 85 |
81 preview_ui_addr_str_ = GetPrintPreviewUIAddress(); | 86 preview_ui_addr_str_ = GetPrintPreviewUIAddress(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } | 139 } |
135 | 140 |
136 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const { | 141 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const { |
137 // Store the PrintPreviewUIAddress as a string. | 142 // Store the PrintPreviewUIAddress as a string. |
138 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; | 143 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; |
139 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; | 144 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; |
140 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); | 145 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); |
141 return preview_ui_addr; | 146 return preview_ui_addr; |
142 } | 147 } |
143 | 148 |
144 void PrintPreviewUI::OnInitiatorTabCrashed() { | 149 void PrintPreviewUI::OnPrintPreviewTabClosed() { |
145 StringValue initiator_tab_url(initiator_url_); | 150 TabContentsWrapper* preview_tab = |
146 CallJavascriptFunction("onInitiatorTabCrashed", initiator_tab_url); | 151 TabContentsWrapper::GetCurrentWrapperForContents(tab_contents()); |
| 152 printing::BackgroundPrintingManager* background_printing_manager = |
| 153 g_browser_process->background_printing_manager(); |
| 154 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) |
| 155 return; |
| 156 OnClosePrintPreviewTab(); |
147 } | 157 } |
148 | 158 |
149 void PrintPreviewUI::OnInitiatorTabClosed() { | 159 void PrintPreviewUI::OnInitiatorTabClosed() { |
150 StringValue initiator_tab_url(initiator_url_); | 160 TabContentsWrapper* preview_tab = |
151 CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url); | 161 TabContentsWrapper::GetCurrentWrapperForContents(tab_contents()); |
| 162 printing::BackgroundPrintingManager* background_printing_manager = |
| 163 g_browser_process->background_printing_manager(); |
| 164 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) |
| 165 CallJavascriptFunction("cancelPendingPrintRequest"); |
| 166 else |
| 167 OnClosePrintPreviewTab(); |
152 } | 168 } |
153 | 169 |
154 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { | 170 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { |
155 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, request_id); | 171 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, request_id); |
156 } | 172 } |
157 | 173 |
158 void PrintPreviewUI::OnShowSystemDialog() { | 174 void PrintPreviewUI::OnShowSystemDialog() { |
159 CallJavascriptFunction("onSystemDialogLinkClicked"); | 175 CallJavascriptFunction("onSystemDialogLinkClicked"); |
160 } | 176 } |
161 | 177 |
162 void PrintPreviewUI::OnDidGetPreviewPageCount( | 178 void PrintPreviewUI::OnDidGetPreviewPageCount( |
163 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { | 179 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { |
164 DCHECK_GT(params.page_count, 0); | 180 DCHECK_GT(params.page_count, 0); |
165 base::FundamentalValue count(params.page_count); | 181 base::FundamentalValue count(params.page_count); |
166 base::FundamentalValue request_id(params.preview_request_id); | 182 base::FundamentalValue request_id(params.preview_request_id); |
167 CallJavascriptFunction("onDidGetPreviewPageCount", count,request_id); | 183 CallJavascriptFunction("onDidGetPreviewPageCount", count, request_id); |
168 } | 184 } |
169 | 185 |
170 void PrintPreviewUI::OnDidGetDefaultPageLayout( | 186 void PrintPreviewUI::OnDidGetDefaultPageLayout( |
171 const PageSizeMargins& page_layout) { | 187 const PageSizeMargins& page_layout) { |
172 if (page_layout.margin_top < 0 || page_layout.margin_left < 0 || | 188 if (page_layout.margin_top < 0 || page_layout.margin_left < 0 || |
173 page_layout.margin_bottom < 0 || page_layout.margin_right < 0 || | 189 page_layout.margin_bottom < 0 || page_layout.margin_right < 0 || |
174 page_layout.content_width < 0 || page_layout.content_height < 0) { | 190 page_layout.content_width < 0 || page_layout.content_height < 0) { |
175 NOTREACHED(); | 191 NOTREACHED(); |
176 return; | 192 return; |
177 } | 193 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 CallJavascriptFunction("printPreviewFailed"); | 254 CallJavascriptFunction("printPreviewFailed"); |
239 } | 255 } |
240 | 256 |
241 void PrintPreviewUI::OnInvalidPrinterSettings() { | 257 void PrintPreviewUI::OnInvalidPrinterSettings() { |
242 CallJavascriptFunction("invalidPrinterSettings"); | 258 CallJavascriptFunction("invalidPrinterSettings"); |
243 } | 259 } |
244 | 260 |
245 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { | 261 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { |
246 return PrintPreviewDataService::GetInstance(); | 262 return PrintPreviewDataService::GetInstance(); |
247 } | 263 } |
| 264 |
| 265 void PrintPreviewUI::OnHidePreviewTab() { |
| 266 TabContentsWrapper* preview_tab = |
| 267 TabContentsWrapper::GetCurrentWrapperForContents(tab_contents()); |
| 268 printing::BackgroundPrintingManager* background_printing_manager = |
| 269 g_browser_process->background_printing_manager(); |
| 270 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) |
| 271 return; |
| 272 |
| 273 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); |
| 274 if (!delegate) |
| 275 return; |
| 276 delegate->ReleaseTabContentsOnDialogClose(); |
| 277 background_printing_manager->OwnPrintPreviewTab(preview_tab); |
| 278 OnClosePrintPreviewTab(); |
| 279 } |
| 280 |
| 281 void PrintPreviewUI::OnClosePrintPreviewTab() { |
| 282 if (tab_closed_) |
| 283 return; |
| 284 tab_closed_ = true; |
| 285 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); |
| 286 if (!delegate) |
| 287 return; |
| 288 delegate->GetHtmlDialogUIDelegate()->OnDialogClosed(""); |
| 289 delegate->OnDialogCloseFromWebUI(); |
| 290 } |
| 291 |
| 292 void PrintPreviewUI::OnReloadPrintersList() { |
| 293 CallJavascriptFunction("reloadPrintersList"); |
| 294 } |
OLD | NEW |