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 DISALLOW_COPY_AND_ASSIGN(Job
);
77 MojoProxyResolverFactoryImpl::Job::Job(
78 MojoProxyResolverFactoryImpl
* factory
,
79 const scoped_refptr
<ProxyResolverScriptData
>& pac_script
,
80 ProxyResolverV8TracingFactory
* proxy_resolver_factory
,
81 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
,
82 interfaces::ProxyResolverFactoryRequestClientPtr client
)
84 proxy_request_(request
.Pass()),
85 factory_(proxy_resolver_factory
),
86 client_ptr_(client
.Pass()) {
87 client_ptr_
.set_connection_error_handler(
88 base::Bind(&MojoProxyResolverFactoryImpl::Job::OnConnectionError
,
89 base::Unretained(this)));
90 factory_
->CreateProxyResolverV8Tracing(
92 make_scoped_ptr(new MojoProxyResolverV8TracingBindings
<
93 interfaces::ProxyResolverFactoryRequestClient
>(
95 &proxy_resolver_impl_
,
96 base::Bind(&MojoProxyResolverFactoryImpl::Job::OnProxyResolverCreated
,
97 base::Unretained(this)),
101 MojoProxyResolverFactoryImpl::Job::~Job() = default;
103 void MojoProxyResolverFactoryImpl::Job::OnConnectionError() {
104 client_ptr_
->ReportResult(ERR_PAC_SCRIPT_TERMINATED
);
105 parent_
->RemoveJob(this);
108 void MojoProxyResolverFactoryImpl::Job::OnProxyResolverCreated(int error
) {
110 // The MojoProxyResolverHolder will delete itself if |proxy_request_|
111 // encounters a connection error.
112 new MojoProxyResolverHolder(proxy_resolver_impl_
.Pass(),
113 proxy_request_
.Pass());
115 client_ptr_
->ReportResult(error
);
116 parent_
->RemoveJob(this);
119 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl(
120 scoped_ptr
<ProxyResolverV8TracingFactory
> proxy_resolver_factory
,
121 mojo::InterfaceRequest
<interfaces::ProxyResolverFactory
> request
)
122 : proxy_resolver_impl_factory_(proxy_resolver_factory
.Pass()),
123 binding_(this, request
.Pass()) {
126 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl(
127 mojo::InterfaceRequest
<interfaces::ProxyResolverFactory
> request
)
128 : MojoProxyResolverFactoryImpl(ProxyResolverV8TracingFactory::Create(),
132 MojoProxyResolverFactoryImpl::~MojoProxyResolverFactoryImpl() {
133 STLDeleteElements(&jobs_
);
136 void MojoProxyResolverFactoryImpl::CreateResolver(
137 const mojo::String
& pac_script
,
138 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
,
139 interfaces::ProxyResolverFactoryRequestClientPtr client
) {
140 // The Job will call RemoveJob on |this| when either the create request
141 // finishes or |request| or |client| encounters a connection error.
142 jobs_
.insert(new Job(
143 this, ProxyResolverScriptData::FromUTF8(pac_script
.To
<std::string
>()),
144 proxy_resolver_impl_factory_
.get(), request
.Pass(), client
.Pass()));
147 void MojoProxyResolverFactoryImpl::RemoveJob(Job
* job
) {
148 size_t erased
= jobs_
.erase(job
);
149 DCHECK_EQ(1u, erased
);