1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/proxy/mojo_proxy_resolver_factory_impl.h"
9 #include "base/stl_util.h"
10 #include "net/base/net_errors.h"
11 #include "net/proxy/mojo_proxy_resolver_impl.h"
12 #include "net/proxy/mojo_proxy_resolver_v8_tracing_bindings.h"
13 #include "net/proxy/proxy_resolver_factory.h"
14 #include "net/proxy/proxy_resolver_v8_tracing.h"
19 // A class to manage the lifetime of a MojoProxyResolverImpl. An instance will
20 // remain while the message pipe for the mojo connection remains open.
21 class MojoProxyResolverHolder
{
23 MojoProxyResolverHolder(
24 scoped_ptr
<ProxyResolverV8Tracing
> proxy_resolver_impl
,
25 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
);
28 // Mojo error handler.
29 void OnConnectionError();
31 MojoProxyResolverImpl mojo_proxy_resolver_
;
32 mojo::Binding
<interfaces::ProxyResolver
> binding_
;
34 DISALLOW_COPY_AND_ASSIGN(MojoProxyResolverHolder
);
37 MojoProxyResolverHolder::MojoProxyResolverHolder(
38 scoped_ptr
<ProxyResolverV8Tracing
> proxy_resolver_impl
,
39 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
)
40 : mojo_proxy_resolver_(proxy_resolver_impl
.Pass()),
41 binding_(&mojo_proxy_resolver_
, request
.Pass()) {
42 binding_
.set_connection_error_handler(base::Bind(
43 &MojoProxyResolverHolder::OnConnectionError
, base::Unretained(this)));
46 void MojoProxyResolverHolder::OnConnectionError() {
52 class MojoProxyResolverFactoryImpl::Job
{
54 Job(MojoProxyResolverFactoryImpl
* parent
,
55 const scoped_refptr
<ProxyResolverScriptData
>& pac_script
,
56 ProxyResolverV8TracingFactory
* proxy_resolver_factory
,
57 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
,
58 interfaces::ProxyResolverFactoryRequestClientPtr client
);
62 // Mojo error handler.
63 void OnConnectionError();
65 void OnProxyResolverCreated(int error
);
67 MojoProxyResolverFactoryImpl
* const parent_
;
68 scoped_ptr
<ProxyResolverV8Tracing
> proxy_resolver_impl_
;
69 mojo::InterfaceRequest
<interfaces::ProxyResolver
> proxy_request_
;
70 ProxyResolverV8TracingFactory
* factory_
;
71 scoped_ptr
<net::ProxyResolverFactory::Request
> request_
;
72 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr_
;
74 base::WeakPtrFactory
<interfaces::ProxyResolverFactoryRequestClient
>
77 DISALLOW_COPY_AND_ASSIGN(Job
);
80 MojoProxyResolverFactoryImpl::Job::Job(
81 MojoProxyResolverFactoryImpl
* factory
,
82 const scoped_refptr
<ProxyResolverScriptData
>& pac_script
,
83 ProxyResolverV8TracingFactory
* proxy_resolver_factory
,
84 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
,
85 interfaces::ProxyResolverFactoryRequestClientPtr client
)
87 proxy_request_(request
.Pass()),
88 factory_(proxy_resolver_factory
),
89 client_ptr_(client
.Pass()),
90 weak_factory_(client_ptr_
.get()) {
91 client_ptr_
.set_connection_error_handler(
92 base::Bind(&MojoProxyResolverFactoryImpl::Job::OnConnectionError
,
93 base::Unretained(this)));
94 factory_
->CreateProxyResolverV8Tracing(
95 pac_script
, make_scoped_ptr(new MojoProxyResolverV8TracingBindings
<
96 interfaces::ProxyResolverFactoryRequestClient
>(
97 weak_factory_
.GetWeakPtr())),
98 &proxy_resolver_impl_
,
99 base::Bind(&MojoProxyResolverFactoryImpl::Job::OnProxyResolverCreated
,
100 base::Unretained(this)),
104 MojoProxyResolverFactoryImpl::Job::~Job() = default;
106 void MojoProxyResolverFactoryImpl::Job::OnConnectionError() {
107 client_ptr_
->ReportResult(ERR_PAC_SCRIPT_TERMINATED
);
108 parent_
->RemoveJob(this);
111 void MojoProxyResolverFactoryImpl::Job::OnProxyResolverCreated(int error
) {
113 // The MojoProxyResolverHolder will delete itself if |proxy_request_|
114 // encounters a connection error.
115 new MojoProxyResolverHolder(proxy_resolver_impl_
.Pass(),
116 proxy_request_
.Pass());
118 client_ptr_
->ReportResult(error
);
119 parent_
->RemoveJob(this);
122 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl(
123 scoped_ptr
<ProxyResolverV8TracingFactory
> proxy_resolver_factory
,
124 mojo::InterfaceRequest
<interfaces::ProxyResolverFactory
> request
)
125 : proxy_resolver_impl_factory_(proxy_resolver_factory
.Pass()),
126 binding_(this, request
.Pass()) {
129 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl(
130 mojo::InterfaceRequest
<interfaces::ProxyResolverFactory
> request
)
131 : MojoProxyResolverFactoryImpl(ProxyResolverV8TracingFactory::Create(),
135 MojoProxyResolverFactoryImpl::~MojoProxyResolverFactoryImpl() {
136 STLDeleteElements(&jobs_
);
139 void MojoProxyResolverFactoryImpl::CreateResolver(
140 const mojo::String
& pac_script
,
141 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
,
142 interfaces::ProxyResolverFactoryRequestClientPtr client
) {
143 // The Job will call RemoveJob on |this| when either the create request
144 // finishes or |request| or |client| encounters a connection error.
145 jobs_
.insert(new Job(
146 this, ProxyResolverScriptData::FromUTF8(pac_script
.To
<std::string
>()),
147 proxy_resolver_impl_factory_
.get(), request
.Pass(), client
.Pass()));
150 void MojoProxyResolverFactoryImpl::RemoveJob(Job
* job
) {
151 size_t erased
= jobs_
.erase(job
);
152 DCHECK_EQ(1u, erased
);