2018-10-04 18:08:56 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/net/resolve_proxy_helper.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2018-10-29 13:15:52 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2023-02-03 11:43:42 +00:00
|
|
|
#include "base/functional/bind.h"
|
2018-10-29 13:15:52 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2024-07-15 08:46:24 +00:00
|
|
|
#include "content/public/browser/storage_partition.h"
|
2019-10-28 22:12:35 +00:00
|
|
|
#include "mojo/public/cpp/bindings/pending_remote.h"
|
2022-10-17 14:22:24 +00:00
|
|
|
#include "net/base/network_anonymization_key.h"
|
2018-10-29 13:15:52 +00:00
|
|
|
#include "net/proxy_resolution/proxy_info.h"
|
2024-07-15 08:46:24 +00:00
|
|
|
#include "services/network/public/mojom/network_context.mojom.h"
|
|
|
|
#include "shell/browser/electron_browser_context.h"
|
|
|
|
#include "shell/browser/net/system_network_context_manager.h"
|
2018-10-29 13:15:52 +00:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2024-07-15 08:46:24 +00:00
|
|
|
ResolveProxyHelper::ResolveProxyHelper(ElectronBrowserContext* browser_context)
|
|
|
|
: browser_context_(browser_context) {}
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
ResolveProxyHelper::~ResolveProxyHelper() {
|
2018-10-29 13:15:52 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
DCHECK(!owned_self_);
|
2019-10-28 22:12:35 +00:00
|
|
|
DCHECK(!receiver_.is_bound());
|
2018-10-04 18:08:56 +00:00
|
|
|
// Clear all pending requests if the ProxyService is still alive.
|
|
|
|
pending_requests_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveProxyHelper::ResolveProxy(const GURL& url,
|
2019-05-01 20:45:08 +00:00
|
|
|
ResolveProxyCallback callback) {
|
2018-10-29 13:15:52 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2018-10-04 18:08:56 +00:00
|
|
|
// Enqueue the pending request.
|
2019-09-13 14:26:59 +00:00
|
|
|
pending_requests_.emplace_back(url, std::move(callback));
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
// If nothing is in progress, start.
|
2019-10-28 22:12:35 +00:00
|
|
|
if (!receiver_.is_bound()) {
|
2018-10-29 13:15:52 +00:00
|
|
|
DCHECK_EQ(1u, pending_requests_.size());
|
2018-10-04 18:08:56 +00:00
|
|
|
StartPendingRequest();
|
2018-10-29 13:15:52 +00:00
|
|
|
}
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResolveProxyHelper::StartPendingRequest() {
|
2018-10-29 13:15:52 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2019-10-28 22:12:35 +00:00
|
|
|
DCHECK(!receiver_.is_bound());
|
2018-10-29 13:15:52 +00:00
|
|
|
DCHECK(!pending_requests_.empty());
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
// Start the request.
|
2019-10-28 22:12:35 +00:00
|
|
|
mojo::PendingRemote<network::mojom::ProxyLookupClient> proxy_lookup_client =
|
|
|
|
receiver_.BindNewPipeAndPassRemote();
|
|
|
|
receiver_.set_disconnect_handler(
|
2018-10-29 13:15:52 +00:00
|
|
|
base::BindOnce(&ResolveProxyHelper::OnProxyLookupComplete,
|
2024-01-10 22:23:35 +00:00
|
|
|
base::Unretained(this), net::ERR_ABORTED, std::nullopt));
|
2024-07-15 08:46:24 +00:00
|
|
|
network::mojom::NetworkContext* network_context = nullptr;
|
|
|
|
if (browser_context_) {
|
|
|
|
network_context =
|
|
|
|
browser_context_->GetDefaultStoragePartition()->GetNetworkContext();
|
|
|
|
} else {
|
|
|
|
DCHECK(SystemNetworkContextManager::GetInstance());
|
|
|
|
network_context = SystemNetworkContextManager::GetInstance()->GetContext();
|
|
|
|
}
|
|
|
|
CHECK(network_context);
|
|
|
|
network_context->LookUpProxyForURL(pending_requests_.front().url,
|
|
|
|
net::NetworkAnonymizationKey(),
|
|
|
|
std::move(proxy_lookup_client));
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 13:15:52 +00:00
|
|
|
void ResolveProxyHelper::OnProxyLookupComplete(
|
2019-01-24 14:57:08 +00:00
|
|
|
int32_t net_error,
|
2024-01-10 22:23:35 +00:00
|
|
|
const std::optional<net::ProxyInfo>& proxy_info) {
|
2018-10-29 13:15:52 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2018-10-04 18:08:56 +00:00
|
|
|
DCHECK(!pending_requests_.empty());
|
|
|
|
|
2019-10-28 22:12:35 +00:00
|
|
|
receiver_.reset();
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2018-10-29 13:15:52 +00:00
|
|
|
// Clear the current (completed) request.
|
|
|
|
PendingRequest completed_request = std::move(pending_requests_.front());
|
|
|
|
pending_requests_.pop_front();
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2018-10-29 13:15:52 +00:00
|
|
|
std::string proxy;
|
|
|
|
if (proxy_info)
|
|
|
|
proxy = proxy_info->ToPacString();
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
if (!completed_request.callback.is_null())
|
2019-05-01 20:45:08 +00:00
|
|
|
std::move(completed_request.callback).Run(proxy);
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
// Start the next request.
|
|
|
|
if (!pending_requests_.empty())
|
|
|
|
StartPendingRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolveProxyHelper::PendingRequest::PendingRequest(
|
|
|
|
const GURL& url,
|
2019-05-01 20:45:08 +00:00
|
|
|
ResolveProxyCallback callback)
|
|
|
|
: url(url), callback(std::move(callback)) {}
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
ResolveProxyHelper::PendingRequest::PendingRequest(
|
2022-01-10 22:31:39 +00:00
|
|
|
ResolveProxyHelper::PendingRequest&& pending_request) noexcept = default;
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
ResolveProxyHelper::PendingRequest::~PendingRequest() noexcept = default;
|
|
|
|
|
2020-04-13 23:39:26 +00:00
|
|
|
ResolveProxyHelper::PendingRequest&
|
|
|
|
ResolveProxyHelper::PendingRequest::operator=(
|
|
|
|
ResolveProxyHelper::PendingRequest&& pending_request) noexcept = default;
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|