1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/locale_change_guard.h"
#include "app/l10n_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/notifications/system_notification.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/notifications/notification_delegate.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "grit/app_resources.h"
#include "grit/generated_resources.h"
namespace {
base::LazyInstance<chromeos::LocaleChangeGuard> g_locale_change_guard(
base::LINKER_INITIALIZED);
} // namespace
namespace chromeos {
class LocaleChangeGuard::Delegate : public NotificationDelegate {
public:
explicit Delegate(chromeos::LocaleChangeGuard* master) : master_(master) {}
void Close(bool by_user);
void Display() {}
void Error() {}
void Click() {}
std::string id() const;
private:
chromeos::LocaleChangeGuard* master_;
DISALLOW_COPY_AND_ASSIGN(Delegate);
};
LocaleChangeGuard::LocaleChangeGuard()
: profile_id_(Profile::InvalidProfileId),
tab_contents_(NULL),
note_(NULL),
reverted_(false) {
}
void LocaleChangeGuard::RevertLocaleChange(const ListValue* list) {
if (note_ == NULL ||
tab_contents_ == NULL ||
from_locale_.empty() ||
to_locale_.empty()) {
NOTREACHED();
return;
}
if (reverted_)
return;
PrefService* prefs = tab_contents_->profile()->GetPrefs();
if (prefs == NULL)
return;
reverted_ = true;
UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Revert"));
tab_contents_->profile()->ChangeApplicationLocale(from_locale_, true);
prefs->SetString(prefs::kApplicationLocaleBackup, from_locale_);
prefs->ClearPref(prefs::kApplicationLocaleAccepted);
prefs->ScheduleSavePersistentPrefs();
Browser* browser = Browser::GetBrowserForController(
&tab_contents_->controller(), NULL);
if (browser)
browser->ExecuteCommand(IDC_EXIT);
}
void LocaleChangeGuard::CheckLocaleChange(TabContents* tab_contents) {
// We want notification to be shown no more than once per session.
if (note_ != NULL)
return;
DCHECK(note_ == NULL && tab_contents_ == NULL);
DCHECK(from_locale_.empty() && to_locale_.empty());
// We check profile Id because:
// (1) we want to exit fast in common case when nothing should be done.
// (2) on ChromeOS this guard may be invoked for a dummy profile first time.
ProfileId cur_profile_id = tab_contents->profile()->GetRuntimeId();
if (cur_profile_id == profile_id_) {
// We have already checked this profile, exiting fast.
return;
}
profile_id_ = cur_profile_id;
std::string cur_locale = g_browser_process->GetApplicationLocale();
if (cur_locale.empty()) {
NOTREACHED();
return;
}
PrefService* prefs = tab_contents->profile()->GetPrefs();
if (prefs == NULL)
return;
std::string to_locale = prefs->GetString(prefs::kApplicationLocaleOverride);
if (!to_locale.empty()) {
DCHECK(to_locale == cur_locale);
return;
}
to_locale = prefs->GetString(prefs::kApplicationLocale);
if (to_locale != cur_locale) {
// This conditional branch can occur in case kApplicationLocale
// preference was modified by synchronization.
return;
}
std::string from_locale = prefs->GetString(prefs::kApplicationLocaleBackup);
if (from_locale.empty() || from_locale == to_locale) {
// No locale change was detected, just exit.
return;
}
// Locale change detected, showing notification.
from_locale_ = from_locale;
to_locale_ = to_locale;
tab_contents_ = tab_contents;
note_.reset(new chromeos::SystemNotification(
tab_contents->profile(),
new Delegate(this),
IDR_DEFAULT_FAVICON,
l10n_util::GetStringUTF16(
IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE)));
note_->Show(
l10n_util::GetStringFUTF16(
IDS_LOCALE_CHANGE_MESSAGE,
l10n_util::GetDisplayNameForLocale(from_locale_, to_locale_, true),
l10n_util::GetDisplayNameForLocale(to_locale_, to_locale_, true)),
l10n_util::GetStringUTF16(IDS_LOCALE_CHANGE_REVERT_MESSAGE),
NewCallback(this, &LocaleChangeGuard::RevertLocaleChange),
true, // urgent
false); // non-sticky
}
void LocaleChangeGuard::AcceptLocaleChange() {
if (note_ == NULL ||
tab_contents_ == NULL ||
from_locale_.empty() ||
to_locale_.empty()) {
NOTREACHED();
return;
}
// Check whether locale has been reverted or changed.
// If not: mark current locale as accepted.
if (reverted_)
return;
PrefService* prefs = tab_contents_->profile()->GetPrefs();
if (prefs == NULL)
return;
std::string override_locale =
prefs->GetString(prefs::kApplicationLocaleOverride);
if (!override_locale.empty())
return;
if (prefs->GetString(prefs::kApplicationLocale) != to_locale_)
return;
UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Accept"));
prefs->SetString(prefs::kApplicationLocaleBackup, to_locale_);
prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_);
prefs->ScheduleSavePersistentPrefs();
}
// static
void LocaleChangeGuard::Check(TabContents* tab_contents) {
g_locale_change_guard.Get().CheckLocaleChange(tab_contents);
}
void LocaleChangeGuard::Delegate::Close(bool by_user) {
if (by_user)
master_->AcceptLocaleChange();
}
std::string LocaleChangeGuard::Delegate::id() const {
// Arbitrary unique Id.
return "8c386938-1e3f-11e0-ac7b-18a90520e2e5";
}
} // namespace chromeos
|