user_cloud_policy_manager_chromeos.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 "chrome/browser/policy/cloud/cloud_policy_client.h"
15#include "chrome/browser/policy/cloud/cloud_policy_constants.h"
16#include "chrome/browser/policy/cloud/cloud_policy_manager.h"
17#include "chrome/browser/policy/cloud/cloud_policy_service.h"
18#include "chrome/browser/policy/cloud/component_cloud_policy_service.h"
19#include "chrome/browser/profiles/profile_keyed_service.h"
20#include "google_apis/gaia/gaia_auth_consumer.h"
21
22class PrefService;
23
24namespace net {
25class URLRequestContextGetter;
26}
27
28namespace policy {
29
30class DeviceManagementService;
31class ResourceCache;
32class PolicyOAuth2TokenFetcher;
33
34// UserCloudPolicyManagerChromeOS implements logic for initializing user policy
35// on Chrome OS.
36class UserCloudPolicyManagerChromeOS
37    : public CloudPolicyManager,
38      public CloudPolicyClient::Observer,
39      public CloudPolicyService::Observer,
40      public ComponentCloudPolicyService::Delegate,
41      public ProfileKeyedService {
42 public:
43  // If |wait_for_policy_fetch| is true, IsInitializationComplete() will return
44  // false as long as there hasn't been a successful policy fetch.
45  UserCloudPolicyManagerChromeOS(
46      scoped_ptr<CloudPolicyStore> store,
47      scoped_ptr<ResourceCache> resource_cache,
48      bool wait_for_policy_fetch);
49  virtual ~UserCloudPolicyManagerChromeOS();
50
51  // Initializes the cloud connection. |local_state| and
52  // |device_management_service| must stay valid until this object is deleted.
53  void Connect(PrefService* local_state,
54               DeviceManagementService* device_management_service,
55               scoped_refptr<net::URLRequestContextGetter> request_context,
56               UserAffiliation user_affiliation);
57
58  // The OAuth2 login |refresh_token| can be used to obtain a policy OAuth2
59  // token, if the CloudPolicyClient isn't registered yet.
60  void OnRefreshTokenAvailable(const std::string& refresh_token);
61
62  // Returns true if the underlying CloudPolicyClient is already registered.
63  bool IsClientRegistered() const;
64
65  // Returns the OAuth2 tokens obtained by the manager for the initial
66  // registration, if it had to perform a blocking policy fetch.
67  const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens() const {
68    return oauth2_login_tokens_;
69  }
70
71  // ConfigurationPolicyProvider:
72  virtual void Shutdown() OVERRIDE;
73  virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE;
74  virtual void RegisterPolicyDomain(
75      PolicyDomain domain,
76      const std::set<std::string>& component_ids) OVERRIDE;
77
78  // CloudPolicyManager:
79  virtual scoped_ptr<PolicyBundle> CreatePolicyBundle() OVERRIDE;
80
81  // CloudPolicyService::Observer:
82  virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
83
84  // CloudPolicyClient::Observer:
85  virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
86  virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
87  virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
88
89  // ComponentCloudPolicyService::Delegate:
90  virtual void OnComponentCloudPolicyRefreshNeeded() OVERRIDE;
91  virtual void OnComponentCloudPolicyUpdated() OVERRIDE;
92
93 private:
94  // These methods fetch a policy token using either the authentication context
95  // of the signin Profile or a refresh token passed in OnRefreshTokenAvailable.
96  // OnOAuth2PolicyTokenFetched is called back when the policy token is fetched.
97  void FetchPolicyOAuthTokenUsingSigninProfile();
98  void FetchPolicyOAuthTokenUsingRefreshToken();
99  void OnOAuth2PolicyTokenFetched(const std::string& policy_token);
100
101  // Completion handler for the explicit policy fetch triggered on startup in
102  // case |wait_for_policy_fetch_| is true. |success| is true if the fetch was
103  // successful.
104  void OnInitialPolicyFetchComplete(bool success);
105
106  // Cancels waiting for the policy fetch and flags the
107  // ConfigurationPolicyProvider ready (assuming all other initialization tasks
108  // have completed).
109  void CancelWaitForPolicyFetch();
110
111  void StartRefreshScheduler();
112
113  // Owns the store, note that CloudPolicyManager just keeps a plain pointer.
114  scoped_ptr<CloudPolicyStore> store_;
115
116  // Handles fetching and storing cloud policy for components. It uses the
117  // |store_|, so destroy it first.
118  scoped_ptr<ComponentCloudPolicyService> component_policy_service_;
119
120  // Whether to wait for a policy fetch to complete before reporting
121  // IsInitializationComplete().
122  bool wait_for_policy_fetch_;
123
124  // The pref service to pass to the refresh scheduler on initialization.
125  PrefService* local_state_;
126
127  // Used to fetch the policy OAuth token, when necessary. This object holds
128  // a callback with an unretained reference to the manager, when it exists.
129  scoped_ptr<PolicyOAuth2TokenFetcher> token_fetcher_;
130
131  // The OAuth2 login tokens fetched by the |token_fetcher_|, which can be
132  // retrieved using oauth2_tokens().
133  GaiaAuthConsumer::ClientOAuthResult oauth2_login_tokens_;
134
135  DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOS);
136};
137
138}  // namespace policy
139
140#endif  // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_MANAGER_CHROMEOS_H_
141