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

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

Issue 896203003: Implement utility-side proxy resolver Mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proxy-diffbase
Patch Set: 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/mojo_proxy_resolver_impl.h"
6
7 #include "base/stl_util.h"
8 #include "mojo/common/common_type_converters.h"
9 #include "net/base/net_errors.h"
10 #include "net/base/net_log.h"
11 #include "net/proxy/mojo_type_converters.h"
12 #include "net/proxy/proxy_info.h"
13 #include "net/proxy/proxy_resolver.h"
14 #include "net/proxy/proxy_resolver_script_data.h"
15
16 namespace net {
17
18 class MojoProxyResolverImpl::Job : public mojo::ErrorHandler {
19 public:
20 Job(interfaces::ProxyResolverRequestClientPtr client,
21 MojoProxyResolverImpl* resolver,
22 const GURL& url);
23 ~Job() override;
24
25 void Start();
26
27 private:
28 // mojo::ErrorHandler override.
29 void OnConnectionError() override;
eroman 2015/02/12 19:00:42 Is this also how requests are cancelled from the b
Sam McNally 2015/02/13 00:03:19 Yes. Added a comment to make it more obvious.
30
31 void GetProxyDone(int32_t error);
eroman 2015/02/12 19:00:41 Same comment as on Anand's CL: In the network st
Sam McNally 2015/02/13 00:03:19 Done.
32
33 MojoProxyResolverImpl* resolver_;
34
35 interfaces::ProxyResolverRequestClientPtr client_;
36 ProxyInfo result_;
37 GURL url_;
38 net::ProxyResolver::RequestHandle request_handle_;
39
40 DISALLOW_COPY_AND_ASSIGN(Job);
41 };
42
43 MojoProxyResolverImpl::MojoProxyResolverImpl(
44 scoped_ptr<net::ProxyResolver> resolver)
45 : resolver_(resolver.Pass()) {
46 DCHECK(resolver_->expects_pac_bytes());
47 }
48
49 MojoProxyResolverImpl::~MojoProxyResolverImpl() {
50 if (!set_pac_script_callback_.is_null()) {
51 resolver_->CancelSetPacScript();
52 set_pac_script_callback_.Run(ERR_ABORTED);
eroman 2015/02/12 19:00:42 Why is the callback run in this case? Typically i
Sam McNally 2015/02/13 00:03:19 It was to avoid leaving the client with a hanging
53 }
54 STLDeleteElements(&resolve_jobs_);
55 }
56
57 void MojoProxyResolverImpl::SetPacScript(
58 const mojo::String& data,
59 const mojo::Callback<void(int32_t)>& callback) {
60 DVLOG(1) << "SetPacScript(" << data << ")";
61 DCHECK(set_pac_script_callback_.is_null());
62 set_pac_script_callback_ = callback;
63 int result = resolver_->SetPacScript(
64 ProxyResolverScriptData::FromUTF8(data),
65 base::Bind(&MojoProxyResolverImpl::SetPacScriptDone,
66 base::Unretained(this)));
67 if (result != ERR_IO_PENDING)
68 SetPacScriptDone(result);
69 }
70
71 void MojoProxyResolverImpl::GetProxyForUrl(
72 const mojo::String& url,
73 interfaces::ProxyResolverRequestClientPtr client) {
74 DVLOG(1) << "GetProxyForUrl(" << url << ")";
75 Job* job = new Job(client.Pass(), this, url.To<GURL>());
76 bool inserted = resolve_jobs_.insert(job).second;
77 DCHECK(inserted);
78 job->Start();
79 }
80
81 void MojoProxyResolverImpl::DeleteJob(Job* job) {
82 size_t num_erased = resolve_jobs_.erase(job);
83 DCHECK(num_erased);
84 delete job;
85 }
86
87 void MojoProxyResolverImpl::SetPacScriptDone(int result) {
88 DVLOG(1) << "SetPacScript finished with error " << result;
89 DCHECK(!set_pac_script_callback_.is_null());
90 set_pac_script_callback_.Run(result);
91 set_pac_script_callback_.reset();
92 }
93
94 MojoProxyResolverImpl::Job::Job(
95 interfaces::ProxyResolverRequestClientPtr client,
96 MojoProxyResolverImpl* resolver,
97 const GURL& url)
98 : resolver_(resolver),
99 client_(client.Pass()),
100 url_(url),
101 request_handle_(nullptr) {
102 }
103
104 MojoProxyResolverImpl::Job::~Job() {
105 if (request_handle_)
106 resolver_->resolver_->CancelRequest(request_handle_);
107 }
108
109 void MojoProxyResolverImpl::Job::Start() {
110 int result = resolver_->resolver_->GetProxyForURL(
111 url_, &result_, base::Bind(&Job::GetProxyDone, base::Unretained(this)),
112 &request_handle_, BoundNetLog());
113 if (result != ERR_IO_PENDING) {
114 GetProxyDone(result);
115 return;
116 }
117 client_.set_error_handler(this);
eroman 2015/02/12 19:00:42 Is it intentional that the error handler is only s
Sam McNally 2015/02/13 00:03:19 Yes, there's no need to set it in the sync case.
118 }
119
120 void MojoProxyResolverImpl::Job::GetProxyDone(int32_t error) {
eroman 2015/02/12 19:00:42 int
Sam McNally 2015/02/13 00:03:19 Done.
121 request_handle_ = nullptr;
122 DVLOG(1) << "GetProxyForUrl(" << url_ << ") finished with error " << error
123 << ". " << result_.proxy_list().size() << " Proxies returned:";
124 for (const auto& proxy : result_.proxy_list().GetAll()) {
125 DVLOG(1) << proxy.ToURI();
126 }
127 mojo::Array<interfaces::ProxyServerPtr> result;
128 if (error == OK)
eroman 2015/02/12 19:00:41 Use curly braces around this if statement (more th
Sam McNally 2015/02/13 00:03:19 Done.
129 result = mojo::Array<interfaces::ProxyServerPtr>::From(
130 result_.proxy_list().GetAll());
131 client_->ReportResult(error, result.Pass());
132 resolver_->DeleteJob(this);
133 }
134
135 void MojoProxyResolverImpl::Job::OnConnectionError() {
136 resolver_->DeleteJob(this);
137 }
138
139 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698