Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / common / cloud / cloud_policy_service.h
blob465db98e4af41b08d8378a5ad905349e8a805338
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.
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_SERVICE_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_SERVICE_H_
8 #include <string>
9 #include <vector>
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_store.h"
17 #include "components/policy/policy_export.h"
19 namespace policy {
21 // Coordinates cloud policy handling, moving downloaded policy from the client
22 // to the store, and setting up client registrations from cached data in the
23 // store. Also coordinates actions on policy refresh triggers.
24 class POLICY_EXPORT CloudPolicyService : public CloudPolicyClient::Observer,
25 public CloudPolicyStore::Observer {
26 public:
27 // Callback invoked once the policy refresh attempt has completed. Passed
28 // bool parameter is true if the refresh was successful (no error).
29 using RefreshPolicyCallback = base::Callback<void(bool)>;
31 // Callback invoked once the unregister attempt has completed. Passed bool
32 // parameter is true if unregistering was successful (no error).
33 using UnregisterCallback = base::Callback<void(bool)>;
35 class POLICY_EXPORT Observer {
36 public:
37 // Invoked when CloudPolicyService has finished initializing (any initial
38 // policy load activity has completed and the CloudPolicyClient has
39 // been registered, if possible).
40 virtual void OnInitializationCompleted(CloudPolicyService* service) = 0;
41 virtual ~Observer() {}
44 // |client| and |store| must remain valid for the object life time.
45 CloudPolicyService(const std::string& policy_type,
46 const std::string& settings_entity_id,
47 CloudPolicyClient* client,
48 CloudPolicyStore* store);
49 ~CloudPolicyService() override;
51 // Returns the domain that manages this user/device, according to the current
52 // policy blob. Empty if not managed/not available.
53 std::string ManagedBy() const;
55 // Refreshes policy. |callback| will be invoked after the operation completes
56 // or aborts because of errors.
57 void RefreshPolicy(const RefreshPolicyCallback& callback);
59 // Unregisters the device. |callback| will be invoked after the operation
60 // completes or aborts because of errors. All pending refresh policy requests
61 // will be aborted, and no further refresh policy requests will be allowed.
62 void Unregister(const UnregisterCallback& callback);
64 // Adds/Removes an Observer for this object.
65 void AddObserver(Observer* observer);
66 void RemoveObserver(Observer* observer);
68 // CloudPolicyClient::Observer:
69 void OnPolicyFetched(CloudPolicyClient* client) override;
70 void OnRegistrationStateChanged(CloudPolicyClient* client) override;
71 void OnClientError(CloudPolicyClient* client) override;
73 // CloudPolicyStore::Observer:
74 void OnStoreLoaded(CloudPolicyStore* store) override;
75 void OnStoreError(CloudPolicyStore* store) override;
77 bool IsInitializationComplete() const { return initialization_complete_; }
79 private:
80 // Helper function that is called when initialization may be complete, and
81 // which is responsible for notifying observers.
82 void CheckInitializationCompleted();
84 // Invokes the refresh callbacks and clears refresh state. The |success| flag
85 // is passed through to the refresh callbacks.
86 void RefreshCompleted(bool success);
88 // Invokes the unregister callback and clears unregister state. The |success|
89 // flag is passed through to the unregister callback.
90 void UnregisterCompleted(bool success);
92 // The policy type that will be fetched by the |client_|, with the optional
93 // |settings_entity_id_|.
94 std::string policy_type_;
95 std::string settings_entity_id_;
97 // The client used to talk to the cloud.
98 CloudPolicyClient* client_;
100 // Takes care of persisting and decoding cloud policy.
101 CloudPolicyStore* store_;
103 // Tracks the state of a pending refresh operation, if any.
104 enum {
105 // No refresh pending.
106 REFRESH_NONE,
107 // Policy fetch is pending.
108 REFRESH_POLICY_FETCH,
109 // Policy store is pending.
110 REFRESH_POLICY_STORE,
111 } refresh_state_;
113 // Tracks the state of a pending unregister operation, if any.
114 enum {
115 UNREGISTER_NONE,
116 UNREGISTER_PENDING,
117 } unregister_state_;
119 // Callbacks to invoke upon policy refresh.
120 std::vector<RefreshPolicyCallback> refresh_callbacks_;
122 UnregisterCallback unregister_callback_;
124 // Set to true once the service is initialized (initial policy load/refresh
125 // is complete).
126 bool initialization_complete_;
128 // Observers who will receive notifications when the service has finished
129 // initializing.
130 base::ObserverList<Observer, true> observers_;
132 DISALLOW_COPY_AND_ASSIGN(CloudPolicyService);
135 } // namespace policy
137 #endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_SERVICE_H_