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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
// 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.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chromeos/ash/components/settings/timezone_settings.h"
#include <stddef.h>
#include <memory>
#include <string>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/i18n/unicodestring.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/singleton.h"
#include "base/observer_list.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "base/task/task_runner.h"
#include "base/task/thread_pool.h"
#include "chromeos/ash/components/settings/timezone_settings_helper.h"
namespace ash {
namespace system {
const char kUTCTimezoneName[] = "Etc/GMT";
} // namespace system
} // namespace ash
namespace {
// The filepath to the timezone file that symlinks to the actual timezone file.
const char kTimezoneSymlink[] = "/var/lib/timezone/localtime";
const char kTimezoneSymlink2[] = "/var/lib/timezone/localtime2";
// The directory that contains all the timezone files. So for timezone
// "US/Pacific", the actual timezone file is: "/usr/share/zoneinfo/US/Pacific"
const char kTimezoneFilesDir[] = "/usr/share/zoneinfo/";
// Fallback time zone ID used in case of an unexpected error.
const char kFallbackTimeZoneId[] = "America/Los_Angeles";
// TODO(jungshik): Using Enumerate method in ICU gives 600+ timezones.
// Even after filtering out duplicate entries with a strict identity check,
// we still have 400+ zones. Relaxing the criteria for the timezone
// identity is likely to cut down the number to < 100. Until we
// come up with a better list, we hard-code the following list. It came from
// from Android initially, but more entries have been added.
// Note that the list is sorted in terms of timezone offset from UTC.
static const char* kTimeZones[] = {
"Pacific/Midway",
"Pacific/Honolulu",
"America/Anchorage",
"America/Los_Angeles",
"America/Vancouver",
"America/Tijuana",
"America/Phoenix",
"America/Chihuahua",
"America/Denver",
"America/Edmonton",
"America/Mazatlan",
"America/Regina",
"America/Costa_Rica",
"America/Chicago",
"America/Mexico_City",
"America/Tegucigalpa",
"America/Winnipeg",
"Pacific/Easter",
"America/Bogota",
"America/Lima",
"America/New_York",
"America/Toronto",
"America/Caracas",
"America/Barbados",
"America/Halifax",
"America/Manaus",
"America/Santiago",
"America/St_Johns",
"America/Araguaina",
"America/Argentina/Buenos_Aires",
"America/Argentina/San_Luis",
"America/Montevideo",
"America/Santiago",
"America/Sao_Paulo",
"America/Godthab",
"Atlantic/South_Georgia",
"Atlantic/Cape_Verde",
ash::system::kUTCTimezoneName,
"Atlantic/Azores",
"Atlantic/Reykjavik",
"Atlantic/St_Helena",
"Africa/Casablanca",
"Atlantic/Faroe",
"Europe/Dublin",
"Europe/Lisbon",
"Europe/London",
"Europe/Amsterdam",
"Europe/Belgrade",
"Europe/Berlin",
"Europe/Bratislava",
"Europe/Brussels",
"Europe/Budapest",
"Europe/Copenhagen",
"Europe/Ljubljana",
"Europe/Madrid",
"Europe/Malta",
"Europe/Oslo",
"Europe/Paris",
"Europe/Prague",
"Europe/Rome",
"Europe/Stockholm",
"Europe/Sarajevo",
"Europe/Tirane",
"Europe/Vaduz",
"Europe/Vienna",
"Europe/Warsaw",
"Europe/Zagreb",
"Europe/Zurich",
"Africa/Windhoek",
"Africa/Lagos",
"Africa/Brazzaville",
"Africa/Cairo",
"Africa/Harare",
"Africa/Maputo",
"Africa/Johannesburg",
"Europe/Kaliningrad",
"Europe/Athens",
"Europe/Bucharest",
"Europe/Chisinau",
"Europe/Helsinki",
"Europe/Istanbul",
"Europe/Kiev",
"Europe/Riga",
"Europe/Sofia",
"Europe/Tallinn",
"Europe/Vilnius",
"Asia/Amman",
"Asia/Beirut",
"Asia/Jerusalem",
"Africa/Nairobi",
"Asia/Baghdad",
"Asia/Riyadh",
"Asia/Kuwait",
"Europe/Minsk",
"Europe/Moscow",
"Asia/Tehran",
"Europe/Samara",
"Asia/Dubai",
"Asia/Tbilisi",
"Indian/Mauritius",
"Asia/Baku",
"Asia/Yerevan",
"Asia/Kabul",
"Asia/Karachi",
"Asia/Aqtobe",
"Asia/Ashgabat",
"Asia/Oral",
"Asia/Yekaterinburg",
"Asia/Calcutta",
"Asia/Colombo",
"Asia/Katmandu",
"Asia/Omsk",
"Asia/Almaty",
"Asia/Dhaka",
"Asia/Novosibirsk",
"Asia/Rangoon",
"Asia/Bangkok",
"Asia/Jakarta",
"Asia/Krasnoyarsk",
"Asia/Novokuznetsk",
"Asia/Ho_Chi_Minh",
"Asia/Phnom_Penh",
"Asia/Vientiane",
"Asia/Shanghai",
"Asia/Hong_Kong",
"Asia/Kuala_Lumpur",
"Asia/Singapore",
"Asia/Manila",
"Asia/Taipei",
"Asia/Ulaanbaatar",
"Asia/Makassar",
"Asia/Irkutsk",
"Asia/Yakutsk",
"Australia/Perth",
"Australia/Eucla",
"Asia/Seoul",
"Asia/Tokyo",
"Asia/Jayapura",
"Asia/Sakhalin",
"Asia/Vladivostok",
"Asia/Magadan",
"Australia/Darwin",
"Australia/Adelaide",
"Pacific/Guam",
"Australia/Brisbane",
"Australia/Hobart",
"Australia/Sydney",
"Asia/Anadyr",
"Pacific/Port_Moresby",
"Asia/Kamchatka",
"Pacific/Fiji",
"Pacific/Majuro",
"Pacific/Auckland",
"Pacific/Tongatapu",
"Pacific/Apia",
"Pacific/Kiritimati",
};
std::string GetTimezoneIDAsString() {
// Compare with chromiumos/src/platform/init/ui.conf which fixes certain
// incorrect states of the timezone symlink on startup. Thus errors occuring
// here should be rather contrived.
// Look at kTimezoneSymlink, see which timezone we are symlinked to.
char buf[256];
const ssize_t len = readlink(kTimezoneSymlink, buf, sizeof(buf) - 1);
if (len == -1) {
LOG(ERROR) << "GetTimezoneID: Cannot read timezone symlink "
<< kTimezoneSymlink;
return std::string();
}
std::string_view timezone(buf, len);
// Remove kTimezoneFilesDir from the beginning.
auto remainder = base::RemovePrefix(timezone, kTimezoneFilesDir);
if (!remainder) {
LOG(ERROR) << "GetTimezoneID: Timezone symlink is wrong " << timezone;
return std::string();
}
return std::string(*remainder);
}
void SetTimezoneIDFromString(const std::string& id) {
// Change the kTimezoneSymlink symlink to the path for this timezone.
// We want to do this in an atomic way. So we are going to create the symlink
// at kTimezoneSymlink2 and then move it to kTimezoneSymlink
base::FilePath timezone_symlink(kTimezoneSymlink);
base::FilePath timezone_symlink2(kTimezoneSymlink2);
base::FilePath timezone_file(kTimezoneFilesDir + id);
// Make sure timezone_file exists.
if (!base::PathExists(timezone_file)) {
LOG(ERROR) << "SetTimezoneID: Cannot find timezone file "
<< timezone_file.value();
return;
}
// Delete old symlink2 if it exists.
base::DeleteFile(timezone_symlink2);
// Create new symlink2.
if (symlink(timezone_file.value().c_str(),
timezone_symlink2.value().c_str()) == -1) {
LOG(ERROR) << "SetTimezoneID: Unable to create symlink "
<< timezone_symlink2.value() << " to " << timezone_file.value();
return;
}
// Move symlink2 to symlink.
if (!base::ReplaceFile(timezone_symlink2, timezone_symlink, NULL)) {
LOG(ERROR) << "SetTimezoneID: Unable to move symlink "
<< timezone_symlink2.value() << " to "
<< timezone_symlink.value();
}
}
// Common code of the TimezoneSettings implementations.
class TimezoneSettingsBaseImpl : public ash::system::TimezoneSettings {
public:
TimezoneSettingsBaseImpl(const TimezoneSettingsBaseImpl&) = delete;
TimezoneSettingsBaseImpl& operator=(const TimezoneSettingsBaseImpl&) = delete;
~TimezoneSettingsBaseImpl() override;
// TimezoneSettings implementation:
const icu::TimeZone& GetTimezone() override;
std::u16string GetCurrentTimezoneID() override;
void SetTimezoneFromID(const std::u16string& timezone_id) override;
void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
const std::vector<std::unique_ptr<icu::TimeZone>>& GetTimezoneList()
const override;
protected:
TimezoneSettingsBaseImpl();
// Returns |timezone| if it is an element of |timezones_|.
// Otherwise, returns a timezone from |timezones_|, if such exists, that has
// the same rule as the given |timezone|.
// Otherwise, returns NULL.
// Note multiple timezones with the same time zone rules may exist
// e.g.
// US/Pacific == America/Los_Angeles
const icu::TimeZone* GetKnownTimezoneOrNull(
const icu::TimeZone& timezone) const;
base::ObserverList<Observer>::Unchecked observers_;
std::vector<std::unique_ptr<icu::TimeZone>> timezones_;
std::unique_ptr<icu::TimeZone> timezone_;
};
// The TimezoneSettings implementation used in production.
class TimezoneSettingsImpl : public TimezoneSettingsBaseImpl {
public:
// TimezoneSettings implementation:
void SetTimezone(const icu::TimeZone& timezone) override;
static TimezoneSettingsImpl* GetInstance();
TimezoneSettingsImpl(const TimezoneSettingsImpl&) = delete;
TimezoneSettingsImpl& operator=(const TimezoneSettingsImpl&) = delete;
private:
friend struct base::DefaultSingletonTraits<TimezoneSettingsImpl>;
TimezoneSettingsImpl();
};
// The stub TimezoneSettings implementation used on Linux desktop.
class TimezoneSettingsStubImpl : public TimezoneSettingsBaseImpl {
public:
// TimezoneSettings implementation:
void SetTimezone(const icu::TimeZone& timezone) override;
static TimezoneSettingsStubImpl* GetInstance();
TimezoneSettingsStubImpl(const TimezoneSettingsStubImpl&) = delete;
TimezoneSettingsStubImpl& operator=(const TimezoneSettingsStubImpl&) = delete;
private:
friend struct base::DefaultSingletonTraits<TimezoneSettingsStubImpl>;
TimezoneSettingsStubImpl();
};
TimezoneSettingsBaseImpl::~TimezoneSettingsBaseImpl() = default;
const icu::TimeZone& TimezoneSettingsBaseImpl::GetTimezone() {
return *timezone_.get();
}
std::u16string TimezoneSettingsBaseImpl::GetCurrentTimezoneID() {
return ash::system::TimezoneSettings::GetTimezoneID(GetTimezone());
}
void TimezoneSettingsBaseImpl::SetTimezoneFromID(
const std::u16string& timezone_id) {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone(
icu::UnicodeString(timezone_id.c_str(), timezone_id.size())));
SetTimezone(*timezone);
}
void TimezoneSettingsBaseImpl::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void TimezoneSettingsBaseImpl::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
const std::vector<std::unique_ptr<icu::TimeZone>>&
TimezoneSettingsBaseImpl::GetTimezoneList() const {
return timezones_;
}
TimezoneSettingsBaseImpl::TimezoneSettingsBaseImpl() {
for (size_t i = 0; i < std::size(kTimeZones); ++i) {
timezones_.push_back(base::WrapUnique(icu::TimeZone::createTimeZone(
icu::UnicodeString(kTimeZones[i], -1, US_INV))));
}
}
const icu::TimeZone* TimezoneSettingsBaseImpl::GetKnownTimezoneOrNull(
const icu::TimeZone& timezone) const {
return ash::system::GetKnownTimezoneOrNull(timezone, timezones_);
}
void TimezoneSettingsImpl::SetTimezone(const icu::TimeZone& timezone) {
// Replace |timezone| by a known timezone with the same rules. If none exists
// go on with |timezone|.
const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone);
if (!known_timezone)
known_timezone = &timezone;
timezone_.reset(known_timezone->clone());
std::string id = base::UTF16ToUTF8(GetTimezoneID(*known_timezone));
VLOG(1) << "Setting timezone to " << id;
// It's safe to change the timezone config files in the background as the
// following operations don't depend on the completion of the config change.
base::ThreadPool::PostTask(FROM_HERE,
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&SetTimezoneIDFromString, id));
icu::TimeZone::setDefault(*known_timezone);
for (auto& observer : observers_)
observer.TimezoneChanged(*known_timezone);
}
// static
TimezoneSettingsImpl* TimezoneSettingsImpl::GetInstance() {
return base::Singleton<
TimezoneSettingsImpl,
base::DefaultSingletonTraits<TimezoneSettingsImpl>>::get();
}
TimezoneSettingsImpl::TimezoneSettingsImpl() {
std::string id = GetTimezoneIDAsString();
if (id.empty()) {
id = kFallbackTimeZoneId;
LOG(ERROR) << "Got an empty string for timezone, default to '" << id;
}
timezone_.reset(
icu::TimeZone::createTimeZone(icu::UnicodeString::fromUTF8(id)));
// Store a known timezone equivalent to id in |timezone_|.
const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(*timezone_);
if (known_timezone != NULL && *known_timezone != *timezone_)
// Not necessary to update the filesystem because |known_timezone| has the
// same rules.
timezone_.reset(known_timezone->clone());
icu::TimeZone::setDefault(*timezone_);
VLOG(1) << "Timezone initially set to " << id;
icu::UnicodeString resolvedId;
std::string resolvedIdStr;
timezone_->getID(resolvedId);
VLOG(1) << "Timezone initially resolved to "
<< resolvedId.toUTF8String(resolvedIdStr);
}
void TimezoneSettingsStubImpl::SetTimezone(const icu::TimeZone& timezone) {
// Replace |timezone| by a known timezone with the same rules. If none exists
// go on with |timezone|.
const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone);
if (!known_timezone)
known_timezone = &timezone;
std::string id = base::UTF16ToUTF8(GetTimezoneID(*known_timezone));
VLOG(1) << "Setting timezone to " << id;
timezone_.reset(known_timezone->clone());
icu::TimeZone::setDefault(*known_timezone);
for (auto& observer : observers_)
observer.TimezoneChanged(*known_timezone);
}
// static
TimezoneSettingsStubImpl* TimezoneSettingsStubImpl::GetInstance() {
return base::Singleton<
TimezoneSettingsStubImpl,
base::DefaultSingletonTraits<TimezoneSettingsStubImpl>>::get();
}
TimezoneSettingsStubImpl::TimezoneSettingsStubImpl() {
timezone_.reset(icu::TimeZone::createDefault());
const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(*timezone_);
if (known_timezone != NULL && *known_timezone != *timezone_)
timezone_.reset(known_timezone->clone());
}
} // namespace
namespace ash {
namespace system {
TimezoneSettings::Observer::~Observer() = default;
// static
TimezoneSettings* TimezoneSettings::GetInstance() {
if (base::SysInfo::IsRunningOnChromeOS()) {
return TimezoneSettingsImpl::GetInstance();
} else {
return TimezoneSettingsStubImpl::GetInstance();
}
}
// static
std::u16string TimezoneSettings::GetTimezoneID(const icu::TimeZone& timezone) {
icu::UnicodeString id;
return base::i18n::UnicodeStringToString16(timezone.getID(id));
}
} // namespace system
} // namespace ash
|