Chromium Code Reviews| Index: chrome/browser/ui/webui/metroizer_ui_win.cc |
| diff --git a/chrome/browser/ui/webui/metroizer_ui_win.cc b/chrome/browser/ui/webui/metroizer_ui_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1b08e2d661f07ba89149fcf2cb488e5879e9d454 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/metroizer_ui_win.cc |
| @@ -0,0 +1,315 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/metroizer_ui_win.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/path_service.h" |
| +#include "chrome/browser/first_run/first_run.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_finder.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| +#include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| +#include "chrome/browser/shell_integration.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "chrome/installer/util/install_util.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_contents_delegate.h" |
| +#include "content/public/browser/web_ui.h" |
| +#include "content/public/browser/web_ui_message_handler.h" |
| +#include "grit/browser_resources.h" |
| +#include "grit/generated_resources.h" |
| +#include "grit/locale_settings.h" |
| +#include "ui/base/l10n/l10n_font_util.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/gfx/font.h" |
| +#include "ui/web_dialogs/web_dialog_delegate.h" |
| + |
| +using content::BrowserThread; |
| +using content::WebContents; |
| +using content::WebUIMessageHandler; |
| + |
| +namespace { |
| + |
| +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.
|
| + ChromeWebUIDataSource * data_source = new ChromeWebUIDataSource( |
| + chrome::kChromeUIMetroFlowHost); |
| + data_source->AddLocalizedString("page-title", IDS_METRO_FLOW_TAB_TITLE); |
| + data_source->AddLocalizedString("flowTitle", IDS_METRO_FLOW_TITLE_SHORT); |
| + data_source->AddLocalizedString("flowDescription", |
| + IDS_METRO_FLOW_DESCRIPTION); |
| + data_source->AddLocalizedString("flowNext", |
| + IDS_METRO_FLOW_SET_DEFAULT); |
| + data_source->AddLocalizedString("flowCancel", |
| + IDS_METRO_FLOW_SET_DEFAULT_CANCEL); |
| + data_source->set_json_path("strings.js"); |
| + data_source->add_resource_path("metroize.js", IDR_METROIZE_JS); |
| + data_source->set_default_resource(IDR_METROIZE_HTML); |
| + return data_source; |
| +} |
| + |
| +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.
|
| + public base::SupportsWeakPtr<MetroizeHandler>, |
| + public ShellIntegration::DefaultWebClientObserver { |
| + public: |
| + MetroizeHandler(); |
| + virtual ~MetroizeHandler(); |
| + |
| + // WebUIMessageHandler implementation. |
| + virtual void RegisterMessages() OVERRIDE; |
| + |
| + // ShellIntegration::DefaultWebClientObserver implementation. |
| + virtual void SetDefaultWebClientUIState( |
| + ShellIntegration::DefaultWebClientUIState state) OVERRIDE; |
| + virtual void OnSetAsDefaultConcluded(bool call_result) OVERRIDE; |
| + virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE; |
| + |
| + private: |
| + void HandleLaunchMetroizeProcedure(const ListValue* args); |
| + void HandleReturnToChrome(const ListValue* args); |
| + void CloseMetroizeWindow(); |
| + void ActivateMetroChrome(); |
| + |
| + scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_; |
| + bool set_default_returned_; |
| + bool set_default_result_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MetroizeHandler); |
| +}; |
| + |
| +MetroizeHandler::MetroizeHandler() |
| + : set_default_returned_(false), set_default_result_(false) { |
| + 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.
|
| +} |
| + |
| +MetroizeHandler::~MetroizeHandler() { |
| + 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.
|
| + default_browser_worker_->ObserverDestroyed(); |
| +} |
| + |
| +void MetroizeHandler::RegisterMessages() { |
| + web_ui()->RegisterMessageCallback( |
| + "Metroize:LaunchSetDefaultBrowserFlow", |
| + base::Bind(&MetroizeHandler::HandleLaunchMetroizeProcedure, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + "Metroize:ReturnToBrowser", |
| + base::Bind(&MetroizeHandler::HandleReturnToChrome, |
| + base::Unretained(this))); |
| +} |
| + |
| +void MetroizeHandler::HandleLaunchMetroizeProcedure(const ListValue* args) { |
| + 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.
|
| + set_default_returned_ = false; |
| + set_default_result_ = false; |
| + default_browser_worker_->StartSetAsDefault(); |
| + } |
| +} |
| + |
| +void MetroizeHandler::HandleReturnToChrome(const ListValue* args) { |
| + CloseMetroizeWindow(); |
| +} |
| + |
| +void MetroizeHandler::CloseMetroizeWindow() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + WebContents* contents = web_ui()->GetWebContents(); |
| + if (contents) { |
| + content::WebContentsDelegate* delegate = contents->GetDelegate(); |
| + if (delegate) { |
| + if (!delegate->IsPopupOrPanel(contents)) { |
| + Browser* browser = browser::FindBrowserWithWebContents(contents); |
| + if (browser) |
| + browser->ShowSyncSetup(SyncPromoUI::SOURCE_START_PAGE); |
| + } |
| + delegate->CloseContents(contents); |
| + } |
| + } |
| +} |
| + |
| +void MetroizeHandler::ActivateMetroChrome() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + |
| + FilePath cur_chrome_exe; |
| + bool sentinel_removed = false; |
| + if (PathService::Get(base::FILE_EXE, &cur_chrome_exe) && |
| + first_run::IsChromeFirstRun() && |
| + InstallUtil::IsPerUserInstall(cur_chrome_exe.value().c_str())) { |
| + // If this is per-user install, we will have to remove the sentinel file |
| + // to assure the user goes through the intended 'first-run flow'. |
| + sentinel_removed = first_run::RemoveSentinel(); |
| + } |
| + |
| + if (ShellIntegration::ActivateMetroChrome()) { |
| + // If Metro Chrome has been activated, we should close this process. |
| + // We are restarting as metro now. |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + base::Bind(&BrowserList::CloseAllBrowsersWithProfile, |
| + Profile::FromWebUI(web_ui()))); |
| + } else { |
| + // This will return false if the operation failed for any reason, |
| + // including invocation under a Windows version earlier than 8. |
| + // In such case we simply close the window and carry on. |
| + if (sentinel_removed) |
| + first_run::CreateSentinel(); |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&MetroizeHandler::CloseMetroizeWindow, |
| + base::Unretained(this))); |
| + } |
| +} |
| + |
| +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.
|
| + ShellIntegration::DefaultWebClientUIState state) { |
| + // The callback is expected to be invoked once the procedure has completed. |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + if (!set_default_returned_) |
| + return; |
| + |
| + if (state == ShellIntegration::STATE_NOT_DEFAULT && set_default_result_) { |
| + // The operation concluded, but Chrome is still not the default. |
| + // If the call has succeeded, this suggests user has decided not to make |
| + // chrome the default. We fold this UI and move on. |
| + CloseMetroizeWindow(); |
| + } else if (state == ShellIntegration::STATE_IS_DEFAULT) { |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&MetroizeHandler::ActivateMetroChrome, |
| + base::Unretained(this))); |
| + } |
| +} |
| + |
| +void MetroizeHandler::OnSetAsDefaultConcluded(bool call_result) { |
| + set_default_returned_ = true; |
| + set_default_result_ = call_result; |
| +} |
| + |
| +bool MetroizeHandler::IsInteractiveSetDefaultPermitted() { |
| + return true; |
| +} |
| + |
| +class MetroizerDialogImpl : public ui::WebDialogDelegate { |
| + public: |
| + MetroizerDialogImpl(Profile* profile, Browser* browser); |
| + void ShowDialog(); |
| + |
| + protected: |
| + // Overridden from WebDialogDelegate: |
| + virtual ui::ModalType GetDialogModalType() const OVERRIDE; |
| + virtual string16 GetDialogTitle() const OVERRIDE; |
| + virtual GURL GetDialogContentURL() const OVERRIDE; |
| + virtual void GetWebUIMessageHandlers( |
| + std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE; |
| + virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; |
| + virtual std::string GetDialogArgs() const OVERRIDE; |
| + virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; |
| + virtual void OnCloseContents(WebContents* source, |
| + bool* out_close_dialog) OVERRIDE; |
| + virtual bool ShouldShowDialogTitle() const OVERRIDE; |
| + virtual bool HandleContextMenu( |
| + const content::ContextMenuParams& params) OVERRIDE; |
| + |
| + private: |
| + Profile* profile_; |
| + Browser* browser_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MetroizerDialogImpl); |
| +}; |
| + |
| +MetroizerDialogImpl::MetroizerDialogImpl(Profile* profile, Browser* browser) |
| + : profile_(profile), browser_(browser) { |
| +} |
| + |
| +void MetroizerDialogImpl::ShowDialog() { |
| + browser_->BrowserShowWebDialog(this, NULL); |
| +} |
| + |
| +ui::ModalType MetroizerDialogImpl::GetDialogModalType() const { |
| + return ui::MODAL_TYPE_SYSTEM; |
| +} |
| + |
| +string16 MetroizerDialogImpl::GetDialogTitle() const { |
| + return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE); |
| +} |
| + |
| +GURL MetroizerDialogImpl::GetDialogContentURL() const { |
| + std::string url_string(chrome::kChromeUIMetroFlowURL); |
| + return GURL(url_string); |
| +} |
| + |
| +void MetroizerDialogImpl::GetWebUIMessageHandlers( |
| + std::vector<WebUIMessageHandler*>* handlers) const { |
| +} |
| + |
| +void MetroizerDialogImpl::GetDialogSize(gfx::Size* size) const { |
| + PrefService* prefs = profile_->GetPrefs(); |
| + gfx::Font approximate_web_font( |
| + prefs->GetString(prefs::kWebKitSansSerifFontFamily), |
| + prefs->GetInteger(prefs::kWebKitDefaultFontSize)); |
| + |
| + *size = ui::GetLocalizedContentsSizeForFont( |
| + IDS_METRO_FLOW_WIDTH_CHARS, IDS_METRO_FLOW_HEIGHT_LINES, |
| + approximate_web_font); |
| +} |
| + |
| +std::string MetroizerDialogImpl::GetDialogArgs() const { |
| + return "[]"; |
| +} |
| + |
| +void MetroizerDialogImpl::OnDialogClosed(const std::string& json_retval) { |
| + delete this; |
| +} |
| + |
| +void MetroizerDialogImpl::OnCloseContents(WebContents* source, |
| + bool* out_close_dialog) { |
| + *out_close_dialog = true; |
| +} |
| + |
| +bool MetroizerDialogImpl::ShouldShowDialogTitle() const { |
| + return true; |
| +} |
| + |
| +bool MetroizerDialogImpl::HandleContextMenu( |
| + const content::ContextMenuParams& params) { |
| + return true; |
| +} |
| + |
| +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.
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + MetroizerDialogImpl* dialog = new MetroizerDialogImpl(profile, browser); |
| + dialog->ShowDialog(); |
| +} |
| + |
| +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.
|
| + GURL url(chrome::kChromeUIMetroFlowURL); |
| + browser::NavigateParams params(browser->GetSingletonTabNavigateParams(url)); |
| + params.path_behavior = browser::NavigateParams::IGNORE_AND_NAVIGATE; |
| + browser->ShowSingletonTabOverwritingNTP(params); |
| +} |
| + |
| +} // namespace |
| + |
| +MetroizerUI::MetroizerUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| + web_ui->AddMessageHandler(new MetroizeHandler()); |
| + ChromeURLDataManager::AddDataSource(Profile::FromWebUI(web_ui), |
| + CreateMetroizerUIHTMLSource()); |
| +} |
| + |
| +// static |
| +void MetroizerUI::Show(Profile* profile, Browser* browser, bool dialog) { |
| + if (dialog) { |
| + // Show the UI. |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&ShowMetroizerInDialog, profile, browser)); |
| + } else { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&AddMetroizerTab, browser)); |
| + } |
| +} |