Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_api_call_flow.cc
blob570075656674d1294236b7c71387fcf3fc90a950
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_api_call_flow.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "net/url_request/url_fetcher.h"
10 namespace proximity_auth {
12 namespace {
14 const char kResponseBodyError[] = "Failed to get response body";
15 const char kRequestFailedError[] = "Request failed";
16 const char kHttpStatusErrorPrefix[] = "HTTP status: ";
18 } // namespace
20 CryptAuthApiCallFlow::CryptAuthApiCallFlow(const GURL& request_url)
21 : request_url_(request_url) {
24 CryptAuthApiCallFlow::~CryptAuthApiCallFlow() {
27 void CryptAuthApiCallFlow::Start(net::URLRequestContextGetter* context,
28 const std::string& access_token,
29 const std::string& serialized_request,
30 const ResultCallback& result_callback,
31 const ErrorCallback& error_callback) {
32 serialized_request_ = serialized_request;
33 result_callback_ = result_callback;
34 error_callback_ = error_callback;
35 OAuth2ApiCallFlow::Start(context, access_token);
38 GURL CryptAuthApiCallFlow::CreateApiCallUrl() {
39 return request_url_;
42 std::string CryptAuthApiCallFlow::CreateApiCallBody() {
43 return serialized_request_;
46 std::string CryptAuthApiCallFlow::CreateApiCallBodyContentType() {
47 return "application/x-protobuf";
50 void CryptAuthApiCallFlow::ProcessApiCallSuccess(
51 const net::URLFetcher* source) {
52 std::string serialized_response;
53 if (!source->GetResponseAsString(&serialized_response)) {
54 error_callback_.Run(kResponseBodyError);
55 return;
57 result_callback_.Run(serialized_response);
60 void CryptAuthApiCallFlow::ProcessApiCallFailure(
61 const net::URLFetcher* source) {
62 std::string error_message;
63 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS) {
64 error_message =
65 kHttpStatusErrorPrefix + base::IntToString(source->GetResponseCode());
66 } else {
67 error_message = kRequestFailedError;
69 error_callback_.Run(error_message);
72 } // proximity_auth