[email protected] | 8b03c07 | 2014-04-30 15:49:25 | [diff] [blame] | 1 | // Copyright 2014 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. |
| 4 | |
| 5 | #include "content/browser/time_zone_monitor.h" |
| 6 | |
| 7 | #include "base/logging.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 8 | #include "build/build_config.h" |
[email protected] | 8b03c07 | 2014-04-30 15:49:25 | [diff] [blame] | 9 | #include "content/public/browser/browser_thread.h" |
| 10 | #include "content/public/browser/render_process_host.h" |
jshin | 0a9aa63 | 2015-04-29 18:35:29 | [diff] [blame] | 11 | #include "third_party/icu/source/common/unicode/unistr.h" |
| 12 | #include "third_party/icu/source/i18n/unicode/timezone.h" |
[email protected] | 8b03c07 | 2014-04-30 15:49:25 | [diff] [blame] | 13 | |
| 14 | namespace content { |
| 15 | |
| 16 | TimeZoneMonitor::TimeZoneMonitor() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 17 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 8b03c07 | 2014-04-30 15:49:25 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | TimeZoneMonitor::~TimeZoneMonitor() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 21 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 8b03c07 | 2014-04-30 15:49:25 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | void TimeZoneMonitor::NotifyRenderers() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 25 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
jshin | 0a9aa63 | 2015-04-29 18:35:29 | [diff] [blame] | 26 | #if defined(OS_CHROMEOS) |
| 27 | // On CrOS, ICU's default tz is already set to a new zone. No |
| 28 | // need to redetect it with detectHostTimeZone(). |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 29 | std::unique_ptr<icu::TimeZone> new_zone(icu::TimeZone::createDefault()); |
jshin | 0a9aa63 | 2015-04-29 18:35:29 | [diff] [blame] | 30 | #else |
| 31 | icu::TimeZone* new_zone = icu::TimeZone::detectHostTimeZone(); |
| 32 | #if defined(OS_LINUX) |
| 33 | // We get here multiple times on Linux per a single tz change, but |
| 34 | // want to update the ICU default zone and notify renderer only once. |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 35 | std::unique_ptr<icu::TimeZone> current_zone(icu::TimeZone::createDefault()); |
jshin | 0a9aa63 | 2015-04-29 18:35:29 | [diff] [blame] | 36 | if (*current_zone == *new_zone) { |
| 37 | VLOG(1) << "timezone already updated"; |
| 38 | delete new_zone; |
| 39 | return; |
| 40 | } |
| 41 | #endif |
| 42 | icu::TimeZone::adoptDefault(new_zone); |
| 43 | #endif |
| 44 | icu::UnicodeString zone_id; |
| 45 | std::string zone_id_str; |
| 46 | new_zone->getID(zone_id).toUTF8String(zone_id_str); |
| 47 | VLOG(1) << "timezone reset to " << zone_id_str; |
[email protected] | 8b03c07 | 2014-04-30 15:49:25 | [diff] [blame] | 48 | for (RenderProcessHost::iterator iterator = |
| 49 | RenderProcessHost::AllHostsIterator(); |
| 50 | !iterator.IsAtEnd(); |
| 51 | iterator.Advance()) { |
jshin | 0a9aa63 | 2015-04-29 18:35:29 | [diff] [blame] | 52 | iterator.GetCurrentValue()->NotifyTimezoneChange(zone_id_str); |
[email protected] | 8b03c07 | 2014-04-30 15:49:25 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | } // namespace content |