fix: use proxy lookup api of network context to resolve proxies
This commit is contained in:
parent
28cff4644e
commit
7a68d632e6
2 changed files with 61 additions and 52 deletions
|
@ -4,76 +4,79 @@
|
||||||
|
|
||||||
#include "atom/browser/net/resolve_proxy_helper.h"
|
#include "atom/browser/net/resolve_proxy_helper.h"
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "base/bind.h"
|
#include "base/bind.h"
|
||||||
#include "base/threading/thread_task_runner_handle.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "net/url_request/url_request_context.h"
|
#include "content/public/browser/storage_partition.h"
|
||||||
#include "net/url_request/url_request_context_getter.h"
|
#include "mojo/public/cpp/bindings/interface_request.h"
|
||||||
|
#include "net/proxy_resolution/proxy_info.h"
|
||||||
|
#include "services/network/public/mojom/network_context.mojom.h"
|
||||||
|
|
||||||
|
using content::BrowserThread;
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
ResolveProxyHelper::ResolveProxyHelper(AtomBrowserContext* browser_context)
|
ResolveProxyHelper::ResolveProxyHelper(AtomBrowserContext* browser_context)
|
||||||
: context_getter_(browser_context->GetRequestContext()),
|
: binding_(this), browser_context_(browser_context) {}
|
||||||
original_thread_(base::ThreadTaskRunnerHandle::Get()) {}
|
|
||||||
|
|
||||||
ResolveProxyHelper::~ResolveProxyHelper() {
|
ResolveProxyHelper::~ResolveProxyHelper() {
|
||||||
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||||
|
DCHECK(!owned_self_);
|
||||||
|
DCHECK(!binding_.is_bound());
|
||||||
// Clear all pending requests if the ProxyService is still alive.
|
// Clear all pending requests if the ProxyService is still alive.
|
||||||
pending_requests_.clear();
|
pending_requests_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResolveProxyHelper::ResolveProxy(const GURL& url,
|
void ResolveProxyHelper::ResolveProxy(const GURL& url,
|
||||||
const ResolveProxyCallback& callback) {
|
const ResolveProxyCallback& callback) {
|
||||||
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||||
// Enqueue the pending request.
|
// Enqueue the pending request.
|
||||||
pending_requests_.push_back(PendingRequest(url, callback));
|
pending_requests_.push_back(PendingRequest(url, callback));
|
||||||
|
|
||||||
// If nothing is in progress, start.
|
// If nothing is in progress, start.
|
||||||
if (pending_requests_.size() == 1)
|
if (!binding_.is_bound()) {
|
||||||
|
DCHECK_EQ(1u, pending_requests_.size());
|
||||||
StartPendingRequest();
|
StartPendingRequest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResolveProxyHelper::StartPendingRequest() {
|
void ResolveProxyHelper::StartPendingRequest() {
|
||||||
auto& pending_request = pending_requests_.front();
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||||
context_getter_->GetNetworkTaskRunner()->PostTask(
|
DCHECK(!binding_.is_bound());
|
||||||
FROM_HERE, base::BindOnce(&ResolveProxyHelper::StartPendingRequestInIO,
|
DCHECK(!pending_requests_.empty());
|
||||||
base::Unretained(this), pending_request.url));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ResolveProxyHelper::StartPendingRequestInIO(const GURL& url) {
|
|
||||||
auto* proxy_service =
|
|
||||||
context_getter_->GetURLRequestContext()->proxy_resolution_service();
|
|
||||||
// Start the request.
|
// Start the request.
|
||||||
int result = proxy_service->ResolveProxy(
|
network::mojom::ProxyLookupClientPtr proxy_lookup_client;
|
||||||
url, std::string(), &proxy_info_,
|
binding_.Bind(mojo::MakeRequest(&proxy_lookup_client));
|
||||||
base::Bind(&ResolveProxyHelper::OnProxyResolveComplete,
|
binding_.set_connection_error_handler(
|
||||||
base::RetainedRef(this)),
|
base::BindOnce(&ResolveProxyHelper::OnProxyLookupComplete,
|
||||||
nullptr, nullptr, net::NetLogWithSource());
|
base::Unretained(this), base::nullopt));
|
||||||
// Completed synchronously.
|
content::BrowserContext::GetDefaultStoragePartition(browser_context_)
|
||||||
if (result != net::ERR_IO_PENDING)
|
->GetNetworkContext()
|
||||||
OnProxyResolveComplete(result);
|
->LookUpProxyForURL(pending_requests_.front().url,
|
||||||
|
std::move(proxy_lookup_client));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResolveProxyHelper::OnProxyResolveComplete(int result) {
|
void ResolveProxyHelper::OnProxyLookupComplete(
|
||||||
|
const base::Optional<net::ProxyInfo>& proxy_info) {
|
||||||
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||||
DCHECK(!pending_requests_.empty());
|
DCHECK(!pending_requests_.empty());
|
||||||
|
|
||||||
std::string proxy;
|
binding_.Close();
|
||||||
if (result == net::OK)
|
|
||||||
proxy = proxy_info_.ToPacString();
|
|
||||||
|
|
||||||
original_thread_->PostTask(
|
|
||||||
FROM_HERE, base::BindOnce(&ResolveProxyHelper::SendProxyResult,
|
|
||||||
base::RetainedRef(this), proxy));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ResolveProxyHelper::SendProxyResult(const std::string& proxy) {
|
|
||||||
DCHECK(!pending_requests_.empty());
|
|
||||||
|
|
||||||
const auto& completed_request = pending_requests_.front();
|
|
||||||
if (!completed_request.callback.is_null())
|
|
||||||
completed_request.callback.Run(proxy);
|
|
||||||
|
|
||||||
// Clear the current (completed) request.
|
// Clear the current (completed) request.
|
||||||
|
PendingRequest completed_request = std::move(pending_requests_.front());
|
||||||
pending_requests_.pop_front();
|
pending_requests_.pop_front();
|
||||||
|
|
||||||
|
std::string proxy;
|
||||||
|
if (proxy_info)
|
||||||
|
proxy = proxy_info->ToPacString();
|
||||||
|
|
||||||
|
if (!completed_request.callback.is_null())
|
||||||
|
completed_request.callback.Run(proxy);
|
||||||
|
|
||||||
// Start the next request.
|
// Start the next request.
|
||||||
if (!pending_requests_.empty())
|
if (!pending_requests_.empty())
|
||||||
StartPendingRequest();
|
StartPendingRequest();
|
||||||
|
|
|
@ -9,19 +9,18 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "base/memory/ref_counted.h"
|
#include "base/memory/ref_counted.h"
|
||||||
#include "net/proxy_resolution/proxy_resolution_service.h"
|
#include "base/optional.h"
|
||||||
|
#include "mojo/public/cpp/bindings/binding.h"
|
||||||
|
#include "services/network/public/mojom/proxy_lookup_client.mojom.h"
|
||||||
#include "url/gurl.h"
|
#include "url/gurl.h"
|
||||||
|
|
||||||
namespace net {
|
|
||||||
class URLRequestContextGetter;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
class AtomBrowserContext;
|
class AtomBrowserContext;
|
||||||
|
|
||||||
class ResolveProxyHelper
|
class ResolveProxyHelper
|
||||||
: public base::RefCountedThreadSafe<ResolveProxyHelper> {
|
: public base::RefCountedThreadSafe<ResolveProxyHelper>,
|
||||||
|
network::mojom::ProxyLookupClient {
|
||||||
public:
|
public:
|
||||||
using ResolveProxyCallback = base::Callback<void(std::string)>;
|
using ResolveProxyCallback = base::Callback<void(std::string)>;
|
||||||
|
|
||||||
|
@ -29,6 +28,9 @@ class ResolveProxyHelper
|
||||||
|
|
||||||
void ResolveProxy(const GURL& url, const ResolveProxyCallback& callback);
|
void ResolveProxy(const GURL& url, const ResolveProxyCallback& callback);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
~ResolveProxyHelper() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class base::RefCountedThreadSafe<ResolveProxyHelper>;
|
friend class base::RefCountedThreadSafe<ResolveProxyHelper>;
|
||||||
// A PendingRequest is a resolve request that is in progress, or queued.
|
// A PendingRequest is a resolve request that is in progress, or queued.
|
||||||
|
@ -47,18 +49,22 @@ class ResolveProxyHelper
|
||||||
DISALLOW_COPY_AND_ASSIGN(PendingRequest);
|
DISALLOW_COPY_AND_ASSIGN(PendingRequest);
|
||||||
};
|
};
|
||||||
|
|
||||||
~ResolveProxyHelper();
|
|
||||||
|
|
||||||
// Starts the first pending request.
|
// Starts the first pending request.
|
||||||
void StartPendingRequest();
|
void StartPendingRequest();
|
||||||
void StartPendingRequestInIO(const GURL& url);
|
|
||||||
void OnProxyResolveComplete(int result);
|
|
||||||
void SendProxyResult(const std::string& proxy);
|
|
||||||
|
|
||||||
net::ProxyInfo proxy_info_;
|
// network::mojom::ProxyLookupClient implementation.
|
||||||
|
void OnProxyLookupComplete(
|
||||||
|
const base::Optional<net::ProxyInfo>& proxy_info) override;
|
||||||
|
|
||||||
|
// Self-reference. Owned as long as there's an outstanding proxy lookup.
|
||||||
|
scoped_refptr<ResolveProxyHelper> owned_self_;
|
||||||
|
|
||||||
std::deque<PendingRequest> pending_requests_;
|
std::deque<PendingRequest> pending_requests_;
|
||||||
scoped_refptr<net::URLRequestContextGetter> context_getter_;
|
// Binding for the currently in-progress request, if any.
|
||||||
scoped_refptr<base::SingleThreadTaskRunner> original_thread_;
|
mojo::Binding<network::mojom::ProxyLookupClient> binding_;
|
||||||
|
|
||||||
|
// Weak Ref
|
||||||
|
AtomBrowserContext* browser_context_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
|
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue