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 "net/proxy/mojo_proxy_resolver_factory_impl.h" | 5 #include "net/proxy/mojo_proxy_resolver_factory_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
14 #include "net/proxy/mojo_proxy_resolver_impl.h" | 15 #include "net/proxy/mojo_proxy_resolver_impl.h" |
15 #include "net/proxy/mojo_proxy_resolver_v8_tracing_bindings.h" | 16 #include "net/proxy/mojo_proxy_resolver_v8_tracing_bindings.h" |
16 #include "net/proxy/proxy_resolver_factory.h" | 17 #include "net/proxy/proxy_resolver_factory.h" |
17 #include "net/proxy/proxy_resolver_v8_tracing.h" | 18 #include "net/proxy/proxy_resolver_v8_tracing.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 namespace { | |
21 | |
22 // A class to manage the lifetime of a MojoProxyResolverImpl. An instance will | |
23 // remain while the message pipe for the mojo connection remains open. | |
24 class MojoProxyResolverHolder { | |
25 public: | |
26 MojoProxyResolverHolder( | |
27 std::unique_ptr<ProxyResolverV8Tracing> proxy_resolver_impl, | |
28 mojo::InterfaceRequest<interfaces::ProxyResolver> request); | |
29 | |
30 private: | |
31 // Mojo error handler. | |
32 void OnConnectionError(); | |
33 | |
34 MojoProxyResolverImpl mojo_proxy_resolver_; | |
35 mojo::Binding<interfaces::ProxyResolver> binding_; | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(MojoProxyResolverHolder); | |
38 }; | |
39 | |
40 MojoProxyResolverHolder::MojoProxyResolverHolder( | |
41 std::unique_ptr<ProxyResolverV8Tracing> proxy_resolver_impl, | |
42 mojo::InterfaceRequest<interfaces::ProxyResolver> request) | |
43 : mojo_proxy_resolver_(std::move(proxy_resolver_impl)), | |
44 binding_(&mojo_proxy_resolver_, std::move(request)) { | |
45 binding_.set_connection_error_handler(base::Bind( | |
46 &MojoProxyResolverHolder::OnConnectionError, base::Unretained(this))); | |
47 } | |
48 | |
49 void MojoProxyResolverHolder::OnConnectionError() { | |
50 delete this; | |
51 } | |
52 | |
53 } // namespace | |
54 | 21 |
55 class MojoProxyResolverFactoryImpl::Job { | 22 class MojoProxyResolverFactoryImpl::Job { |
56 public: | 23 public: |
57 Job(MojoProxyResolverFactoryImpl* parent, | 24 Job(MojoProxyResolverFactoryImpl* parent, |
58 const scoped_refptr<ProxyResolverScriptData>& pac_script, | 25 const scoped_refptr<ProxyResolverScriptData>& pac_script, |
59 ProxyResolverV8TracingFactory* proxy_resolver_factory, | 26 ProxyResolverV8TracingFactory* proxy_resolver_factory, |
60 mojo::InterfaceRequest<interfaces::ProxyResolver> request, | 27 mojo::InterfaceRequest<interfaces::ProxyResolver> request, |
61 interfaces::ProxyResolverFactoryRequestClientPtr client); | 28 interfaces::ProxyResolverFactoryRequestClientPtr client); |
62 ~Job(); | 29 ~Job(); |
63 | 30 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 69 |
103 MojoProxyResolverFactoryImpl::Job::~Job() = default; | 70 MojoProxyResolverFactoryImpl::Job::~Job() = default; |
104 | 71 |
105 void MojoProxyResolverFactoryImpl::Job::OnConnectionError() { | 72 void MojoProxyResolverFactoryImpl::Job::OnConnectionError() { |
106 client_ptr_->ReportResult(ERR_PAC_SCRIPT_TERMINATED); | 73 client_ptr_->ReportResult(ERR_PAC_SCRIPT_TERMINATED); |
107 parent_->RemoveJob(this); | 74 parent_->RemoveJob(this); |
108 } | 75 } |
109 | 76 |
110 void MojoProxyResolverFactoryImpl::Job::OnProxyResolverCreated(int error) { | 77 void MojoProxyResolverFactoryImpl::Job::OnProxyResolverCreated(int error) { |
111 if (error == OK) { | 78 if (error == OK) { |
112 // The MojoProxyResolverHolder will delete itself if |proxy_request_| | 79 mojo::MakeStrongBinding(base::MakeUnique<MojoProxyResolverImpl>( |
113 // encounters a connection error. | 80 std::move(proxy_resolver_impl_)), |
114 new MojoProxyResolverHolder(std::move(proxy_resolver_impl_), | 81 std::move(proxy_request_)); |
115 std::move(proxy_request_)); | |
116 } | 82 } |
117 client_ptr_->ReportResult(error); | 83 client_ptr_->ReportResult(error); |
118 parent_->RemoveJob(this); | 84 parent_->RemoveJob(this); |
119 } | 85 } |
120 | 86 |
121 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl( | 87 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl( |
122 std::unique_ptr<ProxyResolverV8TracingFactory> proxy_resolver_factory, | 88 std::unique_ptr<ProxyResolverV8TracingFactory> proxy_resolver_factory) |
123 mojo::InterfaceRequest<interfaces::ProxyResolverFactory> request) | 89 : proxy_resolver_impl_factory_(std::move(proxy_resolver_factory)) {} |
124 : proxy_resolver_impl_factory_(std::move(proxy_resolver_factory)), | |
125 binding_(this, std::move(request)) {} | |
126 | 90 |
127 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl( | 91 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl() |
128 mojo::InterfaceRequest<interfaces::ProxyResolverFactory> request) | 92 : MojoProxyResolverFactoryImpl(ProxyResolverV8TracingFactory::Create()) {} |
129 : MojoProxyResolverFactoryImpl(ProxyResolverV8TracingFactory::Create(), | |
130 std::move(request)) {} | |
131 | 93 |
132 MojoProxyResolverFactoryImpl::~MojoProxyResolverFactoryImpl() { | 94 MojoProxyResolverFactoryImpl::~MojoProxyResolverFactoryImpl() { |
133 base::STLDeleteElements(&jobs_); | 95 base::STLDeleteElements(&jobs_); |
134 } | 96 } |
135 | 97 |
136 void MojoProxyResolverFactoryImpl::CreateResolver( | 98 void MojoProxyResolverFactoryImpl::CreateResolver( |
137 const mojo::String& pac_script, | 99 const mojo::String& pac_script, |
138 mojo::InterfaceRequest<interfaces::ProxyResolver> request, | 100 mojo::InterfaceRequest<interfaces::ProxyResolver> request, |
139 interfaces::ProxyResolverFactoryRequestClientPtr client) { | 101 interfaces::ProxyResolverFactoryRequestClientPtr client) { |
140 // The Job will call RemoveJob on |this| when either the create request | 102 // The Job will call RemoveJob on |this| when either the create request |
141 // finishes or |request| or |client| encounters a connection error. | 103 // finishes or |request| or |client| encounters a connection error. |
142 jobs_.insert(new Job( | 104 jobs_.insert(new Job( |
143 this, ProxyResolverScriptData::FromUTF8(pac_script.To<std::string>()), | 105 this, ProxyResolverScriptData::FromUTF8(pac_script.To<std::string>()), |
144 proxy_resolver_impl_factory_.get(), std::move(request), | 106 proxy_resolver_impl_factory_.get(), std::move(request), |
145 std::move(client))); | 107 std::move(client))); |
146 } | 108 } |
147 | 109 |
148 void MojoProxyResolverFactoryImpl::RemoveJob(Job* job) { | 110 void MojoProxyResolverFactoryImpl::RemoveJob(Job* job) { |
149 size_t erased = jobs_.erase(job); | 111 size_t erased = jobs_.erase(job); |
150 DCHECK_EQ(1u, erased); | 112 DCHECK_EQ(1u, erased); |
151 delete job; | 113 delete job; |
152 } | 114 } |
153 | 115 |
154 } // namespace net | 116 } // namespace net |
OLD | NEW |