Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / proxy / mojo_proxy_resolver_impl.cc
blob7c52c17901f207fc18b9e5e22569869d3dc10b7f
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 net::ProxyResolver::RequestHandle request_handle() { return request_handle_; }
30 private:
31 // Mojo error handler. This is invoked in response to the client
32 // disconnecting, indicating cancellation.
33 void OnConnectionError();
35 void GetProxyDone(int error);
37 MojoProxyResolverImpl* resolver_;
39 interfaces::ProxyResolverRequestClientPtr client_;
40 ProxyInfo result_;
41 GURL url_;
42 net::ProxyResolver::RequestHandle request_handle_;
43 bool done_;
45 base::WeakPtrFactory<interfaces::ProxyResolverRequestClient> weak_factory_;
47 DISALLOW_COPY_AND_ASSIGN(Job);
50 MojoProxyResolverImpl::MojoProxyResolverImpl(
51 scoped_ptr<ProxyResolverV8Tracing> resolver)
52 : resolver_(resolver.Pass()) {
55 MojoProxyResolverImpl::~MojoProxyResolverImpl() {
56 STLDeleteElements(&resolve_jobs_);
59 void MojoProxyResolverImpl::GetProxyForUrl(
60 const mojo::String& url,
61 interfaces::ProxyResolverRequestClientPtr client) {
62 DVLOG(1) << "GetProxyForUrl(" << url << ")";
63 Job* job = new Job(client.Pass(), this, url.To<GURL>());
64 bool inserted = resolve_jobs_.insert(job).second;
65 DCHECK(inserted);
66 job->Start();
69 void MojoProxyResolverImpl::DeleteJob(Job* job) {
70 if (job->request_handle())
71 request_handle_to_job_.erase(job->request_handle());
73 size_t num_erased = resolve_jobs_.erase(job);
74 DCHECK(num_erased);
75 delete job;
78 MojoProxyResolverImpl::Job::Job(
79 interfaces::ProxyResolverRequestClientPtr client,
80 MojoProxyResolverImpl* resolver,
81 const GURL& url)
82 : resolver_(resolver),
83 client_(client.Pass()),
84 url_(url),
85 request_handle_(nullptr),
86 done_(false),
87 weak_factory_(client_.get()) {
90 MojoProxyResolverImpl::Job::~Job() {
91 if (request_handle_ && !done_)
92 resolver_->resolver_->CancelRequest(request_handle_);
95 void MojoProxyResolverImpl::Job::Start() {
96 resolver_->resolver_->GetProxyForURL(
97 url_, &result_, base::Bind(&Job::GetProxyDone, base::Unretained(this)),
98 &request_handle_,
99 make_scoped_ptr(new MojoProxyResolverV8TracingBindings<
100 interfaces::ProxyResolverRequestClient>(weak_factory_.GetWeakPtr())));
101 client_.set_connection_error_handler(base::Bind(
102 &MojoProxyResolverImpl::Job::OnConnectionError, base::Unretained(this)));
103 resolver_->request_handle_to_job_.insert(
104 std::make_pair(request_handle_, this));
107 void MojoProxyResolverImpl::Job::GetProxyDone(int error) {
108 done_ = true;
109 DVLOG(1) << "GetProxyForUrl(" << url_ << ") finished with error " << error
110 << ". " << result_.proxy_list().size() << " Proxies returned:";
111 for (const auto& proxy : result_.proxy_list().GetAll()) {
112 DVLOG(1) << proxy.ToURI();
114 mojo::Array<interfaces::ProxyServerPtr> result;
115 if (error == OK) {
116 result = mojo::Array<interfaces::ProxyServerPtr>::From(
117 result_.proxy_list().GetAll());
119 client_->ReportResult(error, result.Pass());
120 resolver_->DeleteJob(this);
123 void MojoProxyResolverImpl::Job::OnConnectionError() {
124 resolver_->DeleteJob(this);
127 } // namespace net