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"
7 #include "net/dns/host_resolver_mojo.h"
8 #include "net/proxy/mojo_proxy_resolver_impl.h"
9 #include "net/proxy/proxy_resolver_v8.h"
10 #include "net/proxy/proxy_resolver_v8_tracing.h"
11 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
16 scoped_ptr
<ProxyResolver
> CreateDefaultProxyResolver(
17 HostResolver
* host_resolver
,
18 const ProxyResolver::LoadStateChangedCallback
& callback
) {
19 return make_scoped_ptr(
20 new ProxyResolverV8Tracing(host_resolver
, nullptr, nullptr, callback
));
23 // A class to manage the lifetime of a MojoProxyResolverImpl and a
24 // HostResolverMojo. An instance will remain while the message pipes for both
25 // mojo connections remain open.
26 class MojoProxyResolverHolder
: public mojo::ErrorHandler
{
28 MojoProxyResolverHolder(
29 const MojoProxyResolverFactoryImpl::Factory
& proxy_resolver_factory
,
30 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
,
31 interfaces::HostResolverPtr host_resolver
);
34 // mojo::ErrorHandler override.
35 void OnConnectionError() override
;
37 void LoadStateChanged(ProxyResolver::RequestHandle handle
,
38 LoadState load_state
);
40 HostResolverMojo host_resolver_
;
41 MojoProxyResolverImpl proxy_resolver_
;
42 mojo::Binding
<interfaces::ProxyResolver
> binding_
;
44 DISALLOW_COPY_AND_ASSIGN(MojoProxyResolverHolder
);
47 MojoProxyResolverHolder::MojoProxyResolverHolder(
48 const MojoProxyResolverFactoryImpl::Factory
& proxy_resolver_factory
,
49 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
,
50 interfaces::HostResolverPtr host_resolver
)
51 : host_resolver_(host_resolver
.Pass(),
52 base::Bind(&MojoProxyResolverHolder::OnConnectionError
,
53 base::Unretained(this))),
54 proxy_resolver_(proxy_resolver_factory
.Run(
56 base::Bind(&MojoProxyResolverHolder::LoadStateChanged
,
57 base::Unretained(this)))),
58 binding_(&proxy_resolver_
, request
.Pass()) {
59 binding_
.set_error_handler(this);
62 void MojoProxyResolverHolder::OnConnectionError() {
66 void MojoProxyResolverHolder::LoadStateChanged(
67 ProxyResolver::RequestHandle handle
,
68 LoadState load_state
) {
69 proxy_resolver_
.LoadStateChanged(handle
, load_state
);
74 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl(
75 const MojoProxyResolverFactoryImpl::Factory
& proxy_resolver_factory
,
76 mojo::InterfaceRequest
<interfaces::ProxyResolverFactory
> request
)
77 : proxy_resolver_impl_factory_(proxy_resolver_factory
),
78 binding_(this, request
.Pass()) {
81 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl(
82 mojo::InterfaceRequest
<interfaces::ProxyResolverFactory
> request
)
83 : MojoProxyResolverFactoryImpl(base::Bind(&CreateDefaultProxyResolver
),
87 MojoProxyResolverFactoryImpl::~MojoProxyResolverFactoryImpl() = default;
89 void MojoProxyResolverFactoryImpl::CreateResolver(
90 mojo::InterfaceRequest
<interfaces::ProxyResolver
> request
,
91 interfaces::HostResolverPtr host_resolver
) {
92 // The MojoProxyResolverHolder will delete itself when either |request| or
93 // |host_resolver| encounters a connection error, that is, when either the
94 // ProxyResolver client or the HostResolver implementation is deleted.
95 new MojoProxyResolverHolder(proxy_resolver_impl_factory_
, request
.Pass(),
96 host_resolver
.Pass());