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 "chrome/browser/net/utility_process_mojo_proxy_resolver_factory.h"
7 #include "base/logging.h"
8 #include "base/memory/singleton.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/utility_process_host.h"
11 #include "content/public/browser/utility_process_host_client.h"
12 #include "content/public/common/service_registry.h"
13 #include "net/proxy/mojo_proxy_resolver_factory.h"
16 UtilityProcessMojoProxyResolverFactory
*
17 UtilityProcessMojoProxyResolverFactory::GetInstance() {
18 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
20 UtilityProcessMojoProxyResolverFactory
,
21 LeakySingletonTraits
<UtilityProcessMojoProxyResolverFactory
>>::get();
24 UtilityProcessMojoProxyResolverFactory::
25 UtilityProcessMojoProxyResolverFactory() {
26 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
29 UtilityProcessMojoProxyResolverFactory::
30 ~UtilityProcessMojoProxyResolverFactory() {
31 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
34 void UtilityProcessMojoProxyResolverFactory::CreateProcessAndConnect() {
35 DVLOG(1) << "Attempting to create utility process for proxy resolver";
36 content::UtilityProcessHost
* utility_process_host
=
37 content::UtilityProcessHost::Create(
38 scoped_refptr
<content::UtilityProcessHostClient
>(),
39 base::MessageLoopProxy::current().get());
40 bool process_started
= utility_process_host
->StartMojoMode();
41 if (process_started
) {
42 content::ServiceRegistry
* service_registry
=
43 utility_process_host
->GetServiceRegistry();
44 service_registry
->ConnectToRemoteService(&resolver_factory_
);
45 resolver_factory_
.set_error_handler(this);
47 LOG(ERROR
) << "Unable to connect to utility process";
51 void UtilityProcessMojoProxyResolverFactory::Create(
52 mojo::InterfaceRequest
<net::interfaces::ProxyResolver
> req
,
53 net::interfaces::HostResolverPtr host_resolver
) {
54 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
55 if (!resolver_factory_
)
56 CreateProcessAndConnect();
58 if (!resolver_factory_
) {
59 // If there's still no factory, then utility process creation failed so
60 // close |req|'s message pipe, which should cause a connection error.
64 resolver_factory_
->CreateResolver(req
.Pass(), host_resolver
.Pass());
67 void UtilityProcessMojoProxyResolverFactory::OnConnectionError() {
68 DVLOG(1) << "Disconnection from utility process detected";
69 resolver_factory_
.reset();