chromeos: Lock proxy settings UI for policy managed network.
[chromium-blink-merge.git] / chrome / browser / chromeos / locale_change_guard.cc
blob8725089d4a32ce7baa0e01f1f2880c807e289dec
1 // Copyright (c) 2011 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 "base/bind.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/login/user_manager.h"
12 #include "chrome/browser/notifications/notification_delegate.h"
13 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/common/chrome_notification_types.h"
17 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/user_metrics.h"
21 #include "content/public/browser/web_contents.h"
22 #include "grit/generated_resources.h"
23 #include "grit/theme_resources.h"
24 #include "ui/base/l10n/l10n_util.h"
26 using content::UserMetricsAction;
27 using content::WebContents;
29 namespace chromeos {
31 class LocaleChangeGuard::Delegate : public NotificationDelegate {
32 public:
33 explicit Delegate(chromeos::LocaleChangeGuard* master) : master_(master) {}
34 void Close(bool by_user);
35 void Display() {}
36 void Error() {}
37 void Click() {}
38 std::string id() const;
40 private:
41 chromeos::LocaleChangeGuard* master_;
43 DISALLOW_COPY_AND_ASSIGN(Delegate);
46 LocaleChangeGuard::LocaleChangeGuard(Profile* profile)
47 : profile_(profile),
48 note_(NULL),
49 reverted_(false) {
50 DCHECK(profile_);
51 registrar_.Add(this, chrome::NOTIFICATION_OWNERSHIP_CHECKED,
52 content::NotificationService::AllSources());
55 LocaleChangeGuard::~LocaleChangeGuard() {}
57 void LocaleChangeGuard::OnLogin() {
58 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
59 content::NotificationService::AllBrowserContextsAndSources());
62 void LocaleChangeGuard::RevertLocaleChange(const ListValue* list) {
63 if (note_ == NULL ||
64 profile_ == NULL ||
65 from_locale_.empty() ||
66 to_locale_.empty()) {
67 NOTREACHED();
68 return;
70 if (reverted_)
71 return;
72 reverted_ = true;
73 content::RecordAction(UserMetricsAction("LanguageChange_Revert"));
74 profile_->ChangeAppLocale(
75 from_locale_, Profile::APP_LOCALE_CHANGED_VIA_REVERT);
77 Browser* browser = Browser::GetTabbedBrowser(profile_, false);
78 if (browser)
79 browser->ExecuteCommand(IDC_EXIT);
82 void LocaleChangeGuard::Observe(int type,
83 const content::NotificationSource& source,
84 const content::NotificationDetails& details) {
85 if (profile_ == NULL) {
86 NOTREACHED();
87 return;
89 switch (type) {
90 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: {
91 if (profile_ ==
92 content::Source<WebContents>(source)->GetBrowserContext()) {
93 // We need to perform locale change check only once, so unsubscribe.
94 registrar_.Remove(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
95 content::NotificationService::AllSources());
96 Check();
98 break;
100 case chrome::NOTIFICATION_OWNERSHIP_CHECKED: {
101 if (UserManager::Get()->current_user_is_owner()) {
102 PrefService* local_state = g_browser_process->local_state();
103 if (local_state) {
104 PrefService* prefs = profile_->GetPrefs();
105 if (prefs == NULL) {
106 NOTREACHED();
107 return;
109 std::string owner_locale =
110 prefs->GetString(prefs::kApplicationLocale);
111 if (!owner_locale.empty())
112 local_state->SetString(prefs::kOwnerLocale, owner_locale);
115 break;
117 default: {
118 NOTREACHED();
119 break;
124 void LocaleChangeGuard::Check() {
125 if (note_ != NULL) {
126 // Somehow we are invoked more than once. Once is enough.
127 return;
130 std::string cur_locale = g_browser_process->GetApplicationLocale();
131 if (cur_locale.empty()) {
132 NOTREACHED();
133 return;
136 PrefService* prefs = profile_->GetPrefs();
137 if (prefs == NULL) {
138 NOTREACHED();
139 return;
142 std::string to_locale = prefs->GetString(prefs::kApplicationLocale);
143 if (to_locale != cur_locale) {
144 // This conditional branch can occur in cases like:
145 // (1) kApplicationLocale preference was modified by synchronization;
146 // (2) kApplicationLocale is managed by policy.
147 return;
150 std::string from_locale = prefs->GetString(prefs::kApplicationLocaleBackup);
151 if (from_locale.empty() || from_locale == to_locale)
152 return; // No locale change was detected, just exit.
154 if (prefs->GetString(prefs::kApplicationLocaleAccepted) == to_locale)
155 return; // Already accepted.
157 // Locale change detected, showing notification.
158 if (from_locale_ != from_locale || to_locale_ != to_locale) {
159 // Falling back to showing message in current locale.
160 LOG(ERROR) <<
161 "Showing locale change notification in current (not previous) language";
162 PrepareChangingLocale(from_locale, to_locale);
164 note_.reset(new chromeos::SystemNotification(
165 profile_,
166 new Delegate(this),
167 IDR_NOTIFICATION_LOCALE_CHANGE,
168 title_text_));
169 note_->Show(
170 message_text_, revert_link_text_,
171 base::Bind(&LocaleChangeGuard::RevertLocaleChange,
172 AsWeakPtr()),
173 true, // urgent
174 false); // non-sticky
177 void LocaleChangeGuard::AcceptLocaleChange() {
178 if (note_ == NULL ||
179 profile_ == NULL ||
180 from_locale_.empty() ||
181 to_locale_.empty()) {
182 NOTREACHED();
183 return;
186 // Check whether locale has been reverted or changed.
187 // If not: mark current locale as accepted.
188 if (reverted_)
189 return;
190 PrefService* prefs = profile_->GetPrefs();
191 if (prefs == NULL) {
192 NOTREACHED();
193 return;
195 if (prefs->GetString(prefs::kApplicationLocale) != to_locale_)
196 return;
197 content::RecordAction(UserMetricsAction("LanguageChange_Accept"));
198 prefs->SetString(prefs::kApplicationLocaleBackup, to_locale_);
199 prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_);
202 void LocaleChangeGuard::PrepareChangingLocale(
203 const std::string& from_locale, const std::string& to_locale) {
204 std::string cur_locale = g_browser_process->GetApplicationLocale();
205 if (!from_locale.empty())
206 from_locale_ = from_locale;
207 if (!to_locale.empty())
208 to_locale_ = to_locale;
210 if (!from_locale_.empty() && !to_locale_.empty()) {
211 string16 from = l10n_util::GetDisplayNameForLocale(
212 from_locale_, cur_locale, true);
213 string16 to = l10n_util::GetDisplayNameForLocale(
214 to_locale_, cur_locale, true);
216 title_text_ = l10n_util::GetStringUTF16(
217 IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE);
218 message_text_ = l10n_util::GetStringFUTF16(
219 IDS_LOCALE_CHANGE_MESSAGE, from, to);
220 revert_link_text_ = l10n_util::GetStringFUTF16(
221 IDS_LOCALE_CHANGE_REVERT_MESSAGE, from);
225 void LocaleChangeGuard::Delegate::Close(bool by_user) {
226 if (by_user)
227 master_->AcceptLocaleChange();
230 std::string LocaleChangeGuard::Delegate::id() const {
231 // Arbitrary unique Id.
232 return "8c386938-1e3f-11e0-ac7b-18a90520e2e5";
235 } // namespace chromeos