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 <memory> 21 #include <set> 22 #include <string> 23 #include <utility> 24 25 #include <brillo/message_loops/message_loop.h> 26 #include <gtest/gtest_prod.h> // for FRIEND_TEST 27 #include <policy/libpolicy.h> 28 #if USE_DBUS 29 #include <session_manager/dbus-proxies.h> 30 #endif // USE_DBUS 31 32 #include "update_engine/update_manager/device_policy_provider.h" 33 #include "update_engine/update_manager/generic_variables.h" 34 35 namespace chromeos_update_manager { 36 37 // |DevicePolicyProvider| concrete implementation. 38 class RealDevicePolicyProvider : public DevicePolicyProvider { 39 public: 40 #if USE_DBUS RealDevicePolicyProvider(std::unique_ptr<org::chromium::SessionManagerInterfaceProxyInterface> session_manager_proxy,policy::PolicyProvider * policy_provider)41 RealDevicePolicyProvider( 42 std::unique_ptr<org::chromium::SessionManagerInterfaceProxyInterface> 43 session_manager_proxy, 44 policy::PolicyProvider* policy_provider) 45 : policy_provider_(policy_provider), 46 session_manager_proxy_(std::move(session_manager_proxy)) {} 47 #endif // USE_DBUS RealDevicePolicyProvider(policy::PolicyProvider * policy_provider)48 explicit RealDevicePolicyProvider(policy::PolicyProvider* policy_provider) 49 : policy_provider_(policy_provider) {} 50 ~RealDevicePolicyProvider(); 51 52 // Initializes the provider and returns whether it succeeded. 53 bool Init(); 54 var_device_policy_is_loaded()55 Variable<bool>* var_device_policy_is_loaded() override { 56 return &var_device_policy_is_loaded_; 57 } 58 var_release_channel()59 Variable<std::string>* var_release_channel() override { 60 return &var_release_channel_; 61 } 62 var_release_channel_delegated()63 Variable<bool>* var_release_channel_delegated() override { 64 return &var_release_channel_delegated_; 65 } 66 var_release_lts_tag()67 Variable<std::string>* var_release_lts_tag() override { 68 return &var_release_lts_tag_; 69 } 70 var_update_disabled()71 Variable<bool>* var_update_disabled() override { 72 return &var_update_disabled_; 73 } 74 var_target_version_prefix()75 Variable<std::string>* var_target_version_prefix() override { 76 return &var_target_version_prefix_; 77 } 78 var_rollback_to_target_version()79 Variable<RollbackToTargetVersion>* var_rollback_to_target_version() override { 80 return &var_rollback_to_target_version_; 81 } 82 var_rollback_allowed_milestones()83 Variable<int>* var_rollback_allowed_milestones() override { 84 return &var_rollback_allowed_milestones_; 85 } 86 var_scatter_factor()87 Variable<base::TimeDelta>* var_scatter_factor() override { 88 return &var_scatter_factor_; 89 } 90 91 Variable<std::set<chromeos_update_engine::ConnectionType>>* var_allowed_connection_types_for_update()92 var_allowed_connection_types_for_update() override { 93 return &var_allowed_connection_types_for_update_; 94 } 95 var_has_owner()96 Variable<bool>* var_has_owner() override { return &var_has_owner_; } 97 var_http_downloads_enabled()98 Variable<bool>* var_http_downloads_enabled() override { 99 return &var_http_downloads_enabled_; 100 } 101 var_au_p2p_enabled()102 Variable<bool>* var_au_p2p_enabled() override { return &var_au_p2p_enabled_; } 103 var_allow_kiosk_app_control_chrome_version()104 Variable<bool>* var_allow_kiosk_app_control_chrome_version() override { 105 return &var_allow_kiosk_app_control_chrome_version_; 106 } 107 var_auto_launched_kiosk_app_id()108 Variable<std::string>* var_auto_launched_kiosk_app_id() override { 109 return &var_auto_launched_kiosk_app_id_; 110 } 111 var_disallowed_time_intervals()112 Variable<WeeklyTimeIntervalVector>* var_disallowed_time_intervals() override { 113 return &var_disallowed_time_intervals_; 114 } 115 var_channel_downgrade_behavior()116 Variable<ChannelDowngradeBehavior>* var_channel_downgrade_behavior() 117 override { 118 return &var_channel_downgrade_behavior_; 119 } 120 var_device_minimum_version()121 Variable<base::Version>* var_device_minimum_version() override { 122 return &var_device_minimum_version_; 123 } 124 var_quick_fix_build_token()125 Variable<std::string>* var_quick_fix_build_token() override { 126 return &var_quick_fix_build_token_; 127 } 128 129 private: 130 FRIEND_TEST(UmRealDevicePolicyProviderTest, RefreshScheduledTest); 131 FRIEND_TEST(UmRealDevicePolicyProviderTest, NonExistentDevicePolicyReloaded); 132 FRIEND_TEST(UmRealDevicePolicyProviderTest, ValuesUpdated); 133 FRIEND_TEST(UmRealDevicePolicyProviderTest, HasOwnerConverted); 134 135 // A static handler for the |PropertyChangedCompleted| signal from the session 136 // manager used as a callback. 137 void OnPropertyChangedCompletedSignal(const std::string& success); 138 139 // Called when the signal in |UpdateEngineLibcrosProxyResolvedInterface| is 140 // connected. 141 void OnSignalConnected(const std::string& interface_name, 142 const std::string& signal_name, 143 bool successful); 144 145 // Schedules a call to periodically refresh the device policy. 146 void RefreshDevicePolicyAndReschedule(); 147 148 // Reloads the device policy and updates all the exposed variables. 149 void RefreshDevicePolicy(); 150 151 // Updates the async variable |var| based on the result value of the method 152 // passed, which is a DevicePolicy getter method. 153 template <typename T> 154 void UpdateVariable(AsyncCopyVariable<T>* var, 155 bool (policy::DevicePolicy::*getter)(T*) const); 156 157 // Updates the async variable |var| based on the result value of the getter 158 // method passed, which is a wrapper getter on this class. 159 template <typename T> 160 void UpdateVariable(AsyncCopyVariable<T>* var, 161 bool (RealDevicePolicyProvider::*getter)(T*) const); 162 163 // Wrapper for |DevicePolicy::GetRollbackToTargetVersion()| that converts the 164 // result to |RollbackToTargetVersion|. 165 bool ConvertRollbackToTargetVersion( 166 RollbackToTargetVersion* rollback_to_target_version) const; 167 168 // Wrapper for |DevicePolicy::GetScatterFactorInSeconds()| that converts the 169 // result to a |base::TimeDelta|. It returns the same value as 170 // |GetScatterFactorInSeconds()|. 171 bool ConvertScatterFactor(base::TimeDelta* scatter_factor) const; 172 173 // Wrapper for |DevicePolicy::GetAllowedConnectionTypesForUpdate()| that 174 // converts the result to a set of |ConnectionType| elements instead of 175 // strings. 176 bool ConvertAllowedConnectionTypesForUpdate( 177 std::set<chromeos_update_engine::ConnectionType>* allowed_types) const; 178 179 // Wrapper for |DevicePolicy::GetUpdateTimeRestrictions()| that converts 180 // the |DevicePolicy::WeeklyTimeInterval| structs to |WeeklyTimeInterval| 181 // objects, which offer more functionality. 182 bool ConvertDisallowedTimeIntervals( 183 WeeklyTimeIntervalVector* disallowed_intervals_out) const; 184 185 // Wrapper for |DevicePolicy::GetOwner()| that converts the result to a 186 // boolean of whether the device has an owner. (Enterprise enrolled 187 // devices do not have an owner). 188 bool ConvertHasOwner(bool* has_owner) const; 189 190 // Wrapper for |DevicePolicy::GetChannelDowngradeBehavior| that converts the 191 // result to |ChannelDowngradeBehavior|. 192 bool ConvertChannelDowngradeBehavior( 193 ChannelDowngradeBehavior* channel_downgrade_behavior) const; 194 195 // Used for fetching information about the device policy. 196 policy::PolicyProvider* policy_provider_; 197 198 // Used to schedule refreshes of the device policy. 199 brillo::MessageLoop::TaskId scheduled_refresh_{ 200 brillo::MessageLoop::kTaskIdNull}; 201 202 #if USE_DBUS 203 // The DBus (mockable) session manager proxy. 204 std::unique_ptr<org::chromium::SessionManagerInterfaceProxyInterface> 205 session_manager_proxy_; 206 #endif // USE_DBUS 207 208 // Variable exposing whether the policy is loaded. 209 AsyncCopyVariable<bool> var_device_policy_is_loaded_{"policy_is_loaded", 210 false}; 211 212 // Variables mapping the exposed methods from the |policy::DevicePolicy|. 213 AsyncCopyVariable<std::string> var_release_channel_{"release_channel"}; 214 AsyncCopyVariable<bool> var_release_channel_delegated_{ 215 "release_channel_delegated"}; 216 AsyncCopyVariable<std::string> var_release_lts_tag_{"release_lts_tag"}; 217 AsyncCopyVariable<bool> var_update_disabled_{"update_disabled"}; 218 AsyncCopyVariable<std::string> var_target_version_prefix_{ 219 "target_version_prefix"}; 220 AsyncCopyVariable<RollbackToTargetVersion> var_rollback_to_target_version_{ 221 "rollback_to_target_version"}; 222 AsyncCopyVariable<int> var_rollback_allowed_milestones_{ 223 "rollback_allowed_milestones"}; 224 AsyncCopyVariable<base::TimeDelta> var_scatter_factor_{"scatter_factor"}; 225 AsyncCopyVariable<std::set<chromeos_update_engine::ConnectionType>> 226 var_allowed_connection_types_for_update_{ 227 "allowed_connection_types_for_update"}; 228 AsyncCopyVariable<bool> var_has_owner_{"owner"}; 229 AsyncCopyVariable<bool> var_http_downloads_enabled_{"http_downloads_enabled"}; 230 AsyncCopyVariable<bool> var_au_p2p_enabled_{"au_p2p_enabled"}; 231 AsyncCopyVariable<bool> var_allow_kiosk_app_control_chrome_version_{ 232 "allow_kiosk_app_control_chrome_version"}; 233 AsyncCopyVariable<WeeklyTimeIntervalVector> var_disallowed_time_intervals_{ 234 "update_time_restrictions"}; 235 AsyncCopyVariable<std::string> var_auto_launched_kiosk_app_id_{ 236 "auto_launched_kiosk_app_id"}; 237 AsyncCopyVariable<ChannelDowngradeBehavior> var_channel_downgrade_behavior_{ 238 "channel_downgrade_behavior"}; 239 AsyncCopyVariable<base::Version> var_device_minimum_version_{ 240 "device_minimum_version"}; 241 AsyncCopyVariable<std::string> var_quick_fix_build_token_{ 242 "quick_fix_build_token"}; 243 244 DISALLOW_COPY_AND_ASSIGN(RealDevicePolicyProvider); 245 }; 246 247 } // namespace chromeos_update_manager 248 249 #endif // UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_ 250