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_ocg_p.h"
24#include "js_spell_p.h"
25#include "js_util_p.h"
26
27#include <QDebug>
28#include <QJSEngine>
29#include <QThread>
30#include <QTimer>
31
32using namespace Okular;
33
34class Okular::ExecutorJSPrivate
35{
36public:
37 explicit ExecutorJSPrivate(DocumentPrivate *doc)
38 : m_doc(doc)
39 {
40 initTypes();
41 }
42 ~ExecutorJSPrivate()
43 {
44 m_watchdogTimer->deleteLater();
45 m_watchdogThread.quit();
46 m_watchdogThread.wait();
47 }
48
49 void initTypes();
50
51 DocumentPrivate *m_doc;
52 QJSEngine m_interpreter;
53
54 QThread m_watchdogThread;
55 QTimer *m_watchdogTimer = nullptr;
56};
57
58void ExecutorJSPrivate::initTypes()
59{
60 m_watchdogThread.start();
61 m_watchdogTimer = new QTimer;
62 m_watchdogTimer->setInterval(std::chrono::seconds(2)); // max 2 secs allowed
63 m_watchdogTimer->setSingleShot(true);
64 m_watchdogTimer->moveToThread(&m_watchdogThread);
66 m_watchdogTimer, &QTimer::timeout, &m_interpreter, [this]() { m_interpreter.setInterrupted(true); }, Qt::DirectConnection);
67
68 m_interpreter.globalObject().setProperty(QStringLiteral("app"), m_interpreter.newQObject(new JSApp(m_doc, m_watchdogTimer)));
69 m_interpreter.globalObject().setProperty(QStringLiteral("console"), m_interpreter.newQObject(new JSConsole));
70 m_interpreter.globalObject().setProperty(QStringLiteral("Doc"), m_interpreter.newQObject(new JSDocument(m_doc)));
71 m_interpreter.globalObject().setProperty(QStringLiteral("display"), m_interpreter.newQObject(new JSDisplay));
72 m_interpreter.globalObject().setProperty(QStringLiteral("spell"), m_interpreter.newQObject(new JSSpell));
73 m_interpreter.globalObject().setProperty(QStringLiteral("util"), m_interpreter.newQObject(new JSUtil));
74}
75
76ExecutorJS::ExecutorJS(DocumentPrivate *doc)
77 : d(new ExecutorJSPrivate(doc))
78{
79}
80
81ExecutorJS::~ExecutorJS()
82{
83 JSField::clearCachedFields();
84 JSApp::clearCachedFields();
85 delete d;
86}
87
88void ExecutorJS::execute(const QString &script, Event *event)
89{
90 const auto eventVal = event ? d->m_interpreter.newQObject(new JSEvent(event)) : QJSValue(QJSValue::UndefinedValue);
91 d->m_interpreter.globalObject().setProperty(QStringLiteral("event"), eventVal);
92
93 QMetaObject::invokeMethod(d->m_watchdogTimer, qOverload<>(&QTimer::start));
94 d->m_interpreter.setInterrupted(false);
95 auto result = d->m_interpreter.evaluate(script, QStringLiteral("okular.js"));
96 QMetaObject::invokeMethod(d->m_watchdogTimer, qOverload<>(&QTimer::stop));
97
98 if (result.isError()) {
99 qCDebug(OkularCoreDebug) << "JS exception" << result.toString() << "(line " << result.property(QStringLiteral("lineNumber")).toInt() << ")";
100 } else {
101 qCDebug(OkularCoreDebug) << "result:" << result.toString();
102
103 if (event) {
104 qCDebug(OkularCoreDebug) << "Event Result:" << event->name() << event->type() << "value:" << event->value();
105 }
106 }
107}
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 Tue Mar 26 2024 11:17:35 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.