Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1195)

Side by Side Diff: chrome/browser/ui/webui/options2/chromeos/system_settings_provider.cc

Issue 8930012: Revert 114236 - Options2: Pull the trigger. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "chrome/browser/ui/webui/options2/chromeos/system_settings_provider.h"
6
7 #include <string>
8
9 #include "base/i18n/rtl.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/stl_util.h"
13 #include "base/string_util.h"
14 #include "base/stringprintf.h"
15 #include "base/synchronization/lock.h"
16 #include "base/time.h"
17 #include "base/utf_string_conversions.h"
18 #include "base/values.h"
19 #include "chrome/browser/chromeos/cros/cros_library.h"
20 #include "chrome/browser/chromeos/cros_settings.h"
21 #include "chrome/browser/chromeos/cros_settings_names.h"
22 #include "chrome/browser/chromeos/login/user_manager.h"
23 #include "grit/generated_resources.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "unicode/calendar.h"
26 #include "unicode/timezone.h"
27 #include "unicode/ures.h"
28
29 namespace {
30
31 // TODO(jungshik): Using Enumerate method in ICU gives 600+ timezones.
32 // Even after filtering out duplicate entries with a strict identity check,
33 // we still have 400+ zones. Relaxing the criteria for the timezone
34 // identity is likely to cut down the number to < 100. Until we
35 // come up with a better list, we hard-code the following list as used by
36 // Android.
37 static const char* kTimeZones[] = {
38 "Pacific/Majuro",
39 "Pacific/Midway",
40 "Pacific/Honolulu",
41 "America/Anchorage",
42 "America/Los_Angeles",
43 "America/Tijuana",
44 "America/Denver",
45 "America/Phoenix",
46 "America/Chihuahua",
47 "America/Chicago",
48 "America/Mexico_City",
49 "America/Costa_Rica",
50 "America/Regina",
51 "America/New_York",
52 "America/Bogota",
53 "America/Caracas",
54 "America/Barbados",
55 "America/Manaus",
56 "America/Santiago",
57 "America/St_Johns",
58 "America/Sao_Paulo",
59 "America/Araguaina",
60 "America/Argentina/Buenos_Aires",
61 "America/Godthab",
62 "America/Montevideo",
63 "Atlantic/South_Georgia",
64 "Atlantic/Azores",
65 "Atlantic/Cape_Verde",
66 "Africa/Casablanca",
67 "Europe/London",
68 "Europe/Amsterdam",
69 "Europe/Belgrade",
70 "Europe/Brussels",
71 "Europe/Sarajevo",
72 "Africa/Windhoek",
73 "Africa/Brazzaville",
74 "Asia/Amman",
75 "Europe/Athens",
76 "Asia/Beirut",
77 "Africa/Cairo",
78 "Europe/Helsinki",
79 "Asia/Jerusalem",
80 "Europe/Minsk",
81 "Africa/Harare",
82 "Asia/Baghdad",
83 "Europe/Moscow",
84 "Asia/Kuwait",
85 "Africa/Nairobi",
86 "Asia/Tehran",
87 "Asia/Baku",
88 "Asia/Tbilisi",
89 "Asia/Yerevan",
90 "Asia/Dubai",
91 "Asia/Kabul",
92 "Asia/Karachi",
93 "Asia/Oral",
94 "Asia/Yekaterinburg",
95 "Asia/Calcutta",
96 "Asia/Colombo",
97 "Asia/Katmandu",
98 "Asia/Almaty",
99 "Asia/Rangoon",
100 "Asia/Krasnoyarsk",
101 "Asia/Bangkok",
102 "Asia/Shanghai",
103 "Asia/Hong_Kong",
104 "Asia/Irkutsk",
105 "Asia/Kuala_Lumpur",
106 "Australia/Perth",
107 "Asia/Taipei",
108 "Asia/Seoul",
109 "Asia/Tokyo",
110 "Asia/Yakutsk",
111 "Australia/Adelaide",
112 "Australia/Darwin",
113 "Australia/Brisbane",
114 "Australia/Hobart",
115 "Australia/Sydney",
116 "Asia/Vladivostok",
117 "Pacific/Guam",
118 "Asia/Magadan",
119 "Pacific/Auckland",
120 "Pacific/Fiji",
121 "Pacific/Tongatapu",
122 };
123
124 static base::LazyInstance<base::Lock,
125 base::LeakyLazyInstanceTraits<base::Lock> >
126 g_timezone_bundle_lock = LAZY_INSTANCE_INITIALIZER;
127
128 struct UResClose {
129 inline void operator() (UResourceBundle* b) const {
130 ures_close(b);
131 }
132 };
133
134 string16 GetExemplarCity(const icu::TimeZone& zone) {
135 // TODO(jungshik): After upgrading to ICU 4.6, use U_ICUDATA_ZONE
136 static const char* zone_bundle_name = NULL;
137
138 // These will be leaked at the end.
139 static UResourceBundle *zone_bundle = NULL;
140 static UResourceBundle *zone_strings = NULL;
141
142 UErrorCode status = U_ZERO_ERROR;
143 {
144 base::AutoLock lock(g_timezone_bundle_lock.Get());
145 if (zone_bundle == NULL)
146 zone_bundle = ures_open(zone_bundle_name, uloc_getDefault(), &status);
147
148 if (zone_strings == NULL)
149 zone_strings = ures_getByKey(zone_bundle, "zone_strings", NULL, &status);
150 }
151
152 icu::UnicodeString zone_id;
153 zone.getID(zone_id);
154 std::string zone_id_str;
155 zone_id.toUTF8String(zone_id_str);
156
157 // resource keys for timezones use ':' in place of '/'.
158 ReplaceSubstringsAfterOffset(&zone_id_str, 0, "/", ":");
159 scoped_ptr_malloc<UResourceBundle, UResClose> zone_item(
160 ures_getByKey(zone_strings, zone_id_str.c_str(), NULL, &status));
161 icu::UnicodeString city;
162 if (!U_FAILURE(status)) {
163 city = icu::ures_getUnicodeStringByKey(zone_item.get(), "ec", &status);
164 if (U_SUCCESS(status))
165 return string16(city.getBuffer(), city.length());
166 }
167
168 // Fallback case in case of failure.
169 ReplaceSubstringsAfterOffset(&zone_id_str, 0, ":", "/");
170 // Take the last component of a timezone id (e.g. 'Baz' in 'Foo/Bar/Baz').
171 // Depending on timezones, keeping all but the 1st component
172 // (e.g. Bar/Baz) may be better, but our current list does not have
173 // any timezone for which that's the case.
174 std::string::size_type slash_pos = zone_id_str.rfind('/');
175 if (slash_pos != std::string::npos && slash_pos < zone_id_str.size())
176 zone_id_str.erase(0, slash_pos + 1);
177 // zone id has '_' in place of ' '.
178 ReplaceSubstringsAfterOffset(&zone_id_str, 0, "_", " ");
179 return ASCIIToUTF16(zone_id_str);
180 }
181
182 } // namespace anonymous
183
184 namespace chromeos {
185
186 SystemSettingsProvider::SystemSettingsProvider() {
187 for (size_t i = 0; i < arraysize(kTimeZones); i++) {
188 timezones_.push_back(icu::TimeZone::createTimeZone(
189 icu::UnicodeString(kTimeZones[i], -1, US_INV)));
190 }
191 system::TimezoneSettings::GetInstance()->AddObserver(this);
192 timezone_value_.reset(base::Value::CreateStringValue(GetKnownTimezoneID(
193 system::TimezoneSettings::GetInstance()->GetTimezone())));
194 }
195
196 SystemSettingsProvider::~SystemSettingsProvider() {
197 system::TimezoneSettings::GetInstance()->RemoveObserver(this);
198 STLDeleteElements(&timezones_);
199 }
200
201 void SystemSettingsProvider::DoSet(const std::string& path,
202 const base::Value& in_value) {
203 // Non-guest users can change the time zone.
204 if (UserManager::Get()->IsLoggedInAsGuest())
205 return;
206
207 if (path == kSystemTimezone) {
208 string16 value;
209 if (!in_value.IsType(Value::TYPE_STRING) || !in_value.GetAsString(&value))
210 return;
211 const icu::TimeZone* timezone = GetTimezone(value);
212 if (!timezone)
213 return;
214 system::TimezoneSettings::GetInstance()->SetTimezone(*timezone);
215 timezone_value_.reset(
216 base::Value::CreateStringValue(GetKnownTimezoneID(*timezone)));
217 }
218 }
219
220 const base::Value* SystemSettingsProvider::Get(const std::string& path) const {
221 if (path == kSystemTimezone)
222 return timezone_value_.get();
223 return NULL;
224 }
225
226 // The timezone is always trusted.
227 bool SystemSettingsProvider::GetTrusted(const std::string& path,
228 const base::Closure& callback) {
229 return true;
230 }
231
232 bool SystemSettingsProvider::HandlesSetting(const std::string& path) const {
233 return path == kSystemTimezone;
234 }
235
236 void SystemSettingsProvider::Reload() {
237 // TODO(pastarmovj): We can actually cache the timezone here to make returning
238 // it faster.
239 }
240
241 void SystemSettingsProvider::TimezoneChanged(const icu::TimeZone& timezone) {
242 // Fires system setting change notification.
243 timezone_value_.reset(
244 base::Value::CreateStringValue(GetKnownTimezoneID(timezone)));
245 CrosSettings::Get()->FireObservers(kSystemTimezone);
246 }
247
248 ListValue* SystemSettingsProvider::GetTimezoneList() {
249 ListValue* timezoneList = new ListValue();
250 for (std::vector<icu::TimeZone*>::iterator iter = timezones_.begin();
251 iter != timezones_.end(); ++iter) {
252 const icu::TimeZone* timezone = *iter;
253 ListValue* option = new ListValue();
254 option->Append(Value::CreateStringValue(GetTimezoneID(*timezone)));
255 option->Append(Value::CreateStringValue(GetTimezoneName(*timezone)));
256 timezoneList->Append(option);
257 }
258 return timezoneList;
259 }
260
261 string16 SystemSettingsProvider::GetTimezoneName(
262 const icu::TimeZone& timezone) {
263 // Instead of using the raw_offset, use the offset in effect now.
264 // For instance, US Pacific Time, the offset shown will be -7 in summer
265 // while it'll be -8 in winter.
266 int raw_offset, dst_offset;
267 UDate now = icu::Calendar::getNow();
268 UErrorCode status = U_ZERO_ERROR;
269 timezone.getOffset(now, false, raw_offset, dst_offset, status);
270 DCHECK(U_SUCCESS(status));
271 int offset = raw_offset + dst_offset;
272 // offset is in msec.
273 int minute_offset = std::abs(offset) / 60000;
274 int hour_offset = minute_offset / 60;
275 int min_remainder = minute_offset % 60;
276 // Some timezones have a non-integral hour offset. So, we need to
277 // use hh:mm form.
278 std::string offset_str = base::StringPrintf(offset >= 0 ?
279 "UTC+%d:%02d" : "UTC-%d:%02d", hour_offset, min_remainder);
280
281 // TODO(jungshik): When coming up with a better list of timezones, we also
282 // have to come up with better 'display' names. One possibility is to list
283 // multiple cities (e.g. "Los Angeles, Vancouver .." in the order of
284 // the population of a country the city belongs to.).
285 // We can also think of using LONG_GENERIC or LOCATION once we upgrade
286 // to ICU 4.6.
287 // In the meantime, we use "LONG" name with "Exemplar City" to distinguish
288 // multiple timezones with the same "LONG" name but with different
289 // rules (e.g. US Mountain Time in Denver vs Phoenix).
290 icu::UnicodeString name;
291 timezone.getDisplayName(dst_offset != 0, icu::TimeZone::LONG, name);
292 string16 result(l10n_util::GetStringFUTF16(
293 IDS_OPTIONS_SETTINGS_TIMEZONE_DISPLAY_TEMPLATE, ASCIIToUTF16(offset_str),
294 string16(name.getBuffer(), name.length()), GetExemplarCity(timezone)));
295 base::i18n::AdjustStringForLocaleDirection(&result);
296 return result;
297 }
298
299 string16 SystemSettingsProvider::GetTimezoneID(
300 const icu::TimeZone& timezone) {
301 icu::UnicodeString id;
302 timezone.getID(id);
303 return string16(id.getBuffer(), id.length());
304 }
305
306 const icu::TimeZone* SystemSettingsProvider::GetTimezone(
307 const string16& timezone_id) {
308 for (std::vector<icu::TimeZone*>::iterator iter = timezones_.begin();
309 iter != timezones_.end(); ++iter) {
310 const icu::TimeZone* timezone = *iter;
311 if (GetTimezoneID(*timezone) == timezone_id) {
312 return timezone;
313 }
314 }
315 return NULL;
316 }
317
318 string16 SystemSettingsProvider::GetKnownTimezoneID(
319 const icu::TimeZone& timezone) const {
320 for (std::vector<icu::TimeZone*>::const_iterator iter = timezones_.begin();
321 iter != timezones_.end(); ++iter) {
322 const icu::TimeZone* known_timezone = *iter;
323 if (known_timezone->hasSameRules(timezone))
324 return GetTimezoneID(*known_timezone);
325 }
326
327 // Not able to find a matching timezone in our list.
328 return string16();
329 }
330
331 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698