Chromium Code Reviews| Index: chrome/browser/tab_contents/locale_change_guard.cc |
| diff --git a/chrome/browser/tab_contents/locale_change_guard.cc b/chrome/browser/tab_contents/locale_change_guard.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1ea16723567e6847aeeea8d8fc40a7498f036955 |
| --- /dev/null |
| +++ b/chrome/browser/tab_contents/locale_change_guard.cc |
| @@ -0,0 +1,155 @@ |
| +// Copyright (c) 2011 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/tab_contents/locale_change_guard.h" |
| + |
| +#include "app/l10n_util.h" |
| +#include "base/lazy_instance.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/metrics/user_metrics.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/tab_contents/infobar_delegate.h" |
| +#include "chrome/browser/tab_contents/tab_contents.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "grit/generated_resources.h" |
| + |
| +#if defined(OS_CHROMEOS) |
| +namespace { |
| + |
| +class LocaleChangeInfoBar : public ConfirmInfoBarDelegate { |
| + public: |
| + LocaleChangeInfoBar(TabContents* tab_contents, |
| + const std::string& from_locale, |
| + const std::string& to_locale) |
| + : ConfirmInfoBarDelegate(tab_contents), |
| + tab_contents_(tab_contents), |
| + from_locale_(from_locale), |
| + to_locale_(to_locale) {} |
| + |
| + virtual int GetButtons() const { |
| + return BUTTON_OK | BUTTON_CANCEL; |
| + } |
| + |
| + virtual bool NeedElevation(InfoBarButton button) const { |
| + return button == BUTTON_CANCEL; |
| + } |
| + |
| + virtual string16 GetButtonLabel(InfoBarButton button) const { |
| + if (button == BUTTON_OK) |
|
whywhat
2011/01/11 10:24:14
So OK is revert and Cancel is Awesome? Not vice ve
Denis Lagno
2011/01/12 15:33:28
it was deliberate.
Not applicable anymore.
|
| + return l10n_util::GetStringUTF16(IDS_LOCALE_CHANGE_REVERT_MESSAGE); |
| + if (button == BUTTON_CANCEL) |
| + return l10n_util::GetStringUTF16(IDS_AWESOME); |
| + return ConfirmInfoBarDelegate::GetButtonLabel(button); |
| + } |
| + |
| + virtual string16 GetMessageText() const { |
| + return l10n_util::GetStringFUTF16( |
| + IDS_LOCALE_CHANGE_MESSAGE, |
| + l10n_util::GetDisplayNameForLocale(from_locale_, to_locale_, true), |
| + l10n_util::GetDisplayNameForLocale(to_locale_, to_locale_, true)); |
| + } |
| + |
| + virtual bool Accept() { |
| + UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Revert")); |
| + // Revert locale change. |
| + tab_contents_->profile()->ChangeApplicationLocale(from_locale_, true); |
| + PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
| + if (prefs) { |
| + prefs->SetString(prefs::kApplicationLocaleBackup, from_locale_); |
| + prefs->ClearPref(prefs::kApplicationLocaleAccepted); |
| + prefs->ScheduleSavePersistentPrefs(); |
| + } |
| + Browser* browser = Browser::GetBrowserForController( |
| + &tab_contents_->controller(), NULL); |
| + if (browser) |
| + browser->ExecuteCommand(IDC_EXIT); |
| + return true; |
| + } |
| + |
| + virtual bool Cancel() { |
| + UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Accept")); |
| + PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
| + if (prefs) { |
| + prefs->SetString(prefs::kApplicationLocaleBackup, to_locale_); |
| + prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_); |
| + prefs->ScheduleSavePersistentPrefs(); |
| + } |
| + return true; |
| + } |
| + |
| + virtual bool ShouldExpire( |
| + const NavigationController::LoadCommittedDetails& details) const { |
| + return false; |
| + } |
| + |
| + virtual void InfoBarDismissed() { |
| + Cancel(); |
| + } |
| + |
| + private: |
| + TabContents* tab_contents_; |
| + std::string from_locale_, to_locale_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(LocaleChangeInfoBar); |
| +}; |
| + |
| +class LocaleChangeGuardImpl { |
| + LocaleChangeGuardImpl() |
| + : profile_id_(Profile::InvalidProfileId), |
| + bar_(NULL) {} |
| + |
| + void Check(TabContents* tab_contents) { |
| + // We want Locale Change InfoBar to be shown no more than once per session. |
| + if (bar_ != NULL) |
| + return; |
| + // We check profile fingerprint because on ChromeOS this guard |
| + // first time is invoked for a dummy profile. |
| + ProfileId cur_profile_id = tab_contents->profile()->GetRuntimeId(); |
| + if (cur_profile_id == profile_id_) |
| + return; |
| + profile_id_ = cur_profile_id; |
| + std::string cur_locale = g_browser_process->GetApplicationLocale(); |
| + if (cur_locale.empty()) |
| + return; |
| + PrefService* prefs = tab_contents->profile()->GetPrefs(); |
| + if (prefs == NULL) |
| + return; |
| + std::string to_locale = |
| + prefs->GetString(prefs::kApplicationLocaleOverride); |
| + if (!to_locale.empty()) { |
| + DCHECK(to_locale == cur_locale); |
| + return; |
| + } |
| + to_locale = prefs->GetString(prefs::kApplicationLocale); |
| + if (to_locale != cur_locale) |
| + return; |
| + std::string from_locale = prefs->GetString(prefs::kApplicationLocaleBackup); |
| + if (from_locale.empty() || from_locale == to_locale) |
| + return; |
| + bar_.reset(new LocaleChangeInfoBar(tab_contents, from_locale, to_locale)); |
| + tab_contents->AddInfoBar(bar_.get()); |
| + } |
| + |
| + ProfileId profile_id_; |
| + scoped_ptr<LocaleChangeInfoBar> bar_; |
| + |
| + friend struct base::DefaultLazyInstanceTraits<LocaleChangeGuardImpl>; |
| + friend struct ::LocaleChangeGuard; |
| +}; |
| + |
| +base::LazyInstance<LocaleChangeGuardImpl> g_locale_change_guard( |
| + base::LINKER_INITIALIZED); |
| + |
| +} // namespace |
| +#endif // defined(OS_CHROMEOS) |
| + |
| +// static |
| +void LocaleChangeGuard::Check(TabContents* tab_contents) { |
| +#if defined(OS_CHROMEOS) |
| + g_locale_change_guard.Get().Check(tab_contents); |
| +#endif |
| +} |