user_cloud_policy_manager_chromeos.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
6#define CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
7
8#include <string>
9
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"
22
23class GoogleServiceAuthError;
24class PrefService;
25
26namespace base {
27class SequencedTaskRunner;
28}
29
30namespace net {
31class URLRequestContextGetter;
32}
33
34namespace policy {
35
36class CloudExternalDataManager;
37class DeviceManagementService;
38class PolicyOAuth2TokenFetcher;
39class WildcardLoginChecker;
40
41// UserCloudPolicyManagerChromeOS implements logic for initializing user policy
42// on Chrome OS.
43class UserCloudPolicyManagerChromeOS
44    : public CloudPolicyManager,
45      public CloudPolicyClient::Observer,
46      public CloudPolicyService::Observer,
47      public BrowserContextKeyedService {
48 public:
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
55  // BrowserThread.
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();
66
67  // Initializes the cloud connection. |local_state| and
68  // |device_management_service| must stay valid until this object is deleted.
69  void Connect(
70      PrefService* local_state,
71      DeviceManagementService* device_management_service,
72      scoped_refptr<net::URLRequestContextGetter> system_request_context,
73      UserAffiliation user_affiliation);
74
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);
85
86  // Returns true if the underlying CloudPolicyClient is already registered.
87  bool IsClientRegistered() const;
88
89  // Indicates a wildcard login check should be performed once an access token
90  // is available.
91  void EnableWildcardLoginCheck(const std::string& username);
92
93  // ConfigurationPolicyProvider:
94  virtual void Shutdown() OVERRIDE;
95  virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE;
96
97  // CloudPolicyService::Observer:
98  virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
99
100  // CloudPolicyClient::Observer:
101  virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
102  virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
103  virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
104
105  // ComponentCloudPolicyService::Delegate:
106  virtual void OnComponentCloudPolicyUpdated() OVERRIDE;
107
108 private:
109  // Fetches a policy token using the authentication context of the signin
110  // Profile, and calls back to OnOAuth2PolicyTokenFetched when done.
111  void FetchPolicyOAuthTokenUsingSigninProfile();
112
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);
117
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
120  // successful.
121  void OnInitialPolicyFetchComplete(bool success);
122
123  // Called when |policy_fetch_timeout_| times out, to cancel the blocking
124  // wait for the initial policy fetch.
125  void OnBlockingFetchTimeout();
126
127  // Cancels waiting for the policy fetch and flags the
128  // ConfigurationPolicyProvider ready (assuming all other initialization tasks
129  // have completed).
130  void CancelWaitForPolicyFetch();
131
132  void StartRefreshSchedulerIfReady();
133
134  // Owns the store, note that CloudPolicyManager just keeps a plain pointer.
135  scoped_ptr<CloudPolicyStore> store_;
136
137  // Manages external data referenced by policies.
138  scoped_ptr<CloudExternalDataManager> external_data_manager_;
139
140  // Username for the wildcard login check if applicable, empty otherwise.
141  std::string wildcard_username_;
142
143  // Path where policy for components will be cached.
144  base::FilePath component_policy_cache_path_;
145
146  // Whether to wait for a policy fetch to complete before reporting
147  // IsInitializationComplete().
148  bool wait_for_policy_fetch_;
149
150  // A timer that puts a hard limit on the maximum time to wait for the intial
151  // policy fetch.
152  base::Timer policy_fetch_timeout_;
153
154  // The pref service to pass to the refresh scheduler on initialization.
155  PrefService* local_state_;
156
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_;
160
161  // Keeps alive the wildcard checker while its running.
162  scoped_ptr<WildcardLoginChecker> wildcard_login_checker_;
163
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_;
167
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_;
173
174  DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOS);
175};
176
177}  // namespace policy
178
179#endif  // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
180