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

Side by Side Diff: net/proxy/proxy_resolver_mojo.cc

Issue 917863005: Implementation of ProxyResolver that uses a Mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sam-v8-pac-utility-proxy
Patch Set: Fix build. Created 5 years, 9 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
« no previous file with comments | « net/proxy/proxy_resolver_mojo.h ('k') | net/proxy/proxy_resolver_mojo_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/proxy/proxy_resolver_mojo.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "mojo/common/common_type_converters.h"
11 #include "mojo/common/url_type_converters.h"
12 #include "net/base/net_errors.h"
13 #include "net/dns/mojo_host_resolver_impl.h"
14 #include "net/proxy/mojo_proxy_resolver_factory.h"
15 #include "net/proxy/mojo_proxy_type_converters.h"
16 #include "net/proxy/proxy_info.h"
17 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
19
20 namespace net {
21
22 class ProxyResolverMojo::Job : public interfaces::ProxyResolverRequestClient,
23 public mojo::ErrorHandler {
24 public:
25 Job(ProxyResolverMojo* resolver,
26 const GURL& url,
27 ProxyInfo* results,
28 const net::CompletionCallback& callback);
29 ~Job() override;
30
31 // Cancels the job and prevents the callback from being run.
32 void Cancel();
33
34 private:
35 // Overridden from mojo::ErrorHandler:
36 void OnConnectionError() override;
37
38 // Overridden from interfaces::ProxyResolverRequestClient:
39 void ReportResult(
40 int32_t error,
41 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override;
42
43 ProxyResolverMojo* resolver_;
44 const GURL url_;
45 ProxyInfo* results_;
46 net::CompletionCallback callback_;
47
48 base::ThreadChecker thread_checker_;
49 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_;
50 };
51
52 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver,
53 const GURL& url,
54 ProxyInfo* results,
55 const net::CompletionCallback& callback)
56 : resolver_(resolver),
57 url_(url),
58 results_(results),
59 callback_(callback),
60 binding_(this) {
61 binding_.set_error_handler(this);
62
63 interfaces::ProxyResolverRequestClientPtr client_ptr;
64 binding_.Bind(mojo::GetProxy(&client_ptr));
65 resolver_->mojo_proxy_resolver_ptr_->GetProxyForUrl(mojo::String::From(url_),
66 client_ptr.Pass());
67 }
68
69 ProxyResolverMojo::Job::~Job() {
70 DCHECK(thread_checker_.CalledOnValidThread());
71 if (!callback_.is_null())
72 callback_.Run(ERR_PAC_SCRIPT_TERMINATED);
73 }
74
75 void ProxyResolverMojo::Job::Cancel() {
76 DCHECK(thread_checker_.CalledOnValidThread());
77 DCHECK(!callback_.is_null());
78 callback_.Reset();
79 }
80
81 void ProxyResolverMojo::Job::OnConnectionError() {
82 DCHECK(thread_checker_.CalledOnValidThread());
83 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError";
84 resolver_->RemoveJob(this);
85 }
86
87 void ProxyResolverMojo::Job::ReportResult(
88 int32_t error,
89 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) {
90 DCHECK(thread_checker_.CalledOnValidThread());
91 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error;
92
93 if (error == OK) {
94 *results_ = proxy_servers.To<ProxyInfo>();
95 DVLOG(1) << "Servers: " << results_->ToPacString();
96 }
97
98 callback_.Run(error);
99 callback_.Reset();
100 resolver_->RemoveJob(this);
101 }
102
103 ProxyResolverMojo::ProxyResolverMojo(
104 MojoProxyResolverFactory* mojo_proxy_resolver_factory,
105 HostResolver* host_resolver)
106 : ProxyResolver(true /* |expects_pac_bytes| */),
107 mojo_proxy_resolver_factory_(mojo_proxy_resolver_factory),
108 host_resolver_(host_resolver) {
109 }
110
111 ProxyResolverMojo::~ProxyResolverMojo() {
112 DCHECK(thread_checker_.CalledOnValidThread());
113 // All pending requests should have been cancelled.
114 DCHECK(pending_jobs_.empty());
115 DCHECK(set_pac_script_callback_.IsCancelled());
116 }
117
118 void ProxyResolverMojo::CancelSetPacScript() {
119 DCHECK(thread_checker_.CalledOnValidThread());
120 set_pac_script_callback_.Cancel();
121 }
122
123 int ProxyResolverMojo::SetPacScript(
124 const scoped_refptr<ProxyResolverScriptData>& pac_script,
125 const net::CompletionCallback& callback) {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 DCHECK(set_pac_script_callback_.IsCancelled());
128 DCHECK(!callback.is_null());
129 if (pac_script->type() != ProxyResolverScriptData::TYPE_SCRIPT_CONTENTS ||
130 pac_script->utf16().empty()) {
131 return ERR_PAC_SCRIPT_FAILED;
132 }
133
134 DVLOG(1) << "ProxyResolverMojo::SetPacScript: " << pac_script->utf16();
135 set_pac_script_callback_.Reset(
136 base::Bind(&ProxyResolverMojo::OnSetPacScriptDone, base::Unretained(this),
137 pac_script, callback));
138
139 if (!mojo_proxy_resolver_ptr_)
140 SetUpServices();
141
142 mojo_proxy_resolver_ptr_->SetPacScript(
143 mojo::String::From(pac_script->utf16()),
144 set_pac_script_callback_.callback());
145
146 return ERR_IO_PENDING;
147 }
148
149 void ProxyResolverMojo::OnSetPacScriptDone(
150 const scoped_refptr<ProxyResolverScriptData>& pac_script,
151 const net::CompletionCallback& callback,
152 int32_t result) {
153 DCHECK(thread_checker_.CalledOnValidThread());
154 DCHECK(!set_pac_script_callback_.IsCancelled());
155 DVLOG(1) << "ProxyResolverMojo::OnSetPacScriptDone: " << result;
156
157 callback.Run(result);
158 set_pac_script_callback_.Cancel();
159 }
160
161 void ProxyResolverMojo::SetUpServices() {
162 DCHECK(thread_checker_.CalledOnValidThread());
163 // A Mojo service implementation must outlive its binding.
164 mojo_host_resolver_binding_.reset();
165
166 interfaces::HostResolverPtr mojo_host_resolver_ptr;
167 mojo_host_resolver_.reset(new MojoHostResolverImpl(host_resolver_));
168 mojo_host_resolver_binding_.reset(new mojo::Binding<interfaces::HostResolver>(
169 mojo_host_resolver_.get(), mojo::GetProxy(&mojo_host_resolver_ptr)));
170 mojo_proxy_resolver_ptr_.reset();
171 mojo_proxy_resolver_factory_->Create(
172 mojo::GetProxy(&mojo_proxy_resolver_ptr_), mojo_host_resolver_ptr.Pass());
173 mojo_proxy_resolver_ptr_.set_error_handler(this);
174 }
175
176 void ProxyResolverMojo::AbortPendingRequests() {
177 DCHECK(thread_checker_.CalledOnValidThread());
178 if (!set_pac_script_callback_.IsCancelled()) {
179 set_pac_script_callback_.callback().Run(ERR_PAC_SCRIPT_TERMINATED);
180 set_pac_script_callback_.Cancel();
181 }
182
183 // Need to use this loop because deleting a Job will cause its callback to be
184 // run with a failure error code, which may cause other Jobs to be deleted.
185 while (!pending_jobs_.empty()) {
186 auto it = pending_jobs_.begin();
187 Job* job = *it;
188 pending_jobs_.erase(it);
189
190 // Deleting the job will cause its completion callback to be run with an
191 // ERR_PAC_SCRIPT_TERMINATED error.
192 delete job;
193 }
194 }
195
196 void ProxyResolverMojo::OnConnectionError() {
197 DCHECK(thread_checker_.CalledOnValidThread());
198 DVLOG(1) << "ProxyResolverMojo::OnConnectionError";
199
200 // Disconnect from the Mojo proxy resolver service. An attempt to reconnect
201 // will happen on the next |SetPacScript()| request.
202 mojo_proxy_resolver_ptr_.reset();
203
204 // Aborting requests will invoke their callbacks, which may call
205 // |SetPacScript()| and re-create the connection. So disconnect from the Mojo
206 // service (above) before aborting the pending requests.
207 AbortPendingRequests();
208 }
209
210 void ProxyResolverMojo::RemoveJob(Job* job) {
211 DCHECK(thread_checker_.CalledOnValidThread());
212 size_t num_erased = pending_jobs_.erase(job);
213 DCHECK(num_erased);
214 delete job;
215 }
216
217 int ProxyResolverMojo::GetProxyForURL(const GURL& url,
218 ProxyInfo* results,
219 const net::CompletionCallback& callback,
220 RequestHandle* request,
221 const BoundNetLog& net_log) {
222 DCHECK(thread_checker_.CalledOnValidThread());
223
224 // If the Mojo service is not connected, fail. The Mojo service is connected
225 // when the script is set, which must be done after construction and after a
226 // previous request returns ERR_PAC_SCRIPT_TERMINATED due to the Mojo proxy
227 // resolver process crashing.
228 if (!mojo_proxy_resolver_ptr_) {
229 DVLOG(1) << "ProxyResolverMojo::GetProxyForURL: Mojo not connected";
230 return ERR_PAC_SCRIPT_TERMINATED;
231 }
232
233 Job* job = new Job(this, url, results, callback);
234 bool inserted = pending_jobs_.insert(job).second;
235 DCHECK(inserted);
236 *request = job;
237
238 return ERR_IO_PENDING;
239 }
240
241 void ProxyResolverMojo::CancelRequest(RequestHandle request) {
242 DCHECK(thread_checker_.CalledOnValidThread());
243 Job* job = static_cast<Job*>(request);
244 DCHECK(job);
245 job->Cancel();
246 RemoveJob(job);
247 }
248
249 LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const {
250 // TODO(amistry): Implement real LoadState.
251 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
252 }
253
254 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/proxy_resolver_mojo.h ('k') | net/proxy/proxy_resolver_mojo_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698