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_
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"
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.
25 class POLICY_EXPORT CloudPolicyService
: public CloudPolicyClient::Observer
,
26 public CloudPolicyStore::Observer
{
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
;
32 class POLICY_EXPORT Observer
{
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() {}
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();
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;
51 // Refreshes policy. |callback| will be invoked after the operation completes
52 // or aborts because of errors.
53 void RefreshPolicy(const RefreshPolicyCallback
& callback
);
55 // Adds/Removes an Observer for this object.
56 void AddObserver(Observer
* observer
);
57 void RemoveObserver(Observer
* observer
);
59 // CloudPolicyClient::Observer:
60 virtual void OnPolicyFetched(CloudPolicyClient
* client
) OVERRIDE
;
61 virtual void OnRegistrationStateChanged(CloudPolicyClient
* client
) OVERRIDE
;
62 virtual void OnClientError(CloudPolicyClient
* client
) OVERRIDE
;
64 // CloudPolicyStore::Observer:
65 virtual void OnStoreLoaded(CloudPolicyStore
* store
) OVERRIDE
;
66 virtual void OnStoreError(CloudPolicyStore
* store
) OVERRIDE
;
68 bool IsInitializationComplete() const { return initialization_complete_
; }
71 // Helper function that is called when initialization may be complete, and
72 // which is responsible for notifying observers.
73 void CheckInitializationCompleted();
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
);
79 // The policy namespace fetched by |client_| and expected by |store_|.
80 PolicyNamespaceKey policy_ns_key_
;
82 // The client used to talk to the cloud.
83 CloudPolicyClient
* client_
;
85 // Takes care of persisting and decoding cloud policy.
86 CloudPolicyStore
* store_
;
88 // Tracks the state of a pending refresh operation, if any.
90 // No refresh pending.
92 // Policy fetch is pending.
94 // Policy store is pending.
98 // Callbacks to invoke upon policy refresh.
99 std::vector
<RefreshPolicyCallback
> refresh_callbacks_
;
101 // Set to true once the service is initialized (initial policy load/refresh
103 bool initialization_complete_
;
105 // Observers who will receive notifications when the service has finished
107 ObserverList
<Observer
, true> observers_
;
109 DISALLOW_COPY_AND_ASSIGN(CloudPolicyService
);
112 } // namespace policy
114 #endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_SERVICE_H_