From 835b24055cfbc953fd4c844d264e7fbc8550d575 Mon Sep 17 00:00:00 2001 From: Szabolcs David Date: Thu, 28 Jan 2016 04:49:02 -0800 Subject: Rename UserScriptController/Host to UserResourceController/Host These classes can operate user stylesheets too. Change-Id: Ia283af92e52a822b26003ff65e0e7dc391b0904d Reviewed-by: Allan Sandfeld Jensen --- src/core/browser_context_adapter.cpp | 10 +- src/core/browser_context_adapter.h | 6 +- src/core/common/qt_messages.h | 6 +- src/core/content_browser_client_qt.cpp | 4 +- src/core/core_gyp_generator.pro | 8 +- src/core/renderer/content_renderer_client_qt.cpp | 6 +- src/core/renderer/user_resource_controller.cpp | 262 +++++++++++++++++++++++ src/core/renderer/user_resource_controller.h | 85 ++++++++ src/core/renderer/user_script_controller.cpp | 262 ----------------------- src/core/renderer/user_script_controller.h | 85 -------- src/core/user_resource_controller_host.cpp | 238 ++++++++++++++++++++ src/core/user_resource_controller_host.h | 90 ++++++++ src/core/user_script.cpp | 1 - src/core/user_script.h | 4 +- src/core/user_script_controller_host.cpp | 238 -------------------- src/core/user_script_controller_host.h | 90 -------- src/core/web_contents_adapter_p.h | 1 - 17 files changed, 697 insertions(+), 699 deletions(-) create mode 100644 src/core/renderer/user_resource_controller.cpp create mode 100644 src/core/renderer/user_resource_controller.h delete mode 100644 src/core/renderer/user_script_controller.cpp delete mode 100644 src/core/renderer/user_script_controller.h create mode 100644 src/core/user_resource_controller_host.cpp create mode 100644 src/core/user_resource_controller_host.h delete mode 100644 src/core/user_script_controller_host.cpp delete mode 100644 src/core/user_script_controller_host.h (limited to 'src/core') diff --git a/src/core/browser_context_adapter.cpp b/src/core/browser_context_adapter.cpp index dea845e68..19280cf10 100644 --- a/src/core/browser_context_adapter.cpp +++ b/src/core/browser_context_adapter.cpp @@ -48,7 +48,7 @@ #include "web_engine_context.h" #include "web_engine_visited_links_manager.h" #include "url_request_context_getter_qt.h" -#include "user_script_controller_host.h" +#include "user_resource_controller_host.h" #include "net/proxy/proxy_service.h" @@ -391,11 +391,11 @@ void BrowserContextAdapter::addCustomUrlSchemeHandler(const QByteArray &scheme, updateCustomUrlSchemeHandlers(); } -UserScriptControllerHost *BrowserContextAdapter::userScriptController() +UserResourceControllerHost *BrowserContextAdapter::userResourceController() { - if (!m_userScriptController) - m_userScriptController.reset(new UserScriptControllerHost); - return m_userScriptController.data(); + if (!m_userResourceController) + m_userResourceController.reset(new UserResourceControllerHost); + return m_userResourceController.data(); } void BrowserContextAdapter::permissionRequestReply(const QUrl &origin, PermissionType type, bool reply) diff --git a/src/core/browser_context_adapter.h b/src/core/browser_context_adapter.h index 8eb7631f7..55ab688a7 100644 --- a/src/core/browser_context_adapter.h +++ b/src/core/browser_context_adapter.h @@ -60,7 +60,7 @@ namespace QtWebEngineCore { class BrowserContextAdapterClient; class BrowserContextQt; class DownloadManagerDelegateQt; -class UserScriptControllerHost; +class UserResourceControllerHost; class WebEngineVisitedLinksManager; class QWEBENGINE_EXPORT BrowserContextAdapter : public QSharedData @@ -160,7 +160,7 @@ public: void addCustomUrlSchemeHandler(const QByteArray &, QWebEngineUrlSchemeHandler *); bool removeCustomUrlSchemeHandler(QWebEngineUrlSchemeHandler *); QWebEngineUrlSchemeHandler *takeCustomUrlSchemeHandler(const QByteArray &); - UserScriptControllerHost *userScriptController(); + UserResourceControllerHost *userResourceController(); void permissionRequestReply(const QUrl &origin, PermissionType type, bool reply); @@ -176,7 +176,7 @@ private: QScopedPointer m_browserContext; QScopedPointer m_visitedLinksManager; QScopedPointer m_downloadManagerDelegate; - QScopedPointer m_userScriptController; + QScopedPointer m_userResourceController; QScopedPointer m_cookieStore; QPointer m_requestInterceptor; diff --git a/src/core/common/qt_messages.h b/src/core/common/qt_messages.h index de30c34d6..d76b1c911 100644 --- a/src/core/common/qt_messages.h +++ b/src/core/common/qt_messages.h @@ -45,9 +45,9 @@ IPC_MESSAGE_ROUTED1(RenderViewObserverHelper_RemoveScript, UserScriptData /* script */) IPC_MESSAGE_ROUTED0(RenderViewObserverHelper_ClearScripts) -IPC_MESSAGE_CONTROL1(UserScriptController_AddScript, UserScriptData /* scriptContents */) -IPC_MESSAGE_CONTROL1(UserScriptController_RemoveScript, UserScriptData /* scriptContents */) -IPC_MESSAGE_CONTROL0(UserScriptController_ClearScripts) +IPC_MESSAGE_CONTROL1(UserResourceController_AddScript, UserScriptData /* scriptContents */) +IPC_MESSAGE_CONTROL1(UserResourceController_RemoveScript, UserScriptData /* scriptContents */) +IPC_MESSAGE_CONTROL0(UserResourceController_ClearScripts) //----------------------------------------------------------------------------- // WebContents messages diff --git a/src/core/content_browser_client_qt.cpp b/src/core/content_browser_client_qt.cpp index 9e021dd6a..97d43fb90 100644 --- a/src/core/content_browser_client_qt.cpp +++ b/src/core/content_browser_client_qt.cpp @@ -80,7 +80,7 @@ #include "printing_message_filter_qt.h" #endif // defined(ENABLE_BASIC_PRINTING) #include "resource_dispatcher_host_delegate_qt.h" -#include "user_script_controller_host.h" +#include "user_resource_controller_host.h" #include "web_contents_delegate_qt.h" #include "web_engine_context.h" #include "web_engine_library_info.h" @@ -360,7 +360,7 @@ void ContentBrowserClientQt::RenderProcessWillLaunch(content::RenderProcessHost* // FIXME: Add a settings variable to enable/disable the file scheme. const int id = host->GetID(); content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(id, url::kFileScheme); - static_cast(host->GetBrowserContext())->m_adapter->userScriptController()->renderProcessStartedWithHost(host); + static_cast(host->GetBrowserContext())->m_adapter->userResourceController()->renderProcessStartedWithHost(host); #if defined(ENABLE_PEPPER_CDMS) host->AddFilter(new BrowserMessageFilterQt(id)); #endif diff --git a/src/core/core_gyp_generator.pro b/src/core/core_gyp_generator.pro index 5e6641d51..327ad9625 100644 --- a/src/core/core_gyp_generator.pro +++ b/src/core/core_gyp_generator.pro @@ -76,7 +76,7 @@ SOURCES = \ renderer/pepper/pepper_renderer_host_factory_qt.cpp \ renderer/render_frame_observer_qt.cpp \ renderer/render_view_observer_qt.cpp \ - renderer/user_script_controller.cpp \ + renderer/user_resource_controller.cpp \ renderer/web_channel_ipc_transport.cpp \ resource_bundle_qt.cpp \ resource_context_qt.cpp \ @@ -88,8 +88,8 @@ SOURCES = \ url_request_custom_job.cpp \ url_request_custom_job_delegate.cpp \ url_request_qrc_job_qt.cpp \ + user_resource_controller_host.cpp \ user_script.cpp \ - user_script_controller_host.cpp \ web_channel_ipc_transport_host.cpp \ web_contents_adapter.cpp \ web_contents_delegate_qt.cpp \ @@ -156,7 +156,7 @@ HEADERS = \ renderer/pepper/pepper_renderer_host_factory_qt.h \ renderer/render_frame_observer_qt.h \ renderer/render_view_observer_qt.h \ - renderer/user_script_controller.h \ + renderer/user_resource_controller.h \ renderer/web_channel_ipc_transport.h \ resource_context_qt.h \ resource_dispatcher_host_delegate_qt.h \ @@ -167,8 +167,8 @@ HEADERS = \ url_request_custom_job.h \ url_request_custom_job_delegate.h \ url_request_qrc_job_qt.h \ + user_resource_controller_host.h \ user_script.h \ - user_script_controller_host.h \ web_channel_ipc_transport_host.h \ web_contents_adapter.h \ web_contents_adapter_client.h \ diff --git a/src/core/renderer/content_renderer_client_qt.cpp b/src/core/renderer/content_renderer_client_qt.cpp index 77d93226c..3d235ff0b 100644 --- a/src/core/renderer/content_renderer_client_qt.cpp +++ b/src/core/renderer/content_renderer_client_qt.cpp @@ -74,7 +74,7 @@ #include "renderer/render_frame_observer_qt.h" #include "renderer/render_view_observer_qt.h" -#include "renderer/user_script_controller.h" +#include "renderer/user_resource_controller.h" #include "grit/renderer_resources.h" @@ -100,7 +100,7 @@ void ContentRendererClientQt::RenderThreadStarted() m_webCacheObserver.reset(new web_cache::WebCacheRenderProcessObserver()); renderThread->AddObserver(m_visitedLinkSlave.data()); renderThread->AddObserver(m_webCacheObserver.data()); - renderThread->AddObserver(UserScriptController::instance()); + renderThread->AddObserver(UserResourceController::instance()); // mark qrc as a secure scheme (avoids deprecation warnings) blink::WebSecurityPolicy::registerURLSchemeAsSecure(blink::WebString::fromLatin1(kQrcSchemeQt)); @@ -115,7 +115,7 @@ void ContentRendererClientQt::RenderViewCreated(content::RenderView* render_view // RenderViewObservers destroy themselves with their RenderView. new RenderViewObserverQt(render_view, m_webCacheObserver.data()); new WebChannelIPCTransport(render_view); - UserScriptController::instance()->renderViewCreated(render_view); + UserResourceController::instance()->renderViewCreated(render_view); #if defined(ENABLE_SPELLCHECK) new SpellCheckProvider(render_view, m_spellCheck.data()); #endif diff --git a/src/core/renderer/user_resource_controller.cpp b/src/core/renderer/user_resource_controller.cpp new file mode 100644 index 000000000..0927aa53a --- /dev/null +++ b/src/core/renderer/user_resource_controller.cpp @@ -0,0 +1,262 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "user_resource_controller.h" + +#include "content/public/renderer/render_view.h" +#include "content/public/renderer/render_view_observer.h" +#include "third_party/WebKit/public/web/WebLocalFrame.h" +#include "third_party/WebKit/public/web/WebScriptSource.h" +#include "third_party/WebKit/public/web/WebView.h" +#include "v8/include/v8.h" + +#include "common/qt_messages.h" +#include "common/user_script_data.h" + +Q_GLOBAL_STATIC(UserResourceController, qt_webengine_userResourceController) + +static content::RenderView * const globalScriptsIndex = 0; + +// Scripts meant to run after the load event will be run 500ms after DOMContentLoaded if the load event doesn't come within that delay. +static const int afterLoadTimeout = 500; + +class UserResourceController::RenderViewObserverHelper : public content::RenderViewObserver +{ +public: + RenderViewObserverHelper(content::RenderView *); +private: + // RenderViewObserver implementation. + virtual void DidCreateDocumentElement(blink::WebLocalFrame* frame) Q_DECL_OVERRIDE; + virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) Q_DECL_OVERRIDE; + virtual void DidFinishLoad(blink::WebLocalFrame* frame) Q_DECL_OVERRIDE; + virtual void DidStartProvisionalLoad(blink::WebLocalFrame* frame) Q_DECL_OVERRIDE; + virtual void FrameDetached(blink::WebFrame* frame) Q_DECL_OVERRIDE; + virtual void OnDestruct() Q_DECL_OVERRIDE; + virtual bool OnMessageReceived(const IPC::Message& message) Q_DECL_OVERRIDE; + + void onUserScriptAdded(const UserScriptData &); + void onUserScriptRemoved(const UserScriptData &); + void onScriptsCleared(); + + void runScripts(UserScriptData::InjectionPoint, blink::WebLocalFrame *); + QSet m_pendingFrames; +}; + +void UserResourceController::RenderViewObserverHelper::runScripts(UserScriptData::InjectionPoint p, blink::WebLocalFrame *frame) +{ + if (p == UserScriptData::AfterLoad && !m_pendingFrames.remove(frame)) + return; + content::RenderView *renderView = content::RenderView::FromWebView(frame->view()); + const bool isMainFrame = (frame == renderView->GetWebView()->mainFrame()); + + QList scriptsToRun = UserResourceController::instance()->m_viewUserScriptMap.value(globalScriptsIndex).toList(); + scriptsToRun.append(UserResourceController::instance()->m_viewUserScriptMap.value(renderView).toList()); + + Q_FOREACH (uint64 id, scriptsToRun) { + const UserScriptData &script = UserResourceController::instance()->m_scripts.value(id); + if (script.injectionPoint != p + || (!script.injectForSubframes && !isMainFrame)) + continue; + blink::WebScriptSource source(blink::WebString::fromUTF8(script.source), script.url); + if (script.worldId) + frame->executeScriptInIsolatedWorld(script.worldId, &source, /*numSources = */1, /*contentScriptExtentsionGroup = */ 0); + else + frame->executeScript(source); + } +} + + +UserResourceController::RenderViewObserverHelper::RenderViewObserverHelper(content::RenderView *renderView) + : content::RenderViewObserver(renderView) +{ +} + +void UserResourceController::RenderViewObserverHelper::DidCreateDocumentElement(blink::WebLocalFrame *frame) +{ + runScripts(UserScriptData::DocumentElementCreation, frame); +} + +void UserResourceController::RenderViewObserverHelper::DidFinishDocumentLoad(blink::WebLocalFrame *frame) +{ + runScripts(UserScriptData::DocumentLoadFinished, frame); + m_pendingFrames.insert(frame); + base::MessageLoop::current()->PostDelayedTask(FROM_HERE, base::Bind(&UserResourceController::RenderViewObserverHelper::runScripts, + base::Unretained(this), UserScriptData::AfterLoad, frame), + base::TimeDelta::FromMilliseconds(afterLoadTimeout)); +} + +void UserResourceController::RenderViewObserverHelper::DidFinishLoad(blink::WebLocalFrame *frame) +{ + // DidFinishDocumentLoad always comes before this, so frame has already been marked as pending. + base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&UserResourceController::RenderViewObserverHelper::runScripts, + base::Unretained(this), UserScriptData::AfterLoad, frame)); +} + +void UserResourceController::RenderViewObserverHelper::DidStartProvisionalLoad(blink::WebLocalFrame *frame) +{ + m_pendingFrames.remove(frame); +} + +void UserResourceController::RenderViewObserverHelper::FrameDetached(blink::WebFrame *frame) +{ + if (frame->isWebLocalFrame()) + m_pendingFrames.remove(frame->toWebLocalFrame()); +} + +void UserResourceController::RenderViewObserverHelper::OnDestruct() +{ + UserResourceController::instance()->renderViewDestroyed(render_view()); +} + +bool UserResourceController::RenderViewObserverHelper::OnMessageReceived(const IPC::Message &message) +{ + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(UserResourceController::RenderViewObserverHelper, message) + IPC_MESSAGE_HANDLER(RenderViewObserverHelper_AddScript, onUserScriptAdded) + IPC_MESSAGE_HANDLER(RenderViewObserverHelper_RemoveScript, onUserScriptRemoved) + IPC_MESSAGE_HANDLER(RenderViewObserverHelper_ClearScripts, onScriptsCleared) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +void UserResourceController::RenderViewObserverHelper::onUserScriptAdded(const UserScriptData &script) +{ + UserResourceController::instance()->addScriptForView(script, render_view()); +} + +void UserResourceController::RenderViewObserverHelper::onUserScriptRemoved(const UserScriptData &script) +{ + UserResourceController::instance()->removeScriptForView(script, render_view()); +} + +void UserResourceController::RenderViewObserverHelper::onScriptsCleared() +{ + UserResourceController::instance()->clearScriptsForView(render_view()); +} + +UserResourceController *UserResourceController::instance() +{ + return qt_webengine_userResourceController(); +} + +bool UserResourceController::OnControlMessageReceived(const IPC::Message &message) +{ + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(UserResourceController, message) + IPC_MESSAGE_HANDLER(UserResourceController_AddScript, onAddScript) + IPC_MESSAGE_HANDLER(UserResourceController_RemoveScript, onRemoveScript) + IPC_MESSAGE_HANDLER(UserResourceController_ClearScripts, onClearScripts) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +UserResourceController::UserResourceController() +{ +#if !defined(QT_NO_DEBUG) || defined(QT_FORCE_ASSERTS) + static bool onlyCalledOnce = true; + Q_ASSERT(onlyCalledOnce); + onlyCalledOnce = false; +#endif // !defined(QT_NO_DEBUG) || defined(QT_FORCE_ASSERTS) +} + +void UserResourceController::renderViewCreated(content::RenderView *renderView) +{ + // Will destroy itself with their RenderView. + new RenderViewObserverHelper(renderView); +} + +void UserResourceController::renderViewDestroyed(content::RenderView *renderView) +{ + ViewUserScriptMap::iterator it = m_viewUserScriptMap.find(renderView); + if (it == m_viewUserScriptMap.end()) // ASSERT maybe? + return; + Q_FOREACH (uint64 id, it.value()) { + m_scripts.remove(id); + } + m_viewUserScriptMap.remove(renderView); +} + +void UserResourceController::addScriptForView(const UserScriptData &script, content::RenderView *view) +{ + ViewUserScriptMap::iterator it = m_viewUserScriptMap.find(view); + if (it == m_viewUserScriptMap.end()) + it = m_viewUserScriptMap.insert(view, UserScriptSet()); + + (*it).insert(script.scriptId); + m_scripts.insert(script.scriptId, script); +} + +void UserResourceController::removeScriptForView(const UserScriptData &script, content::RenderView *view) +{ + ViewUserScriptMap::iterator it = m_viewUserScriptMap.find(view); + if (it == m_viewUserScriptMap.end()) + return; + + (*it).remove(script.scriptId); + m_scripts.remove(script.scriptId); +} + +void UserResourceController::clearScriptsForView(content::RenderView *view) +{ + ViewUserScriptMap::iterator it = m_viewUserScriptMap.find(view); + if (it == m_viewUserScriptMap.end()) + return; + Q_FOREACH (uint64 id, it.value()) + m_scripts.remove(id); + + m_viewUserScriptMap.remove(view); +} + +void UserResourceController::onAddScript(const UserScriptData &script) +{ + addScriptForView(script, globalScriptsIndex); +} + +void UserResourceController::onRemoveScript(const UserScriptData &script) +{ + removeScriptForView(script, globalScriptsIndex); +} + +void UserResourceController::onClearScripts() +{ + clearScriptsForView(globalScriptsIndex); +} + diff --git a/src/core/renderer/user_resource_controller.h b/src/core/renderer/user_resource_controller.h new file mode 100644 index 000000000..b2f7d92c3 --- /dev/null +++ b/src/core/renderer/user_resource_controller.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef USER_RESOURCE_CONTROLLER_H +#define USER_RESOURCE_CONTROLLER_H + +#include "content/public/renderer/render_process_observer.h" + +#include "common/user_script_data.h" + +#include +#include +#include + +namespace content { +class RenderView; +} + + +class UserResourceController : public content::RenderProcessObserver { + +public: + static UserResourceController *instance(); + UserResourceController(); + void renderViewCreated(content::RenderView *); + void renderViewDestroyed(content::RenderView *); + void addScriptForView(const UserScriptData &, content::RenderView *); + void removeScriptForView(const UserScriptData &, content::RenderView *); + void clearScriptsForView(content::RenderView *); + +private: + Q_DISABLE_COPY(UserResourceController) + + class RenderViewObserverHelper; + + // RenderProcessObserver implementation. + virtual bool OnControlMessageReceived(const IPC::Message &message) Q_DECL_OVERRIDE; + + void onAddScript(const UserScriptData &); + void onRemoveScript(const UserScriptData &); + void onClearScripts(); + + typedef QSet UserScriptSet; + typedef QHash ViewUserScriptMap; + ViewUserScriptMap m_viewUserScriptMap; + QHash m_scripts; +}; + +#endif // USER_RESOURCE_CONTROLLER_H diff --git a/src/core/renderer/user_script_controller.cpp b/src/core/renderer/user_script_controller.cpp deleted file mode 100644 index 2a7778db3..000000000 --- a/src/core/renderer/user_script_controller.cpp +++ /dev/null @@ -1,262 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWebEngine module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "user_script_controller.h" - -#include "content/public/renderer/render_view.h" -#include "content/public/renderer/render_view_observer.h" -#include "third_party/WebKit/public/web/WebLocalFrame.h" -#include "third_party/WebKit/public/web/WebScriptSource.h" -#include "third_party/WebKit/public/web/WebView.h" -#include "v8/include/v8.h" - -#include "common/qt_messages.h" -#include "common/user_script_data.h" - -Q_GLOBAL_STATIC(UserScriptController, qt_webengine_userScriptController) - -static content::RenderView * const globalScriptsIndex = 0; - -// Scripts meant to run after the load event will be run 500ms after DOMContentLoaded if the load event doesn't come within that delay. -static const int afterLoadTimeout = 500; - -class UserScriptController::RenderViewObserverHelper : public content::RenderViewObserver -{ -public: - RenderViewObserverHelper(content::RenderView *); -private: - // RenderViewObserver implementation. - virtual void DidCreateDocumentElement(blink::WebLocalFrame* frame) Q_DECL_OVERRIDE; - virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) Q_DECL_OVERRIDE; - virtual void DidFinishLoad(blink::WebLocalFrame* frame) Q_DECL_OVERRIDE; - virtual void DidStartProvisionalLoad(blink::WebLocalFrame* frame) Q_DECL_OVERRIDE; - virtual void FrameDetached(blink::WebFrame* frame) Q_DECL_OVERRIDE; - virtual void OnDestruct() Q_DECL_OVERRIDE; - virtual bool OnMessageReceived(const IPC::Message& message) Q_DECL_OVERRIDE; - - void onUserScriptAdded(const UserScriptData &); - void onUserScriptRemoved(const UserScriptData &); - void onScriptsCleared(); - - void runScripts(UserScriptData::InjectionPoint, blink::WebLocalFrame *); - QSet m_pendingFrames; -}; - -void UserScriptController::RenderViewObserverHelper::runScripts(UserScriptData::InjectionPoint p, blink::WebLocalFrame *frame) -{ - if (p == UserScriptData::AfterLoad && !m_pendingFrames.remove(frame)) - return; - content::RenderView *renderView = content::RenderView::FromWebView(frame->view()); - const bool isMainFrame = (frame == renderView->GetWebView()->mainFrame()); - - QList scriptsToRun = UserScriptController::instance()->m_viewUserScriptMap.value(globalScriptsIndex).toList(); - scriptsToRun.append(UserScriptController::instance()->m_viewUserScriptMap.value(renderView).toList()); - - Q_FOREACH (uint64 id, scriptsToRun) { - const UserScriptData &script = UserScriptController::instance()->m_scripts.value(id); - if (script.injectionPoint != p - || (!script.injectForSubframes && !isMainFrame)) - continue; - blink::WebScriptSource source(blink::WebString::fromUTF8(script.source), script.url); - if (script.worldId) - frame->executeScriptInIsolatedWorld(script.worldId, &source, /*numSources = */1, /*contentScriptExtentsionGroup = */ 0); - else - frame->executeScript(source); - } -} - - -UserScriptController::RenderViewObserverHelper::RenderViewObserverHelper(content::RenderView *renderView) - : content::RenderViewObserver(renderView) -{ -} - -void UserScriptController::RenderViewObserverHelper::DidCreateDocumentElement(blink::WebLocalFrame *frame) -{ - runScripts(UserScriptData::DocumentElementCreation, frame); -} - -void UserScriptController::RenderViewObserverHelper::DidFinishDocumentLoad(blink::WebLocalFrame *frame) -{ - runScripts(UserScriptData::DocumentLoadFinished, frame); - m_pendingFrames.insert(frame); - base::MessageLoop::current()->PostDelayedTask(FROM_HERE, base::Bind(&UserScriptController::RenderViewObserverHelper::runScripts, - base::Unretained(this), UserScriptData::AfterLoad, frame), - base::TimeDelta::FromMilliseconds(afterLoadTimeout)); -} - -void UserScriptController::RenderViewObserverHelper::DidFinishLoad(blink::WebLocalFrame *frame) -{ - // DidFinishDocumentLoad always comes before this, so frame has already been marked as pending. - base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&UserScriptController::RenderViewObserverHelper::runScripts, - base::Unretained(this), UserScriptData::AfterLoad, frame)); -} - -void UserScriptController::RenderViewObserverHelper::DidStartProvisionalLoad(blink::WebLocalFrame *frame) -{ - m_pendingFrames.remove(frame); -} - -void UserScriptController::RenderViewObserverHelper::FrameDetached(blink::WebFrame *frame) -{ - if (frame->isWebLocalFrame()) - m_pendingFrames.remove(frame->toWebLocalFrame()); -} - -void UserScriptController::RenderViewObserverHelper::OnDestruct() -{ - UserScriptController::instance()->renderViewDestroyed(render_view()); -} - -bool UserScriptController::RenderViewObserverHelper::OnMessageReceived(const IPC::Message &message) -{ - bool handled = true; - IPC_BEGIN_MESSAGE_MAP(UserScriptController::RenderViewObserverHelper, message) - IPC_MESSAGE_HANDLER(RenderViewObserverHelper_AddScript, onUserScriptAdded) - IPC_MESSAGE_HANDLER(RenderViewObserverHelper_RemoveScript, onUserScriptRemoved) - IPC_MESSAGE_HANDLER(RenderViewObserverHelper_ClearScripts, onScriptsCleared) - IPC_MESSAGE_UNHANDLED(handled = false) - IPC_END_MESSAGE_MAP() - return handled; -} - -void UserScriptController::RenderViewObserverHelper::onUserScriptAdded(const UserScriptData &script) -{ - UserScriptController::instance()->addScriptForView(script, render_view()); -} - -void UserScriptController::RenderViewObserverHelper::onUserScriptRemoved(const UserScriptData &script) -{ - UserScriptController::instance()->removeScriptForView(script, render_view()); -} - -void UserScriptController::RenderViewObserverHelper::onScriptsCleared() -{ - UserScriptController::instance()->clearScriptsForView(render_view()); -} - -UserScriptController *UserScriptController::instance() -{ - return qt_webengine_userScriptController(); -} - -bool UserScriptController::OnControlMessageReceived(const IPC::Message &message) -{ - bool handled = true; - IPC_BEGIN_MESSAGE_MAP(UserScriptController, message) - IPC_MESSAGE_HANDLER(UserScriptController_AddScript, onAddScript) - IPC_MESSAGE_HANDLER(UserScriptController_RemoveScript, onRemoveScript) - IPC_MESSAGE_HANDLER(UserScriptController_ClearScripts, onClearScripts) - IPC_MESSAGE_UNHANDLED(handled = false) - IPC_END_MESSAGE_MAP() - return handled; -} - -UserScriptController::UserScriptController() -{ -#if !defined(QT_NO_DEBUG) || defined(QT_FORCE_ASSERTS) - static bool onlyCalledOnce = true; - Q_ASSERT(onlyCalledOnce); - onlyCalledOnce = false; -#endif // !defined(QT_NO_DEBUG) || defined(QT_FORCE_ASSERTS) -} - -void UserScriptController::renderViewCreated(content::RenderView *renderView) -{ - // Will destroy itself with their RenderView. - new RenderViewObserverHelper(renderView); -} - -void UserScriptController::renderViewDestroyed(content::RenderView *renderView) -{ - ViewUserScriptMap::iterator it = m_viewUserScriptMap.find(renderView); - if (it == m_viewUserScriptMap.end()) // ASSERT maybe? - return; - Q_FOREACH (uint64 id, it.value()) { - m_scripts.remove(id); - } - m_viewUserScriptMap.remove(renderView); -} - -void UserScriptController::addScriptForView(const UserScriptData &script, content::RenderView *view) -{ - ViewUserScriptMap::iterator it = m_viewUserScriptMap.find(view); - if (it == m_viewUserScriptMap.end()) - it = m_viewUserScriptMap.insert(view, UserScriptSet()); - - (*it).insert(script.scriptId); - m_scripts.insert(script.scriptId, script); -} - -void UserScriptController::removeScriptForView(const UserScriptData &script, content::RenderView *view) -{ - ViewUserScriptMap::iterator it = m_viewUserScriptMap.find(view); - if (it == m_viewUserScriptMap.end()) - return; - - (*it).remove(script.scriptId); - m_scripts.remove(script.scriptId); -} - -void UserScriptController::clearScriptsForView(content::RenderView *view) -{ - ViewUserScriptMap::iterator it = m_viewUserScriptMap.find(view); - if (it == m_viewUserScriptMap.end()) - return; - Q_FOREACH (uint64 id, it.value()) - m_scripts.remove(id); - - m_viewUserScriptMap.remove(view); -} - -void UserScriptController::onAddScript(const UserScriptData &script) -{ - addScriptForView(script, globalScriptsIndex); -} - -void UserScriptController::onRemoveScript(const UserScriptData &script) -{ - removeScriptForView(script, globalScriptsIndex); -} - -void UserScriptController::onClearScripts() -{ - clearScriptsForView(globalScriptsIndex); -} - diff --git a/src/core/renderer/user_script_controller.h b/src/core/renderer/user_script_controller.h deleted file mode 100644 index 15a057074..000000000 --- a/src/core/renderer/user_script_controller.h +++ /dev/null @@ -1,85 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWebEngine module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef USER_SCRIPT_CONTROLLER_H -#define USER_SCRIPT_CONTROLLER_H - -#include "content/public/renderer/render_process_observer.h" - -#include "common/user_script_data.h" - -#include -#include -#include - -namespace content { -class RenderView; -} - - -class UserScriptController : public content::RenderProcessObserver { - -public: - static UserScriptController *instance(); - UserScriptController(); - void renderViewCreated(content::RenderView *); - void renderViewDestroyed(content::RenderView *); - void addScriptForView(const UserScriptData &, content::RenderView *); - void removeScriptForView(const UserScriptData &, content::RenderView *); - void clearScriptsForView(content::RenderView *); - -private: - Q_DISABLE_COPY(UserScriptController) - - class RenderViewObserverHelper; - - // RenderProcessObserver implementation. - virtual bool OnControlMessageReceived(const IPC::Message &message) Q_DECL_OVERRIDE; - - void onAddScript(const UserScriptData &); - void onRemoveScript(const UserScriptData &); - void onClearScripts(); - - typedef QSet UserScriptSet; - typedef QHash ViewUserScriptMap; - ViewUserScriptMap m_viewUserScriptMap; - QHash m_scripts; -}; - -#endif // USER_SCRIPT_CONTROLLER_H diff --git a/src/core/user_resource_controller_host.cpp b/src/core/user_resource_controller_host.cpp new file mode 100644 index 000000000..b2de41cc0 --- /dev/null +++ b/src/core/user_resource_controller_host.cpp @@ -0,0 +1,238 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "user_resource_controller_host.h" + +#include "common/qt_messages.h" +#include "type_conversion.h" +#include "web_contents_adapter.h" +#include "web_contents_adapter_p.h" + +#include "content/public/browser/render_process_host.h" +#include "content/public/browser/render_process_host_observer.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/web_contents.h" +#include "content/public/browser/web_contents_observer.h" + +namespace QtWebEngineCore { + +class UserResourceControllerHost::WebContentsObserverHelper : public content::WebContentsObserver { +public: + WebContentsObserverHelper(UserResourceControllerHost *, content::WebContents *); + + // WebContentsObserver overrides: + void RenderViewCreated(content::RenderViewHost *renderViewHost) override; + void RenderViewHostChanged(content::RenderViewHost *oldHost, content::RenderViewHost *newHost) override; + void WebContentsDestroyed() override; + +private: + UserResourceControllerHost *m_controllerHost; +}; + +UserResourceControllerHost::WebContentsObserverHelper::WebContentsObserverHelper(UserResourceControllerHost *controller, content::WebContents *contents) + : content::WebContentsObserver(contents) + , m_controllerHost(controller) +{ +} + +void UserResourceControllerHost::WebContentsObserverHelper::RenderViewCreated(content::RenderViewHost *renderViewHost) +{ + content::WebContents *contents = web_contents(); + Q_FOREACH (const UserScript &script, m_controllerHost->m_perContentsScripts.value(contents)) + renderViewHost->Send(new RenderViewObserverHelper_AddScript(renderViewHost->GetRoutingID(), script.data())); +} + +void UserResourceControllerHost::WebContentsObserverHelper::RenderViewHostChanged(content::RenderViewHost *oldHost, + content::RenderViewHost *newHost) +{ + oldHost->Send(new RenderViewObserverHelper_ClearScripts(oldHost->GetRoutingID())); + + content::WebContents *contents = web_contents(); + Q_FOREACH (const UserScript &script, m_controllerHost->m_perContentsScripts.value(contents)) + newHost->Send(new RenderViewObserverHelper_AddScript(newHost->GetRoutingID(), script.data())); +} + +void UserResourceControllerHost::WebContentsObserverHelper::WebContentsDestroyed() +{ + m_controllerHost->webContentsDestroyed(web_contents()); + delete this; +} + +class UserResourceControllerHost::RenderProcessObserverHelper : public content::RenderProcessHostObserver { +public: + RenderProcessObserverHelper(UserResourceControllerHost *); + virtual void RenderProcessHostDestroyed(content::RenderProcessHost *) Q_DECL_OVERRIDE; +private: + UserResourceControllerHost *m_controllerHost; +}; + +UserResourceControllerHost::RenderProcessObserverHelper::RenderProcessObserverHelper(UserResourceControllerHost *controller) + : m_controllerHost(controller) +{ +} + +void UserResourceControllerHost::RenderProcessObserverHelper::RenderProcessHostDestroyed(content::RenderProcessHost *renderer) +{ + Q_ASSERT(m_controllerHost); + m_controllerHost->m_observedProcesses.remove(renderer); +} + +void UserResourceControllerHost::addUserScript(const UserScript &script, WebContentsAdapter *adapter) +{ + if (script.isNull()) + return; + // Global scripts should be dispatched to all our render processes. + if (!adapter) { + if (!m_profileWideScripts.contains(script)) { + m_profileWideScripts.append(script); + Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses) + renderer->Send(new UserResourceController_AddScript(script.data())); + } + } else { + content::WebContents *contents = adapter->webContents(); + ContentsScriptsMap::iterator it = m_perContentsScripts.find(contents); + if (it == m_perContentsScripts.end()) { + // We need to keep track of RenderView/RenderViewHost changes for a given contents + // in order to make sure the scripts stay in sync + new WebContentsObserverHelper(this, contents); + it = m_perContentsScripts.insert(contents, (QList() << script)); + } else { + QList currentScripts = it.value(); + if (!currentScripts.contains(script)) { + currentScripts.append(script); + m_perContentsScripts.insert(contents, currentScripts); + } + } + contents->Send(new RenderViewObserverHelper_AddScript(contents->GetRoutingID(), script.data())); + } +} + +bool UserResourceControllerHost::containsUserScript(const UserScript &script, WebContentsAdapter *adapter) +{ + if (script.isNull()) + return false; + // Global scripts should be dispatched to all our render processes. + if (!adapter) + return m_profileWideScripts.contains(script); + return m_perContentsScripts.value(adapter->webContents()).contains(script); +} + +bool UserResourceControllerHost::removeUserScript(const UserScript &script, WebContentsAdapter *adapter) +{ + if (script.isNull()) + return false; + if (!adapter) { + QList::iterator it + = std::find(m_profileWideScripts.begin(), m_profileWideScripts.end(), script); + if (it == m_profileWideScripts.end()) + return false; + Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses) + renderer->Send(new UserResourceController_RemoveScript((*it).data())); + m_profileWideScripts.erase(it); + } else { + content::WebContents *contents = adapter->webContents(); + if (!m_perContentsScripts.contains(contents)) + return false; + QList &list(m_perContentsScripts[contents]); + QList::iterator it = std::find(list.begin(), list.end(), script); + if (it == list.end()) + return false; + contents->Send(new RenderViewObserverHelper_RemoveScript(contents->GetRoutingID(), (*it).data())); + list.erase(it); + } + return true; +} + +void UserResourceControllerHost::clearAllScripts(WebContentsAdapter *adapter) +{ + if (!adapter) { + m_profileWideScripts.clear(); + Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses) + renderer->Send(new UserResourceController_ClearScripts); + } else { + content::WebContents *contents = adapter->webContents(); + m_perContentsScripts.remove(contents); + contents->Send(new RenderViewObserverHelper_ClearScripts(contents->GetRoutingID())); + } +} + +const QList UserResourceControllerHost::registeredScripts(WebContentsAdapter *adapter) const +{ + if (!adapter) + return m_profileWideScripts; + return m_perContentsScripts.value(adapter->webContents()); +} + +void UserResourceControllerHost::reserve(WebContentsAdapter *adapter, int count) +{ + if (!adapter) + m_profileWideScripts.reserve(count); + else + m_perContentsScripts[adapter->webContents()].reserve(count); +} + +void UserResourceControllerHost::renderProcessStartedWithHost(content::RenderProcessHost *renderer) +{ + if (m_observedProcesses.contains(renderer)) + return; + + if (m_renderProcessObserver.isNull()) + m_renderProcessObserver.reset(new RenderProcessObserverHelper(this)); + renderer->AddObserver(m_renderProcessObserver.data()); + m_observedProcesses.insert(renderer); + Q_FOREACH (const UserScript &script, m_profileWideScripts) + renderer->Send(new UserResourceController_AddScript(script.data())); +} + +void UserResourceControllerHost::webContentsDestroyed(content::WebContents *contents) +{ + m_perContentsScripts.remove(contents); +} + +UserResourceControllerHost::UserResourceControllerHost() +{ +} + +UserResourceControllerHost::~UserResourceControllerHost() +{ + Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses) + renderer->RemoveObserver(m_renderProcessObserver.data()); +} + +} // namespace diff --git a/src/core/user_resource_controller_host.h b/src/core/user_resource_controller_host.h new file mode 100644 index 000000000..8e6e1e3bf --- /dev/null +++ b/src/core/user_resource_controller_host.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef USER_RESOURCE_CONTROLLER_HOST_H +#define USER_RESOURCE_CONTROLLER_HOST_H + +#include "qtwebenginecoreglobal.h" + +#include +#include +#include "user_script.h" + +namespace content { +class RenderProcessHost; +class WebContents; +} + +namespace QtWebEngineCore { + +class WebContentsAdapter; +class WebContentsAdapterPrivate; + +class QWEBENGINE_EXPORT UserResourceControllerHost { + +public: + UserResourceControllerHost(); + ~UserResourceControllerHost(); + + void addUserScript(const UserScript &script, WebContentsAdapter *adapter); + bool containsUserScript(const UserScript &script, WebContentsAdapter *adapter); + bool removeUserScript(const UserScript &script, WebContentsAdapter *adapter); + void clearAllScripts(WebContentsAdapter *adapter); + void reserve(WebContentsAdapter *adapter, int count); + const QList registeredScripts(WebContentsAdapter *adapter) const; + + void renderProcessStartedWithHost(content::RenderProcessHost *renderer); + +private: + Q_DISABLE_COPY(UserResourceControllerHost) + class WebContentsObserverHelper; + class RenderProcessObserverHelper; + + void webContentsDestroyed(content::WebContents *); + + QList m_profileWideScripts; + typedef QHash> ContentsScriptsMap; + ContentsScriptsMap m_perContentsScripts; + QSet m_observedProcesses; + QScopedPointer m_renderProcessObserver; +}; + +} // namespace + +#endif // USER_RESOURCE_CONTROLLER_HOST_H diff --git a/src/core/user_script.cpp b/src/core/user_script.cpp index 20764eeb0..839eff366 100644 --- a/src/core/user_script.cpp +++ b/src/core/user_script.cpp @@ -39,7 +39,6 @@ #include "common/user_script_data.h" #include "user_script.h" -#include "user_script_controller_host.h" #include "type_conversion.h" namespace QtWebEngineCore { diff --git a/src/core/user_script.h b/src/core/user_script.h index 6835f4d20..9d7d66a58 100644 --- a/src/core/user_script.h +++ b/src/core/user_script.h @@ -51,7 +51,7 @@ struct UserScriptData; namespace QtWebEngineCore { -class UserScriptControllerHost; +class UserResourceControllerHost; class QWEBENGINE_EXPORT UserScript : public QSharedData { public: @@ -88,7 +88,7 @@ public: private: void initData(); UserScriptData &data() const; - friend class UserScriptControllerHost; + friend class UserResourceControllerHost; QScopedPointer scriptData; QString m_name; diff --git a/src/core/user_script_controller_host.cpp b/src/core/user_script_controller_host.cpp deleted file mode 100644 index 799996987..000000000 --- a/src/core/user_script_controller_host.cpp +++ /dev/null @@ -1,238 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWebEngine module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "user_script_controller_host.h" - -#include "common/qt_messages.h" -#include "type_conversion.h" -#include "web_contents_adapter.h" -#include "web_contents_adapter_p.h" - -#include "content/public/browser/render_process_host.h" -#include "content/public/browser/render_process_host_observer.h" -#include "content/public/browser/render_view_host.h" -#include "content/public/browser/web_contents.h" -#include "content/public/browser/web_contents_observer.h" - -namespace QtWebEngineCore { - -class UserScriptControllerHost::WebContentsObserverHelper : public content::WebContentsObserver { -public: - WebContentsObserverHelper(UserScriptControllerHost *, content::WebContents *); - - // WebContentsObserver overrides: - void RenderViewCreated(content::RenderViewHost *renderViewHost) override; - void RenderViewHostChanged(content::RenderViewHost *oldHost, content::RenderViewHost *newHost) override; - void WebContentsDestroyed() override; - -private: - UserScriptControllerHost *m_controllerHost; -}; - -UserScriptControllerHost::WebContentsObserverHelper::WebContentsObserverHelper(UserScriptControllerHost *controller, content::WebContents *contents) - : content::WebContentsObserver(contents) - , m_controllerHost(controller) -{ -} - -void UserScriptControllerHost::WebContentsObserverHelper::RenderViewCreated(content::RenderViewHost *renderViewHost) -{ - content::WebContents *contents = web_contents(); - Q_FOREACH (const UserScript &script, m_controllerHost->m_perContentsScripts.value(contents)) - renderViewHost->Send(new RenderViewObserverHelper_AddScript(renderViewHost->GetRoutingID(), script.data())); -} - -void UserScriptControllerHost::WebContentsObserverHelper::RenderViewHostChanged(content::RenderViewHost *oldHost, - content::RenderViewHost *newHost) -{ - oldHost->Send(new RenderViewObserverHelper_ClearScripts(oldHost->GetRoutingID())); - - content::WebContents *contents = web_contents(); - Q_FOREACH (const UserScript &script, m_controllerHost->m_perContentsScripts.value(contents)) - newHost->Send(new RenderViewObserverHelper_AddScript(newHost->GetRoutingID(), script.data())); -} - -void UserScriptControllerHost::WebContentsObserverHelper::WebContentsDestroyed() -{ - m_controllerHost->webContentsDestroyed(web_contents()); - delete this; -} - -class UserScriptControllerHost::RenderProcessObserverHelper : public content::RenderProcessHostObserver { -public: - RenderProcessObserverHelper(UserScriptControllerHost *); - virtual void RenderProcessHostDestroyed(content::RenderProcessHost *) Q_DECL_OVERRIDE; -private: - UserScriptControllerHost *m_controllerHost; -}; - -UserScriptControllerHost::RenderProcessObserverHelper::RenderProcessObserverHelper(UserScriptControllerHost *controller) - : m_controllerHost(controller) -{ -} - -void UserScriptControllerHost::RenderProcessObserverHelper::RenderProcessHostDestroyed(content::RenderProcessHost *renderer) -{ - Q_ASSERT(m_controllerHost); - m_controllerHost->m_observedProcesses.remove(renderer); -} - -void UserScriptControllerHost::addUserScript(const UserScript &script, WebContentsAdapter *adapter) -{ - if (script.isNull()) - return; - // Global scripts should be dispatched to all our render processes. - if (!adapter) { - if (!m_profileWideScripts.contains(script)) { - m_profileWideScripts.append(script); - Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses) - renderer->Send(new UserScriptController_AddScript(script.data())); - } - } else { - content::WebContents *contents = adapter->webContents(); - ContentsScriptsMap::iterator it = m_perContentsScripts.find(contents); - if (it == m_perContentsScripts.end()) { - // We need to keep track of RenderView/RenderViewHost changes for a given contents - // in order to make sure the scripts stay in sync - new WebContentsObserverHelper(this, contents); - it = m_perContentsScripts.insert(contents, (QList() << script)); - } else { - QList currentScripts = it.value(); - if (!currentScripts.contains(script)) { - currentScripts.append(script); - m_perContentsScripts.insert(contents, currentScripts); - } - } - contents->Send(new RenderViewObserverHelper_AddScript(contents->GetRoutingID(), script.data())); - } -} - -bool UserScriptControllerHost::containsUserScript(const UserScript &script, WebContentsAdapter *adapter) -{ - if (script.isNull()) - return false; - // Global scripts should be dispatched to all our render processes. - if (!adapter) - return m_profileWideScripts.contains(script); - return m_perContentsScripts.value(adapter->webContents()).contains(script); -} - -bool UserScriptControllerHost::removeUserScript(const UserScript &script, WebContentsAdapter *adapter) -{ - if (script.isNull()) - return false; - if (!adapter) { - QList::iterator it - = std::find(m_profileWideScripts.begin(), m_profileWideScripts.end(), script); - if (it == m_profileWideScripts.end()) - return false; - Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses) - renderer->Send(new UserScriptController_RemoveScript((*it).data())); - m_profileWideScripts.erase(it); - } else { - content::WebContents *contents = adapter->webContents(); - if (!m_perContentsScripts.contains(contents)) - return false; - QList &list(m_perContentsScripts[contents]); - QList::iterator it = std::find(list.begin(), list.end(), script); - if (it == list.end()) - return false; - contents->Send(new RenderViewObserverHelper_RemoveScript(contents->GetRoutingID(), (*it).data())); - list.erase(it); - } - return true; -} - -void UserScriptControllerHost::clearAllScripts(WebContentsAdapter *adapter) -{ - if (!adapter) { - m_profileWideScripts.clear(); - Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses) - renderer->Send(new UserScriptController_ClearScripts); - } else { - content::WebContents *contents = adapter->webContents(); - m_perContentsScripts.remove(contents); - contents->Send(new RenderViewObserverHelper_ClearScripts(contents->GetRoutingID())); - } -} - -const QList UserScriptControllerHost::registeredScripts(WebContentsAdapter *adapter) const -{ - if (!adapter) - return m_profileWideScripts; - return m_perContentsScripts.value(adapter->webContents()); -} - -void UserScriptControllerHost::reserve(WebContentsAdapter *adapter, int count) -{ - if (!adapter) - m_profileWideScripts.reserve(count); - else - m_perContentsScripts[adapter->webContents()].reserve(count); -} - -void UserScriptControllerHost::renderProcessStartedWithHost(content::RenderProcessHost *renderer) -{ - if (m_observedProcesses.contains(renderer)) - return; - - if (m_renderProcessObserver.isNull()) - m_renderProcessObserver.reset(new RenderProcessObserverHelper(this)); - renderer->AddObserver(m_renderProcessObserver.data()); - m_observedProcesses.insert(renderer); - Q_FOREACH (const UserScript &script, m_profileWideScripts) - renderer->Send(new UserScriptController_AddScript(script.data())); -} - -void UserScriptControllerHost::webContentsDestroyed(content::WebContents *contents) -{ - m_perContentsScripts.remove(contents); -} - -UserScriptControllerHost::UserScriptControllerHost() -{ -} - -UserScriptControllerHost::~UserScriptControllerHost() -{ - Q_FOREACH (content::RenderProcessHost *renderer, m_observedProcesses) - renderer->RemoveObserver(m_renderProcessObserver.data()); -} - -} // namespace diff --git a/src/core/user_script_controller_host.h b/src/core/user_script_controller_host.h deleted file mode 100644 index ea2160678..000000000 --- a/src/core/user_script_controller_host.h +++ /dev/null @@ -1,90 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWebEngine module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef USER_SCRIPT_CONTROLLER_HOST_H -#define USER_SCRIPT_CONTROLLER_HOST_H - -#include "qtwebenginecoreglobal.h" - -#include -#include -#include "user_script.h" - -namespace content { -class RenderProcessHost; -class WebContents; -} - -namespace QtWebEngineCore { - -class WebContentsAdapter; -class WebContentsAdapterPrivate; - -class QWEBENGINE_EXPORT UserScriptControllerHost { - -public: - UserScriptControllerHost(); - ~UserScriptControllerHost(); - - void addUserScript(const UserScript &script, WebContentsAdapter *adapter); - bool containsUserScript(const UserScript &script, WebContentsAdapter *adapter); - bool removeUserScript(const UserScript &script, WebContentsAdapter *adapter); - void clearAllScripts(WebContentsAdapter *adapter); - void reserve(WebContentsAdapter *adapter, int count); - const QList registeredScripts(WebContentsAdapter *adapter) const; - - void renderProcessStartedWithHost(content::RenderProcessHost *renderer); - -private: - Q_DISABLE_COPY(UserScriptControllerHost) - class WebContentsObserverHelper; - class RenderProcessObserverHelper; - - void webContentsDestroyed(content::WebContents *); - - QList m_profileWideScripts; - typedef QHash> ContentsScriptsMap; - ContentsScriptsMap m_perContentsScripts; - QSet m_observedProcesses; - QScopedPointer m_renderProcessObserver; -}; - -} // namespace - -#endif // USER_SCRIPT_CONTROLLER_HOST_H diff --git a/src/core/web_contents_adapter_p.h b/src/core/web_contents_adapter_p.h index 01cb8507a..0507ebf4a 100644 --- a/src/core/web_contents_adapter_p.h +++ b/src/core/web_contents_adapter_p.h @@ -73,7 +73,6 @@ namespace QtWebEngineCore { class BrowserContextAdapter; class RenderViewObserverHostQt; -class UserScriptControllerHost; class WebChannelIPCTransportHost; class WebContentsAdapterClient; class WebContentsDelegateQt; -- cgit v1.2.3