blob: a9a5a9b6c53689695468bb91294d934281e89c67 [file] [log] [blame]
[email protected]8b03c072014-04-30 15:49:251// 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"
avib7348942015-12-25 20:57:108#include "build/build_config.h"
[email protected]8b03c072014-04-30 15:49:259#include "content/public/browser/browser_thread.h"
10#include "content/public/browser/render_process_host.h"
jshin0a9aa632015-04-29 18:35:2911#include "third_party/icu/source/common/unicode/unistr.h"
12#include "third_party/icu/source/i18n/unicode/timezone.h"
[email protected]8b03c072014-04-30 15:49:2513
14namespace content {
15
16TimeZoneMonitor::TimeZoneMonitor() {
mostynb042582e2015-03-16 22:13:4017 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]8b03c072014-04-30 15:49:2518}
19
20TimeZoneMonitor::~TimeZoneMonitor() {
mostynb042582e2015-03-16 22:13:4021 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]8b03c072014-04-30 15:49:2522}
23
24void TimeZoneMonitor::NotifyRenderers() {
mostynb042582e2015-03-16 22:13:4025 DCHECK_CURRENTLY_ON(BrowserThread::UI);
jshin0a9aa632015-04-29 18:35:2926#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().
dcheng59716272016-04-09 05:19:0829 std::unique_ptr<icu::TimeZone> new_zone(icu::TimeZone::createDefault());
jshin0a9aa632015-04-29 18:35:2930#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.
dcheng59716272016-04-09 05:19:0835 std::unique_ptr<icu::TimeZone> current_zone(icu::TimeZone::createDefault());
jshin0a9aa632015-04-29 18:35:2936 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]8b03c072014-04-30 15:49:2548 for (RenderProcessHost::iterator iterator =
49 RenderProcessHost::AllHostsIterator();
50 !iterator.IsAtEnd();
51 iterator.Advance()) {
jshin0a9aa632015-04-29 18:35:2952 iterator.GetCurrentValue()->NotifyTimezoneChange(zone_id_str);
[email protected]8b03c072014-04-30 15:49:2553 }
54}
55
56} // namespace content