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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
// Copyright 2013 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/system/timezone_util.h"
#include <stddef.h>
#include <memory>
#include <string>
#include <utility>
#include "ash/constants/ash_switches.h"
#include "base/command_line.h"
#include "base/i18n/rtl.h"
#include "base/i18n/unicodestring.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/ash/system/timezone_resolver_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/components/install_attributes/install_attributes.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "chromeos/ash/components/settings/timezone_settings.h"
#include "chromeos/ash/components/timezone/timezone_request.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "third_party/icu/source/common/unicode/ures.h"
#include "third_party/icu/source/common/unicode/utypes.h"
#include "third_party/icu/source/i18n/unicode/calendar.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"
#include "ui/base/l10n/l10n_util.h"
namespace {
struct UResClose {
inline void operator() (UResourceBundle* b) const {
ures_close(b);
}
};
base::LazyInstance<base::Lock>::Leaky g_timezone_bundle_lock =
LAZY_INSTANCE_INITIALIZER;
const size_t kMaxGeolocationResponseLength = 8;
// Returns an exemplary city in the given timezone.
std::u16string GetExemplarCity(const icu::TimeZone& zone) {
// These will be leaked at the end.
static UResourceBundle* zone_bundle = nullptr;
static UResourceBundle* zone_strings = nullptr;
UErrorCode status = U_ZERO_ERROR;
{
// TODO(jungshik): After upgrading to ICU 4.6, use U_ICUDATA_ZONE in
// ures_open().
base::AutoLock lock(g_timezone_bundle_lock.Get());
if (!zone_bundle)
zone_bundle = ures_open(nullptr, uloc_getDefault(), &status);
if (!zone_strings) {
zone_strings =
ures_getByKey(zone_bundle, "zone_strings", nullptr, &status);
}
}
icu::UnicodeString zone_id;
zone.getID(zone_id);
std::string zone_id_str;
zone_id.toUTF8String(zone_id_str);
// Resource keys for timezones use ':' in place of '/'.
base::ReplaceSubstringsAfterOffset(&zone_id_str, 0, "/", ":");
std::unique_ptr<UResourceBundle, UResClose> zone_item(
ures_getByKey(zone_strings, zone_id_str.c_str(), nullptr, &status));
icu::UnicodeString city;
if (!U_FAILURE(status)) {
city = icu::ures_getUnicodeStringByKey(zone_item.get(), "ec", &status);
if (U_SUCCESS(status))
return base::i18n::UnicodeStringToString16(city);
}
// Fallback case in case of failure.
base::ReplaceSubstringsAfterOffset(&zone_id_str, 0, ":", "/");
// Take the last component of a timezone id (e.g. 'Baz' in 'Foo/Bar/Baz').
// Depending on timezones, keeping all but the 1st component
// (e.g. Bar/Baz) may be better, but our current list does not have
// any timezone for which that's the case.
std::string::size_type slash_pos = zone_id_str.rfind('/');
if (slash_pos != std::string::npos && slash_pos < zone_id_str.size())
zone_id_str.erase(0, slash_pos + 1);
// zone id has '_' in place of ' '.
base::ReplaceSubstringsAfterOffset(&zone_id_str, 0, "_", " ");
return base::ASCIIToUTF16(zone_id_str);
}
// Gets the given timezone's name for visualization.
std::u16string GetTimezoneName(const icu::TimeZone& timezone) {
// Instead of using the raw_offset, use the offset in effect now.
// For instance, US Pacific Time, the offset shown will be -7 in summer
// while it'll be -8 in winter.
int raw_offset, dst_offset;
UDate now = icu::Calendar::getNow();
UErrorCode status = U_ZERO_ERROR;
timezone.getOffset(now, false, raw_offset, dst_offset, status);
DCHECK(U_SUCCESS(status));
int offset = raw_offset + dst_offset;
// |offset| is in msec.
int minute_offset = std::abs(offset) / 60000;
int hour_offset = minute_offset / 60;
int min_remainder = minute_offset % 60;
// Some timezones have a non-integral hour offset. So, we need to use hh:mm
// form.
std::string offset_str = base::StringPrintf(
"UTC%c%d:%02d", offset >= 0 ? '+' : '-', hour_offset, min_remainder);
// TODO(jungshik): When coming up with a better list of timezones, we also
// have to come up with better 'display' names. One possibility is to list
// multiple cities (e.g. "Los Angeles, Vancouver .." in the order of
// the population of a country the city belongs to.).
// We can also think of using LONG_GENERIC or LOCATION once we upgrade
// to ICU 4.6.
// In the meantime, we use "LONG" name with "Exemplar City" to distinguish
// multiple timezones with the same "LONG" name but with different
// rules (e.g. US Mountain Time in Denver vs Phoenix).
icu::UnicodeString id;
icu::UnicodeString name;
timezone.getID(id);
if (id == icu::UnicodeString(ash::system::kUTCTimezoneName)) {
name = icu::UnicodeString(
l10n_util::GetStringUTF8(IDS_OPTIONS_SETTINGS_TIMEZONE_DISPLAY_NAME_UTC)
.c_str());
} else {
timezone.getDisplayName(dst_offset != 0, icu::TimeZone::LONG, name);
}
std::u16string result(l10n_util::GetStringFUTF16(
IDS_OPTIONS_SETTINGS_TIMEZONE_DISPLAY_TEMPLATE,
base::ASCIIToUTF16(offset_str), base::i18n::UnicodeStringToString16(name),
GetExemplarCity(timezone)));
base::i18n::AdjustStringForLocaleDirection(&result);
return result;
}
bool CanSetSystemTimezoneFromManagedGuestSession() {
const int automatic_detection_policy =
g_browser_process->local_state()->GetInteger(
prefs::kSystemTimezoneAutomaticDetectionPolicy);
return (automatic_detection_policy ==
enterprise_management::SystemTimezoneProto::DISABLED) ||
(automatic_detection_policy ==
enterprise_management::SystemTimezoneProto::USERS_DECIDE);
}
} // namespace
namespace ash {
namespace system {
std::optional<std::string> GetCountryCodeFromTimezoneIfAvailable(
const std::string& timezone) {
// Determine region code from timezone id.
char region[kMaxGeolocationResponseLength];
UErrorCode error = U_ZERO_ERROR;
auto timezone_unicode = icu::UnicodeString::fromUTF8(timezone);
icu::TimeZone::getRegion(timezone_unicode, region,
kMaxGeolocationResponseLength, error);
// Track failures.
if (U_FAILURE(error))
return std::nullopt;
return base::ToLowerASCII(region);
}
std::u16string GetCurrentTimezoneName() {
return GetTimezoneName(TimezoneSettings::GetInstance()->GetTimezone());
}
// Creates a list of pairs of each timezone's ID and name.
base::Value::List GetTimezoneList() {
const auto& timezones = TimezoneSettings::GetInstance()->GetTimezoneList();
base::Value::List timezone_list;
for (const auto& timezone : timezones) {
base::Value::List option;
option.Append(TimezoneSettings::GetTimezoneID(*timezone));
option.Append(GetTimezoneName(*timezone));
timezone_list.Append(std::move(option));
}
return timezone_list;
}
bool HasSystemTimezonePolicy() {
if (!InstallAttributes::Get()->IsEnterpriseManaged())
return false;
std::string policy_timezone;
if (CrosSettings::Get()->GetString(kSystemTimezonePolicy, &policy_timezone) &&
!policy_timezone.empty()) {
VLOG(1) << "Refresh TimeZone: TimeZone settings are overridden"
<< " by DevicePolicy.";
return true;
}
return false;
}
bool IsTimezonePrefsManaged(const std::string& pref_name) {
DCHECK(pref_name == kSystemTimezone || pref_name == prefs::kUserTimezone ||
pref_name == prefs::kResolveTimezoneByGeolocationMethod);
std::string policy_timezone;
if (CrosSettings::Get()->GetString(kSystemTimezonePolicy, &policy_timezone) &&
!policy_timezone.empty()) {
return true;
}
// System timezone preference is managed only if kSystemTimezonePolicy
// present, which we checked above.
//
// kSystemTimezoneAutomaticDetectionPolicy (see below) controls only user
// time zone preference, and user time zone resolve preference.
if (pref_name == kSystemTimezone)
return false;
const PrefService* local_state = g_browser_process->local_state();
if (!local_state->IsManagedPreference(
prefs::kSystemTimezoneAutomaticDetectionPolicy)) {
return false;
}
int resolve_policy_value =
local_state->GetInteger(prefs::kSystemTimezoneAutomaticDetectionPolicy);
switch (resolve_policy_value) {
case enterprise_management::SystemTimezoneProto::USERS_DECIDE:
return false;
case enterprise_management::SystemTimezoneProto::DISABLED:
// This only disables resolving.
return pref_name == prefs::kResolveTimezoneByGeolocationMethod;
case enterprise_management::SystemTimezoneProto::IP_ONLY:
case enterprise_management::SystemTimezoneProto::SEND_WIFI_ACCESS_POINTS:
case enterprise_management::SystemTimezoneProto::SEND_ALL_LOCATION_INFO:
return true;
}
// Default for unknown policy value.
NOTREACHED() << "Unrecognized policy value: " << resolve_policy_value;
}
void ApplyTimeZone(const TimeZoneResponseData* timezone) {
if (!g_browser_process->platform_part()
->GetTimezoneResolverManager()
->ShouldApplyResolvedTimezone()) {
return;
}
if (timezone->timeZoneId.empty())
return;
VLOG(1) << "Refresh TimeZone: setting timezone to '" << timezone->timeZoneId
<< "'";
if (PerUserTimezoneEnabled()) {
const user_manager::UserManager* user_manager =
user_manager::UserManager::Get();
const user_manager::User* primary_user = user_manager->GetPrimaryUser();
if (primary_user) {
Profile* profile = ProfileHelper::Get()->GetProfileByUser(primary_user);
// profile can be NULL only if user has logged in, but profile has not
// been initialized yet. Ignore delayed time zone update until user
// preferences are initialized.
if (!profile)
return;
profile->GetPrefs()->SetString(prefs::kUserTimezone,
timezone->timeZoneId);
// For non-enterprise device, `Preferences::ApplyPreferences()`
// will automatically change system timezone because user is primary.
// But it may not happen for enterprise device, as policy may prevent
// user from changing device time zone manually.
// That is the reason we always update system time zone here.
TimezoneSettings::GetInstance()->SetTimezoneFromID(
base::UTF8ToUTF16(timezone->timeZoneId));
} else {
SetSystemAndSigninScreenTimezone(timezone->timeZoneId);
}
} else {
TimezoneSettings::GetInstance()->SetTimezoneFromID(
base::UTF8ToUTF16(timezone->timeZoneId));
}
}
void UpdateSystemTimezone(Profile* profile) {
if (IsTimezonePrefsManaged(prefs::kUserTimezone)) {
VLOG(1) << "Ignoring user timezone change, because timezone is enterprise "
"managed.";
return;
}
const user_manager::UserManager* user_manager =
user_manager::UserManager::Get();
const user_manager::User* user =
ProfileHelper::Get()->GetUserByProfile(profile);
const AccountId owner(user_manager->GetOwnerAccountId());
const bool user_is_owner =
owner.is_valid() && (owner == user->GetAccountId());
const std::string value =
profile->GetPrefs()->GetString(prefs::kUserTimezone);
if (user_is_owner) {
g_browser_process->local_state()->SetString(prefs::kSigninScreenTimezone,
value);
}
if (user_manager->GetPrimaryUser() == user && PerUserTimezoneEnabled())
SetSystemTimezone(user, value);
}
// TODO(b/353580799): Add unit tests for this function.
bool CanSetSystemTimezone(const user_manager::User* user) {
if (!user->is_logged_in()) {
return false;
}
switch (user->GetType()) {
case user_manager::UserType::kRegular:
case user_manager::UserType::kKioskChromeApp:
case user_manager::UserType::kKioskWebApp:
case user_manager::UserType::kKioskIWA:
case user_manager::UserType::kChild:
return true;
case user_manager::UserType::kGuest:
return false;
case user_manager::UserType::kPublicAccount:
return CanSetSystemTimezoneFromManagedGuestSession();
// No default case means the compiler makes sure we handle new types.
}
NOTREACHED();
}
bool SetSystemTimezone(const user_manager::User* user,
const std::string& timezone) {
DCHECK(user);
if (!CanSetSystemTimezone(user))
return false;
TimezoneSettings::GetInstance()->SetTimezoneFromID(
base::UTF8ToUTF16(timezone));
return true;
}
void SetSystemAndSigninScreenTimezone(const std::string& timezone) {
if (timezone.empty())
return;
g_browser_process->local_state()->SetString(prefs::kSigninScreenTimezone,
timezone);
std::string current_timezone_id;
CrosSettings::Get()->GetString(kSystemTimezone, ¤t_timezone_id);
if (current_timezone_id != timezone) {
TimezoneSettings::GetInstance()->SetTimezoneFromID(
base::UTF8ToUTF16(timezone));
}
}
bool PerUserTimezoneEnabled() {
return !base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisablePerUserTimezone);
}
void SetTimezoneFromUI(Profile* profile, const std::string& timezone_id) {
const user_manager::User* user =
ProfileHelper::Get()->GetUserByProfile(profile);
if (!PerUserTimezoneEnabled()) {
SetSystemTimezone(user, timezone_id);
return;
}
if (ProfileHelper::IsSigninProfile(profile)) {
SetSystemAndSigninScreenTimezone(timezone_id);
return;
}
if (ProfileHelper::IsEphemeralUserProfile(profile)) {
SetSystemTimezone(user, timezone_id);
return;
}
Profile* primary_profile = ProfileManager::GetPrimaryUserProfile();
if (primary_profile && profile->IsSameOrParent(primary_profile)) {
profile->GetPrefs()->SetString(prefs::kUserTimezone, timezone_id);
return;
}
// Time zone UI should be blocked for non-primary users.
NOTREACHED();
}
bool FineGrainedTimeZoneDetectionEnabled() {
return !base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableFineGrainedTimeZoneDetection);
}
} // namespace system
} // namespace ash
|