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 <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/observer_list.h"
14#include "base/prefs/pref_member.h"
15#include "components/policy/core/common/cloud/cloud_policy_constants.h"
16#include "components/policy/policy_export.h"
17
18class PrefService;
19
20namespace base {
21class SequencedTaskRunner;
22}
23
24namespace policy {
25
26class CloudPolicyClient;
27class CloudPolicyRefreshScheduler;
28class CloudPolicyService;
29class CloudPolicyStore;
30
31// CloudPolicyCore glues together the ingredients that are essential for
32// obtaining a fully-functional cloud policy system: CloudPolicyClient and
33// CloudPolicyStore, which are responsible for fetching policy from the cloud
34// and storing it locally, respectively, as well as a CloudPolicyService
35// instance that moves data between the two former components, and
36// CloudPolicyRefreshScheduler which triggers periodic refreshes.
37class POLICY_EXPORT CloudPolicyCore {
38 public:
39  // Callbacks for policy core events.
40  class POLICY_EXPORT Observer {
41   public:
42    virtual ~Observer();
43
44    // Called after the core is connected.
45    virtual void OnCoreConnected(CloudPolicyCore* core) = 0;
46
47    // Called after the refresh scheduler is started.
48    virtual void OnRefreshSchedulerStarted(CloudPolicyCore* core) = 0;
49
50    // Called before the core is disconnected.
51    virtual void OnCoreDisconnecting(CloudPolicyCore* core) = 0;
52  };
53
54  // |task_runner| is the runner for policy refresh tasks.
55  CloudPolicyCore(const PolicyNamespaceKey& policy_ns_key,
56                  CloudPolicyStore* store,
57                  const scoped_refptr<base::SequencedTaskRunner>& task_runner);
58  ~CloudPolicyCore();
59
60  CloudPolicyClient* client() { return client_.get(); }
61  const CloudPolicyClient* client() const { return client_.get(); }
62
63  CloudPolicyStore* store() { return store_; }
64  const CloudPolicyStore* store() const { return store_; }
65
66  CloudPolicyService* service() { return service_.get(); }
67  const CloudPolicyService* service() const { return service_.get(); }
68
69  CloudPolicyRefreshScheduler* refresh_scheduler() {
70    return refresh_scheduler_.get();
71  }
72  const CloudPolicyRefreshScheduler* refresh_scheduler() const {
73    return refresh_scheduler_.get();
74  }
75
76  // Initializes the cloud connection.
77  void Connect(scoped_ptr<CloudPolicyClient> client);
78
79  // Shuts down the cloud connection.
80  void Disconnect();
81
82  // Requests a policy refresh to be performed soon. This may apply throttling,
83  // and the request may not be immediately sent.
84  void RefreshSoon();
85
86  // Starts a refresh scheduler in case none is running yet.
87  void StartRefreshScheduler();
88
89  // Watches the pref named |refresh_pref_name| in |pref_service| and adjusts
90  // |refresh_scheduler_|'s refresh delay accordingly.
91  void TrackRefreshDelayPref(PrefService* pref_service,
92                             const std::string& refresh_pref_name);
93
94  // Registers an observer to be notified of policy core events.
95  void AddObserver(Observer* observer);
96
97  // Removes the specified observer.
98  void RemoveObserver(Observer* observer);
99
100 private:
101  // Updates the refresh scheduler on refresh delay changes.
102  void UpdateRefreshDelayFromPref();
103
104  PolicyNamespaceKey policy_ns_key_;
105  CloudPolicyStore* store_;
106  scoped_refptr<base::SequencedTaskRunner> task_runner_;
107  scoped_ptr<CloudPolicyClient> client_;
108  scoped_ptr<CloudPolicyService> service_;
109  scoped_ptr<CloudPolicyRefreshScheduler> refresh_scheduler_;
110  scoped_ptr<IntegerPrefMember> refresh_delay_;
111  ObserverList<Observer, true> observers_;
112
113  DISALLOW_COPY_AND_ASSIGN(CloudPolicyCore);
114};
115
116}  // namespace policy
117
118#endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_CORE_H_
119