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
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
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 
55   Variable<bool>* var_device_policy_is_loaded() override {
56     return &var_device_policy_is_loaded_;
57   }
58 
59   Variable<std::string>* var_release_channel() override {
60     return &var_release_channel_;
61   }
62 
63   Variable<bool>* var_release_channel_delegated() override {
64     return &var_release_channel_delegated_;
65   }
66 
67   Variable<bool>* var_update_disabled() override {
68     return &var_update_disabled_;
69   }
70 
71   Variable<std::string>* var_target_version_prefix() override {
72     return &var_target_version_prefix_;
73   }
74 
75   Variable<RollbackToTargetVersion>* var_rollback_to_target_version() override {
76     return &var_rollback_to_target_version_;
77   }
78 
79   Variable<int>* var_rollback_allowed_milestones() override {
80     return &var_rollback_allowed_milestones_;
81   }
82 
83   Variable<base::TimeDelta>* var_scatter_factor() override {
84     return &var_scatter_factor_;
85   }
86 
87   Variable<std::set<chromeos_update_engine::ConnectionType>>*
88   var_allowed_connection_types_for_update() override {
89     return &var_allowed_connection_types_for_update_;
90   }
91 
92   Variable<std::string>* var_owner() override { return &var_owner_; }
93 
94   Variable<bool>* var_http_downloads_enabled() override {
95     return &var_http_downloads_enabled_;
96   }
97 
98   Variable<bool>* var_au_p2p_enabled() override { return &var_au_p2p_enabled_; }
99 
100   Variable<bool>* var_allow_kiosk_app_control_chrome_version() override {
101     return &var_allow_kiosk_app_control_chrome_version_;
102   }
103 
104   Variable<std::string>* var_auto_launched_kiosk_app_id() override {
105     return &var_auto_launched_kiosk_app_id_;
106   }
107 
108   Variable<WeeklyTimeIntervalVector>* var_disallowed_time_intervals() override {
109     return &var_disallowed_time_intervals_;
110   }
111 
112  private:
113   FRIEND_TEST(UmRealDevicePolicyProviderTest, RefreshScheduledTest);
114   FRIEND_TEST(UmRealDevicePolicyProviderTest, NonExistentDevicePolicyReloaded);
115   FRIEND_TEST(UmRealDevicePolicyProviderTest, ValuesUpdated);
116 
117   // A static handler for the PropertyChangedCompleted signal from the session
118   // manager used as a callback.
119   void OnPropertyChangedCompletedSignal(const std::string& success);
120 
121   // Called when the signal in UpdateEngineLibcrosProxyResolvedInterface is
122   // connected.
123   void OnSignalConnected(const std::string& interface_name,
124                          const std::string& signal_name,
125                          bool successful);
126 
127   // Schedules a call to periodically refresh the device policy.
128   void RefreshDevicePolicyAndReschedule();
129 
130   // Reloads the device policy and updates all the exposed variables.
131   void RefreshDevicePolicy();
132 
133   // Updates the async variable |var| based on the result value of the method
134   // passed, which is a DevicePolicy getter method.
135   template <typename T>
136   void UpdateVariable(AsyncCopyVariable<T>* var,
137                       bool (policy::DevicePolicy::*getter_method)(T*) const);
138 
139   // Updates the async variable |var| based on the result value of the getter
140   // method passed, which is a wrapper getter on this class.
141   template <typename T>
142   void UpdateVariable(AsyncCopyVariable<T>* var,
143                       bool (RealDevicePolicyProvider::*getter_method)(T*)
144                           const);
145 
146   // Wrapper for DevicePolicy::GetRollbackToTargetVersion() that converts the
147   // result to RollbackToTargetVersion.
148   bool ConvertRollbackToTargetVersion(
149       RollbackToTargetVersion* rollback_to_target_version) const;
150 
151   // Wrapper for DevicePolicy::GetScatterFactorInSeconds() that converts the
152   // result to a base::TimeDelta. It returns the same value as
153   // GetScatterFactorInSeconds().
154   bool ConvertScatterFactor(base::TimeDelta* scatter_factor) const;
155 
156   // Wrapper for DevicePolicy::GetAllowedConnectionTypesForUpdate() that
157   // converts the result to a set of ConnectionType elements instead of strings.
158   bool ConvertAllowedConnectionTypesForUpdate(
159       std::set<chromeos_update_engine::ConnectionType>* allowed_types) const;
160 
161   // Wrapper for DevicePolicy::GetUpdateTimeRestrictions() that converts
162   // the DevicePolicy::WeeklyTimeInterval structs to WeeklyTimeInterval objects,
163   // which offer more functionality.
164   bool ConvertDisallowedTimeIntervals(
165       WeeklyTimeIntervalVector* disallowed_intervals_out) const;
166 
167   // Used for fetching information about the device policy.
168   policy::PolicyProvider* policy_provider_;
169 
170   // Used to schedule refreshes of the device policy.
171   brillo::MessageLoop::TaskId scheduled_refresh_{
172       brillo::MessageLoop::kTaskIdNull};
173 
174 #if USE_DBUS
175   // The DBus (mockable) session manager proxy.
176   std::unique_ptr<org::chromium::SessionManagerInterfaceProxyInterface>
177       session_manager_proxy_;
178 #endif  // USE_DBUS
179 
180   // Variable exposing whether the policy is loaded.
181   AsyncCopyVariable<bool> var_device_policy_is_loaded_{"policy_is_loaded",
182                                                        false};
183 
184   // Variables mapping the exposed methods from the policy::DevicePolicy.
185   AsyncCopyVariable<std::string> var_release_channel_{"release_channel"};
186   AsyncCopyVariable<bool> var_release_channel_delegated_{
187       "release_channel_delegated"};
188   AsyncCopyVariable<bool> var_update_disabled_{"update_disabled"};
189   AsyncCopyVariable<std::string> var_target_version_prefix_{
190       "target_version_prefix"};
191   AsyncCopyVariable<RollbackToTargetVersion> var_rollback_to_target_version_{
192       "rollback_to_target_version"};
193   AsyncCopyVariable<int> var_rollback_allowed_milestones_{
194       "rollback_allowed_milestones"};
195   AsyncCopyVariable<base::TimeDelta> var_scatter_factor_{"scatter_factor"};
196   AsyncCopyVariable<std::set<chromeos_update_engine::ConnectionType>>
197       var_allowed_connection_types_for_update_{
198           "allowed_connection_types_for_update"};
199   AsyncCopyVariable<std::string> var_owner_{"owner"};
200   AsyncCopyVariable<bool> var_http_downloads_enabled_{"http_downloads_enabled"};
201   AsyncCopyVariable<bool> var_au_p2p_enabled_{"au_p2p_enabled"};
202   AsyncCopyVariable<bool> var_allow_kiosk_app_control_chrome_version_{
203       "allow_kiosk_app_control_chrome_version"};
204   AsyncCopyVariable<WeeklyTimeIntervalVector> var_disallowed_time_intervals_{
205       "update_time_restrictions"};
206   AsyncCopyVariable<std::string> var_auto_launched_kiosk_app_id_{
207       "auto_launched_kiosk_app_id"};
208 
209   DISALLOW_COPY_AND_ASSIGN(RealDevicePolicyProvider);
210 };
211 
212 }  // namespace chromeos_update_manager
213 
214 #endif  // UPDATE_ENGINE_UPDATE_MANAGER_REAL_DEVICE_POLICY_PROVIDER_H_
215