Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / proxy / mojo_proxy_resolver_impl.cc
blobc5ae034040bc9a1c8e030b49c11d8f9a8d9a458c
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.
5 #include "net/proxy/mojo_proxy_resolver_impl.h"
7 #include "base/stl_util.h"
8 #include "mojo/common/url_type_converters.h"
9 #include "net/base/net_errors.h"
10 #include "net/log/net_log.h"
11 #include "net/proxy/mojo_proxy_resolver_v8_tracing_bindings.h"
12 #include "net/proxy/mojo_proxy_type_converters.h"
13 #include "net/proxy/proxy_info.h"
14 #include "net/proxy/proxy_resolver_script_data.h"
15 #include "net/proxy/proxy_resolver_v8_tracing.h"
17 namespace net {
19 class MojoProxyResolverImpl::Job {
20 public:
21 Job(interfaces::ProxyResolverRequestClientPtr client,
22 MojoProxyResolverImpl* resolver,
23 const GURL& url);
24 ~Job();
26 void Start();
28 private:
29 // Mojo error handler. This is invoked in response to the client
30 // disconnecting, indicating cancellation.
31 void OnConnectionError();
33 void GetProxyDone(int error);
35 MojoProxyResolverImpl* resolver_;
37 interfaces::ProxyResolverRequestClientPtr client_;
38 ProxyInfo result_;
39 GURL url_;
40 net::ProxyResolver::RequestHandle request_handle_;
41 bool done_;
43 DISALLOW_COPY_AND_ASSIGN(Job);
46 MojoProxyResolverImpl::MojoProxyResolverImpl(
47 scoped_ptr<ProxyResolverV8Tracing> resolver)
48 : resolver_(resolver.Pass()) {
51 MojoProxyResolverImpl::~MojoProxyResolverImpl() {
52 STLDeleteElements(&resolve_jobs_);
55 void MojoProxyResolverImpl::GetProxyForUrl(
56 const mojo::String& url,
57 interfaces::ProxyResolverRequestClientPtr client) {
58 DVLOG(1) << "GetProxyForUrl(" << url << ")";
59 Job* job = new Job(client.Pass(), this, url.To<GURL>());
60 bool inserted = resolve_jobs_.insert(job).second;
61 DCHECK(inserted);
62 job->Start();
65 void MojoProxyResolverImpl::DeleteJob(Job* job) {
66 size_t num_erased = resolve_jobs_.erase(job);
67 DCHECK(num_erased);
68 delete job;
71 MojoProxyResolverImpl::Job::Job(
72 interfaces::ProxyResolverRequestClientPtr client,
73 MojoProxyResolverImpl* resolver,
74 const GURL& url)
75 : resolver_(resolver),
76 client_(client.Pass()),
77 url_(url),
78 request_handle_(nullptr),
79 done_(false) {}
81 MojoProxyResolverImpl::Job::~Job() {
82 if (request_handle_ && !done_)
83 resolver_->resolver_->CancelRequest(request_handle_);
86 void MojoProxyResolverImpl::Job::Start() {
87 resolver_->resolver_->GetProxyForURL(
88 url_, &result_, base::Bind(&Job::GetProxyDone, base::Unretained(this)),
89 &request_handle_,
90 make_scoped_ptr(new MojoProxyResolverV8TracingBindings<
91 interfaces::ProxyResolverRequestClient>(client_.get())));
92 client_.set_connection_error_handler(base::Bind(
93 &MojoProxyResolverImpl::Job::OnConnectionError, base::Unretained(this)));
96 void MojoProxyResolverImpl::Job::GetProxyDone(int error) {
97 done_ = true;
98 DVLOG(1) << "GetProxyForUrl(" << url_ << ") finished with error " << error
99 << ". " << result_.proxy_list().size() << " Proxies returned:";
100 for (const auto& proxy : result_.proxy_list().GetAll()) {
101 DVLOG(1) << proxy.ToURI();
103 mojo::Array<interfaces::ProxyServerPtr> result;
104 if (error == OK) {
105 result = mojo::Array<interfaces::ProxyServerPtr>::From(
106 result_.proxy_list().GetAll());
108 client_->ReportResult(error, result.Pass());
109 resolver_->DeleteJob(this);
112 void MojoProxyResolverImpl::Job::OnConnectionError() {
113 resolver_->DeleteJob(this);
116 } // namespace net