Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: chrome/browser/ui/webui/print_preview/print_preview_ui.cc

Issue 9224002: Make WebUI objects not derive from WebUI. WebUI objects own the controller. This is the ownership... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync to head to clear linux_chromeos browsertest failures Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/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/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 PrintPreviewRequestIdMap map_; 65 PrintPreviewRequestIdMap map_;
66 base::Lock lock_; 66 base::Lock lock_;
67 }; 67 };
68 68
69 // Written to on the UI thread, read from any thread. 69 // Written to on the UI thread, read from any thread.
70 base::LazyInstance<PrintPreviewRequestIdMapWithLock> 70 base::LazyInstance<PrintPreviewRequestIdMapWithLock>
71 g_print_preview_request_id_map = LAZY_INSTANCE_INITIALIZER; 71 g_print_preview_request_id_map = LAZY_INSTANCE_INITIALIZER;
72 72
73 } // namespace 73 } // namespace
74 74
75 PrintPreviewUI::PrintPreviewUI(WebContents* contents) 75 PrintPreviewUI::PrintPreviewUI(WebUI* web_ui)
76 : ConstrainedHtmlUI(contents), 76 : ConstrainedHtmlUI(web_ui),
77 initial_preview_start_time_(base::TimeTicks::Now()), 77 initial_preview_start_time_(base::TimeTicks::Now()),
78 handler_(NULL), 78 handler_(NULL),
79 source_is_modifiable_(true), 79 source_is_modifiable_(true),
80 tab_closed_(false) { 80 tab_closed_(false) {
81 printing::PrintPreviewTabController* controller = 81 printing::PrintPreviewTabController* controller =
82 printing::PrintPreviewTabController::GetInstance(); 82 printing::PrintPreviewTabController::GetInstance();
83 is_dummy_ = (!controller || !controller->is_creating_print_preview_tab()); 83 is_dummy_ = (!controller || !controller->is_creating_print_preview_tab());
84 84
85 // Set up the chrome://print/ data source. 85 // Set up the chrome://print/ data source.
86 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 86 Profile* profile = Profile::FromBrowserContext(
87 web_ui->web_contents()->GetBrowserContext());
87 profile->GetChromeURLDataManager()->AddDataSource( 88 profile->GetChromeURLDataManager()->AddDataSource(
88 new PrintPreviewDataSource(is_dummy_)); 89 new PrintPreviewDataSource(is_dummy_));
89 if (is_dummy_) 90 if (is_dummy_)
90 return; 91 return;
91 92
92 // WebUI owns |handler_|. 93 // WebUI owns |handler_|.
93 handler_ = new PrintPreviewHandler(); 94 handler_ = new PrintPreviewHandler();
94 AddMessageHandler(handler_); 95 web_ui->AddMessageHandler(handler_);
95 96
96 preview_ui_addr_str_ = GetPrintPreviewUIAddress(); 97 preview_ui_addr_str_ = GetPrintPreviewUIAddress();
97 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1); 98 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1);
98 } 99 }
99 100
100 PrintPreviewUI::~PrintPreviewUI() { 101 PrintPreviewUI::~PrintPreviewUI() {
101 if (is_dummy_) 102 if (is_dummy_)
102 return; 103 return;
103 104
104 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); 105 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_);
(...skipping 27 matching lines...) Expand all
132 initiator_tab_title_ = job_title; 133 initiator_tab_title_ = job_title;
133 } 134 }
134 135
135 // static 136 // static
136 void PrintPreviewUI::SetSourceIsModifiable( 137 void PrintPreviewUI::SetSourceIsModifiable(
137 TabContentsWrapper* print_preview_tab, 138 TabContentsWrapper* print_preview_tab,
138 bool source_is_modifiable) { 139 bool source_is_modifiable) {
139 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI()) 140 if (!print_preview_tab || !print_preview_tab->web_contents()->GetWebUI())
140 return; 141 return;
141 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( 142 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
142 print_preview_tab->web_contents()->GetWebUI()); 143 print_preview_tab->web_contents()->GetWebUI()->GetController());
143 print_preview_ui->source_is_modifiable_ = source_is_modifiable; 144 print_preview_ui->source_is_modifiable_ = source_is_modifiable;
144 } 145 }
145 146
146 // static 147 // static
147 void PrintPreviewUI::GetCurrentPrintPreviewStatus( 148 void PrintPreviewUI::GetCurrentPrintPreviewStatus(
148 const std::string& preview_ui_addr, 149 const std::string& preview_ui_addr,
149 int request_id, 150 int request_id,
150 bool* cancel) { 151 bool* cancel) {
151 int current_id = -1; 152 int current_id = -1;
152 if (!g_print_preview_request_id_map.Get().Get(preview_ui_addr, &current_id)) { 153 if (!g_print_preview_request_id_map.Get().Get(preview_ui_addr, &current_id)) {
153 *cancel = true; 154 *cancel = true;
154 return; 155 return;
155 } 156 }
156 *cancel = (request_id != current_id); 157 *cancel = (request_id != current_id);
157 } 158 }
158 159
159 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const { 160 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const {
160 // Store the PrintPreviewUIAddress as a string. 161 // Store the PrintPreviewUIAddress as a string.
161 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; 162 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1;
162 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; 163 char preview_ui_addr[2 + (2 * sizeof(this)) + 1];
163 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); 164 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this);
164 return preview_ui_addr; 165 return preview_ui_addr;
165 } 166 }
166 167
167 void PrintPreviewUI::OnPrintPreviewTabClosed() { 168 void PrintPreviewUI::OnPrintPreviewTabClosed() {
168 TabContentsWrapper* preview_tab = 169 TabContentsWrapper* preview_tab =
169 TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); 170 TabContentsWrapper::GetCurrentWrapperForContents(
171 web_ui()->web_contents());
170 printing::BackgroundPrintingManager* background_printing_manager = 172 printing::BackgroundPrintingManager* background_printing_manager =
171 g_browser_process->background_printing_manager(); 173 g_browser_process->background_printing_manager();
172 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) 174 if (background_printing_manager->HasPrintPreviewTab(preview_tab))
173 return; 175 return;
174 OnClosePrintPreviewTab(); 176 OnClosePrintPreviewTab();
175 } 177 }
176 178
177 void PrintPreviewUI::OnInitiatorTabClosed() { 179 void PrintPreviewUI::OnInitiatorTabClosed() {
178 TabContentsWrapper* preview_tab = 180 TabContentsWrapper* preview_tab =
179 TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); 181 TabContentsWrapper::GetCurrentWrapperForContents(
182 web_ui()->web_contents());
180 printing::BackgroundPrintingManager* background_printing_manager = 183 printing::BackgroundPrintingManager* background_printing_manager =
181 g_browser_process->background_printing_manager(); 184 g_browser_process->background_printing_manager();
182 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) 185 if (background_printing_manager->HasPrintPreviewTab(preview_tab))
183 CallJavascriptFunction("cancelPendingPrintRequest"); 186 web_ui()->CallJavascriptFunction("cancelPendingPrintRequest");
184 else 187 else
185 OnClosePrintPreviewTab(); 188 OnClosePrintPreviewTab();
186 } 189 }
187 190
188 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { 191 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) {
189 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, request_id); 192 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, request_id);
190 } 193 }
191 194
192 void PrintPreviewUI::OnShowSystemDialog() { 195 void PrintPreviewUI::OnShowSystemDialog() {
193 CallJavascriptFunction("onSystemDialogLinkClicked"); 196 web_ui()->CallJavascriptFunction("onSystemDialogLinkClicked");
194 } 197 }
195 198
196 void PrintPreviewUI::OnDidGetPreviewPageCount( 199 void PrintPreviewUI::OnDidGetPreviewPageCount(
197 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { 200 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
198 DCHECK_GT(params.page_count, 0); 201 DCHECK_GT(params.page_count, 0);
199 base::FundamentalValue count(params.page_count); 202 base::FundamentalValue count(params.page_count);
200 base::FundamentalValue request_id(params.preview_request_id); 203 base::FundamentalValue request_id(params.preview_request_id);
201 CallJavascriptFunction("onDidGetPreviewPageCount", count, request_id); 204 web_ui()->CallJavascriptFunction("onDidGetPreviewPageCount", count, request_id );
202 } 205 }
203 206
204 void PrintPreviewUI::OnDidGetDefaultPageLayout( 207 void PrintPreviewUI::OnDidGetDefaultPageLayout(
205 const PageSizeMargins& page_layout, bool has_custom_page_size_style) { 208 const PageSizeMargins& page_layout, bool has_custom_page_size_style) {
206 if (page_layout.margin_top < 0 || page_layout.margin_left < 0 || 209 if (page_layout.margin_top < 0 || page_layout.margin_left < 0 ||
207 page_layout.margin_bottom < 0 || page_layout.margin_right < 0 || 210 page_layout.margin_bottom < 0 || page_layout.margin_right < 0 ||
208 page_layout.content_width < 0 || page_layout.content_height < 0) { 211 page_layout.content_width < 0 || page_layout.content_height < 0) {
209 NOTREACHED(); 212 NOTREACHED();
210 return; 213 return;
211 } 214 }
212 215
213 base::DictionaryValue layout; 216 base::DictionaryValue layout;
214 layout.SetDouble(printing::kSettingMarginTop, page_layout.margin_top); 217 layout.SetDouble(printing::kSettingMarginTop, page_layout.margin_top);
215 layout.SetDouble(printing::kSettingMarginLeft, page_layout.margin_left); 218 layout.SetDouble(printing::kSettingMarginLeft, page_layout.margin_left);
216 layout.SetDouble(printing::kSettingMarginBottom, page_layout.margin_bottom); 219 layout.SetDouble(printing::kSettingMarginBottom, page_layout.margin_bottom);
217 layout.SetDouble(printing::kSettingMarginRight, page_layout.margin_right); 220 layout.SetDouble(printing::kSettingMarginRight, page_layout.margin_right);
218 layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width); 221 layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width);
219 layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height); 222 layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height);
220 223
221 base::FundamentalValue has_page_size_style(has_custom_page_size_style); 224 base::FundamentalValue has_page_size_style(has_custom_page_size_style);
222 CallJavascriptFunction("onDidGetDefaultPageLayout", layout, 225 web_ui()->CallJavascriptFunction("onDidGetDefaultPageLayout", layout,
223 has_page_size_style); 226 has_page_size_style);
224 } 227 }
225 228
226 void PrintPreviewUI::OnDidPreviewPage(int page_number, 229 void PrintPreviewUI::OnDidPreviewPage(int page_number,
227 int preview_request_id) { 230 int preview_request_id) {
228 DCHECK_GE(page_number, 0); 231 DCHECK_GE(page_number, 0);
229 base::FundamentalValue number(page_number); 232 base::FundamentalValue number(page_number);
230 StringValue ui_identifier(preview_ui_addr_str_); 233 StringValue ui_identifier(preview_ui_addr_str_);
231 base::FundamentalValue request_id(preview_request_id); 234 base::FundamentalValue request_id(preview_request_id);
232 CallJavascriptFunction("onDidPreviewPage", number, ui_identifier, request_id); 235 web_ui()->CallJavascriptFunction(
236 "onDidPreviewPage", number, ui_identifier, request_id);
233 } 237 }
234 238
235 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { 239 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) {
236 base::StringValue ui_identifier(preview_ui_addr_str_); 240 base::StringValue ui_identifier(preview_ui_addr_str_);
237 base::FundamentalValue ui_preview_request_id(preview_request_id); 241 base::FundamentalValue ui_preview_request_id(preview_request_id);
238 CallJavascriptFunction("reloadPreviewPages", ui_identifier, 242 web_ui()->CallJavascriptFunction("reloadPreviewPages", ui_identifier,
239 ui_preview_request_id); 243 ui_preview_request_id);
240 } 244 }
241 245
242 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, 246 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count,
243 int preview_request_id) { 247 int preview_request_id) {
244 VLOG(1) << "Print preview request finished with " 248 VLOG(1) << "Print preview request finished with "
245 << expected_pages_count << " pages"; 249 << expected_pages_count << " pages";
246 250
247 if (!initial_preview_start_time_.is_null()) { 251 if (!initial_preview_start_time_.is_null()) {
248 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime", 252 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime",
249 base::TimeTicks::Now() - initial_preview_start_time_); 253 base::TimeTicks::Now() - initial_preview_start_time_);
250 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", 254 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial",
251 expected_pages_count); 255 expected_pages_count);
252 initial_preview_start_time_ = base::TimeTicks(); 256 initial_preview_start_time_ = base::TimeTicks();
253 } 257 }
254 base::StringValue ui_identifier(preview_ui_addr_str_); 258 base::StringValue ui_identifier(preview_ui_addr_str_);
255 base::FundamentalValue ui_preview_request_id(preview_request_id); 259 base::FundamentalValue ui_preview_request_id(preview_request_id);
256 CallJavascriptFunction("updatePrintPreview", ui_identifier, 260 web_ui()->CallJavascriptFunction("updatePrintPreview", ui_identifier,
257 ui_preview_request_id); 261 ui_preview_request_id);
258 } 262 }
259 263
260 void PrintPreviewUI::OnTabDestroyed() { 264 void PrintPreviewUI::OnTabDestroyed() {
261 handler_->OnTabDestroyed(); 265 handler_->OnTabDestroyed();
262 } 266 }
263 267
264 void PrintPreviewUI::OnFileSelectionCancelled() { 268 void PrintPreviewUI::OnFileSelectionCancelled() {
265 CallJavascriptFunction("fileSelectionCancelled"); 269 web_ui()->CallJavascriptFunction("fileSelectionCancelled");
266 } 270 }
267 271
268 void PrintPreviewUI::OnCancelPendingPreviewRequest() { 272 void PrintPreviewUI::OnCancelPendingPreviewRequest() {
269 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1); 273 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1);
270 } 274 }
271 275
272 void PrintPreviewUI::OnPrintPreviewFailed() { 276 void PrintPreviewUI::OnPrintPreviewFailed() {
273 handler_->OnPrintPreviewFailed(); 277 handler_->OnPrintPreviewFailed();
274 CallJavascriptFunction("printPreviewFailed"); 278 web_ui()->CallJavascriptFunction("printPreviewFailed");
275 } 279 }
276 280
277 void PrintPreviewUI::OnInvalidPrinterSettings() { 281 void PrintPreviewUI::OnInvalidPrinterSettings() {
278 CallJavascriptFunction("invalidPrinterSettings"); 282 web_ui()->CallJavascriptFunction("invalidPrinterSettings");
279 } 283 }
280 284
281 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { 285 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() {
282 return PrintPreviewDataService::GetInstance(); 286 return PrintPreviewDataService::GetInstance();
283 } 287 }
284 288
285 void PrintPreviewUI::OnHidePreviewTab() { 289 void PrintPreviewUI::OnHidePreviewTab() {
286 TabContentsWrapper* preview_tab = 290 TabContentsWrapper* preview_tab =
287 TabContentsWrapper::GetCurrentWrapperForContents(web_contents()); 291 TabContentsWrapper::GetCurrentWrapperForContents(
292 web_ui()->web_contents());
288 printing::BackgroundPrintingManager* background_printing_manager = 293 printing::BackgroundPrintingManager* background_printing_manager =
289 g_browser_process->background_printing_manager(); 294 g_browser_process->background_printing_manager();
290 if (background_printing_manager->HasPrintPreviewTab(preview_tab)) 295 if (background_printing_manager->HasPrintPreviewTab(preview_tab))
291 return; 296 return;
292 297
293 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); 298 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate();
294 if (!delegate) 299 if (!delegate)
295 return; 300 return;
296 delegate->ReleaseTabContentsOnDialogClose(); 301 delegate->ReleaseTabContentsOnDialogClose();
297 background_printing_manager->OwnPrintPreviewTab(preview_tab); 302 background_printing_manager->OwnPrintPreviewTab(preview_tab);
298 OnClosePrintPreviewTab(); 303 OnClosePrintPreviewTab();
299 } 304 }
300 305
301 void PrintPreviewUI::OnClosePrintPreviewTab() { 306 void PrintPreviewUI::OnClosePrintPreviewTab() {
302 if (tab_closed_) 307 if (tab_closed_)
303 return; 308 return;
304 tab_closed_ = true; 309 tab_closed_ = true;
305 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); 310 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate();
306 if (!delegate) 311 if (!delegate)
307 return; 312 return;
308 delegate->GetHtmlDialogUIDelegate()->OnDialogClosed(""); 313 delegate->GetHtmlDialogUIDelegate()->OnDialogClosed("");
309 delegate->OnDialogCloseFromWebUI(); 314 delegate->OnDialogCloseFromWebUI();
310 } 315 }
311 316
312 void PrintPreviewUI::OnReloadPrintersList() { 317 void PrintPreviewUI::OnReloadPrintersList() {
313 CallJavascriptFunction("reloadPrintersList"); 318 web_ui()->CallJavascriptFunction("reloadPrintersList");
314 } 319 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698