mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 18:56:34 +01:00
Unregister object and remove scripts when plugin unloads
This commit is contained in:
parent
9877833d15
commit
97dbf5aff6
@ -23,6 +23,13 @@ QmlExternalJsObject::QmlExternalJsObject(QObject *parent)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QmlExternalJsObject::~QmlExternalJsObject()
|
||||||
|
{
|
||||||
|
for (QObject *object : m_objects) {
|
||||||
|
ExternalJsObject::unregisterExtraObject(object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void QmlExternalJsObject::registerExtraObject(const QVariantMap &map)
|
void QmlExternalJsObject::registerExtraObject(const QVariantMap &map)
|
||||||
{
|
{
|
||||||
if (!map.contains(QSL("id")) || !map.contains(QSL("object"))) {
|
if (!map.contains(QSL("id")) || !map.contains(QSL("object"))) {
|
||||||
@ -37,6 +44,7 @@ void QmlExternalJsObject::registerExtraObject(const QVariantMap &map)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ExternalJsObject::registerExtraObject(id, object);
|
ExternalJsObject::registerExtraObject(id, object);
|
||||||
|
m_objects.append(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QmlExternalJsObject::unregisterExtraObject(QObject *object)
|
void QmlExternalJsObject::unregisterExtraObject(QObject *object)
|
||||||
|
@ -24,6 +24,9 @@ class QmlExternalJsObject : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit QmlExternalJsObject(QObject *parent = nullptr);
|
explicit QmlExternalJsObject(QObject *parent = nullptr);
|
||||||
|
~QmlExternalJsObject();
|
||||||
Q_INVOKABLE void registerExtraObject(const QVariantMap &map);
|
Q_INVOKABLE void registerExtraObject(const QVariantMap &map);
|
||||||
Q_INVOKABLE void unregisterExtraObject(QObject *object);
|
Q_INVOKABLE void unregisterExtraObject(QObject *object);
|
||||||
|
private:
|
||||||
|
QList<QObject *> m_objects;
|
||||||
};
|
};
|
||||||
|
@ -25,6 +25,14 @@ QmlUserScripts::QmlUserScripts(QObject *parent)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QmlUserScripts::~QmlUserScripts()
|
||||||
|
{
|
||||||
|
// remove scripts added by the plugin
|
||||||
|
for (const QWebEngineScript &webEngineScript : m_webEngineScripts) {
|
||||||
|
mApp->webProfile()->scripts()->remove(webEngineScript);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int QmlUserScripts::count() const
|
int QmlUserScripts::count() const
|
||||||
{
|
{
|
||||||
return mApp->webProfile()->scripts()->count();
|
return mApp->webProfile()->scripts()->count();
|
||||||
@ -93,7 +101,7 @@ QList<QObject*> QmlUserScripts::findScripts(const QString &name) const
|
|||||||
* @brief Inserts a script into collection
|
* @brief Inserts a script into collection
|
||||||
* @param object of type QmlUserScript
|
* @param object of type QmlUserScript
|
||||||
*/
|
*/
|
||||||
void QmlUserScripts::insert(QObject *object) const
|
void QmlUserScripts::insert(QObject *object)
|
||||||
{
|
{
|
||||||
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
|
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
|
||||||
if (!userScript) {
|
if (!userScript) {
|
||||||
@ -101,13 +109,14 @@ void QmlUserScripts::insert(QObject *object) const
|
|||||||
}
|
}
|
||||||
QWebEngineScript webEngineScript = userScript->webEngineScript();
|
QWebEngineScript webEngineScript = userScript->webEngineScript();
|
||||||
mApp->webProfile()->scripts()->insert(webEngineScript);
|
mApp->webProfile()->scripts()->insert(webEngineScript);
|
||||||
|
m_webEngineScripts.append(webEngineScript);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Inserts a list of scripts into collection
|
* @brief Inserts a list of scripts into collection
|
||||||
* @param list of objects, each of type QmlUserScript
|
* @param list of objects, each of type QmlUserScript
|
||||||
*/
|
*/
|
||||||
void QmlUserScripts::insert(const QList<QObject *> &list) const
|
void QmlUserScripts::insert(const QList<QObject *> &list)
|
||||||
{
|
{
|
||||||
for (QObject *object : list) {
|
for (QObject *object : list) {
|
||||||
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
|
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
|
||||||
@ -116,6 +125,7 @@ void QmlUserScripts::insert(const QList<QObject *> &list) const
|
|||||||
}
|
}
|
||||||
QWebEngineScript webEngineScript = userScript->webEngineScript();
|
QWebEngineScript webEngineScript = userScript->webEngineScript();
|
||||||
mApp->webProfile()->scripts()->insert(webEngineScript);
|
mApp->webProfile()->scripts()->insert(webEngineScript);
|
||||||
|
m_webEngineScripts.append(webEngineScript);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,17 +40,19 @@ class FALKON_EXPORT QmlUserScripts : public QObject
|
|||||||
Q_PROPERTY(bool empty READ empty CONSTANT)
|
Q_PROPERTY(bool empty READ empty CONSTANT)
|
||||||
public:
|
public:
|
||||||
explicit QmlUserScripts(QObject *parent = nullptr);
|
explicit QmlUserScripts(QObject *parent = nullptr);
|
||||||
|
~QmlUserScripts();
|
||||||
Q_INVOKABLE bool contains(QObject *object) const;
|
Q_INVOKABLE bool contains(QObject *object) const;
|
||||||
Q_INVOKABLE QObject *findScript(const QString &name) const;
|
Q_INVOKABLE QObject *findScript(const QString &name) const;
|
||||||
Q_INVOKABLE QList<QObject *> findScripts(const QString &name) const;
|
Q_INVOKABLE QList<QObject *> findScripts(const QString &name) const;
|
||||||
Q_INVOKABLE void insert(QObject *object) const;
|
Q_INVOKABLE void insert(QObject *object);
|
||||||
Q_INVOKABLE void insert(const QList<QObject*> &list) const;
|
Q_INVOKABLE void insert(const QList<QObject*> &list);
|
||||||
Q_INVOKABLE void remove(QObject *object) const;
|
Q_INVOKABLE void remove(QObject *object) const;
|
||||||
Q_INVOKABLE QList<QObject *> toList() const;
|
Q_INVOKABLE QList<QObject *> toList() const;
|
||||||
private:
|
private:
|
||||||
int count() const;
|
int count() const;
|
||||||
int size() const;
|
int size() const;
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
|
QList<QWebEngineScript> m_webEngineScripts;
|
||||||
|
|
||||||
QList<QObject *> toQObjectList(QList<QWebEngineScript> list) const;
|
QList<QObject *> toQObjectList(QList<QWebEngineScript> list) const;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user