OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "bindings/core/v8/SourceLocation.h" | 5 #include "bindings/core/v8/SourceLocation.h" |
6 | 6 |
7 #include "bindings/core/v8/V8BindingMacros.h" | 7 #include "bindings/core/v8/V8BindingMacros.h" |
8 #include "bindings/core/v8/V8PerIsolateData.h" | 8 #include "bindings/core/v8/V8PerIsolateData.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/dom/ExecutionContext.h" | 10 #include "core/dom/ExecutionContext.h" |
11 #include "core/dom/ScriptableDocumentParser.h" | 11 #include "core/dom/ScriptableDocumentParser.h" |
12 #include "core/html/HTMLFrameOwnerElement.h" | 12 #include "core/html/HTMLFrameOwnerElement.h" |
13 #include "core/inspector/InspectorInstrumentation.h" | 13 #include "core/inspector/InspectorInstrumentation.h" |
14 #include "core/inspector/ThreadDebugger.h" | 14 #include "core/inspector/ThreadDebugger.h" |
15 #include "platform/ScriptForbiddenScope.h" | 15 #include "platform/ScriptForbiddenScope.h" |
16 #include "platform/TracedValue.h" | 16 #include "platform/TracedValue.h" |
17 #include "platform/v8_inspector/public/V8Debugger.h" | 17 #include "platform/v8_inspector/public/V8Debugger.h" |
| 18 #include "wtf/PtrUtil.h" |
| 19 #include <memory> |
18 | 20 |
19 namespace blink { | 21 namespace blink { |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 std::unique_ptr<V8StackTrace> captureStackTrace(bool full) | 25 std::unique_ptr<V8StackTrace> captureStackTrace(bool full) |
24 { | 26 { |
25 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 27 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
26 V8PerIsolateData* data = V8PerIsolateData::from(isolate); | 28 V8PerIsolateData* data = V8PerIsolateData::from(isolate); |
27 if (!data->threadDebugger() || !isolate->InContext()) | 29 if (!data->threadDebugger() || !isolate->InContext()) |
28 return nullptr; | 30 return nullptr; |
29 size_t stackSize = full ? V8StackTrace::maxCallStackSizeToCapture : 1; | 31 size_t stackSize = full ? V8StackTrace::maxCallStackSizeToCapture : 1; |
30 if (InspectorInstrumentation::hasFrontends()) { | 32 if (InspectorInstrumentation::hasFrontends()) { |
31 if (InspectorInstrumentation::consoleAgentEnabled(currentExecutionContex
t(isolate))) | 33 if (InspectorInstrumentation::consoleAgentEnabled(currentExecutionContex
t(isolate))) |
32 stackSize = V8StackTrace::maxCallStackSizeToCapture; | 34 stackSize = V8StackTrace::maxCallStackSizeToCapture; |
33 } | 35 } |
34 ScriptForbiddenScope::AllowUserAgentScript allowScripting; | 36 ScriptForbiddenScope::AllowUserAgentScript allowScripting; |
35 return data->threadDebugger()->debugger()->captureStackTrace(stackSize); | 37 return data->threadDebugger()->debugger()->captureStackTrace(stackSize); |
36 } | 38 } |
37 | 39 |
38 } | 40 } |
39 | 41 |
40 // static | 42 // static |
41 PassOwnPtr<SourceLocation> SourceLocation::capture(const String& url, unsigned l
ineNumber, unsigned columnNumber) | 43 std::unique_ptr<SourceLocation> SourceLocation::capture(const String& url, unsig
ned lineNumber, unsigned columnNumber) |
42 { | 44 { |
43 std::unique_ptr<V8StackTrace> stackTrace = captureStackTrace(false); | 45 std::unique_ptr<V8StackTrace> stackTrace = captureStackTrace(false); |
44 if (stackTrace && !stackTrace->isEmpty()) | 46 if (stackTrace && !stackTrace->isEmpty()) |
45 return SourceLocation::createFromNonEmptyV8StackTrace(std::move(stackTra
ce), 0); | 47 return SourceLocation::createFromNonEmptyV8StackTrace(std::move(stackTra
ce), 0); |
46 return SourceLocation::create(url, lineNumber, columnNumber, std::move(stack
Trace)); | 48 return SourceLocation::create(url, lineNumber, columnNumber, std::move(stack
Trace)); |
47 } | 49 } |
48 | 50 |
49 // static | 51 // static |
50 PassOwnPtr<SourceLocation> SourceLocation::capture(ExecutionContext* executionCo
ntext) | 52 std::unique_ptr<SourceLocation> SourceLocation::capture(ExecutionContext* execut
ionContext) |
51 { | 53 { |
52 std::unique_ptr<V8StackTrace> stackTrace = captureStackTrace(false); | 54 std::unique_ptr<V8StackTrace> stackTrace = captureStackTrace(false); |
53 if (stackTrace && !stackTrace->isEmpty()) | 55 if (stackTrace && !stackTrace->isEmpty()) |
54 return SourceLocation::createFromNonEmptyV8StackTrace(std::move(stackTra
ce), 0); | 56 return SourceLocation::createFromNonEmptyV8StackTrace(std::move(stackTra
ce), 0); |
55 | 57 |
56 Document* document = executionContext && executionContext->isDocument() ? to
Document(executionContext) : nullptr; | 58 Document* document = executionContext && executionContext->isDocument() ? to
Document(executionContext) : nullptr; |
57 if (document) { | 59 if (document) { |
58 unsigned lineNumber = 0; | 60 unsigned lineNumber = 0; |
59 if (document->scriptableDocumentParser() && !document->isInDocumentWrite
()) { | 61 if (document->scriptableDocumentParser() && !document->isInDocumentWrite
()) { |
60 if (document->scriptableDocumentParser()->isParsingAtLineNumber()) | 62 if (document->scriptableDocumentParser()->isParsingAtLineNumber()) |
61 lineNumber = document->scriptableDocumentParser()->lineNumber().
oneBasedInt(); | 63 lineNumber = document->scriptableDocumentParser()->lineNumber().
oneBasedInt(); |
62 } | 64 } |
63 return SourceLocation::create(document->url().getString(), lineNumber, 0
, std::move(stackTrace)); | 65 return SourceLocation::create(document->url().getString(), lineNumber, 0
, std::move(stackTrace)); |
64 } | 66 } |
65 | 67 |
66 return SourceLocation::create(executionContext ? executionContext->url().get
String() : String(), 0, 0, std::move(stackTrace)); | 68 return SourceLocation::create(executionContext ? executionContext->url().get
String() : String(), 0, 0, std::move(stackTrace)); |
67 } | 69 } |
68 | 70 |
69 // static | 71 // static |
70 PassOwnPtr<SourceLocation> SourceLocation::fromMessage(v8::Isolate* isolate, v8:
:Local<v8::Message> message, ExecutionContext* executionContext) | 72 std::unique_ptr<SourceLocation> SourceLocation::fromMessage(v8::Isolate* isolate
, v8::Local<v8::Message> message, ExecutionContext* executionContext) |
71 { | 73 { |
72 v8::Local<v8::StackTrace> stack = message->GetStackTrace(); | 74 v8::Local<v8::StackTrace> stack = message->GetStackTrace(); |
73 std::unique_ptr<V8StackTrace> stackTrace = nullptr; | 75 std::unique_ptr<V8StackTrace> stackTrace = nullptr; |
74 V8PerIsolateData* data = V8PerIsolateData::from(isolate); | 76 V8PerIsolateData* data = V8PerIsolateData::from(isolate); |
75 if (data && data->threadDebugger()) | 77 if (data && data->threadDebugger()) |
76 stackTrace = data->threadDebugger()->debugger()->createStackTrace(stack)
; | 78 stackTrace = data->threadDebugger()->debugger()->createStackTrace(stack)
; |
77 | 79 |
78 int scriptId = message->GetScriptOrigin().ScriptID()->Value(); | 80 int scriptId = message->GetScriptOrigin().ScriptID()->Value(); |
79 if (!stack.IsEmpty() && stack->GetFrameCount() > 0) { | 81 if (!stack.IsEmpty() && stack->GetFrameCount() > 0) { |
80 int topScriptId = stack->GetFrame(0)->GetScriptId(); | 82 int topScriptId = stack->GetFrame(0)->GetScriptId(); |
(...skipping 10 matching lines...) Expand all Loading... |
91 if ((!scriptId || !lineNumber) && stackTrace && !stackTrace->isEmpty()) | 93 if ((!scriptId || !lineNumber) && stackTrace && !stackTrace->isEmpty()) |
92 return SourceLocation::createFromNonEmptyV8StackTrace(std::move(stackTra
ce), 0); | 94 return SourceLocation::createFromNonEmptyV8StackTrace(std::move(stackTra
ce), 0); |
93 | 95 |
94 String url = toCoreStringWithUndefinedOrNullCheck(message->GetScriptOrigin()
.ResourceName()); | 96 String url = toCoreStringWithUndefinedOrNullCheck(message->GetScriptOrigin()
.ResourceName()); |
95 if (url.isNull()) | 97 if (url.isNull()) |
96 url = executionContext->url(); | 98 url = executionContext->url(); |
97 return SourceLocation::create(url, lineNumber, columnNumber, std::move(stack
Trace), scriptId); | 99 return SourceLocation::create(url, lineNumber, columnNumber, std::move(stack
Trace), scriptId); |
98 } | 100 } |
99 | 101 |
100 // static | 102 // static |
101 PassOwnPtr<SourceLocation> SourceLocation::create(const String& url, unsigned li
neNumber, unsigned columnNumber, std::unique_ptr<V8StackTrace> stackTrace, int s
criptId) | 103 std::unique_ptr<SourceLocation> SourceLocation::create(const String& url, unsign
ed lineNumber, unsigned columnNumber, std::unique_ptr<V8StackTrace> stackTrace,
int scriptId) |
102 { | 104 { |
103 return adoptPtr(new SourceLocation(url, lineNumber, columnNumber, std::move(
stackTrace), scriptId)); | 105 return wrapUnique(new SourceLocation(url, lineNumber, columnNumber, std::mov
e(stackTrace), scriptId)); |
104 } | 106 } |
105 | 107 |
106 // static | 108 // static |
107 PassOwnPtr<SourceLocation> SourceLocation::createFromNonEmptyV8StackTrace(std::u
nique_ptr<V8StackTrace> stackTrace, int scriptId) | 109 std::unique_ptr<SourceLocation> SourceLocation::createFromNonEmptyV8StackTrace(s
td::unique_ptr<V8StackTrace> stackTrace, int scriptId) |
108 { | 110 { |
109 // Retrieve the data before passing the ownership to SourceLocation. | 111 // Retrieve the data before passing the ownership to SourceLocation. |
110 const String& url = stackTrace->topSourceURL(); | 112 const String& url = stackTrace->topSourceURL(); |
111 unsigned lineNumber = stackTrace->topLineNumber(); | 113 unsigned lineNumber = stackTrace->topLineNumber(); |
112 unsigned columnNumber = stackTrace->topColumnNumber(); | 114 unsigned columnNumber = stackTrace->topColumnNumber(); |
113 return adoptPtr(new SourceLocation(url, lineNumber, columnNumber, std::move(
stackTrace), scriptId)); | 115 return wrapUnique(new SourceLocation(url, lineNumber, columnNumber, std::mov
e(stackTrace), scriptId)); |
114 } | 116 } |
115 | 117 |
116 // static | 118 // static |
117 PassOwnPtr<SourceLocation> SourceLocation::fromFunction(v8::Local<v8::Function>
function) | 119 std::unique_ptr<SourceLocation> SourceLocation::fromFunction(v8::Local<v8::Funct
ion> function) |
118 { | 120 { |
119 if (!function.IsEmpty()) | 121 if (!function.IsEmpty()) |
120 return SourceLocation::create(toCoreStringWithUndefinedOrNullCheck(funct
ion->GetScriptOrigin().ResourceName()), function->GetScriptLineNumber() + 1, fun
ction->GetScriptColumnNumber() + 1, nullptr, function->ScriptId()); | 122 return SourceLocation::create(toCoreStringWithUndefinedOrNullCheck(funct
ion->GetScriptOrigin().ResourceName()), function->GetScriptLineNumber() + 1, fun
ction->GetScriptColumnNumber() + 1, nullptr, function->ScriptId()); |
121 return SourceLocation::create(String(), 0, 0, nullptr, 0); | 123 return SourceLocation::create(String(), 0, 0, nullptr, 0); |
122 } | 124 } |
123 | 125 |
124 // static | 126 // static |
125 PassOwnPtr<SourceLocation> SourceLocation::captureWithFullStackTrace() | 127 std::unique_ptr<SourceLocation> SourceLocation::captureWithFullStackTrace() |
126 { | 128 { |
127 std::unique_ptr<V8StackTrace> stackTrace = captureStackTrace(true); | 129 std::unique_ptr<V8StackTrace> stackTrace = captureStackTrace(true); |
128 if (stackTrace && !stackTrace->isEmpty()) | 130 if (stackTrace && !stackTrace->isEmpty()) |
129 return SourceLocation::createFromNonEmptyV8StackTrace(std::move(stackTra
ce), 0); | 131 return SourceLocation::createFromNonEmptyV8StackTrace(std::move(stackTra
ce), 0); |
130 return SourceLocation::create(String(), 0, 0, nullptr, 0); | 132 return SourceLocation::create(String(), 0, 0, nullptr, 0); |
131 } | 133 } |
132 | 134 |
133 SourceLocation::SourceLocation(const String& url, unsigned lineNumber, unsigned
columnNumber, std::unique_ptr<V8StackTrace> stackTrace, int scriptId) | 135 SourceLocation::SourceLocation(const String& url, unsigned lineNumber, unsigned
columnNumber, std::unique_ptr<V8StackTrace> stackTrace, int scriptId) |
134 : m_url(url) | 136 : m_url(url) |
135 , m_lineNumber(lineNumber) | 137 , m_lineNumber(lineNumber) |
(...skipping 15 matching lines...) Expand all Loading... |
151 value->beginDictionary(); | 153 value->beginDictionary(); |
152 value->setString("functionName", m_stackTrace->topFunctionName()); | 154 value->setString("functionName", m_stackTrace->topFunctionName()); |
153 value->setString("scriptId", m_stackTrace->topScriptId()); | 155 value->setString("scriptId", m_stackTrace->topScriptId()); |
154 value->setString("url", m_stackTrace->topSourceURL()); | 156 value->setString("url", m_stackTrace->topSourceURL()); |
155 value->setInteger("lineNumber", m_stackTrace->topLineNumber()); | 157 value->setInteger("lineNumber", m_stackTrace->topLineNumber()); |
156 value->setInteger("columnNumber", m_stackTrace->topColumnNumber()); | 158 value->setInteger("columnNumber", m_stackTrace->topColumnNumber()); |
157 value->endDictionary(); | 159 value->endDictionary(); |
158 value->endArray(); | 160 value->endArray(); |
159 } | 161 } |
160 | 162 |
161 PassOwnPtr<SourceLocation> SourceLocation::clone() const | 163 std::unique_ptr<SourceLocation> SourceLocation::clone() const |
162 { | 164 { |
163 return adoptPtr(new SourceLocation(m_url, m_lineNumber, m_columnNumber, m_st
ackTrace ? m_stackTrace->clone() : nullptr, m_scriptId)); | 165 return wrapUnique(new SourceLocation(m_url, m_lineNumber, m_columnNumber, m_
stackTrace ? m_stackTrace->clone() : nullptr, m_scriptId)); |
164 } | 166 } |
165 | 167 |
166 PassOwnPtr<SourceLocation> SourceLocation::isolatedCopy() const | 168 std::unique_ptr<SourceLocation> SourceLocation::isolatedCopy() const |
167 { | 169 { |
168 return adoptPtr(new SourceLocation(m_url.isolatedCopy(), m_lineNumber, m_col
umnNumber, m_stackTrace ? m_stackTrace->isolatedCopy() : nullptr, m_scriptId)); | 170 return wrapUnique(new SourceLocation(m_url.isolatedCopy(), m_lineNumber, m_c
olumnNumber, m_stackTrace ? m_stackTrace->isolatedCopy() : nullptr, m_scriptId))
; |
169 } | 171 } |
170 | 172 |
171 std::unique_ptr<protocol::Runtime::StackTrace> SourceLocation::buildInspectorObj
ect() const | 173 std::unique_ptr<protocol::Runtime::StackTrace> SourceLocation::buildInspectorObj
ect() const |
172 { | 174 { |
173 return m_stackTrace ? m_stackTrace->buildInspectorObject() : nullptr; | 175 return m_stackTrace ? m_stackTrace->buildInspectorObject() : nullptr; |
174 } | 176 } |
175 | 177 |
176 String SourceLocation::toString() const | 178 String SourceLocation::toString() const |
177 { | 179 { |
178 if (!m_stackTrace) | 180 if (!m_stackTrace) |
179 return String(); | 181 return String(); |
180 return m_stackTrace->toString(); | 182 return m_stackTrace->toString(); |
181 } | 183 } |
182 | 184 |
183 } // namespace blink | 185 } // namespace blink |
OLD | NEW |