1 // 2 // Copyright (C) 2014 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_ 18 #define UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_ 19 20 #include <set> 21 #include <string> 22 23 #include <brillo/message_loops/message_loop.h> 24 #include <gtest/gtest_prod.h> // for FRIEND_TEST 25 #include <policy/libpolicy.h> 26 #include <session_manager/dbus-proxies.h> 27 28 #include "update_engine/update_manager/device_policy_provider.h" 29 #include "update_engine/update_manager/generic_variables.h" 30 31 namespace chromeos_update_manager { 32 33 // DevicePolicyProvider concrete implementation. 34 class RealDevicePolicyProvider : public DevicePolicyProvider { 35 public: RealDevicePolicyProvider(org::chromium::SessionManagerInterfaceProxyInterface * session_manager_proxy,policy::PolicyProvider * policy_provider)36 RealDevicePolicyProvider(org::chromium::SessionManagerInterfaceProxyInterface* 37 session_manager_proxy, 38 policy::PolicyProvider* policy_provider) 39 : policy_provider_(policy_provider), 40 session_manager_proxy_(session_manager_proxy) {} 41 ~RealDevicePolicyProvider(); 42 43 // Initializes the provider and returns whether it succeeded. 44 bool Init(); 45 var_device_policy_is_loaded()46 Variable<bool>* var_device_policy_is_loaded() override { 47 return &var_device_policy_is_loaded_; 48 } 49 var_release_channel()50 Variable<std::string>* var_release_channel() override { 51 return &var_release_channel_; 52 } 53 var_release_channel_delegated()54 Variable<bool>* var_release_channel_delegated() override { 55 return &var_release_channel_delegated_; 56 } 57 var_update_disabled()58 Variable<bool>* var_update_disabled() override { 59 return &var_update_disabled_; 60 } 61 var_target_version_prefix()62 Variable<std::string>* var_target_version_prefix() override { 63 return &var_target_version_prefix_; 64 } 65 var_scatter_factor()66 Variable<base::TimeDelta>* var_scatter_factor() override { 67 return &var_scatter_factor_; 68 } 69 70 Variable<std::set<ConnectionType>>* var_allowed_connection_types_for_update()71 var_allowed_connection_types_for_update() override { 72 return &var_allowed_connection_types_for_update_; 73 } 74 var_owner()75 Variable<std::string>* var_owner() override { 76 return &var_owner_; 77 } 78 var_http_downloads_enabled()79 Variable<bool>* var_http_downloads_enabled() override { 80 return &var_http_downloads_enabled_; 81 } 82 var_au_p2p_enabled()83 Variable<bool>* var_au_p2p_enabled() override { 84 return &var_au_p2p_enabled_; 85 } 86 87 private: 88 FRIEND_TEST(UmRealDevicePolicyProviderTest, RefreshScheduledTest); 89 FRIEND_TEST(UmRealDevicePolicyProviderTest, NonExistentDevicePolicyReloaded); 90 FRIEND_TEST(UmRealDevicePolicyProviderTest, ValuesUpdated); 91 92 // A static handler for the PropertyChangedCompleted signal from the session 93 // manager used as a callback. 94 void OnPropertyChangedCompletedSignal(const std::string& success); 95 96 // Called when the signal in UpdateEngineLibcrosProxyResolvedInterface is 97 // connected. 98 void OnSignalConnected(const std::string& interface_name, 99 const std::string& signal_name, 100 bool successful); 101 102 // Schedules a call to periodically refresh the device policy. 103 void RefreshDevicePolicyAndReschedule(); 104 105 // Reloads the device policy and updates all the exposed variables. 106 void RefreshDevicePolicy(); 107 108 // Updates the async variable |var| based on the result value of the method 109 // passed, which is a DevicePolicy getter method. 110 template<typename T> 111 void UpdateVariable(AsyncCopyVariable<T>* var, 112 bool (policy::DevicePolicy::*getter_method)(T*) const); 113 114 // Updates the async variable |var| based on the result value of the getter 115 // method passed, which is a wrapper getter on this class. 116 template<typename T> 117 void UpdateVariable( 118 AsyncCopyVariable<T>* var, 119 bool (RealDevicePolicyProvider::*getter_method)(T*) const); 120 121 // Wrapper for DevicePolicy::GetScatterFactorInSeconds() that converts the 122 // result to a base::TimeDelta. It returns the same value as 123 // GetScatterFactorInSeconds(). 124 bool ConvertScatterFactor(base::TimeDelta* scatter_factor) const; 125 126 // Wrapper for DevicePolicy::GetAllowedConnectionTypesForUpdate() that 127 // converts the result to a set of ConnectionType elements instead of strings. 128 bool ConvertAllowedConnectionTypesForUpdate( 129 std::set<ConnectionType>* allowed_types) const; 130 131 // Used for fetching information about the device policy. 132 policy::PolicyProvider* policy_provider_; 133 134 // Used to schedule refreshes of the device policy. 135 brillo::MessageLoop::TaskId scheduled_refresh_{ 136 brillo::MessageLoop::kTaskIdNull}; 137 138 // The DBus (mockable) session manager proxy, owned by the caller. 139 org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy_{ 140 nullptr}; 141 142 // Variable exposing whether the policy is loaded. 143 AsyncCopyVariable<bool> var_device_policy_is_loaded_{ 144 "policy_is_loaded", false}; 145 146 // Variables mapping the exposed methods from the policy::DevicePolicy. 147 AsyncCopyVariable<std::string> var_release_channel_{"release_channel"}; 148 AsyncCopyVariable<bool> var_release_channel_delegated_{ 149 "release_channel_delegated"}; 150 AsyncCopyVariable<bool> var_update_disabled_{"update_disabled"}; 151 AsyncCopyVariable<std::string> var_target_version_prefix_{ 152 "target_version_prefix"}; 153 AsyncCopyVariable<base::TimeDelta> var_scatter_factor_{"scatter_factor"}; 154 AsyncCopyVariable<std::set<ConnectionType>> 155 var_allowed_connection_types_for_update_{ 156 "allowed_connection_types_for_update"}; 157 AsyncCopyVariable<std::string> var_owner_{"owner"}; 158 AsyncCopyVariable<bool> var_http_downloads_enabled_{"http_downloads_enabled"}; 159 AsyncCopyVariable<bool> var_au_p2p_enabled_{"au_p2p_enabled"}; 160 161 DISALLOW_COPY_AND_ASSIGN(RealDevicePolicyProvider); 162 }; 163 164 } // namespace chromeos_update_manager 165 166 #endif // UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_ 167