Chromium Code Reviews| Index: chrome/browser/chromeos/locale_change_guard.cc |
| diff --git a/chrome/browser/chromeos/locale_change_guard.cc b/chrome/browser/chromeos/locale_change_guard.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a565bfc8a0ef922e1e91658d83c204bfb832e880 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/locale_change_guard.cc |
| @@ -0,0 +1,136 @@ |
| +// 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/lazy_instance.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/prefs/pref_service.h" |
| +#include "chrome/browser/profiles/profile.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" |
| + |
| +class ListValue; |
| + |
| +namespace { |
| + |
| +class LocaleChangeGuardImpl { |
| + 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
|
| + : profile_id_(Profile::InvalidProfileId), |
| + tab_contents_(NULL), |
| + note_(NULL), |
| + reverted_(false) {} |
| + |
| + void RevertLocaleChange(const ListValue* list) { |
| + reverted_ = true; |
| + UserMetrics::RecordAction(UserMetricsAction("LanguageChange_Revert")); |
| + tab_contents_->profile()->ChangeApplicationLocale(from_locale_, true); |
| + PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
| + if (prefs) { |
| + 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); |
|
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
|
| + } |
| + |
| + void Check(TabContents* tab_contents) { |
| + // 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
|
| + if (note_ != NULL) |
| + return; |
| + // We check profile Id because on ChromeOS this guard |
| + // 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.
|
| + ProfileId cur_profile_id = tab_contents->profile()->GetRuntimeId(); |
| + if (cur_profile_id == profile_id_) |
| + return; |
| + profile_id_ = cur_profile_id; |
| + std::string cur_locale = g_browser_process->GetApplicationLocale(); |
| + if (cur_locale.empty()) |
| + return; |
| + PrefService* prefs = tab_contents->profile()->GetPrefs(); |
| + if (prefs == NULL) |
| + return; |
| + to_locale_ = prefs->GetString(prefs::kApplicationLocaleOverride); |
| + if (!to_locale_.empty()) { |
| + DCHECK(to_locale_ == cur_locale); |
| + 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
|
| + } |
| + to_locale_ = prefs->GetString(prefs::kApplicationLocale); |
| + if (to_locale_ != cur_locale) |
| + return; |
| + from_locale_ = prefs->GetString(prefs::kApplicationLocaleBackup); |
| + if (from_locale_.empty() || from_locale_ == to_locale_) |
| + return; |
| + note_.reset(new chromeos::SystemNotification( |
| + tab_contents->profile(), |
| + "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.
|
| + IDR_DEFAULT_FAVICON, |
| + l10n_util::GetStringUTF16( |
| + IDS_OPTIONS_SETTINGS_SECTION_TITLE_LANGUAGE))); |
| + tab_contents_ = tab_contents; |
| + 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, &LocaleChangeGuardImpl::RevertLocaleChange), |
| + 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.
|
| + } |
| + |
| + ~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.
|
| + // Check whether locale has been reverted or changed. |
| + // If not: mark current locale as accepted. |
| + if (tab_contents_ == NULL) |
| + return; |
| + 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; |
| + prefs->SetString(prefs::kApplicationLocaleBackup, to_locale_); |
| + prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_); |
| + prefs->ScheduleSavePersistentPrefs(); |
| + } |
| + |
| + 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.
|
| + ProfileId profile_id_; |
| + TabContents* tab_contents_; |
| + scoped_ptr<chromeos::SystemNotification> note_; |
| + bool reverted_; |
| + |
| + friend struct base::DefaultLazyInstanceTraits<LocaleChangeGuardImpl>; |
| + friend struct chromeos::LocaleChangeGuard; |
| +}; |
| + |
| +base::LazyInstance<LocaleChangeGuardImpl> g_locale_change_guard( |
| + base::LINKER_INITIALIZED); |
| + |
| +} // namespace |
| + |
| +namespace chromeos { |
| + |
| +// static |
| +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
|
| + g_locale_change_guard.Get().Check(tab_contents); |
| +} |
| + |
| +} // namespace chromeos |