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/tab_contents/locale_change_guard.h" | |
6 | |
7 #include "app/l10n_util.h" | |
8 #include "base/lazy_instance.h" | |
9 #include "chrome/app/chrome_command_ids.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/metrics/user_metrics.h" | |
12 #include "chrome/browser/prefs/pref_service.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/tab_contents/infobar_delegate.h" | |
15 #include "chrome/browser/tab_contents/tab_contents.h" | |
16 #include "chrome/browser/ui/browser.h" | |
17 #include "chrome/common/pref_names.h" | |
18 #include "grit/generated_resources.h" | |
19 | |
20 #if defined(OS_CHROMEOS) | |
21 namespace { | |
22 | |
23 class LocaleChangeInfoBar : public ConfirmInfoBarDelegate { | |
24 public: | |
25 LocaleChangeInfoBar(TabContents* tab_contents, | |
26 const std::string& from_locale, | |
27 const std::string& to_locale) | |
28 : ConfirmInfoBarDelegate(tab_contents), | |
29 tab_contents_(tab_contents), | |
30 from_locale_(from_locale), | |
31 to_locale_(to_locale) {} | |
32 | |
33 virtual int GetButtons() const { | |
34 return BUTTON_OK | BUTTON_CANCEL; | |
35 } | |
36 | |
37 virtual bool NeedElevation(InfoBarButton button) const { | |
38 return button == BUTTON_CANCEL; | |
39 } | |
40 | |
41 virtual string16 GetButtonLabel(InfoBarButton button) const { | |
42 if (button == BUTTON_OK) | |
whywhat
2011/01/11 10:24:14
So OK is revert and Cancel is Awesome? Not vice ve
Denis Lagno
2011/01/12 15:33:28
it was deliberate.
Not applicable anymore.
| |
43 return l10n_util::GetStringUTF16(IDS_LOCALE_CHANGE_REVERT_MESSAGE); | |
44 if (button == BUTTON_CANCEL) | |
45 return l10n_util::GetStringUTF16(IDS_AWESOME); | |
46 return ConfirmInfoBarDelegate::GetButtonLabel(button); | |
47 } | |
48 | |
49 virtual string16 GetMessageText() const { | |
50 return l10n_util::GetStringFUTF16( | |
51 IDS_LOCALE_CHANGE_MESSAGE, | |
52 l10n_util::GetDisplayNameForLocale(from_locale_, to_locale_, true), | |
53 l10n_util::GetDisplayNameForLocale(to_locale_, to_locale_, true)); | |
54 } | |
55 | |
56 virtual bool Accept() { | |
57 UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Revert")); | |
58 // Revert locale change. | |
59 tab_contents_->profile()->ChangeApplicationLocale(from_locale_, true); | |
60 PrefService* prefs = tab_contents_->profile()->GetPrefs(); | |
61 if (prefs) { | |
62 prefs->SetString(prefs::kApplicationLocaleBackup, from_locale_); | |
63 prefs->ClearPref(prefs::kApplicationLocaleAccepted); | |
64 prefs->ScheduleSavePersistentPrefs(); | |
65 } | |
66 Browser* browser = Browser::GetBrowserForController( | |
67 &tab_contents_->controller(), NULL); | |
68 if (browser) | |
69 browser->ExecuteCommand(IDC_EXIT); | |
70 return true; | |
71 } | |
72 | |
73 virtual bool Cancel() { | |
74 UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Accept")); | |
75 PrefService* prefs = tab_contents_->profile()->GetPrefs(); | |
76 if (prefs) { | |
77 prefs->SetString(prefs::kApplicationLocaleBackup, to_locale_); | |
78 prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_); | |
79 prefs->ScheduleSavePersistentPrefs(); | |
80 } | |
81 return true; | |
82 } | |
83 | |
84 virtual bool ShouldExpire( | |
85 const NavigationController::LoadCommittedDetails& details) const { | |
86 return false; | |
87 } | |
88 | |
89 virtual void InfoBarDismissed() { | |
90 Cancel(); | |
91 } | |
92 | |
93 private: | |
94 TabContents* tab_contents_; | |
95 std::string from_locale_, to_locale_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(LocaleChangeInfoBar); | |
98 }; | |
99 | |
100 class LocaleChangeGuardImpl { | |
101 LocaleChangeGuardImpl() | |
102 : profile_id_(Profile::InvalidProfileId), | |
103 bar_(NULL) {} | |
104 | |
105 void Check(TabContents* tab_contents) { | |
106 // We want Locale Change InfoBar to be shown no more than once per session. | |
107 if (bar_ != NULL) | |
108 return; | |
109 // We check profile fingerprint because on ChromeOS this guard | |
110 // first time is invoked for a dummy profile. | |
111 ProfileId cur_profile_id = tab_contents->profile()->GetRuntimeId(); | |
112 if (cur_profile_id == profile_id_) | |
113 return; | |
114 profile_id_ = cur_profile_id; | |
115 std::string cur_locale = g_browser_process->GetApplicationLocale(); | |
116 if (cur_locale.empty()) | |
117 return; | |
118 PrefService* prefs = tab_contents->profile()->GetPrefs(); | |
119 if (prefs == NULL) | |
120 return; | |
121 std::string to_locale = | |
122 prefs->GetString(prefs::kApplicationLocaleOverride); | |
123 if (!to_locale.empty()) { | |
124 DCHECK(to_locale == cur_locale); | |
125 return; | |
126 } | |
127 to_locale = prefs->GetString(prefs::kApplicationLocale); | |
128 if (to_locale != cur_locale) | |
129 return; | |
130 std::string from_locale = prefs->GetString(prefs::kApplicationLocaleBackup); | |
131 if (from_locale.empty() || from_locale == to_locale) | |
132 return; | |
133 bar_.reset(new LocaleChangeInfoBar(tab_contents, from_locale, to_locale)); | |
134 tab_contents->AddInfoBar(bar_.get()); | |
135 } | |
136 | |
137 ProfileId profile_id_; | |
138 scoped_ptr<LocaleChangeInfoBar> bar_; | |
139 | |
140 friend struct base::DefaultLazyInstanceTraits<LocaleChangeGuardImpl>; | |
141 friend struct ::LocaleChangeGuard; | |
142 }; | |
143 | |
144 base::LazyInstance<LocaleChangeGuardImpl> g_locale_change_guard( | |
145 base::LINKER_INITIALIZED); | |
146 | |
147 } // namespace | |
148 #endif // defined(OS_CHROMEOS) | |
149 | |
150 // static | |
151 void LocaleChangeGuard::Check(TabContents* tab_contents) { | |
152 #if defined(OS_CHROMEOS) | |
153 g_locale_change_guard.Get().Check(tab_contents); | |
154 #endif | |
155 } | |
OLD | NEW |