blob: c3e2980912f416f6d172ea4252cff5000804dcfa [file] [log] [blame]
Alex Deymoc83baf62014-04-02 17:43:35 -07001// 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 Arnold48415f12014-06-27 07:10:58 -07005#ifndef UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_
Alex Deymoc83baf62014-04-02 17:43:35 -07007
8#include <set>
9#include <string>
10
11#include <gtest/gtest_prod.h> // for FRIEND_TEST
12#include <policy/libpolicy.h>
13
Alex Deymo63784a52014-05-28 10:46:14 -070014#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 Deymoc83baf62014-04-02 17:43:35 -070017
Alex Deymo63784a52014-05-28 10:46:14 -070018namespace chromeos_update_manager {
Alex Deymoc83baf62014-04-02 17:43:35 -070019
20// DevicePolicyProvider concrete implementation.
21class RealDevicePolicyProvider : public DevicePolicyProvider {
22 public:
23 explicit RealDevicePolicyProvider(policy::PolicyProvider* policy_provider)
24 : policy_provider_(policy_provider) {}
25 ~RealDevicePolicyProvider();
26
Alex Deymo42c30c32014-04-24 18:41:18 -070027 // Initializes the provider and returns whether it succeeded.
28 bool Init();
29
Alex Vakulenko157fe302014-08-11 15:59:58 -070030 Variable<bool>* var_device_policy_is_loaded() override {
Alex Deymoc83baf62014-04-02 17:43:35 -070031 return &var_device_policy_is_loaded_;
32 }
33
Alex Vakulenko157fe302014-08-11 15:59:58 -070034 Variable<std::string>* var_release_channel() override {
Alex Deymoc83baf62014-04-02 17:43:35 -070035 return &var_release_channel_;
36 }
37
Alex Vakulenko157fe302014-08-11 15:59:58 -070038 Variable<bool>* var_release_channel_delegated() override {
Alex Deymoc83baf62014-04-02 17:43:35 -070039 return &var_release_channel_delegated_;
40 }
41
Alex Vakulenko157fe302014-08-11 15:59:58 -070042 Variable<bool>* var_update_disabled() override {
Alex Deymoc83baf62014-04-02 17:43:35 -070043 return &var_update_disabled_;
44 }
45
Alex Vakulenko157fe302014-08-11 15:59:58 -070046 Variable<std::string>* var_target_version_prefix() override {
Alex Deymoc83baf62014-04-02 17:43:35 -070047 return &var_target_version_prefix_;
48 }
49
Alex Vakulenko157fe302014-08-11 15:59:58 -070050 Variable<base::TimeDelta>* var_scatter_factor() override {
Alex Deymoc83baf62014-04-02 17:43:35 -070051 return &var_scatter_factor_;
52 }
53
Alex Vakulenko157fe302014-08-11 15:59:58 -070054 Variable<std::set<ConnectionType>>*
Alex Deymoc83baf62014-04-02 17:43:35 -070055 var_allowed_connection_types_for_update() override {
56 return &var_allowed_connection_types_for_update_;
57 }
58
Gilad Arnoldef8d0872014-10-03 14:14:06 -070059 Variable<std::string>* var_owner() override {
60 return &var_owner_;
Alex Deymoc83baf62014-04-02 17:43:35 -070061 }
62
Alex Vakulenko157fe302014-08-11 15:59:58 -070063 Variable<bool>* var_http_downloads_enabled() override {
Alex Deymoc83baf62014-04-02 17:43:35 -070064 return &var_http_downloads_enabled_;
65 }
66
Alex Vakulenko157fe302014-08-11 15:59:58 -070067 Variable<bool>* var_au_p2p_enabled() override {
Alex Deymoc83baf62014-04-02 17:43:35 -070068 return &var_au_p2p_enabled_;
69 }
70
71 private:
Alex Deymo63784a52014-05-28 10:46:14 -070072 FRIEND_TEST(UmRealDevicePolicyProviderTest, RefreshScheduledTest);
73 FRIEND_TEST(UmRealDevicePolicyProviderTest, NonExistentDevicePolicyReloaded);
74 FRIEND_TEST(UmRealDevicePolicyProviderTest, ValuesUpdated);
Alex Deymoc83baf62014-04-02 17:43:35 -070075
Alex Deymoc83baf62014-04-02 17:43:35 -070076 // 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 Arnoldef8d0872014-10-03 14:14:06 -0700126 AsyncCopyVariable<std::string> var_owner_{"owner"};
Alex Deymoc83baf62014-04-02 17:43:35 -0700127 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 Deymo63784a52014-05-28 10:46:14 -0700133} // namespace chromeos_update_manager
Alex Deymoc83baf62014-04-02 17:43:35 -0700134
Gilad Arnold48415f12014-06-27 07:10:58 -0700135#endif // UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_