Don't preload rarely seen large images
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_api_call_flow.cc
blobc72e86744ba8ebd17406bf6565dec4255103c8b7
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 "components/proximity_auth/logging/logging.h"
9 #include "net/url_request/url_fetcher.h"
11 namespace proximity_auth {
13 namespace {
15 const char kResponseBodyError[] = "Failed to get response body";
16 const char kRequestFailedError[] = "Request failed";
17 const char kHttpStatusErrorPrefix[] = "HTTP status: ";
19 } // namespace
21 CryptAuthApiCallFlow::CryptAuthApiCallFlow() {
24 CryptAuthApiCallFlow::~CryptAuthApiCallFlow() {
27 void CryptAuthApiCallFlow::Start(const GURL& request_url,
28 net::URLRequestContextGetter* context,
29 const std::string& access_token,
30 const std::string& serialized_request,
31 const ResultCallback& result_callback,
32 const ErrorCallback& error_callback) {
33 request_url_ = request_url;
34 serialized_request_ = serialized_request;
35 result_callback_ = result_callback;
36 error_callback_ = error_callback;
37 OAuth2ApiCallFlow::Start(context, access_token);
40 GURL CryptAuthApiCallFlow::CreateApiCallUrl() {
41 return request_url_;
44 std::string CryptAuthApiCallFlow::CreateApiCallBody() {
45 return serialized_request_;
48 std::string CryptAuthApiCallFlow::CreateApiCallBodyContentType() {
49 return "application/x-protobuf";
52 void CryptAuthApiCallFlow::ProcessApiCallSuccess(
53 const net::URLFetcher* source) {
54 std::string serialized_response;
55 if (!source->GetResponseAsString(&serialized_response)) {
56 error_callback_.Run(kResponseBodyError);
57 return;
59 result_callback_.Run(serialized_response);
62 void CryptAuthApiCallFlow::ProcessApiCallFailure(
63 const net::URLFetcher* source) {
64 std::string error_message;
65 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS) {
66 error_message =
67 kHttpStatusErrorPrefix + base::IntToString(source->GetResponseCode());
68 } else {
69 error_message = kRequestFailedError;
72 std::string response;
73 source->GetResponseAsString(&response);
74 PA_LOG(INFO) << "API call failed:\n" << response;
75 error_callback_.Run(error_message);
78 } // proximity_auth