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

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 rules. Created 5 years, 10 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
(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 "net/base/net_errors.h"
12 #include "net/dns/mojo_host_resolver_impl.h"
13 #include "net/proxy/mojo_proxy_resolver_factory.h"
14 #include "net/proxy/mojo_type_converters.h"
15 #include "net/proxy/proxy_info.h"
16 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
17 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
18
19 namespace net {
20
21 class ProxyResolverMojo::Job : public interfaces::ProxyResolverRequestClient,
22 public mojo::ErrorHandler {
23 public:
24 Job(ProxyResolverMojo* resolver,
25 const GURL& url,
26 ProxyInfo* results,
27 const net::CompletionCallback& callback);
28 ~Job() override;
29
30 // Start the job and return an error code.
31 int Start();
Sam McNally 2015/02/16 05:52:38 This Job doesn't need a separate Start().
Anand Mistry (off Chromium) 2015/02/17 05:49:21 Done.
32
33 // Cancels the job and prevents the callback from being run.
34 void Cancel();
35
36 private:
37 // Overridden from mojo::ErrorHandler:
38 void OnConnectionError() override;
39
40 // Overridden from interfaces::ProxyResolverRequestClient:
41 void ReportResult(
42 int32_t error,
43 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override;
44
45 ProxyResolverMojo* resolver_;
46 const GURL url_;
47 ProxyInfo* results_;
48 net::CompletionCallback callback_;
49
50 base::ThreadChecker thread_checker_;
51 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_;
52 };
53
54 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver,
55 const GURL& url,
56 ProxyInfo* results,
57 const net::CompletionCallback& callback)
58 : resolver_(resolver),
59 url_(url),
60 results_(results),
61 callback_(callback),
62 binding_(this) {
63 binding_.set_error_handler(this);
64 }
65
66 ProxyResolverMojo::Job::~Job() {
67 DCHECK(thread_checker_.CalledOnValidThread());
68 if (!callback_.is_null())
69 callback_.Run(ERR_PAC_SCRIPT_FAILED);
70 }
71
72 int ProxyResolverMojo::Job::Start() {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 // Should already have ensured the Mojo resolved is connected.
Sam McNally 2015/02/16 05:52:38 I think this is missing a subject.
Anand Mistry (off Chromium) 2015/02/17 05:49:21 Removed the DCHECK since I don't think it's strict
75 DCHECK(resolver_->proxy_resolver_ptr_);
76
77 interfaces::ProxyResolverRequestClientPtr client_ptr;
78 binding_.Bind(mojo::GetProxy(&client_ptr));
79 resolver_->proxy_resolver_ptr_->GetProxyForUrl(mojo::String::From(url_),
80 client_ptr.Pass());
81
82 return ERR_IO_PENDING;
83 }
84
85 void ProxyResolverMojo::Job::Cancel() {
86 DCHECK(thread_checker_.CalledOnValidThread());
87 DCHECK(!callback_.is_null());
88 callback_.Reset();
89 }
90
91 void ProxyResolverMojo::Job::OnConnectionError() {
92 DCHECK(thread_checker_.CalledOnValidThread());
93 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError";
94 resolver_->RemoveJob(this);
95 }
96
97 void ProxyResolverMojo::Job::ReportResult(
98 int32_t error,
99 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) {
100 DCHECK(thread_checker_.CalledOnValidThread());
101 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error;
102
103 if (error == OK) {
104 *results_ = proxy_servers.To<ProxyInfo>();
105 DVLOG(1) << "Servers: " << results_->ToPacString();
106 }
107
108 callback_.Run(error);
109 callback_.Reset();
110 resolver_->RemoveJob(this);
111 }
112
113 ProxyResolverMojo::ProxyResolverMojo(
114 MojoProxyResolverFactory* proxy_resolver_factory,
115 HostResolver* host_resolver)
116 : ProxyResolver(true /* |expects_pac_bytes| */),
117 proxy_resolver_factory_(proxy_resolver_factory),
118 host_resolver_(host_resolver) {
119 SetupServices();
120 }
121
122 ProxyResolverMojo::~ProxyResolverMojo() {
123 DCHECK(thread_checker_.CalledOnValidThread());
124 // All pending requests should have been cancelled.
125 DCHECK(pending_jobs_.empty());
126 }
127
128 void ProxyResolverMojo::CancelSetPacScript() {
129 DCHECK(thread_checker_.CalledOnValidThread());
130 if (!set_pac_script_callback_.is_null())
131 set_pac_script_callback_.Reset();
132 }
133
134 int ProxyResolverMojo::SetPacScript(
135 const scoped_refptr<ProxyResolverScriptData>& pac_script,
136 const net::CompletionCallback& callback) {
137 DCHECK(thread_checker_.CalledOnValidThread());
138 DCHECK(set_pac_script_callback_.is_null());
139 DCHECK(!callback.is_null());
140 if (pac_script->type() != ProxyResolverScriptData::TYPE_SCRIPT_CONTENTS ||
141 pac_script->utf16().empty())
Sam McNally 2015/02/16 05:52:38 {
Anand Mistry (off Chromium) 2015/02/17 05:49:21 Done.
142 return ERR_PAC_SCRIPT_FAILED;
143
144 DVLOG(1) << "ProxyResolverMojo::SetPacScript: " << pac_script->utf16();
145 pac_script_ = pac_script;
146 set_pac_script_callback_ = callback;
147
148 proxy_resolver_ptr_->SetPacScript(
149 mojo::String::From(pac_script_->utf16()),
150 base::Bind(&ProxyResolverMojo::OnSetPacScriptDone,
151 base::Unretained(this)));
152
153 return ERR_IO_PENDING;
154 }
155
156 void ProxyResolverMojo::OnSetPacScriptDone(int32_t result) {
157 DCHECK(thread_checker_.CalledOnValidThread());
158 DVLOG(1) << "ProxyResolverMojo::OnSetPacScriptDone: " << result;
159 if (!set_pac_script_callback_.is_null())
160 set_pac_script_callback_.Run(result);
161
162 set_pac_script_callback_.Reset();
163 }
164
165 void ProxyResolverMojo::SetupServices() {
166 DCHECK(thread_checker_.CalledOnValidThread());
167 host_resolver_binding_.reset(
168 new mojo::StrongBinding<interfaces::HostResolver>(
169 new MojoHostResolverImpl(host_resolver_)));
Sam McNally 2015/02/16 05:52:38 This will leak if the host resolver pipe isn't clo
Anand Mistry (off Chromium) 2015/02/17 05:49:21 Done.
170 interfaces::HostResolverPtr host_resolver_ptr;
171 host_resolver_binding_->Bind(&host_resolver_ptr);
Sam McNally 2015/02/16 05:52:38 Binding can take this as a constructor arg.
Anand Mistry (off Chromium) 2015/02/17 05:49:21 Done.
172 proxy_resolver_ptr_.reset();
173 proxy_resolver_factory_->Create(mojo::GetProxy(&proxy_resolver_ptr_),
174 host_resolver_ptr.Pass());
175 proxy_resolver_ptr_.set_error_handler(this);
176
177 if (pac_script_) {
178 proxy_resolver_ptr_->SetPacScript(mojo::String::From(pac_script_->utf16()),
Sam McNally 2015/02/16 05:52:38 This and CancelSetPacScript can result in multiple
Anand Mistry (off Chromium) 2015/02/17 05:49:21 Fixed this side to deal with multiple outstanding
179 mojo::Callback<void(int32_t)>());
180 }
181 }
182
183 void ProxyResolverMojo::CancelPendingRequests() {
184 DCHECK(thread_checker_.CalledOnValidThread());
185 if (!set_pac_script_callback_.is_null())
186 set_pac_script_callback_.Run(ERR_PAC_SCRIPT_FAILED);
187 set_pac_script_callback_.Reset();
188
189 // Deleting a Job will automatically fail it.
190 STLDeleteElements(&pending_jobs_);
191 }
192
193 void ProxyResolverMojo::OnConnectionError() {
194 DCHECK(thread_checker_.CalledOnValidThread());
195 DVLOG(1) << "ProxyResolverMojo::OnConnectionError";
196 CancelPendingRequests();
197
198 // Restart.
199 SetupServices();
200 }
201
202 void ProxyResolverMojo::RemoveJob(Job* job) {
203 DCHECK(thread_checker_.CalledOnValidThread());
204 size_t num_erased = pending_jobs_.erase(job);
205 DCHECK(num_erased);
206 delete job;
207 }
208
209 int ProxyResolverMojo::GetProxyForURL(const GURL& url,
210 ProxyInfo* results,
211 const net::CompletionCallback& callback,
212 RequestHandle* request,
213 const BoundNetLog& net_log) {
214 DCHECK(thread_checker_.CalledOnValidThread());
215
216 Job* job = new Job(this, url, results, callback);
217 bool inserted = pending_jobs_.insert(job).second;
218 DCHECK(inserted);
219 *request = job;
220
221 return job->Start();
Sam McNally 2015/02/16 05:52:38 Why not return ERR_IO_PENDING directly? To be str
Anand Mistry (off Chromium) 2015/02/17 05:49:21 Done.
222 }
223
224 void ProxyResolverMojo::CancelRequest(RequestHandle request) {
225 DCHECK(thread_checker_.CalledOnValidThread());
226 Job* job = static_cast<Job*>(request);
227 DCHECK(job);
228 job->Cancel();
229 RemoveJob(job);
230 }
231
232 LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const {
233 // TODO(amistry): Implement real LoadState.
234 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
235 }
236
237 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698