Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / locale_change_guard.cc
blob32e58dc9544c5cb6e56d14a76258e99502ed95aa
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"
7 #include "ash/shell.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;
35 namespace chromeos {
37 class LocaleChangeGuard::Delegate : public NotificationDelegate {
38 public:
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 {
46 return NULL;
49 protected:
50 virtual ~Delegate() {}
52 private:
53 chromeos::LocaleChangeGuard* master_;
55 DISALLOW_COPY_AND_ASSIGN(Delegate);
58 LocaleChangeGuard::LocaleChangeGuard(Profile* profile)
59 : profile_(profile),
60 reverted_(false),
61 session_started_(false),
62 main_frame_loaded_(false) {
63 DCHECK(profile_);
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() ||
80 to_locale_.empty()) {
81 NOTREACHED();
82 return;
84 if (reverted_)
85 return;
86 reverted_ = true;
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) {
95 RevertLocaleChange();
98 void LocaleChangeGuard::Observe(int type,
99 const content::NotificationSource& source,
100 const content::NotificationDetails& details) {
101 if (profile_ == NULL) {
102 NOTREACHED();
103 return;
105 switch (type) {
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_)
111 Check();
112 break;
114 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: {
115 if (profile_ ==
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_)
122 Check();
124 break;
126 case chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED: {
127 if (DeviceSettingsService::Get()->HasPrivateOwnerKey()) {
128 PrefService* local_state = g_browser_process->local_state();
129 if (local_state) {
130 PrefService* prefs = profile_->GetPrefs();
131 if (prefs == NULL) {
132 NOTREACHED();
133 return;
135 std::string owner_locale =
136 prefs->GetString(prefs::kApplicationLocale);
137 if (!owner_locale.empty())
138 local_state->SetString(prefs::kOwnerLocale, owner_locale);
141 break;
143 default: {
144 NOTREACHED();
145 break;
150 void LocaleChangeGuard::Check() {
151 std::string cur_locale = g_browser_process->GetApplicationLocale();
152 if (cur_locale.empty()) {
153 NOTREACHED();
154 return;
157 PrefService* prefs = profile_->GetPrefs();
158 if (prefs == NULL) {
159 NOTREACHED();
160 return;
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.
168 return;
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.
181 LOG(ERROR) <<
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()) {
194 NOTREACHED();
195 return;
198 // Check whether locale has been reverted or changed.
199 // If not: mark current locale as accepted.
200 if (reverted_)
201 return;
202 PrefService* prefs = profile_->GetPrefs();
203 if (prefs == NULL) {
204 NOTREACHED();
205 return;
207 if (prefs->GetString(prefs::kApplicationLocale) != to_locale_)
208 return;
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) {
238 if (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