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 CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "chrome/browser/policy/cloud/cloud_policy_client.h"
17 #include "chrome/browser/policy/cloud/cloud_policy_constants.h"
18 #include "chrome/browser/policy/cloud/cloud_policy_manager.h"
19 #include "chrome/browser/policy/cloud/cloud_policy_service.h"
20 #include "chrome/browser/policy/cloud/component_cloud_policy_service.h"
21 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
23 class GoogleServiceAuthError;
27 class SequencedTaskRunner;
31 class URLRequestContextGetter;
36 class CloudExternalDataManager;
37 class DeviceManagementService;
38 class PolicyOAuth2TokenFetcher;
41 // UserCloudPolicyManagerChromeOS implements logic for initializing user policy
43 class UserCloudPolicyManagerChromeOS
44 : public CloudPolicyManager,
45 public CloudPolicyClient::Observer,
46 public CloudPolicyService::Observer,
47 public ComponentCloudPolicyService::Delegate,
48 public BrowserContextKeyedService {
50 // |task_runner| is the runner for policy refresh tasks.
51 // If |wait_for_policy_fetch| is true, IsInitializationComplete() will return
52 // false as long as there hasn't been a successful policy fetch.
53 UserCloudPolicyManagerChromeOS(
54 scoped_ptr<CloudPolicyStore> store,
55 scoped_ptr<CloudExternalDataManager> external_data_manager,
56 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
57 scoped_ptr<ResourceCache> resource_cache,
58 bool wait_for_policy_fetch,
59 base::TimeDelta initial_policy_fetch_timeout);
60 virtual ~UserCloudPolicyManagerChromeOS();
62 // Initializes the cloud connection. |local_state| and
63 // |device_management_service| must stay valid until this object is deleted.
64 void Connect(PrefService* local_state,
65 DeviceManagementService* device_management_service,
66 scoped_refptr<net::URLRequestContextGetter> request_context,
67 UserAffiliation user_affiliation);
69 // This class is one of the policy providers, and must be ready for the
70 // creation of the Profile's PrefService; all the other
71 // BrowserContextKeyedServices depend on the PrefService, so this class can't
72 // depend on other BCKS to avoid a circular dependency. So instead of using
73 // the ProfileOAuth2TokenService directly to get the access token, a 3rd
74 // service (UserCloudPolicyTokenForwarder) will fetch it later and pass it
75 // to this method once available.
76 // The |access_token| can then be used to authenticate the registration
77 // request to the DMServer.
78 void OnAccessTokenAvailable(const std::string& access_token);
80 // Returns true if the underlying CloudPolicyClient is already registered.
81 bool IsClientRegistered() const;
83 // ConfigurationPolicyProvider:
84 virtual void Shutdown() OVERRIDE;
85 virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE;
86 virtual void RegisterPolicyDomain(
87 scoped_refptr<const PolicyDomainDescriptor> descriptor) OVERRIDE;
89 // CloudPolicyManager:
90 virtual scoped_ptr<PolicyBundle> CreatePolicyBundle() OVERRIDE;
92 // CloudPolicyService::Observer:
93 virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
95 // CloudPolicyClient::Observer:
96 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
97 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
98 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
100 // ComponentCloudPolicyService::Delegate:
101 virtual void OnComponentCloudPolicyRefreshNeeded() OVERRIDE;
102 virtual void OnComponentCloudPolicyUpdated() OVERRIDE;
105 // Fetches a policy token using the authentication context of the signin
106 // Profile, and calls back to OnOAuth2PolicyTokenFetched when done.
107 void FetchPolicyOAuthTokenUsingSigninProfile();
109 // Called once the policy access token is available, and starts the
110 // registration with the policy server if the token was successfully fetched.
111 void OnOAuth2PolicyTokenFetched(const std::string& policy_token,
112 const GoogleServiceAuthError& error);
114 // Completion handler for the explicit policy fetch triggered on startup in
115 // case |wait_for_policy_fetch_| is true. |success| is true if the fetch was
117 void OnInitialPolicyFetchComplete(bool success);
119 // Cancels waiting for the policy fetch and flags the
120 // ConfigurationPolicyProvider ready (assuming all other initialization tasks
122 void CancelWaitForPolicyFetch();
124 void StartRefreshSchedulerIfReady();
126 // Owns the store, note that CloudPolicyManager just keeps a plain pointer.
127 scoped_ptr<CloudPolicyStore> store_;
129 // Manages external data referenced by policies.
130 scoped_ptr<CloudExternalDataManager> external_data_manager_;
132 // Handles fetching and storing cloud policy for components. It uses the
133 // |store_|, so destroy it first.
134 scoped_ptr<ComponentCloudPolicyService> component_policy_service_;
136 // Whether to wait for a policy fetch to complete before reporting
137 // IsInitializationComplete().
138 bool wait_for_policy_fetch_;
140 // A timer that puts a hard limit on the maximum time to wait for the intial
142 base::Timer policy_fetch_timeout_;
144 // The pref service to pass to the refresh scheduler on initialization.
145 PrefService* local_state_;
147 // Used to fetch the policy OAuth token, when necessary. This object holds
148 // a callback with an unretained reference to the manager, when it exists.
149 scoped_ptr<PolicyOAuth2TokenFetcher> token_fetcher_;
151 // The access token passed to OnAccessTokenAvailable. It is stored here so
152 // that it can be used if OnInitializationCompleted is called later.
153 std::string access_token_;
155 // Timestamps for collecting timing UMA stats.
156 base::Time time_init_started_;
157 base::Time time_init_completed_;
158 base::Time time_token_available_;
159 base::Time time_client_registered_;
161 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOS);
164 } // namespace policy
166 #endif // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_