OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/locale_change_guard.h" | 5 #include "chrome/browser/chromeos/locale_change_guard.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 namespace chromeos { | 26 namespace chromeos { |
27 | 27 |
28 LocaleChangeGuard::LocaleChangeGuard() | 28 LocaleChangeGuard::LocaleChangeGuard() |
29 : profile_id_(Profile::InvalidProfileId), | 29 : profile_id_(Profile::InvalidProfileId), |
30 tab_contents_(NULL), | 30 tab_contents_(NULL), |
31 note_(NULL), | 31 note_(NULL), |
32 reverted_(false) { | 32 reverted_(false) { |
33 } | 33 } |
34 | 34 |
35 void LocaleChangeGuard::RevertLocaleChange(const ListValue* list) { | 35 void LocaleChangeGuard::RevertLocaleChange(const ListValue* list) { |
| 36 if (note_ == NULL || |
| 37 tab_contents_ == NULL || |
| 38 from_locale_.empty() || |
| 39 to_locale_.empty()) { |
| 40 NOTREACHED(); |
| 41 return; |
| 42 } |
| 43 if (reverted_) |
| 44 return; |
| 45 |
| 46 PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
| 47 if (prefs == NULL) |
| 48 return; |
| 49 |
36 reverted_ = true; | 50 reverted_ = true; |
37 UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Revert")); | 51 UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Revert")); |
38 tab_contents_->profile()->ChangeApplicationLocale(from_locale_, true); | 52 tab_contents_->profile()->ChangeApplicationLocale(from_locale_, true); |
39 PrefService* prefs = tab_contents_->profile()->GetPrefs(); | 53 prefs->SetString(prefs::kApplicationLocaleBackup, from_locale_); |
40 if (prefs) { | 54 prefs->ClearPref(prefs::kApplicationLocaleAccepted); |
41 prefs->SetString(prefs::kApplicationLocaleBackup, from_locale_); | 55 prefs->ScheduleSavePersistentPrefs(); |
42 prefs->ClearPref(prefs::kApplicationLocaleAccepted); | 56 |
43 prefs->ScheduleSavePersistentPrefs(); | |
44 } | |
45 Browser* browser = Browser::GetBrowserForController( | 57 Browser* browser = Browser::GetBrowserForController( |
46 &tab_contents_->controller(), NULL); | 58 &tab_contents_->controller(), NULL); |
47 if (browser) | 59 if (browser) |
48 browser->ExecuteCommand(IDC_EXIT); | 60 browser->ExecuteCommand(IDC_EXIT); |
49 } | 61 } |
50 | 62 |
51 void LocaleChangeGuard::CheckLocaleChange(TabContents* tab_contents) { | 63 void LocaleChangeGuard::CheckLocaleChange(TabContents* tab_contents) { |
52 // We want notification to be shown no more than once per session. | 64 // We want notification to be shown no more than once per session. |
53 if (note_ != NULL) | 65 if (note_ != NULL) |
54 return; | 66 return; |
| 67 DCHECK(note_ == NULL && tab_contents_ == NULL); |
| 68 DCHECK(from_locale_.empty() && to_locale_.empty()); |
| 69 |
55 // We check profile Id because: | 70 // We check profile Id because: |
56 // (1) we want to exit fast in common case when nothing should be done. | 71 // (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. | 72 // (2) on ChromeOS this guard may be invoked for a dummy profile first time. |
58 ProfileId cur_profile_id = tab_contents->profile()->GetRuntimeId(); | 73 ProfileId cur_profile_id = tab_contents->profile()->GetRuntimeId(); |
59 if (cur_profile_id == profile_id_) | 74 if (cur_profile_id == profile_id_) { |
| 75 // We have already checked this profile, exiting fast. |
60 return; | 76 return; |
| 77 } |
61 profile_id_ = cur_profile_id; | 78 profile_id_ = cur_profile_id; |
| 79 |
62 std::string cur_locale = g_browser_process->GetApplicationLocale(); | 80 std::string cur_locale = g_browser_process->GetApplicationLocale(); |
63 if (cur_locale.empty()) | 81 if (cur_locale.empty()) { |
| 82 NOTREACHED(); |
64 return; | 83 return; |
| 84 } |
| 85 |
65 PrefService* prefs = tab_contents->profile()->GetPrefs(); | 86 PrefService* prefs = tab_contents->profile()->GetPrefs(); |
66 if (prefs == NULL) | 87 if (prefs == NULL) |
67 return; | 88 return; |
68 to_locale_ = prefs->GetString(prefs::kApplicationLocaleOverride); | 89 |
69 if (!to_locale_.empty()) { | 90 std::string to_locale = prefs->GetString(prefs::kApplicationLocaleOverride); |
70 DCHECK(to_locale_ == cur_locale); | 91 if (!to_locale.empty()) { |
| 92 DCHECK(to_locale == cur_locale); |
71 return; | 93 return; |
72 } | 94 } |
73 to_locale_ = prefs->GetString(prefs::kApplicationLocale); | 95 |
74 if (to_locale_ != cur_locale) | 96 to_locale = prefs->GetString(prefs::kApplicationLocale); |
| 97 if (to_locale != cur_locale) { |
| 98 // This conditional branch can occur in case kApplicationLocale |
| 99 // preference was modified by synchronization. |
75 return; | 100 return; |
76 from_locale_ = prefs->GetString(prefs::kApplicationLocaleBackup); | 101 } |
77 if (from_locale_.empty() || from_locale_ == to_locale_) | 102 |
| 103 std::string from_locale = prefs->GetString(prefs::kApplicationLocaleBackup); |
| 104 if (from_locale.empty() || from_locale == to_locale) { |
| 105 // No locale change was detected, just exit. |
78 return; | 106 return; |
| 107 } |
| 108 |
| 109 // Locale change detected, showing notification. |
| 110 from_locale_ = from_locale; |
| 111 to_locale_ = to_locale; |
| 112 tab_contents_ = tab_contents; |
79 note_.reset(new chromeos::SystemNotification( | 113 note_.reset(new chromeos::SystemNotification( |
80 tab_contents->profile(), | 114 tab_contents->profile(), |
81 new Delegate(this), | 115 new Delegate(this), |
82 IDR_DEFAULT_FAVICON, | 116 IDR_DEFAULT_FAVICON, |
83 l10n_util::GetStringUTF16( | 117 l10n_util::GetStringUTF16( |
84 IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE))); | 118 IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE))); |
85 tab_contents_ = tab_contents; | |
86 note_->Show( | 119 note_->Show( |
87 l10n_util::GetStringFUTF16( | 120 l10n_util::GetStringFUTF16( |
88 IDS_LOCALE_CHANGE_MESSAGE, | 121 IDS_LOCALE_CHANGE_MESSAGE, |
89 l10n_util::GetDisplayNameForLocale(from_locale_, to_locale_, true), | 122 l10n_util::GetDisplayNameForLocale(from_locale_, to_locale_, true), |
90 l10n_util::GetDisplayNameForLocale(to_locale_, to_locale_, true)), | 123 l10n_util::GetDisplayNameForLocale(to_locale_, to_locale_, true)), |
91 l10n_util::GetStringUTF16(IDS_LOCALE_CHANGE_REVERT_MESSAGE), | 124 l10n_util::GetStringUTF16(IDS_LOCALE_CHANGE_REVERT_MESSAGE), |
92 NewCallback(this, &LocaleChangeGuard::RevertLocaleChange), | 125 NewCallback(this, &LocaleChangeGuard::RevertLocaleChange), |
93 true, // urgent | 126 true, // urgent |
94 false); // non-sticky | 127 false); // non-sticky |
95 } | 128 } |
96 | 129 |
97 void LocaleChangeGuard::AcceptLocaleChange() { | 130 void LocaleChangeGuard::AcceptLocaleChange() { |
| 131 if (note_ == NULL || |
| 132 tab_contents_ == NULL || |
| 133 from_locale_.empty() || |
| 134 to_locale_.empty()) { |
| 135 NOTREACHED(); |
| 136 return; |
| 137 } |
| 138 |
98 // Check whether locale has been reverted or changed. | 139 // Check whether locale has been reverted or changed. |
99 // If not: mark current locale as accepted. | 140 // If not: mark current locale as accepted. |
100 if (tab_contents_ == NULL) | |
101 return; | |
102 if (reverted_) | 141 if (reverted_) |
103 return; | 142 return; |
104 PrefService* prefs = tab_contents_->profile()->GetPrefs(); | 143 PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
105 if (prefs == NULL) | 144 if (prefs == NULL) |
106 return; | 145 return; |
107 std::string override_locale = | 146 std::string override_locale = |
108 prefs->GetString(prefs::kApplicationLocaleOverride); | 147 prefs->GetString(prefs::kApplicationLocaleOverride); |
109 if (!override_locale.empty()) | 148 if (!override_locale.empty()) |
110 return; | 149 return; |
111 if (prefs->GetString(prefs::kApplicationLocale) != to_locale_) | 150 if (prefs->GetString(prefs::kApplicationLocale) != to_locale_) |
(...skipping 13 matching lines...) Expand all Loading... |
125 if (by_user) | 164 if (by_user) |
126 master_->AcceptLocaleChange(); | 165 master_->AcceptLocaleChange(); |
127 } | 166 } |
128 | 167 |
129 std::string LocaleChangeGuard::Delegate::id() const { | 168 std::string LocaleChangeGuard::Delegate::id() const { |
130 // Arbitrary unique Id. | 169 // Arbitrary unique Id. |
131 return "8c386938-1e3f-11e0-ac7b-18a90520e2e5"; | 170 return "8c386938-1e3f-11e0-ac7b-18a90520e2e5"; |
132 } | 171 } |
133 | 172 |
134 } // namespace chromeos | 173 } // namespace chromeos |
OLD | NEW |