OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/webui/print_preview_ui.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/lazy_instance.h" | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/string_util.h" | |
12 #include "base/synchronization/lock.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" | |
17 #include "chrome/browser/printing/print_preview_tab_controller.h" | |
18 #include "chrome/browser/profiles/profile.h" | |
19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
20 #include "chrome/browser/ui/webui/html_dialog_ui.h" | |
21 #include "chrome/browser/ui/webui/print_preview_data_source.h" | |
22 #include "chrome/browser/ui/webui/print_preview_handler.h" | |
23 #include "chrome/common/print_messages.h" | |
24 #include "content/public/browser/web_contents.h" | |
25 #include "printing/page_size_margins.h" | |
26 #include "printing/print_job_constants.h" | |
27 | |
28 using content::WebContents; | |
29 using printing::PageSizeMargins; | |
30 | |
31 namespace { | |
32 | |
33 // Thread-safe wrapper around a std::map to keep track of mappings from | |
34 // PrintPreviewUI addresses to most recent print preview request ids. | |
35 class PrintPreviewRequestIdMapWithLock { | |
36 public: | |
37 PrintPreviewRequestIdMapWithLock() {} | |
38 ~PrintPreviewRequestIdMapWithLock() {} | |
39 | |
40 // Get the value for |addr|. Returns true and sets |out_value| on success. | |
41 bool Get(const std::string& addr, int* out_value) { | |
42 base::AutoLock lock(lock_); | |
43 PrintPreviewRequestIdMap::const_iterator it = map_.find(addr); | |
44 if (it == map_.end()) | |
45 return false; | |
46 *out_value = it->second; | |
47 return true; | |
48 } | |
49 | |
50 // Sets the |value| for |addr|. | |
51 void Set(const std::string& addr, int value) { | |
52 base::AutoLock lock(lock_); | |
53 map_[addr] = value; | |
54 } | |
55 | |
56 // Erase the entry for |addr|. | |
57 void Erase(const std::string& addr) { | |
58 base::AutoLock lock(lock_); | |
59 map_.erase(addr); | |
60 } | |
61 | |
62 private: | |
63 typedef std::map<std::string, int> PrintPreviewRequestIdMap; | |
64 | |
65 PrintPreviewRequestIdMap map_; | |
66 base::Lock lock_; | |
67 }; | |
68 | |
69 // Written to on the UI thread, read from any thread. | |
70 base::LazyInstance<PrintPreviewRequestIdMapWithLock> | |
71 g_print_preview_request_id_map = LAZY_INSTANCE_INITIALIZER; | |
72 | |
73 } // namespace | |
74 | |
75 PrintPreviewUI::PrintPreviewUI(WebContents* contents) | |
76 : ConstrainedHtmlUI(contents), | |
77 initial_preview_start_time_(base::TimeTicks::Now()), | |
78 handler_(NULL), | |
79 source_is_modifiable_(true), | |
80 tab_closed_(false) { | |
81 printing::PrintPreviewTabController* controller = | |
82 printing::PrintPreviewTabController::GetInstance(); | |
83 is_dummy_ = (!controller || !controller->is_creating_print_preview_tab()); | |
84 | |
85 // Set up the chrome://print/ data source. | |
86 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | |
87 profile->GetChromeURLDataManager()->AddDataSource( | |
88 new PrintPreviewDataSource(is_dummy_)); | |
89 if (is_dummy_) | |
90 return; | |
91 | |
92 // WebUI owns |handler_|. | |
93 handler_ = new PrintPreviewHandler(); | |
94 AddMessageHandler(handler_); | |
95 | |
96 preview_ui_addr_str_ = GetPrintPreviewUIAddress(); | |
97 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1); | |
98 } | |
99 | |
100 PrintPreviewUI::~PrintPreviewUI() { | |
101 if (is_dummy_) | |
102 return; | |
103 | |
104 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); | |
105 g_print_preview_request_id_map.Get().Erase(preview_ui_addr_str_); | |
106 } | |
107 | |
108 void PrintPreviewUI::GetPrintPreviewDataForIndex( | |
109 int index, | |
110 scoped_refptr<RefCountedBytes>* data) { | |
111 print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, index, data); | |
112 } | |
113 | |
114 void PrintPreviewUI::SetPrintPreviewDataForIndex(int index, | |
115 const RefCountedBytes* data) { | |
116 print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, index, data); | |
117 } | |
118 | |
119 void PrintPreviewUI::ClearAllPreviewData() { | |
120 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); | |
121 } | |
122 | |
123 int PrintPreviewUI::GetAvailableDraftPageCount() { | |
124 return print_preview_data_service()->GetAvailableDraftPageCount( | |
125 preview_ui_addr_str_); | |
126 } | |
127 | |
128 void PrintPreviewUI::SetInitiatorTabURLAndTitle( | |
129 const std::string& initiator_url, | |
130 const string16& job_title) { | |
131 initiator_url_ = initiator_url; | |
132 initiator_tab_title_ = job_title; | |
133 } | |
134 | |
135 // static | |
136 void PrintPreviewUI::SetSourceIsModifiable( | |
137 TabContentsWrapper* print_preview_tab, | |
138 bool source_is_modifiable) { | |
139 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) | |
140 return; | |
141 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | |
142 print_preview_tab->web_contents()->GetWebUI()); | |
143 print_preview_ui->source_is_modifiable_ = source_is_modifiable; | |
144 } | |
145 | |
146 // static | |
147 void PrintPreviewUI::GetCurrentPrintPreviewStatus( | |
148 const std::string& preview_ui_addr, | |
149 int request_id, | |
150 bool* cancel) { | |
151 int current_id = -1; | |
152 if (!g_print_preview_request_id_map.Get().Get(preview_ui_addr, ¤t_id)) { | |
153 *cancel = true; | |
154 return; | |
155 } | |
156 *cancel = (request_id != current_id); | |
157 } | |
158 | |
159 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const { | |
160 // Store the PrintPreviewUIAddress as a string. | |
161 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; | |
162 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; | |
163 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); | |
164 return preview_ui_addr; | |
165 } | |
166 | |
167 void PrintPreviewUI::OnPrintPreviewTabClosed() { | |
168 TabContentsWrapper* preview_tab = | |
169 TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); | |
170 printing::BackgroundPrintingManager* background_printing_manager = | |
171 g_browser_process->background_printing_manager(); | |
172 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) | |
173 return; | |
174 OnClosePrintPreviewTab(); | |
175 } | |
176 | |
177 void PrintPreviewUI::OnInitiatorTabClosed() { | |
178 TabContentsWrapper* preview_tab = | |
179 TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); | |
180 printing::BackgroundPrintingManager* background_printing_manager = | |
181 g_browser_process->background_printing_manager(); | |
182 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) | |
183 CallJavascriptFunction("cancelPendingPrintRequest"); | |
184 else | |
185 OnClosePrintPreviewTab(); | |
186 } | |
187 | |
188 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { | |
189 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, request_id); | |
190 } | |
191 | |
192 void PrintPreviewUI::OnShowSystemDialog() { | |
193 CallJavascriptFunction("onSystemDialogLinkClicked"); | |
194 } | |
195 | |
196 void PrintPreviewUI::OnDidGetPreviewPageCount( | |
197 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { | |
198 DCHECK_GT(params.page_count, 0); | |
199 base::FundamentalValue count(params.page_count); | |
200 base::FundamentalValue request_id(params.preview_request_id); | |
201 CallJavascriptFunction("onDidGetPreviewPageCount", count, request_id); | |
202 } | |
203 | |
204 void PrintPreviewUI::OnDidGetDefaultPageLayout( | |
205 const PageSizeMargins& page_layout, bool has_custom_page_size_style) { | |
206 if (page_layout.margin_top < 0 || page_layout.margin_left < 0 || | |
207 page_layout.margin_bottom < 0 || page_layout.margin_right < 0 || | |
208 page_layout.content_width < 0 || page_layout.content_height < 0) { | |
209 NOTREACHED(); | |
210 return; | |
211 } | |
212 | |
213 base::DictionaryValue layout; | |
214 layout.SetDouble(printing::kSettingMarginTop, page_layout.margin_top); | |
215 layout.SetDouble(printing::kSettingMarginLeft, page_layout.margin_left); | |
216 layout.SetDouble(printing::kSettingMarginBottom, page_layout.margin_bottom); | |
217 layout.SetDouble(printing::kSettingMarginRight, page_layout.margin_right); | |
218 layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width); | |
219 layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height); | |
220 | |
221 base::FundamentalValue has_page_size_style(has_custom_page_size_style); | |
222 CallJavascriptFunction("onDidGetDefaultPageLayout", layout, | |
223 has_page_size_style); | |
224 } | |
225 | |
226 void PrintPreviewUI::OnDidPreviewPage(int page_number, | |
227 int preview_request_id) { | |
228 DCHECK_GE(page_number, 0); | |
229 base::FundamentalValue number(page_number); | |
230 StringValue ui_identifier(preview_ui_addr_str_); | |
231 base::FundamentalValue request_id(preview_request_id); | |
232 CallJavascriptFunction("onDidPreviewPage", number, ui_identifier, request_id); | |
233 } | |
234 | |
235 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { | |
236 base::StringValue ui_identifier(preview_ui_addr_str_); | |
237 base::FundamentalValue ui_preview_request_id(preview_request_id); | |
238 CallJavascriptFunction("reloadPreviewPages", ui_identifier, | |
239 ui_preview_request_id); | |
240 } | |
241 | |
242 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, | |
243 int preview_request_id) { | |
244 VLOG(1) << "Print preview request finished with " | |
245 << expected_pages_count << " pages"; | |
246 | |
247 if (!initial_preview_start_time_.is_null()) { | |
248 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime", | |
249 base::TimeTicks::Now() - initial_preview_start_time_); | |
250 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", | |
251 expected_pages_count); | |
252 initial_preview_start_time_ = base::TimeTicks(); | |
253 } | |
254 base::StringValue ui_identifier(preview_ui_addr_str_); | |
255 base::FundamentalValue ui_preview_request_id(preview_request_id); | |
256 CallJavascriptFunction("updatePrintPreview", ui_identifier, | |
257 ui_preview_request_id); | |
258 } | |
259 | |
260 void PrintPreviewUI::OnTabDestroyed() { | |
261 handler_->OnTabDestroyed(); | |
262 } | |
263 | |
264 void PrintPreviewUI::OnFileSelectionCancelled() { | |
265 CallJavascriptFunction("fileSelectionCancelled"); | |
266 } | |
267 | |
268 void PrintPreviewUI::OnCancelPendingPreviewRequest() { | |
269 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1); | |
270 } | |
271 | |
272 void PrintPreviewUI::OnPrintPreviewFailed() { | |
273 handler_->OnPrintPreviewFailed(); | |
274 CallJavascriptFunction("printPreviewFailed"); | |
275 } | |
276 | |
277 void PrintPreviewUI::OnInvalidPrinterSettings() { | |
278 CallJavascriptFunction("invalidPrinterSettings"); | |
279 } | |
280 | |
281 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { | |
282 return PrintPreviewDataService::GetInstance(); | |
283 } | |
284 | |
285 void PrintPreviewUI::OnHidePreviewTab() { | |
286 TabContentsWrapper* preview_tab = | |
287 TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); | |
288 printing::BackgroundPrintingManager* background_printing_manager = | |
289 g_browser_process->background_printing_manager(); | |
290 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) | |
291 return; | |
292 | |
293 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); | |
294 if (!delegate) | |
295 return; | |
296 delegate->ReleaseTabContentsOnDialogClose(); | |
297 background_printing_manager->OwnPrintPreviewTab(preview_tab); | |
298 OnClosePrintPreviewTab(); | |
299 } | |
300 | |
301 void PrintPreviewUI::OnClosePrintPreviewTab() { | |
302 if (tab_closed_) | |
303 return; | |
304 tab_closed_ = true; | |
305 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); | |
306 if (!delegate) | |
307 return; | |
308 delegate->GetHtmlDialogUIDelegate()->OnDialogClosed(""); | |
309 delegate->OnDialogCloseFromWebUI(); | |
310 } | |
311 | |
312 void PrintPreviewUI::OnReloadPrintersList() { | |
313 CallJavascriptFunction("reloadPrintersList"); | |
314 } | |
OLD | NEW |