Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / services / proxy_resolver / proxy_resolver_impl.cc
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "services/proxy_resolver/proxy_resolver_impl.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/memory/raw_ptr.h"
11 #include "mojo/public/cpp/bindings/remote.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/network_anonymization_key.h"
14 #include "net/base/proxy_string_util.h"
15 #include "net/proxy_resolution/pac_file_data.h"
16 #include "net/proxy_resolution/proxy_info.h"
17 #include "services/proxy_resolver/mojo_proxy_resolver_v8_tracing_bindings.h"
18 #include "services/proxy_resolver/proxy_resolver_v8_tracing.h"
19
20 namespace proxy_resolver {
21
22 class ProxyResolverImpl::Job {
23  public:
24   Job(mojo::PendingRemote<mojom::ProxyResolverRequestClient> client,
25       ProxyResolverImpl* resolver,
26       const GURL& url);
27
28   Job(const Job&) = delete;
29   Job& operator=(const Job&) = delete;
30
31   ~Job();
32
33   void Start(const net::NetworkAnonymizationKey& network_anonymization_key);
34
35  private:
36   // Mojo error handler. This is invoked in response to the client
37   // disconnecting, indicating cancellation.
38   void OnDisconnect();
39
40   void GetProxyDone(int error);
41
42   raw_ptr<ProxyResolverImpl> resolver_;
43
44   mojo::Remote<mojom::ProxyResolverRequestClient> client_;
45   net::ProxyInfo result_;
46   GURL url_;
47   std::unique_ptr<net::ProxyResolver::Request> request_;
48   bool done_;
49 };
50
51 ProxyResolverImpl::ProxyResolverImpl(
52     std::unique_ptr<ProxyResolverV8Tracing> resolver)
53     : resolver_(std::move(resolver)) {}
54
55 ProxyResolverImpl::~ProxyResolverImpl() = default;
56
57 void ProxyResolverImpl::GetProxyForUrl(
58     const GURL& url,
59     const net::NetworkAnonymizationKey& network_anonymization_key,
60     mojo::PendingRemote<mojom::ProxyResolverRequestClient> client) {
61   DVLOG(1) << "GetProxyForUrl(" << url << ")";
62   std::unique_ptr<Job> job =
63       std::make_unique<Job>(std::move(client), this, url);
64   Job* job_ptr = job.get();
65   resolve_jobs_[job_ptr] = std::move(job);
66   job_ptr->Start(network_anonymization_key);
67 }
68
69 void ProxyResolverImpl::DeleteJob(Job* job) {
70   size_t erased_count = resolve_jobs_.erase(job);
71   DCHECK_EQ(1U, erased_count);
72 }
73
74 ProxyResolverImpl::Job::Job(
75     mojo::PendingRemote<mojom::ProxyResolverRequestClient> client,
76     ProxyResolverImpl* resolver,
77     const GURL& url)
78     : resolver_(resolver),
79       client_(std::move(client)),
80       url_(url),
81       done_(false) {}
82
83 ProxyResolverImpl::Job::~Job() = default;
84
85 void ProxyResolverImpl::Job::Start(
86     const net::NetworkAnonymizationKey& network_anonymization_key) {
87   resolver_->resolver_->GetProxyForURL(
88       url_, network_anonymization_key, &result_,
89       base::BindOnce(&Job::GetProxyDone, base::Unretained(this)), &request_,
90       std::make_unique<MojoProxyResolverV8TracingBindings<
91           mojom::ProxyResolverRequestClient>>(client_.get()));
92   client_.set_disconnect_handler(base::BindOnce(
93       &ProxyResolverImpl::Job::OnDisconnect, base::Unretained(this)));
94 }
95
96 void ProxyResolverImpl::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) << net::ProxyServerToProxyUri(proxy);
102   }
103   if (error == net::OK)
104     client_->ReportResult(error, result_);
105   else
106     client_->ReportResult(error, net::ProxyInfo());
107
108   resolver_->DeleteJob(this);
109 }
110
111 void ProxyResolverImpl::Job::OnDisconnect() {
112   resolver_->DeleteJob(this);
113 }
114
115 }  // namespace proxy_resolver