diff options
author | Pierre Rossi <pierre.rossi@digia.com> | 2014-11-18 16:08:20 +0100 |
---|---|---|
committer | Andras Becsi <andras.becsi@theqtcompany.com> | 2015-02-21 21:38:57 +0000 |
commit | 4ed187a54e6d2a06bbe272e1429757b6572dc6b6 (patch) | |
tree | 2b1d0485a780904f544623dff96be9dd270988bc /src/core/user_script.cpp | |
parent | 88ca68c1b3ceff005f6a3a33fc8fc0d551c53a70 (diff) |
Introduce a user scripts mechanism
Allowing programmatic injection of JavaScript to accomplish
all sorts of tasks on the render process side.
This API gives control over the point during the loading phase
at which the script is run, whether it is run on sub-frames or
not, as well as the JavaScript world it is run in (either the
page's main world, or an arbitrary isolated world).
This only has the Widgets API. The Quick API, tests and docs
are coming in separate patches
Change-Id: Ia1c79f68f8dfd4d964281d9723d09062ed7abe46
Reviewed-by: Andras Becsi <andras.becsi@theqtcompany.com>
Diffstat (limited to 'src/core/user_script.cpp')
-rw-r--r-- | src/core/user_script.cpp | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/src/core/user_script.cpp b/src/core/user_script.cpp new file mode 100644 index 000000000..7e88f9223 --- /dev/null +++ b/src/core/user_script.cpp @@ -0,0 +1,185 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "common/user_script_data.h" +#include "user_script.h" +#include "user_script_controller_host.h" +#include "type_conversion.h" + +ASSERT_ENUMS_MATCH(UserScript::AfterLoad, UserScriptData::AfterLoad) +ASSERT_ENUMS_MATCH(UserScript::DocumentLoadFinished, UserScriptData::DocumentLoadFinished) +ASSERT_ENUMS_MATCH(UserScript::DocumentElementCreation, UserScriptData::DocumentElementCreation) + +UserScript::UserScript() + : QSharedData() +{ +} + +UserScript::UserScript(const UserScript &other) + : QSharedData(other) +{ + if (other.isNull()) + return; + scriptData.reset(new UserScriptData); + m_name = other.m_name; + scriptData->source = other.scriptData->source; + scriptData->url = other.scriptData->url; + scriptData->injectionPoint = other.scriptData->injectionPoint; + scriptData->injectForSubframes = other.scriptData->injectForSubframes; + scriptData->worldId = other.scriptData->worldId; +} + +UserScript::~UserScript() +{ +} + +UserScript &UserScript::operator=(const UserScript &other) +{ + if (other.isNull()) { + scriptData.reset(); + m_name = QString(); + return *this; + } + scriptData.reset(new UserScriptData); + m_name = other.m_name; + scriptData->source = other.scriptData->source; + scriptData->url = other.scriptData->url; + scriptData->injectionPoint = other.scriptData->injectionPoint; + scriptData->injectForSubframes = other.scriptData->injectForSubframes; + scriptData->worldId = other.scriptData->worldId; + return *this; +} + +QString UserScript::name() const +{ + return m_name; +} + +void UserScript::setName(const QString &name) +{ + m_name = name; + initData(); + scriptData->url = GURL(QStringLiteral("userScript:%1").arg(name).toStdString()); +} + +QString UserScript::source() const +{ + if (isNull()) + return QString(); + return toQt(scriptData->source); +} + +void UserScript::setSource(const QString &source) +{ + initData(); + scriptData->source = source.toStdString(); +} + +UserScript::InjectionPoint UserScript::injectionPoint() const +{ + if (isNull()) + return UserScript::AfterLoad; + return static_cast<UserScript::InjectionPoint>(scriptData->injectionPoint); +} + +void UserScript::setInjectionPoint(UserScript::InjectionPoint p) +{ + initData(); + scriptData->injectionPoint = p; +} + +uint UserScript::worldId() const +{ + if (isNull()) + return 1; + return scriptData->worldId; +} + +void UserScript::setWorldId(uint id) +{ + initData(); + scriptData->worldId = id; +} + +bool UserScript::runsOnSubFrames() const +{ + if (isNull()) + return false; + return scriptData->injectForSubframes; +} + +void UserScript::setRunsOnSubFrames(bool on) +{ + initData(); + scriptData->injectForSubframes = on; +} + +bool UserScript::operator==(const UserScript &other) const +{ + if (isNull() != other.isNull()) + return false; + if (isNull()) // neither is valid + return true; + return worldId() == other.worldId() + && runsOnSubFrames() == other.runsOnSubFrames() + && injectionPoint() == other.injectionPoint() + && name() == other.name() && source() == other.source(); +} + +void UserScript::initData() +{ + if (scriptData.isNull()) + scriptData.reset(new UserScriptData); +} + +bool UserScript::isNull() const +{ + return scriptData.isNull(); +} + +UserScriptData &UserScript::data() const +{ + return *(scriptData.data()); +} + +uint qHash(const UserScript &script, uint seed) +{ + if (script.isNull()) + return 0; + return qHash(script.source(), seed) ^ qHash(script.name(), seed) + ^ (script.injectionPoint() | (script.runsOnSubFrames() << 4)) + ^ script.worldId(); +} |