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" |
|
msw
2016/10/17 22:19:34
nit: remove
Elliot Glaysher
2016/10/17 22:37:17
Done.
| |
| 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/service_manager/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 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 56 | 57 |
| 57 LocaleChangeGuard::~LocaleChangeGuard() {} | 58 LocaleChangeGuard::~LocaleChangeGuard() {} |
| 58 | 59 |
| 59 void LocaleChangeGuard::OnLogin() { | 60 void LocaleChangeGuard::OnLogin() { |
| 60 registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, | 61 registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED, |
| 61 content::NotificationService::AllSources()); | 62 content::NotificationService::AllSources()); |
| 62 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | 63 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 63 content::NotificationService::AllBrowserContextsAndSources()); | 64 content::NotificationService::AllBrowserContextsAndSources()); |
| 64 } | 65 } |
| 65 | 66 |
| 67 void LocaleChangeGuard::ConnectToLocaleNotificationController() { | |
| 68 auto connection = content::ServiceManagerConnection::GetForProcess(); | |
|
msw
2016/10/17 22:19:34
nit: James asked me to avoid auto for these types
Elliot Glaysher
2016/10/17 22:37:17
Done.
| |
| 69 auto connector = connection ? connection->GetConnector() : nullptr; | |
| 70 // Unit tests may not have a connector. | |
| 71 if (!connector) | |
| 72 return; | |
| 73 | |
| 74 if (chrome::IsRunningInMash()) { | |
| 75 connector->ConnectToInterface("service:ash", ¬ification_controller_); | |
| 76 } else { | |
| 77 connector->ConnectToInterface("service:content_browser", | |
| 78 ¬ification_controller_); | |
| 79 } | |
| 80 } | |
| 81 | |
| 66 void LocaleChangeGuard::RevertLocaleChange() { | 82 void LocaleChangeGuard::RevertLocaleChange() { |
| 67 if (profile_ == NULL || | 83 if (profile_ == NULL || |
| 68 from_locale_.empty() || | 84 from_locale_.empty() || |
| 69 to_locale_.empty()) { | 85 to_locale_.empty()) { |
| 70 NOTREACHED(); | 86 NOTREACHED(); |
| 71 return; | 87 return; |
| 72 } | 88 } |
| 73 if (reverted_) | 89 if (reverted_) |
| 74 return; | 90 return; |
| 75 reverted_ = true; | 91 reverted_ = true; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 return; | 185 return; |
| 170 | 186 |
| 171 // Showing notification. | 187 // Showing notification. |
| 172 if (from_locale_ != from_locale || to_locale_ != to_locale) { | 188 if (from_locale_ != from_locale || to_locale_ != to_locale) { |
| 173 // Falling back to showing message in current locale. | 189 // Falling back to showing message in current locale. |
| 174 LOG(ERROR) << | 190 LOG(ERROR) << |
| 175 "Showing locale change notification in current (not previous) language"; | 191 "Showing locale change notification in current (not previous) language"; |
| 176 PrepareChangingLocale(from_locale, to_locale); | 192 PrepareChangingLocale(from_locale, to_locale); |
| 177 } | 193 } |
| 178 | 194 |
| 179 ash::WmShell::Get()->system_tray_notifier()->NotifyLocaleChanged( | 195 if (!notification_controller_) |
| 180 this, cur_locale, from_locale_, to_locale_); | 196 ConnectToLocaleNotificationController(); |
| 197 | |
| 198 notification_controller_->OnLocaleChanged( | |
| 199 cur_locale, from_locale_, to_locale_, | |
| 200 base::Bind(&LocaleChangeGuard::OnResult, AsWeakPtr())); | |
| 201 } | |
| 202 | |
| 203 void LocaleChangeGuard::OnResult(ash::mojom::LocaleNotificationResult result) { | |
| 204 switch (result) { | |
| 205 case ash::mojom::LocaleNotificationResult::ACCEPT: | |
| 206 AcceptLocaleChange(); | |
| 207 break; | |
| 208 case ash::mojom::LocaleNotificationResult::REVERT: | |
| 209 RevertLocaleChange(); | |
| 210 break; | |
| 211 } | |
| 181 } | 212 } |
| 182 | 213 |
| 183 void LocaleChangeGuard::AcceptLocaleChange() { | 214 void LocaleChangeGuard::AcceptLocaleChange() { |
| 184 if (profile_ == NULL || | 215 if (profile_ == NULL || |
| 185 from_locale_.empty() || | 216 from_locale_.empty() || |
| 186 to_locale_.empty()) { | 217 to_locale_.empty()) { |
| 187 NOTREACHED(); | 218 NOTREACHED(); |
| 188 return; | 219 return; |
| 189 } | 220 } |
| 190 | 221 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 204 prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_); | 235 prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_); |
| 205 } | 236 } |
| 206 | 237 |
| 207 void LocaleChangeGuard::PrepareChangingLocale( | 238 void LocaleChangeGuard::PrepareChangingLocale( |
| 208 const std::string& from_locale, const std::string& to_locale) { | 239 const std::string& from_locale, const std::string& to_locale) { |
| 209 std::string cur_locale = g_browser_process->GetApplicationLocale(); | 240 std::string cur_locale = g_browser_process->GetApplicationLocale(); |
| 210 if (!from_locale.empty()) | 241 if (!from_locale.empty()) |
| 211 from_locale_ = from_locale; | 242 from_locale_ = from_locale; |
| 212 if (!to_locale.empty()) | 243 if (!to_locale.empty()) |
| 213 to_locale_ = to_locale; | 244 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); | |
| 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 } | 245 } |
| 229 | 246 |
| 230 // static | 247 // static |
| 231 bool LocaleChangeGuard::ShouldShowLocaleChangeNotification( | 248 bool LocaleChangeGuard::ShouldShowLocaleChangeNotification( |
| 232 const std::string& from_locale, | 249 const std::string& from_locale, |
| 233 const std::string& to_locale) { | 250 const std::string& to_locale) { |
| 234 const std::string from_lang = l10n_util::GetLanguage(from_locale); | 251 const std::string from_lang = l10n_util::GetLanguage(from_locale); |
| 235 const std::string to_lang = l10n_util::GetLanguage(to_locale); | 252 const std::string to_lang = l10n_util::GetLanguage(to_locale); |
| 236 | 253 |
| 237 if (from_locale == to_locale) | 254 if (from_locale == to_locale) |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 252 LocaleChangeGuard::GetSkipShowNotificationLanguagesForTesting() { | 269 LocaleChangeGuard::GetSkipShowNotificationLanguagesForTesting() { |
| 253 return kSkipShowNotificationLanguages; | 270 return kSkipShowNotificationLanguages; |
| 254 } | 271 } |
| 255 | 272 |
| 256 // static | 273 // static |
| 257 size_t LocaleChangeGuard::GetSkipShowNotificationLanguagesSizeForTesting() { | 274 size_t LocaleChangeGuard::GetSkipShowNotificationLanguagesSizeForTesting() { |
| 258 return arraysize(kSkipShowNotificationLanguages); | 275 return arraysize(kSkipShowNotificationLanguages); |
| 259 } | 276 } |
| 260 | 277 |
| 261 } // namespace chromeos | 278 } // namespace chromeos |
| OLD | NEW |