chore: remove pre network service classes from shell/browser/net (#19644)
* refactor: rm IOThread class * chore: rm expose-net-observer-api.patch * chore: rm unused shell/browser/net/ classes * chore: mv CertVerifierClient to separate header * chore: rm url_request_context_getter references
This commit is contained in:
parent
9c7a216814
commit
eebea63bed
31 changed files with 137 additions and 2196 deletions
|
@ -168,8 +168,6 @@ filenames = {
|
|||
"shell/browser/common_web_contents_delegate.h",
|
||||
"shell/browser/cookie_change_notifier.cc",
|
||||
"shell/browser/cookie_change_notifier.h",
|
||||
"shell/browser/io_thread.cc",
|
||||
"shell/browser/io_thread.h",
|
||||
"shell/browser/javascript_environment.cc",
|
||||
"shell/browser/javascript_environment.h",
|
||||
"shell/browser/lib/bluetooth_chooser.cc",
|
||||
|
@ -215,28 +213,18 @@ filenames = {
|
|||
"shell/browser/media/media_stream_devices_controller.h",
|
||||
"shell/browser/net/asar/asar_url_loader.cc",
|
||||
"shell/browser/net/asar/asar_url_loader.h",
|
||||
"shell/browser/net/asar/url_request_asar_job.cc",
|
||||
"shell/browser/net/asar/url_request_asar_job.h",
|
||||
"shell/browser/net/atom_cert_verifier.cc",
|
||||
"shell/browser/net/atom_cert_verifier.h",
|
||||
"shell/browser/net/atom_network_delegate.cc",
|
||||
"shell/browser/net/atom_network_delegate.h",
|
||||
"shell/browser/net/atom_url_loader_factory.cc",
|
||||
"shell/browser/net/atom_url_loader_factory.h",
|
||||
"shell/browser/net/atom_url_request_job_factory.cc",
|
||||
"shell/browser/net/atom_url_request_job_factory.h",
|
||||
"shell/browser/net/cert_verifier_client.cc",
|
||||
"shell/browser/net/cert_verifier_client.h",
|
||||
"shell/browser/net/proxying_url_loader_factory.cc",
|
||||
"shell/browser/net/proxying_url_loader_factory.h",
|
||||
"shell/browser/net/js_asker.cc",
|
||||
"shell/browser/net/js_asker.h",
|
||||
"shell/browser/net/network_context_service_factory.cc",
|
||||
"shell/browser/net/network_context_service_factory.h",
|
||||
"shell/browser/net/network_context_service.cc",
|
||||
"shell/browser/net/network_context_service.h",
|
||||
"shell/browser/net/node_stream_loader.cc",
|
||||
"shell/browser/net/node_stream_loader.h",
|
||||
"shell/browser/net/require_ct_delegate.cc",
|
||||
"shell/browser/net/require_ct_delegate.h",
|
||||
"shell/browser/net/resolve_proxy_helper.cc",
|
||||
"shell/browser/net/resolve_proxy_helper.h",
|
||||
"shell/browser/net/system_network_context_manager.cc",
|
||||
|
|
|
@ -42,7 +42,6 @@ allow_nested_error_trackers.patch
|
|||
blink_initialization_order.patch
|
||||
ssl_security_state_tab_helper.patch
|
||||
exclude-a-few-test-files-from-build.patch
|
||||
expose-net-observer-api.patch
|
||||
desktop_media_list.patch
|
||||
proxy_config_monitor.patch
|
||||
gritsettings_resource_ids.patch
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Jeremy Apthorp <jeremya@chromium.org>
|
||||
Date: Thu, 18 Oct 2018 17:06:56 -0700
|
||||
Subject: expose-net-observer-api.patch
|
||||
|
||||
Expose URLRequestContextGetter::{Add,Remove}Observer.
|
||||
This patch should be removed once we switch to using the net service (cc @robo)
|
||||
|
||||
diff --git a/net/url_request/url_request_context_getter.h b/net/url_request/url_request_context_getter.h
|
||||
index def76d3902f30ae1384ae5f629fa116b3975fa86..e5ee0bfa93f497f18bdb3ad7cd51a6726dc87f3e 100644
|
||||
--- a/net/url_request/url_request_context_getter.h
|
||||
+++ b/net/url_request/url_request_context_getter.h
|
||||
@@ -79,11 +79,16 @@ class NET_EXPORT URLRequestContextGetter
|
||||
friend class web::NetworkContextOwner;
|
||||
#endif // defined(OS_IOS)
|
||||
|
||||
+ // NOTE(nornagon/robo): electron currently depends on this deprecated API
|
||||
+ // from net/, but will be removing it soon when we switch to using the
|
||||
+ // network service.
|
||||
+ public:
|
||||
// Adds / removes an observer to watch for shutdown of |this|'s context. Must
|
||||
// only be called on network thread. May not be called once
|
||||
// GetURLRequestContext() starts returning nullptr.
|
||||
void AddObserver(URLRequestContextGetterObserver* observer);
|
||||
void RemoveObserver(URLRequestContextGetterObserver* observer);
|
||||
+ private:
|
||||
|
||||
// OnDestruct is used to ensure deletion on the thread on which the request
|
||||
// IO happens.
|
|
@ -18,8 +18,6 @@
|
|||
#include "net/cookies/canonical_cookie.h"
|
||||
#include "net/cookies/cookie_store.h"
|
||||
#include "net/cookies/cookie_util.h"
|
||||
#include "net/url_request/url_request_context.h"
|
||||
#include "net/url_request/url_request_context_getter.h"
|
||||
#include "shell/browser/atom_browser_context.h"
|
||||
#include "shell/browser/cookie_change_notifier.h"
|
||||
#include "shell/common/native_mate_converters/callback.h"
|
||||
|
|
|
@ -62,8 +62,6 @@ void Initialize(v8::Local<v8::Object> exports,
|
|||
dict.Set("net", Net::Create(isolate));
|
||||
dict.Set("Net",
|
||||
Net::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
|
||||
dict.Set("isNetworkServiceEnabled",
|
||||
base::FeatureList::IsEnabled(network::features::kNetworkService));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
#include "native_mate/converter.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "native_mate/handle.h"
|
||||
#include "net/url_request/url_request_context_getter.h"
|
||||
#include "shell/browser/atom_browser_context.h"
|
||||
#include "shell/browser/net/system_network_context_manager.h"
|
||||
#include "shell/common/native_mate_converters/callback.h"
|
||||
|
|
|
@ -36,10 +36,6 @@
|
|||
#include "net/http/http_auth_handler_factory.h"
|
||||
#include "net/http/http_auth_preferences.h"
|
||||
#include "net/http/http_cache.h"
|
||||
#include "net/http/http_transaction_factory.h"
|
||||
#include "net/url_request/static_http_user_agent_settings.h"
|
||||
#include "net/url_request/url_request_context.h"
|
||||
#include "net/url_request/url_request_context_getter.h"
|
||||
#include "services/network/network_service.h"
|
||||
#include "services/network/public/cpp/features.h"
|
||||
#include "shell/browser/api/atom_api_cookies.h"
|
||||
|
@ -52,8 +48,7 @@
|
|||
#include "shell/browser/atom_permission_manager.h"
|
||||
#include "shell/browser/browser.h"
|
||||
#include "shell/browser/media/media_device_id_salt.h"
|
||||
#include "shell/browser/net/atom_cert_verifier.h"
|
||||
#include "shell/browser/net/system_network_context_manager.h"
|
||||
#include "shell/browser/net/cert_verifier_client.h"
|
||||
#include "shell/browser/session_preferences.h"
|
||||
#include "shell/common/native_mate_converters/callback.h"
|
||||
#include "shell/common/native_mate_converters/content_converter.h"
|
||||
|
@ -142,19 +137,6 @@ struct Converter<ClearStorageDataOptions> {
|
|||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<electron::VerifyRequestParams> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
electron::VerifyRequestParams val) {
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||
dict.Set("hostname", val.hostname);
|
||||
dict.Set("certificate", val.certificate);
|
||||
dict.Set("verificationResult", val.default_result);
|
||||
dict.Set("errorCode", val.error_code);
|
||||
return dict.GetHandle();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
namespace electron {
|
||||
|
@ -403,51 +385,9 @@ void Session::DisableNetworkEmulation() {
|
|||
network_emulation_token_, network::mojom::NetworkConditions::New());
|
||||
}
|
||||
|
||||
class ElectronCertVerifierClient : public network::mojom::CertVerifierClient {
|
||||
public:
|
||||
using CertVerifyProc =
|
||||
base::RepeatingCallback<void(const VerifyRequestParams& request,
|
||||
base::RepeatingCallback<void(int)>)>;
|
||||
explicit ElectronCertVerifierClient(CertVerifyProc proc)
|
||||
: cert_verify_proc_(proc) {}
|
||||
~ElectronCertVerifierClient() override = default;
|
||||
|
||||
// network::mojom::CertVerifierClient
|
||||
void Verify(int default_error,
|
||||
const net::CertVerifyResult& default_result,
|
||||
const scoped_refptr<net::X509Certificate>& certificate,
|
||||
const std::string& hostname,
|
||||
int flags,
|
||||
const base::Optional<std::string>& ocsp_response,
|
||||
VerifyCallback callback) override {
|
||||
VerifyRequestParams params;
|
||||
params.hostname = hostname;
|
||||
params.default_result = net::ErrorToString(default_error);
|
||||
params.error_code = default_error;
|
||||
params.certificate = certificate;
|
||||
cert_verify_proc_.Run(
|
||||
params,
|
||||
base::AdaptCallbackForRepeating(base::BindOnce(
|
||||
[](VerifyCallback callback, const net::CertVerifyResult& result,
|
||||
int err) { std::move(callback).Run(err, result); },
|
||||
std::move(callback), default_result)));
|
||||
}
|
||||
|
||||
private:
|
||||
CertVerifyProc cert_verify_proc_;
|
||||
};
|
||||
|
||||
void WrapVerifyProc(
|
||||
base::RepeatingCallback<void(const VerifyRequestParams& request,
|
||||
base::RepeatingCallback<void(int)>)> proc,
|
||||
const VerifyRequestParams& request,
|
||||
base::OnceCallback<void(int)> cb) {
|
||||
proc.Run(request, base::AdaptCallbackForRepeating(std::move(cb)));
|
||||
}
|
||||
|
||||
void Session::SetCertVerifyProc(v8::Local<v8::Value> val,
|
||||
mate::Arguments* args) {
|
||||
ElectronCertVerifierClient::CertVerifyProc proc;
|
||||
CertVerifierClient::CertVerifyProc proc;
|
||||
if (!(val->IsNull() || mate::ConvertFromV8(args->isolate(), val, &proc))) {
|
||||
args->ThrowError("Must pass null or function");
|
||||
return;
|
||||
|
@ -455,7 +395,7 @@ void Session::SetCertVerifyProc(v8::Local<v8::Value> val,
|
|||
|
||||
network::mojom::CertVerifierClientPtr cert_verifier_client;
|
||||
if (proc) {
|
||||
mojo::MakeStrongBinding(std::make_unique<ElectronCertVerifierClient>(proc),
|
||||
mojo::MakeStrongBinding(std::make_unique<CertVerifierClient>(proc),
|
||||
mojo::MakeRequest(&cert_verifier_client));
|
||||
}
|
||||
content::BrowserContext::GetDefaultStoragePartition(browser_context_.get())
|
||||
|
@ -534,9 +474,11 @@ v8::Local<v8::Promise> Session::ClearAuthCache() {
|
|||
}
|
||||
|
||||
void Session::AllowNTLMCredentialsForDomains(const std::string& domains) {
|
||||
auto auth_params = CreateHttpAuthDynamicParams();
|
||||
auth_params->server_allowlist = domains;
|
||||
content::GetNetworkService()->ConfigureHttpAuthPrefs(std::move(auth_params));
|
||||
network::mojom::HttpAuthDynamicParamsPtr auth_dynamic_params =
|
||||
network::mojom::HttpAuthDynamicParams::New();
|
||||
auth_dynamic_params->server_allowlist = domains;
|
||||
content::GetNetworkService()->ConfigureHttpAuthPrefs(
|
||||
std::move(auth_dynamic_params));
|
||||
}
|
||||
|
||||
void Session::SetUserAgent(const std::string& user_agent,
|
||||
|
|
|
@ -47,7 +47,6 @@
|
|||
#include "native_mate/converter.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
#include "net/url_request/url_request_context.h"
|
||||
#include "shell/browser/api/atom_api_browser_window.h"
|
||||
#include "shell/browser/api/atom_api_debugger.h"
|
||||
#include "shell/browser/api/atom_api_session.h"
|
||||
|
@ -60,7 +59,6 @@
|
|||
#include "shell/browser/child_web_contents_tracker.h"
|
||||
#include "shell/browser/lib/bluetooth_chooser.h"
|
||||
#include "shell/browser/native_window.h"
|
||||
#include "shell/browser/net/atom_network_delegate.h"
|
||||
#include "shell/browser/session_preferences.h"
|
||||
#include "shell/browser/ui/drag_util.h"
|
||||
#include "shell/browser/ui/inspectable_web_contents.h"
|
||||
|
|
|
@ -60,12 +60,12 @@
|
|||
#include "shell/browser/atom_speech_recognition_manager_delegate.h"
|
||||
#include "shell/browser/child_web_contents_tracker.h"
|
||||
#include "shell/browser/font_defaults.h"
|
||||
#include "shell/browser/io_thread.h"
|
||||
#include "shell/browser/media/media_capture_devices_dispatcher.h"
|
||||
#include "shell/browser/native_window.h"
|
||||
#include "shell/browser/net/network_context_service.h"
|
||||
#include "shell/browser/net/network_context_service_factory.h"
|
||||
#include "shell/browser/net/proxying_url_loader_factory.h"
|
||||
#include "shell/browser/net/system_network_context_manager.h"
|
||||
#include "shell/browser/notifications/notification_presenter.h"
|
||||
#include "shell/browser/notifications/platform_notification_service.h"
|
||||
#include "shell/browser/session_preferences.h"
|
||||
|
|
|
@ -93,14 +93,6 @@ void BrowserProcessImpl::PreCreateThreads() {
|
|||
// this can be created on first use.
|
||||
if (!SystemNetworkContextManager::GetInstance())
|
||||
SystemNetworkContextManager::CreateInstance(local_state_.get());
|
||||
|
||||
// Manage global state of net and other IO thread related.
|
||||
io_thread_ =
|
||||
std::make_unique<IOThread>(SystemNetworkContextManager::GetInstance());
|
||||
}
|
||||
|
||||
void BrowserProcessImpl::PostDestroyThreads() {
|
||||
io_thread_.reset();
|
||||
}
|
||||
|
||||
void BrowserProcessImpl::PostMainMessageLoopRun() {
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
#include "components/prefs/value_map_pref_store.h"
|
||||
#include "printing/buildflags/buildflags.h"
|
||||
#include "services/network/public/cpp/shared_url_loader_factory.h"
|
||||
#include "shell/browser/io_thread.h"
|
||||
#include "shell/browser/net/system_network_context_manager.h"
|
||||
|
||||
namespace printing {
|
||||
|
@ -41,7 +40,7 @@ class BrowserProcessImpl : public BrowserProcess {
|
|||
|
||||
void PostEarlyInitialization();
|
||||
void PreCreateThreads();
|
||||
void PostDestroyThreads();
|
||||
void PostDestroyThreads() {}
|
||||
void PostMainMessageLoopRun();
|
||||
|
||||
void EndSession() override {}
|
||||
|
@ -106,14 +105,11 @@ class BrowserProcessImpl : public BrowserProcess {
|
|||
printing::PrintJobManager* print_job_manager() override;
|
||||
StartupData* startup_data() override;
|
||||
|
||||
IOThread* io_thread() const { return io_thread_.get(); }
|
||||
|
||||
private:
|
||||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
std::unique_ptr<printing::PrintJobManager> print_job_manager_;
|
||||
#endif
|
||||
std::unique_ptr<PrefService> local_state_;
|
||||
std::unique_ptr<IOThread> io_thread_;
|
||||
std::string locale_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl);
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
// Copyright (c) 2017 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/io_thread.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "components/net_log/chrome_net_log.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "content/public/browser/network_service_instance.h"
|
||||
#include "net/cert/caching_cert_verifier.h"
|
||||
#include "net/cert/cert_verifier.h"
|
||||
#include "net/cert/cert_verify_proc.h"
|
||||
#include "net/cert/multi_threaded_cert_verifier.h"
|
||||
#include "net/log/net_log_util.h"
|
||||
#include "net/proxy_resolution/proxy_resolution_service.h"
|
||||
#include "net/url_request/url_request_context.h"
|
||||
#include "services/network/network_service.h"
|
||||
#include "services/network/public/cpp/features.h"
|
||||
#include "services/network/public/cpp/network_switches.h"
|
||||
#include "services/network/public/mojom/net_log.mojom.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
IOThread::IOThread(
|
||||
SystemNetworkContextManager* system_network_context_manager) {
|
||||
BrowserThread::SetIOThreadDelegate(this);
|
||||
|
||||
system_network_context_manager->SetUp(
|
||||
&network_context_request_, &network_context_params_,
|
||||
&http_auth_static_params_, &http_auth_dynamic_params_);
|
||||
}
|
||||
|
||||
IOThread::~IOThread() {
|
||||
BrowserThread::SetIOThreadDelegate(nullptr);
|
||||
}
|
||||
|
||||
void IOThread::Init() {}
|
||||
|
||||
void IOThread::CleanUp() {}
|
|
@ -1,68 +0,0 @@
|
|||
// Copyright (c) 2017 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_IO_THREAD_H_
|
||||
#define SHELL_BROWSER_IO_THREAD_H_
|
||||
|
||||
#include <memory>
|
||||
#include <set>
|
||||
|
||||
#include "base/macros.h"
|
||||
#include "base/synchronization/lock.h"
|
||||
#include "content/public/browser/browser_thread_delegate.h"
|
||||
#include "services/network/public/mojom/network_service.mojom.h"
|
||||
#include "shell/browser/net/system_network_context_manager.h"
|
||||
|
||||
namespace electron {
|
||||
class URLRequestContextGetter;
|
||||
}
|
||||
|
||||
namespace net {
|
||||
class URLRequestContext;
|
||||
}
|
||||
|
||||
class IOThread : public content::BrowserThreadDelegate {
|
||||
public:
|
||||
explicit IOThread(
|
||||
SystemNetworkContextManager* system_network_context_manager);
|
||||
~IOThread() override;
|
||||
|
||||
void RegisterURLRequestContextGetter(
|
||||
electron::URLRequestContextGetter* getter);
|
||||
void DeregisterURLRequestContextGetter(
|
||||
electron::URLRequestContextGetter* getter);
|
||||
|
||||
protected:
|
||||
// BrowserThreadDelegate Implementation, runs on the IO thread.
|
||||
void Init() override;
|
||||
void CleanUp() override;
|
||||
|
||||
private:
|
||||
// When the network service is disabled, this holds on to a
|
||||
// content::NetworkContext class that owns |system_request_context_|.
|
||||
std::unique_ptr<network::mojom::NetworkContext> system_network_context_;
|
||||
net::URLRequestContext* system_request_context_;
|
||||
|
||||
// These are set on the UI thread, and then consumed during initialization on
|
||||
// the IO thread.
|
||||
network::mojom::NetworkContextRequest network_context_request_;
|
||||
network::mojom::NetworkContextParamsPtr network_context_params_;
|
||||
|
||||
// Initial HTTP auth configuration used when setting up the NetworkService on
|
||||
// the IO Thread. Future updates are sent using the NetworkService mojo
|
||||
// interface, but initial state needs to be set non-racily.
|
||||
network::mojom::HttpAuthStaticParamsPtr http_auth_static_params_;
|
||||
network::mojom::HttpAuthDynamicParamsPtr http_auth_dynamic_params_;
|
||||
|
||||
// |lock_| protects access to |request_context_getters_|.
|
||||
base::Lock lock_;
|
||||
|
||||
// List of all request contexts that needs to be notified when
|
||||
// IO thread is shutting down.
|
||||
std::set<electron::URLRequestContextGetter*> request_context_getters_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(IOThread);
|
||||
};
|
||||
|
||||
#endif // SHELL_BROWSER_IO_THREAD_H_
|
|
@ -1,337 +0,0 @@
|
|||
// Copyright (c) 2014 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/net/asar/url_request_asar_job.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/files/file_util.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "base/synchronization/lock.h"
|
||||
#include "base/task_runner.h"
|
||||
#include "base/threading/thread_task_runner_handle.h"
|
||||
#include "net/base/file_stream.h"
|
||||
#include "net/base/filename_util.h"
|
||||
#include "net/base/io_buffer.h"
|
||||
#include "net/base/load_flags.h"
|
||||
#include "net/base/mime_util.h"
|
||||
#include "net/base/net_errors.h"
|
||||
#include "net/filter/gzip_source_stream.h"
|
||||
#include "net/http/http_util.h"
|
||||
#include "net/url_request/url_request_status.h"
|
||||
#include "shell/common/asar/archive.h"
|
||||
#include "shell/common/asar/asar_util.h"
|
||||
#include "shell/common/atom_constants.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
#include "base/win/shortcut.h"
|
||||
#endif
|
||||
|
||||
namespace asar {
|
||||
|
||||
URLRequestAsarJob::FileMetaInfo::FileMetaInfo() = default;
|
||||
|
||||
URLRequestAsarJob::URLRequestAsarJob(net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate)
|
||||
: net::URLRequestJob(request, network_delegate), weak_ptr_factory_(this) {}
|
||||
|
||||
URLRequestAsarJob::~URLRequestAsarJob() {}
|
||||
|
||||
void URLRequestAsarJob::Initialize(
|
||||
const scoped_refptr<base::TaskRunner> file_task_runner,
|
||||
const base::FilePath& file_path) {
|
||||
// Determine whether it is an asar file.
|
||||
base::FilePath asar_path, relative_path;
|
||||
if (!GetAsarArchivePath(file_path, &asar_path, &relative_path)) {
|
||||
InitializeFileJob(file_task_runner, file_path);
|
||||
return;
|
||||
}
|
||||
|
||||
std::shared_ptr<Archive> archive = GetOrCreateAsarArchive(asar_path);
|
||||
Archive::FileInfo file_info;
|
||||
if (!archive || !archive->GetFileInfo(relative_path, &file_info)) {
|
||||
type_ = JobType::kError;
|
||||
return;
|
||||
}
|
||||
|
||||
if (file_info.unpacked) {
|
||||
base::FilePath real_path;
|
||||
archive->CopyFileOut(relative_path, &real_path);
|
||||
InitializeFileJob(file_task_runner, real_path);
|
||||
return;
|
||||
}
|
||||
|
||||
InitializeAsarJob(file_task_runner, archive, relative_path, file_info);
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::InitializeAsarJob(
|
||||
const scoped_refptr<base::TaskRunner> file_task_runner,
|
||||
std::shared_ptr<Archive> archive,
|
||||
const base::FilePath& file_path,
|
||||
const Archive::FileInfo& file_info) {
|
||||
type_ = JobType::kAsar;
|
||||
file_task_runner_ = file_task_runner;
|
||||
stream_.reset(new net::FileStream(file_task_runner_));
|
||||
archive_ = archive;
|
||||
file_path_ = file_path;
|
||||
file_info_ = file_info;
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::InitializeFileJob(
|
||||
const scoped_refptr<base::TaskRunner> file_task_runner,
|
||||
const base::FilePath& file_path) {
|
||||
type_ = JobType::kFile;
|
||||
file_task_runner_ = file_task_runner;
|
||||
stream_.reset(new net::FileStream(file_task_runner_));
|
||||
file_path_ = file_path;
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::Start() {
|
||||
if (type_ == JobType::kAsar || type_ == JobType::kFile) {
|
||||
auto* meta_info = new FileMetaInfo();
|
||||
if (type_ == JobType::kAsar) {
|
||||
meta_info->file_path = archive_->path();
|
||||
meta_info->file_exists = true;
|
||||
meta_info->is_directory = false;
|
||||
meta_info->file_size = file_info_.size;
|
||||
}
|
||||
file_task_runner_->PostTaskAndReply(
|
||||
FROM_HERE,
|
||||
base::BindOnce(&URLRequestAsarJob::FetchMetaInfo, file_path_, type_,
|
||||
base::Unretained(meta_info)),
|
||||
base::BindOnce(&URLRequestAsarJob::DidFetchMetaInfo,
|
||||
weak_ptr_factory_.GetWeakPtr(), base::Owned(meta_info)));
|
||||
} else {
|
||||
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
||||
FROM_HERE, base::BindOnce(&URLRequestAsarJob::DidOpen,
|
||||
weak_ptr_factory_.GetWeakPtr(),
|
||||
net::ERR_FILE_NOT_FOUND));
|
||||
}
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::Kill() {
|
||||
stream_.reset();
|
||||
weak_ptr_factory_.InvalidateWeakPtrs();
|
||||
|
||||
URLRequestJob::Kill();
|
||||
}
|
||||
|
||||
int URLRequestAsarJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
|
||||
if (remaining_bytes_ < dest_size)
|
||||
dest_size = static_cast<int>(remaining_bytes_);
|
||||
|
||||
// If we should copy zero bytes because |remaining_bytes_| is zero, short
|
||||
// circuit here.
|
||||
if (!dest_size)
|
||||
return 0;
|
||||
|
||||
int rv = stream_->Read(
|
||||
dest, dest_size,
|
||||
base::BindOnce(&URLRequestAsarJob::DidRead,
|
||||
weak_ptr_factory_.GetWeakPtr(), WrapRefCounted(dest)));
|
||||
if (rv >= 0) {
|
||||
remaining_bytes_ -= rv;
|
||||
DCHECK_GE(remaining_bytes_, 0);
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool URLRequestAsarJob::IsRedirectResponse(GURL* location,
|
||||
int* http_status_code,
|
||||
bool* insecure_scheme_was_upgraded) {
|
||||
if (type_ != JobType::kFile)
|
||||
return false;
|
||||
#if defined(OS_WIN)
|
||||
// Follow a Windows shortcut.
|
||||
// We just resolve .lnk file, ignore others.
|
||||
if (!base::LowerCaseEqualsASCII(file_path_.Extension(), ".lnk"))
|
||||
return false;
|
||||
|
||||
base::FilePath new_path = file_path_;
|
||||
bool resolved;
|
||||
resolved = base::win::ResolveShortcut(new_path, &new_path, NULL);
|
||||
|
||||
// If shortcut is not resolved succesfully, do not redirect.
|
||||
if (!resolved)
|
||||
return false;
|
||||
|
||||
*location = net::FilePathToFileURL(new_path);
|
||||
*http_status_code = 301;
|
||||
*insecure_scheme_was_upgraded = false;
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::unique_ptr<net::SourceStream> URLRequestAsarJob::SetUpSourceStream() {
|
||||
std::unique_ptr<net::SourceStream> source =
|
||||
net::URLRequestJob::SetUpSourceStream();
|
||||
// Bug 9936 - .svgz files needs to be decompressed.
|
||||
return base::LowerCaseEqualsASCII(file_path_.Extension(), ".svgz")
|
||||
? net::GzipSourceStream::Create(std::move(source),
|
||||
net::SourceStream::TYPE_GZIP)
|
||||
: std::move(source);
|
||||
}
|
||||
|
||||
bool URLRequestAsarJob::GetMimeType(std::string* mime_type) const {
|
||||
if (meta_info_.mime_type_result) {
|
||||
*mime_type = meta_info_.mime_type;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::SetExtraRequestHeaders(
|
||||
const net::HttpRequestHeaders& headers) {
|
||||
std::string range_header;
|
||||
if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) {
|
||||
// This job only cares about the Range header. This method stashes the value
|
||||
// for later use in DidOpen(), which is responsible for some of the range
|
||||
// validation as well. NotifyStartError is not legal to call here since
|
||||
// the job has not started.
|
||||
std::vector<net::HttpByteRange> ranges;
|
||||
if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) {
|
||||
if (ranges.size() == 1) {
|
||||
byte_range_ = ranges[0];
|
||||
} else {
|
||||
range_parse_result_ = net::ERR_REQUEST_RANGE_NOT_SATISFIABLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int URLRequestAsarJob::GetResponseCode() const {
|
||||
// Request Job gets created only if path exists.
|
||||
return 200;
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::GetResponseInfo(net::HttpResponseInfo* info) {
|
||||
std::string status("HTTP/1.1 200 OK");
|
||||
auto* headers = new net::HttpResponseHeaders(status);
|
||||
|
||||
headers->AddHeader(electron::kCORSHeader);
|
||||
info->headers = headers;
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::FetchMetaInfo(const base::FilePath& file_path,
|
||||
JobType type,
|
||||
FileMetaInfo* meta_info) {
|
||||
if (type == JobType::kFile) {
|
||||
base::File::Info file_info;
|
||||
meta_info->file_exists = base::GetFileInfo(file_path, &file_info);
|
||||
if (meta_info->file_exists) {
|
||||
meta_info->file_path = file_path;
|
||||
meta_info->file_size = file_info.size;
|
||||
meta_info->is_directory = file_info.is_directory;
|
||||
}
|
||||
}
|
||||
|
||||
// We use GetWellKnownMimeTypeFromExtension() to ensure that configurations
|
||||
// that may have been set by other programs on a user's machine don't affect
|
||||
// the mime type returned (in particular, JS should always be
|
||||
// (application/javascript). See https://crbug.com/797712. Using an accurate
|
||||
// mime type is necessary at least for modules and sw, which enforce strict
|
||||
// mime type requirements.
|
||||
// TODO(deepak1556): Revert this when sw support is removed for file scheme.
|
||||
base::FilePath::StringType file_extension = file_path.Extension();
|
||||
if (file_extension.empty()) {
|
||||
meta_info->mime_type_result = false;
|
||||
} else {
|
||||
meta_info->mime_type_result = net::GetWellKnownMimeTypeFromExtension(
|
||||
file_extension.substr(1), &meta_info->mime_type);
|
||||
}
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::DidFetchMetaInfo(const FileMetaInfo* meta_info) {
|
||||
meta_info_ = *meta_info;
|
||||
if (!meta_info_.file_exists || meta_info_.is_directory) {
|
||||
DidOpen(net::ERR_FILE_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
|
||||
int flags =
|
||||
base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
|
||||
int rv = stream_->Open(meta_info_.file_path, flags,
|
||||
base::BindOnce(&URLRequestAsarJob::DidOpen,
|
||||
weak_ptr_factory_.GetWeakPtr()));
|
||||
if (rv != net::ERR_IO_PENDING)
|
||||
DidOpen(rv);
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::DidOpen(int result) {
|
||||
if (result != net::OK) {
|
||||
NotifyStartError(
|
||||
net::URLRequestStatus(net::URLRequestStatus::FAILED, result));
|
||||
return;
|
||||
}
|
||||
|
||||
if (range_parse_result_ != net::OK) {
|
||||
NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
|
||||
range_parse_result_));
|
||||
return;
|
||||
}
|
||||
|
||||
int64_t file_size, read_offset;
|
||||
if (type_ == JobType::kAsar) {
|
||||
file_size = file_info_.size;
|
||||
read_offset = file_info_.offset;
|
||||
} else {
|
||||
file_size = meta_info_.file_size;
|
||||
read_offset = 0;
|
||||
}
|
||||
|
||||
if (!byte_range_.ComputeBounds(file_size)) {
|
||||
NotifyStartError(net::URLRequestStatus(
|
||||
net::URLRequestStatus::FAILED, net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
|
||||
return;
|
||||
}
|
||||
|
||||
remaining_bytes_ =
|
||||
byte_range_.last_byte_position() - byte_range_.first_byte_position() + 1;
|
||||
seek_offset_ = byte_range_.first_byte_position() + read_offset;
|
||||
|
||||
if (remaining_bytes_ > 0 && seek_offset_ != 0) {
|
||||
int rv = stream_->Seek(seek_offset_,
|
||||
base::BindOnce(&URLRequestAsarJob::DidSeek,
|
||||
weak_ptr_factory_.GetWeakPtr()));
|
||||
if (rv != net::ERR_IO_PENDING) {
|
||||
// stream_->Seek() failed, so pass an intentionally erroneous value
|
||||
// into DidSeek().
|
||||
DidSeek(-1);
|
||||
}
|
||||
} else {
|
||||
// We didn't need to call stream_->Seek() at all, so we pass to DidSeek()
|
||||
// the value that would mean seek success. This way we skip the code
|
||||
// handling seek failure.
|
||||
DidSeek(seek_offset_);
|
||||
}
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::DidSeek(int64_t result) {
|
||||
if (result != seek_offset_) {
|
||||
NotifyStartError(net::URLRequestStatus(
|
||||
net::URLRequestStatus::FAILED, net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
|
||||
return;
|
||||
}
|
||||
set_expected_content_size(remaining_bytes_);
|
||||
NotifyHeadersComplete();
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::DidRead(scoped_refptr<net::IOBuffer> buf, int result) {
|
||||
if (result >= 0) {
|
||||
remaining_bytes_ -= result;
|
||||
DCHECK_GE(remaining_bytes_, 0);
|
||||
}
|
||||
|
||||
buf = nullptr;
|
||||
|
||||
ReadRawDataComplete(result);
|
||||
}
|
||||
|
||||
} // namespace asar
|
|
@ -1,137 +0,0 @@
|
|||
// Copyright (c) 2014 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_NET_ASAR_URL_REQUEST_ASAR_JOB_H_
|
||||
#define SHELL_BROWSER_NET_ASAR_URL_REQUEST_ASAR_JOB_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "net/http/http_byte_range.h"
|
||||
#include "net/url_request/url_request_job.h"
|
||||
#include "shell/browser/net/js_asker.h"
|
||||
#include "shell/common/asar/archive.h"
|
||||
|
||||
namespace base {
|
||||
class TaskRunner;
|
||||
}
|
||||
|
||||
namespace net {
|
||||
class FileStream;
|
||||
}
|
||||
|
||||
namespace asar {
|
||||
|
||||
// Createa a request job according to the file path.
|
||||
net::URLRequestJob* CreateJobFromPath(
|
||||
const base::FilePath& full_path,
|
||||
net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate,
|
||||
const scoped_refptr<base::TaskRunner> file_task_runner);
|
||||
|
||||
class URLRequestAsarJob : public net::URLRequestJob {
|
||||
public:
|
||||
URLRequestAsarJob(net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate);
|
||||
|
||||
void Initialize(const scoped_refptr<base::TaskRunner> file_task_runner,
|
||||
const base::FilePath& file_path);
|
||||
|
||||
protected:
|
||||
~URLRequestAsarJob() override;
|
||||
|
||||
void InitializeAsarJob(const scoped_refptr<base::TaskRunner> file_task_runner,
|
||||
std::shared_ptr<Archive> archive,
|
||||
const base::FilePath& file_path,
|
||||
const Archive::FileInfo& file_info);
|
||||
void InitializeFileJob(const scoped_refptr<base::TaskRunner> file_task_runner,
|
||||
const base::FilePath& file_path);
|
||||
|
||||
// net::URLRequestJob:
|
||||
void Start() override;
|
||||
void Kill() override;
|
||||
int ReadRawData(net::IOBuffer* buf, int buf_size) override;
|
||||
bool IsRedirectResponse(GURL* location,
|
||||
int* http_status_code,
|
||||
bool* insecure_scheme_was_upgraded) override;
|
||||
std::unique_ptr<net::SourceStream> SetUpSourceStream() override;
|
||||
bool GetMimeType(std::string* mime_type) const override;
|
||||
void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override;
|
||||
int GetResponseCode() const override;
|
||||
void GetResponseInfo(net::HttpResponseInfo* info) override;
|
||||
|
||||
private:
|
||||
// The type of this job.
|
||||
enum class JobType {
|
||||
kError,
|
||||
kAsar,
|
||||
kFile,
|
||||
};
|
||||
|
||||
// Meta information about the file. It's used as a member in the
|
||||
// URLRequestFileJob and also passed between threads because disk access is
|
||||
// necessary to obtain it.
|
||||
struct FileMetaInfo {
|
||||
// Size of the file.
|
||||
int64_t file_size = 0;
|
||||
// Mime type associated with the file.
|
||||
std::string mime_type;
|
||||
// Result returned from GetMimeTypeFromFile(), i.e. flag showing whether
|
||||
// obtaining of the mime type was successful.
|
||||
bool mime_type_result = false;
|
||||
// Flag showing whether the file exists.
|
||||
bool file_exists = false;
|
||||
// Flag showing whether the file name actually refers to a directory.
|
||||
bool is_directory = false;
|
||||
// Path to the file.
|
||||
base::FilePath file_path;
|
||||
|
||||
FileMetaInfo();
|
||||
};
|
||||
|
||||
// Fetches file info on a background thread.
|
||||
static void FetchMetaInfo(const base::FilePath& file_path,
|
||||
JobType type,
|
||||
FileMetaInfo* meta_info);
|
||||
|
||||
// Callback after fetching file info on a background thread.
|
||||
void DidFetchMetaInfo(const FileMetaInfo* meta_info);
|
||||
|
||||
// Callback after opening file on a background thread.
|
||||
void DidOpen(int result);
|
||||
|
||||
// Callback after seeking to the beginning of |byte_range_| in the file
|
||||
// on a background thread.
|
||||
void DidSeek(int64_t result);
|
||||
|
||||
// Callback after data is asynchronously read from the file into |buf|.
|
||||
void DidRead(scoped_refptr<net::IOBuffer> buf, int result);
|
||||
|
||||
JobType type_ = JobType::kError;
|
||||
|
||||
std::shared_ptr<Archive> archive_;
|
||||
base::FilePath file_path_;
|
||||
Archive::FileInfo file_info_;
|
||||
|
||||
std::unique_ptr<net::FileStream> stream_;
|
||||
FileMetaInfo meta_info_;
|
||||
scoped_refptr<base::TaskRunner> file_task_runner_;
|
||||
|
||||
net::HttpByteRange byte_range_;
|
||||
int64_t remaining_bytes_ = 0;
|
||||
int64_t seek_offset_ = 0;
|
||||
|
||||
net::Error range_parse_result_ = net::OK;
|
||||
|
||||
base::WeakPtrFactory<URLRequestAsarJob> weak_ptr_factory_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(URLRequestAsarJob);
|
||||
};
|
||||
|
||||
} // namespace asar
|
||||
|
||||
#endif // SHELL_BROWSER_NET_ASAR_URL_REQUEST_ASAR_JOB_H_
|
|
@ -1,209 +0,0 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/net/atom_cert_verifier.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "base/containers/linked_list.h"
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "base/task/post_task.h"
|
||||
#include "content/public/browser/browser_task_traits.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "net/base/net_errors.h"
|
||||
#include "net/cert/cert_verify_result.h"
|
||||
#include "net/cert/x509_certificate.h"
|
||||
#include "shell/browser/browser.h"
|
||||
#include "shell/browser/net/require_ct_delegate.h"
|
||||
#include "shell/common/native_mate_converters/net_converter.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
namespace electron {
|
||||
|
||||
VerifyRequestParams::VerifyRequestParams() = default;
|
||||
VerifyRequestParams::~VerifyRequestParams() = default;
|
||||
VerifyRequestParams::VerifyRequestParams(const VerifyRequestParams&) = default;
|
||||
|
||||
namespace {
|
||||
|
||||
class Response : public base::LinkNode<Response> {
|
||||
public:
|
||||
Response(net::CertVerifyResult* verify_result,
|
||||
net::CompletionOnceCallback callback)
|
||||
: verify_result_(verify_result), callback_(std::move(callback)) {}
|
||||
net::CertVerifyResult* verify_result() { return verify_result_; }
|
||||
net::CompletionOnceCallback callback() { return std::move(callback_); }
|
||||
|
||||
private:
|
||||
net::CertVerifyResult* verify_result_;
|
||||
net::CompletionOnceCallback callback_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Response);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
class CertVerifierRequest : public AtomCertVerifier::Request {
|
||||
public:
|
||||
CertVerifierRequest(const AtomCertVerifier::RequestParams& params,
|
||||
AtomCertVerifier* cert_verifier)
|
||||
: params_(params),
|
||||
cert_verifier_(cert_verifier),
|
||||
weak_ptr_factory_(this) {}
|
||||
|
||||
~CertVerifierRequest() override {
|
||||
cert_verifier_->RemoveRequest(params_);
|
||||
default_verifier_request_.reset();
|
||||
while (!response_list_.empty() && !first_response_) {
|
||||
base::LinkNode<Response>* response_node = response_list_.head();
|
||||
response_node->RemoveFromList();
|
||||
Response* response = response_node->value();
|
||||
RunResponse(response);
|
||||
}
|
||||
cert_verifier_ = nullptr;
|
||||
weak_ptr_factory_.InvalidateWeakPtrs();
|
||||
}
|
||||
|
||||
void RunResponse(Response* response) {
|
||||
if (custom_response_ == net::ERR_ABORTED) {
|
||||
*(response->verify_result()) = result_;
|
||||
response->callback().Run(error_);
|
||||
} else {
|
||||
response->verify_result()->Reset();
|
||||
response->verify_result()->verified_cert = params_.certificate();
|
||||
cert_verifier_->ct_delegate()->AddCTExcludedHost(params_.hostname());
|
||||
response->callback().Run(custom_response_);
|
||||
}
|
||||
delete response;
|
||||
}
|
||||
|
||||
void Start(const net::NetLogWithSource& net_log) {
|
||||
int error = cert_verifier_->default_verifier()->Verify(
|
||||
params_, &result_,
|
||||
base::BindOnce(&CertVerifierRequest::OnDefaultVerificationDone,
|
||||
weak_ptr_factory_.GetWeakPtr()),
|
||||
&default_verifier_request_, net_log);
|
||||
if (error != net::ERR_IO_PENDING)
|
||||
OnDefaultVerificationDone(error);
|
||||
}
|
||||
|
||||
void OnDefaultVerificationDone(int error) {
|
||||
error_ = error;
|
||||
auto request = std::make_unique<VerifyRequestParams>();
|
||||
request->hostname = params_.hostname();
|
||||
request->default_result = net::ErrorToString(error);
|
||||
request->error_code = error;
|
||||
request->certificate = params_.certificate();
|
||||
auto response_callback = base::BindOnce(
|
||||
&CertVerifierRequest::OnResponseInUI, weak_ptr_factory_.GetWeakPtr());
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::UI},
|
||||
base::BindOnce(&CertVerifierRequest::OnVerifyRequestInUI,
|
||||
cert_verifier_->verify_proc(), std::move(request),
|
||||
std::move(response_callback)));
|
||||
}
|
||||
|
||||
static void OnVerifyRequestInUI(
|
||||
const AtomCertVerifier::VerifyProc& verify_proc,
|
||||
std::unique_ptr<VerifyRequestParams> request,
|
||||
base::OnceCallback<void(int)> response_callback) {
|
||||
verify_proc.Run(*(request.get()), std::move(response_callback));
|
||||
}
|
||||
|
||||
static void OnResponseInUI(base::WeakPtr<CertVerifierRequest> self,
|
||||
int result) {
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::IO},
|
||||
base::BindOnce(&CertVerifierRequest::NotifyResponseInIO, self, result));
|
||||
}
|
||||
|
||||
void NotifyResponseInIO(int result) {
|
||||
custom_response_ = result;
|
||||
first_response_ = false;
|
||||
// Responding to first request in the list will initiate destruction of
|
||||
// the class, respond to others in the list inside destructor.
|
||||
base::LinkNode<Response>* response_node = response_list_.head();
|
||||
response_node->RemoveFromList();
|
||||
Response* response = response_node->value();
|
||||
RunResponse(response);
|
||||
}
|
||||
|
||||
void AddResponseListener(net::CertVerifyResult* verify_result,
|
||||
net::CompletionOnceCallback callback) {
|
||||
response_list_.Append(new Response(verify_result, std::move(callback)));
|
||||
}
|
||||
|
||||
const AtomCertVerifier::RequestParams& params() const { return params_; }
|
||||
|
||||
private:
|
||||
using ResponseList = base::LinkedList<Response>;
|
||||
|
||||
const AtomCertVerifier::RequestParams params_;
|
||||
AtomCertVerifier* cert_verifier_;
|
||||
int error_ = net::ERR_IO_PENDING;
|
||||
int custom_response_ = net::ERR_IO_PENDING;
|
||||
bool first_response_ = true;
|
||||
ResponseList response_list_;
|
||||
net::CertVerifyResult result_;
|
||||
std::unique_ptr<AtomCertVerifier::Request> default_verifier_request_;
|
||||
base::WeakPtrFactory<CertVerifierRequest> weak_ptr_factory_;
|
||||
};
|
||||
|
||||
AtomCertVerifier::AtomCertVerifier(RequireCTDelegate* ct_delegate)
|
||||
: default_cert_verifier_(net::CertVerifier::CreateDefault(nullptr)),
|
||||
ct_delegate_(ct_delegate) {}
|
||||
|
||||
AtomCertVerifier::~AtomCertVerifier() {}
|
||||
|
||||
void AtomCertVerifier::SetVerifyProc(const VerifyProc& proc) {
|
||||
verify_proc_ = proc;
|
||||
}
|
||||
|
||||
int AtomCertVerifier::Verify(const RequestParams& params,
|
||||
net::CertVerifyResult* verify_result,
|
||||
net::CompletionOnceCallback callback,
|
||||
std::unique_ptr<Request>* out_req,
|
||||
const net::NetLogWithSource& net_log) {
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
||||
|
||||
if (verify_proc_.is_null()) {
|
||||
ct_delegate_->ClearCTExcludedHostsList();
|
||||
return default_cert_verifier_->Verify(
|
||||
params, verify_result, std::move(callback), out_req, net_log);
|
||||
} else {
|
||||
CertVerifierRequest* request = FindRequest(params);
|
||||
if (!request) {
|
||||
out_req->reset();
|
||||
auto new_request = std::make_unique<CertVerifierRequest>(params, this);
|
||||
new_request->Start(net_log);
|
||||
request = new_request.get();
|
||||
*out_req = std::move(new_request);
|
||||
inflight_requests_[params] = request;
|
||||
}
|
||||
request->AddResponseListener(verify_result, std::move(callback));
|
||||
|
||||
return net::ERR_IO_PENDING;
|
||||
}
|
||||
}
|
||||
|
||||
void AtomCertVerifier::SetConfig(const Config& config) {
|
||||
default_cert_verifier_->SetConfig(config);
|
||||
}
|
||||
|
||||
void AtomCertVerifier::RemoveRequest(const RequestParams& params) {
|
||||
auto it = inflight_requests_.find(params);
|
||||
if (it != inflight_requests_.end())
|
||||
inflight_requests_.erase(it);
|
||||
}
|
||||
|
||||
CertVerifierRequest* AtomCertVerifier::FindRequest(
|
||||
const RequestParams& params) {
|
||||
auto it = inflight_requests_.find(params);
|
||||
if (it != inflight_requests_.end())
|
||||
return it->second;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace electron
|
|
@ -1,71 +0,0 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_NET_ATOM_CERT_VERIFIER_H_
|
||||
#define SHELL_BROWSER_NET_ATOM_CERT_VERIFIER_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "net/cert/cert_verifier.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
class CertVerifierRequest;
|
||||
class RequireCTDelegate;
|
||||
|
||||
struct VerifyRequestParams {
|
||||
std::string hostname;
|
||||
std::string default_result;
|
||||
int error_code;
|
||||
scoped_refptr<net::X509Certificate> certificate;
|
||||
|
||||
VerifyRequestParams();
|
||||
VerifyRequestParams(const VerifyRequestParams&);
|
||||
~VerifyRequestParams();
|
||||
};
|
||||
|
||||
class AtomCertVerifier : public net::CertVerifier {
|
||||
public:
|
||||
explicit AtomCertVerifier(RequireCTDelegate* ct_delegate);
|
||||
~AtomCertVerifier() override;
|
||||
|
||||
using VerifyProc = base::Callback<void(const VerifyRequestParams& request,
|
||||
net::CompletionOnceCallback)>;
|
||||
|
||||
void SetVerifyProc(const VerifyProc& proc);
|
||||
|
||||
const VerifyProc verify_proc() const { return verify_proc_; }
|
||||
RequireCTDelegate* ct_delegate() const { return ct_delegate_; }
|
||||
net::CertVerifier* default_verifier() const {
|
||||
return default_cert_verifier_.get();
|
||||
}
|
||||
|
||||
protected:
|
||||
// net::CertVerifier:
|
||||
int Verify(const RequestParams& params,
|
||||
net::CertVerifyResult* verify_result,
|
||||
net::CompletionOnceCallback callback,
|
||||
std::unique_ptr<Request>* out_req,
|
||||
const net::NetLogWithSource& net_log) override;
|
||||
void SetConfig(const Config& config) override;
|
||||
|
||||
private:
|
||||
friend class CertVerifierRequest;
|
||||
|
||||
void RemoveRequest(const RequestParams& params);
|
||||
CertVerifierRequest* FindRequest(const RequestParams& params);
|
||||
|
||||
std::map<RequestParams, CertVerifierRequest*> inflight_requests_;
|
||||
VerifyProc verify_proc_;
|
||||
std::unique_ptr<net::CertVerifier> default_cert_verifier_;
|
||||
RequireCTDelegate* ct_delegate_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AtomCertVerifier);
|
||||
};
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_BROWSER_NET_ATOM_CERT_VERIFIER_H_
|
|
@ -1,545 +0,0 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/net/atom_network_delegate.h"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "base/command_line.h"
|
||||
#include "base/stl_util.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "base/task/post_task.h"
|
||||
#include "content/public/browser/browser_task_traits.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "content/public/browser/render_frame_host.h"
|
||||
#include "content/public/browser/resource_request_info.h"
|
||||
#include "net/base/load_flags.h"
|
||||
#include "net/base/net_errors.h"
|
||||
#include "net/url_request/url_request.h"
|
||||
#include "shell/browser/api/atom_api_web_contents.h"
|
||||
#include "shell/browser/login_handler.h"
|
||||
#include "shell/common/native_mate_converters/net_converter.h"
|
||||
#include "shell/common/options_switches.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
namespace electron {
|
||||
|
||||
const char* ResourceTypeToString(content::ResourceType type) {
|
||||
switch (type) {
|
||||
case content::ResourceType::kMainFrame:
|
||||
return "mainFrame";
|
||||
case content::ResourceType::kSubFrame:
|
||||
return "subFrame";
|
||||
case content::ResourceType::kStylesheet:
|
||||
return "stylesheet";
|
||||
case content::ResourceType::kScript:
|
||||
return "script";
|
||||
case content::ResourceType::kImage:
|
||||
return "image";
|
||||
case content::ResourceType::kObject:
|
||||
return "object";
|
||||
case content::ResourceType::kXhr:
|
||||
return "xhr";
|
||||
default:
|
||||
return "other";
|
||||
}
|
||||
}
|
||||
|
||||
int32_t GetWebContentsID(int process_id, int frame_id) {
|
||||
auto* webContents = content::WebContents::FromRenderFrameHost(
|
||||
content::RenderFrameHost::FromID(process_id, frame_id));
|
||||
return electron::api::WebContents::GetIDFromWrappedClass(webContents);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
using ResponseHeadersContainer =
|
||||
std::pair<scoped_refptr<net::HttpResponseHeaders>*, const std::string&>;
|
||||
|
||||
void RunSimpleListener(const AtomNetworkDelegate::SimpleListener& listener,
|
||||
std::unique_ptr<base::DictionaryValue> details,
|
||||
int render_process_id,
|
||||
int render_frame_id) {
|
||||
int32_t id = GetWebContentsID(render_process_id, render_frame_id);
|
||||
// id must be greater than zero
|
||||
if (id)
|
||||
details->SetInteger("webContentsId", id);
|
||||
return listener.Run(*(details.get()));
|
||||
}
|
||||
|
||||
void RunResponseListener(const AtomNetworkDelegate::ResponseListener& listener,
|
||||
std::unique_ptr<base::DictionaryValue> details,
|
||||
int render_process_id,
|
||||
int render_frame_id,
|
||||
AtomNetworkDelegate::ResponseCallback callback) {
|
||||
int32_t id = GetWebContentsID(render_process_id, render_frame_id);
|
||||
// id must be greater than zero
|
||||
if (id)
|
||||
details->SetInteger("webContentsId", id);
|
||||
return listener.Run(*(details.get()), std::move(callback));
|
||||
}
|
||||
|
||||
// Test whether the URL of |request| matches |patterns|.
|
||||
bool MatchesFilterCondition(net::URLRequest* request,
|
||||
const URLPatterns& patterns) {
|
||||
if (patterns.empty())
|
||||
return true;
|
||||
|
||||
for (const auto& pattern : patterns) {
|
||||
if (pattern.MatchesURL(request->url()))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Overloaded by multiple types to fill the |details| object.
|
||||
void ToDictionary(base::DictionaryValue* details, net::URLRequest* request) {
|
||||
FillRequestDetails(details, request);
|
||||
details->SetInteger("id", request->identifier());
|
||||
details->SetDouble("timestamp", base::Time::Now().ToDoubleT() * 1000);
|
||||
auto* info = content::ResourceRequestInfo::ForRequest(request);
|
||||
if (info) {
|
||||
details->SetString("resourceType",
|
||||
ResourceTypeToString(info->GetResourceType()));
|
||||
} else {
|
||||
details->SetString("resourceType", "other");
|
||||
}
|
||||
}
|
||||
|
||||
void ToDictionary(base::DictionaryValue* details,
|
||||
const net::HttpRequestHeaders& headers) {
|
||||
auto dict = std::make_unique<base::DictionaryValue>();
|
||||
net::HttpRequestHeaders::Iterator it(headers);
|
||||
while (it.GetNext())
|
||||
dict->SetKey(it.name(), base::Value(it.value()));
|
||||
details->Set("requestHeaders", std::move(dict));
|
||||
}
|
||||
|
||||
void ToDictionary(base::DictionaryValue* details,
|
||||
const net::HttpResponseHeaders* headers) {
|
||||
if (!headers)
|
||||
return;
|
||||
|
||||
auto dict = std::make_unique<base::DictionaryValue>();
|
||||
size_t iter = 0;
|
||||
std::string key;
|
||||
std::string value;
|
||||
while (headers->EnumerateHeaderLines(&iter, &key, &value)) {
|
||||
if (dict->FindKey(key)) {
|
||||
base::ListValue* values = nullptr;
|
||||
if (dict->GetList(key, &values))
|
||||
values->AppendString(value);
|
||||
} else {
|
||||
auto values = std::make_unique<base::ListValue>();
|
||||
values->AppendString(value);
|
||||
dict->Set(key, std::move(values));
|
||||
}
|
||||
}
|
||||
details->Set("responseHeaders", std::move(dict));
|
||||
details->SetString("statusLine", headers->GetStatusLine());
|
||||
details->SetInteger("statusCode", headers->response_code());
|
||||
}
|
||||
|
||||
void ToDictionary(base::DictionaryValue* details, const GURL& location) {
|
||||
details->SetString("redirectURL", location.spec());
|
||||
}
|
||||
|
||||
void ToDictionary(base::DictionaryValue* details,
|
||||
const net::IPEndPoint& remote_endpoint) {
|
||||
details->SetString("ip", remote_endpoint.ToStringWithoutPort());
|
||||
}
|
||||
|
||||
void ToDictionary(base::DictionaryValue* details, bool from_cache) {
|
||||
details->SetBoolean("fromCache", from_cache);
|
||||
}
|
||||
|
||||
void ToDictionary(base::DictionaryValue* details,
|
||||
const net::URLRequestStatus& status) {
|
||||
details->SetString("error", net::ErrorToString(status.error()));
|
||||
}
|
||||
|
||||
// Helper function to fill |details| with arbitrary |args|.
|
||||
template <typename Arg>
|
||||
void FillDetailsObject(base::DictionaryValue* details, Arg arg) {
|
||||
ToDictionary(details, arg);
|
||||
}
|
||||
|
||||
template <typename Arg, typename... Args>
|
||||
void FillDetailsObject(base::DictionaryValue* details, Arg arg, Args... args) {
|
||||
ToDictionary(details, arg);
|
||||
FillDetailsObject(details, args...);
|
||||
}
|
||||
|
||||
// Fill the native types with the result from the response object.
|
||||
void ReadFromResponseObject(const base::DictionaryValue& response,
|
||||
GURL* new_location) {
|
||||
std::string url;
|
||||
if (response.GetString("redirectURL", &url))
|
||||
*new_location = GURL(url);
|
||||
}
|
||||
|
||||
void ReadFromResponseObject(const base::DictionaryValue& response,
|
||||
net::HttpRequestHeaders* headers) {
|
||||
const base::DictionaryValue* dict;
|
||||
if (response.GetDictionary("requestHeaders", &dict)) {
|
||||
headers->Clear();
|
||||
for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
|
||||
it.Advance()) {
|
||||
if (it.value().is_string()) {
|
||||
std::string value = it.value().GetString();
|
||||
headers->SetHeader(it.key(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ReadFromResponseObject(const base::DictionaryValue& response,
|
||||
const ResponseHeadersContainer& container) {
|
||||
const base::DictionaryValue* dict;
|
||||
std::string status_line;
|
||||
if (!response.GetString("statusLine", &status_line))
|
||||
status_line = container.second;
|
||||
if (response.GetDictionary("responseHeaders", &dict)) {
|
||||
auto* headers = container.first;
|
||||
*headers = new net::HttpResponseHeaders("");
|
||||
(*headers)->ReplaceStatusLine(status_line);
|
||||
for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
|
||||
it.Advance()) {
|
||||
const base::ListValue* list;
|
||||
if (it.value().GetAsList(&list)) {
|
||||
(*headers)->RemoveHeader(it.key());
|
||||
for (size_t i = 0; i < list->GetSize(); ++i) {
|
||||
std::string value;
|
||||
if (list->GetString(i, &value))
|
||||
(*headers)->AddHeader(it.key() + " : " + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
AtomNetworkDelegate::SimpleListenerInfo::SimpleListenerInfo(
|
||||
URLPatterns patterns_,
|
||||
SimpleListener listener_)
|
||||
: url_patterns(patterns_), listener(listener_) {}
|
||||
AtomNetworkDelegate::SimpleListenerInfo::SimpleListenerInfo() = default;
|
||||
AtomNetworkDelegate::SimpleListenerInfo::~SimpleListenerInfo() = default;
|
||||
|
||||
AtomNetworkDelegate::ResponseListenerInfo::ResponseListenerInfo(
|
||||
URLPatterns patterns_,
|
||||
ResponseListener listener_)
|
||||
: url_patterns(patterns_), listener(listener_) {}
|
||||
AtomNetworkDelegate::ResponseListenerInfo::ResponseListenerInfo() = default;
|
||||
AtomNetworkDelegate::ResponseListenerInfo::~ResponseListenerInfo() = default;
|
||||
|
||||
AtomNetworkDelegate::AtomNetworkDelegate() {
|
||||
auto* command_line = base::CommandLine::ForCurrentProcess();
|
||||
if (command_line->HasSwitch(switches::kIgnoreConnectionsLimit)) {
|
||||
std::string value =
|
||||
command_line->GetSwitchValueASCII(switches::kIgnoreConnectionsLimit);
|
||||
ignore_connections_limit_domains_ = base::SplitString(
|
||||
value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
|
||||
}
|
||||
}
|
||||
|
||||
AtomNetworkDelegate::~AtomNetworkDelegate() {}
|
||||
|
||||
void AtomNetworkDelegate::SetSimpleListenerInIO(SimpleEvent type,
|
||||
URLPatterns patterns,
|
||||
SimpleListener callback) {
|
||||
if (callback.is_null())
|
||||
simple_listeners_.erase(type);
|
||||
else
|
||||
simple_listeners_[type] = {std::move(patterns), std::move(callback)};
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::SetResponseListenerInIO(ResponseEvent type,
|
||||
URLPatterns patterns,
|
||||
ResponseListener callback) {
|
||||
if (callback.is_null())
|
||||
response_listeners_.erase(type);
|
||||
else
|
||||
response_listeners_[type] = {std::move(patterns), std::move(callback)};
|
||||
}
|
||||
|
||||
int AtomNetworkDelegate::OnBeforeURLRequest(
|
||||
net::URLRequest* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
GURL* new_url) {
|
||||
if (!base::Contains(response_listeners_, kOnBeforeRequest)) {
|
||||
for (const auto& domain : ignore_connections_limit_domains_) {
|
||||
if (request->url().DomainIs(domain)) {
|
||||
// Allow unlimited concurrent connections.
|
||||
request->SetPriority(net::MAXIMUM_PRIORITY);
|
||||
request->SetLoadFlags(request->load_flags() | net::LOAD_IGNORE_LIMITS);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return net::OK;
|
||||
}
|
||||
|
||||
return HandleResponseEvent(kOnBeforeRequest, request, std::move(callback),
|
||||
new_url);
|
||||
}
|
||||
|
||||
int AtomNetworkDelegate::OnBeforeStartTransaction(
|
||||
net::URLRequest* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
net::HttpRequestHeaders* headers) {
|
||||
if (!base::Contains(response_listeners_, kOnBeforeSendHeaders))
|
||||
return net::OK;
|
||||
|
||||
return HandleResponseEvent(kOnBeforeSendHeaders, request, std::move(callback),
|
||||
headers, *headers);
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnStartTransaction(
|
||||
net::URLRequest* request,
|
||||
const net::HttpRequestHeaders& headers) {
|
||||
if (!base::Contains(simple_listeners_, kOnSendHeaders))
|
||||
return;
|
||||
|
||||
HandleSimpleEvent(kOnSendHeaders, request, headers);
|
||||
}
|
||||
|
||||
int AtomNetworkDelegate::OnHeadersReceived(
|
||||
net::URLRequest* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
const net::HttpResponseHeaders* original,
|
||||
scoped_refptr<net::HttpResponseHeaders>* override,
|
||||
GURL* allowed) {
|
||||
if (!base::Contains(response_listeners_, kOnHeadersReceived))
|
||||
return net::OK;
|
||||
|
||||
return HandleResponseEvent(
|
||||
kOnHeadersReceived, request, std::move(callback),
|
||||
std::make_pair(override, original->GetStatusLine()), original);
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
|
||||
const GURL& new_location) {
|
||||
if (!base::Contains(simple_listeners_, kOnBeforeRedirect))
|
||||
return;
|
||||
|
||||
HandleSimpleEvent(
|
||||
kOnBeforeRedirect, request, new_location, request->response_headers(),
|
||||
request->GetResponseRemoteEndpoint(), request->was_cached());
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request,
|
||||
int net_error) {
|
||||
if (!base::Contains(simple_listeners_, kOnResponseStarted))
|
||||
return;
|
||||
|
||||
if (request->status().status() != net::URLRequestStatus::SUCCESS)
|
||||
return;
|
||||
|
||||
HandleSimpleEvent(kOnResponseStarted, request, request->response_headers(),
|
||||
request->was_cached());
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnCompleted(net::URLRequest* request,
|
||||
bool started,
|
||||
int net_error) {
|
||||
// OnCompleted may happen before other events.
|
||||
callbacks_.erase(request->identifier());
|
||||
|
||||
if (request->status().status() == net::URLRequestStatus::FAILED ||
|
||||
request->status().status() == net::URLRequestStatus::CANCELED) {
|
||||
// Error event.
|
||||
OnErrorOccurred(request, started, net_error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (request->response_headers() &&
|
||||
net::HttpResponseHeaders::IsRedirectResponseCode(
|
||||
request->response_headers()->response_code())) {
|
||||
// Redirect event.
|
||||
return;
|
||||
}
|
||||
|
||||
if (!base::Contains(simple_listeners_, kOnCompleted))
|
||||
return;
|
||||
|
||||
HandleSimpleEvent(kOnCompleted, request, request->response_headers(),
|
||||
request->was_cached());
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
|
||||
const auto& it = login_handler_map_.find(request->identifier());
|
||||
if (it != login_handler_map_.end()) {
|
||||
it->second->NotifyRequestDestroyed();
|
||||
it->second = nullptr;
|
||||
login_handler_map_.erase(it);
|
||||
}
|
||||
callbacks_.erase(request->identifier());
|
||||
}
|
||||
|
||||
net::NetworkDelegate::AuthRequiredResponse AtomNetworkDelegate::OnAuthRequired(
|
||||
net::URLRequest* request,
|
||||
const net::AuthChallengeInfo& auth_info,
|
||||
AuthCallback callback,
|
||||
net::AuthCredentials* credentials) {
|
||||
auto* resource_request_info =
|
||||
content::ResourceRequestInfo::ForRequest(request);
|
||||
if (!resource_request_info)
|
||||
return AUTH_REQUIRED_RESPONSE_NO_ACTION;
|
||||
login_handler_map_.emplace(
|
||||
request->identifier(),
|
||||
new LoginHandler(request, auth_info, std::move(callback), credentials,
|
||||
resource_request_info));
|
||||
return AUTH_REQUIRED_RESPONSE_IO_PENDING;
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnCanGetCookies(const net::URLRequest& request,
|
||||
const net::CookieList& cookie_list,
|
||||
bool allowed_from_caller) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnCanSetCookie(
|
||||
const net::URLRequest& request,
|
||||
const net::CanonicalCookie& cookie_line,
|
||||
net::CookieOptions* options,
|
||||
bool allowed_from_caller) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnCanAccessFile(
|
||||
const net::URLRequest& request,
|
||||
const base::FilePath& original_path,
|
||||
const base::FilePath& absolute_path) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnForcePrivacyMode(
|
||||
const GURL& url,
|
||||
const GURL& first_party_for_cookies) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
|
||||
const net::URLRequest& request,
|
||||
const GURL& target_url,
|
||||
const GURL& referrer_url) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnCanQueueReportingReport(
|
||||
const url::Origin& origin) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnCanSendReportingReports(
|
||||
std::set<url::Origin> origins,
|
||||
base::OnceCallback<void(std::set<url::Origin>)> result_callback) const {
|
||||
std::move(result_callback).Run(std::move(origins));
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnCanSetReportingClient(const url::Origin& origin,
|
||||
const GURL& endpoint) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AtomNetworkDelegate::OnCanUseReportingClient(const url::Origin& origin,
|
||||
const GURL& endpoint) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void AtomNetworkDelegate::OnErrorOccurred(net::URLRequest* request,
|
||||
bool started,
|
||||
int net_error) {
|
||||
if (!base::Contains(simple_listeners_, kOnErrorOccurred))
|
||||
return;
|
||||
|
||||
HandleSimpleEvent(kOnErrorOccurred, request, request->was_cached(),
|
||||
request->status());
|
||||
}
|
||||
|
||||
template <typename Out, typename... Args>
|
||||
int AtomNetworkDelegate::HandleResponseEvent(
|
||||
ResponseEvent type,
|
||||
net::URLRequest* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
Out out,
|
||||
Args... args) {
|
||||
const auto& info = response_listeners_[type];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return net::OK;
|
||||
|
||||
auto details = std::make_unique<base::DictionaryValue>();
|
||||
FillDetailsObject(details.get(), request, args...);
|
||||
|
||||
int render_process_id, render_frame_id;
|
||||
content::ResourceRequestInfo::GetRenderFrameForRequest(
|
||||
request, &render_process_id, &render_frame_id);
|
||||
|
||||
// The |request| could be destroyed before the |callback| is called.
|
||||
callbacks_[request->identifier()] = std::move(callback);
|
||||
|
||||
ResponseCallback response =
|
||||
base::BindOnce(&AtomNetworkDelegate::OnListenerResultInUI<Out>,
|
||||
base::Unretained(this), request->identifier(), out);
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::UI},
|
||||
base::BindOnce(RunResponseListener, info.listener, std::move(details),
|
||||
render_process_id, render_frame_id, std::move(response)));
|
||||
return net::ERR_IO_PENDING;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void AtomNetworkDelegate::HandleSimpleEvent(SimpleEvent type,
|
||||
net::URLRequest* request,
|
||||
Args... args) {
|
||||
const auto& info = simple_listeners_[type];
|
||||
if (!MatchesFilterCondition(request, info.url_patterns))
|
||||
return;
|
||||
|
||||
auto details = std::make_unique<base::DictionaryValue>();
|
||||
FillDetailsObject(details.get(), request, args...);
|
||||
|
||||
int render_process_id, render_frame_id;
|
||||
content::ResourceRequestInfo::GetRenderFrameForRequest(
|
||||
request, &render_process_id, &render_frame_id);
|
||||
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::UI},
|
||||
base::BindOnce(RunSimpleListener, info.listener, std::move(details),
|
||||
render_process_id, render_frame_id));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void AtomNetworkDelegate::OnListenerResultInIO(
|
||||
uint64_t id,
|
||||
T out,
|
||||
std::unique_ptr<base::DictionaryValue> response) {
|
||||
// The request has been destroyed.
|
||||
if (!base::Contains(callbacks_, id))
|
||||
return;
|
||||
|
||||
ReadFromResponseObject(*response, out);
|
||||
|
||||
bool cancel = false;
|
||||
response->GetBoolean("cancel", &cancel);
|
||||
std::move(callbacks_[id]).Run(cancel ? net::ERR_BLOCKED_BY_CLIENT : net::OK);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void AtomNetworkDelegate::OnListenerResultInUI(
|
||||
uint64_t id,
|
||||
T out,
|
||||
const base::DictionaryValue& response) {
|
||||
auto copy = base::DictionaryValue::From(
|
||||
base::Value::ToUniquePtrValue(response.Clone()));
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::IO},
|
||||
base::BindOnce(&AtomNetworkDelegate::OnListenerResultInIO<T>,
|
||||
base::Unretained(this), id, out, std::move(copy)));
|
||||
}
|
||||
|
||||
} // namespace electron
|
|
@ -1,183 +0,0 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_
|
||||
#define SHELL_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/synchronization/lock.h"
|
||||
#include "base/values.h"
|
||||
#include "content/public/browser/resource_request_info.h"
|
||||
#include "extensions/common/url_pattern.h"
|
||||
#include "net/base/network_delegate.h"
|
||||
#include "net/http/http_request_headers.h"
|
||||
#include "net/http/http_response_headers.h"
|
||||
|
||||
class URLPattern;
|
||||
|
||||
namespace electron {
|
||||
|
||||
using URLPatterns = std::set<URLPattern>;
|
||||
|
||||
const char* ResourceTypeToString(content::ResourceType type);
|
||||
|
||||
class LoginHandler;
|
||||
|
||||
class AtomNetworkDelegate : public net::NetworkDelegate {
|
||||
public:
|
||||
using ResponseCallback =
|
||||
base::OnceCallback<void(const base::DictionaryValue&)>;
|
||||
using SimpleListener =
|
||||
base::RepeatingCallback<void(const base::DictionaryValue&)>;
|
||||
using ResponseListener =
|
||||
base::RepeatingCallback<void(const base::DictionaryValue&,
|
||||
ResponseCallback)>;
|
||||
|
||||
enum SimpleEvent {
|
||||
kOnSendHeaders,
|
||||
kOnBeforeRedirect,
|
||||
kOnResponseStarted,
|
||||
kOnCompleted,
|
||||
kOnErrorOccurred,
|
||||
};
|
||||
|
||||
enum ResponseEvent {
|
||||
kOnBeforeRequest,
|
||||
kOnBeforeSendHeaders,
|
||||
kOnHeadersReceived,
|
||||
};
|
||||
|
||||
struct SimpleListenerInfo {
|
||||
URLPatterns url_patterns;
|
||||
SimpleListener listener;
|
||||
|
||||
SimpleListenerInfo(URLPatterns, SimpleListener);
|
||||
SimpleListenerInfo();
|
||||
~SimpleListenerInfo();
|
||||
};
|
||||
|
||||
struct ResponseListenerInfo {
|
||||
URLPatterns url_patterns;
|
||||
ResponseListener listener;
|
||||
|
||||
ResponseListenerInfo(URLPatterns, ResponseListener);
|
||||
ResponseListenerInfo();
|
||||
~ResponseListenerInfo();
|
||||
};
|
||||
|
||||
AtomNetworkDelegate();
|
||||
~AtomNetworkDelegate() override;
|
||||
|
||||
void SetSimpleListenerInIO(SimpleEvent type,
|
||||
URLPatterns patterns,
|
||||
SimpleListener callback);
|
||||
void SetResponseListenerInIO(ResponseEvent type,
|
||||
URLPatterns patterns,
|
||||
ResponseListener callback);
|
||||
|
||||
protected:
|
||||
// net::NetworkDelegate:
|
||||
int OnBeforeURLRequest(net::URLRequest* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
GURL* new_url) override;
|
||||
int OnBeforeStartTransaction(net::URLRequest* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
net::HttpRequestHeaders* headers) override;
|
||||
void OnBeforeSendHeaders(net::URLRequest* request,
|
||||
const net::ProxyInfo& proxy_info,
|
||||
const net::ProxyRetryInfoMap& proxy_retry_info,
|
||||
net::HttpRequestHeaders* headers) override {}
|
||||
void OnStartTransaction(net::URLRequest* request,
|
||||
const net::HttpRequestHeaders& headers) override;
|
||||
int OnHeadersReceived(
|
||||
net::URLRequest* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
const net::HttpResponseHeaders* original_response_headers,
|
||||
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
|
||||
GURL* allowed_unsafe_redirect_url) override;
|
||||
void OnBeforeRedirect(net::URLRequest* request,
|
||||
const GURL& new_location) override;
|
||||
void OnResponseStarted(net::URLRequest* request, int net_error) override;
|
||||
void OnNetworkBytesReceived(net::URLRequest* request,
|
||||
int64_t bytes_read) override {}
|
||||
void OnNetworkBytesSent(net::URLRequest* request,
|
||||
int64_t bytes_sent) override {}
|
||||
void OnCompleted(net::URLRequest* request,
|
||||
bool started,
|
||||
int net_error) override;
|
||||
void OnURLRequestDestroyed(net::URLRequest* request) override;
|
||||
void OnPACScriptError(int line_number, const base::string16& error) override {
|
||||
}
|
||||
AuthRequiredResponse OnAuthRequired(
|
||||
net::URLRequest* request,
|
||||
const net::AuthChallengeInfo& auth_info,
|
||||
AuthCallback callback,
|
||||
net::AuthCredentials* credentials) override;
|
||||
bool OnCanGetCookies(const net::URLRequest& request,
|
||||
const net::CookieList& cookie_list,
|
||||
bool allowed_from_caller) override;
|
||||
bool OnCanSetCookie(const net::URLRequest& request,
|
||||
const net::CanonicalCookie& cookie_line,
|
||||
net::CookieOptions* options,
|
||||
bool allowed_from_caller) override;
|
||||
bool OnCanAccessFile(const net::URLRequest& request,
|
||||
const base::FilePath& original_path,
|
||||
const base::FilePath& absolute_path) const override;
|
||||
bool OnForcePrivacyMode(const GURL& url,
|
||||
const GURL& first_party_for_cookies) const override;
|
||||
bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
|
||||
const net::URLRequest& request,
|
||||
const GURL& target_url,
|
||||
const GURL& referrer_url) const override;
|
||||
bool OnCanQueueReportingReport(const url::Origin& origin) const override;
|
||||
void OnCanSendReportingReports(std::set<url::Origin> origins,
|
||||
base::OnceCallback<void(std::set<url::Origin>)>
|
||||
result_callback) const override;
|
||||
bool OnCanSetReportingClient(const url::Origin& origin,
|
||||
const GURL& endpoint) const override;
|
||||
bool OnCanUseReportingClient(const url::Origin& origin,
|
||||
const GURL& endpoint) const override;
|
||||
|
||||
private:
|
||||
void OnErrorOccurred(net::URLRequest* request, bool started, int net_error);
|
||||
|
||||
template <typename... Args>
|
||||
void HandleSimpleEvent(SimpleEvent type,
|
||||
net::URLRequest* request,
|
||||
Args... args);
|
||||
template <typename Out, typename... Args>
|
||||
int HandleResponseEvent(ResponseEvent type,
|
||||
net::URLRequest* request,
|
||||
net::CompletionOnceCallback callback,
|
||||
Out out,
|
||||
Args... args);
|
||||
|
||||
// Deal with the results of Listener.
|
||||
template <typename T>
|
||||
void OnListenerResultInIO(uint64_t id,
|
||||
T out,
|
||||
std::unique_ptr<base::DictionaryValue> response);
|
||||
template <typename T>
|
||||
void OnListenerResultInUI(uint64_t id,
|
||||
T out,
|
||||
const base::DictionaryValue& response);
|
||||
|
||||
std::map<uint64_t, scoped_refptr<LoginHandler>> login_handler_map_;
|
||||
std::map<SimpleEvent, SimpleListenerInfo> simple_listeners_;
|
||||
std::map<ResponseEvent, ResponseListenerInfo> response_listeners_;
|
||||
std::map<uint64_t, net::CompletionOnceCallback> callbacks_;
|
||||
std::vector<std::string> ignore_connections_limit_domains_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AtomNetworkDelegate);
|
||||
};
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_BROWSER_NET_ATOM_NETWORK_DELEGATE_H_
|
|
@ -1,147 +0,0 @@
|
|||
// Copyright (c) 2013 GitHub, Inc.
|
||||
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/net/atom_url_request_job_factory.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "base/memory/ptr_util.h"
|
||||
#include "base/stl_util.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "net/base/load_flags.h"
|
||||
#include "net/url_request/url_request.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
namespace electron {
|
||||
|
||||
namespace {
|
||||
|
||||
int disable_protocol_intercept_flag_key = 0;
|
||||
|
||||
} // namespace
|
||||
|
||||
typedef net::URLRequestJobFactory::ProtocolHandler ProtocolHandler;
|
||||
|
||||
const void* DisableProtocolInterceptFlagKey() {
|
||||
return &disable_protocol_intercept_flag_key;
|
||||
}
|
||||
|
||||
AtomURLRequestJobFactory::AtomURLRequestJobFactory() {}
|
||||
|
||||
AtomURLRequestJobFactory::~AtomURLRequestJobFactory() {
|
||||
Clear();
|
||||
}
|
||||
|
||||
void AtomURLRequestJobFactory::Chain(
|
||||
std::unique_ptr<net::URLRequestJobFactory> job_factory) {
|
||||
job_factory_ = std::move(job_factory);
|
||||
}
|
||||
|
||||
bool AtomURLRequestJobFactory::SetProtocolHandler(
|
||||
const std::string& scheme,
|
||||
std::unique_ptr<ProtocolHandler> protocol_handler) {
|
||||
if (!protocol_handler) {
|
||||
auto it = protocol_handler_map_.find(scheme);
|
||||
if (it == protocol_handler_map_.end())
|
||||
return false;
|
||||
|
||||
delete it->second;
|
||||
protocol_handler_map_.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (base::Contains(protocol_handler_map_, scheme))
|
||||
return false;
|
||||
protocol_handler_map_[scheme] = protocol_handler.release();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AtomURLRequestJobFactory::InterceptProtocol(
|
||||
const std::string& scheme,
|
||||
std::unique_ptr<ProtocolHandler> protocol_handler) {
|
||||
if (!base::Contains(protocol_handler_map_, scheme) ||
|
||||
base::Contains(original_protocols_, scheme))
|
||||
return false;
|
||||
ProtocolHandler* original_protocol_handler = protocol_handler_map_[scheme];
|
||||
protocol_handler_map_[scheme] = protocol_handler.release();
|
||||
original_protocols_[scheme].reset(original_protocol_handler);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AtomURLRequestJobFactory::UninterceptProtocol(const std::string& scheme) {
|
||||
auto it = original_protocols_.find(scheme);
|
||||
if (it == original_protocols_.end())
|
||||
return false;
|
||||
protocol_handler_map_[scheme] = it->second.release();
|
||||
original_protocols_.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AtomURLRequestJobFactory::HasProtocolHandler(
|
||||
const std::string& scheme) const {
|
||||
return base::Contains(protocol_handler_map_, scheme);
|
||||
}
|
||||
|
||||
void AtomURLRequestJobFactory::Clear() {
|
||||
for (auto& it : protocol_handler_map_)
|
||||
delete it.second;
|
||||
protocol_handler_map_.clear();
|
||||
original_protocols_.clear();
|
||||
}
|
||||
|
||||
net::URLRequestJob* AtomURLRequestJobFactory::MaybeCreateJobWithProtocolHandler(
|
||||
const std::string& scheme,
|
||||
net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate) const {
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
||||
|
||||
auto* job = job_factory_->MaybeCreateJobWithProtocolHandler(scheme, request,
|
||||
network_delegate);
|
||||
if (job)
|
||||
return job;
|
||||
|
||||
auto it = protocol_handler_map_.find(scheme);
|
||||
if (it == protocol_handler_map_.end())
|
||||
return nullptr;
|
||||
|
||||
if (request->GetUserData(DisableProtocolInterceptFlagKey()))
|
||||
return nullptr;
|
||||
|
||||
return it->second->MaybeCreateJob(request, network_delegate);
|
||||
}
|
||||
|
||||
net::URLRequestJob* AtomURLRequestJobFactory::MaybeInterceptRedirect(
|
||||
net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate,
|
||||
const GURL& location) const {
|
||||
return job_factory_->MaybeInterceptRedirect(request, network_delegate,
|
||||
location);
|
||||
}
|
||||
|
||||
net::URLRequestJob* AtomURLRequestJobFactory::MaybeInterceptResponse(
|
||||
net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate) const {
|
||||
return job_factory_->MaybeInterceptResponse(request, network_delegate);
|
||||
}
|
||||
|
||||
bool AtomURLRequestJobFactory::IsHandledProtocol(
|
||||
const std::string& scheme) const {
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
||||
|
||||
return HasProtocolHandler(scheme) ||
|
||||
net::URLRequest::IsHandledProtocol(scheme);
|
||||
}
|
||||
|
||||
bool AtomURLRequestJobFactory::IsSafeRedirectTarget(
|
||||
const GURL& location) const {
|
||||
if (!location.is_valid()) {
|
||||
// We handle error cases.
|
||||
return true;
|
||||
}
|
||||
return IsHandledProtocol(location.scheme());
|
||||
}
|
||||
|
||||
} // namespace electron
|
|
@ -1,79 +0,0 @@
|
|||
// Copyright (c) 2013 GitHub, Inc.
|
||||
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_NET_ATOM_URL_REQUEST_JOB_FACTORY_H_
|
||||
#define SHELL_BROWSER_NET_ATOM_URL_REQUEST_JOB_FACTORY_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "net/url_request/url_request_job_factory.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
const void* DisableProtocolInterceptFlagKey();
|
||||
|
||||
class AtomURLRequestJobFactory : public net::URLRequestJobFactory {
|
||||
public:
|
||||
AtomURLRequestJobFactory();
|
||||
~AtomURLRequestJobFactory() override;
|
||||
|
||||
// Requests are forwarded to the chained job factory first.
|
||||
void Chain(std::unique_ptr<net::URLRequestJobFactory> job_factory);
|
||||
|
||||
// Sets the ProtocolHandler for a scheme. Returns true on success, false on
|
||||
// failure (a ProtocolHandler already exists for |scheme|). On success,
|
||||
// URLRequestJobFactory takes ownership of |protocol_handler|.
|
||||
bool SetProtocolHandler(const std::string& scheme,
|
||||
std::unique_ptr<ProtocolHandler> protocol_handler);
|
||||
|
||||
// Intercepts the ProtocolHandler for a scheme.
|
||||
bool InterceptProtocol(const std::string& scheme,
|
||||
std::unique_ptr<ProtocolHandler> protocol_handler);
|
||||
bool UninterceptProtocol(const std::string& scheme);
|
||||
|
||||
// Whether the protocol handler is registered by the job factory.
|
||||
bool HasProtocolHandler(const std::string& scheme) const;
|
||||
|
||||
// Clear all protocol handlers.
|
||||
void Clear();
|
||||
|
||||
// URLRequestJobFactory implementation
|
||||
net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
|
||||
const std::string& scheme,
|
||||
net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate) const override;
|
||||
net::URLRequestJob* MaybeInterceptRedirect(
|
||||
net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate,
|
||||
const GURL& location) const override;
|
||||
net::URLRequestJob* MaybeInterceptResponse(
|
||||
net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate) const override;
|
||||
bool IsHandledProtocol(const std::string& scheme) const override;
|
||||
bool IsSafeRedirectTarget(const GURL& location) const override;
|
||||
|
||||
private:
|
||||
using ProtocolHandlerMap = std::map<std::string, ProtocolHandler*>;
|
||||
|
||||
ProtocolHandlerMap protocol_handler_map_;
|
||||
|
||||
// Map that stores the original protocols of schemes.
|
||||
using OriginalProtocolsMap =
|
||||
std::unordered_map<std::string, std::unique_ptr<ProtocolHandler>>;
|
||||
// Can only be accessed in IO thread.
|
||||
OriginalProtocolsMap original_protocols_;
|
||||
|
||||
std::unique_ptr<net::URLRequestJobFactory> job_factory_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AtomURLRequestJobFactory);
|
||||
};
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_BROWSER_NET_ATOM_URL_REQUEST_JOB_FACTORY_H_
|
44
shell/browser/net/cert_verifier_client.cc
Normal file
44
shell/browser/net/cert_verifier_client.cc
Normal file
|
@ -0,0 +1,44 @@
|
|||
// Copyright (c) 2019 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "shell/browser/net/cert_verifier_client.h"
|
||||
#include "shell/common/native_mate_converters/net_converter.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
VerifyRequestParams::VerifyRequestParams() = default;
|
||||
|
||||
VerifyRequestParams::~VerifyRequestParams() = default;
|
||||
|
||||
VerifyRequestParams::VerifyRequestParams(const VerifyRequestParams&) = default;
|
||||
|
||||
CertVerifierClient::CertVerifierClient(CertVerifyProc proc)
|
||||
: cert_verify_proc_(proc) {}
|
||||
|
||||
CertVerifierClient::~CertVerifierClient() = default;
|
||||
|
||||
void CertVerifierClient::Verify(
|
||||
int default_error,
|
||||
const net::CertVerifyResult& default_result,
|
||||
const scoped_refptr<net::X509Certificate>& certificate,
|
||||
const std::string& hostname,
|
||||
int flags,
|
||||
const base::Optional<std::string>& ocsp_response,
|
||||
VerifyCallback callback) {
|
||||
VerifyRequestParams params;
|
||||
params.hostname = hostname;
|
||||
params.default_result = net::ErrorToString(default_error);
|
||||
params.error_code = default_error;
|
||||
params.certificate = certificate;
|
||||
cert_verify_proc_.Run(
|
||||
params,
|
||||
base::AdaptCallbackForRepeating(base::BindOnce(
|
||||
[](VerifyCallback callback, const net::CertVerifyResult& result,
|
||||
int err) { std::move(callback).Run(err, result); },
|
||||
std::move(callback), default_result)));
|
||||
}
|
||||
|
||||
} // namespace electron
|
50
shell/browser/net/cert_verifier_client.h
Normal file
50
shell/browser/net/cert_verifier_client.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
// Copyright (c) 2019 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_NET_CERT_VERIFIER_CLIENT_H_
|
||||
#define SHELL_BROWSER_NET_CERT_VERIFIER_CLIENT_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "net/cert/x509_certificate.h"
|
||||
#include "services/network/public/mojom/network_context.mojom.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
struct VerifyRequestParams {
|
||||
std::string hostname;
|
||||
std::string default_result;
|
||||
int error_code;
|
||||
scoped_refptr<net::X509Certificate> certificate;
|
||||
|
||||
VerifyRequestParams();
|
||||
VerifyRequestParams(const VerifyRequestParams&);
|
||||
~VerifyRequestParams();
|
||||
};
|
||||
|
||||
class CertVerifierClient : public network::mojom::CertVerifierClient {
|
||||
public:
|
||||
using CertVerifyProc =
|
||||
base::RepeatingCallback<void(const VerifyRequestParams& request,
|
||||
base::RepeatingCallback<void(int)>)>;
|
||||
|
||||
explicit CertVerifierClient(CertVerifyProc proc);
|
||||
~CertVerifierClient() override;
|
||||
|
||||
// network::mojom::CertVerifierClient
|
||||
void Verify(int default_error,
|
||||
const net::CertVerifyResult& default_result,
|
||||
const scoped_refptr<net::X509Certificate>& certificate,
|
||||
const std::string& hostname,
|
||||
int flags,
|
||||
const base::Optional<std::string>& ocsp_response,
|
||||
VerifyCallback callback) override;
|
||||
|
||||
private:
|
||||
CertVerifyProc cert_verify_proc_;
|
||||
};
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_BROWSER_NET_CERT_VERIFIER_CLIENT_H_
|
|
@ -1,55 +0,0 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/net/js_asker.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "shell/common/native_mate_converters/once_callback.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
JsAsker::JsAsker() = default;
|
||||
|
||||
JsAsker::~JsAsker() = default;
|
||||
|
||||
void JsAsker::SetHandlerInfo(
|
||||
v8::Isolate* isolate,
|
||||
net::URLRequestContextGetter* request_context_getter,
|
||||
const JavaScriptHandler& handler) {
|
||||
isolate_ = isolate;
|
||||
request_context_getter_ = request_context_getter;
|
||||
handler_ = handler;
|
||||
}
|
||||
|
||||
// static
|
||||
void JsAsker::AskForOptions(
|
||||
v8::Isolate* isolate,
|
||||
const JavaScriptHandler& handler,
|
||||
std::unique_ptr<base::DictionaryValue> request_details,
|
||||
BeforeStartCallback before_start) {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
||||
v8::Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
||||
v8::Context::Scope context_scope(context);
|
||||
handler.Run(*(request_details.get()),
|
||||
mate::ConvertToV8(isolate, std::move(before_start)));
|
||||
}
|
||||
|
||||
// static
|
||||
bool JsAsker::IsErrorOptions(base::Value* value, int* error) {
|
||||
if (value->is_dict()) {
|
||||
base::DictionaryValue* dict = static_cast<base::DictionaryValue*>(value);
|
||||
if (dict->GetInteger("error", error))
|
||||
return true;
|
||||
} else if (value->is_int()) {
|
||||
*error = value->GetInt();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace electron
|
|
@ -1,58 +0,0 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_NET_JS_ASKER_H_
|
||||
#define SHELL_BROWSER_NET_JS_ASKER_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/values.h"
|
||||
#include "native_mate/arguments.h"
|
||||
#include "net/url_request/url_request_context_getter.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
using JavaScriptHandler =
|
||||
base::Callback<void(const base::DictionaryValue&, v8::Local<v8::Value>)>;
|
||||
using BeforeStartCallback = base::OnceCallback<void(mate::Arguments* args)>;
|
||||
|
||||
class JsAsker {
|
||||
public:
|
||||
JsAsker();
|
||||
~JsAsker();
|
||||
|
||||
// Called by |CustomProtocolHandler| to store handler related information.
|
||||
void SetHandlerInfo(v8::Isolate* isolate,
|
||||
net::URLRequestContextGetter* request_context_getter,
|
||||
const JavaScriptHandler& handler);
|
||||
|
||||
// Ask handler for options in UI thread.
|
||||
static void AskForOptions(
|
||||
v8::Isolate* isolate,
|
||||
const JavaScriptHandler& handler,
|
||||
std::unique_ptr<base::DictionaryValue> request_details,
|
||||
BeforeStartCallback before_start);
|
||||
|
||||
// Test whether the |options| means an error.
|
||||
static bool IsErrorOptions(base::Value* value, int* error);
|
||||
|
||||
net::URLRequestContextGetter* request_context_getter() const {
|
||||
return request_context_getter_;
|
||||
}
|
||||
v8::Isolate* isolate() { return isolate_; }
|
||||
JavaScriptHandler handler() { return handler_; }
|
||||
|
||||
private:
|
||||
v8::Isolate* isolate_;
|
||||
net::URLRequestContextGetter* request_context_getter_;
|
||||
JavaScriptHandler handler_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(JsAsker);
|
||||
};
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_BROWSER_NET_JS_ASKER_H_
|
|
@ -1,36 +0,0 @@
|
|||
// Copyright (c) 2017 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/net/require_ct_delegate.h"
|
||||
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
RequireCTDelegate::RequireCTDelegate() {}
|
||||
|
||||
RequireCTDelegate::~RequireCTDelegate() {}
|
||||
|
||||
void RequireCTDelegate::AddCTExcludedHost(const std::string& host) {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
||||
ct_excluded_hosts_.insert(host);
|
||||
}
|
||||
|
||||
void RequireCTDelegate::ClearCTExcludedHostsList() {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
||||
ct_excluded_hosts_.clear();
|
||||
}
|
||||
|
||||
RequireCTDelegate::CTRequirementLevel RequireCTDelegate::IsCTRequiredForHost(
|
||||
const std::string& host,
|
||||
const net::X509Certificate* chain,
|
||||
const net::HashValueVector& hashes) {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
||||
if (!ct_excluded_hosts_.empty() &&
|
||||
(ct_excluded_hosts_.find(host) != ct_excluded_hosts_.end()))
|
||||
return CTRequirementLevel::NOT_REQUIRED;
|
||||
return CTRequirementLevel::DEFAULT;
|
||||
}
|
||||
|
||||
} // namespace electron
|
|
@ -1,37 +0,0 @@
|
|||
// Copyright (c) 2017 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_NET_REQUIRE_CT_DELEGATE_H_
|
||||
#define SHELL_BROWSER_NET_REQUIRE_CT_DELEGATE_H_
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "net/http/transport_security_state.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
class RequireCTDelegate
|
||||
: public net::TransportSecurityState::RequireCTDelegate {
|
||||
public:
|
||||
RequireCTDelegate();
|
||||
~RequireCTDelegate() override;
|
||||
|
||||
void AddCTExcludedHost(const std::string& host);
|
||||
void ClearCTExcludedHostsList();
|
||||
|
||||
// net::TransportSecurityState::RequireCTDelegate:
|
||||
CTRequirementLevel IsCTRequiredForHost(
|
||||
const std::string& host,
|
||||
const net::X509Certificate* chain,
|
||||
const net::HashValueVector& hashes) override;
|
||||
|
||||
private:
|
||||
std::set<std::string> ct_excluded_hosts_;
|
||||
DISALLOW_COPY_AND_ASSIGN(RequireCTDelegate);
|
||||
};
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_BROWSER_NET_REQUIRE_CT_DELEGATE_H_
|
|
@ -21,7 +21,6 @@
|
|||
#include "services/network/public/cpp/features.h"
|
||||
#include "services/network/public/cpp/shared_url_loader_factory.h"
|
||||
#include "shell/browser/atom_browser_client.h"
|
||||
#include "shell/browser/io_thread.h"
|
||||
#include "shell/common/application_info.h"
|
||||
#include "shell/common/options_switches.h"
|
||||
#include "url/gurl.h"
|
||||
|
@ -41,10 +40,6 @@ network::mojom::HttpAuthStaticParamsPtr CreateHttpAuthStaticParams() {
|
|||
return auth_static_params;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace electron {
|
||||
|
||||
network::mojom::HttpAuthDynamicParamsPtr CreateHttpAuthDynamicParams() {
|
||||
auto* command_line = base::CommandLine::ForCurrentProcess();
|
||||
network::mojom::HttpAuthDynamicParamsPtr auth_dynamic_params =
|
||||
|
@ -60,7 +55,7 @@ network::mojom::HttpAuthDynamicParamsPtr CreateHttpAuthDynamicParams() {
|
|||
return auth_dynamic_params;
|
||||
}
|
||||
|
||||
} // namespace electron
|
||||
} // namespace
|
||||
|
||||
// SharedURLLoaderFactory backed by a SystemNetworkContextManager and its
|
||||
// network context. Transparently handles crashes.
|
||||
|
@ -116,14 +111,7 @@ class SystemNetworkContextManager::URLLoaderFactoryForSystem
|
|||
};
|
||||
|
||||
network::mojom::NetworkContext* SystemNetworkContextManager::GetContext() {
|
||||
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
||||
// SetUp should already have been called.
|
||||
DCHECK(io_thread_network_context_);
|
||||
return io_thread_network_context_.get();
|
||||
}
|
||||
|
||||
if (!network_service_network_context_ ||
|
||||
network_service_network_context_.encountered_error()) {
|
||||
if (!network_context_ || network_context_.encountered_error()) {
|
||||
// This should call into OnNetworkServiceCreated(), which will re-create
|
||||
// the network service, if needed. There's a chance that it won't be
|
||||
// invoked, if the NetworkContext has encountered an error but the
|
||||
|
@ -131,9 +119,9 @@ network::mojom::NetworkContext* SystemNetworkContextManager::GetContext() {
|
|||
// trying to create a new NetworkContext would fail, anyways, and hopefully
|
||||
// a new NetworkContext will be created on the next GetContext() call.
|
||||
content::GetNetworkService();
|
||||
DCHECK(network_service_network_context_);
|
||||
DCHECK(network_context_);
|
||||
}
|
||||
return network_service_network_context_.get();
|
||||
return network_context_.get();
|
||||
}
|
||||
|
||||
network::mojom::URLLoaderFactory*
|
||||
|
@ -176,23 +164,6 @@ SystemNetworkContextManager::CreateDefaultNetworkContextParams() {
|
|||
return network_context_params;
|
||||
}
|
||||
|
||||
void SystemNetworkContextManager::SetUp(
|
||||
network::mojom::NetworkContextRequest* network_context_request,
|
||||
network::mojom::NetworkContextParamsPtr* network_context_params,
|
||||
network::mojom::HttpAuthStaticParamsPtr* http_auth_static_params,
|
||||
network::mojom::HttpAuthDynamicParamsPtr* http_auth_dynamic_params) {
|
||||
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
|
||||
*network_context_request = mojo::MakeRequest(&io_thread_network_context_);
|
||||
*network_context_params = CreateNetworkContextParams();
|
||||
} else {
|
||||
// Just use defaults if the network service is enabled, since
|
||||
// CreateNetworkContextParams() can only be called once.
|
||||
*network_context_params = CreateDefaultNetworkContextParams();
|
||||
}
|
||||
*http_auth_static_params = CreateHttpAuthStaticParams();
|
||||
*http_auth_dynamic_params = electron::CreateHttpAuthDynamicParams();
|
||||
}
|
||||
|
||||
// static
|
||||
SystemNetworkContextManager* SystemNetworkContextManager::CreateInstance(
|
||||
PrefService* pref_service) {
|
||||
|
@ -225,18 +196,13 @@ SystemNetworkContextManager::~SystemNetworkContextManager() {
|
|||
|
||||
void SystemNetworkContextManager::OnNetworkServiceCreated(
|
||||
network::mojom::NetworkService* network_service) {
|
||||
if (!base::FeatureList::IsEnabled(network::features::kNetworkService))
|
||||
return;
|
||||
|
||||
network_service->SetUpHttpAuth(CreateHttpAuthStaticParams());
|
||||
network_service->ConfigureHttpAuthPrefs(
|
||||
electron::CreateHttpAuthDynamicParams());
|
||||
network_service->ConfigureHttpAuthPrefs(CreateHttpAuthDynamicParams());
|
||||
|
||||
// The system NetworkContext must be created first, since it sets
|
||||
// |primary_network_context| to true.
|
||||
network_service->CreateNetworkContext(
|
||||
MakeRequest(&network_service_network_context_),
|
||||
CreateNetworkContextParams());
|
||||
network_service->CreateNetworkContext(MakeRequest(&network_context_),
|
||||
CreateNetworkContextParams());
|
||||
}
|
||||
|
||||
network::mojom::NetworkContextParamsPtr
|
||||
|
|
|
@ -59,19 +59,6 @@ class SystemNetworkContextManager {
|
|||
// Returns default set of parameters for configuring the network service.
|
||||
network::mojom::NetworkContextParamsPtr CreateDefaultNetworkContextParams();
|
||||
|
||||
// Initializes |network_context_params| as needed to set up a system
|
||||
// NetworkContext. If the network service is disabled,
|
||||
// |network_context_request| will be for the NetworkContext used by the
|
||||
// SystemNetworkContextManager. Otherwise, this method can still be used to
|
||||
// help set up the IOThread's in-process URLRequestContext.
|
||||
//
|
||||
// Must be called before the system NetworkContext is first used.
|
||||
void SetUp(
|
||||
network::mojom::NetworkContextRequest* network_context_request,
|
||||
network::mojom::NetworkContextParamsPtr* network_context_params,
|
||||
network::mojom::HttpAuthStaticParamsPtr* http_auth_static_params,
|
||||
network::mojom::HttpAuthDynamicParamsPtr* http_auth_dynamic_params);
|
||||
|
||||
// Returns the System NetworkContext. May only be called after SetUp(). Does
|
||||
// any initialization of the NetworkService that may be needed when first
|
||||
// called.
|
||||
|
@ -104,12 +91,7 @@ class SystemNetworkContextManager {
|
|||
|
||||
// NetworkContext using the network service, if the network service is
|
||||
// enabled. nullptr, otherwise.
|
||||
network::mojom::NetworkContextPtr network_service_network_context_;
|
||||
|
||||
// This is a NetworkContext that wraps the IOThread's SystemURLRequestContext.
|
||||
// Always initialized in SetUp, but it's only returned by Context() when the
|
||||
// network service is disabled.
|
||||
network::mojom::NetworkContextPtr io_thread_network_context_;
|
||||
network::mojom::NetworkContextPtr network_context_;
|
||||
|
||||
// URLLoaderFactory backed by the NetworkContext returned by GetContext(), so
|
||||
// consumers don't all need to create their own factory.
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
#include "net/cert/x509_certificate.h"
|
||||
#include "net/cert/x509_util.h"
|
||||
#include "net/http/http_response_headers.h"
|
||||
#include "net/url_request/url_request.h"
|
||||
#include "services/network/public/cpp/resource_request.h"
|
||||
#include "shell/browser/net/cert_verifier_client.h"
|
||||
#include "shell/common/native_mate_converters/gurl_converter.h"
|
||||
#include "shell/common/native_mate_converters/string16_converter.h"
|
||||
#include "shell/common/native_mate_converters/value_converter.h"
|
||||
|
@ -267,6 +267,18 @@ v8::Local<v8::Value> Converter<network::ResourceRequest>::ToV8(
|
|||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Local<v8::Value> Converter<electron::VerifyRequestParams>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
electron::VerifyRequestParams val) {
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||
dict.Set("hostname", val.hostname);
|
||||
dict.Set("certificate", val.certificate);
|
||||
dict.Set("verificationResult", val.default_result);
|
||||
dict.Set("errorCode", val.error_code);
|
||||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
} // namespace mate
|
||||
|
||||
namespace electron {
|
||||
|
|
|
@ -13,6 +13,10 @@ class DictionaryValue;
|
|||
class ListValue;
|
||||
} // namespace base
|
||||
|
||||
namespace electron {
|
||||
struct VerifyRequestParams;
|
||||
}
|
||||
|
||||
namespace net {
|
||||
class AuthChallengeInfo;
|
||||
class URLRequest;
|
||||
|
@ -65,6 +69,12 @@ struct Converter<network::ResourceRequest> {
|
|||
const network::ResourceRequest& val);
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<electron::VerifyRequestParams> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
electron::VerifyRequestParams val);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
namespace electron {
|
||||
|
|
Loading…
Reference in a new issue