OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/system/timezone_util.h" | 5 #include "chrome/browser/chromeos/system/timezone_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
| 13 #include "base/command_line.h" |
13 #include "base/i18n/rtl.h" | 14 #include "base/i18n/rtl.h" |
14 #include "base/i18n/unicodestring.h" | 15 #include "base/i18n/unicodestring.h" |
15 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
21 #include "base/values.h" | 22 #include "base/values.h" |
22 #include "chrome/browser/browser_process.h" | 23 #include "chrome/browser/browser_process.h" |
23 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 24 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 25 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" |
24 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 26 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
25 #include "chrome/browser/chromeos/settings/cros_settings.h" | 27 #include "chrome/browser/chromeos/settings/cros_settings.h" |
26 #include "chrome/browser/chromeos/system/timezone_resolver_manager.h" | 28 #include "chrome/browser/chromeos/system/timezone_resolver_manager.h" |
| 29 #include "chrome/common/pref_names.h" |
27 #include "chrome/grit/generated_resources.h" | 30 #include "chrome/grit/generated_resources.h" |
| 31 #include "chromeos/chromeos_switches.h" |
28 #include "chromeos/settings/timezone_settings.h" | 32 #include "chromeos/settings/timezone_settings.h" |
29 #include "chromeos/timezone/timezone_request.h" | 33 #include "chromeos/timezone/timezone_request.h" |
30 #include "components/prefs/pref_service.h" | 34 #include "components/prefs/pref_service.h" |
| 35 #include "components/user_manager/user.h" |
31 #include "components/user_manager/user_manager.h" | 36 #include "components/user_manager/user_manager.h" |
32 #include "third_party/icu/source/common/unicode/ures.h" | 37 #include "third_party/icu/source/common/unicode/ures.h" |
33 #include "third_party/icu/source/common/unicode/utypes.h" | 38 #include "third_party/icu/source/common/unicode/utypes.h" |
34 #include "third_party/icu/source/i18n/unicode/calendar.h" | 39 #include "third_party/icu/source/i18n/unicode/calendar.h" |
35 #include "third_party/icu/source/i18n/unicode/timezone.h" | 40 #include "third_party/icu/source/i18n/unicode/timezone.h" |
36 #include "ui/base/l10n/l10n_util.h" | 41 #include "ui/base/l10n/l10n_util.h" |
37 | 42 |
38 namespace { | 43 namespace { |
39 | 44 |
40 struct UResClose { | 45 struct UResClose { |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 if (chromeos::CrosSettings::Get()->GetString(chromeos::kSystemTimezonePolicy, | 182 if (chromeos::CrosSettings::Get()->GetString(chromeos::kSystemTimezonePolicy, |
178 &policy_timezone) && | 183 &policy_timezone) && |
179 !policy_timezone.empty()) { | 184 !policy_timezone.empty()) { |
180 VLOG(1) << "Refresh TimeZone: TimeZone settings are overridden" | 185 VLOG(1) << "Refresh TimeZone: TimeZone settings are overridden" |
181 << " by DevicePolicy."; | 186 << " by DevicePolicy."; |
182 return true; | 187 return true; |
183 } | 188 } |
184 return false; | 189 return false; |
185 } | 190 } |
186 | 191 |
| 192 bool IsTimezonePrefsManaged(const std::string& pref_name) { |
| 193 DCHECK(pref_name == prefs::kUserTimezone || |
| 194 pref_name == prefs::kResolveTimezoneByGeolocation); |
| 195 |
| 196 std::string policy_timezone; |
| 197 if (chromeos::CrosSettings::Get()->GetString(chromeos::kSystemTimezonePolicy, |
| 198 &policy_timezone) && |
| 199 !policy_timezone.empty()) { |
| 200 return true; |
| 201 } |
| 202 |
| 203 const PrefService* local_state = g_browser_process->local_state(); |
| 204 if (!local_state->IsManagedPreference( |
| 205 prefs::kSystemTimezoneAutomaticDetectionPolicy)) { |
| 206 return false; |
| 207 } |
| 208 |
| 209 int resolve_policy_value = |
| 210 local_state->GetInteger(prefs::kSystemTimezoneAutomaticDetectionPolicy); |
| 211 |
| 212 switch (resolve_policy_value) { |
| 213 case enterprise_management::SystemTimezoneProto::USERS_DECIDE: |
| 214 return false; |
| 215 case enterprise_management::SystemTimezoneProto::DISABLED: |
| 216 // This only disables resolving. |
| 217 return pref_name == prefs::kResolveTimezoneByGeolocation; |
| 218 case enterprise_management::SystemTimezoneProto::IP_ONLY: |
| 219 case enterprise_management::SystemTimezoneProto::SEND_WIFI_ACCESS_POINTS: |
| 220 case enterprise_management::SystemTimezoneProto::SEND_ALL_LOCATION_INFO: |
| 221 return true; |
| 222 } |
| 223 // Default for unknown policy value. |
| 224 NOTREACHED() << "Unrecognized policy value: " << resolve_policy_value; |
| 225 return true; |
| 226 } |
| 227 |
187 void ApplyTimeZone(const TimeZoneResponseData* timezone) { | 228 void ApplyTimeZone(const TimeZoneResponseData* timezone) { |
188 if (!g_browser_process->platform_part() | 229 if (!g_browser_process->platform_part() |
189 ->GetTimezoneResolverManager() | 230 ->GetTimezoneResolverManager() |
190 ->ShouldApplyResolvedTimezone()) { | 231 ->ShouldApplyResolvedTimezone()) { |
191 return; | 232 return; |
192 } | 233 } |
193 | 234 |
194 if (!timezone->timeZoneId.empty()) { | 235 if (!timezone->timeZoneId.empty()) { |
195 VLOG(1) << "Refresh TimeZone: setting timezone to '" << timezone->timeZoneId | 236 VLOG(1) << "Refresh TimeZone: setting timezone to '" << timezone->timeZoneId |
196 << "'"; | 237 << "'"; |
197 | 238 |
198 chromeos::system::TimezoneSettings::GetInstance()->SetTimezoneFromID( | 239 if (PerUserTimezoneEnabled()) { |
199 base::UTF8ToUTF16(timezone->timeZoneId)); | 240 const user_manager::UserManager* user_manager = |
| 241 user_manager::UserManager::Get(); |
| 242 const user_manager::User* primary_user = user_manager->GetPrimaryUser(); |
| 243 |
| 244 if (primary_user) { |
| 245 Profile* profile = ProfileHelper::Get()->GetProfileByUser(primary_user); |
| 246 profile->GetPrefs()->SetString(prefs::kUserTimezone, |
| 247 timezone->timeZoneId); |
| 248 // chromeos::Preferences::ApplyPreferences() will automatically change |
| 249 // system timezone because user is primary. |
| 250 } else { |
| 251 SetSystemAndSigninScreenTimezone(timezone->timeZoneId); |
| 252 } |
| 253 } else { |
| 254 chromeos::system::TimezoneSettings::GetInstance()->SetTimezoneFromID( |
| 255 base::UTF8ToUTF16(timezone->timeZoneId)); |
| 256 } |
200 } | 257 } |
201 } | 258 } |
202 | 259 |
| 260 void UpdateSystemTimezone(Profile* profile) { |
| 261 if (IsTimezonePrefsManaged(prefs::kUserTimezone)) { |
| 262 VLOG(1) << "Ignoring user timezone change, because timezone is enterprise " |
| 263 "managed."; |
| 264 return; |
| 265 } |
| 266 |
| 267 const user_manager::UserManager* user_manager = |
| 268 user_manager::UserManager::Get(); |
| 269 const user_manager::User* user = |
| 270 ProfileHelper::Get()->GetUserByProfile(profile); |
| 271 |
| 272 const AccountId owner(user_manager->GetOwnerAccountId()); |
| 273 const bool user_is_owner = |
| 274 owner.is_valid() && (owner == user->GetAccountId()); |
| 275 |
| 276 const std::string value = |
| 277 profile->GetPrefs()->GetString(prefs::kUserTimezone); |
| 278 if (user_is_owner) { |
| 279 g_browser_process->local_state()->SetString(prefs::kSigninScreenTimezone, |
| 280 value); |
| 281 } |
| 282 |
| 283 if (user_manager->GetPrimaryUser() == user && PerUserTimezoneEnabled()) |
| 284 CrosSettings::Get()->SetString(kSystemTimezone, value); |
| 285 } |
| 286 |
| 287 void SetSystemAndSigninScreenTimezone(const std::string& timezone) { |
| 288 if (timezone.empty()) |
| 289 return; |
| 290 |
| 291 g_browser_process->local_state()->SetString(prefs::kSigninScreenTimezone, |
| 292 timezone); |
| 293 |
| 294 std::string current_timezone_id; |
| 295 CrosSettings::Get()->GetString(kSystemTimezone, ¤t_timezone_id); |
| 296 if (current_timezone_id != timezone) { |
| 297 system::TimezoneSettings::GetInstance()->SetTimezoneFromID( |
| 298 base::UTF8ToUTF16(timezone)); |
| 299 } |
| 300 } |
| 301 |
| 302 bool PerUserTimezoneEnabled() { |
| 303 return !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 304 switches::kDisablePerUserTimezone); |
| 305 } |
| 306 |
203 } // namespace system | 307 } // namespace system |
204 } // namespace chromeos | 308 } // namespace chromeos |
OLD | NEW |