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/chrome_mojo_proxy_resolver_factory.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 | |
| 16 #if !defined(OS_ANDROID) | |
| 14 #include "chrome/grit/generated_resources.h" | 17 #include "chrome/grit/generated_resources.h" |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/utility_process_host.h" | 18 #include "content/public/browser/utility_process_host.h" |
| 17 #include "content/public/browser/utility_process_host_client.h" | 19 #include "content/public/browser/utility_process_host_client.h" |
| 18 #include "services/service_manager/public/cpp/interface_provider.h" | 20 #include "services/service_manager/public/cpp/interface_provider.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #else // defined(OS_ANDROID) | |
| 23 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 24 #include "net/proxy/mojo_proxy_resolver_factory_impl.h" | |
| 25 #endif // !defined(OS_ANDROID) | |
| 20 | 26 |
| 21 namespace { | 27 namespace { |
| 22 const int kUtilityProcessIdleTimeoutSeconds = 5; | 28 const int kUtilityProcessIdleTimeoutSeconds = 5; |
| 23 } | 29 } |
| 24 | 30 |
| 25 // static | 31 // static |
| 26 UtilityProcessMojoProxyResolverFactory* | 32 ChromeMojoProxyResolverFactory* ChromeMojoProxyResolverFactory::GetInstance() { |
| 27 UtilityProcessMojoProxyResolverFactory::GetInstance() { | |
| 28 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 33 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 29 return base::Singleton<UtilityProcessMojoProxyResolverFactory, | 34 return base::Singleton< |
| 30 base::LeakySingletonTraits< | 35 ChromeMojoProxyResolverFactory, |
| 31 UtilityProcessMojoProxyResolverFactory>>::get(); | 36 base::LeakySingletonTraits<ChromeMojoProxyResolverFactory>>::get(); |
| 32 } | 37 } |
| 33 | 38 |
| 34 UtilityProcessMojoProxyResolverFactory:: | 39 ChromeMojoProxyResolverFactory::ChromeMojoProxyResolverFactory() { |
| 35 UtilityProcessMojoProxyResolverFactory() { | |
| 36 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 40 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 37 } | 41 } |
| 38 | 42 |
| 39 UtilityProcessMojoProxyResolverFactory:: | 43 ChromeMojoProxyResolverFactory::~ChromeMojoProxyResolverFactory() { |
| 40 ~UtilityProcessMojoProxyResolverFactory() { | |
| 41 DCHECK(thread_checker_.CalledOnValidThread()); | 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 42 } | 45 } |
| 43 | 46 |
| 44 void UtilityProcessMojoProxyResolverFactory::CreateProcessAndConnect() { | 47 void ChromeMojoProxyResolverFactory::CreateProcessAndConnect() { |
|
eroman
2017/06/08 23:01:42
This function name is not really applicable on the
mmenke
2017/06/09 15:20:40
Good point. Renamed. (Also reordered things so t
| |
| 45 DCHECK(thread_checker_.CalledOnValidThread()); | 48 DCHECK(thread_checker_.CalledOnValidThread()); |
| 46 DCHECK(!resolver_factory_); | 49 DCHECK(!resolver_factory_); |
| 50 | |
| 51 #if defined(OS_ANDROID) | |
| 52 mojo::MakeStrongBinding(base::MakeUnique<net::MojoProxyResolverFactoryImpl>(), | |
| 53 mojo::MakeRequest(&resolver_factory_)); | |
| 54 #else // !defined(OS_ANDROID) | |
| 47 DCHECK(!weak_utility_process_host_); | 55 DCHECK(!weak_utility_process_host_); |
| 56 | |
| 48 DVLOG(1) << "Attempting to create utility process for proxy resolver"; | 57 DVLOG(1) << "Attempting to create utility process for proxy resolver"; |
| 49 content::UtilityProcessHost* utility_process_host = | 58 content::UtilityProcessHost* utility_process_host = |
| 50 content::UtilityProcessHost::Create( | 59 content::UtilityProcessHost::Create( |
| 51 scoped_refptr<content::UtilityProcessHostClient>(), | 60 scoped_refptr<content::UtilityProcessHostClient>(), |
| 52 base::ThreadTaskRunnerHandle::Get()); | 61 base::ThreadTaskRunnerHandle::Get()); |
| 53 utility_process_host->SetName(l10n_util::GetStringUTF16( | 62 utility_process_host->SetName( |
| 54 IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME)); | 63 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME)); |
| 55 bool process_started = utility_process_host->Start(); | 64 bool process_started = utility_process_host->Start(); |
| 56 if (process_started) { | 65 if (process_started) { |
| 57 BindInterface(utility_process_host, &resolver_factory_); | 66 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(); | 67 weak_utility_process_host_ = utility_process_host->AsWeakPtr(); |
| 62 } else { | 68 } else { |
| 63 LOG(ERROR) << "Unable to connect to utility process"; | 69 LOG(ERROR) << "Unable to connect to utility process"; |
| 70 return; | |
| 64 } | 71 } |
| 72 #endif // defined(OS_ANDROID) | |
| 73 | |
| 74 resolver_factory_.set_connection_error_handler(base::Bind( | |
| 75 &ChromeMojoProxyResolverFactory::DestroyFactory, base::Unretained(this))); | |
|
mmenke
2017/06/08 22:32:41
It's not really clear to me if a "connection error
eroman
2017/06/08 23:01:43
From an abstraction perspective seems like should
mmenke
2017/06/09 15:26:50
I don't think it matters from an abstraction persp
| |
| 65 } | 76 } |
| 66 | 77 |
| 67 std::unique_ptr<base::ScopedClosureRunner> | 78 std::unique_ptr<base::ScopedClosureRunner> |
| 68 UtilityProcessMojoProxyResolverFactory::CreateResolver( | 79 ChromeMojoProxyResolverFactory::CreateResolver( |
| 69 const std::string& pac_script, | 80 const std::string& pac_script, |
| 70 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req, | 81 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req, |
| 71 net::interfaces::ProxyResolverFactoryRequestClientPtr client) { | 82 net::interfaces::ProxyResolverFactoryRequestClientPtr client) { |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 if (!resolver_factory_) | 84 if (!resolver_factory_) |
| 74 CreateProcessAndConnect(); | 85 CreateProcessAndConnect(); |
| 75 | 86 |
| 76 if (!resolver_factory_) { | 87 if (!resolver_factory_) { |
| 77 // If there's still no factory, then utility process creation failed so | 88 // If there's still no factory, then factor creation failed so |
|
eroman
2017/06/08 23:01:43
factory
mmenke
2017/06/09 15:20:40
Done. Also shorted the sentence a bit (4 clauses
| |
| 78 // close |req|'s message pipe, which should cause a connection error. | 89 // close |req|'s message pipe, which should cause a connection error. |
| 79 req = nullptr; | 90 req = nullptr; |
| 80 return nullptr; | 91 return nullptr; |
| 81 } | 92 } |
| 82 idle_timer_.Stop(); | 93 idle_timer_.Stop(); |
| 83 num_proxy_resolvers_++; | 94 num_proxy_resolvers_++; |
| 84 resolver_factory_->CreateResolver(pac_script, std::move(req), | 95 resolver_factory_->CreateResolver(pac_script, std::move(req), |
| 85 std::move(client)); | 96 std::move(client)); |
| 86 return base::MakeUnique<base::ScopedClosureRunner>( | 97 return base::MakeUnique<base::ScopedClosureRunner>( |
| 87 base::Bind(&UtilityProcessMojoProxyResolverFactory::OnResolverDestroyed, | 98 base::Bind(&ChromeMojoProxyResolverFactory::OnResolverDestroyed, |
| 88 base::Unretained(this))); | 99 base::Unretained(this))); |
| 89 } | 100 } |
| 90 | 101 |
| 91 void UtilityProcessMojoProxyResolverFactory::OnConnectionError() { | 102 void ChromeMojoProxyResolverFactory::DestroyFactory() { |
| 92 DVLOG(1) << "Disconnection from utility process detected"; | |
| 93 resolver_factory_.reset(); | 103 resolver_factory_.reset(); |
| 104 #if !defined(OS_ANDROID) | |
| 94 delete weak_utility_process_host_.get(); | 105 delete weak_utility_process_host_.get(); |
| 95 weak_utility_process_host_.reset(); | 106 weak_utility_process_host_.reset(); |
| 107 #endif | |
| 96 } | 108 } |
| 97 | 109 |
| 98 void UtilityProcessMojoProxyResolverFactory::OnResolverDestroyed() { | 110 void ChromeMojoProxyResolverFactory::OnResolverDestroyed() { |
| 99 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 100 DCHECK_GT(num_proxy_resolvers_, 0u); | 112 DCHECK_GT(num_proxy_resolvers_, 0u); |
| 101 if (--num_proxy_resolvers_ == 0) { | 113 if (--num_proxy_resolvers_ == 0) { |
| 102 // When all proxy resolvers have been destroyed, the proxy resolver utility | 114 // When all proxy resolvers have been destroyed, the proxy resolver factory |
| 103 // process is no longer needed. However, new proxy resolvers may be created | 115 // 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 | 116 // shortly after being destroyed (e.g. due to a network change). |
| 117 // | |
| 118 // On desktop, where a utliity process is used, if the | |
|
eroman
2017/06/08 23:01:42
utility
mmenke
2017/06/09 15:20:40
Done. Also re-flowed comment (Not sure why git cl
| |
| 105 // utility process is shut down immediately, this would cause unnecessary | 119 // utility process is shut down immediately, this would cause unnecessary |
| 106 // process churn, so wait for an idle timeout before shutting down the | 120 // process churn, so wait for an idle timeout before shutting down the |
| 107 // proxy resolver utility process. | 121 // proxy resolver utility process. |
| 108 idle_timer_.Start( | 122 idle_timer_.Start( |
| 109 FROM_HERE, | 123 FROM_HERE, |
| 110 base::TimeDelta::FromSeconds(kUtilityProcessIdleTimeoutSeconds), this, | 124 base::TimeDelta::FromSeconds(kUtilityProcessIdleTimeoutSeconds), this, |
| 111 &UtilityProcessMojoProxyResolverFactory::OnIdleTimeout); | 125 &ChromeMojoProxyResolverFactory::OnIdleTimeout); |
| 112 } | 126 } |
| 113 } | 127 } |
| 114 | 128 |
| 115 void UtilityProcessMojoProxyResolverFactory::OnIdleTimeout() { | 129 void ChromeMojoProxyResolverFactory::OnIdleTimeout() { |
| 116 DCHECK(thread_checker_.CalledOnValidThread()); | 130 DCHECK(thread_checker_.CalledOnValidThread()); |
| 117 DCHECK_EQ(num_proxy_resolvers_, 0u); | 131 DCHECK_EQ(num_proxy_resolvers_, 0u); |
| 118 delete weak_utility_process_host_.get(); | 132 DestroyFactory(); |
| 119 weak_utility_process_host_.reset(); | |
| 120 resolver_factory_.reset(); | |
| 121 } | 133 } |
| OLD | NEW |