Chromium Code Reviews| Index: chrome/browser/chromeos/system/timezone_resolver_manager.cc |
| diff --git a/chrome/browser/chromeos/system/timezone_resolver_manager.cc b/chrome/browser/chromeos/system/timezone_resolver_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..672b48a2b20b18ca15a72c6c2e46f7f199c19bd8 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/system/timezone_resolver_manager.cc |
| @@ -0,0 +1,186 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/system/timezone_resolver_manager.h" |
| + |
| +#include "base/command_line.h" |
| +#include "base/logging.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chromeos/preferences.h" |
| +#include "chrome/browser/chromeos/system/timezone_util.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chromeos/chromeos_switches.h" |
| +#include "components/prefs/pref_service.h" |
| + |
| +namespace chromeos { |
| +namespace system { |
| + |
| +namespace { |
| + |
| +// This is the result of several methods calculating configured |
| +// time zone resolve process. |
|
stevenjb
2016/03/28 23:23:17
nit: processes
Alexander Alekseev
2016/03/29 00:29:25
Done.
|
| +enum ServiceConfiguration { |
| + UNSPECIFIED = 0, // Try another configuration source. |
| + SHOULD_START = 1, // This source requires service Start. |
| + SHOULD_STOP = 2, // This source requires service Stop. |
| +}; |
| + |
| +// Starts or stops TimezoneResolver if required by |
| +// SystemTimezoneAutomaticDetectionPolicy. |
| +// Returns SHOULD_* if timezone resolver status is controlled by this policy. |
| +ServiceConfiguration GetServiceConfigurationFromAutomaticDetectionPolicy() { |
| + if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + chromeos::switches::kEnableSystemTimezoneAutomaticDetectionPolicy)) { |
| + return UNSPECIFIED; |
| + } |
| + |
| + PrefService* local_state = g_browser_process->local_state(); |
| + const bool is_managed = local_state->IsManagedPreference( |
| + prefs::kSystemTimezoneAutomaticDetectionPolicy); |
| + if (!is_managed) |
| + return UNSPECIFIED; |
| + |
| + int policy_value = |
| + local_state->GetInteger(prefs::kSystemTimezoneAutomaticDetectionPolicy); |
| + |
| + DCHECK(policy_value < |
| + ResolveDeviceTimezoneByGeolocationPolicyValues::NUM_ELEMENTS); |
|
stevenjb
2016/03/28 23:23:17
Instead, before the default return add:
NOTREACHED
Alexander Alekseev
2016/03/29 00:29:25
Done.
|
| + |
| + if (policy_value == |
| + ResolveDeviceTimezoneByGeolocationPolicyValues::USERS_DECIDE) { |
| + return UNSPECIFIED; |
| + } |
| + |
| + if (policy_value == |
| + ResolveDeviceTimezoneByGeolocationPolicyValues::DISABLED) { |
| + return SHOULD_STOP; |
| + } else if (policy_value == |
| + ResolveDeviceTimezoneByGeolocationPolicyValues::IP_ONLY) { |
| + return SHOULD_START; |
| + } else if (policy_value == ResolveDeviceTimezoneByGeolocationPolicyValues:: |
| + SEND_WIFI_ACCESS_POINTS) { |
| + return SHOULD_START; |
| + } |
| + // Default for unknown policy value. |
| + return SHOULD_STOP; |
| +} |
| + |
| +// Stops TimezoneResolver if SystemTimezonePolicy is applied. |
| +// Returns SHOULD_* if timezone resolver status is controlled by this policy. |
| +ServiceConfiguration GetServiceConfigurationFromSystemTimezonePolicy() { |
| + if (!HasSystemTimezonePolicy()) |
| + return UNSPECIFIED; |
| + |
| + return SHOULD_STOP; |
| +} |
| + |
| +// Starts or stops TimezoneResolver if required by policy. |
| +// Returns true if timezone resolver status is controlled by policy. |
| +ServiceConfiguration GetServiceConfigurationFromPolicy() { |
| + ServiceConfiguration result = |
| + GetServiceConfigurationFromSystemTimezonePolicy(); |
| + |
| + if (result != UNSPECIFIED) |
| + return result; |
| + |
| + result = GetServiceConfigurationFromAutomaticDetectionPolicy(); |
| + return result; |
| +} |
| + |
| +// Returns service configuration for the user. |
| +ServiceConfiguration GetServiceConfigurationFromUserPrefs( |
| + PrefService* user_prefs) { |
| + const bool value = |
| + user_prefs->GetBoolean(prefs::kResolveTimezoneByGeolocation); |
| + if (value) { |
| + return SHOULD_START; |
| + } |
|
stevenjb
2016/03/28 23:23:17
nit: no {}
Alexander Alekseev
2016/03/29 00:29:25
Done.
|
| + return SHOULD_STOP; |
| +} |
| + |
| +// Returns service configuration for the signin screen. |
| +ServiceConfiguration GetServiceConfigurationForSigninScreen() { |
| + if (!g_browser_process->local_state()->GetBoolean( |
| + prefs::kResolveDeviceTimezoneByGeolocation)) { |
| + return SHOULD_START; |
| + } |
| + |
| + // Do not start resolver if we are inside active user session. |
| + // If user preferences permit, it will be started on preferences |
| + // initialization. |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kLoginUser)) |
| + return SHOULD_STOP; |
| + |
| + return SHOULD_START; |
| +} |
| + |
| +} // anonymous namespace. |
| + |
| +TimeZoneResolverManager::TimeZoneResolverManager() |
| + : primary_user_prefs_(nullptr) { |
| + local_state_pref_change_registrar_.Init(g_browser_process->local_state()); |
| + local_state_pref_change_registrar_.Add( |
| + prefs::kSystemTimezoneAutomaticDetectionPolicy, |
| + base::Bind( |
| + &::chromeos::system::TimeZoneResolverManager::UpdateTimezoneResolver, |
| + base::Unretained(this))); |
| +} |
| + |
| +TimeZoneResolverManager::~TimeZoneResolverManager() {} |
| + |
| +void TimeZoneResolverManager::SetPrimaryUserPrefs(PrefService* pref_service) { |
| + primary_user_prefs_ = pref_service; |
| +} |
| + |
| +bool TimeZoneResolverManager::ShouldSendWiFiGeolocationData() { |
| + PrefService* local_state = g_browser_process->local_state(); |
| + const bool is_managed = local_state->IsManagedPreference( |
| + prefs::kSystemTimezoneAutomaticDetectionPolicy); |
| + if (!is_managed) |
| + return false; |
| + |
| + int policy_value = |
| + local_state->GetInteger(prefs::kSystemTimezoneAutomaticDetectionPolicy); |
| + |
| + DCHECK(policy_value < |
| + ResolveDeviceTimezoneByGeolocationPolicyValues::NUM_ELEMENTS); |
| + |
| + return policy_value == ResolveDeviceTimezoneByGeolocationPolicyValues:: |
| + SEND_WIFI_ACCESS_POINTS; |
| +} |
| + |
| +void TimeZoneResolverManager::UpdateTimezoneResolver() { |
| + if (IsTimeZoneResolverShouldBeRunnig()) { |
| + g_browser_process->platform_part()->GetTimezoneResolver()->Start(); |
| + } else { |
| + g_browser_process->platform_part()->GetTimezoneResolver()->Stop(); |
| + } |
|
stevenjb
2016/03/28 23:23:17
nit: no {}
Alexander Alekseev
2016/03/29 00:29:25
Done.
|
| +} |
| + |
| +bool TimeZoneResolverManager::IfShouldApplyResolvedTimezone() { |
| + return IsTimeZoneResolverShouldBeRunnig(); |
| +} |
| + |
| +bool TimeZoneResolverManager::IsTimeZoneResolverShouldBeRunnig() { |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + chromeos::switches::kDisableTimeZoneTrackingOption)) { |
| + return false; |
| + } |
| + ServiceConfiguration result; |
| + |
| + result = GetServiceConfigurationFromPolicy(); |
| + |
| + if (result == UNSPECIFIED) { |
| + if (primary_user_prefs_) { |
| + result = GetServiceConfigurationFromUserPrefs(primary_user_prefs_); |
| + } else { |
| + // We are on a signin page. |
| + result = GetServiceConfigurationForSigninScreen(); |
| + } |
| + } |
| + return result == SHOULD_START; |
| +} |
| + |
| +} // namespace system |
| +} // namespace chromeos |