2
2
// Created by pkanev on 5/10/2017.
3
3
//
4
4
5
+ #include < sstream>
5
6
#include < ArgConverter.h>
6
7
#include < NativeScriptAssert.h>
7
8
#include " DOMDomainCallbackHandlers.h"
@@ -19,65 +20,83 @@ void DOMDomainCallbackHandlers::DocumentUpdatedCallback(const v8::FunctionCallba
19
20
}
20
21
21
22
void DOMDomainCallbackHandlers::ChildNodeInsertedCallback (const v8::FunctionCallbackInfo<v8::Value> &args) {
22
- auto domAgentInstance = V8DOMAgentImpl::Instance;
23
-
24
- if (!domAgentInstance) {
25
- return ;
26
- }
27
-
28
- if (args.Length () == 0 ) {
29
- return ;
30
- }
31
-
32
- auto isolate = args.GetIsolate ();
33
-
34
- v8::HandleScope scope (isolate);
35
-
36
- auto context = isolate->GetCurrentContext ();
37
-
38
- // TODO: Pete: Validate!
39
- auto parentId = args[0 ]->ToNumber (isolate);
40
- auto lastId = args[1 ]->ToNumber (isolate);
41
- auto node = args[2 ]->ToString (isolate);
42
-
43
- auto nodeString = ArgConverter::ConvertToString (node);
44
- auto nodeCStr = nodeString.c_str ();
45
- auto nodeJson = protocol::parseJSON (nodeCStr);
46
-
47
- protocol::ErrorSupport errorSupport;
48
- auto domNode = protocol::DOM::Node::parse (nodeJson.get (), &errorSupport);
49
-
50
- auto errorSupportString = errorSupport.errors ().utf8 ();
51
- if (!errorSupportString.empty ()) {
52
- auto errorMessage = " Error while parsing debug `DOM Node` object. " ;
53
- DEBUG_WRITE_FORCE (" %s Error: %s" , errorMessage, errorSupportString.c_str ());
23
+ try {
24
+ auto domAgentInstance = V8DOMAgentImpl::Instance;
25
+
26
+ if (!domAgentInstance) {
27
+ return ;
28
+ }
29
+
30
+ auto isolate = args.GetIsolate ();
31
+
32
+ v8::HandleScope scope (isolate);
33
+
34
+ if (args.Length () != 3 || !(args[0 ]->IsNumber () && args[1 ]->IsNumber () && args[2 ]->IsString ())) {
35
+ throw NativeScriptException (" Calling ChildNodeInserted with invalid arguments. Required params: parentId: number, lastId: number, node: JSON String" );
36
+ }
37
+
38
+ auto parentId = args[0 ]->ToNumber (isolate);
39
+ auto lastId = args[1 ]->ToNumber (isolate);
40
+ auto node = args[2 ]->ToString (isolate);
41
+
42
+ auto nodeString = ArgConverter::ConvertToString (node);
43
+ auto nodeCStr = nodeString.c_str ();
44
+ auto nodeJson = protocol::parseJSON (nodeCStr);
45
+
46
+ protocol::ErrorSupport errorSupport;
47
+ auto domNode = protocol::DOM::Node::parse (nodeJson.get (), &errorSupport);
48
+
49
+ auto errorSupportString = errorSupport.errors ().utf8 ();
50
+ if (!errorSupportString.empty ()) {
51
+ auto errorMessage = " Error while parsing debug `DOM Node` object. " ;
52
+ DEBUG_WRITE_FORCE (" %s Error: %s" , errorMessage, errorSupportString.c_str ());
53
+ }
54
+
55
+ domAgentInstance->m_frontend .childNodeInserted (parentId->Int32Value (), lastId->Int32Value (), std::move (domNode));
56
+ } catch (NativeScriptException& e) {
57
+ e.ReThrowToV8 ();
58
+ } catch (std::exception e) {
59
+ std::stringstream ss;
60
+ ss << " Error: c exception: " << e.what () << std::endl;
61
+ NativeScriptException nsEx (ss.str ());
62
+ nsEx.ReThrowToV8 ();
63
+ } catch (...) {
64
+ NativeScriptException nsEx (std::string (" Error: c exception!" ));
65
+ nsEx.ReThrowToV8 ();
54
66
}
55
-
56
- domAgentInstance->m_frontend .childNodeInserted (parentId->Int32Value (), lastId->Int32Value (), std::move (domNode));
57
67
}
58
68
59
69
void DOMDomainCallbackHandlers::ChildNodeRemovedCallback (const v8::FunctionCallbackInfo<v8::Value> &args) {
60
- auto domAgentInstance = V8DOMAgentImpl::Instance;
61
-
62
- if (!domAgentInstance) {
63
- return ;
64
- }
65
-
66
- if (args.Length () == 0 ) {
67
- return ;
70
+ try {
71
+ auto domAgentInstance = V8DOMAgentImpl::Instance;
72
+
73
+ if (!domAgentInstance) {
74
+ return ;
75
+ }
76
+
77
+ auto isolate = args.GetIsolate ();
78
+
79
+ v8::HandleScope scope (isolate);
80
+
81
+ if (args.Length () != 2 || !(args[0 ]->IsNumber () && args[1 ]->IsNumber ())) {
82
+ throw NativeScriptException (" Calling ChildNodeRemoved with invalid arguments. Required params: parentId: number, nodeId: number" );
83
+ }
84
+
85
+ auto parentId = args[0 ]->ToNumber (isolate);
86
+ auto nodeId = args[1 ]->ToNumber (isolate);
87
+
88
+ domAgentInstance->m_frontend .childNodeRemoved (parentId->Int32Value (), nodeId->Int32Value ());
89
+ } catch (NativeScriptException& e) {
90
+ e.ReThrowToV8 ();
91
+ } catch (std::exception e) {
92
+ std::stringstream ss;
93
+ ss << " Error: c exception: " << e.what () << std::endl;
94
+ NativeScriptException nsEx (ss.str ());
95
+ nsEx.ReThrowToV8 ();
96
+ } catch (...) {
97
+ NativeScriptException nsEx (std::string (" Error: c exception!" ));
98
+ nsEx.ReThrowToV8 ();
68
99
}
69
-
70
- auto isolate = args.GetIsolate ();
71
-
72
- v8::HandleScope scope (isolate);
73
-
74
- auto context = isolate->GetCurrentContext ();
75
-
76
- // TODO: Pete: Validate!
77
- auto parentId = args[0 ]->ToNumber (isolate);
78
- auto nodeId = args[1 ]->ToNumber (isolate);
79
-
80
- domAgentInstance->m_frontend .childNodeRemoved (parentId->Int32Value (), nodeId->Int32Value ());
81
100
}
82
101
83
102
void DOMDomainCallbackHandlers::AttributeModifiedCallback (const v8::FunctionCallbackInfo<v8::Value> &args) {
0 commit comments