| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> |
| 8 |
| 7 #include "ash/shell.h" | 9 #include "ash/shell.h" |
| 8 #include "ash/system/tray/system_tray.h" | 10 #include "ash/system/tray/system_tray.h" |
| 9 #include "ash/system/tray/system_tray_notifier.h" | 11 #include "ash/system/tray/system_tray_notifier.h" |
| 10 #include "base/bind.h" | 12 #include "base/bind.h" |
| 11 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
| 14 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 18 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 17 #include "chrome/browser/lifetime/application_lifetime.h" | 19 #include "chrome/browser/lifetime/application_lifetime.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_commands.h" | 22 #include "chrome/browser/ui/browser_commands.h" |
| 21 #include "chrome/browser/ui/host_desktop.h" | 23 #include "chrome/browser/ui/host_desktop.h" |
| 22 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/grit/generated_resources.h" | 25 #include "chrome/grit/generated_resources.h" |
| 24 #include "chrome/grit/theme_resources.h" | 26 #include "chrome/grit/theme_resources.h" |
| 25 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
| 26 #include "content/public/browser/notification_source.h" | 28 #include "content/public/browser/notification_source.h" |
| 27 #include "content/public/browser/user_metrics.h" | 29 #include "content/public/browser/user_metrics.h" |
| 28 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 30 | 32 |
| 31 using base::UserMetricsAction; | 33 using base::UserMetricsAction; |
| 32 using content::WebContents; | 34 using content::WebContents; |
| 33 | 35 |
| 34 namespace chromeos { | 36 namespace chromeos { |
| 35 | 37 |
| 38 namespace { |
| 39 |
| 40 // This is the list of languages that do not require user notification when |
| 41 // locale is switched automatically between regions within the same language. |
| 42 // |
| 43 // New language in kAcceptLanguageList should be added either here or to |
| 44 // to the exception list in unit test. |
| 45 const char* const kSkipShowNotificationLanguages[4] = {"en", "de", "fr", "it"}; |
| 46 |
| 47 } // anonymous namespace |
| 48 |
| 36 LocaleChangeGuard::LocaleChangeGuard(Profile* profile) | 49 LocaleChangeGuard::LocaleChangeGuard(Profile* profile) |
| 37 : profile_(profile), | 50 : profile_(profile), |
| 38 reverted_(false), | 51 reverted_(false), |
| 39 session_started_(false), | 52 session_started_(false), |
| 40 main_frame_loaded_(false) { | 53 main_frame_loaded_(false) { |
| 41 DCHECK(profile_); | 54 DCHECK(profile_); |
| 42 registrar_.Add(this, chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED, | 55 registrar_.Add(this, chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED, |
| 43 content::NotificationService::AllSources()); | 56 content::NotificationService::AllSources()); |
| 44 } | 57 } |
| 45 | 58 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 return; | 159 return; |
| 147 } | 160 } |
| 148 | 161 |
| 149 std::string from_locale = prefs->GetString(prefs::kApplicationLocaleBackup); | 162 std::string from_locale = prefs->GetString(prefs::kApplicationLocaleBackup); |
| 150 if (from_locale.empty() || from_locale == to_locale) | 163 if (from_locale.empty() || from_locale == to_locale) |
| 151 return; // No locale change was detected, just exit. | 164 return; // No locale change was detected, just exit. |
| 152 | 165 |
| 153 if (prefs->GetString(prefs::kApplicationLocaleAccepted) == to_locale) | 166 if (prefs->GetString(prefs::kApplicationLocaleAccepted) == to_locale) |
| 154 return; // Already accepted. | 167 return; // Already accepted. |
| 155 | 168 |
| 156 // Locale change detected, showing notification. | 169 // Locale change detected. |
| 170 if (!ShouldShowLocaleChangeNotification(from_locale, to_locale)) |
| 171 return; |
| 172 |
| 173 // Showing notification. |
| 157 if (from_locale_ != from_locale || to_locale_ != to_locale) { | 174 if (from_locale_ != from_locale || to_locale_ != to_locale) { |
| 158 // Falling back to showing message in current locale. | 175 // Falling back to showing message in current locale. |
| 159 LOG(ERROR) << | 176 LOG(ERROR) << |
| 160 "Showing locale change notification in current (not previous) language"; | 177 "Showing locale change notification in current (not previous) language"; |
| 161 PrepareChangingLocale(from_locale, to_locale); | 178 PrepareChangingLocale(from_locale, to_locale); |
| 162 } | 179 } |
| 163 | 180 |
| 164 ash::Shell::GetInstance()->system_tray_notifier()->NotifyLocaleChanged( | 181 ash::Shell::GetInstance()->system_tray_notifier()->NotifyLocaleChanged( |
| 165 this, cur_locale, from_locale_, to_locale_); | 182 this, cur_locale, from_locale_, to_locale_); |
| 166 } | 183 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 | 222 |
| 206 title_text_ = l10n_util::GetStringUTF16( | 223 title_text_ = l10n_util::GetStringUTF16( |
| 207 IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE); | 224 IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE); |
| 208 message_text_ = l10n_util::GetStringFUTF16( | 225 message_text_ = l10n_util::GetStringFUTF16( |
| 209 IDS_LOCALE_CHANGE_MESSAGE, from, to); | 226 IDS_LOCALE_CHANGE_MESSAGE, from, to); |
| 210 revert_link_text_ = l10n_util::GetStringFUTF16( | 227 revert_link_text_ = l10n_util::GetStringFUTF16( |
| 211 IDS_LOCALE_CHANGE_REVERT_MESSAGE, from); | 228 IDS_LOCALE_CHANGE_REVERT_MESSAGE, from); |
| 212 } | 229 } |
| 213 } | 230 } |
| 214 | 231 |
| 232 // static |
| 233 bool LocaleChangeGuard::ShouldShowLocaleChangeNotification( |
| 234 const std::string& from_locale, |
| 235 const std::string& to_locale) { |
| 236 const std::string from_lang = l10n_util::GetLanguage(from_locale); |
| 237 const std::string to_lang = l10n_util::GetLanguage(to_locale); |
| 238 |
| 239 if (from_locale == to_locale) |
| 240 return false; |
| 241 |
| 242 if (from_lang != to_lang) |
| 243 return true; |
| 244 |
| 245 const char* const* begin = kSkipShowNotificationLanguages; |
| 246 const char* const* end = kSkipShowNotificationLanguages + |
| 247 arraysize(kSkipShowNotificationLanguages); |
| 248 |
| 249 return std::find(begin, end, from_lang) == end; |
| 250 } |
| 251 |
| 252 // static |
| 253 const char* const* |
| 254 LocaleChangeGuard::GetSkipShowNotificationLanguagesForTesting() { |
| 255 return kSkipShowNotificationLanguages; |
| 256 } |
| 257 |
| 258 // static |
| 259 size_t LocaleChangeGuard::GetSkipShowNotificationLanguagesSizeForTesting() { |
| 260 return arraysize(kSkipShowNotificationLanguages); |
| 261 } |
| 262 |
| 215 } // namespace chromeos | 263 } // namespace chromeos |
| OLD | NEW |