Chromium Code Reviews| 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> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/common/system/tray/system_tray_notifier.h" | |
| 10 #include "ash/common/wm_shell.h" | |
| 11 #include "base/bind.h" | 9 #include "base/bind.h" |
| 12 #include "base/macros.h" | 10 #include "base/macros.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/app/chrome_command_ids.h" | 12 #include "chrome/app/chrome_command_ids.h" |
| 15 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 15 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 18 #include "chrome/browser/lifetime/application_lifetime.h" | 16 #include "chrome/browser/lifetime/application_lifetime.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/ash/ash_util.h" | |
| 20 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_commands.h" | 20 #include "chrome/browser/ui/browser_commands.h" |
| 22 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/grit/generated_resources.h" | 22 #include "chrome/grit/generated_resources.h" |
| 24 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 25 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 26 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
| 27 #include "content/public/browser/user_metrics.h" | 26 #include "content/public/browser/user_metrics.h" |
| 28 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/common/service_manager_connection.h" | |
| 29 #include "services/shell/public/cpp/connector.h" | |
| 29 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 30 | 31 |
| 31 using base::UserMetricsAction; | 32 using base::UserMetricsAction; |
| 32 using content::WebContents; | 33 using content::WebContents; |
| 33 | 34 |
| 34 namespace chromeos { | 35 namespace chromeos { |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 38 // This is the list of languages that do not require user notification when | 39 // This is the list of languages that do not require user notification when |
| 39 // locale is switched automatically between regions within the same language. | 40 // locale is switched automatically between regions within the same language. |
| 40 // | 41 // |
| 41 // New language in kAcceptLanguageList should be added either here or to | 42 // New language in kAcceptLanguageList should be added either here or to |
| 42 // to the exception list in unit test. | 43 // to the exception list in unit test. |
| 43 const char* const kSkipShowNotificationLanguages[4] = {"en", "de", "fr", "it"}; | 44 const char* const kSkipShowNotificationLanguages[4] = {"en", "de", "fr", "it"}; |
| 44 | 45 |
| 45 } // anonymous namespace | 46 } // anonymous namespace |
| 46 | 47 |
| 47 LocaleChangeGuard::LocaleChangeGuard(Profile* profile) | 48 LocaleChangeGuard::LocaleChangeGuard(Profile* profile) |
| 48 : profile_(profile), | 49 : profile_(profile), |
| 49 reverted_(false), | 50 reverted_(false), |
| 50 session_started_(false), | 51 session_started_(false), |
| 51 main_frame_loaded_(false) { | 52 main_frame_loaded_(false) { |
| 52 DCHECK(profile_); | 53 DCHECK(profile_); |
| 53 registrar_.Add(this, chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED, | 54 registrar_.Add(this, chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED, |
| 54 content::NotificationService::AllSources()); | 55 content::NotificationService::AllSources()); |
| 56 ConnectToLocaleNotificationController(); | |
|
msw
2016/10/14 01:46:29
nit: call in Check() instead, or just inline the c
| |
| 55 } | 57 } |
| 56 | 58 |
| 57 LocaleChangeGuard::~LocaleChangeGuard() {} | 59 LocaleChangeGuard::~LocaleChangeGuard() {} |
| 58 | 60 |
| 59 void LocaleChangeGuard::OnLogin() { | 61 void LocaleChangeGuard::OnLogin() { |
| 60 registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, | 62 registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, |
| 61 content::NotificationService::AllSources()); | 63 content::NotificationService::AllSources()); |
| 62 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | 64 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 63 content::NotificationService::AllBrowserContextsAndSources()); | 65 content::NotificationService::AllBrowserContextsAndSources()); |
| 64 } | 66 } |
| 65 | 67 |
| 68 void LocaleChangeGuard::ConnectToLocaleNotificationController() { | |
| 69 auto connection = content::ServiceManagerConnection::GetForProcess(); | |
| 70 auto connector = connection ? connection->GetConnector() : nullptr; | |
| 71 // Unit tests may not have a connector. | |
| 72 if (!connector) | |
| 73 return; | |
| 74 | |
| 75 if (chrome::IsRunningInMash()) { | |
| 76 connector->ConnectToInterface("service:ash", ¬ification_controller_); | |
| 77 } else { | |
| 78 connector->ConnectToInterface("service:content_browser", | |
| 79 ¬ification_controller_); | |
| 80 } | |
| 81 } | |
| 82 | |
| 66 void LocaleChangeGuard::RevertLocaleChange() { | 83 void LocaleChangeGuard::RevertLocaleChange() { |
| 67 if (profile_ == NULL || | 84 if (profile_ == NULL || |
| 68 from_locale_.empty() || | 85 from_locale_.empty() || |
| 69 to_locale_.empty()) { | 86 to_locale_.empty()) { |
| 70 NOTREACHED(); | 87 NOTREACHED(); |
| 71 return; | 88 return; |
| 72 } | 89 } |
| 73 if (reverted_) | 90 if (reverted_) |
| 74 return; | 91 return; |
| 75 reverted_ = true; | 92 reverted_ = true; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 return; | 186 return; |
| 170 | 187 |
| 171 // Showing notification. | 188 // Showing notification. |
| 172 if (from_locale_ != from_locale || to_locale_ != to_locale) { | 189 if (from_locale_ != from_locale || to_locale_ != to_locale) { |
| 173 // Falling back to showing message in current locale. | 190 // Falling back to showing message in current locale. |
| 174 LOG(ERROR) << | 191 LOG(ERROR) << |
| 175 "Showing locale change notification in current (not previous) language"; | 192 "Showing locale change notification in current (not previous) language"; |
| 176 PrepareChangingLocale(from_locale, to_locale); | 193 PrepareChangingLocale(from_locale, to_locale); |
| 177 } | 194 } |
| 178 | 195 |
| 179 ash::WmShell::Get()->system_tray_notifier()->NotifyLocaleChanged( | 196 notification_controller_->NotifyLocaleChanged( |
| 180 this, cur_locale, from_locale_, to_locale_); | 197 cur_locale, from_locale_, to_locale_, |
| 198 base::Bind(&LocaleChangeGuard::OnResult, AsWeakPtr())); | |
| 199 } | |
| 200 | |
| 201 void LocaleChangeGuard::OnResult(ash::mojom::LocaleNotificationResult result) { | |
| 202 switch (result) { | |
| 203 case ash::mojom::LocaleNotificationResult::ACCEPT: | |
| 204 AcceptLocaleChange(); | |
| 205 break; | |
| 206 case ash::mojom::LocaleNotificationResult::REVERT: | |
| 207 RevertLocaleChange(); | |
| 208 break; | |
| 209 } | |
| 181 } | 210 } |
| 182 | 211 |
| 183 void LocaleChangeGuard::AcceptLocaleChange() { | 212 void LocaleChangeGuard::AcceptLocaleChange() { |
| 184 if (profile_ == NULL || | 213 if (profile_ == NULL || |
| 185 from_locale_.empty() || | 214 from_locale_.empty() || |
| 186 to_locale_.empty()) { | 215 to_locale_.empty()) { |
| 187 NOTREACHED(); | 216 NOTREACHED(); |
| 188 return; | 217 return; |
| 189 } | 218 } |
| 190 | 219 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 204 prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_); | 233 prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_); |
| 205 } | 234 } |
| 206 | 235 |
| 207 void LocaleChangeGuard::PrepareChangingLocale( | 236 void LocaleChangeGuard::PrepareChangingLocale( |
| 208 const std::string& from_locale, const std::string& to_locale) { | 237 const std::string& from_locale, const std::string& to_locale) { |
| 209 std::string cur_locale = g_browser_process->GetApplicationLocale(); | 238 std::string cur_locale = g_browser_process->GetApplicationLocale(); |
| 210 if (!from_locale.empty()) | 239 if (!from_locale.empty()) |
| 211 from_locale_ = from_locale; | 240 from_locale_ = from_locale; |
| 212 if (!to_locale.empty()) | 241 if (!to_locale.empty()) |
| 213 to_locale_ = to_locale; | 242 to_locale_ = to_locale; |
| 214 | |
| 215 if (!from_locale_.empty() && !to_locale_.empty()) { | |
| 216 base::string16 from = l10n_util::GetDisplayNameForLocale( | |
| 217 from_locale_, cur_locale, true); | |
| 218 base::string16 to = l10n_util::GetDisplayNameForLocale( | |
| 219 to_locale_, cur_locale, true); | |
| 220 | |
| 221 title_text_ = l10n_util::GetStringUTF16( | |
| 222 IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE); | |
|
msw
2016/10/14 01:46:29
Remove these three string resources now that they'
| |
| 223 message_text_ = l10n_util::GetStringFUTF16( | |
| 224 IDS_LOCALE_CHANGE_MESSAGE, from, to); | |
| 225 revert_link_text_ = l10n_util::GetStringFUTF16( | |
| 226 IDS_LOCALE_CHANGE_REVERT_MESSAGE, from); | |
| 227 } | |
| 228 } | 243 } |
| 229 | 244 |
| 230 // static | 245 // static |
| 231 bool LocaleChangeGuard::ShouldShowLocaleChangeNotification( | 246 bool LocaleChangeGuard::ShouldShowLocaleChangeNotification( |
| 232 const std::string& from_locale, | 247 const std::string& from_locale, |
| 233 const std::string& to_locale) { | 248 const std::string& to_locale) { |
| 234 const std::string from_lang = l10n_util::GetLanguage(from_locale); | 249 const std::string from_lang = l10n_util::GetLanguage(from_locale); |
| 235 const std::string to_lang = l10n_util::GetLanguage(to_locale); | 250 const std::string to_lang = l10n_util::GetLanguage(to_locale); |
| 236 | 251 |
| 237 if (from_locale == to_locale) | 252 if (from_locale == to_locale) |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 252 LocaleChangeGuard::GetSkipShowNotificationLanguagesForTesting() { | 267 LocaleChangeGuard::GetSkipShowNotificationLanguagesForTesting() { |
| 253 return kSkipShowNotificationLanguages; | 268 return kSkipShowNotificationLanguages; |
| 254 } | 269 } |
| 255 | 270 |
| 256 // static | 271 // static |
| 257 size_t LocaleChangeGuard::GetSkipShowNotificationLanguagesSizeForTesting() { | 272 size_t LocaleChangeGuard::GetSkipShowNotificationLanguagesSizeForTesting() { |
| 258 return arraysize(kSkipShowNotificationLanguages); | 273 return arraysize(kSkipShowNotificationLanguages); |
| 259 } | 274 } |
| 260 | 275 |
| 261 } // namespace chromeos | 276 } // namespace chromeos |
| OLD | NEW |