Chromium Code Reviews| 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/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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 // REGRETTED: user pressed Next but then elected a different default browser. | 60 // REGRETTED: user pressed Next but then elected a different default browser. |
| 61 enum MakeChromeDefaultResult { | 61 enum MakeChromeDefaultResult { |
| 62 MAKE_CHROME_DEFAULT_ACCEPTED = 0, | 62 MAKE_CHROME_DEFAULT_ACCEPTED = 0, |
| 63 MAKE_CHROME_DEFAULT_DECLINED = 1, | 63 MAKE_CHROME_DEFAULT_DECLINED = 1, |
| 64 MAKE_CHROME_DEFAULT_REGRETTED = 2, | 64 MAKE_CHROME_DEFAULT_REGRETTED = 2, |
| 65 // MAKE_CHROME_DEFAULT_ACCEPTED_IMMERSE = 3, // Deprecated. | 65 // MAKE_CHROME_DEFAULT_ACCEPTED_IMMERSE = 3, // Deprecated. |
| 66 MAKE_CHROME_DEFAULT_MAX | 66 MAKE_CHROME_DEFAULT_MAX |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 content::WebUIDataSource* CreateSetAsDefaultBrowserUIHTMLSource() { | 69 content::WebUIDataSource* CreateSetAsDefaultBrowserUIHTMLSource() { |
| 70 content::WebUIDataSource* data_source = content::WebUIDataSource::Create( | 70 content::WebUIDataSource* data_source = |
| 71 chrome::kChromeUIMetroFlowHost); | 71 content::WebUIDataSource::Create(chrome::kChromeUIMetroFlowHost); |
| 72 data_source->AddLocalizedString("page-title", IDS_METRO_FLOW_TAB_TITLE); | 72 data_source->AddLocalizedString("page-title", IDS_METRO_FLOW_TAB_TITLE); |
| 73 data_source->AddLocalizedString("flowTitle", IDS_METRO_FLOW_TITLE_SHORT); | 73 data_source->AddLocalizedString("flowTitle", IDS_METRO_FLOW_TITLE_SHORT); |
| 74 data_source->AddLocalizedString("flowDescription", | 74 data_source->AddLocalizedString("flowDescription", |
| 75 IDS_METRO_FLOW_DESCRIPTION); | 75 IDS_METRO_FLOW_DESCRIPTION); |
| 76 data_source->AddLocalizedString("flowNext", | 76 data_source->AddLocalizedString("flowNext", IDS_METRO_FLOW_SET_DEFAULT); |
| 77 IDS_METRO_FLOW_SET_DEFAULT); | |
| 78 data_source->AddLocalizedString("chromeLogoString", | 77 data_source->AddLocalizedString("chromeLogoString", |
| 79 IDS_METRO_FLOW_LOGO_STRING_ALT); | 78 IDS_METRO_FLOW_LOGO_STRING_ALT); |
| 80 data_source->SetJsonPath("strings.js"); | 79 data_source->SetJsonPath("strings.js"); |
| 81 data_source->AddResourcePath("set_as_default_browser.js", | 80 data_source->AddResourcePath("set_as_default_browser.js", |
| 82 IDR_SET_AS_DEFAULT_BROWSER_JS); | 81 IDR_SET_AS_DEFAULT_BROWSER_JS); |
| 83 data_source->SetDefaultResource(IDR_SET_AS_DEFAULT_BROWSER_HTML); | 82 data_source->SetDefaultResource(IDR_SET_AS_DEFAULT_BROWSER_HTML); |
| 84 return data_source; | 83 return data_source; |
| 85 } | 84 } |
| 86 | 85 |
| 87 // A simple class serving as a delegate for passing down the result of the | 86 // A simple class serving as a delegate for passing down the result of the |
| 88 // interaction. | 87 // interaction. |
| 89 class ResponseDelegate { | 88 class ResponseDelegate { |
| 90 public: | 89 public: |
| 91 virtual void SetDialogInteractionResult(MakeChromeDefaultResult result) = 0; | 90 virtual void SetDialogInteractionResult(MakeChromeDefaultResult result) = 0; |
| 92 | 91 |
| 93 protected: | 92 protected: |
| 94 virtual ~ResponseDelegate() { } | 93 virtual ~ResponseDelegate() {} |
| 95 }; | 94 }; |
| 96 | 95 |
| 97 // Event handler for SetAsDefaultBrowserUI. Capable of setting Chrome as the | 96 // Event handler for SetAsDefaultBrowserUI. Capable of setting Chrome as the |
| 98 // default browser on button click, closing itself and triggering Chrome | 97 // default browser on button click, closing itself and triggering Chrome |
| 99 // restart. | 98 // restart. |
| 100 class SetAsDefaultBrowserHandler : public WebUIMessageHandler { | 99 class SetAsDefaultBrowserHandler : public WebUIMessageHandler { |
| 101 public: | 100 public: |
| 102 explicit SetAsDefaultBrowserHandler( | 101 explicit SetAsDefaultBrowserHandler( |
| 103 const base::WeakPtr<ResponseDelegate>& response_delegate); | 102 const base::WeakPtr<ResponseDelegate>& response_delegate); |
| 104 | 103 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 // Otherwise, keep the dialog open since the user probably didn't make a | 178 // Otherwise, keep the dialog open since the user probably didn't make a |
| 180 // choice. | 179 // choice. |
| 181 } | 180 } |
| 182 | 181 |
| 183 // A web dialog delegate implementation for when 'Make Chrome Metro' UI | 182 // A web dialog delegate implementation for when 'Make Chrome Metro' UI |
| 184 // is displayed on a dialog. | 183 // is displayed on a dialog. |
| 185 class SetAsDefaultBrowserDialogImpl : public ui::WebDialogDelegate, | 184 class SetAsDefaultBrowserDialogImpl : public ui::WebDialogDelegate, |
| 186 public ResponseDelegate, | 185 public ResponseDelegate, |
| 187 public chrome::BrowserListObserver { | 186 public chrome::BrowserListObserver { |
| 188 public: | 187 public: |
| 189 SetAsDefaultBrowserDialogImpl(Profile* profile, Browser* browser); | 188 explicit SetAsDefaultBrowserDialogImpl(Profile* profile); |
| 190 ~SetAsDefaultBrowserDialogImpl() override; | 189 ~SetAsDefaultBrowserDialogImpl() override; |
| 191 // Show a modal web dialog with kChromeUIMetroFlowURL page. | 190 // Show a modal web dialog with kChromeUIMetroFlowURL page. |
| 192 void ShowDialog(); | 191 void ShowDialog(); |
| 192 static views::Widget* dialog_widget() { return dialog_widget_; } | |
| 193 | 193 |
| 194 protected: | 194 protected: |
| 195 // Overridden from WebDialogDelegate: | 195 // Overridden from WebDialogDelegate: |
| 196 ui::ModalType GetDialogModalType() const override; | 196 ui::ModalType GetDialogModalType() const override; |
| 197 base::string16 GetDialogTitle() const override; | 197 base::string16 GetDialogTitle() const override; |
| 198 GURL GetDialogContentURL() const override; | 198 GURL GetDialogContentURL() const override; |
| 199 void GetWebUIMessageHandlers( | 199 void GetWebUIMessageHandlers( |
| 200 std::vector<WebUIMessageHandler*>* handlers) const override; | 200 std::vector<WebUIMessageHandler*>* handlers) const override; |
| 201 void GetDialogSize(gfx::Size* size) const override; | 201 void GetDialogSize(gfx::Size* size) const override; |
| 202 std::string GetDialogArgs() const override; | 202 std::string GetDialogArgs() const override; |
| 203 void OnDialogClosed(const std::string& json_retval) override; | 203 void OnDialogClosed(const std::string& json_retval) override; |
| 204 void OnCloseContents(WebContents* source, bool* out_close_dialog) override; | 204 void OnCloseContents(WebContents* source, bool* out_close_dialog) override; |
| 205 bool ShouldShowDialogTitle() const override; | 205 bool ShouldShowDialogTitle() const override; |
| 206 bool HandleContextMenu(const content::ContextMenuParams& params) override; | 206 bool HandleContextMenu(const content::ContextMenuParams& params) override; |
| 207 | 207 |
| 208 // Overridden from ResponseDelegate: | 208 // Overridden from ResponseDelegate: |
| 209 void SetDialogInteractionResult(MakeChromeDefaultResult result) override; | 209 void SetDialogInteractionResult(MakeChromeDefaultResult result) override; |
| 210 | 210 |
| 211 // Overridden from BrowserListObserver: | 211 // Overridden from BrowserListObserver: |
| 212 void OnBrowserAdded(Browser* browser) override; | |
| 212 void OnBrowserRemoved(Browser* browser) override; | 213 void OnBrowserRemoved(Browser* browser) override; |
| 213 | 214 |
| 214 private: | 215 private: |
| 215 Profile* profile_; | 216 Profile* profile_; |
| 216 Browser* browser_; | 217 Browser* browser_; |
| 217 mutable bool owns_handler_; | 218 mutable bool owns_handler_; |
| 218 base::WeakPtrFactory<ResponseDelegate> response_delegate_ptr_factory_; | 219 base::WeakPtrFactory<ResponseDelegate> response_delegate_ptr_factory_; |
| 219 SetAsDefaultBrowserHandler* handler_; | 220 SetAsDefaultBrowserHandler* handler_; |
| 220 MakeChromeDefaultResult dialog_interaction_result_; | 221 MakeChromeDefaultResult dialog_interaction_result_; |
| 221 | 222 |
| 223 static views::Widget* dialog_widget_; | |
| 224 | |
| 222 DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserDialogImpl); | 225 DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserDialogImpl); |
| 223 }; | 226 }; |
| 224 | 227 |
| 225 SetAsDefaultBrowserDialogImpl::SetAsDefaultBrowserDialogImpl(Profile* profile, | 228 views::Widget* SetAsDefaultBrowserDialogImpl::dialog_widget_ = nullptr; |
|
sky
2016/07/25 20:10:03
nit: prefix with // static (see style guide)
zmin
2016/07/25 20:23:11
Done.
| |
| 226 Browser* browser) | 229 |
| 230 SetAsDefaultBrowserDialogImpl::SetAsDefaultBrowserDialogImpl(Profile* profile) | |
| 227 : profile_(profile), | 231 : profile_(profile), |
| 228 browser_(browser), | 232 browser_(nullptr), |
| 229 owns_handler_(true), | 233 owns_handler_(true), |
| 230 response_delegate_ptr_factory_(this), | 234 response_delegate_ptr_factory_(this), |
| 231 handler_(new SetAsDefaultBrowserHandler( | 235 handler_(new SetAsDefaultBrowserHandler( |
| 232 response_delegate_ptr_factory_.GetWeakPtr())), | 236 response_delegate_ptr_factory_.GetWeakPtr())), |
| 233 dialog_interaction_result_(MAKE_CHROME_DEFAULT_DECLINED) { | 237 dialog_interaction_result_(MAKE_CHROME_DEFAULT_DECLINED) { |
| 234 BrowserList::AddObserver(this); | 238 BrowserList::AddObserver(this); |
| 235 } | 239 } |
| 236 | 240 |
| 237 SetAsDefaultBrowserDialogImpl::~SetAsDefaultBrowserDialogImpl() { | 241 SetAsDefaultBrowserDialogImpl::~SetAsDefaultBrowserDialogImpl() { |
| 238 if (browser_) | 242 if (browser_) |
| 239 BrowserList::RemoveObserver(this); | 243 BrowserList::RemoveObserver(this); |
| 240 if (owns_handler_) | 244 if (owns_handler_) |
| 241 delete handler_; | 245 delete handler_; |
| 242 } | 246 } |
| 243 | 247 |
| 244 void SetAsDefaultBrowserDialogImpl::ShowDialog() { | 248 void SetAsDefaultBrowserDialogImpl::ShowDialog() { |
| 245 // Use a NULL parent window to make sure that the dialog will have an item | 249 // 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 | 250 // in the Windows task bar. The code below will make it highlight if the |
| 247 // dialog is not in the foreground. | 251 // dialog is not in the foreground. |
| 248 gfx::NativeWindow native_window = chrome::ShowWebDialog(NULL, profile_, this); | 252 gfx::NativeWindow native_window = chrome::ShowWebDialog(NULL, profile_, this); |
| 249 views::Widget* widget = views::Widget::GetWidgetForNativeWindow( | 253 DCHECK(!dialog_widget_); |
| 250 native_window); | 254 dialog_widget_ = views::Widget::GetWidgetForNativeWindow(native_window); |
| 251 widget->FlashFrame(true); | 255 dialog_widget_->FlashFrame(true); |
| 252 } | 256 } |
| 253 | 257 |
| 254 ui::ModalType SetAsDefaultBrowserDialogImpl::GetDialogModalType() const { | 258 ui::ModalType SetAsDefaultBrowserDialogImpl::GetDialogModalType() const { |
| 255 return ui::MODAL_TYPE_SYSTEM; | 259 return ui::MODAL_TYPE_SYSTEM; |
| 256 } | 260 } |
| 257 | 261 |
| 258 base::string16 SetAsDefaultBrowserDialogImpl::GetDialogTitle() const { | 262 base::string16 SetAsDefaultBrowserDialogImpl::GetDialogTitle() const { |
| 259 return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE); | 263 return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE); |
| 260 } | 264 } |
| 261 | 265 |
| 262 GURL SetAsDefaultBrowserDialogImpl::GetDialogContentURL() const { | 266 GURL SetAsDefaultBrowserDialogImpl::GetDialogContentURL() const { |
| 263 std::string url_string(chrome::kChromeUIMetroFlowURL); | 267 std::string url_string(chrome::kChromeUIMetroFlowURL); |
| 264 return GURL(url_string); | 268 return GURL(url_string); |
| 265 } | 269 } |
| 266 | 270 |
| 267 void SetAsDefaultBrowserDialogImpl::GetWebUIMessageHandlers( | 271 void SetAsDefaultBrowserDialogImpl::GetWebUIMessageHandlers( |
| 268 std::vector<WebUIMessageHandler*>* handlers) const { | 272 std::vector<WebUIMessageHandler*>* handlers) const { |
| 269 handlers->push_back(handler_); | 273 handlers->push_back(handler_); |
| 270 owns_handler_ = false; | 274 owns_handler_ = false; |
| 271 } | 275 } |
| 272 | 276 |
| 273 void SetAsDefaultBrowserDialogImpl::GetDialogSize(gfx::Size* size) const { | 277 void SetAsDefaultBrowserDialogImpl::GetDialogSize(gfx::Size* size) const { |
| 274 PrefService* prefs = profile_->GetPrefs(); | 278 PrefService* prefs = profile_->GetPrefs(); |
| 275 gfx::Font approximate_web_font( | 279 gfx::Font approximate_web_font( |
| 276 prefs->GetString(prefs::kWebKitSansSerifFontFamily), | 280 prefs->GetString(prefs::kWebKitSansSerifFontFamily), |
| 277 prefs->GetInteger(prefs::kWebKitDefaultFontSize)); | 281 prefs->GetInteger(prefs::kWebKitDefaultFontSize)); |
| 278 | 282 |
| 279 *size = ui::GetLocalizedContentsSizeForFont( | 283 *size = ui::GetLocalizedContentsSizeForFont(IDS_METRO_FLOW_WIDTH_CHARS, |
| 280 IDS_METRO_FLOW_WIDTH_CHARS, IDS_METRO_FLOW_HEIGHT_LINES, | 284 IDS_METRO_FLOW_HEIGHT_LINES, |
| 281 approximate_web_font); | 285 approximate_web_font); |
| 282 } | 286 } |
| 283 | 287 |
| 284 std::string SetAsDefaultBrowserDialogImpl::GetDialogArgs() const { | 288 std::string SetAsDefaultBrowserDialogImpl::GetDialogArgs() const { |
| 285 return "[]"; | 289 return "[]"; |
| 286 } | 290 } |
| 287 | 291 |
| 288 void SetAsDefaultBrowserDialogImpl::OnDialogClosed( | 292 void SetAsDefaultBrowserDialogImpl::OnDialogClosed( |
| 289 const std::string& json_retval) { | 293 const std::string& json_retval) { |
| 290 // Register the user's response in UMA. | 294 // Register the user's response in UMA. |
| 291 UMA_HISTOGRAM_ENUMERATION(kSetAsDefaultBrowserHistogram, | 295 UMA_HISTOGRAM_ENUMERATION(kSetAsDefaultBrowserHistogram, |
| 292 dialog_interaction_result_, | 296 dialog_interaction_result_, |
| 293 MAKE_CHROME_DEFAULT_MAX); | 297 MAKE_CHROME_DEFAULT_MAX); |
| 294 | 298 |
| 295 // Suppress showing the default browser infobar if the user explicitly elected | 299 // Suppress showing the default browser infobar if the user explicitly elected |
| 296 // *not to* make Chrome default. | 300 // *not to* make Chrome default. |
| 297 if (dialog_interaction_result_ == MAKE_CHROME_DEFAULT_REGRETTED) | 301 if (dialog_interaction_result_ == MAKE_CHROME_DEFAULT_REGRETTED) |
| 298 chrome::DefaultBrowserPromptDeclined(profile_); | 302 chrome::DefaultBrowserPromptDeclined(profile_); |
| 299 | 303 |
| 300 // Carry on with a normal chrome session. For the purpose of surfacing this | 304 // 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 | 305 // dialog the actual browser window had to remain hidden. Now it's time to |
| 302 // show it. | 306 // show it. |
| 303 if (browser_) { | 307 if (browser_) { |
| 304 BrowserWindow* window = browser_->window(); | 308 BrowserWindow* window = browser_->window(); |
| 305 WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents(); | 309 WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents(); |
| 306 window->Show(); | 310 window->Show(); |
| 307 if (contents) | 311 if (contents) |
| 308 contents->SetInitialFocus(); | 312 contents->SetInitialFocus(); |
| 309 } | 313 } |
| 310 | 314 |
| 315 DCHECK(dialog_widget_); | |
| 316 dialog_widget_ = nullptr; | |
| 317 | |
| 311 delete this; | 318 delete this; |
| 312 } | 319 } |
| 313 | 320 |
| 314 void SetAsDefaultBrowserDialogImpl::OnCloseContents(WebContents* source, | 321 void SetAsDefaultBrowserDialogImpl::OnCloseContents(WebContents* source, |
| 315 bool* out_close_dialog) { | 322 bool* out_close_dialog) { |
| 316 *out_close_dialog = true; | 323 *out_close_dialog = true; |
| 317 } | 324 } |
| 318 | 325 |
| 319 bool SetAsDefaultBrowserDialogImpl::ShouldShowDialogTitle() const { | 326 bool SetAsDefaultBrowserDialogImpl::ShouldShowDialogTitle() const { |
| 320 return true; | 327 return true; |
| 321 } | 328 } |
| 322 | 329 |
| 323 bool SetAsDefaultBrowserDialogImpl::HandleContextMenu( | 330 bool SetAsDefaultBrowserDialogImpl::HandleContextMenu( |
| 324 const content::ContextMenuParams& params) { | 331 const content::ContextMenuParams& params) { |
| 325 return true; | 332 return true; |
| 326 } | 333 } |
| 327 | 334 |
| 328 void SetAsDefaultBrowserDialogImpl::SetDialogInteractionResult( | 335 void SetAsDefaultBrowserDialogImpl::SetDialogInteractionResult( |
| 329 MakeChromeDefaultResult result) { | 336 MakeChromeDefaultResult result) { |
| 330 dialog_interaction_result_ = result; | 337 dialog_interaction_result_ = result; |
| 331 } | 338 } |
| 332 | 339 |
| 340 void SetAsDefaultBrowserDialogImpl::OnBrowserAdded(Browser* browser) { | |
| 341 if (browser_ || !browser || !browser->is_type_tabbed()) | |
|
sky
2016/07/25 20:10:03
Should this early out if dialog_widget_?
zmin
2016/07/25 20:23:11
dialog_widget_ is only used for testing so I think
sky
2016/07/25 20:24:12
What prevents another browser from being created w
| |
| 342 return; | |
| 343 browser_ = browser; | |
| 344 ShowDialog(); | |
| 345 } | |
| 346 | |
| 333 void SetAsDefaultBrowserDialogImpl::OnBrowserRemoved(Browser* browser) { | 347 void SetAsDefaultBrowserDialogImpl::OnBrowserRemoved(Browser* browser) { |
| 334 if (browser_ == browser) { | 348 if (browser_ == browser) { |
| 335 browser_ = NULL; | 349 browser_ = NULL; |
| 336 BrowserList::RemoveObserver(this); | 350 BrowserList::RemoveObserver(this); |
| 337 } | 351 } |
| 338 } | 352 } |
| 339 | 353 |
| 340 } // namespace | 354 } // namespace |
| 341 | 355 |
| 342 SetAsDefaultBrowserUI::SetAsDefaultBrowserUI(content::WebUI* web_ui) | 356 SetAsDefaultBrowserUI::SetAsDefaultBrowserUI(content::WebUI* web_ui) |
| 343 : ui::WebDialogUI(web_ui) { | 357 : ui::WebDialogUI(web_ui) { |
| 344 content::WebUIDataSource::Add( | 358 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), |
| 345 Profile::FromWebUI(web_ui), CreateSetAsDefaultBrowserUIHTMLSource()); | 359 CreateSetAsDefaultBrowserUIHTMLSource()); |
| 346 } | 360 } |
| 347 | 361 |
| 348 // static | 362 // static |
| 349 void SetAsDefaultBrowserUI::Show(Profile* profile, Browser* browser) { | 363 void SetAsDefaultBrowserUI::Show(Profile* profile) { |
| 350 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 364 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 351 SetAsDefaultBrowserDialogImpl* dialog = | 365 new SetAsDefaultBrowserDialogImpl(profile); |
| 352 new SetAsDefaultBrowserDialogImpl(profile, browser); | |
| 353 dialog->ShowDialog(); | |
| 354 } | 366 } |
| 367 | |
| 368 // static | |
| 369 views::Widget* SetAsDefaultBrowserUI::GetDialogWidgetForTesting() { | |
| 370 return SetAsDefaultBrowserDialogImpl::dialog_widget(); | |
| 371 } | |
| OLD | NEW |