| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/cryptauth/cryptauth_client_impl.h" |
| 6 |
| 5 #include "base/bind.h" | 7 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 7 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h" | |
| 8 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" | |
| 9 #include "components/proximity_auth/cryptauth/cryptauth_client.h" | |
| 10 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" | |
| 11 #include "components/proximity_auth/switches.h" | 9 #include "components/proximity_auth/switches.h" |
| 12 #include "net/url_request/url_request_context_getter.h" | |
| 13 | 10 |
| 14 namespace proximity_auth { | 11 namespace proximity_auth { |
| 15 | 12 |
| 16 namespace { | 13 namespace { |
| 17 | 14 |
| 18 // Default URL of Google APIs endpoint hosting CryptAuth. | 15 // Default URL of Google APIs endpoint hosting CryptAuth. |
| 19 const char kDefaultCryptAuthHTTPHost[] = "https://www.googleapis.com"; | 16 const char kDefaultCryptAuthHTTPHost[] = "https://www.googleapis.com"; |
| 20 | 17 |
| 21 // URL subpath hosting the CryptAuth service. | 18 // URL subpath hosting the CryptAuth service. |
| 22 const char kCryptAuthPath[] = "cryptauth/v1/"; | 19 const char kCryptAuthPath[] = "cryptauth/v1/"; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 40 GURL google_apis_url = | 37 GURL google_apis_url = |
| 41 GURL(command_line->HasSwitch(switches::kCryptAuthHTTPHost) | 38 GURL(command_line->HasSwitch(switches::kCryptAuthHTTPHost) |
| 42 ? command_line->GetSwitchValueASCII(switches::kCryptAuthHTTPHost) | 39 ? command_line->GetSwitchValueASCII(switches::kCryptAuthHTTPHost) |
| 43 : kDefaultCryptAuthHTTPHost); | 40 : kDefaultCryptAuthHTTPHost); |
| 44 return google_apis_url.Resolve(kCryptAuthPath + request_path + | 41 return google_apis_url.Resolve(kCryptAuthPath + request_path + |
| 45 kQueryProtobuf); | 42 kQueryProtobuf); |
| 46 } | 43 } |
| 47 | 44 |
| 48 } // namespace | 45 } // namespace |
| 49 | 46 |
| 50 CryptAuthClient::CryptAuthClient( | 47 CryptAuthClientImpl::CryptAuthClientImpl( |
| 48 scoped_ptr<CryptAuthApiCallFlow> api_call_flow, |
| 51 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, | 49 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, |
| 52 scoped_refptr<net::URLRequestContextGetter> url_request_context, | 50 scoped_refptr<net::URLRequestContextGetter> url_request_context, |
| 53 const cryptauth::DeviceClassifier& device_classifier) | 51 const cryptauth::DeviceClassifier& device_classifier) |
| 54 : url_request_context_(url_request_context), | 52 : api_call_flow_(api_call_flow.Pass()), |
| 55 access_token_fetcher_(access_token_fetcher.Pass()), | 53 access_token_fetcher_(access_token_fetcher.Pass()), |
| 54 url_request_context_(url_request_context), |
| 56 device_classifier_(device_classifier), | 55 device_classifier_(device_classifier), |
| 56 call_started_(false), |
| 57 weak_ptr_factory_(this) { | 57 weak_ptr_factory_(this) { |
| 58 } | 58 } |
| 59 | 59 |
| 60 CryptAuthClient::~CryptAuthClient() { | 60 CryptAuthClientImpl::~CryptAuthClientImpl() { |
| 61 } | 61 } |
| 62 | 62 |
| 63 void CryptAuthClient::GetMyDevices( | 63 void CryptAuthClientImpl::GetMyDevices( |
| 64 const cryptauth::GetMyDevicesRequest& request, | 64 const cryptauth::GetMyDevicesRequest& request, |
| 65 const GetMyDevicesCallback& callback, | 65 const GetMyDevicesCallback& callback, |
| 66 const ErrorCallback& error_callback) { | 66 const ErrorCallback& error_callback) { |
| 67 MakeApiCall(kGetMyDevicesPath, request, callback, error_callback); | 67 MakeApiCall(kGetMyDevicesPath, request, callback, error_callback); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void CryptAuthClient::FindEligibleUnlockDevices( | 70 void CryptAuthClientImpl::FindEligibleUnlockDevices( |
| 71 const cryptauth::FindEligibleUnlockDevicesRequest& request, | 71 const cryptauth::FindEligibleUnlockDevicesRequest& request, |
| 72 const FindEligibleUnlockDevicesCallback& callback, | 72 const FindEligibleUnlockDevicesCallback& callback, |
| 73 const ErrorCallback& error_callback) { | 73 const ErrorCallback& error_callback) { |
| 74 MakeApiCall(kFindEligibleUnlockDevicesPath, request, callback, | 74 MakeApiCall(kFindEligibleUnlockDevicesPath, request, callback, |
| 75 error_callback); | 75 error_callback); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void CryptAuthClient::SendDeviceSyncTickle( | 78 void CryptAuthClientImpl::SendDeviceSyncTickle( |
| 79 const cryptauth::SendDeviceSyncTickleRequest& request, | 79 const cryptauth::SendDeviceSyncTickleRequest& request, |
| 80 const SendDeviceSyncTickleCallback& callback, | 80 const SendDeviceSyncTickleCallback& callback, |
| 81 const ErrorCallback& error_callback) { | 81 const ErrorCallback& error_callback) { |
| 82 MakeApiCall(kSendDeviceSyncTicklePath, request, callback, error_callback); | 82 MakeApiCall(kSendDeviceSyncTicklePath, request, callback, error_callback); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void CryptAuthClient::ToggleEasyUnlock( | 85 void CryptAuthClientImpl::ToggleEasyUnlock( |
| 86 const cryptauth::ToggleEasyUnlockRequest& request, | 86 const cryptauth::ToggleEasyUnlockRequest& request, |
| 87 const ToggleEasyUnlockCallback& callback, | 87 const ToggleEasyUnlockCallback& callback, |
| 88 const ErrorCallback& error_callback) { | 88 const ErrorCallback& error_callback) { |
| 89 MakeApiCall(kToggleEasyUnlockPath, request, callback, error_callback); | 89 MakeApiCall(kToggleEasyUnlockPath, request, callback, error_callback); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void CryptAuthClient::SetupEnrollment( | 92 void CryptAuthClientImpl::SetupEnrollment( |
| 93 const cryptauth::SetupEnrollmentRequest& request, | 93 const cryptauth::SetupEnrollmentRequest& request, |
| 94 const SetupEnrollmentCallback& callback, | 94 const SetupEnrollmentCallback& callback, |
| 95 const ErrorCallback& error_callback) { | 95 const ErrorCallback& error_callback) { |
| 96 MakeApiCall(kSetupEnrollmentPath, request, callback, error_callback); | 96 MakeApiCall(kSetupEnrollmentPath, request, callback, error_callback); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void CryptAuthClient::FinishEnrollment( | 99 void CryptAuthClientImpl::FinishEnrollment( |
| 100 const cryptauth::FinishEnrollmentRequest& request, | 100 const cryptauth::FinishEnrollmentRequest& request, |
| 101 const FinishEnrollmentCallback& callback, | 101 const FinishEnrollmentCallback& callback, |
| 102 const ErrorCallback& error_callback) { | 102 const ErrorCallback& error_callback) { |
| 103 MakeApiCall(kFinishEnrollmentPath, request, callback, error_callback); | 103 MakeApiCall(kFinishEnrollmentPath, request, callback, error_callback); |
| 104 } | 104 } |
| 105 | 105 |
| 106 scoped_ptr<CryptAuthApiCallFlow> CryptAuthClient::CreateFlow( | |
| 107 const GURL& request_url) { | |
| 108 return make_scoped_ptr(new CryptAuthApiCallFlow(request_url)); | |
| 109 } | |
| 110 | |
| 111 template <class RequestProto, class ResponseProto> | 106 template <class RequestProto, class ResponseProto> |
| 112 void CryptAuthClient::MakeApiCall( | 107 void CryptAuthClientImpl::MakeApiCall( |
| 113 const std::string& request_path, | 108 const std::string& request_path, |
| 114 const RequestProto& request_proto, | 109 const RequestProto& request_proto, |
| 115 const base::Callback<void(const ResponseProto&)>& response_callback, | 110 const base::Callback<void(const ResponseProto&)>& response_callback, |
| 116 const ErrorCallback& error_callback) { | 111 const ErrorCallback& error_callback) { |
| 117 if (flow_) { | 112 if (call_started_) { |
| 118 error_callback.Run( | 113 error_callback.Run( |
| 119 "Client has been used for another request. Do not reuse."); | 114 "Client has been used for another request. Do not reuse."); |
| 120 return; | 115 return; |
| 121 } | 116 } |
| 117 call_started_ = true; |
| 122 | 118 |
| 123 // The |device_classifier| field must be present for all CryptAuth requests. | 119 // The |device_classifier| field must be present for all CryptAuth requests. |
| 124 RequestProto request_copy(request_proto); | 120 RequestProto request_copy(request_proto); |
| 125 request_copy.mutable_device_classifier()->CopyFrom(device_classifier_); | 121 request_copy.mutable_device_classifier()->CopyFrom(device_classifier_); |
| 126 | 122 |
| 127 std::string serialized_request; | 123 std::string serialized_request; |
| 128 if (!request_copy.SerializeToString(&serialized_request)) { | 124 if (!request_copy.SerializeToString(&serialized_request)) { |
| 129 error_callback.Run(std::string("Failed to serialize ") + | 125 error_callback.Run(std::string("Failed to serialize ") + |
| 130 request_proto.GetTypeName() + " proto."); | 126 request_proto.GetTypeName() + " proto."); |
| 131 return; | 127 return; |
| 132 } | 128 } |
| 133 | 129 |
| 134 request_path_ = request_path; | 130 request_path_ = request_path; |
| 135 error_callback_ = error_callback; | 131 error_callback_ = error_callback; |
| 136 access_token_fetcher_->FetchAccessToken(base::Bind( | 132 access_token_fetcher_->FetchAccessToken(base::Bind( |
| 137 &CryptAuthClient::OnAccessTokenFetched<ResponseProto>, | 133 &CryptAuthClientImpl::OnAccessTokenFetched<ResponseProto>, |
| 138 weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback)); | 134 weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback)); |
| 139 } | 135 } |
| 140 | 136 |
| 141 template <class ResponseProto> | 137 template <class ResponseProto> |
| 142 void CryptAuthClient::OnAccessTokenFetched( | 138 void CryptAuthClientImpl::OnAccessTokenFetched( |
| 143 const std::string& serialized_request, | 139 const std::string& serialized_request, |
| 144 const base::Callback<void(const ResponseProto&)>& response_callback, | 140 const base::Callback<void(const ResponseProto&)>& response_callback, |
| 145 const std::string& access_token) { | 141 const std::string& access_token) { |
| 146 if (access_token.empty()) { | 142 if (access_token.empty()) { |
| 147 OnApiCallFailed("Failed to get a valid access token."); | 143 OnApiCallFailed("Failed to get a valid access token."); |
| 148 return; | 144 return; |
| 149 } | 145 } |
| 150 | 146 |
| 151 flow_ = CreateFlow(CreateRequestUrl(request_path_)); | 147 api_call_flow_->Start( |
| 152 flow_->Start(url_request_context_.get(), access_token, serialized_request, | 148 CreateRequestUrl(request_path_), url_request_context_.get(), access_token, |
| 153 base::Bind(&CryptAuthClient::OnFlowSuccess<ResponseProto>, | 149 serialized_request, |
| 154 weak_ptr_factory_.GetWeakPtr(), response_callback), | 150 base::Bind(&CryptAuthClientImpl::OnFlowSuccess<ResponseProto>, |
| 155 base::Bind(&CryptAuthClient::OnApiCallFailed, | 151 weak_ptr_factory_.GetWeakPtr(), response_callback), |
| 156 weak_ptr_factory_.GetWeakPtr())); | 152 base::Bind(&CryptAuthClientImpl::OnApiCallFailed, |
| 153 weak_ptr_factory_.GetWeakPtr())); |
| 157 } | 154 } |
| 158 | 155 |
| 159 template <class ResponseProto> | 156 template <class ResponseProto> |
| 160 void CryptAuthClient::OnFlowSuccess( | 157 void CryptAuthClientImpl::OnFlowSuccess( |
| 161 const base::Callback<void(const ResponseProto&)>& result_callback, | 158 const base::Callback<void(const ResponseProto&)>& result_callback, |
| 162 const std::string& serialized_response) { | 159 const std::string& serialized_response) { |
| 163 ResponseProto response; | 160 ResponseProto response; |
| 164 if (!response.ParseFromString(serialized_response)) { | 161 if (!response.ParseFromString(serialized_response)) { |
| 165 OnApiCallFailed("Failed to parse response proto."); | 162 OnApiCallFailed("Failed to parse response proto."); |
| 166 return; | 163 return; |
| 167 } | 164 } |
| 168 result_callback.Run(response); | 165 result_callback.Run(response); |
| 169 }; | 166 }; |
| 170 | 167 |
| 171 void CryptAuthClient::OnApiCallFailed(const std::string& error_message) { | 168 void CryptAuthClientImpl::OnApiCallFailed(const std::string& error_message) { |
| 172 error_callback_.Run(error_message); | 169 error_callback_.Run(error_message); |
| 173 } | 170 } |
| 174 | 171 |
| 175 } // proximity_auth | 172 } // proximity_auth |
| OLD | NEW |