1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/data_sharing/internal/data_sharing_network_loader_impl.h"
#include "base/time/time.h"
#include "components/data_sharing/public/data_sharing_network_loader.h"
#include "components/data_sharing/public/data_sharing_network_utils.h"
#include "components/data_sharing/public/group_data.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_status_code.h"
using endpoint_fetcher::EndpointFetcher;
using endpoint_fetcher::EndpointResponse;
namespace data_sharing {
namespace {
constexpr base::TimeDelta kTimeout = base::Milliseconds(10000);
const char kOauthConsumerName[] = "datasharing";
const char kRequestContentType[] = "application/x-protobuf";
} // namespace
DataSharingNetworkLoaderImpl::DataSharingNetworkLoaderImpl(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
signin::IdentityManager* identity_manager)
: url_loader_factory_(url_loader_factory),
identity_manager_(identity_manager) {
CHECK(identity_manager);
}
DataSharingNetworkLoaderImpl::~DataSharingNetworkLoaderImpl() = default;
void DataSharingNetworkLoaderImpl::LoadUrl(
const GURL& url,
const std::vector<std::string>& scopes,
const std::string& post_data,
const net::NetworkTrafficAnnotationTag& annotation_tag,
NetworkLoaderCallback callback) {
std::unique_ptr<EndpointFetcher> endpoint_fetcher =
CreateEndpointFetcher(url, scopes, post_data, annotation_tag);
auto* const fetcher_ptr = endpoint_fetcher.get();
fetcher_ptr->Fetch(
base::BindOnce(&DataSharingNetworkLoaderImpl::OnDownloadComplete,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(endpoint_fetcher)));
}
void DataSharingNetworkLoaderImpl::LoadUrl(
const GURL& url,
const std::vector<std::string>& scopes,
const std::string& post_data,
DataSharingRequestType requestType,
NetworkLoaderCallback callback) {
LoadUrl(url, scopes, post_data, GetNetworkTrafficAnnotationTag(requestType),
std::move(callback));
}
std::unique_ptr<EndpointFetcher>
DataSharingNetworkLoaderImpl::CreateEndpointFetcher(
const GURL& url,
const std::vector<std::string>& scopes,
const std::string& post_data,
const net::NetworkTrafficAnnotationTag& annotation_tag) {
return std::make_unique<EndpointFetcher>(
url_loader_factory_, kOauthConsumerName, url,
net::HttpRequestHeaders::kPostMethod, kRequestContentType, scopes,
kTimeout, post_data, annotation_tag, identity_manager_,
signin::ConsentLevel::kSignin);
}
void DataSharingNetworkLoaderImpl::OnDownloadComplete(
NetworkLoaderCallback callback,
std::unique_ptr<EndpointFetcher> fetcher,
std::unique_ptr<EndpointResponse> response) {
NetworkLoaderStatus status = NetworkLoaderStatus::kSuccess;
if (response->http_status_code != net::HTTP_OK || response->error_type) {
VLOG(1) << "Data sharing network request failed http status: "
<< response->http_status_code << " "
<< (response->error_type ? static_cast<int>(*response->error_type)
: -1);
// TODO(ssid): Investigate whether some auth errors are permanent.
status = NetworkLoaderStatus::kTransientFailure;
}
std::move(callback).Run(
std::make_unique<DataSharingNetworkLoader::LoadResult>(
std::move(response->response), status, response->http_status_code));
}
const net::NetworkTrafficAnnotationTag&
DataSharingNetworkLoaderImpl::GetNetworkTrafficAnnotationTag(
DataSharingRequestType request_type) {
switch (request_type) {
case DataSharingRequestType::kCreateGroup:
return kCreateGroupTrafficAnnotation;
case DataSharingRequestType::kReadGroups:
case DataSharingRequestType::kReadAllGroups:
return kReadGroupsTrafficAnnotation;
case DataSharingRequestType::kDeleteGroups:
return kDeleteGroupsTrafficAnnotation;
case DataSharingRequestType::kUpdateGroup:
return kUpdateGroupTrafficAnnotation;
// TODO(crbug.com/375594409): Add specific traffic annotation for request
// types below.
case DataSharingRequestType::kLookup:
case DataSharingRequestType::kWarmup:
case DataSharingRequestType::kAutocomplete:
case DataSharingRequestType::kMutateConnectionLabel:
case DataSharingRequestType::kLeaveGroup:
case DataSharingRequestType::kBlockPerson:
case DataSharingRequestType::kJoinGroup:
case DataSharingRequestType::kTestRequest:
case DataSharingRequestType::kUnknown:
return kReadGroupsTrafficAnnotation;
}
}
} // namespace data_sharing
|