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