Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / user_cloud_policy_manager_chromeos.h
blobe7001e54753519e7ad6a050edeef0b67fd78f82f
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_
8 #include <string>
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/keyed_service/core/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;
24 class PrefService;
26 namespace base {
27 class SequencedTaskRunner;
30 namespace net {
31 class URLRequestContextGetter;
34 namespace policy {
36 class CloudExternalDataManager;
37 class DeviceManagementService;
38 class PolicyOAuth2TokenFetcher;
39 class WildcardLoginChecker;
41 // UserCloudPolicyManagerChromeOS implements logic for initializing user policy
42 // on Chrome OS.
43 class UserCloudPolicyManagerChromeOS : public CloudPolicyManager,
44 public CloudPolicyClient::Observer,
45 public CloudPolicyService::Observer,
46 public KeyedService {
47 public:
48 // If |wait_for_policy_fetch| is true, IsInitializationComplete() will return
49 // false as long as there hasn't been a successful policy fetch.
50 // |task_runner| is the runner for policy refresh tasks.
51 // |file_task_runner| is used for file operations. Currently this must be the
52 // FILE BrowserThread.
53 // |io_task_runner| is used for network IO. Currently this must be the IO
54 // BrowserThread.
55 UserCloudPolicyManagerChromeOS(
56 scoped_ptr<CloudPolicyStore> store,
57 scoped_ptr<CloudExternalDataManager> external_data_manager,
58 const base::FilePath& component_policy_cache_path,
59 bool wait_for_policy_fetch,
60 base::TimeDelta initial_policy_fetch_timeout,
61 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
62 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
63 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
64 ~UserCloudPolicyManagerChromeOS() override;
66 // Initializes the cloud connection. |local_state| and
67 // |device_management_service| must stay valid until this object is deleted.
68 void Connect(
69 PrefService* local_state,
70 DeviceManagementService* device_management_service,
71 scoped_refptr<net::URLRequestContextGetter> system_request_context);
73 // This class is one of the policy providers, and must be ready for the
74 // creation of the Profile's PrefService; all the other
75 // KeyedServices depend on the PrefService, so this class can't
76 // depend on other BCKS to avoid a circular dependency. So instead of using
77 // the ProfileOAuth2TokenService directly to get the access token, a 3rd
78 // service (UserCloudPolicyTokenForwarder) will fetch it later and pass it
79 // to this method once available.
80 // The |access_token| can then be used to authenticate the registration
81 // request to the DMServer.
82 void OnAccessTokenAvailable(const std::string& access_token);
84 // Returns true if the underlying CloudPolicyClient is already registered.
85 bool IsClientRegistered() const;
87 // Indicates a wildcard login check should be performed once an access token
88 // is available.
89 void EnableWildcardLoginCheck(const std::string& username);
91 // ConfigurationPolicyProvider:
92 void Shutdown() override;
93 bool IsInitializationComplete(PolicyDomain domain) const override;
95 // CloudPolicyService::Observer:
96 void OnInitializationCompleted(CloudPolicyService* service) override;
98 // CloudPolicyClient::Observer:
99 void OnPolicyFetched(CloudPolicyClient* client) override;
100 void OnRegistrationStateChanged(CloudPolicyClient* client) override;
101 void OnClientError(CloudPolicyClient* client) override;
103 // ComponentCloudPolicyService::Delegate:
104 void OnComponentCloudPolicyUpdated() override;
106 // CloudPolicyManager:
107 void OnStoreLoaded(CloudPolicyStore* cloud_policy_store) override;
109 protected:
110 // CloudPolicyManager:
111 void GetChromePolicy(PolicyMap* policy_map) override;
113 private:
114 // Fetches a policy token using the refresh token if available, or the
115 // authentication context of the signin context, and calls back
116 // OnOAuth2PolicyTokenFetched when done.
117 void FetchPolicyOAuthToken();
119 // Called once the policy access token is available, and starts the
120 // registration with the policy server if the token was successfully fetched.
121 void OnOAuth2PolicyTokenFetched(const std::string& policy_token,
122 const GoogleServiceAuthError& error);
124 // Completion handler for the explicit policy fetch triggered on startup in
125 // case |wait_for_policy_fetch_| is true. |success| is true if the fetch was
126 // successful.
127 void OnInitialPolicyFetchComplete(bool success);
129 // Called when |policy_fetch_timeout_| times out, to cancel the blocking
130 // wait for the initial policy fetch.
131 void OnBlockingFetchTimeout();
133 // Cancels waiting for the policy fetch and flags the
134 // ConfigurationPolicyProvider ready (assuming all other initialization tasks
135 // have completed).
136 void CancelWaitForPolicyFetch();
138 void StartRefreshSchedulerIfReady();
140 // Owns the store, note that CloudPolicyManager just keeps a plain pointer.
141 scoped_ptr<CloudPolicyStore> store_;
143 // Manages external data referenced by policies.
144 scoped_ptr<CloudExternalDataManager> external_data_manager_;
146 // Username for the wildcard login check if applicable, empty otherwise.
147 std::string wildcard_username_;
149 // Path where policy for components will be cached.
150 base::FilePath component_policy_cache_path_;
152 // Whether to wait for a policy fetch to complete before reporting
153 // IsInitializationComplete().
154 bool wait_for_policy_fetch_;
156 // A timer that puts a hard limit on the maximum time to wait for the initial
157 // policy fetch.
158 base::Timer policy_fetch_timeout_;
160 // The pref service to pass to the refresh scheduler on initialization.
161 PrefService* local_state_;
163 // Used to fetch the policy OAuth token, when necessary. This object holds
164 // a callback with an unretained reference to the manager, when it exists.
165 scoped_ptr<PolicyOAuth2TokenFetcher> token_fetcher_;
167 // Keeps alive the wildcard checker while its running.
168 scoped_ptr<WildcardLoginChecker> wildcard_login_checker_;
170 // The access token passed to OnAccessTokenAvailable. It is stored here so
171 // that it can be used if OnInitializationCompleted is called later.
172 std::string access_token_;
174 // Timestamps for collecting timing UMA stats.
175 base::Time time_init_started_;
176 base::Time time_init_completed_;
177 base::Time time_token_available_;
178 base::Time time_client_registered_;
180 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOS);
183 } // namespace policy
185 #endif // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_