OLD | NEW |
(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 const NotifyObserversCallback& notify_cb) |
| 188 : CrosSettingsProvider(notify_cb) { |
| 189 for (size_t i = 0; i < arraysize(kTimeZones); i++) { |
| 190 timezones_.push_back(icu::TimeZone::createTimeZone( |
| 191 icu::UnicodeString(kTimeZones[i], -1, US_INV))); |
| 192 } |
| 193 system::TimezoneSettings::GetInstance()->AddObserver(this); |
| 194 timezone_value_.reset(base::Value::CreateStringValue(GetKnownTimezoneID( |
| 195 system::TimezoneSettings::GetInstance()->GetTimezone()))); |
| 196 } |
| 197 |
| 198 SystemSettingsProvider::~SystemSettingsProvider() { |
| 199 system::TimezoneSettings::GetInstance()->RemoveObserver(this); |
| 200 STLDeleteElements(&timezones_); |
| 201 } |
| 202 |
| 203 void SystemSettingsProvider::DoSet(const std::string& path, |
| 204 const base::Value& in_value) { |
| 205 // Non-guest users can change the time zone. |
| 206 if (UserManager::Get()->IsLoggedInAsGuest()) |
| 207 return; |
| 208 |
| 209 if (path == kSystemTimezone) { |
| 210 string16 value; |
| 211 if (!in_value.IsType(Value::TYPE_STRING) || !in_value.GetAsString(&value)) |
| 212 return; |
| 213 const icu::TimeZone* timezone = GetTimezone(value); |
| 214 if (!timezone) |
| 215 return; |
| 216 system::TimezoneSettings::GetInstance()->SetTimezone(*timezone); |
| 217 timezone_value_.reset( |
| 218 base::Value::CreateStringValue(GetKnownTimezoneID(*timezone))); |
| 219 } |
| 220 } |
| 221 |
| 222 const base::Value* SystemSettingsProvider::Get(const std::string& path) const { |
| 223 if (path == kSystemTimezone) |
| 224 return timezone_value_.get(); |
| 225 return NULL; |
| 226 } |
| 227 |
| 228 // The timezone is always trusted. |
| 229 bool SystemSettingsProvider::GetTrusted(const std::string& path, |
| 230 const base::Closure& callback) { |
| 231 return true; |
| 232 } |
| 233 |
| 234 bool SystemSettingsProvider::HandlesSetting(const std::string& path) const { |
| 235 return path == kSystemTimezone; |
| 236 } |
| 237 |
| 238 void SystemSettingsProvider::Reload() { |
| 239 // TODO(pastarmovj): We can actually cache the timezone here to make returning |
| 240 // it faster. |
| 241 } |
| 242 |
| 243 void SystemSettingsProvider::TimezoneChanged(const icu::TimeZone& timezone) { |
| 244 // Fires system setting change notification. |
| 245 timezone_value_.reset( |
| 246 base::Value::CreateStringValue(GetKnownTimezoneID(timezone))); |
| 247 NotifyObservers(kSystemTimezone); |
| 248 } |
| 249 |
| 250 ListValue* SystemSettingsProvider::GetTimezoneList() { |
| 251 ListValue* timezoneList = new ListValue(); |
| 252 for (std::vector<icu::TimeZone*>::iterator iter = timezones_.begin(); |
| 253 iter != timezones_.end(); ++iter) { |
| 254 const icu::TimeZone* timezone = *iter; |
| 255 ListValue* option = new ListValue(); |
| 256 option->Append(Value::CreateStringValue(GetTimezoneID(*timezone))); |
| 257 option->Append(Value::CreateStringValue(GetTimezoneName(*timezone))); |
| 258 timezoneList->Append(option); |
| 259 } |
| 260 return timezoneList; |
| 261 } |
| 262 |
| 263 string16 SystemSettingsProvider::GetTimezoneName( |
| 264 const icu::TimeZone& timezone) { |
| 265 // Instead of using the raw_offset, use the offset in effect now. |
| 266 // For instance, US Pacific Time, the offset shown will be -7 in summer |
| 267 // while it'll be -8 in winter. |
| 268 int raw_offset, dst_offset; |
| 269 UDate now = icu::Calendar::getNow(); |
| 270 UErrorCode status = U_ZERO_ERROR; |
| 271 timezone.getOffset(now, false, raw_offset, dst_offset, status); |
| 272 DCHECK(U_SUCCESS(status)); |
| 273 int offset = raw_offset + dst_offset; |
| 274 // offset is in msec. |
| 275 int minute_offset = std::abs(offset) / 60000; |
| 276 int hour_offset = minute_offset / 60; |
| 277 int min_remainder = minute_offset % 60; |
| 278 // Some timezones have a non-integral hour offset. So, we need to |
| 279 // use hh:mm form. |
| 280 std::string offset_str = base::StringPrintf(offset >= 0 ? |
| 281 "UTC+%d:%02d" : "UTC-%d:%02d", hour_offset, min_remainder); |
| 282 |
| 283 // TODO(jungshik): When coming up with a better list of timezones, we also |
| 284 // have to come up with better 'display' names. One possibility is to list |
| 285 // multiple cities (e.g. "Los Angeles, Vancouver .." in the order of |
| 286 // the population of a country the city belongs to.). |
| 287 // We can also think of using LONG_GENERIC or LOCATION once we upgrade |
| 288 // to ICU 4.6. |
| 289 // In the meantime, we use "LONG" name with "Exemplar City" to distinguish |
| 290 // multiple timezones with the same "LONG" name but with different |
| 291 // rules (e.g. US Mountain Time in Denver vs Phoenix). |
| 292 icu::UnicodeString name; |
| 293 timezone.getDisplayName(dst_offset != 0, icu::TimeZone::LONG, name); |
| 294 string16 result(l10n_util::GetStringFUTF16( |
| 295 IDS_OPTIONS_SETTINGS_TIMEZONE_DISPLAY_TEMPLATE, ASCIIToUTF16(offset_str), |
| 296 string16(name.getBuffer(), name.length()), GetExemplarCity(timezone))); |
| 297 base::i18n::AdjustStringForLocaleDirection(&result); |
| 298 return result; |
| 299 } |
| 300 |
| 301 string16 SystemSettingsProvider::GetTimezoneID( |
| 302 const icu::TimeZone& timezone) { |
| 303 icu::UnicodeString id; |
| 304 timezone.getID(id); |
| 305 return string16(id.getBuffer(), id.length()); |
| 306 } |
| 307 |
| 308 const icu::TimeZone* SystemSettingsProvider::GetTimezone( |
| 309 const string16& timezone_id) { |
| 310 for (std::vector<icu::TimeZone*>::iterator iter = timezones_.begin(); |
| 311 iter != timezones_.end(); ++iter) { |
| 312 const icu::TimeZone* timezone = *iter; |
| 313 if (GetTimezoneID(*timezone) == timezone_id) { |
| 314 return timezone; |
| 315 } |
| 316 } |
| 317 return NULL; |
| 318 } |
| 319 |
| 320 string16 SystemSettingsProvider::GetKnownTimezoneID( |
| 321 const icu::TimeZone& timezone) const { |
| 322 for (std::vector<icu::TimeZone*>::const_iterator iter = timezones_.begin(); |
| 323 iter != timezones_.end(); ++iter) { |
| 324 const icu::TimeZone* known_timezone = *iter; |
| 325 if (known_timezone->hasSameRules(timezone)) |
| 326 return GetTimezoneID(*known_timezone); |
| 327 } |
| 328 |
| 329 // Not able to find a matching timezone in our list. |
| 330 return string16(); |
| 331 } |
| 332 |
| 333 } // namespace chromeos |
OLD | NEW |