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