Commit 9aa878c3 authored by Josh Nohle's avatar Josh Nohle Committed by Commit Bot

[Nearby] Move HTTP error/result enums to common directory

The HTTP result enum is not needed by the HTTP client, but the HTTP
result enum requires knowledge of the HTTP error enum. So, it is
necessary to move the HTTP error enum out of the "client" directory,
and house all HTTP enums in the "common" directory.

Change-Id: I5458d892f85b4045d2407954d16ef66f447139e8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2333871
Commit-Queue: Josh Nohle <nohle@chromium.org>
Auto-Submit: Josh Nohle <nohle@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#795242}
parent 6246c58f
......@@ -12,14 +12,13 @@ source_set("client") {
"nearby_share_client_impl.h",
"nearby_share_http_notifier.cc",
"nearby_share_http_notifier.h",
"nearby_share_request_error.cc",
"nearby_share_request_error.h",
"nearby_share_switches.cc",
"nearby_share_switches.h",
]
deps = [
"//base",
"//chrome/browser/nearby_sharing/common",
"//chrome/browser/nearby_sharing/logging",
"//chrome/browser/nearby_sharing/proto",
"//components/signin/public/identity_manager",
......@@ -42,6 +41,7 @@ source_set("test_support") {
deps = [
":client",
"//base",
"//chrome/browser/nearby_sharing/common",
"//chrome/browser/nearby_sharing/proto",
]
}
......@@ -58,6 +58,7 @@ source_set("unit_tests") {
":client",
"//base",
"//base/test:test_support",
"//chrome/browser/nearby_sharing/common",
"//chrome/browser/nearby_sharing/proto",
"//components/signin/public/identity_manager:test_support",
"//net",
......
......@@ -11,7 +11,7 @@
#include "base/callback.h"
#include "base/memory/scoped_refptr.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_request_error.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_http_result.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "url/gurl.h"
......@@ -20,7 +20,7 @@ class NearbyShareApiCallFlow {
public:
using ResultCallback =
base::OnceCallback<void(const std::string& serialized_response)>;
using ErrorCallback = base::OnceCallback<void(NearbyShareRequestError error)>;
using ErrorCallback = base::OnceCallback<void(NearbyShareHttpError error)>;
using QueryParameters = std::vector<std::pair<std::string, std::string>>;
NearbyShareApiCallFlow() = default;
......
......@@ -5,6 +5,7 @@
#include "chrome/browser/nearby_sharing/client/nearby_share_api_call_flow_impl.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_http_result.h"
#include "chrome/browser/nearby_sharing/logging/logging.h"
#include "net/base/net_errors.h"
#include "net/base/url_util.h"
......@@ -21,22 +22,6 @@ const char kProtobufContentType[] = "application/x-protobuf";
const char kQueryParameterAlternateOutputKey[] = "alt";
const char kQueryParameterAlternateOutputProto[] = "proto";
NearbyShareRequestError GetErrorForHttpResponseCode(int response_code) {
if (response_code == 400)
return NearbyShareRequestError::kBadRequest;
if (response_code == 403)
return NearbyShareRequestError::kAuthenticationError;
if (response_code == 404)
return NearbyShareRequestError::kEndpointNotFound;
if (response_code >= 500 && response_code < 600)
return NearbyShareRequestError::kInternalServerError;
return NearbyShareRequestError::kUnknown;
}
} // namespace
NearbyShareApiCallFlowImpl::NearbyShareApiCallFlowImpl() = default;
......@@ -132,7 +117,7 @@ void NearbyShareApiCallFlowImpl::ProcessApiCallSuccess(
const network::mojom::URLResponseHead* head,
std::unique_ptr<std::string> body) {
if (!body) {
std::move(error_callback_).Run(NearbyShareRequestError::kResponseMalformed);
std::move(error_callback_).Run(NearbyShareHttpError::kResponseMalformed);
return;
}
std::move(result_callback_).Run(std::move(*body));
......@@ -142,15 +127,15 @@ void NearbyShareApiCallFlowImpl::ProcessApiCallFailure(
int net_error,
const network::mojom::URLResponseHead* head,
std::unique_ptr<std::string> body) {
base::Optional<NearbyShareRequestError> error;
base::Optional<NearbyShareHttpError> error;
std::string error_message;
if (net_error == net::OK) {
int response_code = -1;
if (head && head->headers)
response_code = head->headers->response_code();
error = GetErrorForHttpResponseCode(response_code);
error = NearbyShareHttpErrorForHttpResponseCode(response_code);
} else {
error = NearbyShareRequestError::kOffline;
error = NearbyShareHttpError::kOffline;
}
NS_LOG(ERROR) << "API call failed, error code: "
......
......@@ -13,7 +13,6 @@
#include "base/callback.h"
#include "base/optional.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_api_call_flow.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_request_error.h"
#include "google_apis/gaia/oauth2_api_call_flow.h"
// NearbyShareApiCallFlowImpl is a wrapper around OAuth2ApiCallFlow
......
......@@ -131,9 +131,9 @@ class NearbyShareApiCallFlowImplTest : public testing::Test {
result_ = std::make_unique<std::string>(result);
}
void OnError(NearbyShareRequestError network_error) {
void OnError(NearbyShareHttpError network_error) {
EXPECT_FALSE(result_ || network_error_);
network_error_ = std::make_unique<NearbyShareRequestError>(network_error);
network_error_ = std::make_unique<NearbyShareHttpError>(network_error);
}
void CheckNearbySharingClientHttpPostRequest(
......@@ -270,7 +270,7 @@ class NearbyShareApiCallFlowImplTest : public testing::Test {
}
std::unique_ptr<std::string> result_;
std::unique_ptr<NearbyShareRequestError> network_error_;
std::unique_ptr<NearbyShareHttpError> network_error_;
private:
base::test::TaskEnvironment task_environment_;
......@@ -305,21 +305,21 @@ TEST_F(NearbyShareApiCallFlowImplTest, PostRequestFailure) {
StartPostRequestApiCallFlow();
CompleteCurrentPostRequest(net::ERR_FAILED);
EXPECT_FALSE(result_);
EXPECT_EQ(NearbyShareRequestError::kOffline, *network_error_);
EXPECT_EQ(NearbyShareHttpError::kOffline, *network_error_);
}
TEST_F(NearbyShareApiCallFlowImplTest, PatchRequestFailure) {
StartPatchRequestApiCallFlow();
CompleteCurrentPatchRequest(net::ERR_FAILED);
EXPECT_FALSE(result_);
EXPECT_EQ(NearbyShareRequestError::kOffline, *network_error_);
EXPECT_EQ(NearbyShareHttpError::kOffline, *network_error_);
}
TEST_F(NearbyShareApiCallFlowImplTest, GetRequestFailure) {
StartGetRequestApiCallFlow();
CompleteCurrentPostRequest(net::ERR_FAILED);
EXPECT_FALSE(result_);
EXPECT_EQ(NearbyShareRequestError::kOffline, *network_error_);
EXPECT_EQ(NearbyShareHttpError::kOffline, *network_error_);
}
TEST_F(NearbyShareApiCallFlowImplTest, RequestStatus500) {
......@@ -327,7 +327,7 @@ TEST_F(NearbyShareApiCallFlowImplTest, RequestStatus500) {
CompleteCurrentPostRequest(net::OK, net::HTTP_INTERNAL_SERVER_ERROR,
"Nearby Sharing Meltdown.");
EXPECT_FALSE(result_);
EXPECT_EQ(NearbyShareRequestError::kInternalServerError, *network_error_);
EXPECT_EQ(NearbyShareHttpError::kInternalServerError, *network_error_);
}
TEST_F(NearbyShareApiCallFlowImplTest, PatchRequestStatus500) {
......@@ -335,7 +335,7 @@ TEST_F(NearbyShareApiCallFlowImplTest, PatchRequestStatus500) {
CompleteCurrentPatchRequest(net::OK, net::HTTP_INTERNAL_SERVER_ERROR,
"Nearby Sharing Meltdown.");
EXPECT_FALSE(result_);
EXPECT_EQ(NearbyShareRequestError::kInternalServerError, *network_error_);
EXPECT_EQ(NearbyShareHttpError::kInternalServerError, *network_error_);
}
TEST_F(NearbyShareApiCallFlowImplTest, GetRequestStatus500) {
......@@ -343,7 +343,7 @@ TEST_F(NearbyShareApiCallFlowImplTest, GetRequestStatus500) {
CompleteCurrentPostRequest(net::OK, net::HTTP_INTERNAL_SERVER_ERROR,
"Nearby Sharing Meltdown.");
EXPECT_FALSE(result_);
EXPECT_EQ(NearbyShareRequestError::kInternalServerError, *network_error_);
EXPECT_EQ(NearbyShareHttpError::kInternalServerError, *network_error_);
}
// The empty string is a valid protocol buffer message serialization.
......
......@@ -9,7 +9,7 @@
#include <string>
#include "base/callback_forward.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_request_error.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_http_result.h"
namespace nearbyshare {
namespace proto {
......@@ -32,7 +32,7 @@ class NearbyShareClient {
public:
using CheckContactsReachabilityCallback = base::OnceCallback<void(
const nearbyshare::proto::CheckContactsReachabilityResponse&)>;
using ErrorCallback = base::OnceCallback<void(NearbyShareRequestError)>;
using ErrorCallback = base::OnceCallback<void(NearbyShareHttpError)>;
using ListContactPeopleCallback = base::OnceCallback<void(
const nearbyshare::proto::ListContactPeopleResponse&)>;
using ListPublicCertificatesCallback = base::OnceCallback<void(
......
......@@ -14,6 +14,7 @@
#include "chrome/browser/nearby_sharing/client/nearby_share_api_call_flow_impl.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_http_notifier.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_switches.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_http_result.h"
#include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/browser/nearby_sharing/proto/certificate_rpc.pb.h"
#include "chrome/browser/nearby_sharing/proto/contact_rpc.pb.h"
......@@ -335,7 +336,7 @@ void NearbyShareClientImpl::OnAccessTokenFetched(
access_token_fetcher_.reset();
if (error.state() != GoogleServiceAuthError::NONE) {
OnApiCallFailed(NearbyShareRequestError::kAuthenticationError);
OnApiCallFailed(NearbyShareHttpError::kAuthenticationError);
return;
}
access_token_used_ = access_token_info.token;
......@@ -383,14 +384,14 @@ void NearbyShareClientImpl::OnFlowSuccess(
const std::string& serialized_response) {
ResponseProto response;
if (!response.ParseFromString(serialized_response)) {
OnApiCallFailed(NearbyShareRequestError::kResponseMalformed);
OnApiCallFailed(NearbyShareHttpError::kResponseMalformed);
return;
}
std::move(result_callback).Run(response);
notifier_->NotifyOfResponse(response);
}
void NearbyShareClientImpl::OnApiCallFailed(NearbyShareRequestError error) {
void NearbyShareClientImpl::OnApiCallFailed(NearbyShareHttpError error) {
std::move(error_callback_).Run(error);
NS_LOG(ERROR) << error;
}
......
......@@ -15,7 +15,7 @@
#include "base/optional.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_api_call_flow.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_client.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_request_error.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_http_result.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "url/gurl.h"
......@@ -116,7 +116,7 @@ class NearbyShareClientImpl : public NearbyShareClient {
const std::string& serialized_response);
// Called when the current API call fails at any step.
void OnApiCallFailed(NearbyShareRequestError error);
void OnApiCallFailed(NearbyShareHttpError error);
// Constructs and executes the actual HTTP request.
std::unique_ptr<NearbyShareApiCallFlow> api_call_flow_;
......
......@@ -19,6 +19,7 @@
#include "chrome/browser/nearby_sharing/client/nearby_share_client_impl.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_http_notifier.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_switches.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_http_result.h"
#include "chrome/browser/nearby_sharing/proto/certificate_rpc.pb.h"
#include "chrome/browser/nearby_sharing/proto/contact_rpc.pb.h"
#include "chrome/browser/nearby_sharing/proto/device_rpc.pb.h"
......@@ -268,7 +269,7 @@ class NearbyShareClientImplTest : public testing::Test,
}
// Ends the current API request with |error|.
void FailApiCallFlow(NearbyShareRequestError error) {
void FailApiCallFlow(NearbyShareHttpError error) {
std::move(api_call_flow_->error_callback_).Run(error);
}
......@@ -372,7 +373,7 @@ TEST_F(NearbyShareClientImplTest, UpdateDeviceSuccess) {
base::BindOnce(
&SaveResultConstRef<nearbyshare::proto::UpdateDeviceResponse>,
&result_proto),
base::BindOnce(&NotCalled<NearbyShareRequestError>));
base::BindOnce(&NotCalled<NearbyShareHttpError>));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......@@ -429,12 +430,12 @@ TEST_F(NearbyShareClientImplTest, UpdateDeviceFailure) {
nearbyshare::proto::UpdateDeviceRequest request;
request.mutable_device()->set_name(kDeviceName1);
NearbyShareRequestError error;
NearbyShareHttpError error;
client_->UpdateDevice(
request,
base::BindOnce(
&NotCalledConstRef<nearbyshare::proto::UpdateDeviceResponse>),
base::BindOnce(&SaveResult<NearbyShareRequestError>, &error));
base::BindOnce(&SaveResult<NearbyShareHttpError>, &error));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......@@ -445,8 +446,8 @@ TEST_F(NearbyShareClientImplTest, UpdateDeviceFailure) {
"https://www.nearbysharing-pa.testgoogleapis.com/v1/users/me/devices/" +
std::string(kDeviceName1));
FailApiCallFlow(NearbyShareRequestError::kInternalServerError);
EXPECT_EQ(NearbyShareRequestError::kInternalServerError, error);
FailApiCallFlow(NearbyShareHttpError::kInternalServerError);
EXPECT_EQ(NearbyShareHttpError::kInternalServerError, error);
}
TEST_F(NearbyShareClientImplTest, CheckContactsReachabilitySuccess) {
......@@ -460,7 +461,7 @@ TEST_F(NearbyShareClientImplTest, CheckContactsReachabilitySuccess) {
base::BindOnce(&SaveResultConstRef<
nearbyshare::proto::CheckContactsReachabilityResponse>,
&result_proto),
base::BindOnce(&NotCalled<NearbyShareRequestError>));
base::BindOnce(&NotCalled<NearbyShareHttpError>));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......@@ -501,13 +502,13 @@ TEST_F(NearbyShareClientImplTest, CheckContactsReachabilitySuccess) {
}
TEST_F(NearbyShareClientImplTest, CheckContactsReachabilityFailure) {
NearbyShareRequestError error;
NearbyShareHttpError error;
nearbyshare::proto::CheckContactsReachabilityRequest request_proto;
client_->CheckContactsReachability(
request_proto,
base::BindOnce(&NotCalledConstRef<
nearbyshare::proto::CheckContactsReachabilityResponse>),
base::BindOnce(&SaveResult<NearbyShareRequestError>, &error));
base::BindOnce(&SaveResult<NearbyShareHttpError>, &error));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......@@ -517,8 +518,8 @@ TEST_F(NearbyShareClientImplTest, CheckContactsReachabilityFailure) {
"https://www.nearbysharing-pa.testgoogleapis.com/v1/"
"contactsReachability:check");
FailApiCallFlow(NearbyShareRequestError::kAuthenticationError);
EXPECT_EQ(NearbyShareRequestError::kAuthenticationError, error);
FailApiCallFlow(NearbyShareHttpError::kAuthenticationError);
EXPECT_EQ(NearbyShareHttpError::kAuthenticationError, error);
}
TEST_F(NearbyShareClientImplTest, ListContactPeopleSuccess) {
......@@ -533,7 +534,7 @@ TEST_F(NearbyShareClientImplTest, ListContactPeopleSuccess) {
base::BindOnce(
&SaveResultConstRef<nearbyshare::proto::ListContactPeopleResponse>,
&result_proto),
base::BindOnce(&NotCalled<NearbyShareRequestError>));
base::BindOnce(&NotCalled<NearbyShareHttpError>));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......@@ -594,7 +595,7 @@ TEST_F(NearbyShareClientImplTest, ListPublicCertificatesSuccess) {
base::BindOnce(&SaveResultConstRef<
nearbyshare::proto::ListPublicCertificatesResponse>,
&result_proto),
base::BindOnce(&NotCalled<NearbyShareRequestError>));
base::BindOnce(&NotCalled<NearbyShareHttpError>));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......@@ -665,29 +666,29 @@ TEST_F(NearbyShareClientImplTest, ListPublicCertificatesSuccess) {
}
TEST_F(NearbyShareClientImplTest, FetchAccessTokenFailure) {
NearbyShareRequestError error;
NearbyShareHttpError error;
client_->UpdateDevice(
nearbyshare::proto::UpdateDeviceRequest(),
base::BindOnce(
&NotCalledConstRef<nearbyshare::proto::UpdateDeviceResponse>),
base::BindOnce(&SaveResult<NearbyShareRequestError>, &error));
base::BindOnce(&SaveResult<NearbyShareHttpError>, &error));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
EXPECT_EQ(NearbyShareRequestError::kAuthenticationError, error);
EXPECT_EQ(NearbyShareHttpError::kAuthenticationError, error);
}
TEST_F(NearbyShareClientImplTest, ParseResponseProtoFailure) {
nearbyshare::proto::UpdateDeviceRequest request_proto;
request_proto.mutable_device()->set_name(kDeviceName1);
NearbyShareRequestError error;
NearbyShareHttpError error;
client_->UpdateDevice(
request_proto,
base::BindOnce(
&NotCalledConstRef<nearbyshare::proto::UpdateDeviceResponse>),
base::BindOnce(&SaveResult<NearbyShareRequestError>, &error));
base::BindOnce(&SaveResult<NearbyShareHttpError>, &error));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......@@ -699,7 +700,7 @@ TEST_F(NearbyShareClientImplTest, ParseResponseProtoFailure) {
std::string(kDeviceName1));
FinishApiCallFlowRaw("Not a valid serialized response message.");
EXPECT_EQ(NearbyShareRequestError::kResponseMalformed, error);
EXPECT_EQ(NearbyShareHttpError::kResponseMalformed, error);
}
TEST_F(NearbyShareClientImplTest, MakeSecondRequestBeforeFirstRequestSucceeds) {
......@@ -713,7 +714,7 @@ TEST_F(NearbyShareClientImplTest, MakeSecondRequestBeforeFirstRequestSucceeds) {
base::BindOnce(
&SaveResultConstRef<nearbyshare::proto::UpdateDeviceResponse>,
&result_proto),
base::BindOnce(&NotCalled<NearbyShareRequestError>));
base::BindOnce(&NotCalled<NearbyShareHttpError>));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......@@ -726,12 +727,12 @@ TEST_F(NearbyShareClientImplTest, MakeSecondRequestBeforeFirstRequestSucceeds) {
// With request pending, make second request.
{
NearbyShareRequestError error;
NearbyShareHttpError error;
EXPECT_DCHECK_DEATH(client_->CheckContactsReachability(
nearbyshare::proto::CheckContactsReachabilityRequest(),
base::BindOnce(&NotCalledConstRef<
nearbyshare::proto::CheckContactsReachabilityResponse>),
base::BindOnce(&SaveResult<NearbyShareRequestError>, &error)));
base::BindOnce(&SaveResult<NearbyShareHttpError>, &error)));
}
// Complete first request.
......@@ -756,7 +757,7 @@ TEST_F(NearbyShareClientImplTest, MakeSecondRequestAfterFirstRequestSucceeds) {
base::BindOnce(
&SaveResultConstRef<nearbyshare::proto::UpdateDeviceResponse>,
&result_proto),
base::BindOnce(&NotCalled<NearbyShareRequestError>));
base::BindOnce(&NotCalled<NearbyShareHttpError>));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......@@ -775,12 +776,12 @@ TEST_F(NearbyShareClientImplTest, MakeSecondRequestAfterFirstRequestSucceeds) {
// Second request fails.
{
NearbyShareRequestError error;
NearbyShareHttpError error;
EXPECT_DCHECK_DEATH(client_->CheckContactsReachability(
nearbyshare::proto::CheckContactsReachabilityRequest(),
base::BindOnce(&NotCalledConstRef<
nearbyshare::proto::CheckContactsReachabilityResponse>),
base::BindOnce(&SaveResult<NearbyShareRequestError>, &error)));
base::BindOnce(&SaveResult<NearbyShareHttpError>, &error)));
}
}
......@@ -796,7 +797,7 @@ TEST_F(NearbyShareClientImplTest, GetAccessTokenUsed) {
base::BindOnce(
&SaveResultConstRef<nearbyshare::proto::UpdateDeviceResponse>,
&result_proto),
base::BindOnce(&NotCalled<NearbyShareRequestError>));
base::BindOnce(&NotCalled<NearbyShareHttpError>));
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/client/nearby_share_request_error.h"
std::ostream& operator<<(std::ostream& stream,
const NearbyShareRequestError& error) {
switch (error) {
case NearbyShareRequestError::kOffline:
stream << "[offline]";
break;
case NearbyShareRequestError::kEndpointNotFound:
stream << "[endpoint not found]";
break;
case NearbyShareRequestError::kAuthenticationError:
stream << "[authentication error]";
break;
case NearbyShareRequestError::kBadRequest:
stream << "[bad request]";
break;
case NearbyShareRequestError::kResponseMalformed:
stream << "[response malformed]";
break;
case NearbyShareRequestError::kInternalServerError:
stream << "[internal server error]";
break;
case NearbyShareRequestError::kUnknown:
stream << "[unknown]";
break;
}
return stream;
}
......@@ -5,6 +5,8 @@
source_set("common") {
sources = [
"nearby_share_enums.h",
"nearby_share_http_result.cc",
"nearby_share_http_result.h",
"nearby_share_prefs.cc",
"nearby_share_prefs.h",
]
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/common/nearby_share_http_result.h"
NearbyShareHttpError NearbyShareHttpErrorForHttpResponseCode(
int response_code) {
if (response_code == 400)
return NearbyShareHttpError::kBadRequest;
if (response_code == 403)
return NearbyShareHttpError::kAuthenticationError;
if (response_code == 404)
return NearbyShareHttpError::kEndpointNotFound;
if (response_code >= 500 && response_code < 600)
return NearbyShareHttpError::kInternalServerError;
return NearbyShareHttpError::kUnknown;
}
NearbyShareHttpResult NearbyShareHttpErrorToResult(NearbyShareHttpError error) {
switch (error) {
case NearbyShareHttpError::kOffline:
return NearbyShareHttpResult::kHttpErrorOffline;
case NearbyShareHttpError::kEndpointNotFound:
return NearbyShareHttpResult::kHttpErrorEndpointNotFound;
case NearbyShareHttpError::kAuthenticationError:
return NearbyShareHttpResult::kHttpErrorAuthenticationError;
case NearbyShareHttpError::kBadRequest:
return NearbyShareHttpResult::kHttpErrorBadRequest;
case NearbyShareHttpError::kResponseMalformed:
return NearbyShareHttpResult::kHttpErrorResponseMalformed;
case NearbyShareHttpError::kInternalServerError:
return NearbyShareHttpResult::kHttpErrorInternalServerError;
case NearbyShareHttpError::kUnknown:
return NearbyShareHttpResult::kHttpErrorUnknown;
}
}
std::ostream& operator<<(std::ostream& stream,
const NearbyShareHttpResult& result) {
switch (result) {
case NearbyShareHttpResult::kSuccess:
stream << "[Success]";
break;
case NearbyShareHttpResult::kTimeout:
stream << "[Timeout]";
break;
case NearbyShareHttpResult::kHttpErrorOffline:
stream << "[HTTP Error: Offline]";
break;
case NearbyShareHttpResult::kHttpErrorEndpointNotFound:
stream << "[HTTP Error: Endpoint not found]";
break;
case NearbyShareHttpResult::kHttpErrorAuthenticationError:
stream << "[HTTP Error: Authentication error]";
break;
case NearbyShareHttpResult::kHttpErrorBadRequest:
stream << "[HTTP Error: Bad request]";
break;
case NearbyShareHttpResult::kHttpErrorResponseMalformed:
stream << "[HTTP Error: Response malformed]";
break;
case NearbyShareHttpResult::kHttpErrorInternalServerError:
stream << "[HTTP Error: Internal server error]";
break;
case NearbyShareHttpResult::kHttpErrorUnknown:
stream << "[HTTP Error: Unknown]";
break;
}
return stream;
}
std::ostream& operator<<(std::ostream& stream,
const NearbyShareHttpError& error) {
stream << NearbyShareHttpErrorToResult(error);
return stream;
}
......@@ -2,12 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NEARBY_SHARING_CLIENT_NEARBY_SHARE_REQUEST_ERROR_H_
#define CHROME_BROWSER_NEARBY_SHARING_CLIENT_NEARBY_SHARE_REQUEST_ERROR_H_
#ifndef CHROME_BROWSER_NEARBY_SHARING_COMMON_NEARBY_SHARE_HTTP_RESULT_H_
#define CHROME_BROWSER_NEARBY_SHARING_COMMON_NEARBY_SHARE_HTTP_RESULT_H_
#include <ostream>
enum class NearbyShareRequestError {
enum class NearbyShareHttpError {
// Request could not be completed because the device is offline or has issues
// sending the HTTP request.
kOffline,
......@@ -31,7 +31,27 @@ enum class NearbyShareRequestError {
kUnknown
};
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class NearbyShareHttpResult {
kSuccess = 0,
kTimeout = 1,
kHttpErrorOffline = 2,
kHttpErrorEndpointNotFound = 3,
kHttpErrorAuthenticationError = 4,
kHttpErrorBadRequest = 5,
kHttpErrorResponseMalformed = 6,
kHttpErrorInternalServerError = 7,
kHttpErrorUnknown = 8,
kMaxValue = kHttpErrorUnknown
};
NearbyShareHttpError NearbyShareHttpErrorForHttpResponseCode(int response_code);
NearbyShareHttpResult NearbyShareHttpErrorToResult(NearbyShareHttpError error);
std::ostream& operator<<(std::ostream& stream,
const NearbyShareHttpResult& error);
std::ostream& operator<<(std::ostream& stream,
const NearbyShareRequestError& error);
const NearbyShareHttpError& error);
#endif // CHROME_BROWSER_NEARBY_SHARING_CLIENT_NEARBY_SHARE_REQUEST_ERROR_H_
#endif // CHROME_BROWSER_NEARBY_SHARING_COMMON_NEARBY_SHARE_HTTP_RESULT_H_
......@@ -10,47 +10,12 @@
namespace {
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class UpdaterResultCode {
kSuccess = 0,
kTimeout = 1,
kHttpErrorOffline = 2,
kHttpErrorEndpointNotFound = 3,
kHttpErrorAuthenticationError = 4,
kHttpErrorBadRequest = 5,
kHttpErrorResponseMalformed = 6,
kHttpErrorInternalServerError = 7,
kHttpErrorUnknown = 8,
kMaxValue = kHttpErrorUnknown
};
const char kDeviceIdPrefix[] = "users/me/devices/";
const char kDeviceNameFieldMaskPath[] = "device.display_name";
const char kContactsFieldMaskPath[] = "device.contacts";
const char kCertificatesFieldMaskPath[] = "device.public_certificates";
UpdaterResultCode RequestErrorToUpdaterResultCode(
NearbyShareRequestError error) {
switch (error) {
case NearbyShareRequestError::kOffline:
return UpdaterResultCode::kHttpErrorOffline;
case NearbyShareRequestError::kEndpointNotFound:
return UpdaterResultCode::kHttpErrorEndpointNotFound;
case NearbyShareRequestError::kAuthenticationError:
return UpdaterResultCode::kHttpErrorAuthenticationError;
case NearbyShareRequestError::kBadRequest:
return UpdaterResultCode::kHttpErrorBadRequest;
case NearbyShareRequestError::kResponseMalformed:
return UpdaterResultCode::kHttpErrorResponseMalformed;
case NearbyShareRequestError::kInternalServerError:
return UpdaterResultCode::kHttpErrorInternalServerError;
case NearbyShareRequestError::kUnknown:
return UpdaterResultCode::kHttpErrorUnknown;
}
}
void RecordResultMetrics(UpdaterResultCode code) {
void RecordResultMetrics(NearbyShareHttpResult result) {
// TODO(crbug.com/1105579): Record a histogram value for each result.
}
......@@ -130,20 +95,20 @@ void NearbyShareDeviceDataUpdaterImpl::OnRpcSuccess(
timer_.Stop();
nearbyshare::proto::UpdateDeviceResponse response_copy(response);
client_.reset();
RecordResultMetrics(UpdaterResultCode::kSuccess);
RecordResultMetrics(NearbyShareHttpResult::kSuccess);
FinishAttempt(response_copy);
}
void NearbyShareDeviceDataUpdaterImpl::OnRpcFailure(
NearbyShareRequestError error) {
NearbyShareHttpError error) {
timer_.Stop();
client_.reset();
RecordResultMetrics(RequestErrorToUpdaterResultCode(error));
RecordResultMetrics(NearbyShareHttpErrorToResult(error));
FinishAttempt(/*response=*/base::nullopt);
}
void NearbyShareDeviceDataUpdaterImpl::OnTimeout() {
client_.reset();
RecordResultMetrics(UpdaterResultCode::kTimeout);
RecordResultMetrics(NearbyShareHttpResult::kTimeout);
FinishAttempt(/*response=*/base::nullopt);
}
......@@ -9,7 +9,7 @@
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_request_error.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_http_result.h"
#include "chrome/browser/nearby_sharing/local_device_data/nearby_share_device_data_updater.h"
#include "chrome/browser/nearby_sharing/proto/device_rpc.pb.h"
......@@ -51,7 +51,7 @@ class NearbyShareDeviceDataUpdaterImpl : public NearbyShareDeviceDataUpdater {
void HandleNextRequest() override;
void OnTimeout();
void OnRpcSuccess(const nearbyshare::proto::UpdateDeviceResponse& response);
void OnRpcFailure(NearbyShareRequestError error);
void OnRpcFailure(NearbyShareHttpError error);
base::TimeDelta timeout_;
NearbyShareClientFactory* client_factory_ = nullptr;
......
......@@ -13,7 +13,7 @@
#include "base/time/clock.h"
#include "base/time/time.h"
#include "chrome/browser/nearby_sharing/client/fake_nearby_share_client.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_request_error.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_http_result.h"
#include "chrome/browser/nearby_sharing/local_device_data/nearby_share_device_data_updater_impl.h"
#include "chrome/browser/nearby_sharing/proto/device_rpc.pb.h"
#include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h"
......@@ -179,7 +179,7 @@ class NearbyShareDeviceDataUpdaterImplTest : public ::testing::Test {
break;
case UpdateDeviceRequestResult::kHttpFailure:
std::move(client->update_device_requests()[0].error_callback)
.Run(NearbyShareRequestError::kBadRequest);
.Run(NearbyShareHttpError::kBadRequest);
break;
case UpdateDeviceRequestResult::kTimeout:
FastForward(kTestTimeout);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment