Chromium Code Reviews| Index: chrome/browser/ui/webui/options2/browser_options_handler.cc |
| diff --git a/chrome/browser/ui/webui/options2/browser_options_handler.cc b/chrome/browser/ui/webui/options2/browser_options_handler.cc |
| index e4f6339001e4892f9dd2c6f5df9a311ddffdde11..87e396cb4933d019dc53a01485b6ab9f4fa6bc65 100644 |
| --- a/chrome/browser/ui/webui/options2/browser_options_handler.cc |
| +++ b/chrome/browser/ui/webui/options2/browser_options_handler.cc |
| @@ -11,10 +11,12 @@ |
| #include "base/bind.h" |
| #include "base/bind_helpers.h" |
| #include "base/command_line.h" |
| +#include "base/i18n/rtl.h" |
| #include "base/memory/singleton.h" |
| #include "base/path_service.h" |
| #include "base/stl_util.h" |
| #include "base/string_number_conversions.h" |
| +#include "base/stringprintf.h" |
| #include "base/utf_string_conversions.h" |
| #include "base/value_conversions.h" |
| #include "base/values.h" |
| @@ -82,13 +84,17 @@ |
| #endif |
| #if defined(OS_CHROMEOS) |
| +#include "base/lazy_instance.h" |
| +#include "base/synchronization/lock.h" |
| #include "chrome/browser/chromeos/accessibility/accessibility_util.h" |
| -#include "chrome/browser/chromeos/cros_settings.h" |
| #include "chrome/browser/chromeos/login/user_manager.h" |
| #include "chrome/browser/chromeos/options/take_photo_dialog.h" |
| -#include "chrome/browser/chromeos/system_settings_provider.h" |
| +#include "chrome/browser/chromeos/system/timezone_settings.h" |
| #include "chrome/browser/ui/browser_window.h" |
| #include "ui/gfx/image/image_skia.h" |
| +#include "unicode/calendar.h" |
| +#include "unicode/timezone.h" |
| +#include "unicode/ures.h" |
| #endif // defined(OS_CHROMEOS) |
| #if defined(OS_WIN) |
| @@ -106,6 +112,124 @@ using content::OpenURLParams; |
| using content::Referrer; |
| using content::UserMetricsAction; |
| +#if defined(OS_CHROMEOS) |
|
James Hawkins
2012/07/13 14:25:04
I'm not a fan of having this much CrOS-specific co
pneubeck2
2012/07/16 11:32:38
Done.
|
| +namespace { |
| + |
| +struct UResClose { |
| + inline void operator() (UResourceBundle* b) const { |
| + ures_close(b); |
| + } |
| +}; |
| + |
| +static base::LazyInstance<base::Lock>::Leaky |
| + g_timezone_bundle_lock = LAZY_INSTANCE_INITIALIZER; |
| + |
| +string16 GetExemplarCity(const icu::TimeZone& zone) { |
| + // TODO(jungshik): After upgrading to ICU 4.6, use U_ICUDATA_ZONE |
| + static const char* zone_bundle_name = NULL; |
| + |
| + // These will be leaked at the end. |
| + static UResourceBundle *zone_bundle = NULL; |
| + static UResourceBundle *zone_strings = NULL; |
| + |
| + UErrorCode status = U_ZERO_ERROR; |
| + { |
| + base::AutoLock lock(g_timezone_bundle_lock.Get()); |
| + if (zone_bundle == NULL) |
| + zone_bundle = ures_open(zone_bundle_name, uloc_getDefault(), &status); |
| + |
| + if (zone_strings == NULL) |
| + zone_strings = ures_getByKey(zone_bundle, "zone_strings", NULL, &status); |
| + } |
| + |
| + icu::UnicodeString zone_id; |
| + zone.getID(zone_id); |
| + std::string zone_id_str; |
| + zone_id.toUTF8String(zone_id_str); |
| + |
| + // Resource keys for timezones use ':' in place of '/'. |
| + ReplaceSubstringsAfterOffset(&zone_id_str, 0, "/", ":"); |
| + scoped_ptr_malloc<UResourceBundle, UResClose> zone_item( |
| + ures_getByKey(zone_strings, zone_id_str.c_str(), NULL, &status)); |
| + icu::UnicodeString city; |
| + if (!U_FAILURE(status)) { |
| + city = icu::ures_getUnicodeStringByKey(zone_item.get(), "ec", &status); |
| + if (U_SUCCESS(status)) |
| + return string16(city.getBuffer(), city.length()); |
| + } |
| + |
| + // Fallback case in case of failure. |
| + ReplaceSubstringsAfterOffset(&zone_id_str, 0, ":", "/"); |
| + // Take the last component of a timezone id (e.g. 'Baz' in 'Foo/Bar/Baz'). |
| + // Depending on timezones, keeping all but the 1st component |
| + // (e.g. Bar/Baz) may be better, but our current list does not have |
| + // any timezone for which that's the case. |
| + std::string::size_type slash_pos = zone_id_str.rfind('/'); |
| + if (slash_pos != std::string::npos && slash_pos < zone_id_str.size()) |
| + zone_id_str.erase(0, slash_pos + 1); |
| + // zone id has '_' in place of ' '. |
| + ReplaceSubstringsAfterOffset(&zone_id_str, 0, "_", " "); |
| + return ASCIIToUTF16(zone_id_str); |
| +} |
| + |
| +// Gets the given timezone's name for visualization. |
| +string16 GetTimezoneName(const icu::TimeZone& timezone) { |
| + // Instead of using the raw_offset, use the offset in effect now. |
| + // For instance, US Pacific Time, the offset shown will be -7 in summer |
| + // while it'll be -8 in winter. |
| + int raw_offset, dst_offset; |
| + UDate now = icu::Calendar::getNow(); |
| + UErrorCode status = U_ZERO_ERROR; |
| + timezone.getOffset(now, false, raw_offset, dst_offset, status); |
| + DCHECK(U_SUCCESS(status)); |
| + int offset = raw_offset + dst_offset; |
| + // |offset| is in msec. |
| + int minute_offset = std::abs(offset) / 60000; |
| + int hour_offset = minute_offset / 60; |
| + int min_remainder = minute_offset % 60; |
| + // Some timezones have a non-integral hour offset. So, we need to use hh:mm |
| + // form. |
| + std::string offset_str = base::StringPrintf(offset >= 0 ? |
| + "UTC+%d:%02d" : "UTC-%d:%02d", hour_offset, min_remainder); |
| + |
| + // TODO(jungshik): When coming up with a better list of timezones, we also |
| + // have to come up with better 'display' names. One possibility is to list |
| + // multiple cities (e.g. "Los Angeles, Vancouver .." in the order of |
| + // the population of a country the city belongs to.). |
| + // We can also think of using LONG_GENERIC or LOCATION once we upgrade |
| + // to ICU 4.6. |
| + // In the meantime, we use "LONG" name with "Exemplar City" to distinguish |
| + // multiple timezones with the same "LONG" name but with different |
| + // rules (e.g. US Mountain Time in Denver vs Phoenix). |
| + icu::UnicodeString name; |
| + timezone.getDisplayName(dst_offset != 0, icu::TimeZone::LONG, name); |
| + string16 result(l10n_util::GetStringFUTF16( |
| + IDS_OPTIONS_SETTINGS_TIMEZONE_DISPLAY_TEMPLATE, ASCIIToUTF16(offset_str), |
| + string16(name.getBuffer(), name.length()), GetExemplarCity(timezone))); |
| + base::i18n::AdjustStringForLocaleDirection(&result); |
| + return result; |
| +} |
| + |
| +// Creates a list of pairs of each timezone's ID and name. |
| +ListValue* GetTimezoneList() { |
| + const std::vector<icu::TimeZone*> &timezones = |
| + chromeos::system::TimezoneSettings::GetInstance()->GetTimezoneList(); |
| + ListValue* timezoneList = new ListValue(); |
| + for (std::vector<icu::TimeZone*>::const_iterator iter = timezones.begin(); |
| + iter != timezones.end(); ++iter) { |
| + const icu::TimeZone* timezone = *iter; |
| + ListValue* option = new ListValue(); |
| + option->Append(Value::CreateStringValue( |
| + chromeos::system::TimezoneSettings::GetTimezoneID(*timezone))); |
| + option->Append(Value::CreateStringValue(GetTimezoneName(*timezone))); |
| + timezoneList->Append(option); |
| + } |
| + return timezoneList; |
| +} |
| + |
| +} // namespace |
| +#endif // defined(OS_CHROMEOS) |
| + |
| namespace options2 { |
| BrowserOptionsHandler::BrowserOptionsHandler() |
| @@ -385,10 +509,7 @@ void BrowserOptionsHandler::GetLocalizedValues(DictionaryValue* values) { |
| // TODO(pastarmovj): replace this with a call to the CrosSettings list |
| // handling functionality to come. |
| - values->Set("timezoneList", |
| - static_cast<chromeos::SystemSettingsProvider*>( |
| - chromeos::CrosSettings::Get()->GetProvider( |
| - chromeos::kSystemTimezone))->GetTimezoneList()); |
| + values->Set("timezoneList", GetTimezoneList()); |
| #endif |
| #if defined(OS_MACOSX) |
| values->SetString("macPasswordsWarning", |