user_cloud_policy_manager_chromeos.h revision f2477e01787aa58f445919b809d89e252beef54f
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 "chrome/browser/policy/cloud/cloud_policy_client.h"
18#include "chrome/browser/policy/cloud/cloud_policy_constants.h"
19#include "chrome/browser/policy/cloud/cloud_policy_manager.h"
20#include "chrome/browser/policy/cloud/cloud_policy_service.h"
21#include "components/browser_context_keyed_service/browser_context_keyed_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;
39
40// UserCloudPolicyManagerChromeOS implements logic for initializing user policy
41// on Chrome OS.
42class UserCloudPolicyManagerChromeOS
43    : public CloudPolicyManager,
44      public CloudPolicyClient::Observer,
45      public CloudPolicyService::Observer,
46      public BrowserContextKeyedService {
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  virtual ~UserCloudPolicyManagerChromeOS();
65
66  // Initializes the cloud connection. |local_state| and
67  // |device_management_service| must stay valid until this object is deleted.
68  void Connect(PrefService* local_state,
69               DeviceManagementService* device_management_service,
70               scoped_refptr<net::URLRequestContextGetter> request_context,
71               UserAffiliation user_affiliation);
72
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  // BrowserContextKeyedServices 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);
83
84  // Returns true if the underlying CloudPolicyClient is already registered.
85  bool IsClientRegistered() const;
86
87  // ConfigurationPolicyProvider:
88  virtual void Shutdown() OVERRIDE;
89  virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE;
90
91  // CloudPolicyService::Observer:
92  virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
93
94  // CloudPolicyClient::Observer:
95  virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
96  virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
97  virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
98
99  // ComponentCloudPolicyService::Delegate:
100  virtual void OnComponentCloudPolicyUpdated() OVERRIDE;
101
102 private:
103  // Fetches a policy token using the authentication context of the signin
104  // Profile, and calls back to OnOAuth2PolicyTokenFetched when done.
105  void FetchPolicyOAuthTokenUsingSigninProfile();
106
107  // Called once the policy access token is available, and starts the
108  // registration with the policy server if the token was successfully fetched.
109  void OnOAuth2PolicyTokenFetched(const std::string& policy_token,
110                                  const GoogleServiceAuthError& error);
111
112  // Completion handler for the explicit policy fetch triggered on startup in
113  // case |wait_for_policy_fetch_| is true. |success| is true if the fetch was
114  // successful.
115  void OnInitialPolicyFetchComplete(bool success);
116
117  // Cancels waiting for the policy fetch and flags the
118  // ConfigurationPolicyProvider ready (assuming all other initialization tasks
119  // have completed).
120  void CancelWaitForPolicyFetch();
121
122  void StartRefreshSchedulerIfReady();
123
124  // Owns the store, note that CloudPolicyManager just keeps a plain pointer.
125  scoped_ptr<CloudPolicyStore> store_;
126
127  // Manages external data referenced by policies.
128  scoped_ptr<CloudExternalDataManager> external_data_manager_;
129
130  // Path where policy for components will be cached.
131  base::FilePath component_policy_cache_path_;
132
133  // Whether to wait for a policy fetch to complete before reporting
134  // IsInitializationComplete().
135  bool wait_for_policy_fetch_;
136
137  // A timer that puts a hard limit on the maximum time to wait for the intial
138  // policy fetch.
139  base::Timer policy_fetch_timeout_;
140
141  // The pref service to pass to the refresh scheduler on initialization.
142  PrefService* local_state_;
143
144  // Used to fetch the policy OAuth token, when necessary. This object holds
145  // a callback with an unretained reference to the manager, when it exists.
146  scoped_ptr<PolicyOAuth2TokenFetcher> token_fetcher_;
147
148  // The access token passed to OnAccessTokenAvailable. It is stored here so
149  // that it can be used if OnInitializationCompleted is called later.
150  std::string access_token_;
151
152  // Timestamps for collecting timing UMA stats.
153  base::Time time_init_started_;
154  base::Time time_init_completed_;
155  base::Time time_token_available_;
156  base::Time time_client_registered_;
157
158  DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOS);
159};
160
161}  // namespace policy
162
163#endif  // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
164