Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_client_impl.cc
blob73ab9e524d39f907a44c3d69adab896e9a0a7ee3
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
3 // found in the LICENSE file.
5 #include "components/proximity_auth/cryptauth/cryptauth_client_impl.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher_impl.h"
10 #include "components/proximity_auth/switches.h"
12 namespace proximity_auth {
14 namespace {
16 // Default URL of Google APIs endpoint hosting CryptAuth.
17 const char kDefaultCryptAuthHTTPHost[] = "https://www.googleapis.com";
19 // URL subpath hosting the CryptAuth service.
20 const char kCryptAuthPath[] = "cryptauth/v1/";
22 // URL subpaths for each CryptAuth API.
23 const char kGetMyDevicesPath[] = "deviceSync/getmydevices";
24 const char kFindEligibleUnlockDevicesPath[] =
25 "deviceSync/findeligibleunlockdevices";
26 const char kSendDeviceSyncTicklePath[] = "deviceSync/senddevicesynctickle";
27 const char kToggleEasyUnlockPath[] = "deviceSync/toggleeasyunlock";
28 const char kSetupEnrollmentPath[] = "enrollment/setup";
29 const char kFinishEnrollmentPath[] = "enrollment/finish";
31 // Query string of the API URL indicating that the response should be in a
32 // serialized protobuf format.
33 const char kQueryProtobuf[] = "?alt=proto";
35 // Creates the full CryptAuth URL for endpoint to the API with |request_path|.
36 GURL CreateRequestUrl(const std::string& request_path) {
37 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
38 GURL google_apis_url =
39 GURL(command_line->HasSwitch(switches::kCryptAuthHTTPHost)
40 ? command_line->GetSwitchValueASCII(switches::kCryptAuthHTTPHost)
41 : kDefaultCryptAuthHTTPHost);
42 return google_apis_url.Resolve(kCryptAuthPath + request_path +
43 kQueryProtobuf);
46 } // namespace
48 CryptAuthClientImpl::CryptAuthClientImpl(
49 scoped_ptr<CryptAuthApiCallFlow> api_call_flow,
50 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher,
51 scoped_refptr<net::URLRequestContextGetter> url_request_context,
52 const cryptauth::DeviceClassifier& device_classifier)
53 : api_call_flow_(api_call_flow.Pass()),
54 access_token_fetcher_(access_token_fetcher.Pass()),
55 url_request_context_(url_request_context),
56 device_classifier_(device_classifier),
57 has_call_started_(false),
58 weak_ptr_factory_(this) {
61 CryptAuthClientImpl::~CryptAuthClientImpl() {
64 void CryptAuthClientImpl::GetMyDevices(
65 const cryptauth::GetMyDevicesRequest& request,
66 const GetMyDevicesCallback& callback,
67 const ErrorCallback& error_callback) {
68 MakeApiCall(kGetMyDevicesPath, request, callback, error_callback);
71 void CryptAuthClientImpl::FindEligibleUnlockDevices(
72 const cryptauth::FindEligibleUnlockDevicesRequest& request,
73 const FindEligibleUnlockDevicesCallback& callback,
74 const ErrorCallback& error_callback) {
75 MakeApiCall(kFindEligibleUnlockDevicesPath, request, callback,
76 error_callback);
79 void CryptAuthClientImpl::SendDeviceSyncTickle(
80 const cryptauth::SendDeviceSyncTickleRequest& request,
81 const SendDeviceSyncTickleCallback& callback,
82 const ErrorCallback& error_callback) {
83 MakeApiCall(kSendDeviceSyncTicklePath, request, callback, error_callback);
86 void CryptAuthClientImpl::ToggleEasyUnlock(
87 const cryptauth::ToggleEasyUnlockRequest& request,
88 const ToggleEasyUnlockCallback& callback,
89 const ErrorCallback& error_callback) {
90 MakeApiCall(kToggleEasyUnlockPath, request, callback, error_callback);
93 void CryptAuthClientImpl::SetupEnrollment(
94 const cryptauth::SetupEnrollmentRequest& request,
95 const SetupEnrollmentCallback& callback,
96 const ErrorCallback& error_callback) {
97 MakeApiCall(kSetupEnrollmentPath, request, callback, error_callback);
100 void CryptAuthClientImpl::FinishEnrollment(
101 const cryptauth::FinishEnrollmentRequest& request,
102 const FinishEnrollmentCallback& callback,
103 const ErrorCallback& error_callback) {
104 MakeApiCall(kFinishEnrollmentPath, request, callback, error_callback);
107 std::string CryptAuthClientImpl::GetAccessTokenUsed() {
108 return access_token_used_;
111 template <class RequestProto, class ResponseProto>
112 void CryptAuthClientImpl::MakeApiCall(
113 const std::string& request_path,
114 const RequestProto& request_proto,
115 const base::Callback<void(const ResponseProto&)>& response_callback,
116 const ErrorCallback& error_callback) {
117 if (has_call_started_) {
118 error_callback.Run(
119 "Client has been used for another request. Do not reuse.");
120 return;
122 has_call_started_ = true;
124 // The |device_classifier| field must be present for all CryptAuth requests.
125 RequestProto request_copy(request_proto);
126 request_copy.mutable_device_classifier()->CopyFrom(device_classifier_);
128 std::string serialized_request;
129 if (!request_copy.SerializeToString(&serialized_request)) {
130 error_callback.Run(std::string("Failed to serialize ") +
131 request_proto.GetTypeName() + " proto.");
132 return;
135 request_path_ = request_path;
136 error_callback_ = error_callback;
137 access_token_fetcher_->FetchAccessToken(base::Bind(
138 &CryptAuthClientImpl::OnAccessTokenFetched<ResponseProto>,
139 weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback));
142 template <class ResponseProto>
143 void CryptAuthClientImpl::OnAccessTokenFetched(
144 const std::string& serialized_request,
145 const base::Callback<void(const ResponseProto&)>& response_callback,
146 const std::string& access_token) {
147 if (access_token.empty()) {
148 OnApiCallFailed("Failed to get a valid access token.");
149 return;
151 access_token_used_ = access_token;
153 api_call_flow_->Start(
154 CreateRequestUrl(request_path_), url_request_context_.get(), access_token,
155 serialized_request,
156 base::Bind(&CryptAuthClientImpl::OnFlowSuccess<ResponseProto>,
157 weak_ptr_factory_.GetWeakPtr(), response_callback),
158 base::Bind(&CryptAuthClientImpl::OnApiCallFailed,
159 weak_ptr_factory_.GetWeakPtr()));
162 template <class ResponseProto>
163 void CryptAuthClientImpl::OnFlowSuccess(
164 const base::Callback<void(const ResponseProto&)>& result_callback,
165 const std::string& serialized_response) {
166 ResponseProto response;
167 if (!response.ParseFromString(serialized_response)) {
168 OnApiCallFailed("Failed to parse response proto.");
169 return;
171 result_callback.Run(response);
174 void CryptAuthClientImpl::OnApiCallFailed(const std::string& error_message) {
175 error_callback_.Run(error_message);
178 // CryptAuthClientFactoryImpl
179 CryptAuthClientFactoryImpl::CryptAuthClientFactoryImpl(
180 OAuth2TokenService* token_service,
181 const std::string& account_id,
182 scoped_refptr<net::URLRequestContextGetter> url_request_context,
183 const cryptauth::DeviceClassifier& device_classifier)
184 : token_service_(token_service),
185 account_id_(account_id),
186 url_request_context_(url_request_context),
187 device_classifier_(device_classifier) {
190 CryptAuthClientFactoryImpl::~CryptAuthClientFactoryImpl() {
193 scoped_ptr<CryptAuthClient> CryptAuthClientFactoryImpl::CreateInstance() {
194 return make_scoped_ptr(new CryptAuthClientImpl(
195 make_scoped_ptr(new CryptAuthApiCallFlow()),
196 make_scoped_ptr(
197 new CryptAuthAccessTokenFetcherImpl(token_service_, account_id_)),
198 url_request_context_, device_classifier_));
201 } // proximity_auth