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