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

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: Rebase 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 // Cancels the job and prevents the callback from being run.
31 void Cancel();
32
33 private:
34 // Overridden from mojo::ErrorHandler:
35 void OnConnectionError() override;
36
37 // Overridden from interfaces::ProxyResolverRequestClient:
38 void ReportResult(
39 int32_t error,
40 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) override;
41
42 ProxyResolverMojo* resolver_;
43 const GURL url_;
44 ProxyInfo* results_;
45 net::CompletionCallback callback_;
46
47 base::ThreadChecker thread_checker_;
48 mojo::Binding<interfaces::ProxyResolverRequestClient> binding_;
49 };
50
51 ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver,
52 const GURL& url,
53 ProxyInfo* results,
54 const net::CompletionCallback& callback)
55 : resolver_(resolver),
56 url_(url),
57 results_(results),
58 callback_(callback),
59 binding_(this) {
60 binding_.set_error_handler(this);
61
62 interfaces::ProxyResolverRequestClientPtr client_ptr;
63 binding_.Bind(mojo::GetProxy(&client_ptr));
64 resolver_->mojo_proxy_resolver_ptr_->GetProxyForUrl(mojo::String::From(url_),
65 client_ptr.Pass());
66 }
67
68 ProxyResolverMojo::Job::~Job() {
69 DCHECK(thread_checker_.CalledOnValidThread());
70 if (!callback_.is_null())
71 callback_.Run(ERR_PAC_SCRIPT_FAILED);
72 }
73
74 void ProxyResolverMojo::Job::Cancel() {
75 DCHECK(thread_checker_.CalledOnValidThread());
76 DCHECK(!callback_.is_null());
77 callback_.Reset();
78 }
79
80 void ProxyResolverMojo::Job::OnConnectionError() {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError";
83 resolver_->RemoveJob(this);
84 }
85
86 void ProxyResolverMojo::Job::ReportResult(
87 int32_t error,
88 mojo::Array<interfaces::ProxyServerPtr> proxy_servers) {
89 DCHECK(thread_checker_.CalledOnValidThread());
90 DVLOG(1) << "ProxyResolverMojo::Job::ReportResult: " << error;
91
92 if (error == OK) {
93 *results_ = proxy_servers.To<ProxyInfo>();
94 DVLOG(1) << "Servers: " << results_->ToPacString();
95 }
96
97 callback_.Run(error);
98 callback_.Reset();
99 resolver_->RemoveJob(this);
100 }
101
102 ProxyResolverMojo::ProxyResolverMojo(
103 MojoProxyResolverFactory* mojo_proxy_resolver_factory,
104 HostResolver* host_resolver)
105 : ProxyResolver(true /* |expects_pac_bytes| */),
106 mojo_proxy_resolver_factory_(mojo_proxy_resolver_factory),
107 host_resolver_(host_resolver) {
108 SetUpServices();
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 mojo_proxy_resolver_ptr_->SetPacScript(
140 mojo::String::From(pac_script->utf16()),
141 set_pac_script_callback_.callback());
142
143 return ERR_IO_PENDING;
144 }
145
146 void ProxyResolverMojo::OnSetPacScriptDone(
147 const scoped_refptr<ProxyResolverScriptData>& pac_script,
148 const net::CompletionCallback& callback,
149 int32_t result) {
150 DCHECK(thread_checker_.CalledOnValidThread());
151 DCHECK(!set_pac_script_callback_.IsCancelled());
152 DVLOG(1) << "ProxyResolverMojo::OnSetPacScriptDone: " << result;
153
154 // If SetPacScript was successful, save the PAC script so that it can be set
155 // again if the proxy resolver process crashes.
156 if (result == OK)
157 pac_script_ = pac_script;
eroman 2015/02/25 00:24:30 I don't think this is the right policy: (1) The
Anand Mistry (off Chromium) 2015/02/25 02:47:16 So, the way I see it, I need to make two further c
Anand Mistry (off Chromium) 2015/02/27 04:05:48 I've gone ahead and made this change here. Now, I
158
159 callback.Run(result);
160 set_pac_script_callback_.Cancel();
161 }
162
163 void ProxyResolverMojo::SetUpServices() {
164 DCHECK(thread_checker_.CalledOnValidThread());
165 // A Mojo service implementation must outlive its binding.
166 mojo_host_resolver_binding_.reset();
167
168 interfaces::HostResolverPtr mojo_host_resolver_ptr;
169 mojo_host_resolver_.reset(new MojoHostResolverImpl(host_resolver_));
170 mojo_host_resolver_binding_.reset(new mojo::Binding<interfaces::HostResolver>(
171 mojo_host_resolver_.get(), mojo::GetProxy(&mojo_host_resolver_ptr)));
172 mojo_proxy_resolver_ptr_.reset();
173 mojo_proxy_resolver_factory_->Create(
174 mojo::GetProxy(&mojo_proxy_resolver_ptr_), mojo_host_resolver_ptr.Pass());
175 mojo_proxy_resolver_ptr_.set_error_handler(this);
176
177 if (pac_script_) {
178 mojo_proxy_resolver_ptr_->SetPacScript(
179 mojo::String::From(pac_script_->utf16()),
180 mojo::Callback<void(int32_t)>());
181 }
182 }
183
184 void ProxyResolverMojo::CancelPendingRequests() {
185 DCHECK(thread_checker_.CalledOnValidThread());
186 if (!set_pac_script_callback_.IsCancelled()) {
187 set_pac_script_callback_.callback().Run(ERR_PAC_SCRIPT_FAILED);
188 set_pac_script_callback_.Cancel();
189 }
190
191 // Deleting a Job will automatically fail it.
192 STLDeleteElements(&pending_jobs_);
193 }
194
195 void ProxyResolverMojo::OnConnectionError() {
196 DCHECK(thread_checker_.CalledOnValidThread());
197 DVLOG(1) << "ProxyResolverMojo::OnConnectionError";
198 CancelPendingRequests();
199
200 // Restart.
201 SetUpServices();
202 }
203
204 void ProxyResolverMojo::RemoveJob(Job* job) {
205 DCHECK(thread_checker_.CalledOnValidThread());
206 size_t num_erased = pending_jobs_.erase(job);
207 DCHECK(num_erased);
208 delete job;
209 }
210
211 int ProxyResolverMojo::GetProxyForURL(const GURL& url,
212 ProxyInfo* results,
213 const net::CompletionCallback& callback,
214 RequestHandle* request,
215 const BoundNetLog& net_log) {
216 DCHECK(thread_checker_.CalledOnValidThread());
217
218 Job* job = new Job(this, url, results, callback);
219 bool inserted = pending_jobs_.insert(job).second;
220 DCHECK(inserted);
221 *request = job;
222
223 return ERR_IO_PENDING;
224 }
225
226 void ProxyResolverMojo::CancelRequest(RequestHandle request) {
227 DCHECK(thread_checker_.CalledOnValidThread());
228 Job* job = static_cast<Job*>(request);
229 DCHECK(job);
230 job->Cancel();
231 RemoveJob(job);
232 }
233
234 LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const {
235 // TODO(amistry): Implement real LoadState.
236 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
237 }
238
239 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698