1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "services/proxy_resolver/proxy_resolver_impl.h"
10 #include "base/memory/raw_ptr.h"
11 #include "mojo/public/cpp/bindings/remote.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/network_anonymization_key.h"
14 #include "net/base/proxy_string_util.h"
15 #include "net/proxy_resolution/pac_file_data.h"
16 #include "net/proxy_resolution/proxy_info.h"
17 #include "services/proxy_resolver/mojo_proxy_resolver_v8_tracing_bindings.h"
18 #include "services/proxy_resolver/proxy_resolver_v8_tracing.h"
20 namespace proxy_resolver {
22 class ProxyResolverImpl::Job {
24 Job(mojo::PendingRemote<mojom::ProxyResolverRequestClient> client,
25 ProxyResolverImpl* resolver,
28 Job(const Job&) = delete;
29 Job& operator=(const Job&) = delete;
33 void Start(const net::NetworkAnonymizationKey& network_anonymization_key);
36 // Mojo error handler. This is invoked in response to the client
37 // disconnecting, indicating cancellation.
40 void GetProxyDone(int error);
42 raw_ptr<ProxyResolverImpl> resolver_;
44 mojo::Remote<mojom::ProxyResolverRequestClient> client_;
45 net::ProxyInfo result_;
47 std::unique_ptr<net::ProxyResolver::Request> request_;
51 ProxyResolverImpl::ProxyResolverImpl(
52 std::unique_ptr<ProxyResolverV8Tracing> resolver)
53 : resolver_(std::move(resolver)) {}
55 ProxyResolverImpl::~ProxyResolverImpl() = default;
57 void ProxyResolverImpl::GetProxyForUrl(
59 const net::NetworkAnonymizationKey& network_anonymization_key,
60 mojo::PendingRemote<mojom::ProxyResolverRequestClient> client) {
61 DVLOG(1) << "GetProxyForUrl(" << url << ")";
62 std::unique_ptr<Job> job =
63 std::make_unique<Job>(std::move(client), this, url);
64 Job* job_ptr = job.get();
65 resolve_jobs_[job_ptr] = std::move(job);
66 job_ptr->Start(network_anonymization_key);
69 void ProxyResolverImpl::DeleteJob(Job* job) {
70 size_t erased_count = resolve_jobs_.erase(job);
71 DCHECK_EQ(1U, erased_count);
74 ProxyResolverImpl::Job::Job(
75 mojo::PendingRemote<mojom::ProxyResolverRequestClient> client,
76 ProxyResolverImpl* resolver,
78 : resolver_(resolver),
79 client_(std::move(client)),
83 ProxyResolverImpl::Job::~Job() = default;
85 void ProxyResolverImpl::Job::Start(
86 const net::NetworkAnonymizationKey& network_anonymization_key) {
87 resolver_->resolver_->GetProxyForURL(
88 url_, network_anonymization_key, &result_,
89 base::BindOnce(&Job::GetProxyDone, base::Unretained(this)), &request_,
90 std::make_unique<MojoProxyResolverV8TracingBindings<
91 mojom::ProxyResolverRequestClient>>(client_.get()));
92 client_.set_disconnect_handler(base::BindOnce(
93 &ProxyResolverImpl::Job::OnDisconnect, base::Unretained(this)));
96 void ProxyResolverImpl::Job::GetProxyDone(int error) {
98 DVLOG(1) << "GetProxyForUrl(" << url_ << ") finished with error " << error
99 << ". " << result_.proxy_list().size() << " Proxies returned:";
100 for (const auto& proxy : result_.proxy_list().GetAll()) {
101 DVLOG(1) << net::ProxyServerToProxyUri(proxy);
103 if (error == net::OK)
104 client_->ReportResult(error, result_);
106 client_->ReportResult(error, net::ProxyInfo());
108 resolver_->DeleteJob(this);
111 void ProxyResolverImpl::Job::OnDisconnect() {
112 resolver_->DeleteJob(this);
115 } // namespace proxy_resolver