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 AndroidProfileOAuth2TokenService; 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 #if defined(OS_ANDROID) 44 // Starts the client registration process. This version uses the 45 // AndroidProfileOAuth2TokenService to mint the new token for the userinfo 46 // and DM services, using the |username| account. 47 // |callback| is invoked when the registration is complete. 48 void StartRegistration(AndroidProfileOAuth2TokenService* token_service, 49 const std::string& username, 50 const base::Closure& callback); 51 #else 52 // Starts the client registration process. The |login_refresh_token| is used 53 // to mint a new token for the userinfo and DM services. 54 // |callback| is invoked when the registration is complete. 55 void StartRegistrationWithLoginToken(const std::string& login_refresh_token, 56 const base::Closure& callback); 57 #endif 58 59 private: 60 #if defined(OS_ANDROID) 61 class TokenHelperAndroid; 62 #else 63 class TokenHelper; 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 used to fetch the access token. There is an OS_ANDROID 83 // implementation which uses the AccountManager and a known account name, 84 // and a desktop implementation which uses an OAuth2AccessTokenFetcher. 85 #if defined(OS_ANDROID) 86 scoped_ptr<TokenHelperAndroid> token_helper_; 87 #else 88 scoped_ptr<TokenHelper> token_helper_; 89 #endif 90 91 // Helper class for fetching information from GAIA about the currently 92 // signed-in user. 93 scoped_ptr<UserInfoFetcher> user_info_fetcher_; 94 95 // Access token used to register the CloudPolicyClient and also access 96 // GAIA to get information about the signed in user. 97 std::string oauth_access_token_; 98 99 net::URLRequestContextGetter* context_; 100 CloudPolicyClient* client_; 101 bool should_force_load_policy_; 102 enterprise_management::DeviceRegisterRequest::Type registration_type_; 103 base::Closure callback_; 104 105 DISALLOW_COPY_AND_ASSIGN(CloudPolicyClientRegistrationHelper); 106 }; 107 108 } // namespace policy 109 110 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_CLIENT_REGISTRATION_HELPER_H_ 111