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 "net/proxy/mojo_proxy_resolver_factory.h" | 14 #include "net/proxy/mojo_proxy_resolver_factory.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 16 | 16 |
| 17 namespace { | |
| 18 const int kUtilityProcessIdleTimeoutSeconds = 5; | |
| 19 } | |
| 20 | |
| 17 // static | 21 // static |
| 18 UtilityProcessMojoProxyResolverFactory* | 22 UtilityProcessMojoProxyResolverFactory* |
| 19 UtilityProcessMojoProxyResolverFactory::GetInstance() { | 23 UtilityProcessMojoProxyResolverFactory::GetInstance() { |
| 20 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 24 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 21 return Singleton< | 25 return Singleton< |
| 22 UtilityProcessMojoProxyResolverFactory, | 26 UtilityProcessMojoProxyResolverFactory, |
| 23 LeakySingletonTraits<UtilityProcessMojoProxyResolverFactory>>::get(); | 27 LeakySingletonTraits<UtilityProcessMojoProxyResolverFactory>>::get(); |
| 24 } | 28 } |
| 25 | 29 |
| 26 UtilityProcessMojoProxyResolverFactory:: | 30 UtilityProcessMojoProxyResolverFactory:: |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 40 scoped_refptr<content::UtilityProcessHostClient>(), | 44 scoped_refptr<content::UtilityProcessHostClient>(), |
| 41 base::MessageLoopProxy::current().get()); | 45 base::MessageLoopProxy::current().get()); |
| 42 utility_process_host->SetName(l10n_util::GetStringUTF16( | 46 utility_process_host->SetName(l10n_util::GetStringUTF16( |
| 43 IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME)); | 47 IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME)); |
| 44 bool process_started = utility_process_host->StartMojoMode(); | 48 bool process_started = utility_process_host->StartMojoMode(); |
| 45 if (process_started) { | 49 if (process_started) { |
| 46 content::ServiceRegistry* service_registry = | 50 content::ServiceRegistry* service_registry = |
| 47 utility_process_host->GetServiceRegistry(); | 51 utility_process_host->GetServiceRegistry(); |
| 48 service_registry->ConnectToRemoteService(&resolver_factory_); | 52 service_registry->ConnectToRemoteService(&resolver_factory_); |
| 49 resolver_factory_.set_error_handler(this); | 53 resolver_factory_.set_error_handler(this); |
| 54 utility_process_host_ = utility_process_host->AsWeakPtr(); | |
| 50 } else { | 55 } else { |
| 51 LOG(ERROR) << "Unable to connect to utility process"; | 56 LOG(ERROR) << "Unable to connect to utility process"; |
| 52 } | 57 } |
| 53 } | 58 } |
| 54 | 59 |
| 55 void UtilityProcessMojoProxyResolverFactory::Create( | 60 scoped_ptr<base::ScopedClosureRunner> |
| 61 UtilityProcessMojoProxyResolverFactory::Create( | |
| 56 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req, | 62 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req, |
| 57 net::interfaces::HostResolverPtr host_resolver) { | 63 net::interfaces::HostResolverPtr host_resolver) { |
| 58 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 64 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 59 if (!resolver_factory_) | 65 if (!resolver_factory_) |
| 60 CreateProcessAndConnect(); | 66 CreateProcessAndConnect(); |
| 61 | 67 |
| 62 if (!resolver_factory_) { | 68 if (!resolver_factory_) { |
| 63 // If there's still no factory, then utility process creation failed so | 69 // If there's still no factory, then utility process creation failed so |
| 64 // close |req|'s message pipe, which should cause a connection error. | 70 // close |req|'s message pipe, which should cause a connection error. |
| 65 req = nullptr; | 71 req = nullptr; |
| 66 return; | 72 return nullptr; |
| 67 } | 73 } |
| 74 idle_timer_.Stop(); | |
| 75 num_proxy_resolvers_++; | |
| 68 resolver_factory_->CreateResolver(req.Pass(), host_resolver.Pass()); | 76 resolver_factory_->CreateResolver(req.Pass(), host_resolver.Pass()); |
| 77 return make_scoped_ptr(new base::ScopedClosureRunner( | |
| 78 base::Bind(&UtilityProcessMojoProxyResolverFactory::OnResolverDestroyed, | |
| 79 base::Unretained(this)))); | |
|
eroman
2015/04/15 00:46:44
What makes the use of unretained safe here?
Sam McNally
2015/04/15 06:04:22
UtilityProcessMojoProxyResolverFactory is singleto
| |
| 69 } | 80 } |
| 70 | 81 |
| 71 void UtilityProcessMojoProxyResolverFactory::OnConnectionError() { | 82 void UtilityProcessMojoProxyResolverFactory::OnConnectionError() { |
| 72 DVLOG(1) << "Disconnection from utility process detected"; | 83 DVLOG(1) << "Disconnection from utility process detected"; |
| 73 resolver_factory_.reset(); | 84 resolver_factory_.reset(); |
| 74 } | 85 } |
| 86 | |
| 87 void UtilityProcessMojoProxyResolverFactory::OnResolverDestroyed() { | |
| 88 DCHECK_GT(num_proxy_resolvers_, 0u); | |
| 89 if (--num_proxy_resolvers_ == 0) { | |
| 90 // When all proxy resolvers have been destroyed, the proxy resolver utility | |
| 91 // process is no longer needed. However, new proxy resolvers may be created | |
| 92 // shortly after being destroyed (e.g. due to a network change). If the | |
| 93 // utility process is shut down immediately, this would cause unnecessary | |
| 94 // process churn, so wait for an idle timeout before shutting down the | |
| 95 // proxy resolver utility process. | |
| 96 idle_timer_.Start( | |
| 97 FROM_HERE, | |
| 98 base::TimeDelta::FromSeconds(kUtilityProcessIdleTimeoutSeconds), this, | |
| 99 &UtilityProcessMojoProxyResolverFactory::OnIdleTimeout); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void UtilityProcessMojoProxyResolverFactory::OnIdleTimeout() { | |
| 104 delete utility_process_host_.get(); | |
|
eroman
2015/04/15 00:46:44
Why is it written like this, rather than just a ca
Sam McNally
2015/04/15 06:04:22
utility_process_host_ is a WeakPtr.
| |
| 105 utility_process_host_.reset(); | |
| 106 resolver_factory_.reset(); | |
| 107 } | |
| OLD | NEW |