2015-02-14 00:17:08 +01:00
|
|
|
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/resource_context.h"
|
2015-11-26 03:53:12 +01:00
|
|
|
|
2019-04-30 22:45:13 +02:00
|
|
|
#include "libcef/browser/net/scheme_handler.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
#include "libcef/browser/thread_util.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "libcef/common/net/scheme_registration.h"
|
2015-02-14 00:17:08 +01:00
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "base/i18n/case_conversion.h"
|
2015-02-14 00:17:08 +01:00
|
|
|
#include "base/logging.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "base/strings/string_util.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "content/browser/loader/resource_dispatcher_host_impl.h"
|
2016-11-07 20:14:09 +01:00
|
|
|
#include "content/browser/resource_context_impl.h"
|
2015-02-14 00:17:08 +01:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
|
2015-05-19 19:55:58 +02:00
|
|
|
#if defined(USE_NSS_CERTS)
|
|
|
|
#include "net/ssl/client_cert_store_nss.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "net/ssl/client_cert_store_win.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
#include "net/ssl/client_cert_store_mac.h"
|
|
|
|
#endif
|
|
|
|
|
2019-03-24 00:40:32 +01:00
|
|
|
CefResourceContext::CefResourceContext(bool is_off_the_record)
|
2019-04-30 22:45:13 +02:00
|
|
|
: is_off_the_record_(is_off_the_record) {
|
|
|
|
// Using base::Unretained() is safe because both this callback and possible
|
|
|
|
// deletion of |this| will execute on the IO thread, and this callback will
|
|
|
|
// be executed first.
|
|
|
|
CEF_POST_TASK(CEF_IOT, base::Bind(&CefResourceContext::InitOnIOThread,
|
|
|
|
base::Unretained(this)));
|
|
|
|
}
|
2015-02-14 00:17:08 +01:00
|
|
|
|
2019-07-29 23:27:12 +02:00
|
|
|
CefResourceContext::~CefResourceContext() {}
|
2015-02-14 00:17:08 +01:00
|
|
|
|
2016-07-06 21:34:09 +02:00
|
|
|
std::unique_ptr<net::ClientCertStore>
|
2017-05-17 11:29:28 +02:00
|
|
|
CefResourceContext::CreateClientCertStore() {
|
2015-05-19 19:55:58 +02:00
|
|
|
#if defined(USE_NSS_CERTS)
|
2016-04-27 22:38:52 +02:00
|
|
|
return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreNSS(
|
2015-05-19 19:55:58 +02:00
|
|
|
net::ClientCertStoreNSS::PasswordDelegateFactory()));
|
|
|
|
#elif defined(OS_WIN)
|
2016-04-27 22:38:52 +02:00
|
|
|
return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreWin());
|
2015-05-19 19:55:58 +02:00
|
|
|
#elif defined(OS_MACOSX)
|
2016-04-27 22:38:52 +02:00
|
|
|
return std::unique_ptr<net::ClientCertStore>(new net::ClientCertStoreMac());
|
2015-05-19 19:55:58 +02:00
|
|
|
#elif defined(USE_OPENSSL)
|
|
|
|
// OpenSSL does not use the ClientCertStore infrastructure. On Android client
|
|
|
|
// cert matching is done by the OS as part of the call to show the cert
|
|
|
|
// selection dialog.
|
2016-04-27 22:38:52 +02:00
|
|
|
return std::unique_ptr<net::ClientCertStore>();
|
2015-05-19 19:55:58 +02:00
|
|
|
#else
|
|
|
|
#error Unknown platform.
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
void CefResourceContext::set_extensions_info_map(
|
|
|
|
extensions::InfoMap* extensions_info_map) {
|
|
|
|
DCHECK(!extension_info_map_);
|
|
|
|
extension_info_map_ = extensions_info_map;
|
|
|
|
}
|
|
|
|
|
2019-03-24 00:40:32 +01:00
|
|
|
void CefResourceContext::AddHandler(
|
|
|
|
int render_process_id,
|
|
|
|
int render_frame_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
int frame_tree_node_id,
|
2019-03-24 00:40:32 +01:00
|
|
|
CefRefPtr<CefRequestContextHandler> handler) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
DCHECK_GE(render_process_id, 0);
|
2019-04-24 04:50:25 +02:00
|
|
|
DCHECK_GE(render_frame_id, 0);
|
|
|
|
DCHECK_GE(frame_tree_node_id, 0);
|
2019-03-24 00:40:32 +01:00
|
|
|
DCHECK(handler);
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
render_id_handler_map_.insert(std::make_pair(
|
2019-03-24 00:40:32 +01:00
|
|
|
std::make_pair(render_process_id, render_frame_id), handler));
|
2019-04-24 04:50:25 +02:00
|
|
|
node_id_handler_map_.insert(std::make_pair(frame_tree_node_id, handler));
|
2019-03-24 00:40:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefResourceContext::RemoveHandler(int render_process_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
int render_frame_id,
|
|
|
|
int frame_tree_node_id) {
|
2019-03-24 00:40:32 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
DCHECK_GE(render_process_id, 0);
|
2019-04-24 04:50:25 +02:00
|
|
|
DCHECK_GE(render_frame_id, 0);
|
|
|
|
DCHECK_GE(frame_tree_node_id, 0);
|
|
|
|
|
|
|
|
auto it1 = render_id_handler_map_.find(
|
|
|
|
std::make_pair(render_process_id, render_frame_id));
|
|
|
|
if (it1 != render_id_handler_map_.end())
|
|
|
|
render_id_handler_map_.erase(it1);
|
2019-03-24 00:40:32 +01:00
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
auto it2 = node_id_handler_map_.find(frame_tree_node_id);
|
|
|
|
if (it2 != node_id_handler_map_.end())
|
|
|
|
node_id_handler_map_.erase(it2);
|
2019-03-24 00:40:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefRequestContextHandler> CefResourceContext::GetHandler(
|
|
|
|
int render_process_id,
|
|
|
|
int render_frame_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
int frame_tree_node_id,
|
2019-03-24 00:40:32 +01:00
|
|
|
bool require_frame_match) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
if (render_process_id >= 0 && render_frame_id >= 0) {
|
|
|
|
const auto it1 = render_id_handler_map_.find(
|
|
|
|
std::make_pair(render_process_id, render_frame_id));
|
|
|
|
if (it1 != render_id_handler_map_.end())
|
|
|
|
return it1->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_tree_node_id >= 0) {
|
|
|
|
const auto it2 = node_id_handler_map_.find(frame_tree_node_id);
|
|
|
|
if (it2 != node_id_handler_map_.end())
|
|
|
|
return it2->second;
|
|
|
|
}
|
2019-03-24 00:40:32 +01:00
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
if (render_process_id >= 0 && !require_frame_match) {
|
2019-03-24 00:40:32 +01:00
|
|
|
// Choose an arbitrary handler for the same process.
|
2019-04-24 04:50:25 +02:00
|
|
|
for (auto& kv : render_id_handler_map_) {
|
2019-03-24 00:40:32 +01:00
|
|
|
if (kv.first.first == render_process_id)
|
|
|
|
return kv.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-17 20:14:44 +02:00
|
|
|
void CefResourceContext::AddPluginLoadDecision(
|
|
|
|
int render_process_id,
|
|
|
|
const base::FilePath& plugin_path,
|
2017-01-19 00:37:56 +01:00
|
|
|
bool is_main_frame,
|
|
|
|
const url::Origin& main_frame_origin,
|
2017-12-07 22:44:24 +01:00
|
|
|
chrome::mojom::PluginStatus status) {
|
2016-10-17 20:14:44 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
DCHECK_GE(render_process_id, 0);
|
|
|
|
DCHECK(!plugin_path.empty());
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
plugin_load_decision_map_.insert(std::make_pair(
|
|
|
|
std::make_pair(std::make_pair(render_process_id, plugin_path),
|
|
|
|
std::make_pair(is_main_frame, main_frame_origin)),
|
|
|
|
status));
|
2016-10-17 20:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefResourceContext::HasPluginLoadDecision(
|
|
|
|
int render_process_id,
|
|
|
|
const base::FilePath& plugin_path,
|
2017-01-19 00:37:56 +01:00
|
|
|
bool is_main_frame,
|
|
|
|
const url::Origin& main_frame_origin,
|
2017-12-07 22:44:24 +01:00
|
|
|
chrome::mojom::PluginStatus* status) const {
|
2016-10-17 20:14:44 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
DCHECK_GE(render_process_id, 0);
|
|
|
|
DCHECK(!plugin_path.empty());
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
PluginLoadDecisionMap::const_iterator it = plugin_load_decision_map_.find(
|
|
|
|
std::make_pair(std::make_pair(render_process_id, plugin_path),
|
|
|
|
std::make_pair(is_main_frame, main_frame_origin)));
|
2016-10-17 20:14:44 +02:00
|
|
|
if (it == plugin_load_decision_map_.end())
|
|
|
|
return false;
|
|
|
|
|
2017-01-19 00:37:56 +01:00
|
|
|
*status = it->second;
|
2016-10-17 20:14:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResourceContext::ClearPluginLoadDecision(int render_process_id) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
if (render_process_id == -1) {
|
|
|
|
plugin_load_decision_map_.clear();
|
|
|
|
} else {
|
|
|
|
PluginLoadDecisionMap::iterator it = plugin_load_decision_map_.begin();
|
|
|
|
while (it != plugin_load_decision_map_.end()) {
|
2017-01-19 00:37:56 +01:00
|
|
|
if (it->first.first.first == render_process_id)
|
2016-10-17 20:14:44 +02:00
|
|
|
it = plugin_load_decision_map_.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
void CefResourceContext::RegisterSchemeHandlerFactory(
|
|
|
|
const std::string& scheme_name,
|
|
|
|
const std::string& domain_name,
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> factory) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
const std::string& scheme_lower = base::ToLowerASCII(scheme_name);
|
|
|
|
std::string domain_lower;
|
|
|
|
|
|
|
|
// Hostname is only supported for standard schemes.
|
|
|
|
if (scheme::IsStandardScheme(scheme_lower)) {
|
|
|
|
// Hostname might contain Unicode characters.
|
|
|
|
domain_lower =
|
|
|
|
base::UTF16ToUTF8(base::i18n::ToLower(base::UTF8ToUTF16(domain_name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto key = std::make_pair(scheme_lower, domain_lower);
|
|
|
|
|
|
|
|
if (factory) {
|
|
|
|
// Add or replace the factory.
|
|
|
|
scheme_handler_factory_map_[key] = factory;
|
|
|
|
} else {
|
|
|
|
// Remove the existing factory, if any.
|
|
|
|
auto it = scheme_handler_factory_map_.find(key);
|
|
|
|
if (it != scheme_handler_factory_map_.end())
|
|
|
|
scheme_handler_factory_map_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefResourceContext::ClearSchemeHandlerFactories() {
|
|
|
|
CEF_REQUIRE_IOT();
|
2019-04-30 22:45:13 +02:00
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
scheme_handler_factory_map_.clear();
|
2019-04-30 22:45:13 +02:00
|
|
|
|
|
|
|
// Restore the default internal handlers.
|
|
|
|
scheme::RegisterInternalHandlers(this);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> CefResourceContext::GetSchemeHandlerFactory(
|
|
|
|
const GURL& url) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
if (scheme_handler_factory_map_.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const std::string& scheme_lower = url.scheme();
|
|
|
|
const std::string& domain_lower =
|
|
|
|
url.IsStandard() ? url.host() : std::string();
|
|
|
|
|
|
|
|
if (!domain_lower.empty()) {
|
|
|
|
// Sanity check.
|
|
|
|
DCHECK(scheme::IsStandardScheme(scheme_lower)) << scheme_lower;
|
|
|
|
|
|
|
|
// Try for a match with hostname first.
|
|
|
|
const auto it = scheme_handler_factory_map_.find(
|
|
|
|
std::make_pair(scheme_lower, domain_lower));
|
|
|
|
if (it != scheme_handler_factory_map_.end())
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try for a match with no specified hostname.
|
|
|
|
const auto it = scheme_handler_factory_map_.find(
|
|
|
|
std::make_pair(scheme_lower, std::string()));
|
|
|
|
if (it != scheme_handler_factory_map_.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-04-30 22:45:13 +02:00
|
|
|
|
|
|
|
void CefResourceContext::InitOnIOThread() {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2019-07-29 23:27:12 +02:00
|
|
|
// Add the default internal handlers.
|
|
|
|
scheme::RegisterInternalHandlers(this);
|
2019-04-30 22:45:13 +02:00
|
|
|
}
|