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

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

Issue 939503004: Add LoadState reporting to the mojo proxy resolver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proxy-resolver-mojo
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
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_impl.h" 5 #include "net/proxy/mojo_proxy_resolver_impl.h"
6 6
7 #include "base/memory/weak_ptr.h"
8 #include "base/message_loop/message_loop.h"
7 #include "base/stl_util.h" 9 #include "base/stl_util.h"
8 #include "mojo/common/common_type_converters.h" 10 #include "mojo/common/common_type_converters.h"
11 #include "net/base/load_state_change_coalescer.h"
9 #include "net/base/net_errors.h" 12 #include "net/base/net_errors.h"
10 #include "net/base/net_log.h" 13 #include "net/base/net_log.h"
11 #include "net/proxy/mojo_type_converters.h" 14 #include "net/proxy/mojo_type_converters.h"
12 #include "net/proxy/proxy_info.h" 15 #include "net/proxy/proxy_info.h"
13 #include "net/proxy/proxy_resolver.h" 16 #include "net/proxy/proxy_resolver.h"
14 #include "net/proxy/proxy_resolver_script_data.h" 17 #include "net/proxy/proxy_resolver_script_data.h"
15 18
16 namespace net { 19 namespace net {
20 namespace {
21 const int kLoadStateChangeCoalesceTimeoutMilliseconds = 10;
22 }
17 23
18 class MojoProxyResolverImpl::Job : public mojo::ErrorHandler { 24 class MojoProxyResolverImpl::Job : public mojo::ErrorHandler {
19 public: 25 public:
20 Job(interfaces::ProxyResolverRequestClientPtr client, 26 Job(interfaces::ProxyResolverRequestClientPtr client,
21 MojoProxyResolverImpl* resolver, 27 MojoProxyResolverImpl* resolver,
22 const GURL& url); 28 const GURL& url);
23 ~Job() override; 29 ~Job() override;
24 30
25 void Start(); 31 void Start();
26 32
33 // Invoked when the LoadState for this job changes.
34 void LoadStateChanged(LoadState load_state);
35
36 net::ProxyResolver::RequestHandle request_handle() { return request_handle_; }
37
27 private: 38 private:
28 // mojo::ErrorHandler override. 39 // mojo::ErrorHandler override.
29 // This is invoked in response to the client disconnecting, indicating 40 // This is invoked in response to the client disconnecting, indicating
30 // cancellation. 41 // cancellation.
31 void OnConnectionError() override; 42 void OnConnectionError() override;
32 43
33 void GetProxyDone(int error); 44 void GetProxyDone(int error);
34 45
46 void SendLoadStateChanged(LoadState load_state);
47
35 MojoProxyResolverImpl* resolver_; 48 MojoProxyResolverImpl* resolver_;
36 49
37 interfaces::ProxyResolverRequestClientPtr client_; 50 interfaces::ProxyResolverRequestClientPtr client_;
38 ProxyInfo result_; 51 ProxyInfo result_;
39 GURL url_; 52 GURL url_;
40 net::ProxyResolver::RequestHandle request_handle_; 53 net::ProxyResolver::RequestHandle request_handle_;
54 bool done_;
55 LoadStateChangeCoalescer load_state_change_coalescer_;
41 56
42 DISALLOW_COPY_AND_ASSIGN(Job); 57 DISALLOW_COPY_AND_ASSIGN(Job);
43 }; 58 };
44 59
45 MojoProxyResolverImpl::MojoProxyResolverImpl( 60 MojoProxyResolverImpl::MojoProxyResolverImpl(
46 scoped_ptr<net::ProxyResolver> resolver) 61 scoped_ptr<net::ProxyResolver> resolver)
47 : resolver_(resolver.Pass()) { 62 : resolver_(resolver.Pass()) {
48 DCHECK(resolver_->expects_pac_bytes()); 63 DCHECK(resolver_->expects_pac_bytes());
49 } 64 }
50 65
51 MojoProxyResolverImpl::~MojoProxyResolverImpl() { 66 MojoProxyResolverImpl::~MojoProxyResolverImpl() {
52 if (!set_pac_script_requests_.empty()) 67 if (!set_pac_script_requests_.empty())
53 resolver_->CancelSetPacScript(); 68 resolver_->CancelSetPacScript();
54 STLDeleteElements(&resolve_jobs_); 69 STLDeleteElements(&resolve_jobs_);
55 } 70 }
56 71
72 void MojoProxyResolverImpl::LoadStateChanged(
73 net::ProxyResolver::RequestHandle handle,
74 LoadState load_state) {
75 auto it = request_handle_to_job_.find(handle);
76 DCHECK(it != request_handle_to_job_.end());
77 it->second->LoadStateChanged(load_state);
78 }
79
57 void MojoProxyResolverImpl::SetPacScript( 80 void MojoProxyResolverImpl::SetPacScript(
58 const mojo::String& data, 81 const mojo::String& data,
59 const mojo::Callback<void(int32_t)>& callback) { 82 const mojo::Callback<void(int32_t)>& callback) {
60 DVLOG(1) << "SetPacScript(" << data << ")"; 83 DVLOG(1) << "SetPacScript(" << data << ")";
61 set_pac_script_requests_.push( 84 set_pac_script_requests_.push(
62 SetPacScriptRequest(ProxyResolverScriptData::FromUTF8(data), callback)); 85 SetPacScriptRequest(ProxyResolverScriptData::FromUTF8(data), callback));
63 if (set_pac_script_requests_.size() == 1) 86 if (set_pac_script_requests_.size() == 1)
64 StartSetPacScript(); 87 StartSetPacScript();
65 } 88 }
66 89
67 void MojoProxyResolverImpl::GetProxyForUrl( 90 void MojoProxyResolverImpl::GetProxyForUrl(
68 const mojo::String& url, 91 const mojo::String& url,
69 interfaces::ProxyResolverRequestClientPtr client) { 92 interfaces::ProxyResolverRequestClientPtr client) {
70 DVLOG(1) << "GetProxyForUrl(" << url << ")"; 93 DVLOG(1) << "GetProxyForUrl(" << url << ")";
71 Job* job = new Job(client.Pass(), this, url.To<GURL>()); 94 Job* job = new Job(client.Pass(), this, url.To<GURL>());
72 bool inserted = resolve_jobs_.insert(job).second; 95 bool inserted = resolve_jobs_.insert(job).second;
73 DCHECK(inserted); 96 DCHECK(inserted);
74 job->Start(); 97 job->Start();
75 } 98 }
76 99
77 void MojoProxyResolverImpl::DeleteJob(Job* job) { 100 void MojoProxyResolverImpl::DeleteJob(Job* job) {
101 if (job->request_handle())
102 request_handle_to_job_.erase(job->request_handle());
103
78 size_t num_erased = resolve_jobs_.erase(job); 104 size_t num_erased = resolve_jobs_.erase(job);
79 DCHECK(num_erased); 105 DCHECK(num_erased);
80 delete job; 106 delete job;
81 } 107 }
82 108
83 void MojoProxyResolverImpl::StartSetPacScript() { 109 void MojoProxyResolverImpl::StartSetPacScript() {
84 DCHECK(!set_pac_script_requests_.empty()); 110 DCHECK(!set_pac_script_requests_.empty());
85 int result = resolver_->SetPacScript( 111 int result = resolver_->SetPacScript(
86 set_pac_script_requests_.front().script_data, 112 set_pac_script_requests_.front().script_data,
87 base::Bind(&MojoProxyResolverImpl::SetPacScriptDone, 113 base::Bind(&MojoProxyResolverImpl::SetPacScriptDone,
(...skipping 11 matching lines...) Expand all
99 StartSetPacScript(); 125 StartSetPacScript();
100 } 126 }
101 127
102 MojoProxyResolverImpl::Job::Job( 128 MojoProxyResolverImpl::Job::Job(
103 interfaces::ProxyResolverRequestClientPtr client, 129 interfaces::ProxyResolverRequestClientPtr client,
104 MojoProxyResolverImpl* resolver, 130 MojoProxyResolverImpl* resolver,
105 const GURL& url) 131 const GURL& url)
106 : resolver_(resolver), 132 : resolver_(resolver),
107 client_(client.Pass()), 133 client_(client.Pass()),
108 url_(url), 134 url_(url),
109 request_handle_(nullptr) { 135 request_handle_(nullptr),
136 done_(false),
137 load_state_change_coalescer_(
138 base::Bind(&MojoProxyResolverImpl::Job::SendLoadStateChanged,
139 base::Unretained(this)),
140 base::TimeDelta::FromMilliseconds(
141 kLoadStateChangeCoalesceTimeoutMilliseconds),
142 LOAD_STATE_RESOLVING_PROXY_FOR_URL) {
110 } 143 }
111 144
112 MojoProxyResolverImpl::Job::~Job() { 145 MojoProxyResolverImpl::Job::~Job() {
113 if (request_handle_) 146 if (request_handle_ && !done_)
114 resolver_->resolver_->CancelRequest(request_handle_); 147 resolver_->resolver_->CancelRequest(request_handle_);
115 } 148 }
116 149
117 void MojoProxyResolverImpl::Job::Start() { 150 void MojoProxyResolverImpl::Job::Start() {
118 int result = resolver_->resolver_->GetProxyForURL( 151 int result = resolver_->resolver_->GetProxyForURL(
119 url_, &result_, base::Bind(&Job::GetProxyDone, base::Unretained(this)), 152 url_, &result_, base::Bind(&Job::GetProxyDone, base::Unretained(this)),
120 &request_handle_, BoundNetLog()); 153 &request_handle_, BoundNetLog());
121 if (result != ERR_IO_PENDING) { 154 if (result != ERR_IO_PENDING) {
122 GetProxyDone(result); 155 GetProxyDone(result);
123 return; 156 return;
124 } 157 }
125 client_.set_error_handler(this); 158 client_.set_error_handler(this);
159 resolver_->request_handle_to_job_.insert(
160 std::make_pair(request_handle_, this));
161 }
162
163 void MojoProxyResolverImpl::Job::LoadStateChanged(LoadState load_state) {
164 load_state_change_coalescer_.LoadStateChanged(load_state);
126 } 165 }
127 166
128 void MojoProxyResolverImpl::Job::GetProxyDone(int error) { 167 void MojoProxyResolverImpl::Job::GetProxyDone(int error) {
129 request_handle_ = nullptr; 168 done_ = true;
130 DVLOG(1) << "GetProxyForUrl(" << url_ << ") finished with error " << error 169 DVLOG(1) << "GetProxyForUrl(" << url_ << ") finished with error " << error
131 << ". " << result_.proxy_list().size() << " Proxies returned:"; 170 << ". " << result_.proxy_list().size() << " Proxies returned:";
132 for (const auto& proxy : result_.proxy_list().GetAll()) { 171 for (const auto& proxy : result_.proxy_list().GetAll()) {
133 DVLOG(1) << proxy.ToURI(); 172 DVLOG(1) << proxy.ToURI();
134 } 173 }
135 mojo::Array<interfaces::ProxyServerPtr> result; 174 mojo::Array<interfaces::ProxyServerPtr> result;
136 if (error == OK) { 175 if (error == OK) {
137 result = mojo::Array<interfaces::ProxyServerPtr>::From( 176 result = mojo::Array<interfaces::ProxyServerPtr>::From(
138 result_.proxy_list().GetAll()); 177 result_.proxy_list().GetAll());
139 } 178 }
140 client_->ReportResult(error, result.Pass()); 179 client_->ReportResult(error, result.Pass());
141 resolver_->DeleteJob(this); 180 resolver_->DeleteJob(this);
142 } 181 }
143 182
144 void MojoProxyResolverImpl::Job::OnConnectionError() { 183 void MojoProxyResolverImpl::Job::OnConnectionError() {
145 resolver_->DeleteJob(this); 184 resolver_->DeleteJob(this);
146 } 185 }
147 186
187 void MojoProxyResolverImpl::Job::SendLoadStateChanged(LoadState load_state) {
188 client_->LoadStateChanged(load_state);
189 }
190
148 MojoProxyResolverImpl::SetPacScriptRequest::SetPacScriptRequest( 191 MojoProxyResolverImpl::SetPacScriptRequest::SetPacScriptRequest(
149 const scoped_refptr<ProxyResolverScriptData>& script_data, 192 const scoped_refptr<ProxyResolverScriptData>& script_data,
150 const mojo::Callback<void(int32_t)>& callback) 193 const mojo::Callback<void(int32_t)>& callback)
151 : script_data(script_data), callback(callback) { 194 : script_data(script_data), callback(callback) {
152 } 195 }
153 196
154 MojoProxyResolverImpl::SetPacScriptRequest::~SetPacScriptRequest() = default; 197 MojoProxyResolverImpl::SetPacScriptRequest::~SetPacScriptRequest() = default;
155 198
156 } // namespace net 199 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698