Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/EssosInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class EssosInstance
void onKeyRelease(struct JavaScriptKeyDetails& details);
void update();
void registerKeyListener(JavaScriptKeyListener*);

private:
EssosInstance();
static EssosInstance* mInstance;
Expand Down
15 changes: 9 additions & 6 deletions include/NativeJSRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,30 @@ namespace JsRuntime {
bool terminate();
void run();
void setEnvForConsoleMode(ModuleSettings& moduleSettings);
bool runApplication(uint32_t id, std::string url);
bool runJavaScript(uint32_t id, std::string code);
static std::atomic_bool consoleLoop;
std::atomic_bool mShutdownConsole{false};
std::thread mConsoleThread;
bool runApplication(uint32_t id, std::string url);
bool runJavaScript(uint32_t id, std::string code);
uint32_t createApplication(ModuleSettings& moduleSettings, std::string userAgent = DEFAULT_USER_AGENT) ;
bool terminateApplication(uint32_t id);
std::list<ApplicationDetails> getApplications();
void setExternalApplicationHandler(std::shared_ptr<IExternalApplicationHandler> handler);
std::string getBaseUserAgent();
private:
bool downloadFile(std::string& url, MemoryStruct& chunk);
void processDevConsoleRequests();
void processDevConsoleRequests();
void runDeveloperConsole(ModuleSettings moduleSettings);
void createApplicationInternal(ApplicationRequest& appRequest);
void createApplicationInternal(ApplicationRequest& appRequest);
void runApplicationInternal(ApplicationRequest& appRequest);
void terminateApplicationInternal(ApplicationRequest& appRequest);
void runJavaScriptInternal(ApplicationRequest& appRequest);
uint32_t createApplicationIdentifier();
static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream);
IJavaScriptEngine* mEngine;
IJavaScriptEngine* mEngine;
bool mRunning;
std::string mTestFileName;
std::unique_ptr<ConsoleState> mConsoleState;
std::unique_ptr<ConsoleState> mConsoleState;
bool mEnableTestFileDOMSupport;
bool mEmbedThunderJS;
bool mEmbedRdkWebBridge;
Expand Down
2 changes: 1 addition & 1 deletion include/jsc/JavaScriptContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class JavaScriptContext: public JavaScriptContextBase, public NetworkMetricsList

void setCreateApplicationStartTime(double time);
void setCreateApplicationEndTime(double time,uint32_t id);
void setPlaybackStartTime(double time);
virtual void setPlaybackStartTime(double time);
void setAppdata(uint32_t id, const std::string& url);
double getExecutionDuration() const;

Expand Down
42 changes: 42 additions & 0 deletions include/jsc/JavaScriptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define JAVASCRIPTMISC_H

#include <JavaScriptCore/JavaScript.h>
#include "rtHttpRequest.h"
#include "rtString.h"
#include "rtAtomic.h"
#include <rtError.h>
Expand All @@ -31,6 +32,10 @@
#include <functional>
#include <vector>
#include <thread>
#include <queue>
#include <chrono>
#include <cstdint>

namespace jsruntime
{
template<typename T>
Expand All @@ -51,6 +56,42 @@ class RefCounted: public T
virtual ~RefCounted() {}
};
}

class rtHttpRequestEx : public rtHttpRequest
{
public:
rtDeclareObject(rtHttpRequestEx, rtHttpRequest);
rtHttpRequestEx(const rtString& url);
rtHttpRequestEx(const rtObjectRef& options);
void onDownloadProgressImpl(double progress) final;
void onDownloadCompleteImpl(rtFileDownloadRequest* downloadRequest) final;
};

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);
bool updateForInfo(const TimeoutInfo* info);
};

void dispatchOnMainLoop(std::function<void ()>&& fun);
void dispatchPending();
void dispatchTimeouts();
Expand All @@ -73,6 +114,7 @@ rtError rtSetVideoStartTimeBinding(int numArgs, const rtValue* args, rtValue* re
rtError rtJSRuntimeDownloadMetrics(int numArgs, const rtValue* args, rtValue* result, void* context);
rtError rtSetExternalAppHandlerBinding(int numArgs, const rtValue* args, rtValue* result, void* context);
rtError rtGetRandomValuesBinding(int numArgs, const rtValue* args, rtValue* result, void* context);
rtError rtInstallTimeout(int numArgs, const rtValue* args, rtValue* result, bool repeat);
JSValueRef requireCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception);

#endif /* JAVASCRIPTMISC_H */
55 changes: 40 additions & 15 deletions src/NativeJSRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,32 @@ NativeJSRenderer::NativeJSRenderer(std::string waylandDisplay): mEngine(nullptr)

NativeJSRenderer::~NativeJSRenderer()
{
gPendingRequests.clear();
if (mEngine)
{
delete mEngine;
mEngine = nullptr;
mShutdownConsole = true;
if (mConsoleThread.joinable()) {
mConsoleThread.join();
}
gPendingRequests.clear();

if (mConsoleState && mConsoleState->consoleContext) {
delete mConsoleState->consoleContext;
mConsoleState->consoleContext = nullptr;
}
if (mEngine){
delete mEngine;
mEngine = nullptr;
}

}

void NativeJSRenderer::setEnvForConsoleMode(ModuleSettings& moduleSettings)
{

if (mConsoleThread.joinable()) {
mShutdownConsole = true;
mConsoleThread.join();
}

mShutdownConsole = false;
mConsoleState = std::make_unique<ConsoleState>();
mConsoleState->moduleSettings = moduleSettings;

Expand All @@ -205,8 +218,7 @@ void NativeJSRenderer::setEnvForConsoleMode(ModuleSettings& moduleSettings)
mConsoleState->consoleContext = context;

NativeJSLogger::log(INFO, "Running developer console...\n");
std::thread consoleThread(&JsRuntime::NativeJSRenderer::runDeveloperConsole, this, std::ref(mConsoleState->moduleSettings));
consoleThread.detach();
mConsoleThread = std::thread(&JsRuntime::NativeJSRenderer::runDeveloperConsole, this, std::ref(mConsoleState->moduleSettings));

mConsoleMode = true;
}
Expand Down Expand Up @@ -542,12 +554,10 @@ void NativeJSRenderer::processDevConsoleRequests()
mConsoleState->inputMutex.unlock();
}

namespace {
bool consoleLoop = true;
void handleDevConsoleSigInt(int /*sig*/){
consoleLoop = false;
}
} // namespace
std::atomic_bool NativeJSRenderer::consoleLoop = true;
void handleDevConsoleSigInt(int /*sig*/) {
NativeJSRenderer::consoleLoop = false;
}

void NativeJSRenderer::runDeveloperConsole(ModuleSettings moduleSettings)
{
Expand All @@ -557,7 +567,17 @@ void NativeJSRenderer::runDeveloperConsole(ModuleSettings moduleSettings)
NativeJSLogger::log(INFO, "Type 'exit' or press CTRL+C and ENTER to quit.\n");

signal(SIGINT, handleDevConsoleSigInt);
while (consoleLoop) {

#ifdef UNIT_TEST_BUILD
input = "exit";
NativeJSLogger::log(INFO, "[UNIT_TEST_BUILD] Auto-exiting developer console");
delete mConsoleState->consoleContext;
mConsoleState->consoleContext = nullptr;
signal(SIGINT, SIG_DFL);
return;
#else

while (consoleLoop && !mShutdownConsole) {
Comment thread
vjain008 marked this conversation as resolved.
// Don't display another input prompt until previous code is processed (prevents)
{
std::unique_lock<std::mutex> lk(mConsoleState->isProcessing_cv_m);
Expand All @@ -566,16 +586,20 @@ void NativeJSRenderer::runDeveloperConsole(ModuleSettings moduleSettings)
mConsoleState->isProcessing = true;
}

if (mShutdownConsole) break;
std::getline(std::cin, input);
if (mShutdownConsole) break;
Comment thread
vjain008 marked this conversation as resolved.

// Short-cirtuit: in case consoleLoop was altered by signal handler we shouldn't execute lines below
if (!consoleLoop || input == "exit") {
delete mConsoleState->consoleContext;

mConsoleState->consoleContext = nullptr;

#ifdef NATIVEJS_L2_BUILD
std::this_thread::sleep_for(std::chrono::milliseconds(200));
terminate();
#endif

break;
}

Expand All @@ -585,6 +609,7 @@ void NativeJSRenderer::runDeveloperConsole(ModuleSettings moduleSettings)
}

signal(SIGINT, SIG_DFL);
#endif
}

bool NativeJSRenderer::downloadFile(std::string& url, MemoryStruct& chunk)
Expand Down
62 changes: 18 additions & 44 deletions src/jsc/JavaScriptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,25 @@ static std::list<std::function<void ()>> gPendingFun;
static std::mutex gDispatchMutex;
static const char* envValue = std::getenv("NATIVEJS_DUMP_NETWORKMETRIC");

struct TimeoutInfo
void TimeoutQueue::pushTimeouts(const std::vector<TimeoutInfo*>& timerVec)
{
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)));
}
};
if (!timerVec.size())
return;
this->c.reserve(this->c.size() + timerVec.size());
this->c.insert(this->c.end(), timerVec.begin(), timerVec.end());
std::make_heap(this->c.begin(), this->c.end(), this->comp);
}

class TimeoutQueue : public std::priority_queue<TimeoutInfo*, std::vector<TimeoutInfo*>, TimeoutInfoComparator>
bool TimeoutQueue::updateForInfo(const TimeoutInfo* info)
{
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;
auto it = std::find(this->c.begin(), this->c.end(), info);
if (it != this->c.end()) {
std::make_heap(this->c.begin(), this->c.end(), this->comp);
return true;
}
return false;
}
};
}

static std::map<uint32_t, TimeoutInfo*> gTimeoutMap;
static uint32_t gTimeoutIdx = 0;
static TimeoutQueue gTimeoutQueue;
Expand Down Expand Up @@ -179,22 +159,17 @@ void assertIsMainThread()
assert(std::this_thread::get_id() == gMainThreadId);
}

class rtHttpRequestEx : public rtHttpRequest
{
public:
rtDeclareObject(rtHttpRequestEx, rtHttpRequest);

rtHttpRequestEx(const rtString& url)
rtHttpRequestEx::rtHttpRequestEx(const rtString& url)
: rtHttpRequest(url)
{
}

rtHttpRequestEx(const rtObjectRef& options)
rtHttpRequestEx::rtHttpRequestEx(const rtObjectRef& options)
: rtHttpRequest(options)
{
}

void onDownloadProgressImpl(double progress) final
void rtHttpRequestEx::onDownloadProgressImpl(double progress)
{

AddRef();
Expand All @@ -209,7 +184,7 @@ class rtHttpRequestEx : public rtHttpRequest
});
}

void onDownloadCompleteImpl(rtFileDownloadRequest* downloadRequest) final
void rtHttpRequestEx::onDownloadCompleteImpl(rtFileDownloadRequest* downloadRequest)
{
AddRef();
if (!downloadRequest->errorString().isEmpty()) {
Expand Down Expand Up @@ -275,7 +250,6 @@ class rtHttpRequestEx : public rtHttpRequest
});
}
}
};

rtDefineObject(rtHttpRequestEx, rtHttpRequest);

Expand Down
28 changes: 28 additions & 0 deletions src/linux/KeyInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,34 @@ std::map<uint32_t, std::vector<std::string>> keyMappings = {
{ WAYLAND_KEY_MUTE, { "AudioVolumeMute", "AudioVolumeMute", "AudioVolumeMute", "AudioVolumeMute" } },
{ WAYLAND_KEY_VOLUME_DOWN, { "AudioVolumeDown", "AudioVolumeDown", "AudioVolumeDown", "AudioVolumeDown" } },
{ WAYLAND_KEY_VOLUME_UP, { "AudioVolumeUp", "AudioVolumeUp", "AudioVolumeUp", "AudioVolumeUp" } }

#if defined(UNIT_TEST_BUILD)
#ifdef WAYLAND_KEY_PLAYPAUSE
, { WAYLAND_KEY_PLAYPAUSE, { "PlayPause", "PlayPause", "PlayPause", "PlayPause" } }
#endif
#ifdef WAYLAND_KEY_PLAY
, { WAYLAND_KEY_PLAY, { "Play", "Play", "Play", "Play" } }
#endif
#ifdef WAYLAND_KEY_FASTFORWARD
, { WAYLAND_KEY_FASTFORWARD, { "FastForward", "FastForward", "FastForward", "FastForward" } }
#endif
#ifdef WAYLAND_KEY_REWIND
, { WAYLAND_KEY_REWIND, { "Rewind", "Rewind", "Rewind", "Rewind" } }
#endif
#ifdef WAYLAND_KEY_KPENTER
, { WAYLAND_KEY_KPENTER, { "Enter", "Enter", "Enter", "Enter" } }
#endif
#ifdef WAYLAND_KEY_BACK
, { WAYLAND_KEY_BACK, { "Back", "Back", "Back", "Back" } }
#endif
#ifdef WAYLAND_KEY_MENU
, { WAYLAND_KEY_MENU, { "Menu", "Menu", "Menu", "Menu" } }
#endif
#ifdef WAYLAND_KEY_HOMEPAGE
, { WAYLAND_KEY_HOMEPAGE, { "Homepage", "Homepage", "Homepage", "Homepage" } }
#endif
#endif // UNIT_TEST_BUILD

};

static void getJavaScriptKeyCode(uint32_t waylandKeyCode, std::string& keyCode, uint32_t& keyCodeValue)
Expand Down