1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

Unregister object and remove scripts when plugin unloads

This commit is contained in:
Anmol Gautam 2018-07-09 18:09:02 +05:30
parent 9877833d15
commit 97dbf5aff6
4 changed files with 27 additions and 4 deletions

View File

@ -23,6 +23,13 @@ QmlExternalJsObject::QmlExternalJsObject(QObject *parent)
{
}
QmlExternalJsObject::~QmlExternalJsObject()
{
for (QObject *object : m_objects) {
ExternalJsObject::unregisterExtraObject(object);
}
}
void QmlExternalJsObject::registerExtraObject(const QVariantMap &map)
{
if (!map.contains(QSL("id")) || !map.contains(QSL("object"))) {
@ -37,6 +44,7 @@ void QmlExternalJsObject::registerExtraObject(const QVariantMap &map)
return;
}
ExternalJsObject::registerExtraObject(id, object);
m_objects.append(object);
}
void QmlExternalJsObject::unregisterExtraObject(QObject *object)

View File

@ -24,6 +24,9 @@ class QmlExternalJsObject : public QObject
Q_OBJECT
public:
explicit QmlExternalJsObject(QObject *parent = nullptr);
~QmlExternalJsObject();
Q_INVOKABLE void registerExtraObject(const QVariantMap &map);
Q_INVOKABLE void unregisterExtraObject(QObject *object);
private:
QList<QObject *> m_objects;
};

View File

@ -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
{
return mApp->webProfile()->scripts()->count();
@ -93,7 +101,7 @@ QList<QObject*> QmlUserScripts::findScripts(const QString &name) const
* @brief Inserts a script into collection
* @param object of type QmlUserScript
*/
void QmlUserScripts::insert(QObject *object) const
void QmlUserScripts::insert(QObject *object)
{
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
if (!userScript) {
@ -101,13 +109,14 @@ void QmlUserScripts::insert(QObject *object) const
}
QWebEngineScript webEngineScript = userScript->webEngineScript();
mApp->webProfile()->scripts()->insert(webEngineScript);
m_webEngineScripts.append(webEngineScript);
}
/**
* @brief Inserts a list of scripts into collection
* @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) {
QmlUserScript *userScript = qobject_cast<QmlUserScript*>(object);
@ -116,6 +125,7 @@ void QmlUserScripts::insert(const QList<QObject *> &list) const
}
QWebEngineScript webEngineScript = userScript->webEngineScript();
mApp->webProfile()->scripts()->insert(webEngineScript);
m_webEngineScripts.append(webEngineScript);
}
}

View File

@ -40,17 +40,19 @@ class FALKON_EXPORT QmlUserScripts : public QObject
Q_PROPERTY(bool empty READ empty CONSTANT)
public:
explicit QmlUserScripts(QObject *parent = nullptr);
~QmlUserScripts();
Q_INVOKABLE bool contains(QObject *object) const;
Q_INVOKABLE QObject *findScript(const QString &name) const;
Q_INVOKABLE QList<QObject *> findScripts(const QString &name) const;
Q_INVOKABLE void insert(QObject *object) const;
Q_INVOKABLE void insert(const QList<QObject*> &list) const;
Q_INVOKABLE void insert(QObject *object);
Q_INVOKABLE void insert(const QList<QObject*> &list);
Q_INVOKABLE void remove(QObject *object) const;
Q_INVOKABLE QList<QObject *> toList() const;
private:
int count() const;
int size() const;
bool empty() const;
QList<QWebEngineScript> m_webEngineScripts;
QList<QObject *> toQObjectList(QList<QWebEngineScript> list) const;
};