Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / net / utility_process_mojo_proxy_resolver_factory.cc
bloba01ba16665b622c57a86d08d5ae1540272fdf288
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 "chrome/grit/generated_resources.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/utility_process_host.h"
12 #include "content/public/browser/utility_process_host_client.h"
13 #include "content/public/common/service_registry.h"
14 #include "net/proxy/mojo_proxy_resolver_factory.h"
15 #include "ui/base/l10n/l10n_util.h"
17 // static
18 UtilityProcessMojoProxyResolverFactory*
19 UtilityProcessMojoProxyResolverFactory::GetInstance() {
20 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
21 return Singleton<
22 UtilityProcessMojoProxyResolverFactory,
23 LeakySingletonTraits<UtilityProcessMojoProxyResolverFactory>>::get();
26 UtilityProcessMojoProxyResolverFactory::
27 UtilityProcessMojoProxyResolverFactory() {
28 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
31 UtilityProcessMojoProxyResolverFactory::
32 ~UtilityProcessMojoProxyResolverFactory() {
33 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
36 void UtilityProcessMojoProxyResolverFactory::CreateProcessAndConnect() {
37 DVLOG(1) << "Attempting to create utility process for proxy resolver";
38 content::UtilityProcessHost* utility_process_host =
39 content::UtilityProcessHost::Create(
40 scoped_refptr<content::UtilityProcessHostClient>(),
41 base::MessageLoopProxy::current().get());
42 utility_process_host->SetName(l10n_util::GetStringUTF16(
43 IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME));
44 bool process_started = utility_process_host->StartMojoMode();
45 if (process_started) {
46 content::ServiceRegistry* service_registry =
47 utility_process_host->GetServiceRegistry();
48 service_registry->ConnectToRemoteService(&resolver_factory_);
49 resolver_factory_.set_error_handler(this);
50 } else {
51 LOG(ERROR) << "Unable to connect to utility process";
55 void UtilityProcessMojoProxyResolverFactory::Create(
56 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req,
57 net::interfaces::HostResolverPtr host_resolver) {
58 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
59 if (!resolver_factory_)
60 CreateProcessAndConnect();
62 if (!resolver_factory_) {
63 // If there's still no factory, then utility process creation failed so
64 // close |req|'s message pipe, which should cause a connection error.
65 req = nullptr;
66 return;
68 resolver_factory_->CreateResolver(req.Pass(), host_resolver.Pass());
71 void UtilityProcessMojoProxyResolverFactory::OnConnectionError() {
72 DVLOG(1) << "Disconnection from utility process detected";
73 resolver_factory_.reset();