Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
grt (UTC plus 2)
2012/06/18 19:19:13
why is this file present on non-windows platforms?
motek.
2012/06/19 18:06:44
Good question. I sort-of made it so by placing the
grt (UTC plus 2)
2012/06/19 20:43:09
renaming it to metroizer_ui_win will cause it to b
| |
| 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.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_dialogs.h" | |
| 15 #include "chrome/browser/ui/browser_finder.h" | |
| 16 #include "chrome/browser/ui/browser_list.h" | |
| 17 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | |
| 18 #include "chrome/browser/shell_integration.h" | |
| 19 #include "chrome/common/pref_names.h" | |
| 20 #include "chrome/common/url_constants.h" | |
| 21 #ifdef OS_WIN | |
|
grt (UTC plus 2)
2012/06/18 19:19:13
move platform-specific includes down below the all
motek.
2012/06/19 18:06:44
Done.
| |
| 22 #include "chrome/installer/util/install_util.h" | |
| 23 #endif | |
| 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 * CreateMetroizerUIHTMLSource() { | |
| 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("metroize.js", IDR_METROIZE_JS); | |
| 55 data_source->set_default_resource(IDR_METROIZE_HTML); | |
| 56 return data_source; | |
| 57 } | |
| 58 | |
| 59 class MetroizeHandler : public WebUIMessageHandler, | |
| 60 public base::SupportsWeakPtr<MetroizeHandler>, | |
| 61 public ShellIntegration::DefaultWebClientObserver { | |
| 62 public: | |
| 63 explicit MetroizeHandler(); | |
|
grt (UTC plus 2)
2012/06/18 19:19:13
no explicit
motek.
2012/06/19 18:06:44
Done.
| |
| 64 virtual ~MetroizeHandler(); | |
| 65 | |
| 66 // WebUIMessageHandler implementation. | |
| 67 virtual void RegisterMessages() OVERRIDE; | |
| 68 | |
| 69 // ShellIntegration::DefaultWebClientObserver implementation. | |
| 70 virtual void SetDefaultWebClientUIState( | |
| 71 ShellIntegration::DefaultWebClientUIState state, | |
| 72 ShellIntegration::SetDefaultWebClientResult call_result) OVERRIDE; | |
| 73 virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE; | |
| 74 | |
| 75 private: | |
| 76 void HandleLaunchMetroizeProcedure(const ListValue* args); | |
| 77 void HandleReturnToChrome(const ListValue* args); | |
| 78 void CloseMetroizeWindow(); | |
| 79 | |
| 80 #ifdef OS_WIN | |
| 81 void ActivateMetroChrome(); | |
| 82 #endif | |
| 83 | |
| 84 scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(MetroizeHandler); | |
| 87 }; | |
| 88 | |
| 89 MetroizeHandler::MetroizeHandler() { | |
| 90 default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this); | |
| 91 } | |
| 92 | |
| 93 MetroizeHandler::~MetroizeHandler() { | |
| 94 if (default_browser_worker_.get()) | |
| 95 default_browser_worker_->ObserverDestroyed(); | |
| 96 } | |
| 97 | |
| 98 void MetroizeHandler::RegisterMessages() { | |
| 99 web_ui()->RegisterMessageCallback( | |
| 100 "Metroize:LaunchSetDefaultBrowserFlow", | |
| 101 base::Bind(&MetroizeHandler::HandleLaunchMetroizeProcedure, | |
| 102 base::Unretained(this))); | |
| 103 web_ui()->RegisterMessageCallback( | |
| 104 "Metroize:ReturnToBrowser", | |
| 105 base::Bind(&MetroizeHandler::HandleReturnToChrome, | |
| 106 base::Unretained(this))); | |
| 107 } | |
| 108 | |
| 109 void MetroizeHandler::HandleLaunchMetroizeProcedure(const ListValue* args) { | |
| 110 if (default_browser_worker_.get()) | |
| 111 default_browser_worker_->StartSetAsDefault(); | |
| 112 } | |
| 113 | |
| 114 void MetroizeHandler::HandleReturnToChrome(const ListValue* args) { | |
| 115 CloseMetroizeWindow(); | |
| 116 } | |
| 117 | |
| 118 void MetroizeHandler::CloseMetroizeWindow() { | |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 120 WebContents* contents = web_ui()->GetWebContents(); | |
| 121 if (contents) { | |
| 122 content::WebContentsDelegate* delegate = contents->GetDelegate(); | |
| 123 if (delegate) { | |
| 124 if (!delegate->IsPopupOrPanel(contents)) { | |
| 125 Browser* browser = browser::FindBrowserWithWebContents(contents); | |
| 126 if (browser) | |
| 127 browser->ShowSyncSetup(SyncPromoUI::SOURCE_START_PAGE); | |
| 128 } | |
| 129 delegate->CloseContents(contents); | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 #ifdef OS_WIN | |
| 135 void MetroizeHandler::ActivateMetroChrome() { | |
| 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 137 | |
| 138 FilePath cur_chrome_exe; | |
| 139 bool sentinel_removed = false; | |
| 140 if (PathService::Get(base::FILE_EXE, &cur_chrome_exe) && | |
| 141 first_run::IsChromeFirstRun() && | |
| 142 InstallUtil::IsPerUserInstall(cur_chrome_exe.value().c_str())) { | |
| 143 // If this is per-user install, we will have to remove the sentinel file | |
|
grt (UTC plus 2)
2012/06/18 19:19:13
this removes the sentinel from desktop chrome. if
motek.
2012/06/19 18:06:44
I am not sure I understand this. This is not the b
grt (UTC plus 2)
2012/06/19 20:43:09
Ah! You're right: in-per user there's only one se
| |
| 144 // to assure the user goes through the intended 'first-run flow'. | |
| 145 sentinel_removed = first_run::RemoveSentinel(); | |
| 146 } | |
| 147 | |
| 148 if (ShellIntegration::ActivateMetroChrome()) { | |
| 149 // If Metro Chrome has been activated, we should close this process. | |
|
grt (UTC plus 2)
2012/06/18 19:19:13
indentation here on down
motek.
2012/06/19 18:06:44
Done.
| |
| 150 // We are restarting as metro now. | |
| 151 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 152 base::Bind(&BrowserList::CloseAllBrowsersWithProfile, | |
| 153 Profile::FromWebUI(web_ui()))); | |
| 154 } else { | |
| 155 // This will return false if the operation failed for any reason, | |
| 156 // including invocation under a Windows version earlier than 8. | |
| 157 // In such case we simply close the window and carry on. | |
| 158 if (sentinel_removed) | |
| 159 first_run::CreateSentinel(); | |
| 160 | |
| 161 BrowserThread::PostTask( | |
| 162 BrowserThread::UI, FROM_HERE, | |
| 163 base::Bind(&MetroizeHandler::CloseMetroizeWindow, | |
| 164 base::Unretained(this))); | |
| 165 } | |
| 166 } | |
| 167 #endif | |
| 168 | |
| 169 void MetroizeHandler::SetDefaultWebClientUIState( | |
| 170 ShellIntegration::DefaultWebClientUIState state, | |
|
grt (UTC plus 2)
2012/06/18 19:19:13
indentation
motek.
2012/06/19 18:06:44
Done.
| |
| 171 ShellIntegration::SetDefaultWebClientResult call_result) { | |
| 172 // The callback is expected to be invoked once the procedure has completed. | |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 174 if (state == ShellIntegration::STATE_NOT_DEFAULT && | |
| 175 call_result == ShellIntegration::RESULT_SET_DEFAULT_OK) { | |
| 176 // The operation concluded, but Chrome is still not the default. | |
| 177 // If the call has succeeded, this suggests user has decided not to make | |
| 178 // chrome the default. We fold this UI and move on. | |
| 179 CloseMetroizeWindow(); | |
| 180 } else if (state == ShellIntegration::STATE_IS_DEFAULT) { | |
| 181 // This is a noop under systems other than Windows. | |
| 182 #ifdef OS_WIN | |
| 183 BrowserThread::PostTask( | |
| 184 BrowserThread::FILE, FROM_HERE, | |
| 185 base::Bind(&MetroizeHandler::ActivateMetroChrome, | |
| 186 base::Unretained(this))); | |
| 187 #endif | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 bool MetroizeHandler::IsInteractiveSetDefaultPermitted() { | |
| 192 return true; | |
| 193 } | |
| 194 | |
| 195 class MetroizerDialogImpl : public ui::WebDialogDelegate { | |
| 196 public: | |
| 197 MetroizerDialogImpl(Profile* profile, Browser* browser); | |
| 198 void ShowDialog(); | |
| 199 | |
| 200 protected: | |
| 201 // Overridden from WebDialogDelegate: | |
| 202 virtual ui::ModalType GetDialogModalType() const OVERRIDE { | |
| 203 return ui::MODAL_TYPE_SYSTEM; | |
| 204 } | |
| 205 | |
| 206 virtual string16 GetDialogTitle() const OVERRIDE { | |
| 207 return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE); | |
| 208 } | |
| 209 | |
| 210 virtual GURL GetDialogContentURL() const OVERRIDE { | |
| 211 std::string url_string(chrome::kChromeUIMetroFlowURL); | |
| 212 return GURL(url_string); | |
| 213 } | |
| 214 | |
| 215 virtual void GetWebUIMessageHandlers( | |
| 216 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE { | |
| 217 } | |
| 218 | |
| 219 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; | |
| 220 | |
| 221 virtual std::string GetDialogArgs() const OVERRIDE { | |
| 222 return "[]"; | |
| 223 } | |
| 224 | |
| 225 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { | |
| 226 delete this; | |
| 227 } | |
| 228 | |
| 229 virtual void OnCloseContents(WebContents* source, bool* out_close_dialog) | |
| 230 OVERRIDE { | |
| 231 *out_close_dialog = true; | |
| 232 } | |
| 233 | |
| 234 virtual bool ShouldShowDialogTitle() const OVERRIDE { | |
| 235 return true; | |
| 236 } | |
| 237 | |
| 238 virtual bool HandleContextMenu( | |
| 239 const content::ContextMenuParams& params) OVERRIDE { | |
| 240 return true; | |
| 241 } | |
| 242 | |
| 243 private: | |
| 244 Profile* profile_; | |
| 245 Browser* browser_; | |
| 246 | |
| 247 DISALLOW_COPY_AND_ASSIGN(MetroizerDialogImpl); | |
| 248 }; | |
| 249 | |
| 250 MetroizerDialogImpl::MetroizerDialogImpl(Profile* profile, Browser* browser) | |
| 251 : profile_(profile), browser_(browser) { | |
| 252 } | |
| 253 | |
| 254 void MetroizerDialogImpl::ShowDialog() { | |
| 255 browser::ShowWebDialog(browser_->GetFrameNativeWindow(), | |
| 256 profile_, browser_, this); | |
| 257 } | |
| 258 | |
| 259 void MetroizerDialogImpl::GetDialogSize(gfx::Size* size) const { | |
| 260 PrefService* prefs = profile_->GetPrefs(); | |
| 261 gfx::Font approximate_web_font( | |
| 262 prefs->GetString(prefs::kWebKitSansSerifFontFamily), | |
| 263 prefs->GetInteger(prefs::kWebKitDefaultFontSize)); | |
| 264 | |
| 265 *size = ui::GetLocalizedContentsSizeForFont( | |
| 266 IDS_METRO_FLOW_WIDTH_CHARS, IDS_METRO_FLOW_HEIGHT_LINES, | |
| 267 approximate_web_font); | |
| 268 } | |
| 269 | |
| 270 static void ShowMetroizerInDialog(Profile* profile, Browser* browser) { | |
| 271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 272 MetroizerDialogImpl* dialog = new MetroizerDialogImpl(profile, browser); | |
| 273 dialog->ShowDialog(); | |
| 274 } | |
| 275 | |
| 276 static void AddMetroizerTab(Browser* browser) { | |
| 277 GURL url(chrome::kChromeUIMetroFlowURL); | |
| 278 browser::NavigateParams params(browser->GetSingletonTabNavigateParams(url)); | |
| 279 params.path_behavior = browser::NavigateParams::IGNORE_AND_NAVIGATE; | |
| 280 browser->ShowSingletonTabOverwritingNTP(params); | |
| 281 } | |
| 282 | |
| 283 } // namespace | |
| 284 | |
| 285 MetroizerUI::MetroizerUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
| 286 web_ui->AddMessageHandler(new MetroizeHandler()); | |
| 287 ChromeURLDataManager::AddDataSource(Profile::FromWebUI(web_ui), | |
| 288 CreateMetroizerUIHTMLSource()); | |
| 289 } | |
| 290 | |
| 291 // static | |
| 292 void MetroizerUI::Show(Profile* profile, Browser* browser, bool dialog) { | |
| 293 if (dialog) { | |
| 294 // Show the UI. | |
| 295 BrowserThread::PostTask( | |
| 296 BrowserThread::UI, FROM_HERE, | |
| 297 base::Bind(&ShowMetroizerInDialog, profile, browser)); | |
| 298 } else { | |
| 299 BrowserThread::PostTask( | |
| 300 BrowserThread::UI, FROM_HERE, | |
| 301 base::Bind(&AddMetroizerTab, browser)); | |
| 302 } | |
| 303 } | |
| OLD | NEW |