Chromium Code Reviews| Index: chrome/browser/ui/webui/metroizer_ui.cc |
| diff --git a/chrome/browser/ui/webui/metroizer_ui.cc b/chrome/browser/ui/webui/metroizer_ui.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2c138dc5d779987846cd25c74a3604dd38749f0a |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/metroizer_ui.cc |
| @@ -0,0 +1,303 @@ |
| +// 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
|
| +// 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.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_dialogs.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" |
| +#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.
|
| +#include "chrome/installer/util/install_util.h" |
| +#endif |
| +#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() { |
| + 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, |
| + public base::SupportsWeakPtr<MetroizeHandler>, |
| + public ShellIntegration::DefaultWebClientObserver { |
| + public: |
| + explicit MetroizeHandler(); |
|
grt (UTC plus 2)
2012/06/18 19:19:13
no explicit
motek.
2012/06/19 18:06:44
Done.
|
| + virtual ~MetroizeHandler(); |
| + |
| + // WebUIMessageHandler implementation. |
| + virtual void RegisterMessages() OVERRIDE; |
| + |
| + // ShellIntegration::DefaultWebClientObserver implementation. |
| + virtual void SetDefaultWebClientUIState( |
| + ShellIntegration::DefaultWebClientUIState state, |
| + ShellIntegration::SetDefaultWebClientResult call_result) OVERRIDE; |
| + virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE; |
| + |
| + private: |
| + void HandleLaunchMetroizeProcedure(const ListValue* args); |
| + void HandleReturnToChrome(const ListValue* args); |
| + void CloseMetroizeWindow(); |
| + |
| +#ifdef OS_WIN |
| + void ActivateMetroChrome(); |
| +#endif |
| + |
| + scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MetroizeHandler); |
| +}; |
| + |
| +MetroizeHandler::MetroizeHandler() { |
| + default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this); |
| +} |
| + |
| +MetroizeHandler::~MetroizeHandler() { |
| + if (default_browser_worker_.get()) |
| + 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()) |
| + 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); |
| + } |
| + } |
| +} |
| + |
| +#ifdef OS_WIN |
| +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 |
|
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
|
| + // 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. |
|
grt (UTC plus 2)
2012/06/18 19:19:13
indentation here on down
motek.
2012/06/19 18:06:44
Done.
|
| + // 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))); |
| + } |
| +} |
| +#endif |
| + |
| +void MetroizeHandler::SetDefaultWebClientUIState( |
| + ShellIntegration::DefaultWebClientUIState state, |
|
grt (UTC plus 2)
2012/06/18 19:19:13
indentation
motek.
2012/06/19 18:06:44
Done.
|
| + ShellIntegration::SetDefaultWebClientResult call_result) { |
| + // The callback is expected to be invoked once the procedure has completed. |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + if (state == ShellIntegration::STATE_NOT_DEFAULT && |
| + call_result == ShellIntegration::RESULT_SET_DEFAULT_OK) { |
| + // 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) { |
| + // This is a noop under systems other than Windows. |
| +#ifdef OS_WIN |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&MetroizeHandler::ActivateMetroChrome, |
| + base::Unretained(this))); |
| +#endif |
| + } |
| +} |
| + |
| +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 { |
| + return ui::MODAL_TYPE_SYSTEM; |
| + } |
| + |
| + virtual string16 GetDialogTitle() const OVERRIDE { |
| + return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE); |
| + } |
| + |
| + virtual GURL GetDialogContentURL() const OVERRIDE { |
| + std::string url_string(chrome::kChromeUIMetroFlowURL); |
| + return GURL(url_string); |
| + } |
| + |
| + virtual void GetWebUIMessageHandlers( |
| + std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE { |
| + } |
| + |
| + virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; |
| + |
| + virtual std::string GetDialogArgs() const OVERRIDE { |
| + return "[]"; |
| + } |
| + |
| + virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { |
| + delete this; |
| + } |
| + |
| + virtual void OnCloseContents(WebContents* source, bool* out_close_dialog) |
| + OVERRIDE { |
| + *out_close_dialog = true; |
| + } |
| + |
| + virtual bool ShouldShowDialogTitle() const OVERRIDE { |
| + return true; |
| + } |
| + |
| + virtual bool HandleContextMenu( |
| + const content::ContextMenuParams& params) OVERRIDE { |
| + return true; |
| + } |
| + |
| + private: |
| + Profile* profile_; |
| + Browser* browser_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MetroizerDialogImpl); |
| +}; |
| + |
| +MetroizerDialogImpl::MetroizerDialogImpl(Profile* profile, Browser* browser) |
| + : profile_(profile), browser_(browser) { |
| +} |
| + |
| +void MetroizerDialogImpl::ShowDialog() { |
| + browser::ShowWebDialog(browser_->GetFrameNativeWindow(), |
| + profile_, browser_, this); |
| +} |
| + |
| +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); |
| +} |
| + |
| +static void ShowMetroizerInDialog(Profile* profile, Browser* browser) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + MetroizerDialogImpl* dialog = new MetroizerDialogImpl(profile, browser); |
| + dialog->ShowDialog(); |
| +} |
| + |
| +static void AddMetroizerTab(Browser* browser) { |
| + 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)); |
| + } |
| +} |