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"
10 #include "base/file_util.h"
11 #include "base/files/file_path.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"
23 #include "third_party/icu/source/i18n/unicode/timezone.h"
27 // The filepath to the timezone file that symlinks to the actual timezone file.
28 const char kTimezoneSymlink
[] = "/var/lib/timezone/localtime";
29 const char kTimezoneSymlink2
[] = "/var/lib/timezone/localtime2";
31 // The directory that contains all the timezone files. So for timezone
32 // "US/Pacific", the actual timezone file is: "/usr/share/zoneinfo/US/Pacific"
33 const char kTimezoneFilesDir
[] = "/usr/share/zoneinfo/";
35 // Fallback time zone ID used in case of an unexpected error.
36 const char kFallbackTimeZoneId
[] = "America/Los_Angeles";
38 // TODO(jungshik): Using Enumerate method in ICU gives 600+ timezones.
39 // Even after filtering out duplicate entries with a strict identity check,
40 // we still have 400+ zones. Relaxing the criteria for the timezone
41 // identity is likely to cut down the number to < 100. Until we
42 // come up with a better list, we hard-code the following list. It came from
43 // from Android initially, but more entries have been added.
44 static const char* kTimeZones
[] = {
48 "America/Los_Angeles",
59 "America/Mexico_City",
73 "America/Argentina/Buenos_Aires",
74 "America/Argentina/San_Luis",
78 "Atlantic/South_Georgia",
79 "Atlantic/Cape_Verde",
108 "Africa/Brazzaville",
112 "Africa/Johannesburg",
145 "Asia/Yekaterinburg",
171 "Australia/Adelaide",
174 "Australia/Brisbane",
177 "Pacific/Port_Moresby",
186 "Pacific/Kiritimati",
189 std::string
GetTimezoneIDAsString() {
190 // Compare with chromiumos/src/platform/init/ui.conf which fixes certain
191 // incorrect states of the timezone symlink on startup. Thus errors occuring
192 // here should be rather contrived.
194 // Look at kTimezoneSymlink, see which timezone we are symlinked to.
196 const ssize_t len
= readlink(kTimezoneSymlink
, buf
,
199 LOG(ERROR
) << "GetTimezoneID: Cannot read timezone symlink "
201 return std::string();
204 std::string
timezone(buf
, len
);
205 // Remove kTimezoneFilesDir from the beginning.
206 if (timezone
.find(kTimezoneFilesDir
) != 0) {
207 LOG(ERROR
) << "GetTimezoneID: Timezone symlink is wrong "
209 return std::string();
212 return timezone
.substr(strlen(kTimezoneFilesDir
));
215 void SetTimezoneIDFromString(const std::string
& id
) {
216 // Change the kTimezoneSymlink symlink to the path for this timezone.
217 // We want to do this in an atomic way. So we are going to create the symlink
218 // at kTimezoneSymlink2 and then move it to kTimezoneSymlink
220 base::FilePath
timezone_symlink(kTimezoneSymlink
);
221 base::FilePath
timezone_symlink2(kTimezoneSymlink2
);
222 base::FilePath
timezone_file(kTimezoneFilesDir
+ id
);
224 // Make sure timezone_file exists.
225 if (!base::PathExists(timezone_file
)) {
226 LOG(ERROR
) << "SetTimezoneID: Cannot find timezone file "
227 << timezone_file
.value();
231 // Delete old symlink2 if it exists.
232 base::DeleteFile(timezone_symlink2
, false);
234 // Create new symlink2.
235 if (symlink(timezone_file
.value().c_str(),
236 timezone_symlink2
.value().c_str()) == -1) {
237 LOG(ERROR
) << "SetTimezoneID: Unable to create symlink "
238 << timezone_symlink2
.value() << " to " << timezone_file
.value();
242 // Move symlink2 to symlink.
243 if (!base::ReplaceFile(timezone_symlink2
, timezone_symlink
, NULL
)) {
244 LOG(ERROR
) << "SetTimezoneID: Unable to move symlink "
245 << timezone_symlink2
.value() << " to "
246 << timezone_symlink
.value();
250 // Common code of the TimezoneSettings implementations.
251 class TimezoneSettingsBaseImpl
: public chromeos::system::TimezoneSettings
{
253 virtual ~TimezoneSettingsBaseImpl();
255 // TimezoneSettings implementation:
256 virtual const icu::TimeZone
& GetTimezone() OVERRIDE
;
257 virtual base::string16
GetCurrentTimezoneID() OVERRIDE
;
258 virtual void SetTimezoneFromID(const base::string16
& timezone_id
) OVERRIDE
;
259 virtual void AddObserver(Observer
* observer
) OVERRIDE
;
260 virtual void RemoveObserver(Observer
* observer
) OVERRIDE
;
261 virtual const std::vector
<icu::TimeZone
*>& GetTimezoneList() const OVERRIDE
;
264 TimezoneSettingsBaseImpl();
266 // Returns |timezone| if it is an element of |timezones_|.
267 // Otherwise, returns a timezone from |timezones_|, if such exists, that has
268 // the same rule as the given |timezone|.
269 // Otherwise, returns NULL.
270 // Note multiple timezones with the same time zone offset may exist
272 // US/Pacific == America/Los_Angeles
273 const icu::TimeZone
* GetKnownTimezoneOrNull(
274 const icu::TimeZone
& timezone
) const;
276 ObserverList
<Observer
> observers_
;
277 std::vector
<icu::TimeZone
*> timezones_
;
278 scoped_ptr
<icu::TimeZone
> timezone_
;
281 DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsBaseImpl
);
284 // The TimezoneSettings implementation used in production.
285 class TimezoneSettingsImpl
: public TimezoneSettingsBaseImpl
{
287 // TimezoneSettings implementation:
288 virtual void SetTimezone(const icu::TimeZone
& timezone
) OVERRIDE
;
290 static TimezoneSettingsImpl
* GetInstance();
293 friend struct DefaultSingletonTraits
<TimezoneSettingsImpl
>;
295 TimezoneSettingsImpl();
297 DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsImpl
);
300 // The stub TimezoneSettings implementation used on Linux desktop.
301 class TimezoneSettingsStubImpl
: public TimezoneSettingsBaseImpl
{
303 // TimezoneSettings implementation:
304 virtual void SetTimezone(const icu::TimeZone
& timezone
) OVERRIDE
;
306 static TimezoneSettingsStubImpl
* GetInstance();
309 friend struct DefaultSingletonTraits
<TimezoneSettingsStubImpl
>;
311 TimezoneSettingsStubImpl();
313 DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsStubImpl
);
316 TimezoneSettingsBaseImpl::~TimezoneSettingsBaseImpl() {
317 STLDeleteElements(&timezones_
);
320 const icu::TimeZone
& TimezoneSettingsBaseImpl::GetTimezone() {
321 return *timezone_
.get();
324 base::string16
TimezoneSettingsBaseImpl::GetCurrentTimezoneID() {
325 return chromeos::system::TimezoneSettings::GetTimezoneID(GetTimezone());
328 void TimezoneSettingsBaseImpl::SetTimezoneFromID(
329 const base::string16
& timezone_id
) {
330 scoped_ptr
<icu::TimeZone
> timezone(icu::TimeZone::createTimeZone(
331 icu::UnicodeString(timezone_id
.c_str(), timezone_id
.size())));
332 SetTimezone(*timezone
);
335 void TimezoneSettingsBaseImpl::AddObserver(Observer
* observer
) {
336 observers_
.AddObserver(observer
);
339 void TimezoneSettingsBaseImpl::RemoveObserver(Observer
* observer
) {
340 observers_
.RemoveObserver(observer
);
343 const std::vector
<icu::TimeZone
*>&
344 TimezoneSettingsBaseImpl::GetTimezoneList() const {
348 TimezoneSettingsBaseImpl::TimezoneSettingsBaseImpl() {
349 for (size_t i
= 0; i
< arraysize(kTimeZones
); ++i
) {
350 timezones_
.push_back(icu::TimeZone::createTimeZone(
351 icu::UnicodeString(kTimeZones
[i
], -1, US_INV
)));
355 const icu::TimeZone
* TimezoneSettingsBaseImpl::GetKnownTimezoneOrNull(
356 const icu::TimeZone
& timezone
) const {
357 const icu::TimeZone
* known_timezone
= NULL
;
358 for (std::vector
<icu::TimeZone
*>::const_iterator iter
= timezones_
.begin();
359 iter
!= timezones_
.end(); ++iter
) {
360 const icu::TimeZone
* entry
= *iter
;
361 if (*entry
== timezone
)
363 if (entry
->hasSameRules(timezone
))
364 known_timezone
= entry
;
367 // May return NULL if we did not find a matching timezone in our list.
368 return known_timezone
;
371 void TimezoneSettingsImpl::SetTimezone(const icu::TimeZone
& timezone
) {
372 // Replace |timezone| by a known timezone with the same rules. If none exists
373 // go on with |timezone|.
374 const icu::TimeZone
* known_timezone
= GetKnownTimezoneOrNull(timezone
);
376 known_timezone
= &timezone
;
378 timezone_
.reset(known_timezone
->clone());
379 std::string id
= base::UTF16ToUTF8(GetTimezoneID(*known_timezone
));
380 VLOG(1) << "Setting timezone to " << id
;
381 // It's safe to change the timezone config files in the background as the
382 // following operations don't depend on the completion of the config change.
383 base::WorkerPool::GetTaskRunner(true /* task is slow */)->
384 PostTask(FROM_HERE
, base::Bind(&SetTimezoneIDFromString
, id
));
385 icu::TimeZone::setDefault(*known_timezone
);
386 FOR_EACH_OBSERVER(Observer
, observers_
, TimezoneChanged(*known_timezone
));
390 TimezoneSettingsImpl
* TimezoneSettingsImpl::GetInstance() {
391 return Singleton
<TimezoneSettingsImpl
,
392 DefaultSingletonTraits
<TimezoneSettingsImpl
> >::get();
395 TimezoneSettingsImpl::TimezoneSettingsImpl() {
396 std::string id
= GetTimezoneIDAsString();
398 id
= kFallbackTimeZoneId
;
399 LOG(ERROR
) << "Got an empty string for timezone, default to '" << id
;
402 timezone_
.reset(icu::TimeZone::createTimeZone(
403 icu::UnicodeString::fromUTF8(id
)));
405 // Store a known timezone equivalent to id in |timezone_|.
406 const icu::TimeZone
* known_timezone
= GetKnownTimezoneOrNull(*timezone_
);
407 if (known_timezone
!= NULL
&& *known_timezone
!= *timezone_
)
408 // Not necessary to update the filesystem because |known_timezone| has the
410 timezone_
.reset(known_timezone
->clone());
412 icu::TimeZone::setDefault(*timezone_
);
413 VLOG(1) << "Timezone initially set to " << id
;
416 void TimezoneSettingsStubImpl::SetTimezone(const icu::TimeZone
& timezone
) {
417 // Replace |timezone| by a known timezone with the same rules. If none exists
418 // go on with |timezone|.
419 const icu::TimeZone
* known_timezone
= GetKnownTimezoneOrNull(timezone
);
421 known_timezone
= &timezone
;
423 timezone_
.reset(known_timezone
->clone());
424 icu::TimeZone::setDefault(*known_timezone
);
425 FOR_EACH_OBSERVER(Observer
, observers_
, TimezoneChanged(*known_timezone
));
429 TimezoneSettingsStubImpl
* TimezoneSettingsStubImpl::GetInstance() {
430 return Singleton
<TimezoneSettingsStubImpl
,
431 DefaultSingletonTraits
<TimezoneSettingsStubImpl
> >::get();
434 TimezoneSettingsStubImpl::TimezoneSettingsStubImpl() {
435 timezone_
.reset(icu::TimeZone::createDefault());
436 const icu::TimeZone
* known_timezone
= GetKnownTimezoneOrNull(*timezone_
);
437 if (known_timezone
!= NULL
&& *known_timezone
!= *timezone_
)
438 timezone_
.reset(known_timezone
->clone());
446 TimezoneSettings::Observer::~Observer() {}
449 TimezoneSettings
* TimezoneSettings::GetInstance() {
450 if (base::SysInfo::IsRunningOnChromeOS()) {
451 return TimezoneSettingsImpl::GetInstance();
453 return TimezoneSettingsStubImpl::GetInstance();
458 base::string16
TimezoneSettings::GetTimezoneID(const icu::TimeZone
& timezone
) {
459 icu::UnicodeString id
;
461 return base::string16(id
.getBuffer(), id
.length());
464 } // namespace system
465 } // namespace chromeos