OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/print_preview_ui.h" | 5 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
| 9 #include "base/id_map.h" |
9 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
10 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/printing/background_printing_manager.h" | 17 #include "chrome/browser/printing/background_printing_manager.h" |
17 #include "chrome/browser/printing/print_preview_data_service.h" | 18 #include "chrome/browser/printing/print_preview_data_service.h" |
18 #include "chrome/browser/printing/print_preview_tab_controller.h" | 19 #include "chrome/browser/printing/print_preview_tab_controller.h" |
(...skipping 11 matching lines...) Expand all Loading... |
30 #include "ui/web_dialogs/web_dialog_ui.h" | 31 #include "ui/web_dialogs/web_dialog_ui.h" |
31 | 32 |
32 using content::WebContents; | 33 using content::WebContents; |
33 using printing::PageSizeMargins; | 34 using printing::PageSizeMargins; |
34 using ui::ConstrainedWebDialogDelegate; | 35 using ui::ConstrainedWebDialogDelegate; |
35 using ui::ConstrainedWebDialogUI; | 36 using ui::ConstrainedWebDialogUI; |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 // Thread-safe wrapper around a std::map to keep track of mappings from | 40 // Thread-safe wrapper around a std::map to keep track of mappings from |
40 // PrintPreviewUI addresses to most recent print preview request ids. | 41 // PrintPreviewUI IDs to most recent print preview request IDs. |
41 class PrintPreviewRequestIdMapWithLock { | 42 class PrintPreviewRequestIdMapWithLock { |
42 public: | 43 public: |
43 PrintPreviewRequestIdMapWithLock() {} | 44 PrintPreviewRequestIdMapWithLock() {} |
44 ~PrintPreviewRequestIdMapWithLock() {} | 45 ~PrintPreviewRequestIdMapWithLock() {} |
45 | 46 |
46 // Get the value for |addr|. Returns true and sets |out_value| on success. | 47 // Gets the value for |preview_id|. |
47 bool Get(const std::string& addr, int* out_value) { | 48 // Returns true and sets |out_value| on success. |
| 49 bool Get(int32 preview_id, int* out_value) { |
48 base::AutoLock lock(lock_); | 50 base::AutoLock lock(lock_); |
49 PrintPreviewRequestIdMap::const_iterator it = map_.find(addr); | 51 PrintPreviewRequestIdMap::const_iterator it = map_.find(preview_id); |
50 if (it == map_.end()) | 52 if (it == map_.end()) |
51 return false; | 53 return false; |
52 *out_value = it->second; | 54 *out_value = it->second; |
53 return true; | 55 return true; |
54 } | 56 } |
55 | 57 |
56 // Sets the |value| for |addr|. | 58 // Sets the |value| for |preview_id|. |
57 void Set(const std::string& addr, int value) { | 59 void Set(int32 preview_id, int value) { |
58 base::AutoLock lock(lock_); | 60 base::AutoLock lock(lock_); |
59 map_[addr] = value; | 61 map_[preview_id] = value; |
60 } | 62 } |
61 | 63 |
62 // Erase the entry for |addr|. | 64 // Erases the entry for |preview_id|. |
63 void Erase(const std::string& addr) { | 65 void Erase(int32 preview_id) { |
64 base::AutoLock lock(lock_); | 66 base::AutoLock lock(lock_); |
65 map_.erase(addr); | 67 map_.erase(preview_id); |
66 } | 68 } |
67 | 69 |
68 private: | 70 private: |
69 typedef std::map<std::string, int> PrintPreviewRequestIdMap; | 71 // Mapping from PrintPreviewUI ID to print preview request ID. |
| 72 typedef std::map<int, int> PrintPreviewRequestIdMap; |
70 | 73 |
71 PrintPreviewRequestIdMap map_; | 74 PrintPreviewRequestIdMap map_; |
72 base::Lock lock_; | 75 base::Lock lock_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(PrintPreviewRequestIdMapWithLock); |
73 }; | 78 }; |
74 | 79 |
75 // Written to on the UI thread, read from any thread. | 80 // Written to on the UI thread, read from any thread. |
76 base::LazyInstance<PrintPreviewRequestIdMapWithLock> | 81 base::LazyInstance<PrintPreviewRequestIdMapWithLock> |
77 g_print_preview_request_id_map = LAZY_INSTANCE_INITIALIZER; | 82 g_print_preview_request_id_map = LAZY_INSTANCE_INITIALIZER; |
78 | 83 |
| 84 // PrintPreviewUI IDMap used to avoid exposing raw pointer addresses to WebUI. |
| 85 // Only accessed on the UI thread. |
| 86 base::LazyInstance<IDMap<PrintPreviewUI> > |
| 87 g_print_preview_ui_id_map = LAZY_INSTANCE_INITIALIZER; |
| 88 |
79 } // namespace | 89 } // namespace |
80 | 90 |
81 PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui) | 91 PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui) |
82 : ConstrainedWebDialogUI(web_ui), | 92 : ConstrainedWebDialogUI(web_ui), |
83 initial_preview_start_time_(base::TimeTicks::Now()), | 93 initial_preview_start_time_(base::TimeTicks::Now()), |
| 94 id_(g_print_preview_ui_id_map.Get().Add(this)), |
84 handler_(NULL), | 95 handler_(NULL), |
85 source_is_modifiable_(true), | 96 source_is_modifiable_(true), |
86 tab_closed_(false) { | 97 tab_closed_(false) { |
87 // Set up the chrome://print/ data source. | 98 // Set up the chrome://print/ data source. |
88 Profile* profile = Profile::FromWebUI(web_ui); | 99 Profile* profile = Profile::FromWebUI(web_ui); |
89 ChromeURLDataManager::AddDataSource(profile, new PrintPreviewDataSource()); | 100 ChromeURLDataManager::AddDataSource(profile, new PrintPreviewDataSource()); |
90 | 101 |
91 // WebUI owns |handler_|. | 102 // WebUI owns |handler_|. |
92 handler_ = new PrintPreviewHandler(); | 103 handler_ = new PrintPreviewHandler(); |
93 web_ui->AddMessageHandler(handler_); | 104 web_ui->AddMessageHandler(handler_); |
94 | 105 |
95 preview_ui_addr_str_ = GetPrintPreviewUIAddress(); | 106 g_print_preview_request_id_map.Get().Set(id_, -1); |
96 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1); | |
97 } | 107 } |
98 | 108 |
99 PrintPreviewUI::~PrintPreviewUI() { | 109 PrintPreviewUI::~PrintPreviewUI() { |
100 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); | 110 print_preview_data_service()->RemoveEntry(id_); |
101 g_print_preview_request_id_map.Get().Erase(preview_ui_addr_str_); | 111 g_print_preview_request_id_map.Get().Erase(id_); |
| 112 g_print_preview_ui_id_map.Get().Remove(id_); |
102 } | 113 } |
103 | 114 |
104 void PrintPreviewUI::GetPrintPreviewDataForIndex( | 115 void PrintPreviewUI::GetPrintPreviewDataForIndex( |
105 int index, | 116 int index, |
106 scoped_refptr<base::RefCountedBytes>* data) { | 117 scoped_refptr<base::RefCountedBytes>* data) { |
107 print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, index, data); | 118 print_preview_data_service()->GetDataEntry(id_, index, data); |
108 } | 119 } |
109 | 120 |
110 void PrintPreviewUI::SetPrintPreviewDataForIndex( | 121 void PrintPreviewUI::SetPrintPreviewDataForIndex( |
111 int index, | 122 int index, |
112 const base::RefCountedBytes* data) { | 123 const base::RefCountedBytes* data) { |
113 print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, index, data); | 124 print_preview_data_service()->SetDataEntry(id_, index, data); |
114 } | 125 } |
115 | 126 |
116 void PrintPreviewUI::ClearAllPreviewData() { | 127 void PrintPreviewUI::ClearAllPreviewData() { |
117 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); | 128 print_preview_data_service()->RemoveEntry(id_); |
118 } | 129 } |
119 | 130 |
120 int PrintPreviewUI::GetAvailableDraftPageCount() { | 131 int PrintPreviewUI::GetAvailableDraftPageCount() { |
121 return print_preview_data_service()->GetAvailableDraftPageCount( | 132 return print_preview_data_service()->GetAvailableDraftPageCount(id_); |
122 preview_ui_addr_str_); | |
123 } | 133 } |
124 | 134 |
125 void PrintPreviewUI::SetInitiatorTabURLAndTitle( | 135 void PrintPreviewUI::SetInitiatorTabURLAndTitle( |
126 const std::string& initiator_url, | 136 const std::string& initiator_url, |
127 const string16& job_title) { | 137 const string16& job_title) { |
128 initiator_url_ = initiator_url; | 138 initiator_url_ = initiator_url; |
129 initiator_tab_title_ = job_title; | 139 initiator_tab_title_ = job_title; |
130 } | 140 } |
131 | 141 |
132 // static | 142 // static |
133 void PrintPreviewUI::SetSourceIsModifiable(TabContents* print_preview_tab, | 143 void PrintPreviewUI::SetSourceIsModifiable(TabContents* print_preview_tab, |
134 bool source_is_modifiable) { | 144 bool source_is_modifiable) { |
135 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) | 145 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) |
136 return; | 146 return; |
137 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 147 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
138 print_preview_tab->web_contents()->GetWebUI()->GetController()); | 148 print_preview_tab->web_contents()->GetWebUI()->GetController()); |
139 print_preview_ui->source_is_modifiable_ = source_is_modifiable; | 149 print_preview_ui->source_is_modifiable_ = source_is_modifiable; |
140 } | 150 } |
141 | 151 |
142 // static | 152 // static |
143 void PrintPreviewUI::GetCurrentPrintPreviewStatus( | 153 void PrintPreviewUI::GetCurrentPrintPreviewStatus(int32 preview_ui_id, |
144 const std::string& preview_ui_addr, | 154 int request_id, |
145 int request_id, | 155 bool* cancel) { |
146 bool* cancel) { | |
147 int current_id = -1; | 156 int current_id = -1; |
148 if (!g_print_preview_request_id_map.Get().Get(preview_ui_addr, ¤t_id)) { | 157 if (!g_print_preview_request_id_map.Get().Get(preview_ui_id, ¤t_id)) { |
149 *cancel = true; | 158 *cancel = true; |
150 return; | 159 return; |
151 } | 160 } |
152 *cancel = (request_id != current_id); | 161 *cancel = (request_id != current_id); |
153 } | 162 } |
154 | 163 |
155 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const { | 164 int32 PrintPreviewUI::GetIDForPrintPreviewUI() const { |
156 // Store the PrintPreviewUIAddress as a string. | 165 return id_; |
157 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; | |
158 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; | |
159 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); | |
160 return preview_ui_addr; | |
161 } | 166 } |
162 | 167 |
163 void PrintPreviewUI::OnPrintPreviewTabClosed() { | 168 void PrintPreviewUI::OnPrintPreviewTabClosed() { |
164 TabContents* preview_tab = | 169 TabContents* preview_tab = |
165 TabContents::FromWebContents(web_ui()->GetWebContents()); | 170 TabContents::FromWebContents(web_ui()->GetWebContents()); |
166 printing::BackgroundPrintingManager* background_printing_manager = | 171 printing::BackgroundPrintingManager* background_printing_manager = |
167 g_browser_process->background_printing_manager(); | 172 g_browser_process->background_printing_manager(); |
168 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) | 173 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) |
169 return; | 174 return; |
170 OnClosePrintPreviewTab(); | 175 OnClosePrintPreviewTab(); |
171 } | 176 } |
172 | 177 |
173 void PrintPreviewUI::OnInitiatorTabClosed() { | 178 void PrintPreviewUI::OnInitiatorTabClosed() { |
174 TabContents* preview_tab = | 179 TabContents* preview_tab = |
175 TabContents::FromWebContents(web_ui()->GetWebContents()); | 180 TabContents::FromWebContents(web_ui()->GetWebContents()); |
176 printing::BackgroundPrintingManager* background_printing_manager = | 181 printing::BackgroundPrintingManager* background_printing_manager = |
177 g_browser_process->background_printing_manager(); | 182 g_browser_process->background_printing_manager(); |
178 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) | 183 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) |
179 web_ui()->CallJavascriptFunction("cancelPendingPrintRequest"); | 184 web_ui()->CallJavascriptFunction("cancelPendingPrintRequest"); |
180 else | 185 else |
181 OnClosePrintPreviewTab(); | 186 OnClosePrintPreviewTab(); |
182 } | 187 } |
183 | 188 |
184 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { | 189 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { |
185 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, request_id); | 190 g_print_preview_request_id_map.Get().Set(id_, request_id); |
186 } | 191 } |
187 | 192 |
188 void PrintPreviewUI::OnShowSystemDialog() { | 193 void PrintPreviewUI::OnShowSystemDialog() { |
189 web_ui()->CallJavascriptFunction("onSystemDialogLinkClicked"); | 194 web_ui()->CallJavascriptFunction("onSystemDialogLinkClicked"); |
190 } | 195 } |
191 | 196 |
192 void PrintPreviewUI::OnDidGetPreviewPageCount( | 197 void PrintPreviewUI::OnDidGetPreviewPageCount( |
193 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { | 198 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { |
194 DCHECK_GT(params.page_count, 0); | 199 DCHECK_GT(params.page_count, 0); |
195 base::FundamentalValue count(params.page_count); | 200 base::FundamentalValue count(params.page_count); |
(...skipping 30 matching lines...) Expand all Loading... |
226 | 231 |
227 base::FundamentalValue has_page_size_style(has_custom_page_size_style); | 232 base::FundamentalValue has_page_size_style(has_custom_page_size_style); |
228 web_ui()->CallJavascriptFunction("onDidGetDefaultPageLayout", layout, | 233 web_ui()->CallJavascriptFunction("onDidGetDefaultPageLayout", layout, |
229 has_page_size_style); | 234 has_page_size_style); |
230 } | 235 } |
231 | 236 |
232 void PrintPreviewUI::OnDidPreviewPage(int page_number, | 237 void PrintPreviewUI::OnDidPreviewPage(int page_number, |
233 int preview_request_id) { | 238 int preview_request_id) { |
234 DCHECK_GE(page_number, 0); | 239 DCHECK_GE(page_number, 0); |
235 base::FundamentalValue number(page_number); | 240 base::FundamentalValue number(page_number); |
236 StringValue ui_identifier(preview_ui_addr_str_); | 241 base::FundamentalValue ui_identifier(id_); |
237 base::FundamentalValue request_id(preview_request_id); | 242 base::FundamentalValue request_id(preview_request_id); |
238 web_ui()->CallJavascriptFunction( | 243 web_ui()->CallJavascriptFunction( |
239 "onDidPreviewPage", number, ui_identifier, request_id); | 244 "onDidPreviewPage", number, ui_identifier, request_id); |
240 } | 245 } |
241 | 246 |
242 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { | 247 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { |
243 base::StringValue ui_identifier(preview_ui_addr_str_); | 248 base::FundamentalValue ui_identifier(id_); |
244 base::FundamentalValue ui_preview_request_id(preview_request_id); | 249 base::FundamentalValue ui_preview_request_id(preview_request_id); |
245 web_ui()->CallJavascriptFunction("reloadPreviewPages", ui_identifier, | 250 web_ui()->CallJavascriptFunction("reloadPreviewPages", ui_identifier, |
246 ui_preview_request_id); | 251 ui_preview_request_id); |
247 } | 252 } |
248 | 253 |
249 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, | 254 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, |
250 int preview_request_id) { | 255 int preview_request_id) { |
251 VLOG(1) << "Print preview request finished with " | 256 VLOG(1) << "Print preview request finished with " |
252 << expected_pages_count << " pages"; | 257 << expected_pages_count << " pages"; |
253 | 258 |
254 if (!initial_preview_start_time_.is_null()) { | 259 if (!initial_preview_start_time_.is_null()) { |
255 UMA_HISTOGRAM_TIMES("PrintPreview.InitialDisplayTime", | 260 UMA_HISTOGRAM_TIMES("PrintPreview.InitialDisplayTime", |
256 base::TimeTicks::Now() - initial_preview_start_time_); | 261 base::TimeTicks::Now() - initial_preview_start_time_); |
257 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", | 262 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", |
258 expected_pages_count); | 263 expected_pages_count); |
259 initial_preview_start_time_ = base::TimeTicks(); | 264 initial_preview_start_time_ = base::TimeTicks(); |
260 } | 265 } |
261 base::StringValue ui_identifier(preview_ui_addr_str_); | 266 base::FundamentalValue ui_identifier(id_); |
262 base::FundamentalValue ui_preview_request_id(preview_request_id); | 267 base::FundamentalValue ui_preview_request_id(preview_request_id); |
263 web_ui()->CallJavascriptFunction("updatePrintPreview", ui_identifier, | 268 web_ui()->CallJavascriptFunction("updatePrintPreview", ui_identifier, |
264 ui_preview_request_id); | 269 ui_preview_request_id); |
265 } | 270 } |
266 | 271 |
267 void PrintPreviewUI::OnTabDestroyed() { | 272 void PrintPreviewUI::OnTabDestroyed() { |
268 handler_->OnTabDestroyed(); | 273 handler_->OnTabDestroyed(); |
269 } | 274 } |
270 | 275 |
271 void PrintPreviewUI::OnFileSelectionCancelled() { | 276 void PrintPreviewUI::OnFileSelectionCancelled() { |
272 web_ui()->CallJavascriptFunction("fileSelectionCancelled"); | 277 web_ui()->CallJavascriptFunction("fileSelectionCancelled"); |
273 } | 278 } |
274 | 279 |
275 void PrintPreviewUI::OnCancelPendingPreviewRequest() { | 280 void PrintPreviewUI::OnCancelPendingPreviewRequest() { |
276 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1); | 281 g_print_preview_request_id_map.Get().Set(id_, -1); |
277 } | 282 } |
278 | 283 |
279 void PrintPreviewUI::OnPrintPreviewFailed() { | 284 void PrintPreviewUI::OnPrintPreviewFailed() { |
280 handler_->OnPrintPreviewFailed(); | 285 handler_->OnPrintPreviewFailed(); |
281 web_ui()->CallJavascriptFunction("printPreviewFailed"); | 286 web_ui()->CallJavascriptFunction("printPreviewFailed"); |
282 } | 287 } |
283 | 288 |
284 void PrintPreviewUI::OnInvalidPrinterSettings() { | 289 void PrintPreviewUI::OnInvalidPrinterSettings() { |
285 web_ui()->CallJavascriptFunction("invalidPrinterSettings"); | 290 web_ui()->CallJavascriptFunction("invalidPrinterSettings"); |
286 } | 291 } |
(...skipping 29 matching lines...) Expand all Loading... |
316 delegate->OnDialogCloseFromWebUI(); | 321 delegate->OnDialogCloseFromWebUI(); |
317 } | 322 } |
318 | 323 |
319 void PrintPreviewUI::OnReloadPrintersList() { | 324 void PrintPreviewUI::OnReloadPrintersList() { |
320 web_ui()->CallJavascriptFunction("reloadPrintersList"); | 325 web_ui()->CallJavascriptFunction("reloadPrintersList"); |
321 } | 326 } |
322 | 327 |
323 void PrintPreviewUI::OnPrintPreviewScalingDisabled() { | 328 void PrintPreviewUI::OnPrintPreviewScalingDisabled() { |
324 web_ui()->CallJavascriptFunction("printScalingDisabledForSourcePDF"); | 329 web_ui()->CallJavascriptFunction("printScalingDisabledForSourcePDF"); |
325 } | 330 } |
OLD | NEW |