summaryrefslogtreecommitdiffstats
path: root/src/webengine/api/qquickwebenginescript.cpp
blob: 74bd9899a9d27b632b5dc59105db331d370a0994 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/****************************************************************************
**
** 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 "qquickwebenginescript_p.h"
#include "qquickwebenginescript_p_p.h"

#include <QQmlFile>
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QTimerEvent>
#include "user_resource_controller_host.h"

using QtWebEngineCore::UserScript;

/*!
    \qmltype WebEngineScript
    \instantiates QQuickWebEngineScript
    \inqmlmodule QtWebEngine
    \since QtWebEngine 1.1
    \brief Enables the programmatic injection of scripts in the JavaScript engine.

    The WebEngineScript type enables the programmatic injection of so called \e {user scripts} in
    the JavaScript engine at different points, determined by injectionPoint, during the loading of
    web content.

    Scripts can be executed either in the main JavaScript \e world, along with the rest of the
    JavaScript coming from the web contents, or in their own isolated world. While the DOM of the
    page can be accessed from any world, JavaScript variables of a function defined in one world are
    not accessible from a different one. The worldId property provides some predefined IDs for this
    purpose.

    Use \l{WebEngineView::userScripts}{WebEngineView.userScripts} to access a list of scripts
    attached to the web view.
*/

QQuickWebEngineScript::QQuickWebEngineScript()
    : d_ptr(new QQuickWebEngineScriptPrivate)
{
    d_ptr->q_ptr = this;
}

QQuickWebEngineScript::~QQuickWebEngineScript()
{
}

QString QQuickWebEngineScript::toString() const
{
    Q_D(const QQuickWebEngineScript);
    if (d->coreScript.isNull())
        return QStringLiteral("QWebEngineScript()");
    QString ret = QStringLiteral("QWebEngineScript(") % d->coreScript.name() % QStringLiteral(", ");
    switch (d->coreScript.injectionPoint()) {
    case UserScript::DocumentElementCreation:
        ret.append(QStringLiteral("WebEngineScript::DocumentCreation, "));
        break;
    case UserScript::DocumentLoadFinished:
        ret.append(QStringLiteral("WebEngineScript::DocumentReady, "));
        break;
    case UserScript::AfterLoad:
        ret.append(QStringLiteral("WebEngineScript::Deferred, "));
        break;
    }
    ret.append(QString::number(d->coreScript.worldId()) % QStringLiteral(", ")
               % (d->coreScript.runsOnSubFrames() ? QStringLiteral("true") : QStringLiteral("false"))
               % QStringLiteral(", ") % d->coreScript.sourceCode() % QLatin1Char(')'));
    return ret;
}

/*!
    \qmlproperty QString WebEngineScript::name

    The name of the script. Can be useful to retrieve a particular script from
    \l{WebEngineView::userScripts}{WebEngineView.userScripts}.
*/
QString QQuickWebEngineScript::name() const
{
    Q_D(const QQuickWebEngineScript);
    return d->coreScript.name();
}

/*!
    \qmlproperty url WebEngineScript::sourceUrl

    This property holds the remote source location of the user script (if any).

    Unlike \l sourceCode, this property allows referring to user scripts that
    are not already loaded in memory, for instance, when stored on disk.

    Setting this property will change the \l sourceCode of the script.

    \note At present, only file-based sources are supported.

    \sa sourceCode
*/
QUrl QQuickWebEngineScript::sourceUrl() const
{
    Q_D(const QQuickWebEngineScript);
    return d->m_sourceUrl;
}

/*!
    \qmlproperty string WebEngineScript::sourceCode

    This property holds the JavaScript source code of the user script.

    \sa sourceUrl
*/
QString QQuickWebEngineScript::sourceCode() const
{
    Q_D(const QQuickWebEngineScript);
    return d->coreScript.sourceCode();
}

ASSERT_ENUMS_MATCH(QQuickWebEngineScript::Deferred, UserScript::AfterLoad)
ASSERT_ENUMS_MATCH(QQuickWebEngineScript::DocumentReady, UserScript::DocumentLoadFinished)
ASSERT_ENUMS_MATCH(QQuickWebEngineScript::DocumentCreation, UserScript::DocumentElementCreation)

/*!
    \qmlproperty enumeration WebEngineScript::injectionPoint

    The point in the loading process at which the script will be executed.
    The default value is \c Deferred.

    \value DocumentCreation
           The script will be executed as soon as the document is created. This is not suitable for
           any DOM operation.
    \value DocumentReady
           The script will run as soon as the DOM is ready. This is equivalent to the
           \c DOMContentLoaded event firing in JavaScript.
    \value Deferred
           The script will run when the page load finishes, or 500 ms after the document is ready,
           whichever comes first.
*/
QQuickWebEngineScript::InjectionPoint QQuickWebEngineScript::injectionPoint() const
{
    Q_D(const QQuickWebEngineScript);
    return static_cast<QQuickWebEngineScript::InjectionPoint>(d->coreScript.injectionPoint());
}

/*!
    \qmlproperty enumeration WebEngineScript::worldId

    The world ID defining which isolated world the script is executed in.

    \value MainWorld
           The world used by the page's web contents. It can be useful in order to expose custom
           functionality to web contents in certain scenarios.
    \value ApplicationWorld
           The default isolated world used for application level functionality implemented in
           JavaScript.
    \value UserWorld
           The first isolated world to be used by scripts set by users if the application is not
           making use of more worlds. As a rule of thumb, if that functionality is exposed to the
           application users, each individual script should probably get its own isolated world.
*/
QQuickWebEngineScript::ScriptWorldId QQuickWebEngineScript::worldId() const
{
    Q_D(const QQuickWebEngineScript);
    return static_cast<QQuickWebEngineScript::ScriptWorldId>(d->coreScript.worldId());
}

/*!
    \qmlproperty int WebEngineScript::runOnSubframes

    Set this property to \c true if the script is executed on every frame in the page, or \c false
    if it is only ran for the main frame.
    The default value is \c{false}.
 */
bool QQuickWebEngineScript::runOnSubframes() const
{
    Q_D(const QQuickWebEngineScript);
    return d->coreScript.runsOnSubFrames();
}


void QQuickWebEngineScript::setName(QString arg)
{
    Q_D(QQuickWebEngineScript);
    if (arg == name())
        return;
    d->aboutToUpdateUnderlyingScript();
    d->coreScript.setName(arg);
    Q_EMIT nameChanged(arg);
}

void QQuickWebEngineScript::setSourceCode(QString arg)
{
    Q_D(QQuickWebEngineScript);
    if (arg == sourceCode())
        return;

    // setting the source directly resets the sourceUrl
    if (d->m_sourceUrl != QUrl()) {
        d->m_sourceUrl = QUrl();
        Q_EMIT sourceUrlChanged(d->m_sourceUrl);
    }

    d->aboutToUpdateUnderlyingScript();
    d->coreScript.setSourceCode(arg);
    Q_EMIT sourceCodeChanged(arg);
}

void QQuickWebEngineScript::setSourceUrl(QUrl arg)
{
    Q_D(QQuickWebEngineScript);
    if (arg == sourceUrl())
        return;

    d->m_sourceUrl = arg;
    Q_EMIT sourceUrlChanged(d->m_sourceUrl);

    QFile f(QQmlFile::urlToLocalFileOrQrc(arg));
    if (!f.open(QIODevice::ReadOnly)) {
        qWarning() << "Can't open user script " << arg;
        return;
    }

    d->aboutToUpdateUnderlyingScript();
    QString source = QString::fromUtf8(f.readAll());
    d->coreScript.setSourceCode(source);
    Q_EMIT sourceCodeChanged(source);
}

void QQuickWebEngineScript::setInjectionPoint(QQuickWebEngineScript::InjectionPoint arg)
{
    Q_D(QQuickWebEngineScript);
    if (arg == injectionPoint())
        return;
    d->aboutToUpdateUnderlyingScript();
    d->coreScript.setInjectionPoint(static_cast<UserScript::InjectionPoint>(arg));
    Q_EMIT injectionPointChanged(arg);
}


void QQuickWebEngineScript::setWorldId(QQuickWebEngineScript::ScriptWorldId arg)
{
    Q_D(QQuickWebEngineScript);
    if (arg == worldId())
        return;
    d->aboutToUpdateUnderlyingScript();
    d->coreScript.setWorldId(arg);
    Q_EMIT worldIdChanged(arg);
}


void QQuickWebEngineScript::setRunOnSubframes(bool arg)
{
    Q_D(QQuickWebEngineScript);
    if (arg == runOnSubframes())
        return;
    d->aboutToUpdateUnderlyingScript();
    d->coreScript.setRunsOnSubFrames(arg);
    Q_EMIT runOnSubframesChanged(arg);
}

void QQuickWebEngineScript::timerEvent(QTimerEvent *e)
{
    Q_D(QQuickWebEngineScript);
    if (e->timerId() != d->m_basicTimer.timerId()) {
        QObject::timerEvent(e);
        return;
    }
    if (!d->m_controllerHost)
        return;
    d->m_basicTimer.stop();
    d->m_controllerHost->addUserScript(d->coreScript, d->m_adapter);
}

void QQuickWebEngineScriptPrivate::bind(QtWebEngineCore::UserResourceControllerHost *resourceController, QtWebEngineCore::WebContentsAdapter *adapter)
{
    aboutToUpdateUnderlyingScript();
    m_adapter = adapter;
    m_controllerHost = resourceController;
}

QQuickWebEngineScriptPrivate::QQuickWebEngineScriptPrivate()
    :m_controllerHost(0)
    , m_adapter(0)

{
}

void QQuickWebEngineScriptPrivate::aboutToUpdateUnderlyingScript()
{
    Q_Q(QQuickWebEngineScript);
    if (m_controllerHost)
        m_controllerHost->removeUserScript(coreScript, m_adapter);
   // Defer updates to the next event loop
   m_basicTimer.start(0, q);
}