-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaScriptUtils.cpp
More file actions
769 lines (667 loc) · 21.5 KB
/
JavaScriptUtils.cpp
File metadata and controls
769 lines (667 loc) · 21.5 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2024 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "JavaScriptUtils.h"
#include "JavaScriptWrapper.h"
#include "rtLog.h"
#include "jsc_lib.h"
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <queue>
#include <sstream>
#include <thread>
#include <rtObject.h>
#include <rtWebSocket.h>
#include <uv.h>
#ifdef WS_SERVER_ENABLED
#include <rtWebSocketServer.h>
#endif
#ifdef ENABLE_JSRUNTIME_THUNDER_SECURITYAGENT
#include <securityagent/securityagent.h>
#endif
#include "rtHttpRequest.h"
#include "rtHttpResponse.h"
#include <glib.h>
#include <uv.h>
#include <JavaScriptContext.h>
#define MAX_TOKEN_BUFFER_LENGTH 2048
std::thread::id gMainThreadId;
static std::list<std::function<void ()>> gPendingFun;
static std::mutex gDispatchMutex;
struct TimeoutInfo
{
std::function<int ()> callback;
std::chrono::time_point<std::chrono::steady_clock> fireTime;
std::chrono::milliseconds interval;
bool repeat;
uint32_t tag;
bool canceled;
};
struct TimeoutInfoComparator
{
constexpr bool operator()(const TimeoutInfo *lhs, const TimeoutInfo *rhs) const {
return !((lhs->fireTime < rhs->fireTime) ||
((lhs->fireTime == rhs->fireTime) && (lhs->tag < rhs->tag)));
}
};
class TimeoutQueue : public std::priority_queue<TimeoutInfo*, std::vector<TimeoutInfo*>, TimeoutInfoComparator>
{
public:
void pushTimeouts(const std::vector<TimeoutInfo*>& timerVec)
{
if (!timerVec.size())
return;
c.reserve(c.size() + timerVec.size());
c.insert(c.end(),timerVec.begin(), timerVec.end());
std::make_heap(c.begin(), c.end(), comp);
}
bool updateForInfo(const TimeoutInfo* info)
{
auto it = std::find(c.begin(), c.end(), info);
if (it != c.end()) {
std::make_heap(c.begin(), c.end(), comp);
return true;
}
return false;
}
};
static std::map<uint32_t, TimeoutInfo*> gTimeoutMap;
static uint32_t gTimeoutIdx = 0;
static TimeoutQueue gTimeoutQueue;
void printException(JSContextRef ctx, JSValueRef exception)
{
JSStringRef exceptStr = JSValueToStringCopy(ctx, exception, nullptr);
rtString errorStr = jsToRtString(exceptStr);
JSStringRelease(exceptStr);
rtLogError("Got Exception: %s", errorStr.cString());
}
rtString jsToRtString(JSStringRef str)
{
if (!str)
return rtString();
size_t len = JSStringGetMaximumUTF8CStringSize(str);
std::unique_ptr<char[]> buffer(new char[len]);
len = JSStringGetUTF8CString(str, buffer.get(), len);
return rtString(buffer.get(), len); // does a copy
}
void dispatchPending()
{
std::unique_lock<std::mutex> lock(gDispatchMutex);
std::list<std::function<void ()>> pending = std::move(gPendingFun);
gDispatchMutex.unlock();
for(auto& fun : pending)
fun();
}
void dispatchOnMainLoop(std::function<void ()>&& fun)
{
std::unique_lock<std::mutex> lock(gDispatchMutex);
gPendingFun.push_back(std::move(fun));
}
void dispatchTimeouts()
{
const auto currentTime = std::chrono::steady_clock::now();
std::vector<TimeoutInfo*> timeoutsToRepeat;
while(!gTimeoutQueue.empty()) {
TimeoutInfo* info = gTimeoutQueue.top();
if (!info->canceled && info->fireTime > currentTime) {
break;
}
gTimeoutQueue.pop();
if (info->canceled) {
delete info;
continue;
}
int rc = info->callback();
if (rc != 0 || !info->repeat || info->canceled) {
if (!info->canceled)
gTimeoutMap.erase(info->tag);
delete info;
continue;
}
info->fireTime = currentTime + info->interval;
timeoutsToRepeat.push_back(info);
}
gTimeoutQueue.pushTimeouts(timeoutsToRepeat);
}
void assertIsMainThread()
{
assert(std::this_thread::get_id() == gMainThreadId);
}
class rtHttpRequestEx : public rtHttpRequest
{
public:
rtDeclareObject(rtHttpRequestEx, rtHttpRequest);
rtHttpRequestEx(const rtString& url)
: rtHttpRequest(url)
{ }
rtHttpRequestEx(const rtObjectRef& options)
: rtHttpRequest(options)
{ }
void onDownloadProgressImpl(double progress) final
{
AddRef();
dispatchOnMainLoop(
[this, downloadedsize = progress] ()
{
rtObjectRef e = new rtMapObject;
e.set("progress", downloadedsize);
mEmit.send("loadprogress", e);
Release();
});
}
void onDownloadCompleteImpl(rtFileDownloadRequest* downloadRequest) final
{
AddRef();
if (!downloadRequest->errorString().isEmpty()) {
dispatchOnMainLoop(
[this, errorString = downloadRequest->errorString(), statusCode = downloadRequest->downloadStatusCode()] ()
{
if (statusCode == 28)
{
mEmit.send("error", "TIMEDOUT");
}
else
{
mEmit.send("error", errorString);
}
Release();
});
} else {
rtHttpResponse* resp = new rtHttpResponse();
resp->setStatusCode((int32_t)downloadRequest->httpStatusCode());
resp->setErrorMessage(downloadRequest->errorString());
resp->setHeaders(downloadRequest->headerData(), downloadRequest->headerDataSize());
resp->setDownloadedData(downloadRequest->downloadedData(), downloadRequest->downloadedDataSize());
if (mMetricsListener)
{
rtLogWarn("metriclistener");
NetworkMetrics *metrics = new NetworkMetrics();
if (!metrics){
rtLogError("Failed to allocate NetworkMetrics");
return;
}
metrics->url = this->url();
metrics->method = this->method();
std::vector<rtString> headerValues = this->headers();
metrics->headers = headerValues;
metrics->statusCode = downloadRequest->httpStatusCode();
rtObjectRef timeMetrics = downloadRequest->downloadMetrics();
rtValue keys;
if (timeMetrics->Get("allKeys", &keys) == RT_OK) {
rtArrayObject* keysArray = (rtArrayObject*) keys.toObject().getPtr();
for (size_t i = 0; i < keysArray->length(); ++i) {
rtString key = keysArray->get<rtString>(i);
rtValue value;
if (timeMetrics->Get(key, &value) == RT_OK) {
metrics->timeMetricsData[key] = value;
}
}
}
mMetricsListener->onMetricsData(metrics);
delete metrics;
metrics = nullptr;
}
rtObjectRef protectedRef = resp;
dispatchOnMainLoop(
[this, resp = resp, ref = protectedRef] () {
mEmit.send("response", ref);
resp->onData();
resp->onEnd();
Release();
});
}
}
};
rtDefineObject(rtHttpRequestEx, rtHttpRequest);
rtError rtHttpGetBinding(int numArgs, const rtValue* args, rtValue* result, void* context)
{
UNUSED_PARAM(context);
if (numArgs < 1) {
rtLogError("%s: invalid args", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
JavaScriptContext* jscContext = (JavaScriptContext*)context;
if (!jscContext) {
rtLogError("context is null!");
}
rtHttpRequest* req;
if (args[0].getType() == RT_stringType) {
req = new rtHttpRequestEx(args[0].toString());
}
else {
if (args[0].getType() != RT_objectType) {
rtLogError("%s: invalid arg type", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
rtLogInfo("new rtHttpRequest");
req = new rtHttpRequestEx(args[0].toObject());
}
if (numArgs > 1 && args[1].getType() == RT_functionType) {
req->addListener("response", args[1].toFunction());
}
req->setNetworkMetricsListener(jscContext);
rtObjectRef ref = req;
*result = ref;
return RT_OK;
}
rtError rtSetVideoStartTimeBinding(int numArgs, const rtValue* args, rtValue* result, void* context)
{
if (context == nullptr || numArgs < 1) {
rtLogError("Invalid context or missing arguments");
return RT_ERROR_INVALID_ARG;
}
JavaScriptContext* jscContext = (JavaScriptContext*)context;
if (args[0].getType() != RT_intType && args[0].getType() != RT_doubleType) {
rtLogError("%s: invalid argument type", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
double playbackStartTime = args[0].toDouble();
jscContext->setPlaybackStartTime(playbackStartTime);
if (result)
*result = true;
return RT_OK;
}
rtError rtReadBinaryBinding(int numArgs, const rtValue* args, rtValue* result, void* context)
{
char *buffer = nullptr;
FILE *ptr = nullptr;
ptr = fopen("hello.wasm","rb"); // r for read, b for binary
char *fd = "hello.wasm";
struct stat buf;
stat(fd, &buf);
int size = buf.st_size;
buffer = (char*)malloc(size);
fread(buffer,size,1,ptr); // read 10 bytes to our buffer
fclose(ptr);
if (result)
{
result->setString(buffer);
}
return RT_OK;
}
rtError rtWebSocketBinding(int numArgs, const rtValue* args, rtValue* result, void* context)
{
UNUSED_PARAM(context);
if (numArgs < 1) {
rtLogError("%s: is invalid args", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
if (args[0].getType() != RT_objectType) {
rtLogError("%s: is invalid arg type ", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
rtObjectRef ref = new rtWebSocket(args[0].toObject());
*result = ref;
return RT_OK;
}
#ifdef WS_SERVER_ENABLED
std::vector<rtIObject*> gWebSocketServers;
rtError rtWebSocketServerBinding(int numArgs, const rtValue* args, rtValue* result, void* context)
{
UNUSED_PARAM(context);
if (numArgs < 1) {
rtLogError("%s: invalid args", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
if (args[0].getType() != RT_objectType) {
rtLogError("%s: invalid arg type", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
rtObjectRef ref = new rtWebSocketServer(args[0].toObject());
*result = ref;
gWebSocketServers.push_back(ref.getPtr());
return RT_OK;
}
#endif
rtError rtInstallTimeout(int numArgs, const rtValue* args, rtValue* result, bool repeat)
{
if (numArgs < 1) {
rtLogError("%s: invalid args", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
if (args[0].getType() != RT_functionType) {
rtLogError("%s: invalid arg type", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
double interval = 0;
if (numArgs >= 2)
interval = args[1].toDouble();
rtFunctionRef timeoutCb = args[0].toFunction();
std::vector<rtValue> timeoutArgs;
if (numArgs > 2) {
timeoutArgs.reserve(numArgs - 2);
for (int i = 2; i < numArgs; ++i)
timeoutArgs.push_back(args[i]);
}
uint32_t tag = installTimeout(interval, repeat,
[timeoutCbRef = std::move(timeoutCb), timeoutArgs = std::move(timeoutArgs)] () mutable {
rtError rc = timeoutCbRef.rtFunctionBase::Send(timeoutArgs.size(), timeoutArgs.data());
if (rc != RT_OK) {
rtLogError("timer callback send failed, rc = %d", rc);
return 1;
}
return 0;
});
if (result)
*result = tag;
return RT_OK;
}
rtError rtSetItervalBinding(int numArgs, const rtValue* args, rtValue* result, void* context)
{
UNUSED_PARAM(context);
return rtInstallTimeout(numArgs, args, result, true);
}
rtError rtSetTimeoutBinding(int numArgs, const rtValue* args, rtValue* result, void* context)
{
UNUSED_PARAM(context);
return rtInstallTimeout(numArgs, args, result, false);
}
rtError rtClearTimeoutBinding(int numArgs, const rtValue* args, rtValue* result, void* context)
{
UNUSED_PARAM(context);
if (numArgs < 1) {
rtLogError("%s: invalid args", __FUNCTION__);
return RT_ERROR_INVALID_ARG;
}
if (args[0].isEmpty()) {
rtLogWarn("%s: cannot remove time for 'null' or 'undefined' tag", __FUNCTION__);
return RT_OK;
}
uint32_t tag = args[0].toUInt32();
clearTimeout(tag);
return RT_OK;
}
rtError getThunderTokenBinding(int numArgs, const rtValue* args, rtValue* result, void* context)
{
#ifdef ENABLE_JSRUNTIME_THUNDER_SECURITYAGENT
JavaScriptContext* jsccontext = (JavaScriptContext*) context;
if (!jsccontext)
{
rtLogError("lost context !!!");
return RT_ERROR;
}
if (result)
{
result->setString("");
}
else
{
rtLogError("lost return value !!!");
return RT_ERROR;
}
unsigned char tokenBuffer[MAX_TOKEN_BUFFER_LENGTH];
memset(tokenBuffer, 0, MAX_TOKEN_BUFFER_LENGTH);
std::string appUrl = jsccontext->getUrl();
rtString params(appUrl.c_str());
size_t paramsLength = (size_t)params.length();
if(!memcpy(tokenBuffer,params.cString(),paramsLength))
{
rtLogError("unable to copy url buffer for token");
return RT_FAIL;
}
rtLogInfo("thunder request: %s length: %d", (char*)tokenBuffer, (int)paramsLength);
int tokenResult = GetToken(MAX_TOKEN_BUFFER_LENGTH, paramsLength, tokenBuffer);
if (tokenResult < 0)
{
rtLogError("unable to get token for app");
return RT_FAIL;
}
rtString tokenString = (const char*) tokenBuffer;
if (nullptr != result)
{
result->setString(tokenString);
}
return RT_OK;
#else
{
rtLogError("thunder security agent is disabled");
return RT_OK;
}
#endif
}
void clearTimeout(uint32_t tag)
{
auto it = gTimeoutMap.find(tag);
if (it != gTimeoutMap.end()) {
TimeoutInfo* info = it->second;
constexpr auto steadyMinimal = std::chrono::steady_clock::time_point::min();
info->fireTime = steadyMinimal;
info->canceled = true;
gTimeoutMap.erase(it);
gTimeoutQueue.updateForInfo(info);
}
}
uint32_t installTimeout(double intervalMs, bool repeat, std::function<int ()>&& fun)
{
if (intervalMs < 0)
intervalMs = 0;
auto currentTime = std::chrono::steady_clock::now();
auto interval = std::chrono::milliseconds(static_cast<uint32_t>(intervalMs));
TimeoutInfo *info = new TimeoutInfo;
info->interval = interval;
info->fireTime = currentTime + info->interval;
info->repeat = repeat;
info->callback = std::move(fun);
info->canceled = false;
info->tag = ++gTimeoutIdx; // FIXME:
gTimeoutMap[info->tag] = info;
gTimeoutQueue.push(info);
return info->tag;
}
static std::string readFile(const char *file)
{
std::ifstream src_file(file);
std::stringstream src_script;
src_script << src_file.rdbuf();
return src_script.str();
}
static bool fileExists(const char* name)
{
struct stat buffer;
bool ret = (stat (name, &buffer) == 0);
return ret;
}
JSValueRef requireCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception)
{
if (argumentCount != 1)
return JSValueMakeNull(ctx);
static const auto resolveModulePath = [](const rtString &name, rtString &data) -> bool
{
std::list<rtString> dirs;
std::list<rtString> endings;
bool found = false;
rtString path;
dirs.push_back(""); // this dir
dirs.push_back("/home/root/"); // this dir
dirs.push_back("modules/");
dirs.push_back("modules/lib");
dirs.push_back("/home/root/modules/");
dirs.push_back("/home/root/modules/lib");
endings.push_back(".js");
std::list<rtString>::const_iterator current, next;
for (current = dirs.begin(); !found && current != dirs.end(); ++current) {
rtString s = *current;
if (!s.isEmpty() && !s.endsWith("/"))
s.append("/");
s.append(name.beginsWith("./") ? name.substring(2) : name);
for (next = endings.begin(); !found && next != endings.end(); ++next) {
path = s;
if (!path.endsWith((*next).cString()))
path.append(*next);
found = fileExists(path.cString());
}
}
if (found)
data = path;
return found;
};
do {
JSStringRef reqArgStr = JSValueToStringCopy(ctx, arguments[0], exception);
if (exception && *exception)
break;
rtString moduleName = jsToRtString(reqArgStr);
rtString path;
if (!resolveModulePath(moduleName, path)) {
JSStringRelease(reqArgStr);
rtLogError("Module '%s' not found", moduleName.cString());
break;
}
JSGlobalContextRef globalCtx = JSContextGetGlobalContext(ctx);
rtJSCContextPrivate* priv = rtJSCContextPrivate::fromCtx(globalCtx);
if (!priv) {
rtLogError(" %s ... no priv object.",__PRETTY_FUNCTION__);
break;
}
if (JSObjectRef moduleObj = priv->findModule(path)) {
JSStringRelease(reqArgStr);
return moduleObj;
}
rtLogInfo("Loading %s", path.cString());
std::string codeStr = readFile(path.cString());
if (codeStr.empty()) {
JSStringRelease(reqArgStr);
rtLogError(" %s ... load error / not found.",__PRETTY_FUNCTION__);
break;
}
codeStr =
"(function(){ let m = {}; m.exports = {}; \n"
" (function(module, exports){\n"
+ codeStr +
" \n}).call(undefined, m, m.exports); return m;})()";
JSStringRef jsstr = JSStringCreateWithUTF8CString(codeStr.c_str());
JSValueRef module = JSEvaluateScript(globalCtx, jsstr, nullptr, reqArgStr, 0, exception);
JSStringRelease(jsstr);
JSStringRelease(reqArgStr);
if (exception && *exception) {
JSStringRef exceptStr = JSValueToStringCopy(globalCtx, *exception, nullptr);
rtString errorStr = jsToRtString(exceptStr);
JSStringRelease(exceptStr);
rtLogError("Failed to eval, \n\terror='%s'\n\tmodule=%s\n\tscript='...'", errorStr.cString(), path.cString());
break;
}
JSObjectRef moduleObj = JSValueToObject(globalCtx, module, exception);
if (exception && *exception) {
JSStringRef exceptStr = JSValueToStringCopy(globalCtx, *exception, nullptr);
rtString errorStr = jsToRtString(exceptStr);
JSStringRelease(exceptStr);
rtLogError("Failed to convert module to object, \n\terror='%s'\n\tmodule=%s", errorStr.cString(), path.cString());
break;
}
static JSStringRef exportsStr = JSStringCreateWithUTF8CString("exports");
JSValueRef exportsVal = JSObjectGetProperty(globalCtx, moduleObj, exportsStr, exception);
if (exception && *exception) {
JSStringRef exceptStr = JSValueToStringCopy(globalCtx, *exception, nullptr);
rtString errorStr = jsToRtString(exceptStr);
JSStringRelease(exceptStr);
rtLogError("Failed to get exports module to object, \n\terror='%s'\n\tmodule=%s", errorStr.cString(), path.cString());
break;
}
JSObjectRef exportsObj = JSValueToObject(globalCtx, exportsVal, exception);
if (exception && *exception) {
printException(globalCtx, *exception);
break;
}
priv->addToModuleCache(path, globalCtx, exportsObj);
return exportsVal;
} while(0);
return JSValueMakeNull(ctx);
}
char* JSValueToCString(JSContextRef context, JSValueRef value, JSValueRef* exception)
{
JSStringRef jsstr = JSValueToStringCopy(context, value, exception);
size_t len = JSStringGetMaximumUTF8CStringSize(jsstr);
char* src = new char[len];
JSStringGetUTF8CString(jsstr, src, len);
JSStringRelease(jsstr);
return src;
}
rtError rtJSRuntimeDownloadMetrics(int numArgs, const rtValue* args, rtValue* result, void* context)
{
JavaScriptContext* jscContext = (JavaScriptContext*)context;
if (jscContext == nullptr) {
rtLogError("context null");
return RT_FAIL;
}
rtMapObject* map = jscContext->getNetworkMetricsData();
if (!map) {
return RT_FAIL;
}
rtArrayObject* netMetricsArray = new rtArrayObject();
rtValue keys;
if (map->Get("allKeys", &keys) != RT_OK) {
rtLogWarn("Could not retrieve url for network metrics data.");
return RT_FAIL;
}
rtObjectRef objRef = keys.toObject();
rtArrayObject* keysArray = static_cast<rtArrayObject*>(objRef.getPtr());
if (!keysArray) {
rtLogWarn("No url found in the network metrics data.");
return RT_FAIL;
}
size_t count = keysArray->length();
for (size_t i = 0; i < count; ++i) {
rtValue keyValue;
rtString key = keysArray->get<rtString>(i);
keysArray->Get(key, &keyValue);
rtValue storedValue;
if (map->Get(key.cString(), &storedValue) == RT_OK) {
NetworkMetrics* metrics = (NetworkMetrics*)storedValue.toVoidPtr();
if (!metrics) {
rtLogError("Failed to cast stored value to NetworkMetrics structure for url: %s.", key.cString());
return RT_FAIL;
}
rtMapObject* metricsMap = new rtMapObject();
metricsMap->set("url", metrics->url);
metricsMap->set("method", metrics->method);
rtArrayObject* headersArray = new rtArrayObject();
for (const auto& header : metrics->headers) {
headersArray->pushBack(rtValue(header));
}
metricsMap->set("headers", rtValue(headersArray));
delete headersArray;
rtArrayObject* timeMetricsArray = new rtArrayObject();
for (const auto& metric : metrics->timeMetricsData) {
rtObjectRef timeMetricObj = new rtMapObject();
timeMetricObj->Set(metric.first.cString(), &metric.second);
timeMetricsArray->pushBack(rtValue(timeMetricObj));
delete timeMetricObj;
timeMetricObj = nullptr;
}
metricsMap->set("timeMetricsData", rtValue(timeMetricsArray));
delete timeMetricsArray;
netMetricsArray->pushBack(rtValue(metricsMap));
}
else {
rtLogWarn("Url not found in network metrics data: %s", key.cString());
}
}
*result = rtValue(netMetricsArray);
return RT_OK;
}