Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / chromeos / system / timezone_util.cc
blob52bc6b98d30e11c6ba689f823578c9deb6629539
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 "chrome/browser/chromeos/system/timezone_util.h"
7 #include <string>
9 #include "base/i18n/rtl.h"
10 #include "base/lazy_instance.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/synchronization/lock.h"
16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
19 #include "chrome/browser/chromeos/profiles/profile_helper.h"
20 #include "chrome/browser/chromeos/settings/cros_settings.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "chromeos/settings/timezone_settings.h"
24 #include "chromeos/timezone/timezone_request.h"
25 #include "components/user_manager/user_manager.h"
26 #include "third_party/icu/source/common/unicode/ures.h"
27 #include "third_party/icu/source/common/unicode/utypes.h"
28 #include "third_party/icu/source/i18n/unicode/calendar.h"
29 #include "third_party/icu/source/i18n/unicode/timezone.h"
30 #include "ui/base/l10n/l10n_util.h"
32 namespace {
34 struct UResClose {
35 inline void operator() (UResourceBundle* b) const {
36 ures_close(b);
40 static base::LazyInstance<base::Lock>::Leaky
41 g_timezone_bundle_lock = LAZY_INSTANCE_INITIALIZER;
43 // Returns an exemplary city in the given timezone.
44 base::string16 GetExemplarCity(const icu::TimeZone& zone) {
45 // TODO(jungshik): After upgrading to ICU 4.6, use U_ICUDATA_ZONE
46 static const char* zone_bundle_name = NULL;
48 // These will be leaked at the end.
49 static UResourceBundle *zone_bundle = NULL;
50 static UResourceBundle *zone_strings = NULL;
52 UErrorCode status = U_ZERO_ERROR;
54 base::AutoLock lock(g_timezone_bundle_lock.Get());
55 if (zone_bundle == NULL)
56 zone_bundle = ures_open(zone_bundle_name, uloc_getDefault(), &status);
58 if (zone_strings == NULL)
59 zone_strings = ures_getByKey(zone_bundle, "zone_strings", NULL, &status);
62 icu::UnicodeString zone_id;
63 zone.getID(zone_id);
64 std::string zone_id_str;
65 zone_id.toUTF8String(zone_id_str);
67 // Resource keys for timezones use ':' in place of '/'.
68 ReplaceSubstringsAfterOffset(&zone_id_str, 0, "/", ":");
69 scoped_ptr<UResourceBundle, UResClose> zone_item(
70 ures_getByKey(zone_strings, zone_id_str.c_str(), NULL, &status));
71 icu::UnicodeString city;
72 if (!U_FAILURE(status)) {
73 city = icu::ures_getUnicodeStringByKey(zone_item.get(), "ec", &status);
74 if (U_SUCCESS(status))
75 return base::string16(city.getBuffer(), city.length());
78 // Fallback case in case of failure.
79 ReplaceSubstringsAfterOffset(&zone_id_str, 0, ":", "/");
80 // Take the last component of a timezone id (e.g. 'Baz' in 'Foo/Bar/Baz').
81 // Depending on timezones, keeping all but the 1st component
82 // (e.g. Bar/Baz) may be better, but our current list does not have
83 // any timezone for which that's the case.
84 std::string::size_type slash_pos = zone_id_str.rfind('/');
85 if (slash_pos != std::string::npos && slash_pos < zone_id_str.size())
86 zone_id_str.erase(0, slash_pos + 1);
87 // zone id has '_' in place of ' '.
88 ReplaceSubstringsAfterOffset(&zone_id_str, 0, "_", " ");
89 return base::ASCIIToUTF16(zone_id_str);
92 // Gets the given timezone's name for visualization.
93 base::string16 GetTimezoneName(const icu::TimeZone& timezone) {
94 // Instead of using the raw_offset, use the offset in effect now.
95 // For instance, US Pacific Time, the offset shown will be -7 in summer
96 // while it'll be -8 in winter.
97 int raw_offset, dst_offset;
98 UDate now = icu::Calendar::getNow();
99 UErrorCode status = U_ZERO_ERROR;
100 timezone.getOffset(now, false, raw_offset, dst_offset, status);
101 DCHECK(U_SUCCESS(status));
102 int offset = raw_offset + dst_offset;
103 // |offset| is in msec.
104 int minute_offset = std::abs(offset) / 60000;
105 int hour_offset = minute_offset / 60;
106 int min_remainder = minute_offset % 60;
107 // Some timezones have a non-integral hour offset. So, we need to use hh:mm
108 // form.
109 std::string offset_str = base::StringPrintf(offset >= 0 ?
110 "UTC+%d:%02d" : "UTC-%d:%02d", hour_offset, min_remainder);
112 // TODO(jungshik): When coming up with a better list of timezones, we also
113 // have to come up with better 'display' names. One possibility is to list
114 // multiple cities (e.g. "Los Angeles, Vancouver .." in the order of
115 // the population of a country the city belongs to.).
116 // We can also think of using LONG_GENERIC or LOCATION once we upgrade
117 // to ICU 4.6.
118 // In the meantime, we use "LONG" name with "Exemplar City" to distinguish
119 // multiple timezones with the same "LONG" name but with different
120 // rules (e.g. US Mountain Time in Denver vs Phoenix).
121 icu::UnicodeString name;
122 timezone.getDisplayName(dst_offset != 0, icu::TimeZone::LONG, name);
123 base::string16 result(l10n_util::GetStringFUTF16(
124 IDS_OPTIONS_SETTINGS_TIMEZONE_DISPLAY_TEMPLATE,
125 base::ASCIIToUTF16(offset_str),
126 base::string16(name.getBuffer(), name.length()),
127 GetExemplarCity(timezone)));
128 base::i18n::AdjustStringForLocaleDirection(&result);
129 return result;
132 } // namespace
134 namespace chromeos {
135 namespace system {
137 // Creates a list of pairs of each timezone's ID and name.
138 scoped_ptr<base::ListValue> GetTimezoneList() {
139 const std::vector<icu::TimeZone*> &timezones =
140 chromeos::system::TimezoneSettings::GetInstance()->GetTimezoneList();
141 scoped_ptr<base::ListValue> timezoneList(new base::ListValue());
142 for (std::vector<icu::TimeZone*>::const_iterator iter = timezones.begin();
143 iter != timezones.end(); ++iter) {
144 const icu::TimeZone* timezone = *iter;
145 base::ListValue* option = new base::ListValue();
146 option->Append(new base::StringValue(
147 chromeos::system::TimezoneSettings::GetTimezoneID(*timezone)));
148 option->Append(new base::StringValue(GetTimezoneName(*timezone)));
149 timezoneList->Append(option);
151 return timezoneList.Pass();
154 bool HasSystemTimezonePolicy() {
155 policy::BrowserPolicyConnectorChromeOS* connector =
156 g_browser_process->platform_part()->browser_policy_connector_chromeos();
157 if (!connector->IsEnterpriseManaged())
158 return false;
160 std::string policy_timezone;
161 if (chromeos::CrosSettings::Get()->GetString(chromeos::kSystemTimezonePolicy,
162 &policy_timezone) &&
163 !policy_timezone.empty()) {
164 VLOG(1) << "Refresh TimeZone: TimeZone settings are overridden"
165 << " by DevicePolicy.";
166 return true;
168 return false;
171 void ApplyTimeZone(const TimeZoneResponseData* timezone) {
172 if (HasSystemTimezonePolicy())
173 return;
175 const user_manager::User* primary_user =
176 user_manager::UserManager::Get()->GetPrimaryUser();
177 if (primary_user) {
178 if (!primary_user->is_profile_created())
179 return;
181 Profile* profile =
182 chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user);
183 if (!profile->GetPrefs()->GetBoolean(
184 prefs::kResolveTimezoneByGeolocation)) {
185 return;
189 if (!timezone->timeZoneId.empty()) {
190 VLOG(1) << "Refresh TimeZone: setting timezone to '" << timezone->timeZoneId
191 << "'";
193 chromeos::system::TimezoneSettings::GetInstance()->SetTimezoneFromID(
194 base::UTF8ToUTF16(timezone->timeZoneId));
198 } // namespace system
199 } // namespace chromeos