| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/set_as_default_browser_ui.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/path_service.h" |
| 10 #include "chrome/browser/first_run/first_run.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_finder.h" |
| 15 #include "chrome/browser/ui/browser_list.h" |
| 16 #include "chrome/browser/ui/chrome_pages.h" |
| 17 #include "chrome/browser/ui/singleton_tabs.h" |
| 18 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 19 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" |
| 20 #include "chrome/browser/shell_integration.h" |
| 21 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/common/url_constants.h" |
| 23 #include "chrome/installer/util/install_util.h" |
| 24 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/browser/web_contents_delegate.h" |
| 26 #include "content/public/browser/web_ui.h" |
| 27 #include "content/public/browser/web_ui_message_handler.h" |
| 28 #include "grit/browser_resources.h" |
| 29 #include "grit/generated_resources.h" |
| 30 #include "grit/locale_settings.h" |
| 31 #include "ui/base/l10n/l10n_font_util.h" |
| 32 #include "ui/base/l10n/l10n_util.h" |
| 33 #include "ui/gfx/font.h" |
| 34 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 35 |
| 36 using content::BrowserThread; |
| 37 using content::WebContents; |
| 38 using content::WebUIMessageHandler; |
| 39 |
| 40 namespace { |
| 41 |
| 42 ChromeWebUIDataSource* CreateSetAsDefaultBrowserUIHTMLSource() { |
| 43 ChromeWebUIDataSource* data_source = new ChromeWebUIDataSource( |
| 44 chrome::kChromeUIMetroFlowHost); |
| 45 data_source->AddLocalizedString("page-title", IDS_METRO_FLOW_TAB_TITLE); |
| 46 data_source->AddLocalizedString("flowTitle", IDS_METRO_FLOW_TITLE_SHORT); |
| 47 data_source->AddLocalizedString("flowDescription", |
| 48 IDS_METRO_FLOW_DESCRIPTION); |
| 49 data_source->AddLocalizedString("flowNext", |
| 50 IDS_METRO_FLOW_SET_DEFAULT); |
| 51 data_source->AddLocalizedString("flowCancel", |
| 52 IDS_METRO_FLOW_SET_DEFAULT_CANCEL); |
| 53 data_source->set_json_path("strings.js"); |
| 54 data_source->add_resource_path("set_as_default_browser.js", |
| 55 IDR_SET_AS_DEFAULT_BROWSER_JS); |
| 56 data_source->set_default_resource(IDR_SET_AS_DEFAULT_BROWSER_HTML); |
| 57 return data_source; |
| 58 } |
| 59 |
| 60 // Event handler for SetAsDefaultBrowserUI. Capable of setting Chrome as the |
| 61 // default browser on button click, closing itself and triggering Chrome |
| 62 // restart. |
| 63 class SetAsDefaultBrowserHandler |
| 64 : public WebUIMessageHandler, |
| 65 public base::SupportsWeakPtr<SetAsDefaultBrowserHandler>, |
| 66 public ShellIntegration::DefaultWebClientObserver { |
| 67 public: |
| 68 SetAsDefaultBrowserHandler(); |
| 69 virtual ~SetAsDefaultBrowserHandler(); |
| 70 |
| 71 // WebUIMessageHandler implementation. |
| 72 virtual void RegisterMessages() OVERRIDE; |
| 73 |
| 74 // ShellIntegration::DefaultWebClientObserver implementation. |
| 75 virtual void SetDefaultWebClientUIState( |
| 76 ShellIntegration::DefaultWebClientUIState state) OVERRIDE; |
| 77 virtual void OnSetAsDefaultConcluded(bool call_result) OVERRIDE; |
| 78 virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE; |
| 79 |
| 80 private: |
| 81 // Handler for the 'Next' (or 'make Chrome the Metro browser') button. |
| 82 void HandleLaunchSetDefaultBrowserFlow(const ListValue* args); |
| 83 |
| 84 // Handler for the 'No, thanks' (Cancel) button. |
| 85 void HandleReturnToBrowser(const ListValue* args); |
| 86 |
| 87 // Close this web ui. |
| 88 void ConcludeInteraction(); |
| 89 |
| 90 // If required and possible, spawns a new Chrome in Metro mode and closes the |
| 91 // current instance. Windows 8 only, on earlier systems it will simply close |
| 92 // this UI. |
| 93 void ActivateMetroChrome(); |
| 94 |
| 95 scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_; |
| 96 bool set_default_returned_; |
| 97 bool set_default_result_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserHandler); |
| 100 }; |
| 101 |
| 102 SetAsDefaultBrowserHandler::SetAsDefaultBrowserHandler() |
| 103 : ALLOW_THIS_IN_INITIALIZER_LIST(default_browser_worker_( |
| 104 new ShellIntegration::DefaultBrowserWorker(this))), |
| 105 set_default_returned_(false), set_default_result_(false) { |
| 106 } |
| 107 |
| 108 SetAsDefaultBrowserHandler::~SetAsDefaultBrowserHandler() { |
| 109 default_browser_worker_->ObserverDestroyed(); |
| 110 } |
| 111 |
| 112 void SetAsDefaultBrowserHandler::RegisterMessages() { |
| 113 web_ui()->RegisterMessageCallback( |
| 114 "SetAsDefaultBrowser:LaunchSetDefaultBrowserFlow", |
| 115 base::Bind(&SetAsDefaultBrowserHandler::HandleLaunchSetDefaultBrowserFlow, |
| 116 base::Unretained(this))); |
| 117 web_ui()->RegisterMessageCallback( |
| 118 "SetAsDefaultBrowser:ReturnToBrowser", |
| 119 base::Bind(&SetAsDefaultBrowserHandler::HandleReturnToBrowser, |
| 120 base::Unretained(this))); |
| 121 } |
| 122 |
| 123 void SetAsDefaultBrowserHandler::SetDefaultWebClientUIState( |
| 124 ShellIntegration::DefaultWebClientUIState state) { |
| 125 // The callback is expected to be invoked once the procedure has completed. |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 127 if (!set_default_returned_) |
| 128 return; |
| 129 |
| 130 if (state == ShellIntegration::STATE_NOT_DEFAULT && set_default_result_) { |
| 131 // The operation concluded, but Chrome is still not the default. |
| 132 // If the call has succeeded, this suggests user has decided not to make |
| 133 // chrome the default. We fold this UI and move on. |
| 134 ConcludeInteraction(); |
| 135 } else if (state == ShellIntegration::STATE_IS_DEFAULT) { |
| 136 BrowserThread::PostTask( |
| 137 BrowserThread::FILE, FROM_HERE, |
| 138 base::Bind(&SetAsDefaultBrowserHandler::ActivateMetroChrome, |
| 139 base::Unretained(this))); |
| 140 } |
| 141 } |
| 142 |
| 143 void SetAsDefaultBrowserHandler::OnSetAsDefaultConcluded(bool call_result) { |
| 144 set_default_returned_ = true; |
| 145 set_default_result_ = call_result; |
| 146 } |
| 147 |
| 148 bool SetAsDefaultBrowserHandler::IsInteractiveSetDefaultPermitted() { |
| 149 return true; |
| 150 } |
| 151 |
| 152 void SetAsDefaultBrowserHandler::HandleLaunchSetDefaultBrowserFlow( |
| 153 const ListValue* args) { |
| 154 set_default_returned_ = false; |
| 155 set_default_result_ = false; |
| 156 default_browser_worker_->StartSetAsDefault(); |
| 157 } |
| 158 |
| 159 void SetAsDefaultBrowserHandler::HandleReturnToBrowser(const ListValue* args) { |
| 160 ConcludeInteraction(); |
| 161 } |
| 162 |
| 163 void SetAsDefaultBrowserHandler::ConcludeInteraction() { |
| 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 165 WebContents* contents = web_ui()->GetWebContents(); |
| 166 if (contents) { |
| 167 content::WebContentsDelegate* delegate = contents->GetDelegate(); |
| 168 if (delegate) { |
| 169 if (!delegate->IsPopupOrPanel(contents)) { |
| 170 Browser* browser = browser::FindBrowserWithWebContents(contents); |
| 171 if (browser) |
| 172 chrome::ShowSyncSetup(browser, SyncPromoUI::SOURCE_START_PAGE); |
| 173 } |
| 174 delegate->CloseContents(contents); |
| 175 } |
| 176 } |
| 177 } |
| 178 |
| 179 void SetAsDefaultBrowserHandler::ActivateMetroChrome() { |
| 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 181 |
| 182 FilePath cur_chrome_exe; |
| 183 bool sentinel_removed = false; |
| 184 if (PathService::Get(base::FILE_EXE, &cur_chrome_exe) && |
| 185 first_run::IsChromeFirstRun() && |
| 186 InstallUtil::IsPerUserInstall(cur_chrome_exe.value().c_str())) { |
| 187 // If this is per-user install, we will have to remove the sentinel file |
| 188 // to assure the user goes through the intended 'first-run flow'. |
| 189 sentinel_removed = first_run::RemoveSentinel(); |
| 190 } |
| 191 |
| 192 if (ShellIntegration::ActivateMetroChrome()) { |
| 193 // If Metro Chrome has been activated, we should close this process. |
| 194 // We are restarting as metro now. |
| 195 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 196 base::Bind(&BrowserList::CloseAllBrowsersWithProfile, |
| 197 Profile::FromWebUI(web_ui()))); |
| 198 } else { |
| 199 // This will return false if the operation failed for any reason, |
| 200 // including invocation under a Windows version earlier than 8. |
| 201 // In such case we simply close the window and carry on. |
| 202 if (sentinel_removed) |
| 203 first_run::CreateSentinel(); |
| 204 |
| 205 BrowserThread::PostTask( |
| 206 BrowserThread::UI, FROM_HERE, |
| 207 base::Bind(&SetAsDefaultBrowserHandler::ConcludeInteraction, |
| 208 base::Unretained(this))); |
| 209 } |
| 210 } |
| 211 |
| 212 // A web dialog delegate implementation for when 'Make Chrome Metro' UI |
| 213 // is displayed on a dialog. |
| 214 class SetAsDefaultBrowserDialogImpl : public ui::WebDialogDelegate { |
| 215 public: |
| 216 SetAsDefaultBrowserDialogImpl(Profile* profile, Browser* browser); |
| 217 // Show a modal web dialog with kChromeUIMetroFlowURL page. |
| 218 void ShowDialog(); |
| 219 |
| 220 protected: |
| 221 // Overridden from WebDialogDelegate: |
| 222 virtual ui::ModalType GetDialogModalType() const OVERRIDE; |
| 223 virtual string16 GetDialogTitle() const OVERRIDE; |
| 224 virtual GURL GetDialogContentURL() const OVERRIDE; |
| 225 virtual void GetWebUIMessageHandlers( |
| 226 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE; |
| 227 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; |
| 228 virtual std::string GetDialogArgs() const OVERRIDE; |
| 229 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; |
| 230 virtual void OnCloseContents(WebContents* source, |
| 231 bool* out_close_dialog) OVERRIDE; |
| 232 virtual bool ShouldShowDialogTitle() const OVERRIDE; |
| 233 virtual bool HandleContextMenu( |
| 234 const content::ContextMenuParams& params) OVERRIDE; |
| 235 |
| 236 private: |
| 237 Profile* profile_; |
| 238 Browser* browser_; |
| 239 |
| 240 DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserDialogImpl); |
| 241 }; |
| 242 |
| 243 SetAsDefaultBrowserDialogImpl::SetAsDefaultBrowserDialogImpl(Profile* profile, |
| 244 Browser* browser) |
| 245 : profile_(profile), browser_(browser) { |
| 246 } |
| 247 |
| 248 void SetAsDefaultBrowserDialogImpl::ShowDialog() { |
| 249 browser_->BrowserShowWebDialog(this, NULL); |
| 250 } |
| 251 |
| 252 ui::ModalType SetAsDefaultBrowserDialogImpl::GetDialogModalType() const { |
| 253 return ui::MODAL_TYPE_SYSTEM; |
| 254 } |
| 255 |
| 256 string16 SetAsDefaultBrowserDialogImpl::GetDialogTitle() const { |
| 257 return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE); |
| 258 } |
| 259 |
| 260 GURL SetAsDefaultBrowserDialogImpl::GetDialogContentURL() const { |
| 261 std::string url_string(chrome::kChromeUIMetroFlowURL); |
| 262 return GURL(url_string); |
| 263 } |
| 264 |
| 265 void SetAsDefaultBrowserDialogImpl::GetWebUIMessageHandlers( |
| 266 std::vector<WebUIMessageHandler*>* handlers) const { |
| 267 } |
| 268 |
| 269 void SetAsDefaultBrowserDialogImpl::GetDialogSize(gfx::Size* size) const { |
| 270 PrefService* prefs = profile_->GetPrefs(); |
| 271 gfx::Font approximate_web_font( |
| 272 prefs->GetString(prefs::kWebKitSansSerifFontFamily), |
| 273 prefs->GetInteger(prefs::kWebKitDefaultFontSize)); |
| 274 |
| 275 *size = ui::GetLocalizedContentsSizeForFont( |
| 276 IDS_METRO_FLOW_WIDTH_CHARS, IDS_METRO_FLOW_HEIGHT_LINES, |
| 277 approximate_web_font); |
| 278 } |
| 279 |
| 280 std::string SetAsDefaultBrowserDialogImpl::GetDialogArgs() const { |
| 281 return "[]"; |
| 282 } |
| 283 |
| 284 void SetAsDefaultBrowserDialogImpl::OnDialogClosed( |
| 285 const std::string& json_retval) { |
| 286 delete this; |
| 287 } |
| 288 |
| 289 void SetAsDefaultBrowserDialogImpl::OnCloseContents(WebContents* source, |
| 290 bool* out_close_dialog) { |
| 291 *out_close_dialog = true; |
| 292 } |
| 293 |
| 294 bool SetAsDefaultBrowserDialogImpl::ShouldShowDialogTitle() const { |
| 295 return true; |
| 296 } |
| 297 |
| 298 bool SetAsDefaultBrowserDialogImpl::HandleContextMenu( |
| 299 const content::ContextMenuParams& params) { |
| 300 return true; |
| 301 } |
| 302 |
| 303 } // namespace |
| 304 |
| 305 SetAsDefaultBrowserUI::SetAsDefaultBrowserUI(content::WebUI* web_ui) |
| 306 : WebUIController(web_ui) { |
| 307 web_ui->AddMessageHandler(new SetAsDefaultBrowserHandler()); |
| 308 ChromeURLDataManager::AddDataSource(Profile::FromWebUI(web_ui), |
| 309 CreateSetAsDefaultBrowserUIHTMLSource()); |
| 310 } |
| 311 |
| 312 // static |
| 313 void SetAsDefaultBrowserUI::Show(Profile* profile, |
| 314 Browser* browser, |
| 315 bool dialog) { |
| 316 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 317 if (dialog) { |
| 318 SetAsDefaultBrowserDialogImpl* dialog = |
| 319 new SetAsDefaultBrowserDialogImpl(profile, browser); |
| 320 dialog->ShowDialog(); |
| 321 } else { |
| 322 GURL url(chrome::kChromeUIMetroFlowURL); |
| 323 browser::NavigateParams params( |
| 324 chrome::GetSingletonTabNavigateParams(browser, url)); |
| 325 params.path_behavior = browser::NavigateParams::IGNORE_AND_NAVIGATE; |
| 326 chrome::ShowSingletonTabOverwritingNTP(browser, params); |
| 327 } |
| 328 } |
| OLD | NEW |