Update V8 to version 4.4.28.
[chromium-blink-merge.git] / chromeos / settings / timezone_settings.cc
blob99539ae0a75235ebdf6ea56b4e3b3128317c2006
1 // Copyright 2013 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.
5 #include "chromeos/settings/timezone_settings.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h"
16 #include "base/observer_list.h"
17 #include "base/stl_util.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/sys_info.h"
21 #include "base/task_runner.h"
22 #include "base/threading/worker_pool.h"
24 namespace {
26 // The filepath to the timezone file that symlinks to the actual timezone file.
27 const char kTimezoneSymlink[] = "/var/lib/timezone/localtime";
28 const char kTimezoneSymlink2[] = "/var/lib/timezone/localtime2";
30 // The directory that contains all the timezone files. So for timezone
31 // "US/Pacific", the actual timezone file is: "/usr/share/zoneinfo/US/Pacific"
32 const char kTimezoneFilesDir[] = "/usr/share/zoneinfo/";
34 // Fallback time zone ID used in case of an unexpected error.
35 const char kFallbackTimeZoneId[] = "America/Los_Angeles";
37 // TODO(jungshik): Using Enumerate method in ICU gives 600+ timezones.
38 // Even after filtering out duplicate entries with a strict identity check,
39 // we still have 400+ zones. Relaxing the criteria for the timezone
40 // identity is likely to cut down the number to < 100. Until we
41 // come up with a better list, we hard-code the following list. It came from
42 // from Android initially, but more entries have been added.
43 static const char* kTimeZones[] = {
44 "Pacific/Midway",
45 "Pacific/Honolulu",
46 "America/Anchorage",
47 "America/Los_Angeles",
48 "America/Vancouver",
49 "America/Tijuana",
50 "America/Phoenix",
51 "America/Chihuahua",
52 "America/Denver",
53 "America/Edmonton",
54 "America/Mazatlan",
55 "America/Regina",
56 "America/Costa_Rica",
57 "America/Chicago",
58 "America/Mexico_City",
59 "America/Winnipeg",
60 "Pacific/Easter",
61 "America/Bogota",
62 "America/Lima",
63 "America/New_York",
64 "America/Toronto",
65 "America/Caracas",
66 "America/Barbados",
67 "America/Halifax",
68 "America/Manaus",
69 "America/Santiago",
70 "America/St_Johns",
71 "America/Araguaina",
72 "America/Argentina/Buenos_Aires",
73 "America/Argentina/San_Luis",
74 "America/Sao_Paulo",
75 "America/Montevideo",
76 "America/Godthab",
77 "Atlantic/South_Georgia",
78 "Atlantic/Cape_Verde",
79 "Atlantic/Azores",
80 "Atlantic/Reykjavik",
81 "Atlantic/St_Helena",
82 "Africa/Casablanca",
83 "Atlantic/Faroe",
84 "Europe/Dublin",
85 "Europe/Lisbon",
86 "Europe/London",
87 "Europe/Amsterdam",
88 "Europe/Belgrade",
89 "Europe/Berlin",
90 "Europe/Bratislava",
91 "Europe/Brussels",
92 "Europe/Budapest",
93 "Europe/Copenhagen",
94 "Europe/Ljubljana",
95 "Europe/Madrid",
96 "Europe/Malta",
97 "Europe/Oslo",
98 "Europe/Paris",
99 "Europe/Prague",
100 "Europe/Rome",
101 "Europe/Stockholm",
102 "Europe/Sarajevo",
103 "Europe/Tirane",
104 "Europe/Vaduz",
105 "Europe/Vienna",
106 "Europe/Warsaw",
107 "Europe/Zagreb",
108 "Europe/Zurich",
109 "Africa/Windhoek",
110 "Africa/Lagos",
111 "Africa/Brazzaville",
112 "Africa/Cairo",
113 "Africa/Harare",
114 "Africa/Maputo",
115 "Africa/Johannesburg",
116 "Europe/Kaliningrad",
117 "Europe/Athens",
118 "Europe/Bucharest",
119 "Europe/Chisinau",
120 "Europe/Helsinki",
121 "Europe/Istanbul",
122 "Europe/Kiev",
123 "Europe/Riga",
124 "Europe/Sofia",
125 "Europe/Tallinn",
126 "Europe/Vilnius",
127 "Asia/Amman",
128 "Asia/Beirut",
129 "Asia/Jerusalem",
130 "Africa/Nairobi",
131 "Asia/Baghdad",
132 "Asia/Riyadh",
133 "Asia/Kuwait",
134 "Europe/Minsk",
135 "Europe/Moscow",
136 "Asia/Tehran",
137 "Europe/Samara",
138 "Asia/Dubai",
139 "Asia/Tbilisi",
140 "Indian/Mauritius",
141 "Asia/Baku",
142 "Asia/Yerevan",
143 "Asia/Kabul",
144 "Asia/Karachi",
145 "Asia/Ashgabat",
146 "Asia/Oral",
147 "Asia/Yekaterinburg",
148 "Asia/Calcutta",
149 "Asia/Colombo",
150 "Asia/Katmandu",
151 "Asia/Omsk",
152 "Asia/Almaty",
153 "Asia/Dhaka",
154 "Asia/Novosibirsk",
155 "Asia/Rangoon",
156 "Asia/Bangkok",
157 "Asia/Jakarta",
158 "Asia/Krasnoyarsk",
159 "Asia/Novokuznetsk",
160 "Asia/Ho_Chi_Minh",
161 "Asia/Phnom_Penh",
162 "Asia/Vientiane",
163 "Asia/Shanghai",
164 "Asia/Hong_Kong",
165 "Asia/Kuala_Lumpur",
166 "Asia/Singapore",
167 "Asia/Manila",
168 "Asia/Taipei",
169 "Asia/Makassar",
170 "Asia/Irkutsk",
171 "Asia/Yakutsk",
172 "Australia/Perth",
173 "Australia/Eucla",
174 "Asia/Seoul",
175 "Asia/Tokyo",
176 "Asia/Jayapura",
177 "Asia/Sakhalin",
178 "Asia/Vladivostok",
179 "Asia/Magadan",
180 "Australia/Darwin",
181 "Australia/Adelaide",
182 "Pacific/Guam",
183 "Australia/Brisbane",
184 "Australia/Hobart",
185 "Australia/Sydney",
186 "Asia/Anadyr",
187 "Pacific/Port_Moresby",
188 "Asia/Kamchatka",
189 "Pacific/Fiji",
190 "Pacific/Majuro",
191 "Pacific/Auckland",
192 "Pacific/Tongatapu",
193 "Pacific/Apia",
194 "Pacific/Kiritimati",
197 std::string GetTimezoneIDAsString() {
198 // Compare with chromiumos/src/platform/init/ui.conf which fixes certain
199 // incorrect states of the timezone symlink on startup. Thus errors occuring
200 // here should be rather contrived.
202 // Look at kTimezoneSymlink, see which timezone we are symlinked to.
203 char buf[256];
204 const ssize_t len = readlink(kTimezoneSymlink, buf,
205 sizeof(buf)-1);
206 if (len == -1) {
207 LOG(ERROR) << "GetTimezoneID: Cannot read timezone symlink "
208 << kTimezoneSymlink;
209 return std::string();
212 std::string timezone(buf, len);
213 // Remove kTimezoneFilesDir from the beginning.
214 if (timezone.find(kTimezoneFilesDir) != 0) {
215 LOG(ERROR) << "GetTimezoneID: Timezone symlink is wrong "
216 << timezone;
217 return std::string();
220 return timezone.substr(strlen(kTimezoneFilesDir));
223 void SetTimezoneIDFromString(const std::string& id) {
224 // Change the kTimezoneSymlink symlink to the path for this timezone.
225 // We want to do this in an atomic way. So we are going to create the symlink
226 // at kTimezoneSymlink2 and then move it to kTimezoneSymlink
228 base::FilePath timezone_symlink(kTimezoneSymlink);
229 base::FilePath timezone_symlink2(kTimezoneSymlink2);
230 base::FilePath timezone_file(kTimezoneFilesDir + id);
232 // Make sure timezone_file exists.
233 if (!base::PathExists(timezone_file)) {
234 LOG(ERROR) << "SetTimezoneID: Cannot find timezone file "
235 << timezone_file.value();
236 return;
239 // Delete old symlink2 if it exists.
240 base::DeleteFile(timezone_symlink2, false);
242 // Create new symlink2.
243 if (symlink(timezone_file.value().c_str(),
244 timezone_symlink2.value().c_str()) == -1) {
245 LOG(ERROR) << "SetTimezoneID: Unable to create symlink "
246 << timezone_symlink2.value() << " to " << timezone_file.value();
247 return;
250 // Move symlink2 to symlink.
251 if (!base::ReplaceFile(timezone_symlink2, timezone_symlink, NULL)) {
252 LOG(ERROR) << "SetTimezoneID: Unable to move symlink "
253 << timezone_symlink2.value() << " to "
254 << timezone_symlink.value();
258 // Common code of the TimezoneSettings implementations.
259 class TimezoneSettingsBaseImpl : public chromeos::system::TimezoneSettings {
260 public:
261 ~TimezoneSettingsBaseImpl() override;
263 // TimezoneSettings implementation:
264 const icu::TimeZone& GetTimezone() override;
265 base::string16 GetCurrentTimezoneID() override;
266 void SetTimezoneFromID(const base::string16& timezone_id) override;
267 void AddObserver(Observer* observer) override;
268 void RemoveObserver(Observer* observer) override;
269 const std::vector<icu::TimeZone*>& GetTimezoneList() const override;
271 protected:
272 TimezoneSettingsBaseImpl();
274 // Returns |timezone| if it is an element of |timezones_|.
275 // Otherwise, returns a timezone from |timezones_|, if such exists, that has
276 // the same rule as the given |timezone|.
277 // Otherwise, returns NULL.
278 // Note multiple timezones with the same time zone offset may exist
279 // e.g.
280 // US/Pacific == America/Los_Angeles
281 const icu::TimeZone* GetKnownTimezoneOrNull(
282 const icu::TimeZone& timezone) const;
284 ObserverList<Observer> observers_;
285 std::vector<icu::TimeZone*> timezones_;
286 scoped_ptr<icu::TimeZone> timezone_;
288 private:
289 DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsBaseImpl);
292 // The TimezoneSettings implementation used in production.
293 class TimezoneSettingsImpl : public TimezoneSettingsBaseImpl {
294 public:
295 // TimezoneSettings implementation:
296 void SetTimezone(const icu::TimeZone& timezone) override;
298 static TimezoneSettingsImpl* GetInstance();
300 private:
301 friend struct DefaultSingletonTraits<TimezoneSettingsImpl>;
303 TimezoneSettingsImpl();
305 DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsImpl);
308 // The stub TimezoneSettings implementation used on Linux desktop.
309 class TimezoneSettingsStubImpl : public TimezoneSettingsBaseImpl {
310 public:
311 // TimezoneSettings implementation:
312 void SetTimezone(const icu::TimeZone& timezone) override;
314 static TimezoneSettingsStubImpl* GetInstance();
316 private:
317 friend struct DefaultSingletonTraits<TimezoneSettingsStubImpl>;
319 TimezoneSettingsStubImpl();
321 DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsStubImpl);
324 TimezoneSettingsBaseImpl::~TimezoneSettingsBaseImpl() {
325 STLDeleteElements(&timezones_);
328 const icu::TimeZone& TimezoneSettingsBaseImpl::GetTimezone() {
329 return *timezone_.get();
332 base::string16 TimezoneSettingsBaseImpl::GetCurrentTimezoneID() {
333 return chromeos::system::TimezoneSettings::GetTimezoneID(GetTimezone());
336 void TimezoneSettingsBaseImpl::SetTimezoneFromID(
337 const base::string16& timezone_id) {
338 scoped_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone(
339 icu::UnicodeString(timezone_id.c_str(), timezone_id.size())));
340 SetTimezone(*timezone);
343 void TimezoneSettingsBaseImpl::AddObserver(Observer* observer) {
344 observers_.AddObserver(observer);
347 void TimezoneSettingsBaseImpl::RemoveObserver(Observer* observer) {
348 observers_.RemoveObserver(observer);
351 const std::vector<icu::TimeZone*>&
352 TimezoneSettingsBaseImpl::GetTimezoneList() const {
353 return timezones_;
356 TimezoneSettingsBaseImpl::TimezoneSettingsBaseImpl() {
357 for (size_t i = 0; i < arraysize(kTimeZones); ++i) {
358 timezones_.push_back(icu::TimeZone::createTimeZone(
359 icu::UnicodeString(kTimeZones[i], -1, US_INV)));
363 const icu::TimeZone* TimezoneSettingsBaseImpl::GetKnownTimezoneOrNull(
364 const icu::TimeZone& timezone) const {
365 const icu::TimeZone* known_timezone = NULL;
366 for (std::vector<icu::TimeZone*>::const_iterator iter = timezones_.begin();
367 iter != timezones_.end(); ++iter) {
368 const icu::TimeZone* entry = *iter;
369 if (*entry == timezone)
370 return entry;
371 if (entry->hasSameRules(timezone))
372 known_timezone = entry;
375 // May return NULL if we did not find a matching timezone in our list.
376 return known_timezone;
379 void TimezoneSettingsImpl::SetTimezone(const icu::TimeZone& timezone) {
380 // Replace |timezone| by a known timezone with the same rules. If none exists
381 // go on with |timezone|.
382 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone);
383 if (!known_timezone)
384 known_timezone = &timezone;
386 timezone_.reset(known_timezone->clone());
387 std::string id = base::UTF16ToUTF8(GetTimezoneID(*known_timezone));
388 VLOG(1) << "Setting timezone to " << id;
389 // It's safe to change the timezone config files in the background as the
390 // following operations don't depend on the completion of the config change.
391 base::WorkerPool::GetTaskRunner(true /* task is slow */)->
392 PostTask(FROM_HERE, base::Bind(&SetTimezoneIDFromString, id));
393 icu::TimeZone::setDefault(*known_timezone);
394 FOR_EACH_OBSERVER(Observer, observers_, TimezoneChanged(*known_timezone));
397 // static
398 TimezoneSettingsImpl* TimezoneSettingsImpl::GetInstance() {
399 return Singleton<TimezoneSettingsImpl,
400 DefaultSingletonTraits<TimezoneSettingsImpl> >::get();
403 TimezoneSettingsImpl::TimezoneSettingsImpl() {
404 std::string id = GetTimezoneIDAsString();
405 if (id.empty()) {
406 id = kFallbackTimeZoneId;
407 LOG(ERROR) << "Got an empty string for timezone, default to '" << id;
410 timezone_.reset(icu::TimeZone::createTimeZone(
411 icu::UnicodeString::fromUTF8(id)));
413 // Store a known timezone equivalent to id in |timezone_|.
414 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(*timezone_);
415 if (known_timezone != NULL && *known_timezone != *timezone_)
416 // Not necessary to update the filesystem because |known_timezone| has the
417 // same rules.
418 timezone_.reset(known_timezone->clone());
420 icu::TimeZone::setDefault(*timezone_);
421 VLOG(1) << "Timezone initially set to " << id;
424 void TimezoneSettingsStubImpl::SetTimezone(const icu::TimeZone& timezone) {
425 // Replace |timezone| by a known timezone with the same rules. If none exists
426 // go on with |timezone|.
427 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone);
428 if (!known_timezone)
429 known_timezone = &timezone;
431 timezone_.reset(known_timezone->clone());
432 icu::TimeZone::setDefault(*known_timezone);
433 FOR_EACH_OBSERVER(Observer, observers_, TimezoneChanged(*known_timezone));
436 // static
437 TimezoneSettingsStubImpl* TimezoneSettingsStubImpl::GetInstance() {
438 return Singleton<TimezoneSettingsStubImpl,
439 DefaultSingletonTraits<TimezoneSettingsStubImpl> >::get();
442 TimezoneSettingsStubImpl::TimezoneSettingsStubImpl() {
443 timezone_.reset(icu::TimeZone::createDefault());
444 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(*timezone_);
445 if (known_timezone != NULL && *known_timezone != *timezone_)
446 timezone_.reset(known_timezone->clone());
449 } // namespace
451 namespace chromeos {
452 namespace system {
454 TimezoneSettings::Observer::~Observer() {}
456 // static
457 TimezoneSettings* TimezoneSettings::GetInstance() {
458 if (base::SysInfo::IsRunningOnChromeOS()) {
459 return TimezoneSettingsImpl::GetInstance();
460 } else {
461 return TimezoneSettingsStubImpl::GetInstance();
465 // static
466 base::string16 TimezoneSettings::GetTimezoneID(const icu::TimeZone& timezone) {
467 icu::UnicodeString id;
468 timezone.getID(id);
469 return base::string16(id.getBuffer(), id.length());
472 } // namespace system
473 } // namespace chromeos