Chromium Code Reviews| Index: net/proxy/mojo_proxy_resolver_impl.cc |
| diff --git a/net/proxy/mojo_proxy_resolver_impl.cc b/net/proxy/mojo_proxy_resolver_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c612872724e41da9f918813156f47e6a9c165b26 |
| --- /dev/null |
| +++ b/net/proxy/mojo_proxy_resolver_impl.cc |
| @@ -0,0 +1,139 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/proxy/mojo_proxy_resolver_impl.h" |
| + |
| +#include "base/stl_util.h" |
| +#include "mojo/common/common_type_converters.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/net_log.h" |
| +#include "net/proxy/mojo_type_converters.h" |
| +#include "net/proxy/proxy_info.h" |
| +#include "net/proxy/proxy_resolver.h" |
| +#include "net/proxy/proxy_resolver_script_data.h" |
| + |
| +namespace net { |
| + |
| +class MojoProxyResolverImpl::Job : public mojo::ErrorHandler { |
| + public: |
| + Job(interfaces::ProxyResolverRequestClientPtr client, |
| + MojoProxyResolverImpl* resolver, |
| + const GURL& url); |
| + ~Job() override; |
| + |
| + void Start(); |
| + |
| + private: |
| + // mojo::ErrorHandler override. |
| + 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.
|
| + |
| + 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.
|
| + |
| + MojoProxyResolverImpl* resolver_; |
| + |
| + interfaces::ProxyResolverRequestClientPtr client_; |
| + ProxyInfo result_; |
| + GURL url_; |
| + net::ProxyResolver::RequestHandle request_handle_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Job); |
| +}; |
| + |
| +MojoProxyResolverImpl::MojoProxyResolverImpl( |
| + scoped_ptr<net::ProxyResolver> resolver) |
| + : resolver_(resolver.Pass()) { |
| + DCHECK(resolver_->expects_pac_bytes()); |
| +} |
| + |
| +MojoProxyResolverImpl::~MojoProxyResolverImpl() { |
| + if (!set_pac_script_callback_.is_null()) { |
| + resolver_->CancelSetPacScript(); |
| + 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
|
| + } |
| + STLDeleteElements(&resolve_jobs_); |
| +} |
| + |
| +void MojoProxyResolverImpl::SetPacScript( |
| + const mojo::String& data, |
| + const mojo::Callback<void(int32_t)>& callback) { |
| + DVLOG(1) << "SetPacScript(" << data << ")"; |
| + DCHECK(set_pac_script_callback_.is_null()); |
| + set_pac_script_callback_ = callback; |
| + int result = resolver_->SetPacScript( |
| + ProxyResolverScriptData::FromUTF8(data), |
| + base::Bind(&MojoProxyResolverImpl::SetPacScriptDone, |
| + base::Unretained(this))); |
| + if (result != ERR_IO_PENDING) |
| + SetPacScriptDone(result); |
| +} |
| + |
| +void MojoProxyResolverImpl::GetProxyForUrl( |
| + const mojo::String& url, |
| + interfaces::ProxyResolverRequestClientPtr client) { |
| + DVLOG(1) << "GetProxyForUrl(" << url << ")"; |
| + Job* job = new Job(client.Pass(), this, url.To<GURL>()); |
| + bool inserted = resolve_jobs_.insert(job).second; |
| + DCHECK(inserted); |
| + job->Start(); |
| +} |
| + |
| +void MojoProxyResolverImpl::DeleteJob(Job* job) { |
| + size_t num_erased = resolve_jobs_.erase(job); |
| + DCHECK(num_erased); |
| + delete job; |
| +} |
| + |
| +void MojoProxyResolverImpl::SetPacScriptDone(int result) { |
| + DVLOG(1) << "SetPacScript finished with error " << result; |
| + DCHECK(!set_pac_script_callback_.is_null()); |
| + set_pac_script_callback_.Run(result); |
| + set_pac_script_callback_.reset(); |
| +} |
| + |
| +MojoProxyResolverImpl::Job::Job( |
| + interfaces::ProxyResolverRequestClientPtr client, |
| + MojoProxyResolverImpl* resolver, |
| + const GURL& url) |
| + : resolver_(resolver), |
| + client_(client.Pass()), |
| + url_(url), |
| + request_handle_(nullptr) { |
| +} |
| + |
| +MojoProxyResolverImpl::Job::~Job() { |
| + if (request_handle_) |
| + resolver_->resolver_->CancelRequest(request_handle_); |
| +} |
| + |
| +void MojoProxyResolverImpl::Job::Start() { |
| + int result = resolver_->resolver_->GetProxyForURL( |
| + url_, &result_, base::Bind(&Job::GetProxyDone, base::Unretained(this)), |
| + &request_handle_, BoundNetLog()); |
| + if (result != ERR_IO_PENDING) { |
| + GetProxyDone(result); |
| + return; |
| + } |
| + 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.
|
| +} |
| + |
| +void MojoProxyResolverImpl::Job::GetProxyDone(int32_t error) { |
|
eroman
2015/02/12 19:00:42
int
Sam McNally
2015/02/13 00:03:19
Done.
|
| + request_handle_ = nullptr; |
| + DVLOG(1) << "GetProxyForUrl(" << url_ << ") finished with error " << error |
| + << ". " << result_.proxy_list().size() << " Proxies returned:"; |
| + for (const auto& proxy : result_.proxy_list().GetAll()) { |
| + DVLOG(1) << proxy.ToURI(); |
| + } |
| + mojo::Array<interfaces::ProxyServerPtr> result; |
| + 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.
|
| + result = mojo::Array<interfaces::ProxyServerPtr>::From( |
| + result_.proxy_list().GetAll()); |
| + client_->ReportResult(error, result.Pass()); |
| + resolver_->DeleteJob(this); |
| +} |
| + |
| +void MojoProxyResolverImpl::Job::OnConnectionError() { |
| + resolver_->DeleteJob(this); |
| +} |
| + |
| +} // namespace net |