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