Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/utility_process_mojo_proxy_resolver_factory.h" | 5 #include "chrome/browser/net/utility_process_mojo_proxy_resolver_factory.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "chrome/grit/generated_resources.h" | 9 #include "chrome/grit/generated_resources.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/utility_process_host.h" | 11 #include "content/public/browser/utility_process_host.h" |
| 12 #include "content/public/browser/utility_process_host_client.h" | 12 #include "content/public/browser/utility_process_host_client.h" |
| 13 #include "content/public/common/service_registry.h" | 13 #include "content/public/common/service_registry.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 15 | 15 |
| 16 namespace { | |
| 17 const int kUtilityProcessIdleTimeoutSeconds = 5; | |
| 18 } | |
| 19 | |
| 16 // static | 20 // static |
| 17 UtilityProcessMojoProxyResolverFactory* | 21 UtilityProcessMojoProxyResolverFactory* |
| 18 UtilityProcessMojoProxyResolverFactory::GetInstance() { | 22 UtilityProcessMojoProxyResolverFactory::GetInstance() { |
| 19 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 23 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 20 return Singleton< | 24 return Singleton< |
| 21 UtilityProcessMojoProxyResolverFactory, | 25 UtilityProcessMojoProxyResolverFactory, |
| 22 LeakySingletonTraits<UtilityProcessMojoProxyResolverFactory>>::get(); | 26 LeakySingletonTraits<UtilityProcessMojoProxyResolverFactory>>::get(); |
| 23 } | 27 } |
| 24 | 28 |
| 25 UtilityProcessMojoProxyResolverFactory:: | 29 UtilityProcessMojoProxyResolverFactory:: |
| 26 UtilityProcessMojoProxyResolverFactory() { | 30 UtilityProcessMojoProxyResolverFactory() { |
| 27 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 31 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 28 } | 32 } |
| 29 | 33 |
| 30 UtilityProcessMojoProxyResolverFactory:: | 34 UtilityProcessMojoProxyResolverFactory:: |
| 31 ~UtilityProcessMojoProxyResolverFactory() { | 35 ~UtilityProcessMojoProxyResolverFactory() { |
| 32 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 36 DCHECK(thread_checker_.CalledOnValidThread()); |
| 33 } | 37 } |
| 34 | 38 |
| 35 void UtilityProcessMojoProxyResolverFactory::CreateProcessAndConnect() { | 39 void UtilityProcessMojoProxyResolverFactory::CreateProcessAndConnect() { |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 36 DVLOG(1) << "Attempting to create utility process for proxy resolver"; | 41 DVLOG(1) << "Attempting to create utility process for proxy resolver"; |
| 37 content::UtilityProcessHost* utility_process_host = | 42 content::UtilityProcessHost* utility_process_host = |
| 38 content::UtilityProcessHost::Create( | 43 content::UtilityProcessHost::Create( |
| 39 scoped_refptr<content::UtilityProcessHostClient>(), | 44 scoped_refptr<content::UtilityProcessHostClient>(), |
| 40 base::MessageLoopProxy::current().get()); | 45 base::MessageLoopProxy::current().get()); |
| 41 utility_process_host->SetName(l10n_util::GetStringUTF16( | 46 utility_process_host->SetName(l10n_util::GetStringUTF16( |
| 42 IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME)); | 47 IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME)); |
| 43 bool process_started = utility_process_host->StartMojoMode(); | 48 bool process_started = utility_process_host->StartMojoMode(); |
| 44 if (process_started) { | 49 if (process_started) { |
| 45 content::ServiceRegistry* service_registry = | 50 content::ServiceRegistry* service_registry = |
| 46 utility_process_host->GetServiceRegistry(); | 51 utility_process_host->GetServiceRegistry(); |
| 47 service_registry->ConnectToRemoteService(&resolver_factory_); | 52 service_registry->ConnectToRemoteService(&resolver_factory_); |
| 48 resolver_factory_.set_error_handler(this); | 53 resolver_factory_.set_error_handler(this); |
| 54 weak_utility_process_host_ = utility_process_host->AsWeakPtr(); | |
| 49 } else { | 55 } else { |
| 50 LOG(ERROR) << "Unable to connect to utility process"; | 56 LOG(ERROR) << "Unable to connect to utility process"; |
| 51 } | 57 } |
| 52 } | 58 } |
| 53 | 59 |
| 54 void UtilityProcessMojoProxyResolverFactory::CreateResolver( | 60 scoped_ptr<base::ScopedClosureRunner> |
| 61 UtilityProcessMojoProxyResolverFactory::CreateResolver( | |
| 55 const mojo::String& pac_script, | 62 const mojo::String& pac_script, |
| 56 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req, | 63 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req, |
| 57 net::interfaces::HostResolverPtr host_resolver, | 64 net::interfaces::HostResolverPtr host_resolver, |
| 58 net::interfaces::ProxyResolverFactoryRequestClientPtr client) { | 65 net::interfaces::ProxyResolverFactoryRequestClientPtr client) { |
| 59 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 66 DCHECK(thread_checker_.CalledOnValidThread()); |
| 60 if (!resolver_factory_) | 67 if (!resolver_factory_) |
| 61 CreateProcessAndConnect(); | 68 CreateProcessAndConnect(); |
| 62 | 69 |
| 63 if (!resolver_factory_) { | 70 if (!resolver_factory_) { |
| 64 // If there's still no factory, then utility process creation failed so | 71 // If there's still no factory, then utility process creation failed so |
| 65 // close |req|'s message pipe, which should cause a connection error. | 72 // close |req|'s message pipe, which should cause a connection error. |
| 66 req = nullptr; | 73 req = nullptr; |
| 67 return; | 74 return nullptr; |
| 68 } | 75 } |
| 76 idle_timer_.Stop(); | |
| 77 num_proxy_resolvers_++; | |
| 69 resolver_factory_->CreateResolver(pac_script, req.Pass(), | 78 resolver_factory_->CreateResolver(pac_script, req.Pass(), |
| 70 host_resolver.Pass(), client.Pass()); | 79 host_resolver.Pass(), client.Pass()); |
| 80 return make_scoped_ptr(new base::ScopedClosureRunner( | |
| 81 base::Bind(&UtilityProcessMojoProxyResolverFactory::OnResolverDestroyed, | |
| 82 base::Unretained(this)))); | |
| 71 } | 83 } |
| 72 | 84 |
| 73 void UtilityProcessMojoProxyResolverFactory::OnConnectionError() { | 85 void UtilityProcessMojoProxyResolverFactory::OnConnectionError() { |
| 74 DVLOG(1) << "Disconnection from utility process detected"; | 86 DVLOG(1) << "Disconnection from utility process detected"; |
| 75 resolver_factory_.reset(); | 87 resolver_factory_.reset(); |
| 76 } | 88 } |
| 89 | |
| 90 void UtilityProcessMojoProxyResolverFactory::OnResolverDestroyed() { | |
| 91 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 92 DCHECK_GT(num_proxy_resolvers_, 0u); | |
| 93 if (--num_proxy_resolvers_ == 0) { | |
| 94 // When all proxy resolvers have been destroyed, the proxy resolver utility | |
| 95 // process is no longer needed. However, new proxy resolvers may be created | |
| 96 // shortly after being destroyed (e.g. due to a network change). If the | |
| 97 // utility process is shut down immediately, this would cause unnecessary | |
| 98 // process churn, so wait for an idle timeout before shutting down the | |
| 99 // proxy resolver utility process. | |
| 100 idle_timer_.Start( | |
| 101 FROM_HERE, | |
| 102 base::TimeDelta::FromSeconds(kUtilityProcessIdleTimeoutSeconds), this, | |
| 103 &UtilityProcessMojoProxyResolverFactory::OnIdleTimeout); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void UtilityProcessMojoProxyResolverFactory::OnIdleTimeout() { | |
| 108 DCHECK(thread_checker_.CalledOnValidThread()); | |
|
Anand Mistry (off Chromium)
2015/05/05 03:47:41
DCHECK_EQ(num_proxy_resolvers_, 0u);
Sam McNally
2015/05/05 03:54:04
Done.
| |
| 109 delete weak_utility_process_host_.get(); | |
| 110 weak_utility_process_host_.reset(); | |
| 111 resolver_factory_.reset(); | |
| 112 } | |
| OLD | NEW |