| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/settings/timezone_settings.h" | 5 #include "chromeos/settings/timezone_settings.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
| 16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/sys_info.h" | 20 #include "base/sys_info.h" |
| 21 #include "base/task_runner.h" | 21 #include "base/task_runner.h" |
| 22 #include "base/threading/worker_pool.h" | 22 #include "base/threading/worker_pool.h" |
| 23 #include "chromeos/settings/timezone_settings_helper.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // The filepath to the timezone file that symlinks to the actual timezone file. | 27 // The filepath to the timezone file that symlinks to the actual timezone file. |
| 27 const char kTimezoneSymlink[] = "/var/lib/timezone/localtime"; | 28 const char kTimezoneSymlink[] = "/var/lib/timezone/localtime"; |
| 28 const char kTimezoneSymlink2[] = "/var/lib/timezone/localtime2"; | 29 const char kTimezoneSymlink2[] = "/var/lib/timezone/localtime2"; |
| 29 | 30 |
| 30 // The directory that contains all the timezone files. So for timezone | 31 // 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 // "US/Pacific", the actual timezone file is: "/usr/share/zoneinfo/US/Pacific" |
| 32 const char kTimezoneFilesDir[] = "/usr/share/zoneinfo/"; | 33 const char kTimezoneFilesDir[] = "/usr/share/zoneinfo/"; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 "Asia/Novokuznetsk", | 160 "Asia/Novokuznetsk", |
| 160 "Asia/Ho_Chi_Minh", | 161 "Asia/Ho_Chi_Minh", |
| 161 "Asia/Phnom_Penh", | 162 "Asia/Phnom_Penh", |
| 162 "Asia/Vientiane", | 163 "Asia/Vientiane", |
| 163 "Asia/Shanghai", | 164 "Asia/Shanghai", |
| 164 "Asia/Hong_Kong", | 165 "Asia/Hong_Kong", |
| 165 "Asia/Kuala_Lumpur", | 166 "Asia/Kuala_Lumpur", |
| 166 "Asia/Singapore", | 167 "Asia/Singapore", |
| 167 "Asia/Manila", | 168 "Asia/Manila", |
| 168 "Asia/Taipei", | 169 "Asia/Taipei", |
| 170 "Asia/Ulaanbaatar", |
| 169 "Asia/Makassar", | 171 "Asia/Makassar", |
| 170 "Asia/Irkutsk", | 172 "Asia/Irkutsk", |
| 171 "Asia/Yakutsk", | 173 "Asia/Yakutsk", |
| 172 "Australia/Perth", | 174 "Australia/Perth", |
| 173 "Australia/Eucla", | 175 "Australia/Eucla", |
| 174 "Asia/Seoul", | 176 "Asia/Seoul", |
| 175 "Asia/Tokyo", | 177 "Asia/Tokyo", |
| 176 "Asia/Jayapura", | 178 "Asia/Jayapura", |
| 177 "Asia/Sakhalin", | 179 "Asia/Sakhalin", |
| 178 "Asia/Vladivostok", | 180 "Asia/Vladivostok", |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 void RemoveObserver(Observer* observer) override; | 270 void RemoveObserver(Observer* observer) override; |
| 269 const std::vector<icu::TimeZone*>& GetTimezoneList() const override; | 271 const std::vector<icu::TimeZone*>& GetTimezoneList() const override; |
| 270 | 272 |
| 271 protected: | 273 protected: |
| 272 TimezoneSettingsBaseImpl(); | 274 TimezoneSettingsBaseImpl(); |
| 273 | 275 |
| 274 // Returns |timezone| if it is an element of |timezones_|. | 276 // Returns |timezone| if it is an element of |timezones_|. |
| 275 // Otherwise, returns a timezone from |timezones_|, if such exists, that has | 277 // Otherwise, returns a timezone from |timezones_|, if such exists, that has |
| 276 // the same rule as the given |timezone|. | 278 // the same rule as the given |timezone|. |
| 277 // Otherwise, returns NULL. | 279 // Otherwise, returns NULL. |
| 278 // Note multiple timezones with the same time zone offset may exist | 280 // Note multiple timezones with the same time zone rules may exist |
| 279 // e.g. | 281 // e.g. |
| 280 // US/Pacific == America/Los_Angeles | 282 // US/Pacific == America/Los_Angeles |
| 281 const icu::TimeZone* GetKnownTimezoneOrNull( | 283 const icu::TimeZone* GetKnownTimezoneOrNull( |
| 282 const icu::TimeZone& timezone) const; | 284 const icu::TimeZone& timezone) const; |
| 283 | 285 |
| 284 ObserverList<Observer> observers_; | 286 ObserverList<Observer> observers_; |
| 285 std::vector<icu::TimeZone*> timezones_; | 287 std::vector<icu::TimeZone*> timezones_; |
| 286 scoped_ptr<icu::TimeZone> timezone_; | 288 scoped_ptr<icu::TimeZone> timezone_; |
| 287 | 289 |
| 288 private: | 290 private: |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 | 357 |
| 356 TimezoneSettingsBaseImpl::TimezoneSettingsBaseImpl() { | 358 TimezoneSettingsBaseImpl::TimezoneSettingsBaseImpl() { |
| 357 for (size_t i = 0; i < arraysize(kTimeZones); ++i) { | 359 for (size_t i = 0; i < arraysize(kTimeZones); ++i) { |
| 358 timezones_.push_back(icu::TimeZone::createTimeZone( | 360 timezones_.push_back(icu::TimeZone::createTimeZone( |
| 359 icu::UnicodeString(kTimeZones[i], -1, US_INV))); | 361 icu::UnicodeString(kTimeZones[i], -1, US_INV))); |
| 360 } | 362 } |
| 361 } | 363 } |
| 362 | 364 |
| 363 const icu::TimeZone* TimezoneSettingsBaseImpl::GetKnownTimezoneOrNull( | 365 const icu::TimeZone* TimezoneSettingsBaseImpl::GetKnownTimezoneOrNull( |
| 364 const icu::TimeZone& timezone) const { | 366 const icu::TimeZone& timezone) const { |
| 365 const icu::TimeZone* known_timezone = NULL; | 367 return chromeos::system::GetKnownTimezoneOrNull(timezone, timezones_); |
| 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; | |
| 373 } | |
| 374 | |
| 375 // May return NULL if we did not find a matching timezone in our list. | |
| 376 return known_timezone; | |
| 377 } | 368 } |
| 378 | 369 |
| 379 void TimezoneSettingsImpl::SetTimezone(const icu::TimeZone& timezone) { | 370 void TimezoneSettingsImpl::SetTimezone(const icu::TimeZone& timezone) { |
| 380 // Replace |timezone| by a known timezone with the same rules. If none exists | 371 // Replace |timezone| by a known timezone with the same rules. If none exists |
| 381 // go on with |timezone|. | 372 // go on with |timezone|. |
| 382 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone); | 373 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone); |
| 383 if (!known_timezone) | 374 if (!known_timezone) |
| 384 known_timezone = &timezone; | 375 known_timezone = &timezone; |
| 385 | 376 |
| 386 timezone_.reset(known_timezone->clone()); | 377 timezone_.reset(known_timezone->clone()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 412 | 403 |
| 413 // Store a known timezone equivalent to id in |timezone_|. | 404 // Store a known timezone equivalent to id in |timezone_|. |
| 414 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(*timezone_); | 405 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(*timezone_); |
| 415 if (known_timezone != NULL && *known_timezone != *timezone_) | 406 if (known_timezone != NULL && *known_timezone != *timezone_) |
| 416 // Not necessary to update the filesystem because |known_timezone| has the | 407 // Not necessary to update the filesystem because |known_timezone| has the |
| 417 // same rules. | 408 // same rules. |
| 418 timezone_.reset(known_timezone->clone()); | 409 timezone_.reset(known_timezone->clone()); |
| 419 | 410 |
| 420 icu::TimeZone::setDefault(*timezone_); | 411 icu::TimeZone::setDefault(*timezone_); |
| 421 VLOG(1) << "Timezone initially set to " << id; | 412 VLOG(1) << "Timezone initially set to " << id; |
| 413 icu::UnicodeString resolvedId; |
| 414 std::string resolvedIdStr; |
| 415 timezone_->getID(resolvedId); |
| 416 VLOG(1) << "Timezone initially resolved to " |
| 417 << resolvedId.toUTF8String(resolvedIdStr); |
| 422 } | 418 } |
| 423 | 419 |
| 424 void TimezoneSettingsStubImpl::SetTimezone(const icu::TimeZone& timezone) { | 420 void TimezoneSettingsStubImpl::SetTimezone(const icu::TimeZone& timezone) { |
| 425 // Replace |timezone| by a known timezone with the same rules. If none exists | 421 // Replace |timezone| by a known timezone with the same rules. If none exists |
| 426 // go on with |timezone|. | 422 // go on with |timezone|. |
| 427 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone); | 423 const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone); |
| 428 if (!known_timezone) | 424 if (!known_timezone) |
| 429 known_timezone = &timezone; | 425 known_timezone = &timezone; |
| 430 | 426 |
| 427 std::string id = base::UTF16ToUTF8(GetTimezoneID(*known_timezone)); |
| 428 VLOG(1) << "Setting timezone to " << id; |
| 431 timezone_.reset(known_timezone->clone()); | 429 timezone_.reset(known_timezone->clone()); |
| 432 icu::TimeZone::setDefault(*known_timezone); | 430 icu::TimeZone::setDefault(*known_timezone); |
| 433 FOR_EACH_OBSERVER(Observer, observers_, TimezoneChanged(*known_timezone)); | 431 FOR_EACH_OBSERVER(Observer, observers_, TimezoneChanged(*known_timezone)); |
| 434 } | 432 } |
| 435 | 433 |
| 436 // static | 434 // static |
| 437 TimezoneSettingsStubImpl* TimezoneSettingsStubImpl::GetInstance() { | 435 TimezoneSettingsStubImpl* TimezoneSettingsStubImpl::GetInstance() { |
| 438 return Singleton<TimezoneSettingsStubImpl, | 436 return Singleton<TimezoneSettingsStubImpl, |
| 439 DefaultSingletonTraits<TimezoneSettingsStubImpl> >::get(); | 437 DefaultSingletonTraits<TimezoneSettingsStubImpl> >::get(); |
| 440 } | 438 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 464 | 462 |
| 465 // static | 463 // static |
| 466 base::string16 TimezoneSettings::GetTimezoneID(const icu::TimeZone& timezone) { | 464 base::string16 TimezoneSettings::GetTimezoneID(const icu::TimeZone& timezone) { |
| 467 icu::UnicodeString id; | 465 icu::UnicodeString id; |
| 468 timezone.getID(id); | 466 timezone.getID(id); |
| 469 return base::string16(id.getBuffer(), id.length()); | 467 return base::string16(id.getBuffer(), id.length()); |
| 470 } | 468 } |
| 471 | 469 |
| 472 } // namespace system | 470 } // namespace system |
| 473 } // namespace chromeos | 471 } // namespace chromeos |
| OLD | NEW |