Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(985)

Side by Side Diff: chrome/browser/net/chrome_mojo_proxy_resolver_factory.cc

Issue 2930743002: Use a MojoProxyResolverFactory on Android. (Closed)
Patch Set: Oops Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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() {
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> 47 std::unique_ptr<base::ScopedClosureRunner>
68 UtilityProcessMojoProxyResolverFactory::CreateResolver( 48 ChromeMojoProxyResolverFactory::CreateResolver(
69 const std::string& pac_script, 49 const std::string& pac_script,
70 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req, 50 mojo::InterfaceRequest<net::interfaces::ProxyResolver> req,
71 net::interfaces::ProxyResolverFactoryRequestClientPtr client) { 51 net::interfaces::ProxyResolverFactoryRequestClientPtr client) {
72 DCHECK(thread_checker_.CalledOnValidThread()); 52 DCHECK(thread_checker_.CalledOnValidThread());
73 if (!resolver_factory_) 53 if (!resolver_factory_)
74 CreateProcessAndConnect(); 54 CreateFactory();
75 55
76 if (!resolver_factory_) { 56 if (!resolver_factory_) {
77 // If there's still no factory, then utility process creation failed so 57 // If factory creation failed, close |req|'s message pipe, which should
78 // close |req|'s message pipe, which should cause a connection error. 58 // cause a connection error.
79 req = nullptr; 59 req = nullptr;
80 return nullptr; 60 return nullptr;
81 } 61 }
82 idle_timer_.Stop(); 62 idle_timer_.Stop();
83 num_proxy_resolvers_++; 63 num_proxy_resolvers_++;
84 resolver_factory_->CreateResolver(pac_script, std::move(req), 64 resolver_factory_->CreateResolver(pac_script, std::move(req),
85 std::move(client)); 65 std::move(client));
86 return base::MakeUnique<base::ScopedClosureRunner>( 66 return base::MakeUnique<base::ScopedClosureRunner>(
87 base::Bind(&UtilityProcessMojoProxyResolverFactory::OnResolverDestroyed, 67 base::Bind(&ChromeMojoProxyResolverFactory::OnResolverDestroyed,
88 base::Unretained(this))); 68 base::Unretained(this)));
89 } 69 }
90 70
91 void UtilityProcessMojoProxyResolverFactory::OnConnectionError() { 71 void ChromeMojoProxyResolverFactory::CreateFactory() {
92 DVLOG(1) << "Disconnection from utility process detected"; 72 DCHECK(thread_checker_.CalledOnValidThread());
73 DCHECK(!resolver_factory_);
74
75 #if defined(OS_ANDROID)
76 mojo::MakeStrongBinding(base::MakeUnique<net::MojoProxyResolverFactoryImpl>(),
77 mojo::MakeRequest(&resolver_factory_));
78 #else // !defined(OS_ANDROID)
79 DCHECK(!weak_utility_process_host_);
80
81 DVLOG(1) << "Attempting to create utility process for proxy resolver";
82 content::UtilityProcessHost* utility_process_host =
83 content::UtilityProcessHost::Create(
84 scoped_refptr<content::UtilityProcessHostClient>(),
85 base::ThreadTaskRunnerHandle::Get());
86 utility_process_host->SetName(
87 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_PROXY_RESOLVER_NAME));
88 bool process_started = utility_process_host->Start();
89 if (process_started) {
90 BindInterface(utility_process_host, &resolver_factory_);
91 weak_utility_process_host_ = utility_process_host->AsWeakPtr();
92 } else {
93 LOG(ERROR) << "Unable to connect to utility process";
94 return;
95 }
96 #endif // defined(OS_ANDROID)
97
98 resolver_factory_.set_connection_error_handler(base::Bind(
99 &ChromeMojoProxyResolverFactory::DestroyFactory, base::Unretained(this)));
100 }
101
102 void ChromeMojoProxyResolverFactory::DestroyFactory() {
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).
105 // utility process is shut down immediately, this would cause unnecessary 117 //
106 // process churn, so wait for an idle timeout before shutting down the 118 // On desktop, where a utility process is used, if the utility process is
107 // proxy resolver utility process. 119 // shut down immediately, this would cause unnecessary process churn, so
120 // wait for an idle timeout before shutting down the proxy resolver utility
121 // 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 }
OLDNEW
« no previous file with comments | « chrome/browser/net/chrome_mojo_proxy_resolver_factory.h ('k') | chrome/browser/net/proxy_service_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698