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/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"
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
[] = {
47 "America/Los_Angeles",
58 "America/Mexico_City",
72 "America/Argentina/Buenos_Aires",
73 "America/Argentina/San_Luis",
77 "Atlantic/South_Georgia",
78 "Atlantic/Cape_Verde",
111 "Africa/Brazzaville",
115 "Africa/Johannesburg",
116 "Europe/Kaliningrad",
147 "Asia/Yekaterinburg",
181 "Australia/Adelaide",
183 "Australia/Brisbane",
187 "Pacific/Port_Moresby",
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.
204 const ssize_t len
= readlink(kTimezoneSymlink
, buf
,
207 LOG(ERROR
) << "GetTimezoneID: Cannot read timezone symlink "
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 "
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();
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();
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
{
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
;
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
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_
;
289 DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsBaseImpl
);
292 // The TimezoneSettings implementation used in production.
293 class TimezoneSettingsImpl
: public TimezoneSettingsBaseImpl
{
295 // TimezoneSettings implementation:
296 void SetTimezone(const icu::TimeZone
& timezone
) override
;
298 static TimezoneSettingsImpl
* GetInstance();
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
{
311 // TimezoneSettings implementation:
312 void SetTimezone(const icu::TimeZone
& timezone
) override
;
314 static TimezoneSettingsStubImpl
* GetInstance();
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 {
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
)
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
);
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
));
398 TimezoneSettingsImpl
* TimezoneSettingsImpl::GetInstance() {
399 return Singleton
<TimezoneSettingsImpl
,
400 DefaultSingletonTraits
<TimezoneSettingsImpl
> >::get();
403 TimezoneSettingsImpl::TimezoneSettingsImpl() {
404 std::string id
= GetTimezoneIDAsString();
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
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
);
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
));
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());
454 TimezoneSettings::Observer::~Observer() {}
457 TimezoneSettings
* TimezoneSettings::GetInstance() {
458 if (base::SysInfo::IsRunningOnChromeOS()) {
459 return TimezoneSettingsImpl::GetInstance();
461 return TimezoneSettingsStubImpl::GetInstance();
466 base::string16
TimezoneSettings::GetTimezoneID(const icu::TimeZone
& timezone
) {
467 icu::UnicodeString id
;
469 return base::string16(id
.getBuffer(), id
.length());
472 } // namespace system
473 } // namespace chromeos