File: locale_change_guard.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (235 lines) | stat: -rw-r--r-- 7,434 bytes parent folder | download | duplicates (3)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/locale/locale_change_guard.h"

#include <algorithm>
#include <string_view>

#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/functional/bind.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/ash/base/locale_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/language/core/browser/pref_names.h"
#include "components/language/core/common/locale_util.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"

namespace ash {

namespace {

using ::base::UserMetricsAction;
using ::content::WebContents;

// These are the languages that do not require user notification when locale is
// switched automatically between regions within the same language.
//
// New language in kAcceptLanguageList should be added either here or to
// to the exception list in unit test.
constexpr auto kSkipShowNotificationLanguages =
    base::MakeFixedFlatSet<std::string_view>({"en", "de", "fr", "it"});

}  // anonymous namespace

LocaleChangeGuard::LocaleChangeGuard(Profile* profile, PrefService* local_state)
    : profile_(profile), local_state_(local_state) {
  DCHECK(profile_);
  DeviceSettingsService::Get()->AddObserver(this);
}

LocaleChangeGuard::~LocaleChangeGuard() {
  if (DeviceSettingsService::IsInitialized())
    DeviceSettingsService::Get()->RemoveObserver(this);
}

void LocaleChangeGuard::OnLogin() {
  if (session_manager::SessionManager::Get()->IsSessionStarted()) {
    Check();
  } else {
    if (session_observation_.IsObserving()) {
      DCHECK(session_observation_.IsObservingSource(
          session_manager::SessionManager::Get()));
      return;
    }
    session_observation_.Observe(session_manager::SessionManager::Get());
  }
}

void LocaleChangeGuard::RevertLocaleChange() {
  if (from_locale_.empty() || to_locale_.empty()) {
    NOTREACHED();
  }
  if (reverted_)
    return;
  reverted_ = true;
  base::RecordAction(UserMetricsAction("LanguageChange_Revert"));
  profile_->ChangeAppLocale(from_locale_,
                            Profile::APP_LOCALE_CHANGED_VIA_REVERT);
  chrome::AttemptUserExit();
}

void LocaleChangeGuard::OnUserSessionStarted(bool is_primary_user) {
  session_observation_.Reset();
  Check();
}

void LocaleChangeGuard::OwnershipStatusChanged() {
  if (!DeviceSettingsService::Get()->HasPrivateOwnerKey()) {
    return;
  }

  if (!local_state_) {
    return;
  }

  PrefService* prefs = profile_->GetPrefs();
  DCHECK(prefs);
  std::string owner_locale =
      prefs->GetString(language::prefs::kApplicationLocale);
  language::ConvertToActualUILocale(&owner_locale);
  if (!owner_locale.empty()) {
    local_state_->SetString(prefs::kOwnerLocale, owner_locale);
  }
}

void LocaleChangeGuard::Check() {
  std::string cur_locale = g_browser_process->GetApplicationLocale();
  if (cur_locale.empty()) {
    NOTREACHED();
  }

  PrefService* prefs = profile_->GetPrefs();
  if (prefs == nullptr) {
    NOTREACHED();
  }

  std::string to_locale = prefs->GetString(language::prefs::kApplicationLocale);
  // Ensure that synchronization does not change the locale to a value not
  // allowed by enterprise policy.
  if (!locale_util::IsAllowedUILanguage(to_locale, prefs)) {
    prefs->SetString(language::prefs::kApplicationLocale,
                     locale_util::GetAllowedFallbackUILanguage(prefs));
  }

  language::ConvertToActualUILocale(&to_locale);

  if (to_locale != cur_locale && locale_changed_during_login_) {
    LocaleUpdateController::Get()->OnLocaleChanged();
    return;
  }

  std::string from_locale = prefs->GetString(prefs::kApplicationLocaleBackup);

  if (!RequiresUserConfirmation(from_locale, to_locale)) {
    // If the locale changed during login (e.g. from the owner's locale), just
    // notify ash about the change, so system UI gets updated.
    // If the change also requires user confirmation, the UI will be updates as
    // part of `LocaleUpdateController::ConfirmLocaleChange`.
    if (locale_changed_during_login_)
      LocaleUpdateController::Get()->OnLocaleChanged();
    return;
  }

  // Showing notification.
  if (from_locale_ != from_locale || to_locale_ != to_locale) {
    // Falling back to showing message in current locale.
    LOG(ERROR) << "Showing locale change notification in current (not "
                  "previous) language";
    PrepareChangingLocale(from_locale, to_locale);
  }

  LocaleUpdateController::Get()->ConfirmLocaleChange(
      cur_locale, from_locale_, to_locale_,
      base::BindOnce(&LocaleChangeGuard::OnResult,
                     weak_ptr_factory_.GetWeakPtr()));
}

void LocaleChangeGuard::OnResult(LocaleNotificationResult result) {
  switch (result) {
    case LocaleNotificationResult::kAccept:
      AcceptLocaleChange();
      break;
    case LocaleNotificationResult::kRevert:
      RevertLocaleChange();
      break;
  }
}

void LocaleChangeGuard::AcceptLocaleChange() {
  if (from_locale_.empty() || to_locale_.empty()) {
    NOTREACHED();
  }

  // Check whether locale has been reverted or changed.
  // If not: mark current locale as accepted.
  if (reverted_)
    return;
  PrefService* prefs = profile_->GetPrefs();
  if (prefs == nullptr) {
    NOTREACHED();
  }
  if (prefs->GetString(language::prefs::kApplicationLocale) != to_locale_)
    return;
  base::RecordAction(UserMetricsAction("LanguageChange_Accept"));
  prefs->SetString(prefs::kApplicationLocaleBackup, to_locale_);
  prefs->SetString(prefs::kApplicationLocaleAccepted, to_locale_);
}

void LocaleChangeGuard::PrepareChangingLocale(std::string_view from_locale,
                                              std::string_view to_locale) {
  std::string cur_locale = g_browser_process->GetApplicationLocale();
  if (!from_locale.empty())
    from_locale_ = std::string(from_locale);
  if (!to_locale.empty())
    to_locale_ = std::string(to_locale);
}

bool LocaleChangeGuard::RequiresUserConfirmation(
    std::string_view from_locale,
    std::string_view to_locale) const {
  // No locale change was detected for the user.
  if (from_locale.empty() || from_locale == to_locale)
    return false;

  // The target locale is already accepted.
  if (profile_->GetPrefs()->GetString(prefs::kApplicationLocaleAccepted) ==
      to_locale) {
    return false;
  }

  return ShouldShowLocaleChangeNotification(from_locale, to_locale);
}

// static
bool LocaleChangeGuard::ShouldShowLocaleChangeNotification(
    std::string_view from_locale,
    std::string_view to_locale) {
  const std::string_view from_lang = l10n_util::GetLanguage(from_locale);
  const std::string_view to_lang = l10n_util::GetLanguage(to_locale);

  if (from_locale == to_locale)
    return false;

  if (from_lang != to_lang)
    return true;

  return !base::Contains(kSkipShowNotificationLanguages, from_lang);
}

// static
base::span<const std::string_view>
LocaleChangeGuard::GetSkipShowNotificationLanguagesForTesting() {
  return kSkipShowNotificationLanguages;
}

}  // namespace ash