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/browser/profiles/profile_manager.h" |
| 30 #include "chrome/common/pref_names.h" |
27 #include "chrome/grit/generated_resources.h" | 31 #include "chrome/grit/generated_resources.h" |
| 32 #include "chromeos/chromeos_switches.h" |
28 #include "chromeos/settings/timezone_settings.h" | 33 #include "chromeos/settings/timezone_settings.h" |
29 #include "chromeos/timezone/timezone_request.h" | 34 #include "chromeos/timezone/timezone_request.h" |
30 #include "components/prefs/pref_service.h" | 35 #include "components/prefs/pref_service.h" |
| 36 #include "components/user_manager/user.h" |
31 #include "components/user_manager/user_manager.h" | 37 #include "components/user_manager/user_manager.h" |
32 #include "third_party/icu/source/common/unicode/ures.h" | 38 #include "third_party/icu/source/common/unicode/ures.h" |
33 #include "third_party/icu/source/common/unicode/utypes.h" | 39 #include "third_party/icu/source/common/unicode/utypes.h" |
34 #include "third_party/icu/source/i18n/unicode/calendar.h" | 40 #include "third_party/icu/source/i18n/unicode/calendar.h" |
35 #include "third_party/icu/source/i18n/unicode/timezone.h" | 41 #include "third_party/icu/source/i18n/unicode/timezone.h" |
36 #include "ui/base/l10n/l10n_util.h" | 42 #include "ui/base/l10n/l10n_util.h" |
37 | 43 |
38 namespace { | 44 namespace { |
39 | 45 |
40 struct UResClose { | 46 struct UResClose { |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 if (chromeos::CrosSettings::Get()->GetString(chromeos::kSystemTimezonePolicy, | 183 if (chromeos::CrosSettings::Get()->GetString(chromeos::kSystemTimezonePolicy, |
178 &policy_timezone) && | 184 &policy_timezone) && |
179 !policy_timezone.empty()) { | 185 !policy_timezone.empty()) { |
180 VLOG(1) << "Refresh TimeZone: TimeZone settings are overridden" | 186 VLOG(1) << "Refresh TimeZone: TimeZone settings are overridden" |
181 << " by DevicePolicy."; | 187 << " by DevicePolicy."; |
182 return true; | 188 return true; |
183 } | 189 } |
184 return false; | 190 return false; |
185 } | 191 } |
186 | 192 |
| 193 bool IsTimezonePrefsManaged(const std::string& pref_name) { |
| 194 DCHECK(pref_name == prefs::kUserTimezone || |
| 195 pref_name == prefs::kResolveTimezoneByGeolocation); |
| 196 |
| 197 std::string policy_timezone; |
| 198 if (chromeos::CrosSettings::Get()->GetString(chromeos::kSystemTimezonePolicy, |
| 199 &policy_timezone) && |
| 200 !policy_timezone.empty()) { |
| 201 return true; |
| 202 } |
| 203 |
| 204 const PrefService* local_state = g_browser_process->local_state(); |
| 205 if (!local_state->IsManagedPreference( |
| 206 prefs::kSystemTimezoneAutomaticDetectionPolicy)) { |
| 207 return false; |
| 208 } |
| 209 |
| 210 int resolve_policy_value = |
| 211 local_state->GetInteger(prefs::kSystemTimezoneAutomaticDetectionPolicy); |
| 212 |
| 213 switch (resolve_policy_value) { |
| 214 case enterprise_management::SystemTimezoneProto::USERS_DECIDE: |
| 215 return false; |
| 216 case enterprise_management::SystemTimezoneProto::DISABLED: |
| 217 // This only disables resolving. |
| 218 return pref_name == prefs::kResolveTimezoneByGeolocation; |
| 219 case enterprise_management::SystemTimezoneProto::IP_ONLY: |
| 220 case enterprise_management::SystemTimezoneProto::SEND_WIFI_ACCESS_POINTS: |
| 221 case enterprise_management::SystemTimezoneProto::SEND_ALL_LOCATION_INFO: |
| 222 return true; |
| 223 } |
| 224 // Default for unknown policy value. |
| 225 NOTREACHED() << "Unrecognized policy value: " << resolve_policy_value; |
| 226 return true; |
| 227 } |
| 228 |
187 void ApplyTimeZone(const TimeZoneResponseData* timezone) { | 229 void ApplyTimeZone(const TimeZoneResponseData* timezone) { |
188 if (!g_browser_process->platform_part() | 230 if (!g_browser_process->platform_part() |
189 ->GetTimezoneResolverManager() | 231 ->GetTimezoneResolverManager() |
190 ->ShouldApplyResolvedTimezone()) { | 232 ->ShouldApplyResolvedTimezone()) { |
191 return; | 233 return; |
192 } | 234 } |
193 | 235 |
194 if (!timezone->timeZoneId.empty()) { | 236 if (!timezone->timeZoneId.empty()) { |
195 VLOG(1) << "Refresh TimeZone: setting timezone to '" << timezone->timeZoneId | 237 VLOG(1) << "Refresh TimeZone: setting timezone to '" << timezone->timeZoneId |
196 << "'"; | 238 << "'"; |
197 | 239 |
198 chromeos::system::TimezoneSettings::GetInstance()->SetTimezoneFromID( | 240 if (PerUserTimezoneEnabled()) { |
199 base::UTF8ToUTF16(timezone->timeZoneId)); | 241 const user_manager::UserManager* user_manager = |
| 242 user_manager::UserManager::Get(); |
| 243 const user_manager::User* primary_user = user_manager->GetPrimaryUser(); |
| 244 |
| 245 if (primary_user) { |
| 246 Profile* profile = ProfileHelper::Get()->GetProfileByUser(primary_user); |
| 247 profile->GetPrefs()->SetString(prefs::kUserTimezone, |
| 248 timezone->timeZoneId); |
| 249 // chromeos::Preferences::ApplyPreferences() will automatically change |
| 250 // system timezone because user is primary. |
| 251 } else { |
| 252 SetSystemAndSigninScreenTimezone(timezone->timeZoneId); |
| 253 } |
| 254 } else { |
| 255 chromeos::system::TimezoneSettings::GetInstance()->SetTimezoneFromID( |
| 256 base::UTF8ToUTF16(timezone->timeZoneId)); |
| 257 } |
200 } | 258 } |
201 } | 259 } |
202 | 260 |
| 261 void UpdateSystemTimezone(Profile* profile) { |
| 262 if (IsTimezonePrefsManaged(prefs::kUserTimezone)) { |
| 263 VLOG(1) << "Ignoring user timezone change, because timezone is enterprise " |
| 264 "managed."; |
| 265 return; |
| 266 } |
| 267 |
| 268 const user_manager::UserManager* user_manager = |
| 269 user_manager::UserManager::Get(); |
| 270 const user_manager::User* user = |
| 271 ProfileHelper::Get()->GetUserByProfile(profile); |
| 272 |
| 273 const AccountId owner(user_manager->GetOwnerAccountId()); |
| 274 const bool user_is_owner = |
| 275 owner.is_valid() && (owner == user->GetAccountId()); |
| 276 |
| 277 const std::string value = |
| 278 profile->GetPrefs()->GetString(prefs::kUserTimezone); |
| 279 if (user_is_owner) { |
| 280 g_browser_process->local_state()->SetString(prefs::kSigninScreenTimezone, |
| 281 value); |
| 282 } |
| 283 |
| 284 if (user_manager->GetPrimaryUser() == user && PerUserTimezoneEnabled()) |
| 285 CrosSettings::Get()->SetString(kSystemTimezone, value); |
| 286 } |
| 287 |
| 288 void SetSystemAndSigninScreenTimezone(const std::string& timezone) { |
| 289 if (timezone.empty()) |
| 290 return; |
| 291 |
| 292 g_browser_process->local_state()->SetString(prefs::kSigninScreenTimezone, |
| 293 timezone); |
| 294 |
| 295 std::string current_timezone_id; |
| 296 CrosSettings::Get()->GetString(kSystemTimezone, ¤t_timezone_id); |
| 297 if (current_timezone_id != timezone) { |
| 298 system::TimezoneSettings::GetInstance()->SetTimezoneFromID( |
| 299 base::UTF8ToUTF16(timezone)); |
| 300 } |
| 301 } |
| 302 |
| 303 bool PerUserTimezoneEnabled() { |
| 304 return !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 305 switches::kDisablePerUserTimezone); |
| 306 } |
| 307 |
| 308 void SetTimezoneFromUI(Profile* profile, const std::string& timezone_id) { |
| 309 if (!PerUserTimezoneEnabled()) { |
| 310 CrosSettings::Get()->SetString(kSystemTimezone, timezone_id); |
| 311 return; |
| 312 } |
| 313 |
| 314 if (ProfileHelper::IsSigninProfile(profile)) { |
| 315 SetSystemAndSigninScreenTimezone(timezone_id); |
| 316 return; |
| 317 } |
| 318 |
| 319 if (ProfileHelper::IsEphemeralUserProfile(profile)) { |
| 320 CrosSettings::Get()->SetString(kSystemTimezone, timezone_id); |
| 321 return; |
| 322 } |
| 323 |
| 324 Profile* primary_profile = ProfileManager::GetPrimaryUserProfile(); |
| 325 if (primary_profile && profile->IsSameProfile(primary_profile)) { |
| 326 profile->GetPrefs()->SetString(prefs::kUserTimezone, timezone_id); |
| 327 } |
| 328 // Time zone UI should be blocked for non-primary users. |
| 329 NOTREACHED(); |
| 330 } |
| 331 |
203 } // namespace system | 332 } // namespace system |
204 } // namespace chromeos | 333 } // namespace chromeos |
OLD | NEW |