diff options
Diffstat (limited to 'src/core/custom_handlers')
7 files changed, 316 insertions, 0 deletions
diff --git a/src/core/custom_handlers/protocol_handler_registry_delegate_qt.cpp b/src/core/custom_handlers/protocol_handler_registry_delegate_qt.cpp new file mode 100644 index 000000000..a5074fb88 --- /dev/null +++ b/src/core/custom_handlers/protocol_handler_registry_delegate_qt.cpp @@ -0,0 +1,36 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +// based on chrome/browser/custom_handlers/chrome_protocol_handler_registry_delegate.cc: +// Copyright 2021 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 "protocol_handler_registry_delegate_qt.h" + +#include "content/public/browser/child_process_security_policy.h" +#include "url/url_util_qt.h" + +namespace QtWebEngineCore { + +using content::ChildProcessSecurityPolicy; + +ProtocolHandlerRegistryDelegateQt::ProtocolHandlerRegistryDelegateQt() = default; + +ProtocolHandlerRegistryDelegateQt::~ProtocolHandlerRegistryDelegateQt() = default; + +// ProtocolHandlerRegistry::Delegate: +void ProtocolHandlerRegistryDelegateQt::RegisterExternalHandler(const std::string &protocol) +{ + ChildProcessSecurityPolicy *policy = ChildProcessSecurityPolicy::GetInstance(); + if (!policy->IsWebSafeScheme(protocol)) { + policy->RegisterWebSafeScheme(protocol); + } +} + +bool ProtocolHandlerRegistryDelegateQt::IsExternalHandlerRegistered(const std::string &protocol) +{ + return url::IsHandledProtocol(protocol); +} + +} // namespace QtWebEngineCore diff --git a/src/core/custom_handlers/protocol_handler_registry_delegate_qt.h b/src/core/custom_handlers/protocol_handler_registry_delegate_qt.h new file mode 100644 index 000000000..6a0753d22 --- /dev/null +++ b/src/core/custom_handlers/protocol_handler_registry_delegate_qt.h @@ -0,0 +1,35 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +// based on chrome/browser/custom_handlers/chrome_protocol_handler_registry_delegate.h: +// Copyright 2021 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. + +#ifndef PROTOCOL_HANDLER_REGISTRY_DELEGATE_QT_H_ +#define PROTOCOL_HANDLER_REGISTRY_DELEGATE_QT_H_ + +#include <string> + +#include "components/custom_handlers/protocol_handler_registry.h" + +namespace QtWebEngineCore { + +// This class implements the ProtocolHandlerRegistry::Delegate +// abstract class to provide an OS dependent implementation +class ProtocolHandlerRegistryDelegateQt : public custom_handlers::ProtocolHandlerRegistry::Delegate { +public: + ProtocolHandlerRegistryDelegateQt(); + ~ProtocolHandlerRegistryDelegateQt() override; + + ProtocolHandlerRegistryDelegateQt(const ProtocolHandlerRegistryDelegateQt &other) = delete; + ProtocolHandlerRegistryDelegateQt &operator=(const ProtocolHandlerRegistryDelegateQt &other) = delete; + + // ProtocolHandlerRegistry::Delegate: + void RegisterExternalHandler(const std::string &protocol) override; + bool IsExternalHandlerRegistered(const std::string &protocol) override; +}; + +} // namespace QtWebEngineCore + +#endif // PROTOCOL_HANDLER_REGISTRY_DELEGATE_QT_H_ diff --git a/src/core/custom_handlers/protocol_handler_registry_factory.cpp b/src/core/custom_handlers/protocol_handler_registry_factory.cpp new file mode 100644 index 000000000..50b17006b --- /dev/null +++ b/src/core/custom_handlers/protocol_handler_registry_factory.cpp @@ -0,0 +1,75 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +// based on chrome/browser/custom_handlers/protocol_handler_registry_factory.cc +// Copyright (c) 2012 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 "protocol_handler_registry_factory.h" + +#include <memory> + +#include "base/memory/singleton.h" +#include "components/custom_handlers/protocol_handler_registry.h" +#include "components/keyed_service/content/browser_context_dependency_manager.h" + +#include "protocol_handler_registry_delegate_qt.h" + +namespace QtWebEngineCore { + +// static +ProtocolHandlerRegistryFactory *ProtocolHandlerRegistryFactory::GetInstance() +{ + return base::Singleton<ProtocolHandlerRegistryFactory>::get(); +} + +// static +custom_handlers::ProtocolHandlerRegistry *ProtocolHandlerRegistryFactory::GetForBrowserContext(content::BrowserContext *context) +{ + return static_cast<custom_handlers::ProtocolHandlerRegistry *>(GetInstance()->GetServiceForBrowserContext(context, true)); +} + +ProtocolHandlerRegistryFactory::ProtocolHandlerRegistryFactory() + : BrowserContextKeyedServiceFactory("ProtocolHandlerRegistry", BrowserContextDependencyManager::GetInstance()) {} + +ProtocolHandlerRegistryFactory::~ProtocolHandlerRegistryFactory() +{ +} + +// Will be created when initializing profile_io_data, so we might +// as well have the framework create this along with other +// PKSs to preserve orderly civic conduct :) +bool ProtocolHandlerRegistryFactory::ServiceIsCreatedWithBrowserContext() const +{ + return true; +} + +// Allows the produced registry to be used in incognito mode. +content::BrowserContext *ProtocolHandlerRegistryFactory::GetBrowserContextToUse(content::BrowserContext *context) const +{ + return context; +// return chrome::GetBrowserContextRedirectedInIncognito(context); +} + +// Do not create this service for tests. MANY tests will fail +// due to the threading requirements of this service. ALSO, +// not creating this increases test isolation (which is GOOD!) +bool ProtocolHandlerRegistryFactory::ServiceIsNULLWhileTesting() const +{ + return true; +} + +KeyedService *ProtocolHandlerRegistryFactory::BuildServiceInstanceFor(content::BrowserContext *context) const +{ + custom_handlers::ProtocolHandlerRegistry *registry = + new custom_handlers::ProtocolHandlerRegistry(/*prefs*/ nullptr, + std::make_unique<ProtocolHandlerRegistryDelegateQt>()); + + // Must be called as a part of the creation process. + registry->InitProtocolSettings(); + + return registry; +} + +} // namespace QtWebEngineCore diff --git a/src/core/custom_handlers/protocol_handler_registry_factory.h b/src/core/custom_handlers/protocol_handler_registry_factory.h new file mode 100644 index 000000000..6559addc0 --- /dev/null +++ b/src/core/custom_handlers/protocol_handler_registry_factory.h @@ -0,0 +1,57 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +// based on chrome/browser/custom_handlers/protocol_handler_registry_factory.h: +// Copyright (c) 2012 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. + +#ifndef PROTOCOL_HANDLER_REGISTRY_FACTORY_H_ +#define PROTOCOL_HANDLER_REGISTRY_FACTORY_H_ + +#include "components/keyed_service/content/browser_context_keyed_service_factory.h" + +namespace custom_handlers { +class ProtocolHandlerRegistry; +} + +namespace base { +template <typename T> struct DefaultSingletonTraits; +} + +namespace QtWebEngineCore { + +// Singleton that owns all ProtocolHandlerRegistrys and associates them with +// Profiles. Listens for the Profile's destruction notification and cleans up +// the associated ProtocolHandlerRegistry. +class ProtocolHandlerRegistryFactory : public BrowserContextKeyedServiceFactory { +public: + // Returns the singleton instance of the ProtocolHandlerRegistryFactory. + static ProtocolHandlerRegistryFactory *GetInstance(); + + // Returns the ProtocolHandlerRegistry that provides intent registration for + // |context|. Ownership stays with this factory object. + static custom_handlers::ProtocolHandlerRegistry *GetForBrowserContext(content::BrowserContext *context); + + ProtocolHandlerRegistryFactory(const ProtocolHandlerRegistryFactory &) = delete; + ProtocolHandlerRegistryFactory &operator=(const ProtocolHandlerRegistryFactory &) = delete; + +protected: + // BrowserContextKeyedServiceFactory implementation. + bool ServiceIsCreatedWithBrowserContext() const override; + content::BrowserContext *GetBrowserContextToUse(content::BrowserContext *context) const override; + bool ServiceIsNULLWhileTesting() const override; + +private: + friend struct base::DefaultSingletonTraits<ProtocolHandlerRegistryFactory>; + + ProtocolHandlerRegistryFactory(); + ~ProtocolHandlerRegistryFactory() override; + + // BrowserContextKeyedServiceFactory implementation. + KeyedService *BuildServiceInstanceFor(content::BrowserContext *profile) const override; +}; + +} // namespace QtWebEngineCore + +#endif // PROTOCOL_HANDLER_REGISTRY_FACTORY_H_ diff --git a/src/core/custom_handlers/register_protocol_handler_request_controller.h b/src/core/custom_handlers/register_protocol_handler_request_controller.h new file mode 100644 index 000000000..6305ce5b7 --- /dev/null +++ b/src/core/custom_handlers/register_protocol_handler_request_controller.h @@ -0,0 +1,26 @@ +// Copyright (C) 2018 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef REGISTER_PROTOCOL_HANDLER_REQUEST_CONTROLLER_H +#define REGISTER_PROTOCOL_HANDLER_REQUEST_CONTROLLER_H + +#include "request_controller.h" + +namespace QtWebEngineCore { + +class RegisterProtocolHandlerRequestController : public RequestController { +public: + RegisterProtocolHandlerRequestController(QUrl origin, QString scheme) + : RequestController(std::move(origin)) + , m_scheme(std::move(scheme)) + {} + + QString scheme() const { return m_scheme; } + +private: + QString m_scheme; +}; + +} // namespace QtWebEngineCore + +#endif // REGISTER_PROTOCOL_HANDLER_REQUEST_CONTROLLER_H diff --git a/src/core/custom_handlers/register_protocol_handler_request_controller_impl.cpp b/src/core/custom_handlers/register_protocol_handler_request_controller_impl.cpp new file mode 100644 index 000000000..efaf54cd2 --- /dev/null +++ b/src/core/custom_handlers/register_protocol_handler_request_controller_impl.cpp @@ -0,0 +1,49 @@ +// Copyright (C) 2018 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +#include "custom_handlers/register_protocol_handler_request_controller_impl.h" + +#include "components/custom_handlers/protocol_handler_registry.h" +#include "content/public/browser/web_contents.h" + +#include "custom_handlers/protocol_handler_registry_factory.h" +#include "type_conversion.h" + +namespace QtWebEngineCore { + +RegisterProtocolHandlerRequestControllerImpl::RegisterProtocolHandlerRequestControllerImpl( + content::WebContents *webContents, + custom_handlers::ProtocolHandler handler) + : RegisterProtocolHandlerRequestController( + toQt(handler.url()), + toQt(handler.protocol())) + , content::WebContentsObserver(webContents) + , m_handler(handler) +{} + +RegisterProtocolHandlerRequestControllerImpl::~RegisterProtocolHandlerRequestControllerImpl() +{ + reject(); +} + +custom_handlers::ProtocolHandlerRegistry *RegisterProtocolHandlerRequestControllerImpl::protocolHandlerRegistry() +{ + content::WebContents *webContents = web_contents(); + if (!webContents) + return nullptr; + content::BrowserContext *context = webContents->GetBrowserContext(); + return ProtocolHandlerRegistryFactory::GetForBrowserContext(context); +} + +void RegisterProtocolHandlerRequestControllerImpl::accepted() +{ + if (custom_handlers::ProtocolHandlerRegistry *registry = protocolHandlerRegistry()) + registry->OnAcceptRegisterProtocolHandler(m_handler); +} + +void RegisterProtocolHandlerRequestControllerImpl::rejected() +{ + if (custom_handlers::ProtocolHandlerRegistry *registry = protocolHandlerRegistry()) + registry->OnIgnoreRegisterProtocolHandler(m_handler); +} + +} // namespace QtWebEngineCore diff --git a/src/core/custom_handlers/register_protocol_handler_request_controller_impl.h b/src/core/custom_handlers/register_protocol_handler_request_controller_impl.h new file mode 100644 index 000000000..073ca9bf8 --- /dev/null +++ b/src/core/custom_handlers/register_protocol_handler_request_controller_impl.h @@ -0,0 +1,38 @@ +// Copyright (C) 2018 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef REGISTER_PROTOCOL_HANDLER_REQUEST_CONTROLLER_IMPL_H +#define REGISTER_PROTOCOL_HANDLER_REQUEST_CONTROLLER_IMPL_H + +#include "register_protocol_handler_request_controller.h" + +#include "content/public/browser/web_contents_observer.h" +#include "components/custom_handlers/protocol_handler.h" + +namespace custom_handlers { +class ProtocolHandlerRegistry; +} + +namespace QtWebEngineCore { + +class RegisterProtocolHandlerRequestControllerImpl final : public RegisterProtocolHandlerRequestController, + private content::WebContentsObserver { +public: + RegisterProtocolHandlerRequestControllerImpl( + content::WebContents *webContents, + custom_handlers::ProtocolHandler handler); + + ~RegisterProtocolHandlerRequestControllerImpl(); + +protected: + void accepted() override; + void rejected() override; + +private: + custom_handlers::ProtocolHandlerRegistry *protocolHandlerRegistry(); + custom_handlers::ProtocolHandler m_handler; +}; + +} // namespace QtWebEngineCore + +#endif // REGISTER_PROTOCOL_HANDLER_REQUEST_CONTROLLER_IMPL_H |