OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/chromeos/locale_change_guard.h" | |
6 | |
7 #include "app/l10n_util.h" | |
8 #include "base/lazy_instance.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/app/chrome_command_ids.h" | |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/browser/chromeos/notifications/system_notification.h" | |
13 #include "chrome/browser/metrics/user_metrics.h" | |
14 #include "chrome/browser/prefs/pref_service.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/tab_contents/tab_contents.h" | |
17 #include "chrome/browser/ui/browser.h" | |
18 #include "chrome/common/pref_names.h" | |
19 #include "grit/app_resources.h" | |
20 #include "grit/generated_resources.h" | |
21 | |
22 class ListValue; | |
23 | |
24 namespace { | |
25 | |
26 class LocaleChangeGuardImpl { | |
27 LocaleChangeGuardImpl() | |
whywhat
2011/01/12 16:33:16
Could you put explicit private: modifier here to a
Denis Lagno
2011/01/12 20:27:34
not applicable anymore
| |
28 : profile_id_(Profile::InvalidProfileId), | |
29 tab_contents_(NULL), | |
30 note_(NULL), | |
31 reverted_(false) {} | |
32 | |
33 void RevertLocaleChange(const ListValue* list) { | |
34 reverted_ = true; | |
35 UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Revert")); | |
36 tab_contents_->profile()->ChangeApplicationLocale(from_locale_, true); | |
37 PrefService* prefs = tab_contents_->profile()->GetPrefs(); | |
38 if (prefs) { | |
39 prefs->SetString(prefs::kApplicationLocaleBackup, from_locale_); | |
40 prefs->ClearPref(prefs::kApplicationLocaleAccepted); | |
41 prefs->ScheduleSavePersistentPrefs(); | |
42 } | |
43 Browser* browser = Browser::GetBrowserForController( | |
44 &tab_contents_->controller(), NULL); | |
45 if (browser) | |
46 browser->ExecuteCommand(IDC_EXIT); | |
whywhat
2011/01/12 16:33:16
Do we say in the message we show to user that we'l
Denis Lagno
2011/01/12 20:27:34
It happens if user presses on the link.
Link is cu
| |
47 } | |
48 | |
49 void Check(TabContents* tab_contents) { | |
50 // We want notification to be shown no more than once per session. | |
whywhat
2011/01/12 16:33:16
nit: no more than -> only?
Denis Lagno
2011/01/12 20:27:34
Not sure.. "Only once" sounds to me as exactly on
| |
51 if (note_ != NULL) | |
52 return; | |
53 // We check profile Id because on ChromeOS this guard | |
54 // first time may be invoked for a dummy profile. | |
whywhat
2011/01/12 16:33:16
nit: move "first time" to the end of the sentence?
Denis Lagno
2011/01/12 20:27:34
Done.
| |
55 ProfileId cur_profile_id = tab_contents->profile()->GetRuntimeId(); | |
56 if (cur_profile_id == profile_id_) | |
57 return; | |
58 profile_id_ = cur_profile_id; | |
59 std::string cur_locale = g_browser_process->GetApplicationLocale(); | |
60 if (cur_locale.empty()) | |
61 return; | |
62 PrefService* prefs = tab_contents->profile()->GetPrefs(); | |
63 if (prefs == NULL) | |
64 return; | |
65 to_locale_ = prefs->GetString(prefs::kApplicationLocaleOverride); | |
66 if (!to_locale_.empty()) { | |
67 DCHECK(to_locale_ == cur_locale); | |
68 return; | |
whywhat
2011/01/12 16:33:16
We return in multiple places, leaving the object p
Denis Lagno
2011/01/12 20:27:34
if it exited early in this function then it means
| |
69 } | |
70 to_locale_ = prefs->GetString(prefs::kApplicationLocale); | |
71 if (to_locale_ != cur_locale) | |
72 return; | |
73 from_locale_ = prefs->GetString(prefs::kApplicationLocaleBackup); | |
74 if (from_locale_.empty() || from_locale_ == to_locale_) | |
75 return; | |
76 note_.reset(new chromeos::SystemNotification( | |
77 tab_contents->profile(), | |
78 "8c386938-1e3f-11e0-ac7b-18a90520e2e5" /* arbitrary unique id */, | |
whywhat
2011/01/12 16:33:16
Could you make it a named constant?
Denis Lagno
2011/01/12 20:27:34
I moved it into dedicated named method.
| |
79 IDR_DEFAULT_FAVICON, | |
80 l10n_util::GetStringUTF16( | |
81 IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE))); | |
82 tab_contents_ = tab_contents; | |
83 note_->Show( | |
84 l10n_util::GetStringFUTF16( | |
85 IDS_LOCALE_CHANGE_MESSAGE, | |
86 l10n_util::GetDisplayNameForLocale(from_locale_, to_locale_, true), | |
87 l10n_util::GetDisplayNameForLocale(to_locale_, to_locale_, true)), | |
88 l10n_util::GetStringUTF16(IDS_LOCALE_CHANGE_REVERT_MESSAGE), | |
89 NewCallback(this, &LocaleChangeGuardImpl::RevertLocaleChange), | |
90 true /* urgent */, false /* non-sticky */); | |
whywhat
2011/01/12 16:33:16
Could you write it like this:
true, // urgent
f
Denis Lagno
2011/01/12 20:27:34
Done.
| |
91 } | |
92 | |
93 ~LocaleChangeGuardImpl() { | |
whywhat
2011/01/12 16:33:16
There was recent discussion about dtor and ctor be
whywhat
2011/01/12 16:33:16
When the dtor will be executed? As I understand, o
Denis Lagno
2011/01/12 20:27:34
dtor removed. not applicable anymore.
Denis Lagno
2011/01/12 20:27:34
You're rignt. That is bad. Reworked that part.
| |
94 // Check whether locale has been reverted or changed. | |
95 // If not: mark current locale as accepted. | |
96 if (tab_contents_ == NULL) | |
97 return; | |
98 if (reverted_) | |
99 return; | |
100 PrefService* prefs = tab_contents_->profile()->GetPrefs(); | |
101 if (prefs == NULL) | |
102 return; | |
103 std::string override_locale = | |
104 prefs->GetString(prefs::kApplicationLocaleOverride); | |
105 if (!override_locale.empty()) | |
106 return; | |
107 if (prefs->GetString(prefs::kApplicationLocale) != to_locale_) | |
108 return; | |
109 prefs->SetString(prefs::kApplicationLocaleBackup, to_locale_); | |
110 prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_); | |
111 prefs->ScheduleSavePersistentPrefs(); | |
112 } | |
113 | |
114 std::string from_locale_, to_locale_; | |
whywhat
2011/01/12 16:33:16
Declaration of two variables on a single line is n
Denis Lagno
2011/01/12 20:27:34
Done.
| |
115 ProfileId profile_id_; | |
116 TabContents* tab_contents_; | |
117 scoped_ptr<chromeos::SystemNotification> note_; | |
118 bool reverted_; | |
119 | |
120 friend struct base::DefaultLazyInstanceTraits<LocaleChangeGuardImpl>; | |
121 friend struct chromeos::LocaleChangeGuard; | |
122 }; | |
123 | |
124 base::LazyInstance<LocaleChangeGuardImpl> g_locale_change_guard( | |
125 base::LINKER_INITIALIZED); | |
126 | |
127 } // namespace | |
128 | |
129 namespace chromeos { | |
130 | |
131 // static | |
132 void LocaleChangeGuard::Check(TabContents* tab_contents) { | |
whywhat
2011/01/12 16:33:16
I think using impl class here hurts readability: t
Denis Lagno
2011/01/12 20:27:34
ok, I moved it into single class.
But I cannot say
whywhat
2011/01/13 10:09:43
Still I don't believe this will create a bottlenec
| |
133 g_locale_change_guard.Get().Check(tab_contents); | |
134 } | |
135 | |
136 } // namespace chromeos | |
OLD | NEW |