OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/chromeos/locale_change_guard.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/metrics/user_metrics.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/tab_contents/tab_contents.h" |
| 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/common/pref_names.h" |
| 16 #include "grit/app_resources.h" |
| 17 #include "grit/generated_resources.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 base::LazyInstance<chromeos::LocaleChangeGuard> g_locale_change_guard( |
| 22 base::LINKER_INITIALIZED); |
| 23 |
| 24 } // namespace |
| 25 |
| 26 namespace chromeos { |
| 27 |
| 28 LocaleChangeGuard::LocaleChangeGuard() |
| 29 : profile_id_(Profile::InvalidProfileId), |
| 30 tab_contents_(NULL), |
| 31 note_(NULL), |
| 32 reverted_(false) { |
| 33 } |
| 34 |
| 35 void LocaleChangeGuard::RevertLocaleChange(const ListValue* list) { |
| 36 reverted_ = true; |
| 37 UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Revert")); |
| 38 tab_contents_->profile()->ChangeApplicationLocale(from_locale_, true); |
| 39 PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
| 40 if (prefs) { |
| 41 prefs->SetString(prefs::kApplicationLocaleBackup, from_locale_); |
| 42 prefs->ClearPref(prefs::kApplicationLocaleAccepted); |
| 43 prefs->ScheduleSavePersistentPrefs(); |
| 44 } |
| 45 Browser* browser = Browser::GetBrowserForController( |
| 46 &tab_contents_->controller(), NULL); |
| 47 if (browser) |
| 48 browser->ExecuteCommand(IDC_EXIT); |
| 49 } |
| 50 |
| 51 void LocaleChangeGuard::CheckLocaleChange(TabContents* tab_contents) { |
| 52 // We want notification to be shown no more than once per session. |
| 53 if (note_ != NULL) |
| 54 return; |
| 55 // We check profile Id because: |
| 56 // (1) we want to exit fast in common case when nothing should be done. |
| 57 // (2) on ChromeOS this guard may be invoked for a dummy profile first time. |
| 58 ProfileId cur_profile_id = tab_contents->profile()->GetRuntimeId(); |
| 59 if (cur_profile_id == profile_id_) |
| 60 return; |
| 61 profile_id_ = cur_profile_id; |
| 62 std::string cur_locale = g_browser_process->GetApplicationLocale(); |
| 63 if (cur_locale.empty()) |
| 64 return; |
| 65 PrefService* prefs = tab_contents->profile()->GetPrefs(); |
| 66 if (prefs == NULL) |
| 67 return; |
| 68 to_locale_ = prefs->GetString(prefs::kApplicationLocaleOverride); |
| 69 if (!to_locale_.empty()) { |
| 70 DCHECK(to_locale_ == cur_locale); |
| 71 return; |
| 72 } |
| 73 to_locale_ = prefs->GetString(prefs::kApplicationLocale); |
| 74 if (to_locale_ != cur_locale) |
| 75 return; |
| 76 from_locale_ = prefs->GetString(prefs::kApplicationLocaleBackup); |
| 77 if (from_locale_.empty() || from_locale_ == to_locale_) |
| 78 return; |
| 79 note_.reset(new chromeos::SystemNotification( |
| 80 tab_contents->profile(), |
| 81 new Delegate(this), |
| 82 IDR_DEFAULT_FAVICON, |
| 83 l10n_util::GetStringUTF16( |
| 84 IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE))); |
| 85 tab_contents_ = tab_contents; |
| 86 note_->Show( |
| 87 l10n_util::GetStringFUTF16( |
| 88 IDS_LOCALE_CHANGE_MESSAGE, |
| 89 l10n_util::GetDisplayNameForLocale(from_locale_, to_locale_, true), |
| 90 l10n_util::GetDisplayNameForLocale(to_locale_, to_locale_, true)), |
| 91 l10n_util::GetStringUTF16(IDS_LOCALE_CHANGE_REVERT_MESSAGE), |
| 92 NewCallback(this, &LocaleChangeGuard::RevertLocaleChange), |
| 93 true, // urgent |
| 94 false); // non-sticky |
| 95 } |
| 96 |
| 97 void LocaleChangeGuard::AcceptLocaleChange() { |
| 98 // Check whether locale has been reverted or changed. |
| 99 // If not: mark current locale as accepted. |
| 100 if (tab_contents_ == NULL) |
| 101 return; |
| 102 if (reverted_) |
| 103 return; |
| 104 PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
| 105 if (prefs == NULL) |
| 106 return; |
| 107 std::string override_locale = |
| 108 prefs->GetString(prefs::kApplicationLocaleOverride); |
| 109 if (!override_locale.empty()) |
| 110 return; |
| 111 if (prefs->GetString(prefs::kApplicationLocale) != to_locale_) |
| 112 return; |
| 113 UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Accept")); |
| 114 prefs->SetString(prefs::kApplicationLocaleBackup, to_locale_); |
| 115 prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_); |
| 116 prefs->ScheduleSavePersistentPrefs(); |
| 117 } |
| 118 |
| 119 // static |
| 120 void LocaleChangeGuard::Check(TabContents* tab_contents) { |
| 121 g_locale_change_guard.Get().CheckLocaleChange(tab_contents); |
| 122 } |
| 123 |
| 124 void LocaleChangeGuard::Delegate::Close(bool by_user) { |
| 125 if (by_user) |
| 126 master_->AcceptLocaleChange(); |
| 127 } |
| 128 |
| 129 std::string LocaleChangeGuard::Delegate::id() const { |
| 130 // Arbitrary unique Id. |
| 131 return "8c386938-1e3f-11e0-ac7b-18a90520e2e5"; |
| 132 } |
| 133 |
| 134 } // namespace chromeos |
OLD | NEW |