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.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/iothread_state.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"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
|
|
|
#include "cef/libcef/common/net/scheme_registration.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"
|
2021-08-19 23:07:44 +02:00
|
|
|
#include "content/public/browser/global_routing_id.h"
|
2024-04-30 03:09:17 +02:00
|
|
|
#include "url/gurl.h"
|
|
|
|
|
2024-06-26 02:12:37 +02:00
|
|
|
CefIOThreadState::CefIOThreadState() = default;
|
2015-02-14 00:17:08 +01:00
|
|
|
|
2021-04-07 22:58:43 +02:00
|
|
|
CefIOThreadState::~CefIOThreadState() {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
}
|
2015-02-14 00:17:08 +01:00
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
void CefIOThreadState::AddHandler(
|
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
|
|
|
CefRefPtr<CefRequestContextHandler> handler) {
|
2019-03-24 00:40:32 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
2021-08-19 23:07:44 +02:00
|
|
|
handler_map_.AddHandler(global_id, handler);
|
2019-03-24 00:40:32 +01:00
|
|
|
}
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
void CefIOThreadState::RemoveHandler(
|
|
|
|
const content::GlobalRenderFrameHostId& global_id) {
|
2019-03-24 00:40:32 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
2021-08-19 23:07:44 +02:00
|
|
|
handler_map_.RemoveHandler(global_id);
|
2019-03-24 00:40:32 +01:00
|
|
|
}
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
CefRefPtr<CefRequestContextHandler> CefIOThreadState::GetHandler(
|
2021-08-19 23:07:44 +02:00
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
2019-10-01 15:55:16 +02:00
|
|
|
bool require_frame_match) const {
|
2016-10-17 20:14:44 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
2021-08-19 23:07:44 +02:00
|
|
|
return handler_map_.GetHandler(global_id, require_frame_match);
|
2016-10-17 20:14:44 +02:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
void CefIOThreadState::RegisterSchemeHandlerFactory(
|
2019-04-24 04:50:25 +02:00
|
|
|
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);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != scheme_handler_factory_map_.end()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
scheme_handler_factory_map_.erase(it);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
void CefIOThreadState::ClearSchemeHandlerFactories() {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
2019-04-30 22:45:13 +02:00
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
scheme_handler_factory_map_.clear();
|
|
|
|
}
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
CefRefPtr<CefSchemeHandlerFactory> CefIOThreadState::GetSchemeHandlerFactory(
|
2019-04-24 04:50:25 +02:00
|
|
|
const GURL& url) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (scheme_handler_factory_map_.empty()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
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));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != scheme_handler_factory_map_.end()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
return it->second;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try for a match with no specified hostname.
|
|
|
|
const auto it = scheme_handler_factory_map_.find(
|
|
|
|
std::make_pair(scheme_lower, std::string()));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != scheme_handler_factory_map_.end()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
return it->second;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|