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