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() { | |
|
grt (UTC plus 2)
2012/06/20 19:42:12
no space between ChromeWebUIDataSource and *
motek.
2012/06/20 22:02:29
Done.
| |
| 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 class MetroizeHandler : public WebUIMessageHandler, | |
|
grt (UTC plus 2)
2012/06/20 19:42:12
please add doc comments to the classes in this fil
motek.
2012/06/20 22:02:29
Done.
| |
| 57 public base::SupportsWeakPtr<MetroizeHandler>, | |
| 58 public ShellIntegration::DefaultWebClientObserver { | |
| 59 public: | |
| 60 MetroizeHandler(); | |
| 61 virtual ~MetroizeHandler(); | |
| 62 | |
| 63 // WebUIMessageHandler implementation. | |
| 64 virtual void RegisterMessages() OVERRIDE; | |
| 65 | |
| 66 // ShellIntegration::DefaultWebClientObserver implementation. | |
| 67 virtual void SetDefaultWebClientUIState( | |
| 68 ShellIntegration::DefaultWebClientUIState state) OVERRIDE; | |
| 69 virtual void OnSetAsDefaultConcluded(bool call_result) OVERRIDE; | |
| 70 virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE; | |
| 71 | |
| 72 private: | |
| 73 void HandleLaunchMetroizeProcedure(const ListValue* args); | |
| 74 void HandleReturnToChrome(const ListValue* args); | |
| 75 void CloseMetroizeWindow(); | |
| 76 void ActivateMetroChrome(); | |
| 77 | |
| 78 scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_; | |
| 79 bool set_default_returned_; | |
| 80 bool set_default_result_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(MetroizeHandler); | |
| 83 }; | |
| 84 | |
| 85 MetroizeHandler::MetroizeHandler() | |
| 86 : set_default_returned_(false), set_default_result_(false) { | |
| 87 default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this); | |
|
grt (UTC plus 2)
2012/06/20 19:42:12
please move this into the initalizer list:
: ALLOW
motek.
2012/06/20 22:02:29
Done.
| |
| 88 } | |
| 89 | |
| 90 MetroizeHandler::~MetroizeHandler() { | |
| 91 if (default_browser_worker_.get()) | |
|
grt (UTC plus 2)
2012/06/20 19:42:12
why this check?
motek.
2012/06/20 22:02:29
Not needed anymore. Done.
| |
| 92 default_browser_worker_->ObserverDestroyed(); | |
| 93 } | |
| 94 | |
| 95 void MetroizeHandler::RegisterMessages() { | |
| 96 web_ui()->RegisterMessageCallback( | |
| 97 "Metroize:LaunchSetDefaultBrowserFlow", | |
| 98 base::Bind(&MetroizeHandler::HandleLaunchMetroizeProcedure, | |
| 99 base::Unretained(this))); | |
| 100 web_ui()->RegisterMessageCallback( | |
| 101 "Metroize:ReturnToBrowser", | |
| 102 base::Bind(&MetroizeHandler::HandleReturnToChrome, | |
| 103 base::Unretained(this))); | |
| 104 } | |
| 105 | |
| 106 void MetroizeHandler::HandleLaunchMetroizeProcedure(const ListValue* args) { | |
| 107 if (default_browser_worker_.get()) { | |
|
grt (UTC plus 2)
2012/06/20 19:42:12
why is this check needed? default_browser_worker_
motek.
2012/06/20 22:02:29
Done.
| |
| 108 set_default_returned_ = false; | |
| 109 set_default_result_ = false; | |
| 110 default_browser_worker_->StartSetAsDefault(); | |
| 111 } | |
| 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 void MetroizeHandler::ActivateMetroChrome() { | |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 136 | |
| 137 FilePath cur_chrome_exe; | |
| 138 bool sentinel_removed = false; | |
| 139 if (PathService::Get(base::FILE_EXE, &cur_chrome_exe) && | |
| 140 first_run::IsChromeFirstRun() && | |
| 141 InstallUtil::IsPerUserInstall(cur_chrome_exe.value().c_str())) { | |
| 142 // If this is per-user install, we will have to remove the sentinel file | |
| 143 // to assure the user goes through the intended 'first-run flow'. | |
| 144 sentinel_removed = first_run::RemoveSentinel(); | |
| 145 } | |
| 146 | |
| 147 if (ShellIntegration::ActivateMetroChrome()) { | |
| 148 // If Metro Chrome has been activated, we should close this process. | |
| 149 // We are restarting as metro now. | |
| 150 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 151 base::Bind(&BrowserList::CloseAllBrowsersWithProfile, | |
| 152 Profile::FromWebUI(web_ui()))); | |
| 153 } else { | |
| 154 // This will return false if the operation failed for any reason, | |
| 155 // including invocation under a Windows version earlier than 8. | |
| 156 // In such case we simply close the window and carry on. | |
| 157 if (sentinel_removed) | |
| 158 first_run::CreateSentinel(); | |
| 159 | |
| 160 BrowserThread::PostTask( | |
| 161 BrowserThread::UI, FROM_HERE, | |
| 162 base::Bind(&MetroizeHandler::CloseMetroizeWindow, | |
| 163 base::Unretained(this))); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 void MetroizeHandler::SetDefaultWebClientUIState( | |
|
grt (UTC plus 2)
2012/06/20 19:42:12
the order of method definitions should match the o
motek.
2012/06/20 22:02:29
Done.
| |
| 168 ShellIntegration::DefaultWebClientUIState state) { | |
| 169 // The callback is expected to be invoked once the procedure has completed. | |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 171 if (!set_default_returned_) | |
| 172 return; | |
| 173 | |
| 174 if (state == ShellIntegration::STATE_NOT_DEFAULT && set_default_result_) { | |
| 175 // The operation concluded, but Chrome is still not the default. | |
| 176 // If the call has succeeded, this suggests user has decided not to make | |
| 177 // chrome the default. We fold this UI and move on. | |
| 178 CloseMetroizeWindow(); | |
| 179 } else if (state == ShellIntegration::STATE_IS_DEFAULT) { | |
| 180 BrowserThread::PostTask( | |
| 181 BrowserThread::FILE, FROM_HERE, | |
| 182 base::Bind(&MetroizeHandler::ActivateMetroChrome, | |
| 183 base::Unretained(this))); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 void MetroizeHandler::OnSetAsDefaultConcluded(bool call_result) { | |
| 188 set_default_returned_ = true; | |
| 189 set_default_result_ = call_result; | |
| 190 } | |
| 191 | |
| 192 bool MetroizeHandler::IsInteractiveSetDefaultPermitted() { | |
| 193 return true; | |
| 194 } | |
| 195 | |
| 196 class MetroizerDialogImpl : public ui::WebDialogDelegate { | |
| 197 public: | |
| 198 MetroizerDialogImpl(Profile* profile, Browser* browser); | |
| 199 void ShowDialog(); | |
| 200 | |
| 201 protected: | |
| 202 // Overridden from WebDialogDelegate: | |
| 203 virtual ui::ModalType GetDialogModalType() const OVERRIDE; | |
| 204 virtual string16 GetDialogTitle() const OVERRIDE; | |
| 205 virtual GURL GetDialogContentURL() const OVERRIDE; | |
| 206 virtual void GetWebUIMessageHandlers( | |
| 207 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE; | |
| 208 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; | |
| 209 virtual std::string GetDialogArgs() const OVERRIDE; | |
| 210 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; | |
| 211 virtual void OnCloseContents(WebContents* source, | |
| 212 bool* out_close_dialog) OVERRIDE; | |
| 213 virtual bool ShouldShowDialogTitle() const OVERRIDE; | |
| 214 virtual bool HandleContextMenu( | |
| 215 const content::ContextMenuParams& params) OVERRIDE; | |
| 216 | |
| 217 private: | |
| 218 Profile* profile_; | |
| 219 Browser* browser_; | |
| 220 | |
| 221 DISALLOW_COPY_AND_ASSIGN(MetroizerDialogImpl); | |
| 222 }; | |
| 223 | |
| 224 MetroizerDialogImpl::MetroizerDialogImpl(Profile* profile, Browser* browser) | |
| 225 : profile_(profile), browser_(browser) { | |
| 226 } | |
| 227 | |
| 228 void MetroizerDialogImpl::ShowDialog() { | |
| 229 browser_->BrowserShowWebDialog(this, NULL); | |
| 230 } | |
| 231 | |
| 232 ui::ModalType MetroizerDialogImpl::GetDialogModalType() const { | |
| 233 return ui::MODAL_TYPE_SYSTEM; | |
| 234 } | |
| 235 | |
| 236 string16 MetroizerDialogImpl::GetDialogTitle() const { | |
| 237 return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE); | |
| 238 } | |
| 239 | |
| 240 GURL MetroizerDialogImpl::GetDialogContentURL() const { | |
| 241 std::string url_string(chrome::kChromeUIMetroFlowURL); | |
| 242 return GURL(url_string); | |
| 243 } | |
| 244 | |
| 245 void MetroizerDialogImpl::GetWebUIMessageHandlers( | |
| 246 std::vector<WebUIMessageHandler*>* handlers) const { | |
| 247 } | |
| 248 | |
| 249 void MetroizerDialogImpl::GetDialogSize(gfx::Size* size) const { | |
| 250 PrefService* prefs = profile_->GetPrefs(); | |
| 251 gfx::Font approximate_web_font( | |
| 252 prefs->GetString(prefs::kWebKitSansSerifFontFamily), | |
| 253 prefs->GetInteger(prefs::kWebKitDefaultFontSize)); | |
| 254 | |
| 255 *size = ui::GetLocalizedContentsSizeForFont( | |
| 256 IDS_METRO_FLOW_WIDTH_CHARS, IDS_METRO_FLOW_HEIGHT_LINES, | |
| 257 approximate_web_font); | |
| 258 } | |
| 259 | |
| 260 std::string MetroizerDialogImpl::GetDialogArgs() const { | |
| 261 return "[]"; | |
| 262 } | |
| 263 | |
| 264 void MetroizerDialogImpl::OnDialogClosed(const std::string& json_retval) { | |
| 265 delete this; | |
| 266 } | |
| 267 | |
| 268 void MetroizerDialogImpl::OnCloseContents(WebContents* source, | |
| 269 bool* out_close_dialog) { | |
| 270 *out_close_dialog = true; | |
| 271 } | |
| 272 | |
| 273 bool MetroizerDialogImpl::ShouldShowDialogTitle() const { | |
| 274 return true; | |
| 275 } | |
| 276 | |
| 277 bool MetroizerDialogImpl::HandleContextMenu( | |
| 278 const content::ContextMenuParams& params) { | |
| 279 return true; | |
| 280 } | |
| 281 | |
| 282 static void ShowMetroizerInDialog(Profile* profile, Browser* browser) { | |
|
grt (UTC plus 2)
2012/06/20 19:42:12
no static
motek.
2012/06/20 22:02:29
Done.
| |
| 283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 284 MetroizerDialogImpl* dialog = new MetroizerDialogImpl(profile, browser); | |
| 285 dialog->ShowDialog(); | |
| 286 } | |
| 287 | |
| 288 static void AddMetroizerTab(Browser* browser) { | |
|
grt (UTC plus 2)
2012/06/20 19:42:12
no static
motek.
2012/06/20 22:02:29
Done.
| |
| 289 GURL url(chrome::kChromeUIMetroFlowURL); | |
| 290 browser::NavigateParams params(browser->GetSingletonTabNavigateParams(url)); | |
| 291 params.path_behavior = browser::NavigateParams::IGNORE_AND_NAVIGATE; | |
| 292 browser->ShowSingletonTabOverwritingNTP(params); | |
| 293 } | |
| 294 | |
| 295 } // namespace | |
| 296 | |
| 297 MetroizerUI::MetroizerUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
| 298 web_ui->AddMessageHandler(new MetroizeHandler()); | |
| 299 ChromeURLDataManager::AddDataSource(Profile::FromWebUI(web_ui), | |
| 300 CreateMetroizerUIHTMLSource()); | |
| 301 } | |
| 302 | |
| 303 // static | |
| 304 void MetroizerUI::Show(Profile* profile, Browser* browser, bool dialog) { | |
| 305 if (dialog) { | |
| 306 // Show the UI. | |
| 307 BrowserThread::PostTask( | |
| 308 BrowserThread::UI, FROM_HERE, | |
| 309 base::Bind(&ShowMetroizerInDialog, profile, browser)); | |
| 310 } else { | |
| 311 BrowserThread::PostTask( | |
| 312 BrowserThread::UI, FROM_HERE, | |
| 313 base::Bind(&AddMetroizerTab, browser)); | |
| 314 } | |
| 315 } | |
| OLD | NEW |