| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/net/utility_process_mojo_proxy_resolver_factory.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/memory/singleton.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "base/threading/thread_task_runner_handle.h" | |
| 14 #include "chrome/grit/generated_resources.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/utility_process_host.h" | |
| 17 #include "content/public/browser/utility_process_host_client.h" | |
| 18 #include "services/service_manager/public/cpp/interface_provider.h" | |
| 19 #include "ui/base/l10n/l10n_util.h" | |
| 20 | |
| 21 namespace { | |
| 22 const int kUtilityProcessIdleTimeoutSeconds = 5; | |
| 23 } | |
| 24 | |
| 25 // static | |
| 26 UtilityProcessMojoProxyResolverFactory* | |
| 27 UtilityProcessMojoProxyResolverFactory::GetInstance() { | |
| 28 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 29 return base::Singleton<UtilityProcessMojoProxyResolverFactory, | |
| 30 base::LeakySingletonTraits< | |
| 31 UtilityProcessMojoProxyResolverFactory>>::get(); | |
| 32 } | |
| 33 | |
| 34 UtilityProcessMojoProxyResolverFactory:: | |
| 35 UtilityProcessMojoProxyResolverFactory() { | |
| 36 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 37 } | |
| 38 | |
| 39 UtilityProcessMojoProxyResolverFactory:: | |
| 40 ~UtilityProcessMojoProxyResolverFactory() { | |
| 41 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 42 } | |
| 43 | |
| 44 void UtilityProcessMojoProxyResolverFactory::CreateProcessAndConnect() { | |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 46 DCHECK(!resolver_factory_); | |
| 47 DCHECK(!weak_utility_process_host_); | |
| 48 DVLOG(1) << "Attempting to create utility process for proxy resolver"; | |
| 49 content::UtilityProcessHost* utility_process_host = | |
| 50 content::UtilityProcessHost::Create( | |
| 51 scoped_refptr<content::UtilityProcessHostClient>(), | |
| 52 base::ThreadTaskRunnerHandle::Get()); | |
| 53 utility_process_host->SetName(l10n_util::GetStringUTF16( | |
| 54 IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME)); | |
| 55 bool process_started = utility_process_host->Start(); | |
| 56 if (process_started) { | |
| 57 BindInterface(utility_process_host, &resolver_factory_); | |
| 58 resolver_factory_.set_connection_error_handler( | |
| 59 base::Bind(&UtilityProcessMojoProxyResolverFactory::OnConnectionError, | |
| 60 base::Unretained(this))); | |
| 61 weak_utility_process_host_ = utility_process_host->AsWeakPtr(); | |
| 62 } else { | |
| 63 LOG(ERROR) << "Unable to connect to utility process"; | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 std::unique_ptr<base::ScopedClosureRunner> | |
| 68 UtilityProcessMojoProxyResolverFactory::CreateResolver( | |
| 69 const std::string& pac_script, | |
| 70 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req, | |
| 71 net::interfaces::ProxyResolverFactoryRequestClientPtr client) { | |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 73 if (!resolver_factory_) | |
| 74 CreateProcessAndConnect(); | |
| 75 | |
| 76 if (!resolver_factory_) { | |
| 77 // If there's still no factory, then utility process creation failed so | |
| 78 // close |req|'s message pipe, which should cause a connection error. | |
| 79 req = nullptr; | |
| 80 return nullptr; | |
| 81 } | |
| 82 idle_timer_.Stop(); | |
| 83 num_proxy_resolvers_++; | |
| 84 resolver_factory_->CreateResolver(pac_script, std::move(req), | |
| 85 std::move(client)); | |
| 86 return base::MakeUnique<base::ScopedClosureRunner>( | |
| 87 base::Bind(&UtilityProcessMojoProxyResolverFactory::OnResolverDestroyed, | |
| 88 base::Unretained(this))); | |
| 89 } | |
| 90 | |
| 91 void UtilityProcessMojoProxyResolverFactory::OnConnectionError() { | |
| 92 DVLOG(1) << "Disconnection from utility process detected"; | |
| 93 resolver_factory_.reset(); | |
| 94 delete weak_utility_process_host_.get(); | |
| 95 weak_utility_process_host_.reset(); | |
| 96 } | |
| 97 | |
| 98 void UtilityProcessMojoProxyResolverFactory::OnResolverDestroyed() { | |
| 99 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 100 DCHECK_GT(num_proxy_resolvers_, 0u); | |
| 101 if (--num_proxy_resolvers_ == 0) { | |
| 102 // When all proxy resolvers have been destroyed, the proxy resolver utility | |
| 103 // process is no longer needed. However, new proxy resolvers may be created | |
| 104 // shortly after being destroyed (e.g. due to a network change). If the | |
| 105 // utility process is shut down immediately, this would cause unnecessary | |
| 106 // process churn, so wait for an idle timeout before shutting down the | |
| 107 // proxy resolver utility process. | |
| 108 idle_timer_.Start( | |
| 109 FROM_HERE, | |
| 110 base::TimeDelta::FromSeconds(kUtilityProcessIdleTimeoutSeconds), this, | |
| 111 &UtilityProcessMojoProxyResolverFactory::OnIdleTimeout); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void UtilityProcessMojoProxyResolverFactory::OnIdleTimeout() { | |
| 116 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 117 DCHECK_EQ(num_proxy_resolvers_, 0u); | |
| 118 delete weak_utility_process_host_.get(); | |
| 119 weak_utility_process_host_.reset(); | |
| 120 resolver_factory_.reset(); | |
| 121 } | |
| OLD | NEW |