Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: components/proximity_auth/cryptauth/cryptauth_client_impl.cc

Issue 1066453002: Refactor CryptAuth component to be more testable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cryptauth_securemessage
Patch Set: rename tests Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 9 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher_imp l.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" 10 #include "components/proximity_auth/switches.h"
12 #include "net/url_request/url_request_context_getter.h"
13 11
14 namespace proximity_auth { 12 namespace proximity_auth {
15 13
16 namespace { 14 namespace {
17 15
18 // Default URL of Google APIs endpoint hosting CryptAuth. 16 // Default URL of Google APIs endpoint hosting CryptAuth.
19 const char kDefaultCryptAuthHTTPHost[] = "https://www.googleapis.com"; 17 const char kDefaultCryptAuthHTTPHost[] = "https://www.googleapis.com";
20 18
21 // URL subpath hosting the CryptAuth service. 19 // URL subpath hosting the CryptAuth service.
22 const char kCryptAuthPath[] = "cryptauth/v1/"; 20 const char kCryptAuthPath[] = "cryptauth/v1/";
(...skipping 17 matching lines...) Expand all
40 GURL google_apis_url = 38 GURL google_apis_url =
41 GURL(command_line->HasSwitch(switches::kCryptAuthHTTPHost) 39 GURL(command_line->HasSwitch(switches::kCryptAuthHTTPHost)
42 ? command_line->GetSwitchValueASCII(switches::kCryptAuthHTTPHost) 40 ? command_line->GetSwitchValueASCII(switches::kCryptAuthHTTPHost)
43 : kDefaultCryptAuthHTTPHost); 41 : kDefaultCryptAuthHTTPHost);
44 return google_apis_url.Resolve(kCryptAuthPath + request_path + 42 return google_apis_url.Resolve(kCryptAuthPath + request_path +
45 kQueryProtobuf); 43 kQueryProtobuf);
46 } 44 }
47 45
48 } // namespace 46 } // namespace
49 47
50 CryptAuthClient::CryptAuthClient( 48 CryptAuthClientImpl::CryptAuthClientImpl(
49 scoped_ptr<CryptAuthApiCallFlow> api_call_flow,
51 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, 50 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher,
52 scoped_refptr<net::URLRequestContextGetter> url_request_context, 51 scoped_refptr<net::URLRequestContextGetter> url_request_context,
53 const cryptauth::DeviceClassifier& device_classifier) 52 const cryptauth::DeviceClassifier& device_classifier)
54 : url_request_context_(url_request_context), 53 : api_call_flow_(api_call_flow.Pass()),
55 access_token_fetcher_(access_token_fetcher.Pass()), 54 access_token_fetcher_(access_token_fetcher.Pass()),
55 url_request_context_(url_request_context),
56 device_classifier_(device_classifier), 56 device_classifier_(device_classifier),
57 has_call_started_(false),
57 weak_ptr_factory_(this) { 58 weak_ptr_factory_(this) {
58 } 59 }
59 60
60 CryptAuthClient::~CryptAuthClient() { 61 CryptAuthClientImpl::~CryptAuthClientImpl() {
61 } 62 }
62 63
63 void CryptAuthClient::GetMyDevices( 64 void CryptAuthClientImpl::GetMyDevices(
64 const cryptauth::GetMyDevicesRequest& request, 65 const cryptauth::GetMyDevicesRequest& request,
65 const GetMyDevicesCallback& callback, 66 const GetMyDevicesCallback& callback,
66 const ErrorCallback& error_callback) { 67 const ErrorCallback& error_callback) {
67 MakeApiCall(kGetMyDevicesPath, request, callback, error_callback); 68 MakeApiCall(kGetMyDevicesPath, request, callback, error_callback);
68 } 69 }
69 70
70 void CryptAuthClient::FindEligibleUnlockDevices( 71 void CryptAuthClientImpl::FindEligibleUnlockDevices(
71 const cryptauth::FindEligibleUnlockDevicesRequest& request, 72 const cryptauth::FindEligibleUnlockDevicesRequest& request,
72 const FindEligibleUnlockDevicesCallback& callback, 73 const FindEligibleUnlockDevicesCallback& callback,
73 const ErrorCallback& error_callback) { 74 const ErrorCallback& error_callback) {
74 MakeApiCall(kFindEligibleUnlockDevicesPath, request, callback, 75 MakeApiCall(kFindEligibleUnlockDevicesPath, request, callback,
75 error_callback); 76 error_callback);
76 } 77 }
77 78
78 void CryptAuthClient::SendDeviceSyncTickle( 79 void CryptAuthClientImpl::SendDeviceSyncTickle(
79 const cryptauth::SendDeviceSyncTickleRequest& request, 80 const cryptauth::SendDeviceSyncTickleRequest& request,
80 const SendDeviceSyncTickleCallback& callback, 81 const SendDeviceSyncTickleCallback& callback,
81 const ErrorCallback& error_callback) { 82 const ErrorCallback& error_callback) {
82 MakeApiCall(kSendDeviceSyncTicklePath, request, callback, error_callback); 83 MakeApiCall(kSendDeviceSyncTicklePath, request, callback, error_callback);
83 } 84 }
84 85
85 void CryptAuthClient::ToggleEasyUnlock( 86 void CryptAuthClientImpl::ToggleEasyUnlock(
86 const cryptauth::ToggleEasyUnlockRequest& request, 87 const cryptauth::ToggleEasyUnlockRequest& request,
87 const ToggleEasyUnlockCallback& callback, 88 const ToggleEasyUnlockCallback& callback,
88 const ErrorCallback& error_callback) { 89 const ErrorCallback& error_callback) {
89 MakeApiCall(kToggleEasyUnlockPath, request, callback, error_callback); 90 MakeApiCall(kToggleEasyUnlockPath, request, callback, error_callback);
90 } 91 }
91 92
92 void CryptAuthClient::SetupEnrollment( 93 void CryptAuthClientImpl::SetupEnrollment(
93 const cryptauth::SetupEnrollmentRequest& request, 94 const cryptauth::SetupEnrollmentRequest& request,
94 const SetupEnrollmentCallback& callback, 95 const SetupEnrollmentCallback& callback,
95 const ErrorCallback& error_callback) { 96 const ErrorCallback& error_callback) {
96 MakeApiCall(kSetupEnrollmentPath, request, callback, error_callback); 97 MakeApiCall(kSetupEnrollmentPath, request, callback, error_callback);
97 } 98 }
98 99
99 void CryptAuthClient::FinishEnrollment( 100 void CryptAuthClientImpl::FinishEnrollment(
100 const cryptauth::FinishEnrollmentRequest& request, 101 const cryptauth::FinishEnrollmentRequest& request,
101 const FinishEnrollmentCallback& callback, 102 const FinishEnrollmentCallback& callback,
102 const ErrorCallback& error_callback) { 103 const ErrorCallback& error_callback) {
103 MakeApiCall(kFinishEnrollmentPath, request, callback, error_callback); 104 MakeApiCall(kFinishEnrollmentPath, request, callback, error_callback);
104 } 105 }
105 106
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> 107 template <class RequestProto, class ResponseProto>
112 void CryptAuthClient::MakeApiCall( 108 void CryptAuthClientImpl::MakeApiCall(
113 const std::string& request_path, 109 const std::string& request_path,
114 const RequestProto& request_proto, 110 const RequestProto& request_proto,
115 const base::Callback<void(const ResponseProto&)>& response_callback, 111 const base::Callback<void(const ResponseProto&)>& response_callback,
116 const ErrorCallback& error_callback) { 112 const ErrorCallback& error_callback) {
117 if (flow_) { 113 if (has_call_started_) {
118 error_callback.Run( 114 error_callback.Run(
119 "Client has been used for another request. Do not reuse."); 115 "Client has been used for another request. Do not reuse.");
120 return; 116 return;
121 } 117 }
118 has_call_started_ = true;
122 119
123 // The |device_classifier| field must be present for all CryptAuth requests. 120 // The |device_classifier| field must be present for all CryptAuth requests.
124 RequestProto request_copy(request_proto); 121 RequestProto request_copy(request_proto);
125 request_copy.mutable_device_classifier()->CopyFrom(device_classifier_); 122 request_copy.mutable_device_classifier()->CopyFrom(device_classifier_);
126 123
127 std::string serialized_request; 124 std::string serialized_request;
128 if (!request_copy.SerializeToString(&serialized_request)) { 125 if (!request_copy.SerializeToString(&serialized_request)) {
129 error_callback.Run(std::string("Failed to serialize ") + 126 error_callback.Run(std::string("Failed to serialize ") +
130 request_proto.GetTypeName() + " proto."); 127 request_proto.GetTypeName() + " proto.");
131 return; 128 return;
132 } 129 }
133 130
134 request_path_ = request_path; 131 request_path_ = request_path;
135 error_callback_ = error_callback; 132 error_callback_ = error_callback;
136 access_token_fetcher_->FetchAccessToken(base::Bind( 133 access_token_fetcher_->FetchAccessToken(base::Bind(
137 &CryptAuthClient::OnAccessTokenFetched<ResponseProto>, 134 &CryptAuthClientImpl::OnAccessTokenFetched<ResponseProto>,
138 weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback)); 135 weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback));
139 } 136 }
140 137
141 template <class ResponseProto> 138 template <class ResponseProto>
142 void CryptAuthClient::OnAccessTokenFetched( 139 void CryptAuthClientImpl::OnAccessTokenFetched(
143 const std::string& serialized_request, 140 const std::string& serialized_request,
144 const base::Callback<void(const ResponseProto&)>& response_callback, 141 const base::Callback<void(const ResponseProto&)>& response_callback,
145 const std::string& access_token) { 142 const std::string& access_token) {
146 if (access_token.empty()) { 143 if (access_token.empty()) {
147 OnApiCallFailed("Failed to get a valid access token."); 144 OnApiCallFailed("Failed to get a valid access token.");
148 return; 145 return;
149 } 146 }
150 147
151 flow_ = CreateFlow(CreateRequestUrl(request_path_)); 148 api_call_flow_->Start(
152 flow_->Start(url_request_context_.get(), access_token, serialized_request, 149 CreateRequestUrl(request_path_), url_request_context_.get(), access_token,
153 base::Bind(&CryptAuthClient::OnFlowSuccess<ResponseProto>, 150 serialized_request,
154 weak_ptr_factory_.GetWeakPtr(), response_callback), 151 base::Bind(&CryptAuthClientImpl::OnFlowSuccess<ResponseProto>,
155 base::Bind(&CryptAuthClient::OnApiCallFailed, 152 weak_ptr_factory_.GetWeakPtr(), response_callback),
156 weak_ptr_factory_.GetWeakPtr())); 153 base::Bind(&CryptAuthClientImpl::OnApiCallFailed,
154 weak_ptr_factory_.GetWeakPtr()));
157 } 155 }
158 156
159 template <class ResponseProto> 157 template <class ResponseProto>
160 void CryptAuthClient::OnFlowSuccess( 158 void CryptAuthClientImpl::OnFlowSuccess(
161 const base::Callback<void(const ResponseProto&)>& result_callback, 159 const base::Callback<void(const ResponseProto&)>& result_callback,
162 const std::string& serialized_response) { 160 const std::string& serialized_response) {
163 ResponseProto response; 161 ResponseProto response;
164 if (!response.ParseFromString(serialized_response)) { 162 if (!response.ParseFromString(serialized_response)) {
165 OnApiCallFailed("Failed to parse response proto."); 163 OnApiCallFailed("Failed to parse response proto.");
166 return; 164 return;
167 } 165 }
168 result_callback.Run(response); 166 result_callback.Run(response);
169 }; 167 };
170 168
171 void CryptAuthClient::OnApiCallFailed(const std::string& error_message) { 169 void CryptAuthClientImpl::OnApiCallFailed(const std::string& error_message) {
172 error_callback_.Run(error_message); 170 error_callback_.Run(error_message);
173 } 171 }
174 172
173 // CryptAuthClientFactoryImpl
174 CryptAuthClientFactoryImpl::CryptAuthClientFactoryImpl(
175 OAuth2TokenService* token_service,
176 const std::string& account_id,
177 scoped_refptr<net::URLRequestContextGetter> url_request_context,
178 const cryptauth::DeviceClassifier& device_classifier)
179 : token_service_(token_service),
180 account_id_(account_id),
181 url_request_context_(url_request_context),
182 device_classifier_(device_classifier) {
183 }
184
185 CryptAuthClientFactoryImpl::~CryptAuthClientFactoryImpl() {
186 }
187
188 scoped_ptr<CryptAuthClient> CryptAuthClientFactoryImpl::CreateInstance() {
189 return make_scoped_ptr(new CryptAuthClientImpl(
190 make_scoped_ptr(new CryptAuthApiCallFlow()),
191 make_scoped_ptr(
192 new CryptAuthAccessTokenFetcherImpl(token_service_, account_id_)),
193 url_request_context_, device_classifier_));
194 }
195
175 } // proximity_auth 196 } // proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698