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
169 changes: 169 additions & 0 deletions runtime/src/main/jni/DOMDomainCallbackHandlers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
//
// Created by pkanev on 5/10/2017.
//

#include <sstream>
#include <ArgConverter.h>
#include <NativeScriptAssert.h>
#include "DOMDomainCallbackHandlers.h"

using namespace tns;

void DOMDomainCallbackHandlers::DocumentUpdatedCallback(const v8::FunctionCallbackInfo<v8::Value> &args) {
auto domAgentInstance = V8DOMAgentImpl::Instance;

if (!domAgentInstance) {
return;
}

domAgentInstance->m_frontend.documentUpdated();
}

void DOMDomainCallbackHandlers::ChildNodeInsertedCallback(const v8::FunctionCallbackInfo<v8::Value> &args) {
try {
auto domAgentInstance = V8DOMAgentImpl::Instance;

if (!domAgentInstance) {
return;
}

auto isolate = args.GetIsolate();

v8::HandleScope scope(isolate);

if (args.Length() != 3 || !(args[0]->IsNumber() && args[1]->IsNumber() && args[2]->IsString())) {
throw NativeScriptException("Calling ChildNodeInserted with invalid arguments. Required params: parentId: number, lastId: number, node: JSON String");
}

auto parentId = args[0]->ToNumber(isolate);
auto lastId = args[1]->ToNumber(isolate);
auto node = args[2]->ToString(isolate);

auto nodeString = ArgConverter::ConvertToString(node);
auto nodeCStr = nodeString.c_str();
auto nodeJson = protocol::parseJSON(nodeCStr);

protocol::ErrorSupport errorSupport;
auto domNode = protocol::DOM::Node::parse(nodeJson.get(), &errorSupport);

auto errorSupportString = errorSupport.errors().utf8();
if (!errorSupportString.empty()) {
auto errorMessage = "Error while parsing debug `DOM Node` object. ";
DEBUG_WRITE_FORCE("%s Error: %s", errorMessage, errorSupportString.c_str());
}

domAgentInstance->m_frontend.childNodeInserted(parentId->Int32Value(), lastId->Int32Value(), std::move(domNode));
} catch (NativeScriptException& e) {
e.ReThrowToV8();
} catch (std::exception e) {
std::stringstream ss;
ss << "Error: c exception: " << e.what() << std::endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
} catch (...) {
NativeScriptException nsEx(std::string("Error: c exception!"));
nsEx.ReThrowToV8();
}
}

void DOMDomainCallbackHandlers::ChildNodeRemovedCallback(const v8::FunctionCallbackInfo<v8::Value> &args) {
try {
auto domAgentInstance = V8DOMAgentImpl::Instance;

if (!domAgentInstance) {
return;
}

auto isolate = args.GetIsolate();

v8::HandleScope scope(isolate);

if (args.Length() != 2 || !(args[0]->IsNumber() && args[1]->IsNumber())) {
throw NativeScriptException("Calling ChildNodeRemoved with invalid arguments. Required params: parentId: number, nodeId: number");
}

auto parentId = args[0]->ToNumber(isolate);
auto nodeId = args[1]->ToNumber(isolate);

domAgentInstance->m_frontend.childNodeRemoved(parentId->Int32Value(), nodeId->Int32Value());
} catch (NativeScriptException& e) {
e.ReThrowToV8();
} catch (std::exception e) {
std::stringstream ss;
ss << "Error: c exception: " << e.what() << std::endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
} catch (...) {
NativeScriptException nsEx(std::string("Error: c exception!"));
nsEx.ReThrowToV8();
}
}

void DOMDomainCallbackHandlers::AttributeModifiedCallback(const v8::FunctionCallbackInfo<v8::Value> &args) {
try {
auto domAgentInstance = V8DOMAgentImpl::Instance;

if (!domAgentInstance) {
return;
}

auto isolate = args.GetIsolate();

v8::HandleScope scope(isolate);

if (args.Length() != 3 || !(args[0]->IsNumber() && args[1]->IsString() && args[2]->IsString())) {
throw NativeScriptException("Calling AttributeModified with invalid arguments. Required params: nodeId: number, name: string, value: string");
}

auto nodeId = args[0]->ToNumber(isolate);
auto attributeName = args[1]->ToString();
auto attributeValue = args[2]->ToString();

domAgentInstance->m_frontend.attributeModified(nodeId->Int32Value(),
ArgConverter::ConvertToString(attributeName).c_str(),
ArgConverter::ConvertToString(attributeValue).c_str());
} catch (NativeScriptException& e) {
e.ReThrowToV8();
} catch (std::exception e) {
std::stringstream ss;
ss << "Error: c exception: " << e.what() << std::endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
} catch (...) {
NativeScriptException nsEx(std::string("Error: c exception!"));
nsEx.ReThrowToV8();
}
}

void DOMDomainCallbackHandlers::AttributeRemovedCallback(const v8::FunctionCallbackInfo<v8::Value> &args) {
try {
auto domAgentInstance = V8DOMAgentImpl::Instance;

if (!domAgentInstance) {
return;
}
auto isolate = args.GetIsolate();

v8::HandleScope scope(isolate);

if (args.Length() != 2 || !(args[0]->IsNumber() && args[1]->IsString())) {
throw NativeScriptException("Calling AttributeRemoved with invalid arguments. Required params: nodeId: number, name: string");
}

auto nodeId = args[0]->ToNumber(isolate);
auto attributeName = args[1]->ToString();

domAgentInstance->m_frontend.attributeRemoved(nodeId->Int32Value(),
ArgConverter::ConvertToString(attributeName).c_str());
} catch (NativeScriptException& e) {
e.ReThrowToV8();
} catch (std::exception e) {
std::stringstream ss;
ss << "Error: c exception: " << e.what() << std::endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
} catch (...) {
NativeScriptException nsEx(std::string("Error: c exception!"));
nsEx.ReThrowToV8();
}
}
26 changes: 26 additions & 0 deletions runtime/src/main/jni/DOMDomainCallbackHandlers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by pkanev on 5/10/2017.
//

#ifndef DOMDOMAINCALLBACKHANDLERS_H
#define DOMDOMAINCALLBACKHANDLERS_H

#include <include/v8.h>
#include <v8_inspector/src/inspector/v8-dom-agent-impl.h>
#include "JsV8InspectorClient.h"
#include "NativeScriptException.h"

namespace tns {
class DOMDomainCallbackHandlers {

public:
static void DocumentUpdatedCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ChildNodeInsertedCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ChildNodeRemovedCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
static void AttributeModifiedCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
static void AttributeRemovedCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
};
}


#endif //DOMDOMAINCALLBACKHANDLERS_H
7 changes: 7 additions & 0 deletions runtime/src/main/jni/JsV8InspectorClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "NativeScriptException.h"

#include "ArgConverter.h"
#include "DOMDomainCallbackHandlers.h"
#include "NetworkDomainCallbackHandlers.h"

using namespace std;
Expand Down Expand Up @@ -272,6 +273,12 @@ void JsV8InspectorClient::attachInspectorCallbacks(Isolate* isolate,
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "loadingFinished"), FunctionTemplate::New(isolate, NetworkDomainCallbackHandlers::LoadingFinishedCallback));
inspectorJSObject->SetAccessor(ArgConverter::ConvertToV8String(isolate, "isConnected"), JsV8InspectorClient::InspectorIsConnectedGetterCallback);

inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "documentUpdated"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::DocumentUpdatedCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "childNodeInserted"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::ChildNodeInsertedCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "childNodeRemoved"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::ChildNodeRemovedCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "attributeModified"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::AttributeModifiedCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "attributeRemoved"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::AttributeRemovedCallback));

globalObjectTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__inspector"), inspectorJSObject);
}

Expand Down
Loading