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 
24 #include <brillo/message_loops/message_loop.h>
25 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
26 #include <policy/libpolicy.h>
27 #if USE_DBUS
28 #include <session_manager/dbus-proxies.h>
29 #endif  // USE_DBUS
30 
31 #include "update_engine/update_manager/device_policy_provider.h"
32 #include "update_engine/update_manager/generic_variables.h"
33 
34 namespace chromeos_update_manager {
35 
36 // DevicePolicyProvider concrete implementation.
37 class RealDevicePolicyProvider : public DevicePolicyProvider {
38  public:
39 #if USE_DBUS
RealDevicePolicyProvider(std::unique_ptr<org::chromium::SessionManagerInterfaceProxyInterface> session_manager_proxy,policy::PolicyProvider * policy_provider)40   RealDevicePolicyProvider(
41       std::unique_ptr<org::chromium::SessionManagerInterfaceProxyInterface>
42           session_manager_proxy,
43       policy::PolicyProvider* policy_provider)
44       : policy_provider_(policy_provider),
45         session_manager_proxy_(std::move(session_manager_proxy)) {}
46 #endif  // USE_DBUS
RealDevicePolicyProvider(policy::PolicyProvider * policy_provider)47   explicit RealDevicePolicyProvider(policy::PolicyProvider* policy_provider)
48       : policy_provider_(policy_provider) {}
49   ~RealDevicePolicyProvider();
50 
51   // Initializes the provider and returns whether it succeeded.
52   bool Init();
53 
var_device_policy_is_loaded()54   Variable<bool>* var_device_policy_is_loaded() override {
55     return &var_device_policy_is_loaded_;
56   }
57 
var_release_channel()58   Variable<std::string>* var_release_channel() override {
59     return &var_release_channel_;
60   }
61 
var_release_channel_delegated()62   Variable<bool>* var_release_channel_delegated() override {
63     return &var_release_channel_delegated_;
64   }
65 
var_update_disabled()66   Variable<bool>* var_update_disabled() override {
67     return &var_update_disabled_;
68   }
69 
var_target_version_prefix()70   Variable<std::string>* var_target_version_prefix() override {
71     return &var_target_version_prefix_;
72   }
73 
var_scatter_factor()74   Variable<base::TimeDelta>* var_scatter_factor() override {
75     return &var_scatter_factor_;
76   }
77 
78   Variable<std::set<chromeos_update_engine::ConnectionType>>*
var_allowed_connection_types_for_update()79       var_allowed_connection_types_for_update() override {
80     return &var_allowed_connection_types_for_update_;
81   }
82 
var_owner()83   Variable<std::string>* var_owner() override {
84     return &var_owner_;
85   }
86 
var_http_downloads_enabled()87   Variable<bool>* var_http_downloads_enabled() override {
88     return &var_http_downloads_enabled_;
89   }
90 
var_au_p2p_enabled()91   Variable<bool>* var_au_p2p_enabled() override {
92     return &var_au_p2p_enabled_;
93   }
94 
var_allow_kiosk_app_control_chrome_version()95   Variable<bool>* var_allow_kiosk_app_control_chrome_version() override {
96     return &var_allow_kiosk_app_control_chrome_version_;
97   }
98 
99  private:
100   FRIEND_TEST(UmRealDevicePolicyProviderTest, RefreshScheduledTest);
101   FRIEND_TEST(UmRealDevicePolicyProviderTest, NonExistentDevicePolicyReloaded);
102   FRIEND_TEST(UmRealDevicePolicyProviderTest, ValuesUpdated);
103 
104   // A static handler for the PropertyChangedCompleted signal from the session
105   // manager used as a callback.
106   void OnPropertyChangedCompletedSignal(const std::string& success);
107 
108   // Called when the signal in UpdateEngineLibcrosProxyResolvedInterface is
109   // connected.
110   void OnSignalConnected(const std::string& interface_name,
111                          const std::string& signal_name,
112                          bool successful);
113 
114   // Schedules a call to periodically refresh the device policy.
115   void RefreshDevicePolicyAndReschedule();
116 
117   // Reloads the device policy and updates all the exposed variables.
118   void RefreshDevicePolicy();
119 
120   // Updates the async variable |var| based on the result value of the method
121   // passed, which is a DevicePolicy getter method.
122   template<typename T>
123   void UpdateVariable(AsyncCopyVariable<T>* var,
124                       bool (policy::DevicePolicy::*getter_method)(T*) const);
125 
126   // Updates the async variable |var| based on the result value of the getter
127   // method passed, which is a wrapper getter on this class.
128   template<typename T>
129   void UpdateVariable(
130       AsyncCopyVariable<T>* var,
131       bool (RealDevicePolicyProvider::*getter_method)(T*) const);
132 
133   // Wrapper for DevicePolicy::GetScatterFactorInSeconds() that converts the
134   // result to a base::TimeDelta. It returns the same value as
135   // GetScatterFactorInSeconds().
136   bool ConvertScatterFactor(base::TimeDelta* scatter_factor) const;
137 
138   // Wrapper for DevicePolicy::GetAllowedConnectionTypesForUpdate() that
139   // converts the result to a set of ConnectionType elements instead of strings.
140   bool ConvertAllowedConnectionTypesForUpdate(
141       std::set<chromeos_update_engine::ConnectionType>* allowed_types) const;
142 
143   // Used for fetching information about the device policy.
144   policy::PolicyProvider* policy_provider_;
145 
146   // Used to schedule refreshes of the device policy.
147   brillo::MessageLoop::TaskId scheduled_refresh_{
148       brillo::MessageLoop::kTaskIdNull};
149 
150 #if USE_DBUS
151   // The DBus (mockable) session manager proxy.
152   std::unique_ptr<org::chromium::SessionManagerInterfaceProxyInterface>
153       session_manager_proxy_;
154 #endif  // USE_DBUS
155 
156   // Variable exposing whether the policy is loaded.
157   AsyncCopyVariable<bool> var_device_policy_is_loaded_{
158       "policy_is_loaded", false};
159 
160   // Variables mapping the exposed methods from the policy::DevicePolicy.
161   AsyncCopyVariable<std::string> var_release_channel_{"release_channel"};
162   AsyncCopyVariable<bool> var_release_channel_delegated_{
163       "release_channel_delegated"};
164   AsyncCopyVariable<bool> var_update_disabled_{"update_disabled"};
165   AsyncCopyVariable<std::string> var_target_version_prefix_{
166       "target_version_prefix"};
167   AsyncCopyVariable<base::TimeDelta> var_scatter_factor_{"scatter_factor"};
168   AsyncCopyVariable<std::set<chromeos_update_engine::ConnectionType>>
169       var_allowed_connection_types_for_update_{
170           "allowed_connection_types_for_update"};
171   AsyncCopyVariable<std::string> var_owner_{"owner"};
172   AsyncCopyVariable<bool> var_http_downloads_enabled_{"http_downloads_enabled"};
173   AsyncCopyVariable<bool> var_au_p2p_enabled_{"au_p2p_enabled"};
174   AsyncCopyVariable<bool> var_allow_kiosk_app_control_chrome_version_{
175       "allow_kiosk_app_control_chrome_version"};
176 
177   DISALLOW_COPY_AND_ASSIGN(RealDevicePolicyProvider);
178 };
179 
180 }  // namespace chromeos_update_manager
181 
182 #endif  // UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_
183