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/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
18 #include "components/policy/core/common/cloud/cloud_policy_client.h"
19 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
20 #include "components/policy/core/common/cloud/cloud_policy_manager.h"
21 #include "components/policy/core/common/cloud/cloud_policy_service.h"
23 class GoogleServiceAuthError;
27 class SequencedTaskRunner;
31 class URLRequestContextGetter;
36 class CloudExternalDataManager;
37 class DeviceManagementService;
38 class PolicyOAuth2TokenFetcher;
39 class WildcardLoginChecker;
41 // UserCloudPolicyManagerChromeOS implements logic for initializing user policy
43 class UserCloudPolicyManagerChromeOS
44 : public CloudPolicyManager,
45 public CloudPolicyClient::Observer,
46 public CloudPolicyService::Observer,
47 public BrowserContextKeyedService {
49 // If |wait_for_policy_fetch| is true, IsInitializationComplete() will return
50 // false as long as there hasn't been a successful policy fetch.
51 // |task_runner| is the runner for policy refresh tasks.
52 // |file_task_runner| is used for file operations. Currently this must be the
53 // FILE BrowserThread.
54 // |io_task_runner| is used for network IO. Currently this must be the IO
56 UserCloudPolicyManagerChromeOS(
57 scoped_ptr<CloudPolicyStore> store,
58 scoped_ptr<CloudExternalDataManager> external_data_manager,
59 const base::FilePath& component_policy_cache_path,
60 bool wait_for_policy_fetch,
61 base::TimeDelta initial_policy_fetch_timeout,
62 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
63 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
64 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
65 virtual ~UserCloudPolicyManagerChromeOS();
67 // Initializes the cloud connection. |local_state| and
68 // |device_management_service| must stay valid until this object is deleted.
70 PrefService* local_state,
71 DeviceManagementService* device_management_service,
72 scoped_refptr<net::URLRequestContextGetter> system_request_context,
73 UserAffiliation user_affiliation);
75 // This class is one of the policy providers, and must be ready for the
76 // creation of the Profile's PrefService; all the other
77 // BrowserContextKeyedServices depend on the PrefService, so this class can't
78 // depend on other BCKS to avoid a circular dependency. So instead of using
79 // the ProfileOAuth2TokenService directly to get the access token, a 3rd
80 // service (UserCloudPolicyTokenForwarder) will fetch it later and pass it
81 // to this method once available.
82 // The |access_token| can then be used to authenticate the registration
83 // request to the DMServer.
84 void OnAccessTokenAvailable(const std::string& access_token);
86 // Returns true if the underlying CloudPolicyClient is already registered.
87 bool IsClientRegistered() const;
89 // Indicates a wildcard login check should be performed once an access token
91 void EnableWildcardLoginCheck(const std::string& username);
93 // ConfigurationPolicyProvider:
94 virtual void Shutdown() OVERRIDE;
95 virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE;
97 // CloudPolicyService::Observer:
98 virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
100 // CloudPolicyClient::Observer:
101 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
102 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
103 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
105 // ComponentCloudPolicyService::Delegate:
106 virtual void OnComponentCloudPolicyUpdated() OVERRIDE;
109 // Fetches a policy token using the authentication context of the signin
110 // Profile, and calls back to OnOAuth2PolicyTokenFetched when done.
111 void FetchPolicyOAuthTokenUsingSigninProfile();
113 // Called once the policy access token is available, and starts the
114 // registration with the policy server if the token was successfully fetched.
115 void OnOAuth2PolicyTokenFetched(const std::string& policy_token,
116 const GoogleServiceAuthError& error);
118 // Completion handler for the explicit policy fetch triggered on startup in
119 // case |wait_for_policy_fetch_| is true. |success| is true if the fetch was
121 void OnInitialPolicyFetchComplete(bool success);
123 // Called when |policy_fetch_timeout_| times out, to cancel the blocking
124 // wait for the initial policy fetch.
125 void OnBlockingFetchTimeout();
127 // Cancels waiting for the policy fetch and flags the
128 // ConfigurationPolicyProvider ready (assuming all other initialization tasks
130 void CancelWaitForPolicyFetch();
132 void StartRefreshSchedulerIfReady();
134 // Owns the store, note that CloudPolicyManager just keeps a plain pointer.
135 scoped_ptr<CloudPolicyStore> store_;
137 // Manages external data referenced by policies.
138 scoped_ptr<CloudExternalDataManager> external_data_manager_;
140 // Username for the wildcard login check if applicable, empty otherwise.
141 std::string wildcard_username_;
143 // Path where policy for components will be cached.
144 base::FilePath component_policy_cache_path_;
146 // Whether to wait for a policy fetch to complete before reporting
147 // IsInitializationComplete().
148 bool wait_for_policy_fetch_;
150 // A timer that puts a hard limit on the maximum time to wait for the intial
152 base::Timer policy_fetch_timeout_;
154 // The pref service to pass to the refresh scheduler on initialization.
155 PrefService* local_state_;
157 // Used to fetch the policy OAuth token, when necessary. This object holds
158 // a callback with an unretained reference to the manager, when it exists.
159 scoped_ptr<PolicyOAuth2TokenFetcher> token_fetcher_;
161 // Keeps alive the wildcard checker while its running.
162 scoped_ptr<WildcardLoginChecker> wildcard_login_checker_;
164 // The access token passed to OnAccessTokenAvailable. It is stored here so
165 // that it can be used if OnInitializationCompleted is called later.
166 std::string access_token_;
168 // Timestamps for collecting timing UMA stats.
169 base::Time time_init_started_;
170 base::Time time_init_completed_;
171 base::Time time_token_available_;
172 base::Time time_client_registered_;
174 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOS);
177 } // namespace policy
179 #endif // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_