- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / cloud / cloud_policy_client_registration_helper.h
1 // Copyright 2013 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_POLICY_CLOUD_CLOUD_POLICY_CLIENT_REGISTRATION_HELPER_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_CLIENT_REGISTRATION_HELPER_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/browser/policy/cloud/cloud_policy_client.h"
15 #include "chrome/browser/policy/cloud/user_info_fetcher.h"
16 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
17
18 class OAuth2TokenService;
19
20 namespace net {
21 class URLRequestContextGetter;
22 }
23
24 namespace policy {
25
26 // Helper class that registers a CloudPolicyClient. It fetches an OAuth2 token
27 // for the DM service if needed, and checks with Gaia if the account has policy
28 // management enabled.
29 class CloudPolicyClientRegistrationHelper : public UserInfoFetcher::Delegate,
30                                             public CloudPolicyClient::Observer {
31  public:
32   // |context| and |client| are not owned and must outlive this object.
33   // If |should_force_load_policy| then the cloud policy registration is
34   // performed even if Gaia indicates that this account doesn't have management
35   // enabled.
36   CloudPolicyClientRegistrationHelper(
37       net::URLRequestContextGetter* context,
38       CloudPolicyClient* client,
39       bool should_force_load_policy,
40       enterprise_management::DeviceRegisterRequest::Type registration_type);
41   virtual ~CloudPolicyClientRegistrationHelper();
42
43   // Starts the client registration process. This version uses the
44   // supplied OAuth2TokenService to mint the new token for the userinfo
45   // and DM services, using the |account_id|.
46   // |callback| is invoked when the registration is complete.
47   void StartRegistration(
48       OAuth2TokenService* token_service,
49       const std::string& account_id,
50       const base::Closure& callback);
51
52 #if !defined(OS_ANDROID)
53   // Starts the client registration process. The |login_refresh_token| is used
54   // to mint a new token for the userinfo and DM services.
55   // |callback| is invoked when the registration is complete.
56   void StartRegistrationWithLoginToken(const std::string& login_refresh_token,
57                                        const base::Closure& callback);
58 #endif
59
60  private:
61   class TokenServiceHelper;
62 #if !defined(OS_ANDROID)
63   class LoginTokenHelper;
64 #endif
65
66   void OnTokenFetched(const std::string& oauth_access_token);
67
68   // UserInfoFetcher::Delegate implementation:
69   virtual void OnGetUserInfoSuccess(
70       const base::DictionaryValue* response) OVERRIDE;
71   virtual void OnGetUserInfoFailure(
72       const GoogleServiceAuthError& error) OVERRIDE;
73
74   // CloudPolicyClient::Observer implementation:
75   virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
76   virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
77   virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
78
79   // Invoked when the registration request has been completed.
80   void RequestCompleted();
81
82   // Internal helper class that uses OAuth2TokenService to fetch an OAuth
83   // access token. On desktop, this is only used after the user has signed in -
84   // desktop platforms use LoginTokenHelper for policy fetches performed before
85   // signin is complete.
86   scoped_ptr<TokenServiceHelper> token_service_helper_;
87
88 #if !defined(OS_ANDROID)
89   // Special desktop-only helper to fetch an OAuth access token prior to
90   // the completion of signin. Not used on Android since all token fetching
91   // is done via OAuth2TokenService.
92   scoped_ptr<LoginTokenHelper> login_token_helper_;
93 #endif
94
95   // Helper class for fetching information from GAIA about the currently
96   // signed-in user.
97   scoped_ptr<UserInfoFetcher> user_info_fetcher_;
98
99   // Access token used to register the CloudPolicyClient and also access
100   // GAIA to get information about the signed in user.
101   std::string oauth_access_token_;
102
103   net::URLRequestContextGetter* context_;
104   CloudPolicyClient* client_;
105   bool should_force_load_policy_;
106   enterprise_management::DeviceRegisterRequest::Type registration_type_;
107   base::Closure callback_;
108
109   DISALLOW_COPY_AND_ASSIGN(CloudPolicyClientRegistrationHelper);
110 };
111
112 }  // namespace policy
113
114 #endif  // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_CLIENT_REGISTRATION_HELPER_H_