user_cloud_policy_manager_chromeos.h revision 58537e28ecd584eab876aee8be7156509866d23a
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/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"
22
23class GoogleServiceAuthError;
24class PrefService;
25
26namespace net {
27class URLRequestContextGetter;
28}
29
30namespace policy {
31
32class DeviceManagementService;
33class PolicyOAuth2TokenFetcher;
34class ResourceCache;
35
36// UserCloudPolicyManagerChromeOS implements logic for initializing user policy
37// on Chrome OS.
38class UserCloudPolicyManagerChromeOS
39    : public CloudPolicyManager,
40      public CloudPolicyClient::Observer,
41      public CloudPolicyService::Observer,
42      public ComponentCloudPolicyService::Delegate,
43      public BrowserContextKeyedService {
44 public:
45  // If |wait_for_policy_fetch| is true, IsInitializationComplete() will return
46  // false as long as there hasn't been a successful policy fetch.
47  UserCloudPolicyManagerChromeOS(
48      scoped_ptr<CloudPolicyStore> store,
49      scoped_ptr<ResourceCache> resource_cache,
50      bool wait_for_policy_fetch,
51      base::TimeDelta initial_policy_fetch_timeout);
52  virtual ~UserCloudPolicyManagerChromeOS();
53
54  // Initializes the cloud connection. |local_state| and
55  // |device_management_service| must stay valid until this object is deleted.
56  void Connect(PrefService* local_state,
57               DeviceManagementService* device_management_service,
58               scoped_refptr<net::URLRequestContextGetter> request_context,
59               UserAffiliation user_affiliation);
60
61  // This class is one of the policy providers, and must be ready for the
62  // creation of the Profile's PrefService; all the other
63  // BrowserContextKeyedServices depend on the PrefService, so this class can't
64  // depend on other BCKS to avoid a circular dependency. So instead of using
65  // the ProfileOAuth2TokenService directly to get the access token, a 3rd
66  // service (UserCloudPolicyTokenForwarder) will fetch it later and pass it
67  // to this method once available.
68  // The |access_token| can then be used to authenticate the registration
69  // request to the DMServer.
70  void OnAccessTokenAvailable(const std::string& access_token);
71
72  // Returns true if the underlying CloudPolicyClient is already registered.
73  bool IsClientRegistered() const;
74
75  // ConfigurationPolicyProvider:
76  virtual void Shutdown() OVERRIDE;
77  virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE;
78  virtual void RegisterPolicyDomain(
79      scoped_refptr<const PolicyDomainDescriptor> descriptor) OVERRIDE;
80
81  // CloudPolicyManager:
82  virtual scoped_ptr<PolicyBundle> CreatePolicyBundle() OVERRIDE;
83
84  // CloudPolicyService::Observer:
85  virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
86
87  // CloudPolicyClient::Observer:
88  virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
89  virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
90  virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
91
92  // ComponentCloudPolicyService::Delegate:
93  virtual void OnComponentCloudPolicyRefreshNeeded() OVERRIDE;
94  virtual void OnComponentCloudPolicyUpdated() OVERRIDE;
95
96 private:
97  // Fetches a policy token using the authentication context of the signin
98  // Profile, and calls back to OnOAuth2PolicyTokenFetched when done.
99  void FetchPolicyOAuthTokenUsingSigninProfile();
100
101  // Called once the policy access token is available, and starts the
102  // registration with the policy server if the token was successfully fetched.
103  void OnOAuth2PolicyTokenFetched(const std::string& policy_token,
104                                  const GoogleServiceAuthError& error);
105
106  // Completion handler for the explicit policy fetch triggered on startup in
107  // case |wait_for_policy_fetch_| is true. |success| is true if the fetch was
108  // successful.
109  void OnInitialPolicyFetchComplete(bool success);
110
111  // Cancels waiting for the policy fetch and flags the
112  // ConfigurationPolicyProvider ready (assuming all other initialization tasks
113  // have completed).
114  void CancelWaitForPolicyFetch();
115
116  void StartRefreshSchedulerIfReady();
117
118  // Owns the store, note that CloudPolicyManager just keeps a plain pointer.
119  scoped_ptr<CloudPolicyStore> store_;
120
121  // Handles fetching and storing cloud policy for components. It uses the
122  // |store_|, so destroy it first.
123  scoped_ptr<ComponentCloudPolicyService> component_policy_service_;
124
125  // Whether to wait for a policy fetch to complete before reporting
126  // IsInitializationComplete().
127  bool wait_for_policy_fetch_;
128
129  // A timer that puts a hard limit on the maximum time to wait for the intial
130  // policy fetch.
131  base::Timer policy_fetch_timeout_;
132
133  // The pref service to pass to the refresh scheduler on initialization.
134  PrefService* local_state_;
135
136  // Used to fetch the policy OAuth token, when necessary. This object holds
137  // a callback with an unretained reference to the manager, when it exists.
138  scoped_ptr<PolicyOAuth2TokenFetcher> token_fetcher_;
139
140  // The access token passed to OnAccessTokenAvailable. It is stored here so
141  // that it can be used if OnInitializationCompleted is called later.
142  std::string access_token_;
143
144  // Timestamps for collecting timing UMA stats.
145  base::Time time_init_started_;
146  base::Time time_init_completed_;
147  base::Time time_token_available_;
148  base::Time time_client_registered_;
149
150  DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOS);
151};
152
153}  // namespace policy
154
155#endif  // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
156