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

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

Issue 10689175: Refactored code for timezone settings. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added comment referring to initialization in ui.conf. Created 8 years, 5 months 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/options2/browser_options_handler.h" 5 #include "chrome/browser/ui/webui/options2/browser_options_handler.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/i18n/rtl.h"
14 #include "base/memory/singleton.h" 15 #include "base/memory/singleton.h"
15 #include "base/path_service.h" 16 #include "base/path_service.h"
16 #include "base/stl_util.h" 17 #include "base/stl_util.h"
17 #include "base/string_number_conversions.h" 18 #include "base/string_number_conversions.h"
19 #include "base/stringprintf.h"
18 #include "base/utf_string_conversions.h" 20 #include "base/utf_string_conversions.h"
19 #include "base/value_conversions.h" 21 #include "base/value_conversions.h"
20 #include "base/values.h" 22 #include "base/values.h"
21 #include "chrome/browser/auto_launch_trial.h" 23 #include "chrome/browser/auto_launch_trial.h"
22 #include "chrome/browser/browser_process.h" 24 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/chrome_page_zoom.h" 25 #include "chrome/browser/chrome_page_zoom.h"
24 #include "chrome/browser/custom_home_pages_table_model.h" 26 #include "chrome/browser/custom_home_pages_table_model.h"
25 #include "chrome/browser/download/download_prefs.h" 27 #include "chrome/browser/download/download_prefs.h"
26 #include "chrome/browser/instant/instant_controller.h" 28 #include "chrome/browser/instant/instant_controller.h"
27 #include "chrome/browser/net/url_fixer_upper.h" 29 #include "chrome/browser/net/url_fixer_upper.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 #include "grit/theme_resources.h" 77 #include "grit/theme_resources.h"
76 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 78 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
77 #include "ui/base/l10n/l10n_util.h" 79 #include "ui/base/l10n/l10n_util.h"
78 80
79 #if !defined(OS_CHROMEOS) 81 #if !defined(OS_CHROMEOS)
80 #include "chrome/browser/printing/cloud_print/cloud_print_setup_handler.h" 82 #include "chrome/browser/printing/cloud_print/cloud_print_setup_handler.h"
81 #include "chrome/browser/ui/webui/options2/advanced_options_utils.h" 83 #include "chrome/browser/ui/webui/options2/advanced_options_utils.h"
82 #endif 84 #endif
83 85
84 #if defined(OS_CHROMEOS) 86 #if defined(OS_CHROMEOS)
87 #include "base/lazy_instance.h"
88 #include "base/synchronization/lock.h"
85 #include "chrome/browser/chromeos/accessibility/accessibility_util.h" 89 #include "chrome/browser/chromeos/accessibility/accessibility_util.h"
86 #include "chrome/browser/chromeos/cros_settings.h"
87 #include "chrome/browser/chromeos/login/user_manager.h" 90 #include "chrome/browser/chromeos/login/user_manager.h"
88 #include "chrome/browser/chromeos/options/take_photo_dialog.h" 91 #include "chrome/browser/chromeos/options/take_photo_dialog.h"
89 #include "chrome/browser/chromeos/system_settings_provider.h" 92 #include "chrome/browser/chromeos/system/timezone_settings.h"
90 #include "chrome/browser/ui/browser_window.h" 93 #include "chrome/browser/ui/browser_window.h"
91 #include "ui/gfx/image/image_skia.h" 94 #include "ui/gfx/image/image_skia.h"
95 #include "unicode/calendar.h"
96 #include "unicode/timezone.h"
97 #include "unicode/ures.h"
92 #endif // defined(OS_CHROMEOS) 98 #endif // defined(OS_CHROMEOS)
93 99
94 #if defined(OS_WIN) 100 #if defined(OS_WIN)
95 #include "chrome/installer/util/auto_launch_util.h" 101 #include "chrome/installer/util/auto_launch_util.h"
96 #endif // defined(OS_WIN) 102 #endif // defined(OS_WIN)
97 103
98 #if defined(TOOLKIT_GTK) 104 #if defined(TOOLKIT_GTK)
99 #include "chrome/browser/ui/gtk/gtk_theme_service.h" 105 #include "chrome/browser/ui/gtk/gtk_theme_service.h"
100 #endif // defined(TOOLKIT_GTK) 106 #endif // defined(TOOLKIT_GTK)
101 107
102 using content::BrowserContext; 108 using content::BrowserContext;
103 using content::BrowserThread; 109 using content::BrowserThread;
104 using content::DownloadManager; 110 using content::DownloadManager;
105 using content::OpenURLParams; 111 using content::OpenURLParams;
106 using content::Referrer; 112 using content::Referrer;
107 using content::UserMetricsAction; 113 using content::UserMetricsAction;
108 114
115 #if defined(OS_CHROMEOS)
116 namespace {
117
118 struct UResClose {
119 inline void operator() (UResourceBundle* b) const {
120 ures_close(b);
121 }
122 };
123
124 static base::LazyInstance<base::Lock>::Leaky
125 g_timezone_bundle_lock = LAZY_INSTANCE_INITIALIZER;
126
127 string16 GetExemplarCity(const icu::TimeZone& zone) {
128 // TODO(jungshik): After upgrading to ICU 4.6, use U_ICUDATA_ZONE
129 static const char* zone_bundle_name = NULL;
130
131 // These will be leaked at the end.
132 static UResourceBundle *zone_bundle = NULL;
133 static UResourceBundle *zone_strings = NULL;
134
135 UErrorCode status = U_ZERO_ERROR;
136 {
137 base::AutoLock lock(g_timezone_bundle_lock.Get());
138 if (zone_bundle == NULL)
139 zone_bundle = ures_open(zone_bundle_name, uloc_getDefault(), &status);
140
141 if (zone_strings == NULL)
142 zone_strings = ures_getByKey(zone_bundle, "zone_strings", NULL, &status);
143 }
144
145 icu::UnicodeString zone_id;
146 zone.getID(zone_id);
147 std::string zone_id_str;
148 zone_id.toUTF8String(zone_id_str);
149
150 // resource keys for timezones use ':' in place of '/'.
Joao da Silva 2012/07/12 18:34:09 Nit: start sentence in uppercase
pneubeck2 2012/07/13 10:31:39 Done.
151 ReplaceSubstringsAfterOffset(&zone_id_str, 0, "/", ":");
152 scoped_ptr_malloc<UResourceBundle, UResClose> zone_item(
153 ures_getByKey(zone_strings, zone_id_str.c_str(), NULL, &status));
154 icu::UnicodeString city;
155 if (!U_FAILURE(status)) {
156 city = icu::ures_getUnicodeStringByKey(zone_item.get(), "ec", &status);
157 if (U_SUCCESS(status))
158 return string16(city.getBuffer(), city.length());
159 }
160
161 // Fallback case in case of failure.
162 ReplaceSubstringsAfterOffset(&zone_id_str, 0, ":", "/");
163 // Take the last component of a timezone id (e.g. 'Baz' in 'Foo/Bar/Baz').
164 // Depending on timezones, keeping all but the 1st component
165 // (e.g. Bar/Baz) may be better, but our current list does not have
166 // any timezone for which that's the case.
167 std::string::size_type slash_pos = zone_id_str.rfind('/');
168 if (slash_pos != std::string::npos && slash_pos < zone_id_str.size())
169 zone_id_str.erase(0, slash_pos + 1);
170 // zone id has '_' in place of ' '.
171 ReplaceSubstringsAfterOffset(&zone_id_str, 0, "_", " ");
172 return ASCIIToUTF16(zone_id_str);
173 }
174
175 // Gets the given timezone's name for visualization.
176 string16 GetTimezoneName(
177 const icu::TimeZone& timezone) {
Joao da Silva 2012/07/12 18:34:09 No need to break the previous line?
pneubeck2 2012/07/13 10:31:39 Done.
178 // Instead of using the raw_offset, use the offset in effect now.
179 // For instance, US Pacific Time, the offset shown will be -7 in summer
180 // while it'll be -8 in winter.
181 int raw_offset, dst_offset;
182 UDate now = icu::Calendar::getNow();
183 UErrorCode status = U_ZERO_ERROR;
184 timezone.getOffset(now, false, raw_offset, dst_offset, status);
185 DCHECK(U_SUCCESS(status));
186 int offset = raw_offset + dst_offset;
187 // offset is in msec.
188 int minute_offset = std::abs(offset) / 60000;
189 int hour_offset = minute_offset / 60;
190 int min_remainder = minute_offset % 60;
191 // Some timezones have a non-integral hour offset. So, we need to
192 // use hh:mm form.
193 std::string offset_str = base::StringPrintf(offset >= 0 ?
194 "UTC+%d:%02d" : "UTC-%d:%02d", hour_offset, min_remainder);
195
196 // TODO(jungshik): When coming up with a better list of timezones, we also
197 // have to come up with better 'display' names. One possibility is to list
198 // multiple cities (e.g. "Los Angeles, Vancouver .." in the order of
199 // the population of a country the city belongs to.).
200 // We can also think of using LONG_GENERIC or LOCATION once we upgrade
201 // to ICU 4.6.
202 // In the meantime, we use "LONG" name with "Exemplar City" to distinguish
203 // multiple timezones with the same "LONG" name but with different
204 // rules (e.g. US Mountain Time in Denver vs Phoenix).
205 icu::UnicodeString name;
206 timezone.getDisplayName(dst_offset != 0, icu::TimeZone::LONG, name);
207 string16 result(l10n_util::GetStringFUTF16(
208 IDS_OPTIONS_SETTINGS_TIMEZONE_DISPLAY_TEMPLATE, ASCIIToUTF16(offset_str),
209 string16(name.getBuffer(), name.length()), GetExemplarCity(timezone)));
210 base::i18n::AdjustStringForLocaleDirection(&result);
211 return result;
212 }
213
214 // Creates a list of pairs of each timezone's ID and name.
215 ListValue* GetTimezoneList() {
216 const std::vector<icu::TimeZone*> &timezones =
217 chromeos::system::TimezoneSettings::GetInstance()->GetTimezoneList();
218 ListValue* timezoneList = new ListValue();
219 for (std::vector<icu::TimeZone*>::const_iterator iter = timezones.begin();
220 iter != timezones.end(); ++iter) {
221 const icu::TimeZone* timezone = *iter;
222 ListValue* option = new ListValue();
223 option->Append(Value::CreateStringValue(
224 chromeos::system::TimezoneSettings::GetTimezoneID(*timezone)));
225 option->Append(Value::CreateStringValue(GetTimezoneName(*timezone)));
226 timezoneList->Append(option);
227 }
228 return timezoneList;
229 }
230
231 } // namespace anonymous
Joao da Silva 2012/07/12 18:34:09 Two spaces before the comment, and just "namespace
pneubeck2 2012/07/13 10:31:39 Done.
232 #endif // defined(OS_CHROMEOS)
233
109 namespace options2 { 234 namespace options2 {
110 235
111 BrowserOptionsHandler::BrowserOptionsHandler() 236 BrowserOptionsHandler::BrowserOptionsHandler()
112 : template_url_service_(NULL), 237 : template_url_service_(NULL),
113 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_for_file_(this)), 238 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_for_file_(this)),
114 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_for_ui_(this)) { 239 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_for_ui_(this)) {
115 multiprofile_ = ProfileManager::IsMultipleProfilesEnabled(); 240 multiprofile_ = ProfileManager::IsMultipleProfilesEnabled();
116 #if !defined(OS_MACOSX) 241 #if !defined(OS_MACOSX)
117 default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this); 242 default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this);
118 #endif 243 #endif
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 "languageSectionLabel", 503 "languageSectionLabel",
379 l10n_util::GetStringFUTF16( 504 l10n_util::GetStringFUTF16(
380 IDS_OPTIONS_ADVANCED_LANGUAGE_LABEL, 505 IDS_OPTIONS_ADVANCED_LANGUAGE_LABEL,
381 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); 506 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)));
382 507
383 #if defined(OS_CHROMEOS) 508 #if defined(OS_CHROMEOS)
384 values->SetString("cloudPrintLearnMoreURL", chrome::kCloudPrintLearnMoreURL); 509 values->SetString("cloudPrintLearnMoreURL", chrome::kCloudPrintLearnMoreURL);
385 510
386 // TODO(pastarmovj): replace this with a call to the CrosSettings list 511 // TODO(pastarmovj): replace this with a call to the CrosSettings list
387 // handling functionality to come. 512 // handling functionality to come.
388 values->Set("timezoneList", 513 values->Set("timezoneList", GetTimezoneList());
389 static_cast<chromeos::SystemSettingsProvider*>(
390 chromeos::CrosSettings::Get()->GetProvider(
391 chromeos::kSystemTimezone))->GetTimezoneList());
392 #endif 514 #endif
393 #if defined(OS_MACOSX) 515 #if defined(OS_MACOSX)
394 values->SetString("macPasswordsWarning", 516 values->SetString("macPasswordsWarning",
395 l10n_util::GetStringUTF16(IDS_OPTIONS_PASSWORDS_MAC_WARNING)); 517 l10n_util::GetStringUTF16(IDS_OPTIONS_PASSWORDS_MAC_WARNING));
396 values->SetBoolean("multiple_profiles", 518 values->SetBoolean("multiple_profiles",
397 g_browser_process->profile_manager()->GetNumberOfProfiles() > 1); 519 g_browser_process->profile_manager()->GetNumberOfProfiles() > 1);
398 #endif 520 #endif
399 521
400 if (multiprofile_) 522 if (multiprofile_)
401 values->Set("profilesInfo", GetProfilesInfoList().release()); 523 values->Set("profilesInfo", GetProfilesInfoList().release());
(...skipping 1041 matching lines...) Expand 10 before | Expand all | Expand 10 after
1443 } 1565 }
1444 1566
1445 void BrowserOptionsHandler::SetupSSLConfigSettings() { 1567 void BrowserOptionsHandler::SetupSSLConfigSettings() {
1446 base::FundamentalValue checked(rev_checking_enabled_.GetValue()); 1568 base::FundamentalValue checked(rev_checking_enabled_.GetValue());
1447 base::FundamentalValue disabled(rev_checking_enabled_.IsManaged()); 1569 base::FundamentalValue disabled(rev_checking_enabled_.IsManaged());
1448 web_ui()->CallJavascriptFunction( 1570 web_ui()->CallJavascriptFunction(
1449 "BrowserOptions.setCheckRevocationCheckboxState", checked, disabled); 1571 "BrowserOptions.setCheckRevocationCheckboxState", checked, disabled);
1450 } 1572 }
1451 1573
1452 } // namespace options2 1574 } // namespace options2
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698