Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / net / proxy / mojo_proxy_resolver_impl.cc
blobc300b6eff0dc5604a27b1aa804f38781fb3e978f
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/load_state_change_coalescer.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"
16 namespace net {
17 namespace {
18 const int kLoadStateChangeCoalesceTimeoutMilliseconds = 10;
21 class MojoProxyResolverImpl::Job : public mojo::ErrorHandler {
22 public:
23 Job(interfaces::ProxyResolverRequestClientPtr client,
24 MojoProxyResolverImpl* resolver,
25 const GURL& url);
26 ~Job() override;
28 void Start();
30 // Invoked when the LoadState for this job changes.
31 void LoadStateChanged(LoadState load_state);
33 net::ProxyResolver::RequestHandle request_handle() { return request_handle_; }
35 private:
36 // mojo::ErrorHandler override.
37 // This is invoked in response to the client disconnecting, indicating
38 // cancellation.
39 void OnConnectionError() override;
41 void GetProxyDone(int error);
43 void SendLoadStateChanged(LoadState load_state);
45 MojoProxyResolverImpl* resolver_;
47 interfaces::ProxyResolverRequestClientPtr client_;
48 ProxyInfo result_;
49 GURL url_;
50 net::ProxyResolver::RequestHandle request_handle_;
51 bool done_;
52 LoadStateChangeCoalescer load_state_change_coalescer_;
54 DISALLOW_COPY_AND_ASSIGN(Job);
57 MojoProxyResolverImpl::MojoProxyResolverImpl(
58 scoped_ptr<net::ProxyResolver> resolver,
59 const base::Callback<
60 void(const net::ProxyResolver::LoadStateChangedCallback&)>&
61 load_state_change_callback_setter)
62 : resolver_(resolver.Pass()) {
63 load_state_change_callback_setter.Run(base::Bind(
64 &MojoProxyResolverImpl::LoadStateChanged, base::Unretained(this)));
67 MojoProxyResolverImpl::~MojoProxyResolverImpl() {
68 STLDeleteElements(&resolve_jobs_);
71 void MojoProxyResolverImpl::LoadStateChanged(
72 net::ProxyResolver::RequestHandle handle,
73 LoadState load_state) {
74 auto it = request_handle_to_job_.find(handle);
75 DCHECK(it != request_handle_to_job_.end());
76 it->second->LoadStateChanged(load_state);
79 void MojoProxyResolverImpl::GetProxyForUrl(
80 const mojo::String& url,
81 interfaces::ProxyResolverRequestClientPtr client) {
82 DVLOG(1) << "GetProxyForUrl(" << url << ")";
83 Job* job = new Job(client.Pass(), this, url.To<GURL>());
84 bool inserted = resolve_jobs_.insert(job).second;
85 DCHECK(inserted);
86 job->Start();
89 void MojoProxyResolverImpl::DeleteJob(Job* job) {
90 if (job->request_handle())
91 request_handle_to_job_.erase(job->request_handle());
93 size_t num_erased = resolve_jobs_.erase(job);
94 DCHECK(num_erased);
95 delete job;
98 MojoProxyResolverImpl::Job::Job(
99 interfaces::ProxyResolverRequestClientPtr client,
100 MojoProxyResolverImpl* resolver,
101 const GURL& url)
102 : resolver_(resolver),
103 client_(client.Pass()),
104 url_(url),
105 request_handle_(nullptr),
106 done_(false),
107 load_state_change_coalescer_(
108 base::Bind(&MojoProxyResolverImpl::Job::SendLoadStateChanged,
109 base::Unretained(this)),
110 base::TimeDelta::FromMilliseconds(
111 kLoadStateChangeCoalesceTimeoutMilliseconds),
112 LOAD_STATE_RESOLVING_PROXY_FOR_URL) {
115 MojoProxyResolverImpl::Job::~Job() {
116 if (request_handle_ && !done_)
117 resolver_->resolver_->CancelRequest(request_handle_);
120 void MojoProxyResolverImpl::Job::Start() {
121 int result = resolver_->resolver_->GetProxyForURL(
122 url_, &result_, base::Bind(&Job::GetProxyDone, base::Unretained(this)),
123 &request_handle_, BoundNetLog());
124 if (result != ERR_IO_PENDING) {
125 GetProxyDone(result);
126 return;
128 client_.set_error_handler(this);
129 resolver_->request_handle_to_job_.insert(
130 std::make_pair(request_handle_, this));
133 void MojoProxyResolverImpl::Job::LoadStateChanged(LoadState load_state) {
134 load_state_change_coalescer_.LoadStateChanged(load_state);
137 void MojoProxyResolverImpl::Job::GetProxyDone(int error) {
138 done_ = true;
139 DVLOG(1) << "GetProxyForUrl(" << url_ << ") finished with error " << error
140 << ". " << result_.proxy_list().size() << " Proxies returned:";
141 for (const auto& proxy : result_.proxy_list().GetAll()) {
142 DVLOG(1) << proxy.ToURI();
144 mojo::Array<interfaces::ProxyServerPtr> result;
145 if (error == OK) {
146 result = mojo::Array<interfaces::ProxyServerPtr>::From(
147 result_.proxy_list().GetAll());
149 client_->ReportResult(error, result.Pass());
150 resolver_->DeleteJob(this);
153 void MojoProxyResolverImpl::Job::OnConnectionError() {
154 resolver_->DeleteJob(this);
157 void MojoProxyResolverImpl::Job::SendLoadStateChanged(LoadState load_state) {
158 client_->LoadStateChanged(load_state);
161 } // namespace net