[email protected] | 833a6bf2 | 2013-10-10 21:59:26 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 833a6bf2 | 2013-10-10 21:59:26 | [diff] [blame] | 5 | #include "chromeos/settings/timezone_settings.h" |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 6 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 7 | #include <string> |
| 8 | |
[email protected] | 3b7a16e7 | 2011-09-21 11:38:11 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 10 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | 833a6bf2 | 2013-10-10 21:59:26 | [diff] [blame] | 12 | #include "base/location.h" |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 13 | #include "base/logging.h" |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
| 15 | #include "base/memory/singleton.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 16 | #include "base/observer_list.h" |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 17 | #include "base/stl_util.h" |
[email protected] | 5c07332 | 2013-06-11 08:03:30 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 20 | #include "base/sys_info.h" |
[email protected] | 833a6bf2 | 2013-10-10 21:59:26 | [diff] [blame] | 21 | #include "base/task_runner.h" |
| 22 | #include "base/threading/worker_pool.h" |
[email protected] | 8bbf619 | 2013-07-18 11:14:04 | [diff] [blame] | 23 | #include "third_party/icu/source/i18n/unicode/timezone.h" |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 24 | |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // The filepath to the timezone file that symlinks to the actual timezone file. |
| 28 | const char kTimezoneSymlink[] = "/var/lib/timezone/localtime"; |
| 29 | const char kTimezoneSymlink2[] = "/var/lib/timezone/localtime2"; |
| 30 | |
| 31 | // The directory that contains all the timezone files. So for timezone |
| 32 | // "US/Pacific", the actual timezone file is: "/usr/share/zoneinfo/US/Pacific" |
| 33 | const char kTimezoneFilesDir[] = "/usr/share/zoneinfo/"; |
| 34 | |
| 35 | // Fallback time zone ID used in case of an unexpected error. |
| 36 | const char kFallbackTimeZoneId[] = "America/Los_Angeles"; |
| 37 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 38 | // TODO(jungshik): Using Enumerate method in ICU gives 600+ timezones. |
| 39 | // Even after filtering out duplicate entries with a strict identity check, |
| 40 | // we still have 400+ zones. Relaxing the criteria for the timezone |
| 41 | // identity is likely to cut down the number to < 100. Until we |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 42 | // come up with a better list, we hard-code the following list. It came from |
| 43 | // from Android initially, but more entries have been added. |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 44 | static const char* kTimeZones[] = { |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 45 | "Pacific/Midway", |
| 46 | "Pacific/Honolulu", |
| 47 | "America/Anchorage", |
| 48 | "America/Los_Angeles", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 49 | "America/Vancouver", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 50 | "America/Tijuana", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 51 | "America/Phoenix", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 52 | "America/Chihuahua", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 53 | "America/Denver", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 54 | "America/Edmonton", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 55 | "America/Mazatlan", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 56 | "America/Regina", |
| 57 | "America/Costa_Rica", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 58 | "America/Chicago", |
| 59 | "America/Mexico_City", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 60 | "America/Winnipeg", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 61 | "Pacific/Easter", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 62 | "America/Bogota", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 63 | "America/Lima", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 64 | "America/New_York", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 65 | "America/Toronto", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 66 | "America/Caracas", |
| 67 | "America/Barbados", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 68 | "America/Halifax", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 69 | "America/Manaus", |
| 70 | "America/Santiago", |
| 71 | "America/St_Johns", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 72 | "America/Araguaina", |
| 73 | "America/Argentina/Buenos_Aires", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 74 | "America/Argentina/San_Luis", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 75 | "America/Sao_Paulo", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 76 | "America/Montevideo", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 77 | "America/Godthab", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 78 | "Atlantic/South_Georgia", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 79 | "Atlantic/Cape_Verde", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 80 | "Atlantic/Azores", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 81 | "Atlantic/Reykjavik", |
| 82 | "Atlantic/St_Helena", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 83 | "Africa/Casablanca", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 84 | "Atlantic/Faroe", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 85 | "Europe/Dublin", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 86 | "Europe/Lisbon", |
| 87 | "Europe/London", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 88 | "Europe/Amsterdam", |
| 89 | "Europe/Belgrade", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 90 | "Europe/Berlin", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 91 | "Europe/Brussels", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 92 | "Europe/Budapest", |
| 93 | "Europe/Copenhagen", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 94 | "Europe/Ljubljana", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 95 | "Europe/Madrid", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 96 | "Europe/Oslo", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 97 | "Europe/Paris", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 98 | "Europe/Prague", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 99 | "Europe/Rome", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 100 | "Europe/Stockholm", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 101 | "Europe/Sarajevo", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 102 | "Europe/Tirane", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 103 | "Europe/Vienna", |
| 104 | "Europe/Warsaw", |
| 105 | "Europe/Zurich", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 106 | "Africa/Windhoek", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 107 | "Africa/Lagos", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 108 | "Africa/Brazzaville", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 109 | "Africa/Cairo", |
| 110 | "Africa/Harare", |
| 111 | "Africa/Maputo", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 112 | "Africa/Johannesburg", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 113 | "Europe/Athens", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 114 | "Europe/Bucharest", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 115 | "Europe/Chisinau", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 116 | "Europe/Helsinki", |
| 117 | "Europe/Istanbul", |
| 118 | "Europe/Kiev", |
| 119 | "Europe/Riga", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 120 | "Europe/Sofia", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 121 | "Europe/Tallinn", |
| 122 | "Europe/Vilnius", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 123 | "Asia/Amman", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 124 | "Asia/Beirut", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 125 | "Asia/Jerusalem", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 126 | "Africa/Nairobi", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 127 | "Asia/Baghdad", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 128 | "Asia/Riyadh", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 129 | "Asia/Kuwait", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 130 | "Europe/Minsk", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 131 | "Asia/Tehran", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 132 | "Europe/Moscow", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 133 | "Asia/Dubai", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 134 | "Asia/Tbilisi", |
| 135 | "Indian/Mauritius", |
| 136 | "Asia/Baku", |
| 137 | "Asia/Yerevan", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 138 | "Asia/Kabul", |
| 139 | "Asia/Karachi", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 140 | "Asia/Ashgabat", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 141 | "Asia/Oral", |
[email protected] | 8e0a318 | 2014-02-27 06:27:31 | [diff] [blame] | 142 | "Asia/Calcutta", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 143 | "Asia/Colombo", |
| 144 | "Asia/Katmandu", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 145 | "Asia/Yekaterinburg", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 146 | "Asia/Almaty", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 147 | "Asia/Dhaka", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 148 | "Asia/Rangoon", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 149 | "Asia/Bangkok", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 150 | "Asia/Jakarta", |
| 151 | "Asia/Omsk", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 152 | "Asia/Novosibirsk", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 153 | "Asia/Ho_Chi_Minh", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 154 | "Asia/Phnom_Penh", |
[email protected] | 0f43b2c | 2014-02-14 00:12:36 | [diff] [blame] | 155 | "Asia/Vientiane", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 156 | "Asia/Shanghai", |
| 157 | "Asia/Hong_Kong", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 158 | "Asia/Kuala_Lumpur", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 159 | "Asia/Singapore", |
| 160 | "Asia/Manila", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 161 | "Asia/Taipei", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 162 | "Asia/Makassar", |
| 163 | "Asia/Krasnoyarsk", |
| 164 | "Australia/Perth", |
| 165 | "Australia/Eucla", |
| 166 | "Asia/Irkutsk", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 167 | "Asia/Seoul", |
| 168 | "Asia/Tokyo", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 169 | "Asia/Jayapura", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 170 | "Australia/Darwin", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 171 | "Australia/Adelaide", |
| 172 | "Asia/Yakutsk", |
| 173 | "Pacific/Guam", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 174 | "Australia/Brisbane", |
| 175 | "Australia/Hobart", |
| 176 | "Australia/Sydney", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 177 | "Pacific/Port_Moresby", |
| 178 | "Asia/Vladivostok", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 179 | "Asia/Sakhalin", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 180 | "Asia/Magadan", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 181 | "Pacific/Fiji", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 182 | "Pacific/Majuro", |
[email protected] | bdf5b228 | 2014-02-15 14:21:09 | [diff] [blame] | 183 | "Pacific/Auckland", |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 184 | "Pacific/Tongatapu", |
[email protected] | f07059d | 2013-05-23 16:20:32 | [diff] [blame] | 185 | "Pacific/Apia", |
[email protected] | fd22e70 | 2013-06-06 05:09:16 | [diff] [blame] | 186 | "Pacific/Kiritimati", |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | std::string GetTimezoneIDAsString() { |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 190 | // Compare with chromiumos/src/platform/init/ui.conf which fixes certain |
| 191 | // incorrect states of the timezone symlink on startup. Thus errors occuring |
| 192 | // here should be rather contrived. |
| 193 | |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 194 | // Look at kTimezoneSymlink, see which timezone we are symlinked to. |
| 195 | char buf[256]; |
| 196 | const ssize_t len = readlink(kTimezoneSymlink, buf, |
| 197 | sizeof(buf)-1); |
| 198 | if (len == -1) { |
| 199 | LOG(ERROR) << "GetTimezoneID: Cannot read timezone symlink " |
| 200 | << kTimezoneSymlink; |
| 201 | return std::string(); |
| 202 | } |
| 203 | |
| 204 | std::string timezone(buf, len); |
| 205 | // Remove kTimezoneFilesDir from the beginning. |
| 206 | if (timezone.find(kTimezoneFilesDir) != 0) { |
| 207 | LOG(ERROR) << "GetTimezoneID: Timezone symlink is wrong " |
| 208 | << timezone; |
| 209 | return std::string(); |
| 210 | } |
| 211 | |
| 212 | return timezone.substr(strlen(kTimezoneFilesDir)); |
| 213 | } |
| 214 | |
| 215 | void SetTimezoneIDFromString(const std::string& id) { |
| 216 | // Change the kTimezoneSymlink symlink to the path for this timezone. |
| 217 | // We want to do this in an atomic way. So we are going to create the symlink |
| 218 | // at kTimezoneSymlink2 and then move it to kTimezoneSymlink |
| 219 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 220 | base::FilePath timezone_symlink(kTimezoneSymlink); |
| 221 | base::FilePath timezone_symlink2(kTimezoneSymlink2); |
| 222 | base::FilePath timezone_file(kTimezoneFilesDir + id); |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 223 | |
| 224 | // Make sure timezone_file exists. |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 225 | if (!base::PathExists(timezone_file)) { |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 226 | LOG(ERROR) << "SetTimezoneID: Cannot find timezone file " |
| 227 | << timezone_file.value(); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | // Delete old symlink2 if it exists. |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 232 | base::DeleteFile(timezone_symlink2, false); |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 233 | |
| 234 | // Create new symlink2. |
| 235 | if (symlink(timezone_file.value().c_str(), |
| 236 | timezone_symlink2.value().c_str()) == -1) { |
| 237 | LOG(ERROR) << "SetTimezoneID: Unable to create symlink " |
| 238 | << timezone_symlink2.value() << " to " << timezone_file.value(); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | // Move symlink2 to symlink. |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 243 | if (!base::ReplaceFile(timezone_symlink2, timezone_symlink, NULL)) { |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 244 | LOG(ERROR) << "SetTimezoneID: Unable to move symlink " |
| 245 | << timezone_symlink2.value() << " to " |
| 246 | << timezone_symlink.value(); |
| 247 | } |
| 248 | } |
| 249 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 250 | // Common code of the TimezoneSettings implementations. |
| 251 | class TimezoneSettingsBaseImpl : public chromeos::system::TimezoneSettings { |
| 252 | public: |
| 253 | virtual ~TimezoneSettingsBaseImpl(); |
| 254 | |
| 255 | // TimezoneSettings implementation: |
| 256 | virtual const icu::TimeZone& GetTimezone() OVERRIDE; |
[email protected] | 33c4bc79 | 2013-12-19 20:52:30 | [diff] [blame] | 257 | virtual base::string16 GetCurrentTimezoneID() OVERRIDE; |
| 258 | virtual void SetTimezoneFromID(const base::string16& timezone_id) OVERRIDE; |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 259 | virtual void AddObserver(Observer* observer) OVERRIDE; |
| 260 | virtual void RemoveObserver(Observer* observer) OVERRIDE; |
| 261 | virtual const std::vector<icu::TimeZone*>& GetTimezoneList() const OVERRIDE; |
| 262 | |
| 263 | protected: |
| 264 | TimezoneSettingsBaseImpl(); |
| 265 | |
| 266 | // Returns |timezone| if it is an element of |timezones_|. |
| 267 | // Otherwise, returns a timezone from |timezones_|, if such exists, that has |
| 268 | // the same rule as the given |timezone|. |
| 269 | // Otherwise, returns NULL. |
| 270 | // Note multiple timezones with the same time zone offset may exist |
| 271 | // e.g. |
| 272 | // US/Pacific == America/Los_Angeles |
| 273 | const icu::TimeZone* GetKnownTimezoneOrNull( |
| 274 | const icu::TimeZone& timezone) const; |
| 275 | |
| 276 | ObserverList<Observer> observers_; |
| 277 | std::vector<icu::TimeZone*> timezones_; |
| 278 | scoped_ptr<icu::TimeZone> timezone_; |
| 279 | |
| 280 | private: |
| 281 | DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsBaseImpl); |
| 282 | }; |
| 283 | |
| 284 | // The TimezoneSettings implementation used in production. |
| 285 | class TimezoneSettingsImpl : public TimezoneSettingsBaseImpl { |
| 286 | public: |
| 287 | // TimezoneSettings implementation: |
| 288 | virtual void SetTimezone(const icu::TimeZone& timezone) OVERRIDE; |
| 289 | |
| 290 | static TimezoneSettingsImpl* GetInstance(); |
| 291 | |
| 292 | private: |
| 293 | friend struct DefaultSingletonTraits<TimezoneSettingsImpl>; |
| 294 | |
| 295 | TimezoneSettingsImpl(); |
| 296 | |
| 297 | DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsImpl); |
| 298 | }; |
| 299 | |
| 300 | // The stub TimezoneSettings implementation used on Linux desktop. |
| 301 | class TimezoneSettingsStubImpl : public TimezoneSettingsBaseImpl { |
| 302 | public: |
| 303 | // TimezoneSettings implementation: |
| 304 | virtual void SetTimezone(const icu::TimeZone& timezone) OVERRIDE; |
| 305 | |
| 306 | static TimezoneSettingsStubImpl* GetInstance(); |
| 307 | |
| 308 | private: |
| 309 | friend struct DefaultSingletonTraits<TimezoneSettingsStubImpl>; |
| 310 | |
| 311 | TimezoneSettingsStubImpl(); |
| 312 | |
| 313 | DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsStubImpl); |
| 314 | }; |
| 315 | |
| 316 | TimezoneSettingsBaseImpl::~TimezoneSettingsBaseImpl() { |
| 317 | STLDeleteElements(&timezones_); |
| 318 | } |
| 319 | |
| 320 | const icu::TimeZone& TimezoneSettingsBaseImpl::GetTimezone() { |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 321 | return *timezone_.get(); |
| 322 | } |
| 323 | |
[email protected] | 33c4bc79 | 2013-12-19 20:52:30 | [diff] [blame] | 324 | base::string16 TimezoneSettingsBaseImpl::GetCurrentTimezoneID() { |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 325 | return chromeos::system::TimezoneSettings::GetTimezoneID(GetTimezone()); |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | 33c4bc79 | 2013-12-19 20:52:30 | [diff] [blame] | 328 | void TimezoneSettingsBaseImpl::SetTimezoneFromID( |
| 329 | const base::string16& timezone_id) { |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 330 | scoped_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone( |
| 331 | icu::UnicodeString(timezone_id.c_str(), timezone_id.size()))); |
| 332 | SetTimezone(*timezone); |
| 333 | } |
| 334 | |
| 335 | void TimezoneSettingsBaseImpl::AddObserver(Observer* observer) { |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 336 | observers_.AddObserver(observer); |
| 337 | } |
| 338 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 339 | void TimezoneSettingsBaseImpl::RemoveObserver(Observer* observer) { |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 340 | observers_.RemoveObserver(observer); |
| 341 | } |
| 342 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 343 | const std::vector<icu::TimeZone*>& |
| 344 | TimezoneSettingsBaseImpl::GetTimezoneList() const { |
| 345 | return timezones_; |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 346 | } |
| 347 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 348 | TimezoneSettingsBaseImpl::TimezoneSettingsBaseImpl() { |
| 349 | for (size_t i = 0; i < arraysize(kTimeZones); ++i) { |
| 350 | timezones_.push_back(icu::TimeZone::createTimeZone( |
| 351 | icu::UnicodeString(kTimeZones[i], -1, US_INV))); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | const icu::TimeZone* TimezoneSettingsBaseImpl::GetKnownTimezoneOrNull( |
| 356 | const icu::TimeZone& timezone) const { |
| 357 | const icu::TimeZone* known_timezone = NULL; |
| 358 | for (std::vector<icu::TimeZone*>::const_iterator iter = timezones_.begin(); |
| 359 | iter != timezones_.end(); ++iter) { |
| 360 | const icu::TimeZone* entry = *iter; |
| 361 | if (*entry == timezone) |
| 362 | return entry; |
| 363 | if (entry->hasSameRules(timezone)) |
| 364 | known_timezone = entry; |
| 365 | } |
| 366 | |
| 367 | // May return NULL if we did not find a matching timezone in our list. |
| 368 | return known_timezone; |
| 369 | } |
| 370 | |
| 371 | void TimezoneSettingsImpl::SetTimezone(const icu::TimeZone& timezone) { |
| 372 | // Replace |timezone| by a known timezone with the same rules. If none exists |
| 373 | // go on with |timezone|. |
| 374 | const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone); |
| 375 | if (!known_timezone) |
| 376 | known_timezone = &timezone; |
| 377 | |
| 378 | timezone_.reset(known_timezone->clone()); |
[email protected] | d519b81 | 2013-12-25 18:15:40 | [diff] [blame] | 379 | std::string id = base::UTF16ToUTF8(GetTimezoneID(*known_timezone)); |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 380 | VLOG(1) << "Setting timezone to " << id; |
| 381 | // It's safe to change the timezone config files in the background as the |
| 382 | // following operations don't depend on the completion of the config change. |
[email protected] | 833a6bf2 | 2013-10-10 21:59:26 | [diff] [blame] | 383 | base::WorkerPool::GetTaskRunner(true /* task is slow */)-> |
| 384 | PostTask(FROM_HERE, base::Bind(&SetTimezoneIDFromString, id)); |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 385 | icu::TimeZone::setDefault(*known_timezone); |
| 386 | FOR_EACH_OBSERVER(Observer, observers_, TimezoneChanged(*known_timezone)); |
| 387 | } |
| 388 | |
| 389 | // static |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 390 | TimezoneSettingsImpl* TimezoneSettingsImpl::GetInstance() { |
| 391 | return Singleton<TimezoneSettingsImpl, |
| 392 | DefaultSingletonTraits<TimezoneSettingsImpl> >::get(); |
| 393 | } |
| 394 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 395 | TimezoneSettingsImpl::TimezoneSettingsImpl() { |
| 396 | std::string id = GetTimezoneIDAsString(); |
| 397 | if (id.empty()) { |
| 398 | id = kFallbackTimeZoneId; |
| 399 | LOG(ERROR) << "Got an empty string for timezone, default to '" << id; |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 400 | } |
| 401 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 402 | timezone_.reset(icu::TimeZone::createTimeZone( |
| 403 | icu::UnicodeString::fromUTF8(id))); |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 404 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 405 | // Store a known timezone equivalent to id in |timezone_|. |
| 406 | const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(*timezone_); |
| 407 | if (known_timezone != NULL && *known_timezone != *timezone_) |
| 408 | // Not necessary to update the filesystem because |known_timezone| has the |
| 409 | // same rules. |
| 410 | timezone_.reset(known_timezone->clone()); |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 411 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 412 | icu::TimeZone::setDefault(*timezone_); |
| 413 | VLOG(1) << "Timezone initially set to " << id; |
| 414 | } |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 415 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 416 | void TimezoneSettingsStubImpl::SetTimezone(const icu::TimeZone& timezone) { |
| 417 | // Replace |timezone| by a known timezone with the same rules. If none exists |
| 418 | // go on with |timezone|. |
| 419 | const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(timezone); |
| 420 | if (!known_timezone) |
| 421 | known_timezone = &timezone; |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 422 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 423 | timezone_.reset(known_timezone->clone()); |
| 424 | icu::TimeZone::setDefault(*known_timezone); |
| 425 | FOR_EACH_OBSERVER(Observer, observers_, TimezoneChanged(*known_timezone)); |
| 426 | } |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 427 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 428 | // static |
| 429 | TimezoneSettingsStubImpl* TimezoneSettingsStubImpl::GetInstance() { |
| 430 | return Singleton<TimezoneSettingsStubImpl, |
| 431 | DefaultSingletonTraits<TimezoneSettingsStubImpl> >::get(); |
| 432 | } |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 433 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 434 | TimezoneSettingsStubImpl::TimezoneSettingsStubImpl() { |
| 435 | timezone_.reset(icu::TimeZone::createDefault()); |
| 436 | const icu::TimeZone* known_timezone = GetKnownTimezoneOrNull(*timezone_); |
| 437 | if (known_timezone != NULL && *known_timezone != *timezone_) |
| 438 | timezone_.reset(known_timezone->clone()); |
| 439 | } |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 440 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 441 | } // namespace |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 442 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 443 | namespace chromeos { |
| 444 | namespace system { |
| 445 | |
| 446 | TimezoneSettings::Observer::~Observer() {} |
| 447 | |
| 448 | // static |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 449 | TimezoneSettings* TimezoneSettings::GetInstance() { |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 450 | if (base::SysInfo::IsRunningOnChromeOS()) { |
[email protected] | 65acb40 | 2011-09-30 06:09:06 | [diff] [blame] | 451 | return TimezoneSettingsImpl::GetInstance(); |
| 452 | } else { |
| 453 | return TimezoneSettingsStubImpl::GetInstance(); |
| 454 | } |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 455 | } |
| 456 | |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 457 | // static |
[email protected] | 33c4bc79 | 2013-12-19 20:52:30 | [diff] [blame] | 458 | base::string16 TimezoneSettings::GetTimezoneID(const icu::TimeZone& timezone) { |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 459 | icu::UnicodeString id; |
| 460 | timezone.getID(id); |
[email protected] | 33c4bc79 | 2013-12-19 20:52:30 | [diff] [blame] | 461 | return base::string16(id.getBuffer(), id.length()); |
[email protected] | 5f647a3 | 2012-07-18 15:36:06 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 464 | } // namespace system |
| 465 | } // namespace chromeos |