Skip to content

Commit 6d6547c

Browse files
committed
extract common inspector utils method
1 parent ccf2f54 commit 6d6547c

File tree

5 files changed

+110
-50
lines changed

5 files changed

+110
-50
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Created by pkanev on 6/5/2017.
3+
//
4+
5+
#include <v8_inspector/src/inspector/utils/v8-inspector-common.h>
6+
#include <ArgConverter.h>
7+
8+
using tns::ArgConverter;
9+
10+
namespace v8_inspector {
11+
namespace utils {
12+
v8::Local<v8::Object> Common::getGlobalInspectorObject(v8::Isolate *isolate) {
13+
auto context = isolate->GetCurrentContext();
14+
auto global = context->Global();
15+
16+
auto inspectorObjectString = "__inspector";
17+
18+
v8::Local<v8::Value> outInspector;
19+
20+
auto maybeInspectorObj = global->Get(context, ArgConverter::ConvertToV8String(isolate, inspectorObjectString));
21+
22+
if (maybeInspectorObj.ToLocal(&outInspector)) {
23+
return outInspector->ToObject();
24+
}
25+
26+
return v8::Local<v8::Object>();
27+
}
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Created by pkanev on 6/5/2017.
3+
//
4+
5+
#ifndef V8_INSPECTOR_COMMON_H
6+
#define V8_INSPECTOR_COMMON_H
7+
8+
#include <include/v8.h>
9+
10+
namespace v8_inspector {
11+
namespace utils {
12+
class Common {
13+
public:
14+
static v8::Local<v8::Object> getGlobalInspectorObject(v8::Isolate *isolate);
15+
};
16+
}
17+
}
18+
19+
#endif //V8_INSPECTOR_COMMON_H

runtime/src/main/jni/v8_inspector/src/inspector/v8-css-agent-impl.cpp

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <NativeScriptAssert.h>
66
#include <ArgConverter.h>
7+
#include <v8_inspector/src/inspector/utils/v8-inspector-common.h>
78
#include "v8-css-agent-impl.h"
89

910
namespace v8_inspector {
@@ -65,7 +66,6 @@ namespace v8_inspector {
6566
.build();
6667

6768
//// out_attributesStyle
68-
6969
auto attrArr = protocol::Array<protocol::CSS::CSSProperty>::create();
7070
auto attributeStyle = protocol::CSS::CSSStyle::create()
7171
.setCssProperties(std::move(attrArr))
@@ -144,37 +144,41 @@ namespace v8_inspector {
144144
std::unique_ptr<protocol::Array<protocol::CSS::CSSComputedStyleProperty>> *out_computedStyle) {
145145
auto computedStylePropertyArr = protocol::Array<protocol::CSS::CSSComputedStyleProperty>::create();
146146

147-
auto getComputedStylesForNodeString = "__getComputedStylesForNode";
147+
auto getComputedStylesForNodeString = "getComputedStylesForNode";
148148
// TODO: Pete: Find a better way to get a hold of the isolate
149149
auto isolate = v8::Isolate::GetCurrent();
150150
auto context = isolate->GetCurrentContext();
151151
auto global = context->Global();
152-
auto getComputedStylesForNode = global->Get(ArgConverter::ConvertToV8String(isolate, getComputedStylesForNodeString));
153152

154-
if (!getComputedStylesForNode.IsEmpty() && getComputedStylesForNode->IsFunction()) {
155-
auto getComputedStylesForNodeFunc = getComputedStylesForNode.As<v8::Function>();
156-
v8::Local<v8::Value> args[] = { v8::Number::New(isolate, in_nodeId) };
157-
auto maybeResult = getComputedStylesForNodeFunc->Call(context, global, 1, args);
158-
v8::Local<v8::Value> outResult;
153+
auto globalInspectorObject = utils::Common::getGlobalInspectorObject(isolate);
154+
155+
if (!globalInspectorObject.IsEmpty()) {
156+
auto getComputedStylesForNode = globalInspectorObject->Get(ArgConverter::ConvertToV8String(isolate, getComputedStylesForNodeString));
159157

160-
maybeResult.ToLocal(&outResult);
158+
if (!getComputedStylesForNode.IsEmpty() && getComputedStylesForNode->IsFunction()) {
159+
auto getComputedStylesForNodeFunc = getComputedStylesForNode.As<v8::Function>();
160+
v8::Local<v8::Value> args[] = {v8::Number::New(isolate, in_nodeId)};
161+
auto maybeResult = getComputedStylesForNodeFunc->Call(context, global, 1, args);
162+
v8::Local<v8::Value> outResult;
161163

162-
if (!outResult.IsEmpty()) {
163-
auto resultString = ArgConverter::ConvertToString(outResult->ToString());
164-
auto resultCStr = resultString.c_str();
165-
auto resultJson = protocol::parseJSON(resultCStr);
164+
if (maybeResult.ToLocal(&outResult)) {
165+
auto resultString = ArgConverter::ConvertToString(outResult->ToString());
166+
auto resultCStr = resultString.c_str();
167+
auto resultJson = protocol::parseJSON(resultCStr);
166168

167-
protocol::ErrorSupport errorSupport;
168-
auto computedStyles = protocol::Array<protocol::CSS::CSSComputedStyleProperty>::parse(resultJson.get(), &errorSupport);
169+
protocol::ErrorSupport errorSupport;
170+
auto computedStyles = protocol::Array<protocol::CSS::CSSComputedStyleProperty>::parse(
171+
resultJson.get(), &errorSupport);
169172

170-
auto errorSupportString = errorSupport.errors().utf8();
171-
if (!errorSupportString.empty()) {
172-
auto errorMessage = "Error while parsing CSSComputedStyleProperty object. ";
173-
DEBUG_WRITE_FORCE("%s Error: %s", errorMessage, errorSupportString.c_str());
174-
} else {
175-
*out_computedStyle = std::move(computedStyles);
173+
auto errorSupportString = errorSupport.errors().utf8();
174+
if (!errorSupportString.empty()) {
175+
auto errorMessage = "Error while parsing CSSComputedStyleProperty object. ";
176+
DEBUG_WRITE_FORCE("%s Error: %s", errorMessage, errorSupportString.c_str());
177+
} else {
178+
*out_computedStyle = std::move(computedStyles);
176179

177-
return;
180+
return;
181+
}
178182
}
179183
}
180184
}
@@ -185,7 +189,12 @@ namespace v8_inspector {
185189
void V8CSSAgentImpl::getPlatformFontsForNode(ErrorString *, int in_nodeId,
186190
std::unique_ptr<protocol::Array<protocol::CSS::PlatformFontUsage>> *out_fonts) {
187191
auto fontsArr = protocol::Array<protocol::CSS::PlatformFontUsage>::create();
188-
fontsArr->addItem(std::move(protocol::CSS::PlatformFontUsage::create().setFamilyName("System Font").setGlyphCount(1).setIsCustomFont(false).build()));
192+
auto defaultFont = "System Font";
193+
fontsArr->addItem(std::move(protocol::CSS::PlatformFontUsage::create()
194+
.setFamilyName(defaultFont)
195+
.setGlyphCount(1)
196+
.setIsCustomFont(false)
197+
.build()));
189198
*out_fonts = std::move(fontsArr);
190199
}
191200

runtime/src/main/jni/v8_inspector/src/inspector/v8-dom-agent-impl.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "v8-dom-agent-impl.h"
77
#include <ArgConverter.h>
88
#include <Runtime.h>
9+
#include <v8_inspector/src/inspector/utils/v8-inspector-common.h>
910

1011
namespace v8_inspector {
1112

@@ -57,44 +58,46 @@ namespace v8_inspector {
5758
.setNodeValue("")
5859
.build();
5960

60-
auto getDocumentFunctionString = "__getDocument";
61+
auto getDocumentFunctionString = "getDocument";
6162
// TODO: Pete: Find a better way to get a hold of the isolate
6263
auto isolate = v8::Isolate::GetCurrent();
6364
auto context = isolate->GetCurrentContext();
6465
auto global = context->Global();
65-
auto getDocument = global->Get(ArgConverter::ConvertToV8String(isolate, getDocumentFunctionString));
6666

67-
if (!getDocument.IsEmpty() && getDocument->IsFunction()) {
68-
auto getDocumentFunc = getDocument.As<v8::Function>();
69-
v8::Local<v8::Value> args[] = { };
70-
auto maybeResult = getDocumentFunc->Call(context, global, 0, args);
71-
v8::Local<v8::Value> outResult;
67+
auto globalInspectorObject = utils::Common::getGlobalInspectorObject(isolate);
7268

73-
maybeResult.ToLocal(&outResult);
69+
if (!globalInspectorObject.IsEmpty()) {
70+
auto getDocument = globalInspectorObject->Get(ArgConverter::ConvertToV8String(isolate, getDocumentFunctionString));
7471

75-
if (!outResult.IsEmpty()) {
76-
auto resultString = ArgConverter::ConvertToString(outResult->ToString());
77-
auto resultCStr = resultString.c_str();
78-
auto resultJson = protocol::parseJSON(resultCStr);
72+
if (!getDocument.IsEmpty() && getDocument->IsFunction()) {
73+
auto getDocumentFunc = getDocument.As<v8::Function>();
74+
v8::Local<v8::Value> args[] = { };
75+
auto maybeResult = getDocumentFunc->Call(context, global, 0, args);
76+
v8::Local<v8::Value> outResult;
7977

80-
protocol::ErrorSupport errorSupport;
81-
auto domNode = protocol::DOM::Node::parse(resultJson.get(), &errorSupport);
78+
if (maybeResult.ToLocal(&outResult)) {
79+
auto resultString = ArgConverter::ConvertToString(outResult->ToString());
80+
auto resultCStr = resultString.c_str();
81+
auto resultJson = protocol::parseJSON(resultCStr);
8282

83-
auto errorSupportString = errorSupport.errors().utf8();
84-
*errorString = errorSupportString.c_str();
85-
if (!errorSupportString.empty()) {
86-
auto errorMessage = "Error while parsing debug `DOM Node` object. ";
87-
DEBUG_WRITE_FORCE("%s Error: %s", errorMessage, errorSupportString.c_str());
88-
} else {
89-
*out_root = std::move(domNode);
83+
protocol::ErrorSupport errorSupport;
84+
auto domNode = protocol::DOM::Node::parse(resultJson.get(), &errorSupport);
85+
86+
auto errorSupportString = errorSupport.errors().utf8();
87+
*errorString = errorSupportString.c_str();
88+
if (!errorSupportString.empty()) {
89+
auto errorMessage = "Error while parsing debug `DOM Node` object. ";
90+
DEBUG_WRITE_FORCE("%s Error: %s", errorMessage, errorSupportString.c_str());
91+
} else {
92+
*out_root = std::move(domNode);
9093

91-
return;
94+
return;
95+
}
96+
} else {
97+
*errorString = "Didn't get a proper result from __getDocument call. Returning empty visual tree.";
9298
}
93-
} else {
94-
*errorString = "Didn't get a proper result from __getDocument call. Returning empty visual tree.";
9599
}
96100
}
97-
98101
*out_root = std::move(defaultNode);
99102
}
100103

@@ -133,8 +136,7 @@ namespace v8_inspector {
133136

134137
}
135138

136-
void
137-
V8DOMAgentImpl::getSearchResults(ErrorString *, const String &in_searchId, int in_fromIndex,
139+
void V8DOMAgentImpl::getSearchResults(ErrorString *, const String &in_searchId, int in_fromIndex,
138140
int in_toIndex,
139141
std::unique_ptr<protocol::Array<int>> *out_nodeIds) {
140142

runtime/src/main/jni/v8_inspector/src/inspector/v8-dom-agent-impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace v8_inspector {
4747
private:
4848
V8InspectorSessionImpl* m_session;
4949
protocol::DictionaryValue* m_state;
50+
5051
bool m_enabled;
5152

5253
DISALLOW_COPY_AND_ASSIGN(V8DOMAgentImpl);

0 commit comments

Comments
 (0)