1 // Copyright (c) 2012 The Chromium 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 
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_CORE_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_CORE_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/observer_list.h"
14 #include "components/policy/core/common/cloud/policy_invalidation_scope.h"
15 #include "components/policy/policy_export.h"
16 #include "components/prefs/pref_member.h"
17 #include "services/network/public/cpp/network_connection_tracker.h"
18 
19 class PrefService;
20 
21 namespace base {
22 class SequencedTaskRunner;
23 }
24 
25 namespace policy {
26 
27 class CloudPolicyClient;
28 class CloudPolicyRefreshScheduler;
29 class CloudPolicyService;
30 class CloudPolicyStore;
31 class RemoteCommandsFactory;
32 class RemoteCommandsService;
33 
34 // CloudPolicyCore glues together the ingredients that are essential for
35 // obtaining a fully-functional cloud policy system: CloudPolicyClient and
36 // CloudPolicyStore, which are responsible for fetching policy from the cloud
37 // and storing it locally, respectively, as well as a CloudPolicyService
38 // instance that moves data between the two former components, and
39 // CloudPolicyRefreshScheduler which triggers periodic refreshes.
40 class POLICY_EXPORT CloudPolicyCore {
41  public:
42   // Callbacks for policy core events.
43   class POLICY_EXPORT Observer {
44    public:
45     virtual ~Observer();
46 
47     // Called after the core is connected.
48     virtual void OnCoreConnected(CloudPolicyCore* core) = 0;
49 
50     // Called after the refresh scheduler is started.
51     virtual void OnRefreshSchedulerStarted(CloudPolicyCore* core) = 0;
52 
53     // Called before the core is disconnected.
54     virtual void OnCoreDisconnecting(CloudPolicyCore* core) = 0;
55 
56     // Called after the remote commands service is started. Defaults to be
57     // empty.
58     virtual void OnRemoteCommandsServiceStarted(CloudPolicyCore* core);
59   };
60 
61   // |task_runner| is the runner for policy refresh tasks.
62   CloudPolicyCore(const std::string& policy_type,
63                   const std::string& settings_entity_id,
64                   CloudPolicyStore* store,
65                   const scoped_refptr<base::SequencedTaskRunner>& task_runner,
66                   network::NetworkConnectionTrackerGetter
67                       network_connection_tracker_getter);
68   ~CloudPolicyCore();
69 
client()70   CloudPolicyClient* client() { return client_.get(); }
client()71   const CloudPolicyClient* client() const { return client_.get(); }
72 
store()73   CloudPolicyStore* store() { return store_; }
store()74   const CloudPolicyStore* store() const { return store_; }
75 
service()76   CloudPolicyService* service() { return service_.get(); }
service()77   const CloudPolicyService* service() const { return service_.get(); }
78 
refresh_scheduler()79   CloudPolicyRefreshScheduler* refresh_scheduler() {
80     return refresh_scheduler_.get();
81   }
refresh_scheduler()82   const CloudPolicyRefreshScheduler* refresh_scheduler() const {
83     return refresh_scheduler_.get();
84   }
85 
remote_commands_service()86   RemoteCommandsService* remote_commands_service() {
87     return remote_commands_service_.get();
88   }
remote_commands_service()89   const RemoteCommandsService* remote_commands_service() const {
90     return remote_commands_service_.get();
91   }
92 
93   // Initializes the cloud connection.
94   void Connect(std::unique_ptr<CloudPolicyClient> client);
95 
96   // Shuts down the cloud connection.
97   void Disconnect();
98 
99   // Starts a remote commands service, with the provided factory. Will attempt
100   // to fetch commands immediately, thus requiring the cloud policy client to
101   // be registered.
102   void StartRemoteCommandsService(
103       std::unique_ptr<RemoteCommandsFactory> factory,
104       PolicyInvalidationScope scope);
105 
106   // Requests a policy refresh to be performed soon. This may apply throttling,
107   // and the request may not be immediately sent.
108   void RefreshSoon();
109 
110   // Starts a refresh scheduler in case none is running yet.
111   void StartRefreshScheduler();
112 
113   // Watches the pref named |refresh_pref_name| in |pref_service| and adjusts
114   // |refresh_scheduler_|'s refresh delay accordingly.
115   void TrackRefreshDelayPref(PrefService* pref_service,
116                              const std::string& refresh_pref_name);
117 
118   // Registers an observer to be notified of policy core events.
119   void AddObserver(Observer* observer);
120 
121   // Removes the specified observer.
122   void RemoveObserver(Observer* observer);
123 
124   // Initializes the cloud connection using injected |service| and |client|.
125   void ConnectForTesting(std::unique_ptr<CloudPolicyService> service,
126                          std::unique_ptr<CloudPolicyClient> client);
127 
128  private:
129   // Updates the refresh scheduler on refresh delay changes.
130   void UpdateRefreshDelayFromPref();
131 
132   std::string policy_type_;
133   std::string settings_entity_id_;
134   CloudPolicyStore* store_;
135   scoped_refptr<base::SequencedTaskRunner> task_runner_;
136   network::NetworkConnectionTrackerGetter network_connection_tracker_getter_;
137   std::unique_ptr<CloudPolicyClient> client_;
138   std::unique_ptr<CloudPolicyService> service_;
139   std::unique_ptr<CloudPolicyRefreshScheduler> refresh_scheduler_;
140   std::unique_ptr<RemoteCommandsService> remote_commands_service_;
141   std::unique_ptr<IntegerPrefMember> refresh_delay_;
142   base::ObserverList<Observer, true>::Unchecked observers_;
143 
144   DISALLOW_COPY_AND_ASSIGN(CloudPolicyCore);
145 };
146 
147 }  // namespace policy
148 
149 #endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_CORE_H_
150