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 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_ |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 7 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | |
| 11 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 12 | #include <policy/libpolicy.h> |
| 13 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 14 | #include "update_engine/update_manager/device_policy_provider.h" |
| 15 | #include "update_engine/update_manager/event_loop.h" |
| 16 | #include "update_engine/update_manager/generic_variables.h" |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 17 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 18 | namespace chromeos_update_manager { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 19 | |
| 20 | // DevicePolicyProvider concrete implementation. |
| 21 | class RealDevicePolicyProvider : public DevicePolicyProvider { |
| 22 | public: |
| 23 | explicit RealDevicePolicyProvider(policy::PolicyProvider* policy_provider) |
| 24 | : policy_provider_(policy_provider) {} |
| 25 | ~RealDevicePolicyProvider(); |
| 26 | |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 27 | // Initializes the provider and returns whether it succeeded. |
| 28 | bool Init(); |
| 29 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 30 | Variable<bool>* var_device_policy_is_loaded() override { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 31 | return &var_device_policy_is_loaded_; |
| 32 | } |
| 33 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 34 | Variable<std::string>* var_release_channel() override { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 35 | return &var_release_channel_; |
| 36 | } |
| 37 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 38 | Variable<bool>* var_release_channel_delegated() override { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 39 | return &var_release_channel_delegated_; |
| 40 | } |
| 41 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 42 | Variable<bool>* var_update_disabled() override { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 43 | return &var_update_disabled_; |
| 44 | } |
| 45 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 46 | Variable<std::string>* var_target_version_prefix() override { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 47 | return &var_target_version_prefix_; |
| 48 | } |
| 49 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 50 | Variable<base::TimeDelta>* var_scatter_factor() override { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 51 | return &var_scatter_factor_; |
| 52 | } |
| 53 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 54 | Variable<std::set<ConnectionType>>* |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 55 | var_allowed_connection_types_for_update() override { |
| 56 | return &var_allowed_connection_types_for_update_; |
| 57 | } |
| 58 | |
Gilad Arnold | ef8d087 | 2014-10-03 14:14:06 -0700 | [diff] [blame] | 59 | Variable<std::string>* var_owner() override { |
| 60 | return &var_owner_; |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 63 | Variable<bool>* var_http_downloads_enabled() override { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 64 | return &var_http_downloads_enabled_; |
| 65 | } |
| 66 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 67 | Variable<bool>* var_au_p2p_enabled() override { |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 68 | return &var_au_p2p_enabled_; |
| 69 | } |
| 70 | |
| 71 | private: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 72 | FRIEND_TEST(UmRealDevicePolicyProviderTest, RefreshScheduledTest); |
| 73 | FRIEND_TEST(UmRealDevicePolicyProviderTest, NonExistentDevicePolicyReloaded); |
| 74 | FRIEND_TEST(UmRealDevicePolicyProviderTest, ValuesUpdated); |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 75 | |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 76 | // Schedules a call to periodically refresh the device policy. |
| 77 | void RefreshDevicePolicyAndReschedule(); |
| 78 | |
| 79 | // Reloads the device policy and updates all the exposed variables. |
| 80 | void RefreshDevicePolicy(); |
| 81 | |
| 82 | // Updates the async variable |var| based on the result value of the method |
| 83 | // passed, which is a DevicePolicy getter method. |
| 84 | template<typename T> |
| 85 | void UpdateVariable(AsyncCopyVariable<T>* var, |
| 86 | bool (policy::DevicePolicy::*getter_method)(T*) const); |
| 87 | |
| 88 | // Updates the async variable |var| based on the result value of the getter |
| 89 | // method passed, which is a wrapper getter on this class. |
| 90 | template<typename T> |
| 91 | void UpdateVariable( |
| 92 | AsyncCopyVariable<T>* var, |
| 93 | bool (RealDevicePolicyProvider::*getter_method)(T*) const); |
| 94 | |
| 95 | // Wrapper for DevicePolicy::GetScatterFactorInSeconds() that converts the |
| 96 | // result to a base::TimeDelta. It returns the same value as |
| 97 | // GetScatterFactorInSeconds(). |
| 98 | bool ConvertScatterFactor(base::TimeDelta* scatter_factor) const; |
| 99 | |
| 100 | // Wrapper for DevicePolicy::GetAllowedConnectionTypesForUpdate() that |
| 101 | // converts the result to a set of ConnectionType elements instead of strings. |
| 102 | bool ConvertAllowedConnectionTypesForUpdate( |
| 103 | std::set<ConnectionType>* allowed_types) const; |
| 104 | |
| 105 | // Used for fetching information about the device policy. |
| 106 | policy::PolicyProvider* policy_provider_; |
| 107 | |
| 108 | // Used to schedule refreshes of the device policy. |
| 109 | EventId scheduled_refresh_ = kEventIdNull; |
| 110 | |
| 111 | // Variable exposing whether the policy is loaded. |
| 112 | AsyncCopyVariable<bool> var_device_policy_is_loaded_{ |
| 113 | "policy_is_loaded", false}; |
| 114 | |
| 115 | // Variables mapping the exposed methods from the policy::DevicePolicy. |
| 116 | AsyncCopyVariable<std::string> var_release_channel_{"release_channel"}; |
| 117 | AsyncCopyVariable<bool> var_release_channel_delegated_{ |
| 118 | "release_channel_delegated"}; |
| 119 | AsyncCopyVariable<bool> var_update_disabled_{"update_disabled"}; |
| 120 | AsyncCopyVariable<std::string> var_target_version_prefix_{ |
| 121 | "target_version_prefix"}; |
| 122 | AsyncCopyVariable<base::TimeDelta> var_scatter_factor_{"scatter_factor"}; |
| 123 | AsyncCopyVariable<std::set<ConnectionType>> |
| 124 | var_allowed_connection_types_for_update_{ |
| 125 | "allowed_connection_types_for_update"}; |
Gilad Arnold | ef8d087 | 2014-10-03 14:14:06 -0700 | [diff] [blame] | 126 | AsyncCopyVariable<std::string> var_owner_{"owner"}; |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 127 | AsyncCopyVariable<bool> var_http_downloads_enabled_{"http_downloads_enabled"}; |
| 128 | AsyncCopyVariable<bool> var_au_p2p_enabled_{"au_p2p_enabled"}; |
| 129 | |
| 130 | DISALLOW_COPY_AND_ASSIGN(RealDevicePolicyProvider); |
| 131 | }; |
| 132 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 133 | } // namespace chromeos_update_manager |
Alex Deymo | c83baf6 | 2014-04-02 17:43:35 -0700 | [diff] [blame] | 134 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 135 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_ |