Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/policy_manager/real_device_policy_provider.h" |
| 6 | |
| 7 | #include <base/logging.h> |
| 8 | #include <base/time/time.h> |
| 9 | #include <policy/device_policy.h> |
| 10 | |
| 11 | #include "update_engine/policy_manager/generic_variables.h" |
| 12 | #include "update_engine/policy_manager/real_shill_provider.h" |
| 13 | #include "update_engine/utils.h" |
| 14 | |
| 15 | using base::TimeDelta; |
| 16 | using policy::DevicePolicy; |
| 17 | using std::set; |
| 18 | using std::string; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | const int kDevicePolicyRefreshRateInMinutes = 60; |
| 23 | |
| 24 | } // namespace |
| 25 | |
| 26 | namespace chromeos_policy_manager { |
| 27 | |
| 28 | RealDevicePolicyProvider::~RealDevicePolicyProvider() { |
| 29 | CancelMainLoopEvent(scheduled_refresh_); |
| 30 | } |
| 31 | |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame^] | 32 | bool RealDevicePolicyProvider::Init() { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 33 | CHECK(policy_provider_ != nullptr); |
| 34 | |
| 35 | // On Init() we try to get the device policy and keep updating it. |
| 36 | RefreshDevicePolicyAndReschedule(); |
| 37 | |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | void RealDevicePolicyProvider::RefreshDevicePolicyAndReschedule() { |
| 42 | RefreshDevicePolicy(); |
| 43 | scheduled_refresh_ = RunFromMainLoopAfterTimeout( |
| 44 | base::Bind(&RealDevicePolicyProvider::RefreshDevicePolicyAndReschedule, |
| 45 | base::Unretained(this)), |
| 46 | TimeDelta::FromMinutes(kDevicePolicyRefreshRateInMinutes)); |
| 47 | } |
| 48 | |
| 49 | template<typename T> |
| 50 | void RealDevicePolicyProvider::UpdateVariable( |
| 51 | AsyncCopyVariable<T>* var, |
| 52 | bool (policy::DevicePolicy::*getter_method)(T*) const) { |
| 53 | T new_value; |
| 54 | if (policy_provider_->device_policy_is_loaded() && |
| 55 | (policy_provider_->GetDevicePolicy().*getter_method)(&new_value)) { |
| 56 | var->SetValue(new_value); |
| 57 | } else { |
| 58 | var->UnsetValue(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | template<typename T> |
| 63 | void RealDevicePolicyProvider::UpdateVariable( |
| 64 | AsyncCopyVariable<T>* var, |
| 65 | bool (RealDevicePolicyProvider::*getter_method)(T*) const) { |
| 66 | T new_value; |
| 67 | if (policy_provider_->device_policy_is_loaded() && |
| 68 | (this->*getter_method)(&new_value)) { |
| 69 | var->SetValue(new_value); |
| 70 | } else { |
| 71 | var->UnsetValue(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | bool RealDevicePolicyProvider::ConvertAllowedConnectionTypesForUpdate( |
| 76 | set<ConnectionType>* allowed_types) const { |
| 77 | set<string> allowed_types_str; |
| 78 | if (!policy_provider_->GetDevicePolicy() |
| 79 | .GetAllowedConnectionTypesForUpdate(&allowed_types_str)) { |
| 80 | return false; |
| 81 | } |
| 82 | allowed_types->clear(); |
| 83 | for (auto& type_str : allowed_types_str) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 84 | ConnectionType type = |
| 85 | RealShillProvider::ParseConnectionType(type_str.c_str()); |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 86 | if (type != ConnectionType::kUnknown) { |
| 87 | allowed_types->insert(type); |
| 88 | } else { |
| 89 | LOG(WARNING) << "Policy includes unknown connection type: " << type_str; |
| 90 | } |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | bool RealDevicePolicyProvider::ConvertScatterFactor( |
| 96 | base::TimeDelta* scatter_factor) const { |
| 97 | int64 scatter_factor_in_seconds; |
| 98 | if (!policy_provider_->GetDevicePolicy().GetScatterFactorInSeconds( |
| 99 | &scatter_factor_in_seconds)) { |
| 100 | return false; |
| 101 | } |
| 102 | if (scatter_factor_in_seconds < 0) { |
| 103 | LOG(WARNING) << "Ignoring negative scatter factor: " |
| 104 | << scatter_factor_in_seconds; |
| 105 | return false; |
| 106 | } |
| 107 | *scatter_factor = base::TimeDelta::FromSeconds(scatter_factor_in_seconds); |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | void RealDevicePolicyProvider::RefreshDevicePolicy() { |
| 112 | if (!policy_provider_->Reload()) { |
| 113 | LOG(INFO) << "No device policies/settings present."; |
| 114 | } |
| 115 | |
| 116 | var_device_policy_is_loaded_.SetValue( |
| 117 | policy_provider_->device_policy_is_loaded()); |
| 118 | |
| 119 | UpdateVariable(&var_release_channel_, &DevicePolicy::GetReleaseChannel); |
| 120 | UpdateVariable(&var_release_channel_delegated_, |
| 121 | &DevicePolicy::GetReleaseChannelDelegated); |
| 122 | UpdateVariable(&var_update_disabled_, &DevicePolicy::GetUpdateDisabled); |
| 123 | UpdateVariable(&var_target_version_prefix_, |
| 124 | &DevicePolicy::GetTargetVersionPrefix); |
| 125 | UpdateVariable(&var_scatter_factor_, |
| 126 | &RealDevicePolicyProvider::ConvertScatterFactor); |
| 127 | UpdateVariable( |
| 128 | &var_allowed_connection_types_for_update_, |
| 129 | &RealDevicePolicyProvider::ConvertAllowedConnectionTypesForUpdate); |
| 130 | UpdateVariable(&var_get_owner_, &DevicePolicy::GetOwner); |
| 131 | UpdateVariable(&var_http_downloads_enabled_, |
| 132 | &DevicePolicy::GetHttpDownloadsEnabled); |
| 133 | UpdateVariable(&var_au_p2p_enabled_, &DevicePolicy::GetAuP2PEnabled); |
| 134 | } |
| 135 | |
| 136 | } // namespace chromeos_policy_manager |