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 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/chromedriver/net/net_util.h"
#include <memory>
#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/lazy_instance.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "url/gurl.h"
namespace {
base::LazyInstance<scoped_refptr<base::SequencedTaskRunner>>::Leaky
g_io_capable_task_runner_for_tests = LAZY_INSTANCE_INITIALIZER;
class SyncUrlFetcher {
public:
SyncUrlFetcher(const GURL& url,
network::mojom::URLLoaderFactory* url_loader_factory,
std::string* response)
: url_(url),
url_loader_factory_(url_loader_factory),
network_task_runner_(g_io_capable_task_runner_for_tests.Get()
? g_io_capable_task_runner_for_tests.Get()
: base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock()})),
response_(response),
event_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
~SyncUrlFetcher() = default;
bool Fetch() {
network_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&SyncUrlFetcher::FetchOnIOThread,
base::Unretained(this)));
event_.Wait();
return success_;
}
void FetchOnIOThread() {
DCHECK(network_task_runner_->RunsTasksInCurrentSequence());
auto request = std::make_unique<network::ResourceRequest>();
request->url = url_;
loader_ = network::SimpleURLLoader::Create(std::move(request),
TRAFFIC_ANNOTATION_FOR_TESTS);
loader_->SetTimeoutDuration(base::Seconds(10));
loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
url_loader_factory_, base::BindOnce(&SyncUrlFetcher::OnURLLoadComplete,
base::Unretained(this)));
}
void OnURLLoadComplete(std::unique_ptr<std::string> response_body) {
int response_code = -1;
if (loader_->ResponseInfo() && loader_->ResponseInfo()->headers)
response_code = loader_->ResponseInfo()->headers->response_code();
success_ = response_code == 200 && response_body;
if (success_)
*response_ = std::move(*response_body);
loader_.reset();
event_.Signal();
}
private:
GURL url_;
raw_ptr<network::mojom::URLLoaderFactory> url_loader_factory_;
const scoped_refptr<base::SequencedTaskRunner> network_task_runner_;
raw_ptr<std::string> response_;
base::WaitableEvent event_;
std::unique_ptr<network::SimpleURLLoader> loader_;
bool success_;
};
} // namespace
NetAddress::NetAddress() : port_(-1) {}
NetAddress::NetAddress(int port) : host_("localhost"), port_(port) {}
NetAddress::NetAddress(const std::string& host, int port)
: host_(host), port_(port) {}
NetAddress::~NetAddress() = default;
bool NetAddress::IsValid() const {
return port_ >= 0 && port_ < (1 << 16);
}
std::string NetAddress::ToString() const {
return host_ + base::StringPrintf(":%d", port_);
}
const std::string& NetAddress::host() const {
return host_;
}
int NetAddress::port() const {
return port_;
}
void SetIOCapableTaskRunnerForTest(
scoped_refptr<base::SequencedTaskRunner> task_runner) {
g_io_capable_task_runner_for_tests.Get() = task_runner;
}
bool FetchUrl(const std::string& url,
network::mojom::URLLoaderFactory* factory,
std::string* response) {
return SyncUrlFetcher(GURL(url), factory, response).Fetch();
}
bool FetchUrl(const GURL& url,
network::mojom::URLLoaderFactory* factory,
std::string* response) {
return SyncUrlFetcher(url, factory, response).Fetch();
}
|