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

Side by Side Diff: chrome/browser/ui/webui/set_as_default_browser_ui_win.cc

Issue 2090773002: Improves first run dialog performance on Win8 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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
OLDNEW
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/set_as_default_browser_ui.h" 5 #include "chrome/browser/ui/webui/set_as_default_browser_ui_win.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
(...skipping 19 matching lines...) Expand all
35 #include "content/public/browser/browser_thread.h" 35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/browser/web_contents.h" 36 #include "content/public/browser/web_contents.h"
37 #include "content/public/browser/web_contents_delegate.h" 37 #include "content/public/browser/web_contents_delegate.h"
38 #include "content/public/browser/web_ui.h" 38 #include "content/public/browser/web_ui.h"
39 #include "content/public/browser/web_ui_data_source.h" 39 #include "content/public/browser/web_ui_data_source.h"
40 #include "content/public/browser/web_ui_message_handler.h" 40 #include "content/public/browser/web_ui_message_handler.h"
41 #include "grit/browser_resources.h" 41 #include "grit/browser_resources.h"
42 #include "ui/base/l10n/l10n_font_util.h" 42 #include "ui/base/l10n/l10n_font_util.h"
43 #include "ui/base/l10n/l10n_util.h" 43 #include "ui/base/l10n/l10n_util.h"
44 #include "ui/gfx/font.h" 44 #include "ui/gfx/font.h"
45 #include "ui/views/widget/widget.h"
46 #include "ui/web_dialogs/web_dialog_delegate.h" 45 #include "ui/web_dialogs/web_dialog_delegate.h"
47 46
48 using content::BrowserThread; 47 using content::BrowserThread;
49 using content::WebContents; 48 using content::WebContents;
50 using content::WebUIMessageHandler; 49 using content::WebUIMessageHandler;
51 50
52 namespace { 51 namespace {
53 52
54 const char kSetAsDefaultBrowserHistogram[] = "DefaultBrowser.InteractionResult"; 53 const char kSetAsDefaultBrowserHistogram[] = "DefaultBrowser.InteractionResult";
55 54
56 // The enum permits registering in UMA the three possible outcomes (do not 55 // The enum permits registering in UMA the three possible outcomes (do not
57 // reorder these). 56 // reorder these).
58 // ACCEPTED: user pressed Next and made Chrome default. 57 // ACCEPTED: user pressed Next and made Chrome default.
59 // DECLINED: user simply closed the dialog without making Chrome default. 58 // DECLINED: user simply closed the dialog without making Chrome default.
60 // REGRETTED: user pressed Next but then elected a different default browser. 59 // REGRETTED: user pressed Next but then elected a different default browser.
61 enum MakeChromeDefaultResult { 60 enum MakeChromeDefaultResult {
62 MAKE_CHROME_DEFAULT_ACCEPTED = 0, 61 MAKE_CHROME_DEFAULT_ACCEPTED = 0,
63 MAKE_CHROME_DEFAULT_DECLINED = 1, 62 MAKE_CHROME_DEFAULT_DECLINED = 1,
64 MAKE_CHROME_DEFAULT_REGRETTED = 2, 63 MAKE_CHROME_DEFAULT_REGRETTED = 2,
65 // MAKE_CHROME_DEFAULT_ACCEPTED_IMMERSE = 3, // Deprecated. 64 // MAKE_CHROME_DEFAULT_ACCEPTED_IMMERSE = 3, // Deprecated.
66 MAKE_CHROME_DEFAULT_MAX 65 MAKE_CHROME_DEFAULT_MAX
67 }; 66 };
68 67
69 content::WebUIDataSource* CreateSetAsDefaultBrowserUIHTMLSource() { 68 content::WebUIDataSource* CreateSetAsDefaultBrowserUIHTMLSource() {
70 content::WebUIDataSource* data_source = content::WebUIDataSource::Create( 69 content::WebUIDataSource* data_source =
71 chrome::kChromeUIMetroFlowHost); 70 content::WebUIDataSource::Create(chrome::kChromeUIMetroFlowHost);
72 data_source->AddLocalizedString("page-title", IDS_METRO_FLOW_TAB_TITLE); 71 data_source->AddLocalizedString("page-title", IDS_METRO_FLOW_TAB_TITLE);
73 data_source->AddLocalizedString("flowTitle", IDS_METRO_FLOW_TITLE_SHORT); 72 data_source->AddLocalizedString("flowTitle", IDS_METRO_FLOW_TITLE_SHORT);
74 data_source->AddLocalizedString("flowDescription", 73 data_source->AddLocalizedString("flowDescription",
75 IDS_METRO_FLOW_DESCRIPTION); 74 IDS_METRO_FLOW_DESCRIPTION);
76 data_source->AddLocalizedString("flowNext", 75 data_source->AddLocalizedString("flowNext", IDS_METRO_FLOW_SET_DEFAULT);
77 IDS_METRO_FLOW_SET_DEFAULT);
78 data_source->AddLocalizedString("chromeLogoString", 76 data_source->AddLocalizedString("chromeLogoString",
79 IDS_METRO_FLOW_LOGO_STRING_ALT); 77 IDS_METRO_FLOW_LOGO_STRING_ALT);
80 data_source->SetJsonPath("strings.js"); 78 data_source->SetJsonPath("strings.js");
81 data_source->AddResourcePath("set_as_default_browser.js", 79 data_source->AddResourcePath("set_as_default_browser.js",
82 IDR_SET_AS_DEFAULT_BROWSER_JS); 80 IDR_SET_AS_DEFAULT_BROWSER_JS);
83 data_source->SetDefaultResource(IDR_SET_AS_DEFAULT_BROWSER_HTML); 81 data_source->SetDefaultResource(IDR_SET_AS_DEFAULT_BROWSER_HTML);
84 return data_source; 82 return data_source;
85 } 83 }
86 84
87 // A simple class serving as a delegate for passing down the result of the 85 // A simple class serving as a delegate for passing down the result of the
88 // interaction. 86 // interaction.
89 class ResponseDelegate { 87 class ResponseDelegate {
90 public: 88 public:
91 virtual void SetDialogInteractionResult(MakeChromeDefaultResult result) = 0; 89 virtual void SetDialogInteractionResult(MakeChromeDefaultResult result) = 0;
92 90
93 protected: 91 protected:
94 virtual ~ResponseDelegate() { } 92 virtual ~ResponseDelegate() {}
95 }; 93 };
96 94
97 // Event handler for SetAsDefaultBrowserUI. Capable of setting Chrome as the 95 // Event handler for SetAsDefaultBrowserUI. Capable of setting Chrome as the
98 // default browser on button click, closing itself and triggering Chrome 96 // default browser on button click, closing itself and triggering Chrome
99 // restart. 97 // restart.
100 class SetAsDefaultBrowserHandler : public WebUIMessageHandler { 98 class SetAsDefaultBrowserHandler : public WebUIMessageHandler {
101 public: 99 public:
102 explicit SetAsDefaultBrowserHandler( 100 explicit SetAsDefaultBrowserHandler(
103 const base::WeakPtr<ResponseDelegate>& response_delegate); 101 const base::WeakPtr<ResponseDelegate>& response_delegate);
104 102
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 // Otherwise, keep the dialog open since the user probably didn't make a 177 // Otherwise, keep the dialog open since the user probably didn't make a
180 // choice. 178 // choice.
181 } 179 }
182 180
183 // A web dialog delegate implementation for when 'Make Chrome Metro' UI 181 // A web dialog delegate implementation for when 'Make Chrome Metro' UI
184 // is displayed on a dialog. 182 // is displayed on a dialog.
185 class SetAsDefaultBrowserDialogImpl : public ui::WebDialogDelegate, 183 class SetAsDefaultBrowserDialogImpl : public ui::WebDialogDelegate,
186 public ResponseDelegate, 184 public ResponseDelegate,
187 public chrome::BrowserListObserver { 185 public chrome::BrowserListObserver {
188 public: 186 public:
189 SetAsDefaultBrowserDialogImpl(Profile* profile, Browser* browser); 187 explicit SetAsDefaultBrowserDialogImpl(Profile* profile);
190 ~SetAsDefaultBrowserDialogImpl() override; 188 ~SetAsDefaultBrowserDialogImpl() override;
191 // Show a modal web dialog with kChromeUIMetroFlowURL page. 189 // Show a modal web dialog with kChromeUIMetroFlowURL page.
192 void ShowDialog(); 190 void ShowDialog();
191 static views::Widget* dialog_widget() { return dialog_widget_; }
193 192
194 protected: 193 protected:
195 // Overridden from WebDialogDelegate: 194 // Overridden from WebDialogDelegate:
196 ui::ModalType GetDialogModalType() const override; 195 ui::ModalType GetDialogModalType() const override;
197 base::string16 GetDialogTitle() const override; 196 base::string16 GetDialogTitle() const override;
198 GURL GetDialogContentURL() const override; 197 GURL GetDialogContentURL() const override;
199 void GetWebUIMessageHandlers( 198 void GetWebUIMessageHandlers(
200 std::vector<WebUIMessageHandler*>* handlers) const override; 199 std::vector<WebUIMessageHandler*>* handlers) const override;
201 void GetDialogSize(gfx::Size* size) const override; 200 void GetDialogSize(gfx::Size* size) const override;
202 std::string GetDialogArgs() const override; 201 std::string GetDialogArgs() const override;
203 void OnDialogClosed(const std::string& json_retval) override; 202 void OnDialogClosed(const std::string& json_retval) override;
204 void OnCloseContents(WebContents* source, bool* out_close_dialog) override; 203 void OnCloseContents(WebContents* source, bool* out_close_dialog) override;
205 bool ShouldShowDialogTitle() const override; 204 bool ShouldShowDialogTitle() const override;
206 bool HandleContextMenu(const content::ContextMenuParams& params) override; 205 bool HandleContextMenu(const content::ContextMenuParams& params) override;
207 206
208 // Overridden from ResponseDelegate: 207 // Overridden from ResponseDelegate:
209 void SetDialogInteractionResult(MakeChromeDefaultResult result) override; 208 void SetDialogInteractionResult(MakeChromeDefaultResult result) override;
210 209
211 // Overridden from BrowserListObserver: 210 // Overridden from BrowserListObserver:
211 void OnBrowserAdded(Browser* browser) override;
212
grt (UTC plus 2) 2016/07/19 08:13:01 remove blank line and second comment so that the c
zmin 2016/07/20 16:35:45 Done.
213 // Overridden from BrowserListObserver:
212 void OnBrowserRemoved(Browser* browser) override; 214 void OnBrowserRemoved(Browser* browser) override;
213 215
214 private: 216 private:
215 Profile* profile_; 217 Profile* profile_;
216 Browser* browser_; 218 Browser* browser_;
217 mutable bool owns_handler_; 219 mutable bool owns_handler_;
218 base::WeakPtrFactory<ResponseDelegate> response_delegate_ptr_factory_; 220 base::WeakPtrFactory<ResponseDelegate> response_delegate_ptr_factory_;
219 SetAsDefaultBrowserHandler* handler_; 221 SetAsDefaultBrowserHandler* handler_;
220 MakeChromeDefaultResult dialog_interaction_result_; 222 MakeChromeDefaultResult dialog_interaction_result_;
221 223
224 static views::Widget* dialog_widget_;
225
222 DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserDialogImpl); 226 DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserDialogImpl);
223 }; 227 };
224 228
225 SetAsDefaultBrowserDialogImpl::SetAsDefaultBrowserDialogImpl(Profile* profile, 229 views::Widget* SetAsDefaultBrowserDialogImpl::dialog_widget_ = nullptr;
226 Browser* browser) 230
231 SetAsDefaultBrowserDialogImpl::SetAsDefaultBrowserDialogImpl(Profile* profile)
227 : profile_(profile), 232 : profile_(profile),
228 browser_(browser), 233 browser_(nullptr),
229 owns_handler_(true), 234 owns_handler_(true),
230 response_delegate_ptr_factory_(this), 235 response_delegate_ptr_factory_(this),
231 handler_(new SetAsDefaultBrowserHandler( 236 handler_(new SetAsDefaultBrowserHandler(
232 response_delegate_ptr_factory_.GetWeakPtr())), 237 response_delegate_ptr_factory_.GetWeakPtr())),
233 dialog_interaction_result_(MAKE_CHROME_DEFAULT_DECLINED) { 238 dialog_interaction_result_(MAKE_CHROME_DEFAULT_DECLINED) {
234 BrowserList::AddObserver(this); 239 BrowserList::AddObserver(this);
235 } 240 }
236 241
237 SetAsDefaultBrowserDialogImpl::~SetAsDefaultBrowserDialogImpl() { 242 SetAsDefaultBrowserDialogImpl::~SetAsDefaultBrowserDialogImpl() {
238 if (browser_) 243 if (browser_)
239 BrowserList::RemoveObserver(this); 244 BrowserList::RemoveObserver(this);
240 if (owns_handler_) 245 if (owns_handler_)
241 delete handler_; 246 delete handler_;
242 } 247 }
243 248
244 void SetAsDefaultBrowserDialogImpl::ShowDialog() { 249 void SetAsDefaultBrowserDialogImpl::ShowDialog() {
245 // Use a NULL parent window to make sure that the dialog will have an item 250 // Use a NULL parent window to make sure that the dialog will have an item
246 // in the Windows task bar. The code below will make it highlight if the 251 // in the Windows task bar. The code below will make it highlight if the
247 // dialog is not in the foreground. 252 // dialog is not in the foreground.
248 gfx::NativeWindow native_window = chrome::ShowWebDialog(NULL, profile_, this); 253 gfx::NativeWindow native_window = chrome::ShowWebDialog(NULL, profile_, this);
249 views::Widget* widget = views::Widget::GetWidgetForNativeWindow( 254 DCHECK(!dialog_widget_);
250 native_window); 255 dialog_widget_ = views::Widget::GetWidgetForNativeWindow(native_window);
251 widget->FlashFrame(true); 256 dialog_widget_->FlashFrame(true);
252 } 257 }
253 258
254 ui::ModalType SetAsDefaultBrowserDialogImpl::GetDialogModalType() const { 259 ui::ModalType SetAsDefaultBrowserDialogImpl::GetDialogModalType() const {
255 return ui::MODAL_TYPE_SYSTEM; 260 return ui::MODAL_TYPE_SYSTEM;
256 } 261 }
257 262
258 base::string16 SetAsDefaultBrowserDialogImpl::GetDialogTitle() const { 263 base::string16 SetAsDefaultBrowserDialogImpl::GetDialogTitle() const {
259 return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE); 264 return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE);
260 } 265 }
261 266
262 GURL SetAsDefaultBrowserDialogImpl::GetDialogContentURL() const { 267 GURL SetAsDefaultBrowserDialogImpl::GetDialogContentURL() const {
263 std::string url_string(chrome::kChromeUIMetroFlowURL); 268 std::string url_string(chrome::kChromeUIMetroFlowURL);
264 return GURL(url_string); 269 return GURL(url_string);
265 } 270 }
266 271
267 void SetAsDefaultBrowserDialogImpl::GetWebUIMessageHandlers( 272 void SetAsDefaultBrowserDialogImpl::GetWebUIMessageHandlers(
268 std::vector<WebUIMessageHandler*>* handlers) const { 273 std::vector<WebUIMessageHandler*>* handlers) const {
269 handlers->push_back(handler_); 274 handlers->push_back(handler_);
270 owns_handler_ = false; 275 owns_handler_ = false;
271 } 276 }
272 277
273 void SetAsDefaultBrowserDialogImpl::GetDialogSize(gfx::Size* size) const { 278 void SetAsDefaultBrowserDialogImpl::GetDialogSize(gfx::Size* size) const {
274 PrefService* prefs = profile_->GetPrefs(); 279 PrefService* prefs = profile_->GetPrefs();
275 gfx::Font approximate_web_font( 280 gfx::Font approximate_web_font(
276 prefs->GetString(prefs::kWebKitSansSerifFontFamily), 281 prefs->GetString(prefs::kWebKitSansSerifFontFamily),
277 prefs->GetInteger(prefs::kWebKitDefaultFontSize)); 282 prefs->GetInteger(prefs::kWebKitDefaultFontSize));
278 283
279 *size = ui::GetLocalizedContentsSizeForFont( 284 *size = ui::GetLocalizedContentsSizeForFont(IDS_METRO_FLOW_WIDTH_CHARS,
280 IDS_METRO_FLOW_WIDTH_CHARS, IDS_METRO_FLOW_HEIGHT_LINES, 285 IDS_METRO_FLOW_HEIGHT_LINES,
281 approximate_web_font); 286 approximate_web_font);
282 } 287 }
283 288
284 std::string SetAsDefaultBrowserDialogImpl::GetDialogArgs() const { 289 std::string SetAsDefaultBrowserDialogImpl::GetDialogArgs() const {
285 return "[]"; 290 return "[]";
286 } 291 }
287 292
288 void SetAsDefaultBrowserDialogImpl::OnDialogClosed( 293 void SetAsDefaultBrowserDialogImpl::OnDialogClosed(
289 const std::string& json_retval) { 294 const std::string& json_retval) {
290 // Register the user's response in UMA. 295 // Register the user's response in UMA.
291 UMA_HISTOGRAM_ENUMERATION(kSetAsDefaultBrowserHistogram, 296 UMA_HISTOGRAM_ENUMERATION(kSetAsDefaultBrowserHistogram,
292 dialog_interaction_result_, 297 dialog_interaction_result_,
293 MAKE_CHROME_DEFAULT_MAX); 298 MAKE_CHROME_DEFAULT_MAX);
294 299
295 // Suppress showing the default browser infobar if the user explicitly elected 300 // Suppress showing the default browser infobar if the user explicitly elected
296 // *not to* make Chrome default. 301 // *not to* make Chrome default.
297 if (dialog_interaction_result_ == MAKE_CHROME_DEFAULT_REGRETTED) 302 if (dialog_interaction_result_ == MAKE_CHROME_DEFAULT_REGRETTED)
298 chrome::DefaultBrowserPromptDeclined(profile_); 303 chrome::DefaultBrowserPromptDeclined(profile_);
299 304
300 // Carry on with a normal chrome session. For the purpose of surfacing this 305 // Carry on with a normal chrome session. For the purpose of surfacing this
301 // dialog the actual browser window had to remain hidden. Now it's time to 306 // dialog the actual browser window had to remain hidden. Now it's time to
302 // show it. 307 // show it.
303 if (browser_) { 308 if (browser_) {
304 BrowserWindow* window = browser_->window(); 309 BrowserWindow* window = browser_->window();
305 WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents(); 310 WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents();
306 window->Show(); 311 window->Show();
307 if (contents) 312 if (contents)
308 contents->SetInitialFocus(); 313 contents->SetInitialFocus();
309 } 314 }
310 315
316 DCHECK(!dialog_widget_);
grt (UTC plus 2) 2016/07/19 08:13:01 shouldn't this be DCHECK(dialog_widget_);?
zmin 2016/07/20 16:35:45 Done.
317 dialog_widget_ = nullptr;
318
311 delete this; 319 delete this;
312 } 320 }
313 321
314 void SetAsDefaultBrowserDialogImpl::OnCloseContents(WebContents* source, 322 void SetAsDefaultBrowserDialogImpl::OnCloseContents(WebContents* source,
315 bool* out_close_dialog) { 323 bool* out_close_dialog) {
316 *out_close_dialog = true; 324 *out_close_dialog = true;
317 } 325 }
318 326
319 bool SetAsDefaultBrowserDialogImpl::ShouldShowDialogTitle() const { 327 bool SetAsDefaultBrowserDialogImpl::ShouldShowDialogTitle() const {
320 return true; 328 return true;
321 } 329 }
322 330
323 bool SetAsDefaultBrowserDialogImpl::HandleContextMenu( 331 bool SetAsDefaultBrowserDialogImpl::HandleContextMenu(
324 const content::ContextMenuParams& params) { 332 const content::ContextMenuParams& params) {
325 return true; 333 return true;
326 } 334 }
327 335
328 void SetAsDefaultBrowserDialogImpl::SetDialogInteractionResult( 336 void SetAsDefaultBrowserDialogImpl::SetDialogInteractionResult(
329 MakeChromeDefaultResult result) { 337 MakeChromeDefaultResult result) {
330 dialog_interaction_result_ = result; 338 dialog_interaction_result_ = result;
331 } 339 }
332 340
341 void SetAsDefaultBrowserDialogImpl::OnBrowserAdded(Browser* browser) {
342 if (browser_ || !browser || !browser->is_type_tabbed())
343 return;
344 browser_ = browser;
345 ShowDialog();
346 }
347
333 void SetAsDefaultBrowserDialogImpl::OnBrowserRemoved(Browser* browser) { 348 void SetAsDefaultBrowserDialogImpl::OnBrowserRemoved(Browser* browser) {
334 if (browser_ == browser) { 349 if (browser_ == browser) {
335 browser_ = NULL; 350 browser_ = NULL;
336 BrowserList::RemoveObserver(this); 351 BrowserList::RemoveObserver(this);
337 } 352 }
338 } 353 }
339 354
340 } // namespace 355 } // namespace
341 356
342 SetAsDefaultBrowserUI::SetAsDefaultBrowserUI(content::WebUI* web_ui) 357 SetAsDefaultBrowserUI::SetAsDefaultBrowserUI(content::WebUI* web_ui)
343 : ui::WebDialogUI(web_ui) { 358 : ui::WebDialogUI(web_ui) {
344 content::WebUIDataSource::Add( 359 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui),
345 Profile::FromWebUI(web_ui), CreateSetAsDefaultBrowserUIHTMLSource()); 360 CreateSetAsDefaultBrowserUIHTMLSource());
346 } 361 }
347 362
348 // static 363 // static
349 void SetAsDefaultBrowserUI::Show(Profile* profile, Browser* browser) { 364 void SetAsDefaultBrowserUI::Show(Profile* profile) {
350 DCHECK_CURRENTLY_ON(BrowserThread::UI); 365 DCHECK_CURRENTLY_ON(BrowserThread::UI);
351 SetAsDefaultBrowserDialogImpl* dialog = 366 new SetAsDefaultBrowserDialogImpl(profile);
352 new SetAsDefaultBrowserDialogImpl(profile, browser);
353 dialog->ShowDialog();
354 } 367 }
368
369 // static
370 views::Widget* SetAsDefaultBrowserUI::GetDialogWidgetForTesting() {
371 return SetAsDefaultBrowserDialogImpl::dialog_widget();
372 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698