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_SERVICE_H_
6#define COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_SERVICE_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/callback_forward.h"
13#include "base/compiler_specific.h"
14#include "base/observer_list.h"
15#include "components/policy/core/common/cloud/cloud_policy_client.h"
16#include "components/policy/core/common/cloud/cloud_policy_constants.h"
17#include "components/policy/core/common/cloud/cloud_policy_store.h"
18#include "components/policy/policy_export.h"
19
20namespace policy {
21
22// Coordinates cloud policy handling, moving downloaded policy from the client
23// to the store, and setting up client registrations from cached data in the
24// store. Also coordinates actions on policy refresh triggers.
25class POLICY_EXPORT CloudPolicyService : public CloudPolicyClient::Observer,
26                                         public CloudPolicyStore::Observer {
27 public:
28  // Callback invoked once the policy refresh attempt has completed. Passed
29  // bool parameter is true if the refresh was successful (no error).
30  typedef base::Callback<void(bool)> RefreshPolicyCallback;
31
32  class POLICY_EXPORT Observer {
33   public:
34    // Invoked when CloudPolicyService has finished initializing (any initial
35    // policy load activity has completed and the CloudPolicyClient has
36    // been registered, if possible).
37    virtual void OnInitializationCompleted(CloudPolicyService* service) = 0;
38    virtual ~Observer() {}
39  };
40
41  // |client| and |store| must remain valid for the object life time.
42  CloudPolicyService(const PolicyNamespaceKey& policy_ns_key,
43                     CloudPolicyClient* client,
44                     CloudPolicyStore* store);
45  virtual ~CloudPolicyService();
46
47  // Returns the domain that manages this user/device, according to the current
48  // policy blob. Empty if not managed/not available.
49  std::string ManagedBy() const;
50
51  // Refreshes policy. |callback| will be invoked after the operation completes
52  // or aborts because of errors.
53  void RefreshPolicy(const RefreshPolicyCallback& callback);
54
55  // Adds/Removes an Observer for this object.
56  void AddObserver(Observer* observer);
57  void RemoveObserver(Observer* observer);
58
59  // CloudPolicyClient::Observer:
60  virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
61  virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
62  virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
63
64  // CloudPolicyStore::Observer:
65  virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
66  virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
67
68  bool IsInitializationComplete() const { return initialization_complete_; }
69
70 private:
71  // Helper function that is called when initialization may be complete, and
72  // which is responsible for notifying observers.
73  void CheckInitializationCompleted();
74
75  // Invokes the refresh callbacks and clears refresh state. The |success| flag
76  // is passed through to the refresh callbacks.
77  void RefreshCompleted(bool success);
78
79  // The policy namespace fetched by |client_| and expected by |store_|.
80  PolicyNamespaceKey policy_ns_key_;
81
82  // The client used to talk to the cloud.
83  CloudPolicyClient* client_;
84
85  // Takes care of persisting and decoding cloud policy.
86  CloudPolicyStore* store_;
87
88  // Tracks the state of a pending refresh operation, if any.
89  enum {
90    // No refresh pending.
91    REFRESH_NONE,
92    // Policy fetch is pending.
93    REFRESH_POLICY_FETCH,
94    // Policy store is pending.
95    REFRESH_POLICY_STORE,
96  } refresh_state_;
97
98  // Callbacks to invoke upon policy refresh.
99  std::vector<RefreshPolicyCallback> refresh_callbacks_;
100
101  // Set to true once the service is initialized (initial policy load/refresh
102  // is complete).
103  bool initialization_complete_;
104
105  // Observers who will receive notifications when the service has finished
106  // initializing.
107  ObserverList<Observer, true> observers_;
108
109  DISALLOW_COPY_AND_ASSIGN(CloudPolicyService);
110};
111
112}  // namespace policy
113
114#endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_SERVICE_H_
115