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