1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
#include <QFile>
#include <QDir>
#include "jskitmanager.h"
#include "jskitobjects.h"
JSKitManager::JSKitManager(WatchConnector *watch, AppManager *apps, AppMsgManager *appmsg, Settings *settings, QObject *parent) :
QObject(parent), _watch(watch), _apps(apps), _appmsg(appmsg), _settings(settings), _engine(0)
{
connect(_appmsg, &AppMsgManager::appStarted, this, &JSKitManager::handleAppStarted);
connect(_appmsg, &AppMsgManager::appStopped, this, &JSKitManager::handleAppStopped);
}
JSKitManager::~JSKitManager()
{
if (_engine) {
stopJsApp();
}
}
QJSEngine * JSKitManager::engine()
{
return _engine;
}
bool JSKitManager::isJSKitAppRunning() const
{
return _engine != 0;
}
QString JSKitManager::describeError(QJSValue error)
{
return QString("%1:%2: %3")
.arg(error.property("fileName").toString())
.arg(error.property("lineNumber").toInt())
.arg(error.toString());
}
void JSKitManager::showConfiguration()
{
if (_engine) {
logger()->debug() << "requesting configuration";
_jspebble->invokeCallbacks("showConfiguration");
} else {
logger()->warn() << "requested to show configuration, but JS engine is not running";
}
}
void JSKitManager::handleWebviewClosed(const QString &result)
{
if (_engine) {
QJSValue eventObj = _engine->newObject();
eventObj.setProperty("response", _engine->toScriptValue(result));
logger()->debug() << "webview closed with the following result: " << result;
_jspebble->invokeCallbacks("webviewclosed", QJSValueList({eventObj}));
} else {
logger()->warn() << "webview closed event, but JS engine is not running";
}
}
void JSKitManager::handleAppStarted(const QUuid &uuid)
{
AppInfo info = _apps->info(uuid);
if (!info.uuid().isNull() && info.isJSKit()) {
logger()->debug() << "Preparing to start JSKit app" << info.uuid() << info.shortName();
_curApp = info;
startJsApp();
}
}
void JSKitManager::handleAppStopped(const QUuid &uuid)
{
if (!_curApp.uuid().isNull()) {
if (_curApp.uuid() != uuid) {
logger()->warn() << "Closed app with invalid UUID";
}
stopJsApp();
_curApp.setUuid(QUuid()); // Clear the uuid to force invalid app
}
}
void JSKitManager::handleAppMessage(const QUuid &uuid, const QVariantMap &msg)
{
if (_curApp.uuid() == uuid) {
logger()->debug() << "received a message for the current JSKit app";
if (!_engine) {
logger()->debug() << "but engine is stopped";
return;
}
QJSValue eventObj = _engine->newObject();
eventObj.setProperty("payload", _engine->toScriptValue(msg));
_jspebble->invokeCallbacks("appmessage", QJSValueList({eventObj}));
}
}
bool JSKitManager::loadJsFile(const QString &filename)
{
Q_ASSERT(_engine);
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
logger()->warn() << "Failed to load JS file:" << file.fileName();
return false;
}
logger()->debug() << "now parsing" << file.fileName();
QJSValue result = _engine->evaluate(QString::fromUtf8(file.readAll()), file.fileName());
if (result.isError()) {
logger()->warn() << "error while evaluating JS script:" << describeError(result);
return false;
}
logger()->debug() << "JS script evaluated";
return true;
}
void JSKitManager::startJsApp()
{
if (_engine) stopJsApp();
if (_curApp.uuid().isNull()) {
logger()->warn() << "Attempting to start JS app with invalid UUID";
return;
}
_engine = new QJSEngine(this);
_jspebble = new JSKitPebble(_curApp, this);
_jsconsole = new JSKitConsole(this);
_jsstorage = new JSKitLocalStorage(_curApp.uuid(), this);
_jsgeo = new JSKitGeolocation(this);
logger()->debug() << "starting JS app";
QJSValue globalObj = _engine->globalObject();
globalObj.setProperty("Pebble", _engine->newQObject(_jspebble));
globalObj.setProperty("console", _engine->newQObject(_jsconsole));
globalObj.setProperty("localStorage", _engine->newQObject(_jsstorage));
QJSValue navigatorObj = _engine->newObject();
navigatorObj.setProperty("geolocation", _engine->newQObject(_jsgeo));
globalObj.setProperty("navigator", navigatorObj);
// Set this.window = this
globalObj.setProperty("window", globalObj);
// Shims for compatibility...
QJSValue result = _engine->evaluate(
"function XMLHttpRequest() { return Pebble.createXMLHttpRequest(); }\n"
);
Q_ASSERT(!result.isError());
// Polyfills...
loadJsFile("/usr/share/pebble/js/typedarray.js");
// Now load the actual script
loadJsFile(_curApp.path() + "/pebble-js-app.js");
// Setup the message callback
QUuid uuid = _curApp.uuid();
_appmsg->setMessageHandler(uuid, [this, uuid](const QVariantMap &msg) {
QMetaObject::invokeMethod(this, "handleAppMessage", Qt::QueuedConnection,
Q_ARG(QUuid, uuid),
Q_ARG(QVariantMap, msg));
// Invoke the slot as a queued connection to give time for the ACK message
// to go through first.
return true;
});
// We try to invoke the callbacks even if script parsing resulted in error...
_jspebble->invokeCallbacks("ready");
}
void JSKitManager::stopJsApp()
{
if (!_engine) return; // Nothing to do!
logger()->debug() << "stopping JS app";
if (!_curApp.uuid().isNull()) {
_appmsg->clearMessageHandler(_curApp.uuid());
}
_engine->collectGarbage();
_engine->deleteLater();
_engine = 0;
_jsstorage->deleteLater();
_jsstorage = 0;
_jsgeo->deleteLater();
_jsgeo = 0;
_jspebble->deleteLater();
_jspebble = 0;
}
|