summaryrefslogtreecommitdiffstats
path: root/content/browser/time_zone_monitor.cc
blob: 1eb72c6f5c4c56f87a46e5e2168d144085569242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/time_zone_monitor.h"

#include "base/logging.h"
#include "build/build_config.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "third_party/icu/source/common/unicode/unistr.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"

namespace content {

TimeZoneMonitor::TimeZoneMonitor() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

TimeZoneMonitor::~TimeZoneMonitor() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

void TimeZoneMonitor::NotifyRenderers() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
#if defined(OS_CHROMEOS)
  // On CrOS, ICU's default tz is already set to a new zone. No
  // need to redetect it with detectHostTimeZone().
  scoped_ptr<icu::TimeZone> new_zone(icu::TimeZone::createDefault());
#else
  icu::TimeZone* new_zone = icu::TimeZone::detectHostTimeZone();
#if defined(OS_LINUX)
  // We get here multiple times on Linux per a single tz change, but
  // want to update the ICU default zone and notify renderer only once.
  scoped_ptr<icu::TimeZone> current_zone(icu::TimeZone::createDefault());
  if (*current_zone == *new_zone) {
    VLOG(1) << "timezone already updated";
    delete new_zone;
    return;
  }
#endif
  icu::TimeZone::adoptDefault(new_zone);
#endif
  icu::UnicodeString zone_id;
  std::string zone_id_str;
  new_zone->getID(zone_id).toUTF8String(zone_id_str);
  VLOG(1) << "timezone reset to " << zone_id_str;
  for (RenderProcessHost::iterator iterator =
           RenderProcessHost::AllHostsIterator();
       !iterator.IsAtEnd();
       iterator.Advance()) {
    iterator.GetCurrentValue()->NotifyTimezoneChange(zone_id_str);
  }
}

}  // namespace content