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_factory_impl.h"
10 #include "base/bind.h"
11 #include "base/memory/raw_ptr.h"
12 #include "mojo/public/cpp/bindings/pending_receiver.h"
13 #include "mojo/public/cpp/bindings/pending_remote.h"
14 #include "net/base/net_errors.h"
15 #include "net/proxy_resolution/proxy_resolver_factory.h"
16 #include "services/proxy_resolver/mojo_proxy_resolver_v8_tracing_bindings.h"
17 #include "services/proxy_resolver/proxy_resolver_impl.h"
18 #include "services/proxy_resolver/proxy_resolver_v8_tracing.h"
20 namespace proxy_resolver {
22 class ProxyResolverFactoryImpl::Job {
24 Job(ProxyResolverFactoryImpl* parent,
25 const scoped_refptr<net::PacFileData>& pac_script,
26 ProxyResolverV8TracingFactory* proxy_resolver_factory,
27 mojo::PendingReceiver<mojom::ProxyResolver> receiver,
28 mojo::PendingRemote<mojom::ProxyResolverFactoryRequestClient> client);
30 Job(const Job&) = delete;
31 Job& operator=(const Job&) = delete;
37 void OnProxyResolverCreated(int error);
39 const raw_ptr<ProxyResolverFactoryImpl> parent_;
40 std::unique_ptr<ProxyResolverV8Tracing> proxy_resolver_impl_;
41 mojo::PendingReceiver<mojom::ProxyResolver> proxy_receiver_;
42 raw_ptr<ProxyResolverV8TracingFactory> factory_;
43 std::unique_ptr<net::ProxyResolverFactory::Request> request_;
44 mojo::Remote<mojom::ProxyResolverFactoryRequestClient> remote_client_;
47 ProxyResolverFactoryImpl::Job::Job(
48 ProxyResolverFactoryImpl* factory,
49 const scoped_refptr<net::PacFileData>& pac_script,
50 ProxyResolverV8TracingFactory* proxy_resolver_factory,
51 mojo::PendingReceiver<mojom::ProxyResolver> receiver,
52 mojo::PendingRemote<mojom::ProxyResolverFactoryRequestClient> client)
54 proxy_receiver_(std::move(receiver)),
55 factory_(proxy_resolver_factory),
56 remote_client_(std::move(client)) {
57 remote_client_.set_disconnect_handler(base::BindOnce(
58 &ProxyResolverFactoryImpl::Job::OnDisconnect, base::Unretained(this)));
59 factory_->CreateProxyResolverV8Tracing(
61 std::make_unique<MojoProxyResolverV8TracingBindings<
62 mojom::ProxyResolverFactoryRequestClient>>(remote_client_.get()),
63 &proxy_resolver_impl_,
64 base::BindOnce(&ProxyResolverFactoryImpl::Job::OnProxyResolverCreated,
65 base::Unretained(this)),
69 ProxyResolverFactoryImpl::Job::~Job() = default;
71 void ProxyResolverFactoryImpl::Job::OnDisconnect() {
72 remote_client_->ReportResult(net::ERR_PAC_SCRIPT_TERMINATED);
73 parent_->RemoveJob(this);
76 void ProxyResolverFactoryImpl::Job::OnProxyResolverCreated(int error) {
77 if (error == net::OK) {
79 std::make_unique<ProxyResolverImpl>(std::move(proxy_resolver_impl_)),
80 std::move(proxy_receiver_));
82 remote_client_->ReportResult(error);
83 parent_->RemoveJob(this);
86 ProxyResolverFactoryImpl::ProxyResolverFactoryImpl(
87 mojo::PendingReceiver<mojom::ProxyResolverFactory> receiver)
88 : ProxyResolverFactoryImpl(std::move(receiver),
89 ProxyResolverV8TracingFactory::Create()) {}
91 void ProxyResolverFactoryImpl::AddResolver(
92 std::unique_ptr<mojom::ProxyResolver> resolver,
93 mojo::PendingReceiver<mojom::ProxyResolver> receiver) {
94 resolvers_.Add(std::move(resolver), std::move(receiver));
97 ProxyResolverFactoryImpl::ProxyResolverFactoryImpl(
98 mojo::PendingReceiver<mojom::ProxyResolverFactory> receiver,
99 std::unique_ptr<ProxyResolverV8TracingFactory> proxy_resolver_factory)
100 : proxy_resolver_impl_factory_(std::move(proxy_resolver_factory)),
101 receiver_(this, std::move(receiver)) {}
103 ProxyResolverFactoryImpl::~ProxyResolverFactoryImpl() = default;
105 void ProxyResolverFactoryImpl::CreateResolver(
106 const std::string& pac_script,
107 mojo::PendingReceiver<mojom::ProxyResolver> receiver,
108 mojo::PendingRemote<mojom::ProxyResolverFactoryRequestClient> client) {
109 // The Job will call RemoveJob on |this| when either the create request
110 // finishes or |receiver| or |client| encounters a connection error.
111 std::unique_ptr<Job> job =
112 std::make_unique<Job>(this, net::PacFileData::FromUTF8(pac_script),
113 proxy_resolver_impl_factory_.get(),
114 std::move(receiver), std::move(client));
115 Job* job_ptr = job.get();
116 jobs_[job_ptr] = std::move(job);
119 void ProxyResolverFactoryImpl::RemoveJob(Job* job) {
120 size_t erased_count = jobs_.erase(job);
121 DCHECK_EQ(1U, erased_count);
124 } // namespace proxy_resolver