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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/locale_change_guard.h"
8 #include "ash/system/tray/system_tray.h"
9 #include "ash/system/tray/system_tray_notifier.h"
10 #include "base/bind.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/chromeos/settings/device_settings_service.h"
17 #include "chrome/browser/lifetime/application_lifetime.h"
18 #include "chrome/browser/notifications/notification_delegate.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_commands.h"
22 #include "chrome/browser/ui/host_desktop.h"
23 #include "chrome/common/pref_names.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/notification_source.h"
26 #include "content/public/browser/user_metrics.h"
27 #include "content/public/browser/web_contents.h"
28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
32 using base::UserMetricsAction
;
33 using content::WebContents
;
37 class LocaleChangeGuard::Delegate
: public NotificationDelegate
{
39 explicit Delegate(chromeos::LocaleChangeGuard
* master
) : master_(master
) {}
40 virtual void Close(bool by_user
) OVERRIDE
;
41 virtual void Display() OVERRIDE
{}
42 virtual void Error() OVERRIDE
{}
43 virtual void Click() OVERRIDE
{}
44 virtual std::string
id() const OVERRIDE
;
45 virtual content::RenderViewHost
* GetRenderViewHost() const OVERRIDE
{
50 virtual ~Delegate() {}
53 chromeos::LocaleChangeGuard
* master_
;
55 DISALLOW_COPY_AND_ASSIGN(Delegate
);
58 LocaleChangeGuard::LocaleChangeGuard(Profile
* profile
)
61 session_started_(false),
62 main_frame_loaded_(false) {
64 registrar_
.Add(this, chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED
,
65 content::NotificationService::AllSources());
68 LocaleChangeGuard::~LocaleChangeGuard() {}
70 void LocaleChangeGuard::OnLogin() {
71 registrar_
.Add(this, chrome::NOTIFICATION_SESSION_STARTED
,
72 content::NotificationService::AllSources());
73 registrar_
.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
74 content::NotificationService::AllBrowserContextsAndSources());
77 void LocaleChangeGuard::RevertLocaleChange() {
78 if (profile_
== NULL
||
79 from_locale_
.empty() ||
87 content::RecordAction(UserMetricsAction("LanguageChange_Revert"));
88 profile_
->ChangeAppLocale(
89 from_locale_
, Profile::APP_LOCALE_CHANGED_VIA_REVERT
);
90 chrome::AttemptUserExit();
93 void LocaleChangeGuard::RevertLocaleChangeCallback(
94 const base::ListValue
* list
) {
98 void LocaleChangeGuard::Observe(int type
,
99 const content::NotificationSource
& source
,
100 const content::NotificationDetails
& details
) {
101 if (profile_
== NULL
) {
106 case chrome::NOTIFICATION_SESSION_STARTED
: {
107 session_started_
= true;
108 registrar_
.Remove(this, chrome::NOTIFICATION_SESSION_STARTED
,
109 content::NotificationService::AllSources());
110 if (main_frame_loaded_
)
114 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
: {
116 content::Source
<WebContents
>(source
)->GetBrowserContext()) {
117 main_frame_loaded_
= true;
118 // We need to perform locale change check only once, so unsubscribe.
119 registrar_
.Remove(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
120 content::NotificationService::AllSources());
121 if (session_started_
)
126 case chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED
: {
127 if (DeviceSettingsService::Get()->HasPrivateOwnerKey()) {
128 PrefService
* local_state
= g_browser_process
->local_state();
130 PrefService
* prefs
= profile_
->GetPrefs();
135 std::string owner_locale
=
136 prefs
->GetString(prefs::kApplicationLocale
);
137 if (!owner_locale
.empty())
138 local_state
->SetString(prefs::kOwnerLocale
, owner_locale
);
150 void LocaleChangeGuard::Check() {
151 std::string cur_locale
= g_browser_process
->GetApplicationLocale();
152 if (cur_locale
.empty()) {
157 PrefService
* prefs
= profile_
->GetPrefs();
163 std::string to_locale
= prefs
->GetString(prefs::kApplicationLocale
);
164 if (to_locale
!= cur_locale
) {
165 // This conditional branch can occur in cases like:
166 // (1) kApplicationLocale preference was modified by synchronization;
167 // (2) kApplicationLocale is managed by policy.
171 std::string from_locale
= prefs
->GetString(prefs::kApplicationLocaleBackup
);
172 if (from_locale
.empty() || from_locale
== to_locale
)
173 return; // No locale change was detected, just exit.
175 if (prefs
->GetString(prefs::kApplicationLocaleAccepted
) == to_locale
)
176 return; // Already accepted.
178 // Locale change detected, showing notification.
179 if (from_locale_
!= from_locale
|| to_locale_
!= to_locale
) {
180 // Falling back to showing message in current locale.
182 "Showing locale change notification in current (not previous) language";
183 PrepareChangingLocale(from_locale
, to_locale
);
186 ash::Shell::GetInstance()->system_tray_notifier()->NotifyLocaleChanged(
187 this, cur_locale
, from_locale_
, to_locale_
);
190 void LocaleChangeGuard::AcceptLocaleChange() {
191 if (profile_
== NULL
||
192 from_locale_
.empty() ||
193 to_locale_
.empty()) {
198 // Check whether locale has been reverted or changed.
199 // If not: mark current locale as accepted.
202 PrefService
* prefs
= profile_
->GetPrefs();
207 if (prefs
->GetString(prefs::kApplicationLocale
) != to_locale_
)
209 content::RecordAction(UserMetricsAction("LanguageChange_Accept"));
210 prefs
->SetString(prefs::kApplicationLocaleBackup
, to_locale_
);
211 prefs
->SetString(prefs::kApplicationLocaleAccepted
, to_locale_
);
214 void LocaleChangeGuard::PrepareChangingLocale(
215 const std::string
& from_locale
, const std::string
& to_locale
) {
216 std::string cur_locale
= g_browser_process
->GetApplicationLocale();
217 if (!from_locale
.empty())
218 from_locale_
= from_locale
;
219 if (!to_locale
.empty())
220 to_locale_
= to_locale
;
222 if (!from_locale_
.empty() && !to_locale_
.empty()) {
223 base::string16 from
= l10n_util::GetDisplayNameForLocale(
224 from_locale_
, cur_locale
, true);
225 base::string16 to
= l10n_util::GetDisplayNameForLocale(
226 to_locale_
, cur_locale
, true);
228 title_text_
= l10n_util::GetStringUTF16(
229 IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE
);
230 message_text_
= l10n_util::GetStringFUTF16(
231 IDS_LOCALE_CHANGE_MESSAGE
, from
, to
);
232 revert_link_text_
= l10n_util::GetStringFUTF16(
233 IDS_LOCALE_CHANGE_REVERT_MESSAGE
, from
);
237 void LocaleChangeGuard::Delegate::Close(bool by_user
) {
239 master_
->AcceptLocaleChange();
242 std::string
LocaleChangeGuard::Delegate::id() const {
243 // Arbitrary unique Id.
244 return "8c386938-1e3f-11e0-ac7b-18a90520e2e5";
247 } // namespace chromeos