summaryrefslogtreecommitdiff
path: root/src/core/api
diff options
context:
space:
mode:
authorMichal Klocek <michal.klocek@qt.io>2020-08-25 11:37:34 +0200
committerMichal Klocek <michal.klocek@qt.io>2020-09-24 15:14:59 +0200
commitf613095208911cfa6b8609f8b87b4984dc55e69c (patch)
tree09a25473d27a2b7d174966fb28594c07de8db510 /src/core/api
parente2e249b8bb399ef7bcf6fe3be0b4f2fc9fa18182 (diff)
downloadqtwebengine-f613095208911cfa6b8609f8b87b4984dc55e69c.tar.gz
Reuse qwebenginescript in qml
Reuse core class and adopt api: * add missing setUrlSource to c++ class * fix typing for setRunsOnSubFrames * remove all invokable from setters in qml, we use properties for that anyway. * remove invokable toString , since we have debug stream operator in c++ [ChangeLog] In qml websetttings.runOnSubframes is now websettings.runsOnSubFrames Change-Id: Iba822a7aa6a59940484c972726d710a1b66cb20d Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/core/api')
-rw-r--r--src/core/api/qwebenginescript.cpp36
-rw-r--r--src/core/api/qwebenginescript.h25
2 files changed, 57 insertions, 4 deletions
diff --git a/src/core/api/qwebenginescript.cpp b/src/core/api/qwebenginescript.cpp
index e56017166..e3917566a 100644
--- a/src/core/api/qwebenginescript.cpp
+++ b/src/core/api/qwebenginescript.cpp
@@ -41,9 +41,12 @@
#include "user_script.h"
#include <QtCore/QDebug>
+#include <QtCore/QFile>
using QtWebEngineCore::UserScript;
+QT_BEGIN_NAMESPACE
+
/*!
\class QWebEngineScript
\inmodule QtWebEngineCore
@@ -172,6 +175,37 @@ void QWebEngineScript::setName(const QString &scriptName)
d->setName(scriptName);
}
+
+QUrl QWebEngineScript::sourceUrl() const
+{
+ return d->sourceUrl();
+}
+
+void QWebEngineScript::setSourceUrl(const QUrl &url)
+{
+ if (url == sourceUrl())
+ return;
+
+ d->setSourceUrl(url);
+
+ QFile file;
+ if (url.scheme().compare(QLatin1String("qrc"), Qt::CaseInsensitive) == 0) {
+ if (url.authority().isEmpty())
+ file.setFileName(QLatin1Char(':') + url.path());
+ return;
+ } else {
+ file.setFileName(url.toLocalFile());
+ }
+
+ if (!file.open(QIODevice::ReadOnly)) {
+ qWarning() << "Can't open user script " << url;
+ return;
+ }
+
+ QString source = QString::fromUtf8(file.readAll());
+ setSourceCode(source);
+}
+
/*!
Returns the source of the script.
*/
@@ -291,3 +325,5 @@ QDebug operator<<(QDebug d, const QWebEngineScript &script)
return d.space();
}
#endif
+
+QT_END_NAMESPACE
diff --git a/src/core/api/qwebenginescript.h b/src/core/api/qwebenginescript.h
index 628e24ed5..42a98e3e2 100644
--- a/src/core/api/qwebenginescript.h
+++ b/src/core/api/qwebenginescript.h
@@ -41,9 +41,9 @@
#define QWEBENGINESCRIPT_H
#include <QtWebEngineCore/qtwebenginecoreglobal.h>
-
-#include <QtCore/qshareddata.h>
-#include <QtCore/qstring.h>
+#include <QtCore/QUrl>
+#include <QtCore/QObject>
+#include <QtCore/QSharedDataPointer>
namespace QtWebEngineCore {
class UserScript;
@@ -52,19 +52,33 @@ class UserScript;
QT_BEGIN_NAMESPACE
class Q_WEBENGINECORE_EXPORT QWebEngineScript {
+
+ Q_GADGET
+ Q_PROPERTY(QString name READ name WRITE setName FINAL)
+ Q_PROPERTY(QUrl sourceUrl READ sourceUrl WRITE setSourceUrl FINAL)
+ Q_PROPERTY(QString sourceCode READ sourceCode WRITE setSourceCode FINAL)
+ Q_PROPERTY(InjectionPoint injectionPoint READ injectionPoint WRITE setInjectionPoint FINAL)
+ Q_PROPERTY(quint32 worldId READ worldId WRITE setWorldId FINAL)
+ Q_PROPERTY(bool runsOnSubFrames READ runsOnSubFrames WRITE setRunsOnSubFrames FINAL)
+
public:
+
enum InjectionPoint {
Deferred,
DocumentReady,
DocumentCreation
};
+ Q_ENUM(InjectionPoint)
+
enum ScriptWorldId {
MainWorld = 0,
ApplicationWorld,
UserWorld
};
+ Q_ENUM(ScriptWorldId)
+
QWebEngineScript();
QWebEngineScript(const QWebEngineScript &other);
~QWebEngineScript();
@@ -76,6 +90,9 @@ public:
QString name() const;
void setName(const QString &);
+ QUrl sourceUrl() const;
+ void setSourceUrl(const QUrl &url);
+
QString sourceCode() const;
void setSourceCode(const QString &);
@@ -93,8 +110,8 @@ public:
{ return !operator==(other); }
void swap(QWebEngineScript &other) { qSwap(d, other.d); }
-
private:
+ friend class QQuickWebEngineScriptCollectionPrivate;
friend class QWebEngineScriptCollectionPrivate;
friend class QWebEngineScriptCollection;
QWebEngineScript(const QtWebEngineCore::UserScript &);