Okular

executor_js.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Pino Toscano <pino@kde.org>
3 SPDX-FileCopyrightText: 2008 Harri Porten <porten@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "config-okular.h"
9#include "executor_js_p.h"
10
11#include "../debug_p.h"
12#include "../document_p.h"
13
14#include "event_p.h"
15#include "js_app_p.h"
16#include "js_console_p.h"
17#include "js_data_p.h"
18#include "js_display_p.h"
19#include "js_document_p.h"
20#include "js_event_p.h"
21#include "js_field_p.h"
22#include "js_fullscreen_p.h"
23#include "js_global_p.h"
24#include "js_ocg_p.h"
25#include "js_spell_p.h"
26#include "js_util_p.h"
27
28#include <QDebug>
29#include <QJSEngine>
30#include <QThread>
31#include <QTimer>
32
33using namespace Okular;
34
35class Okular::ExecutorJSPrivate
36{
37public:
38 explicit ExecutorJSPrivate(DocumentPrivate *doc)
39 : m_doc(doc)
40 {
41 initTypes();
42 }
43 ~ExecutorJSPrivate()
44 {
45 m_watchdogTimer->deleteLater();
46 m_watchdogThread.quit();
47 m_watchdogThread.wait();
48 }
49
50 void initTypes();
51
52 DocumentPrivate *m_doc;
53 QJSEngine m_interpreter;
54
55 QThread m_watchdogThread;
56 QTimer *m_watchdogTimer = nullptr;
57};
58
59void ExecutorJSPrivate::initTypes()
60{
61 m_watchdogThread.start();
62 m_watchdogTimer = new QTimer;
63 m_watchdogTimer->setInterval(std::chrono::seconds(2)); // max 2 secs allowed
64 m_watchdogTimer->setSingleShot(true);
65 m_watchdogTimer->moveToThread(&m_watchdogThread);
67 m_watchdogTimer, &QTimer::timeout, &m_interpreter, [this]() { m_interpreter.setInterrupted(true); }, Qt::DirectConnection);
68
69 m_interpreter.globalObject().setProperty(QStringLiteral("app"), m_interpreter.newQObject(new JSApp(m_doc, m_watchdogTimer)));
70 m_interpreter.globalObject().setProperty(QStringLiteral("console"), m_interpreter.newQObject(new JSConsole));
71 m_interpreter.globalObject().setProperty(QStringLiteral("Doc"), m_interpreter.newQObject(new JSDocument(m_doc)));
72 m_interpreter.globalObject().setProperty(QStringLiteral("display"), m_interpreter.newQObject(new JSDisplay));
73 m_interpreter.globalObject().setProperty(QStringLiteral("spell"), m_interpreter.newQObject(new JSSpell));
74 m_interpreter.globalObject().setProperty(QStringLiteral("util"), m_interpreter.newQObject(new JSUtil));
75 m_interpreter.globalObject().setProperty(QStringLiteral("global"), m_interpreter.newQObject(new JSGlobal));
76}
77
78ExecutorJS::ExecutorJS(DocumentPrivate *doc)
79 : d(new ExecutorJSPrivate(doc))
80{
81}
82
83ExecutorJS::~ExecutorJS()
84{
85 JSField::clearCachedFields();
86 JSApp::clearCachedFields();
87 delete d;
88}
89
90void ExecutorJS::execute(const QString &script, Event *event)
91{
92 const auto eventVal = event ? d->m_interpreter.newQObject(new JSEvent(event)) : QJSValue(QJSValue::UndefinedValue);
93 d->m_interpreter.globalObject().setProperty(QStringLiteral("event"), eventVal);
94
95 QMetaObject::invokeMethod(d->m_watchdogTimer, qOverload<>(&QTimer::start));
96 d->m_interpreter.setInterrupted(false);
97 auto result = d->m_interpreter.evaluate(script, QStringLiteral("okular.js"));
98 QMetaObject::invokeMethod(d->m_watchdogTimer, qOverload<>(&QTimer::stop));
99
100 if (result.isError()) {
101 qCDebug(OkularCoreDebug) << "JS exception" << result.toString() << "(line " << result.property(QStringLiteral("lineNumber")).toInt() << ")";
102 } else {
103 qCDebug(OkularCoreDebug) << "result:" << result.toString();
104
105 if (event) {
106 qCDebug(OkularCoreDebug) << "Event Result:" << event->name() << event->type() << "value:" << event->value();
107 }
108 }
109}
global.h
Definition action.h:17
QJSValue globalObject() const const
QJSValue newQObject(QObject *object)
void setInterrupted(bool interrupted)
void setProperty(const QString &name, const QJSValue &value)
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
void moveToThread(QThread *targetThread)
DirectConnection
void quit()
void start(Priority priority)
bool wait(QDeadlineTimer deadline)
void setInterval(int msec)
void setSingleShot(bool singleShot)
void start()
void stop()
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:47:54 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.