| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project 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 "src/inspector/V8ConsoleMessage.h" | 5 #include "src/inspector/V8ConsoleMessage.h" |
| 6 | 6 |
| 7 #include "src/inspector/InspectedContext.h" | 7 #include "src/inspector/InspectedContext.h" |
| 8 #include "src/inspector/StringUtil.h" | 8 #include "src/inspector/StringUtil.h" |
| 9 #include "src/inspector/V8ConsoleAgentImpl.h" | 9 #include "src/inspector/V8ConsoleAgentImpl.h" |
| 10 #include "src/inspector/V8InspectorImpl.h" | 10 #include "src/inspector/V8InspectorImpl.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 } | 56 } |
| 57 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log; | 57 return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log; |
| 58 } | 58 } |
| 59 | 59 |
| 60 const unsigned maxConsoleMessageCount = 1000; | 60 const unsigned maxConsoleMessageCount = 1000; |
| 61 const unsigned maxArrayItemsLimit = 10000; | 61 const unsigned maxArrayItemsLimit = 10000; |
| 62 const unsigned maxStackDepthLimit = 32; | 62 const unsigned maxStackDepthLimit = 32; |
| 63 | 63 |
| 64 class V8ValueStringBuilder { | 64 class V8ValueStringBuilder { |
| 65 public: | 65 public: |
| 66 static String16 toString(v8::Local<v8::Value> value, v8::Isolate* isolate) { | 66 static String16 toString(v8::Local<v8::Value> value, |
| 67 V8ValueStringBuilder builder(isolate); | 67 v8::Local<v8::Context> context) { |
| 68 V8ValueStringBuilder builder(context); |
| 68 if (!builder.append(value)) return String16(); | 69 if (!builder.append(value)) return String16(); |
| 69 return builder.toString(); | 70 return builder.toString(); |
| 70 } | 71 } |
| 71 | 72 |
| 72 private: | 73 private: |
| 73 enum { | 74 enum { |
| 74 IgnoreNull = 1 << 0, | 75 IgnoreNull = 1 << 0, |
| 75 IgnoreUndefined = 1 << 1, | 76 IgnoreUndefined = 1 << 1, |
| 76 }; | 77 }; |
| 77 | 78 |
| 78 V8ValueStringBuilder(v8::Isolate* isolate) | 79 V8ValueStringBuilder(v8::Local<v8::Context> context) |
| 79 : m_arrayLimit(maxArrayItemsLimit), | 80 : m_arrayLimit(maxArrayItemsLimit), |
| 80 m_isolate(isolate), | 81 m_isolate(context->GetIsolate()), |
| 81 m_tryCatch(isolate) {} | 82 m_tryCatch(context->GetIsolate()), |
| 83 m_context(context) {} |
| 82 | 84 |
| 83 bool append(v8::Local<v8::Value> value, unsigned ignoreOptions = 0) { | 85 bool append(v8::Local<v8::Value> value, unsigned ignoreOptions = 0) { |
| 84 if (value.IsEmpty()) return true; | 86 if (value.IsEmpty()) return true; |
| 85 if ((ignoreOptions & IgnoreNull) && value->IsNull()) return true; | 87 if ((ignoreOptions & IgnoreNull) && value->IsNull()) return true; |
| 86 if ((ignoreOptions & IgnoreUndefined) && value->IsUndefined()) return true; | 88 if ((ignoreOptions & IgnoreUndefined) && value->IsUndefined()) return true; |
| 87 if (value->IsString()) return append(v8::Local<v8::String>::Cast(value)); | 89 if (value->IsString()) return append(v8::Local<v8::String>::Cast(value)); |
| 88 if (value->IsStringObject()) | 90 if (value->IsStringObject()) |
| 89 return append(v8::Local<v8::StringObject>::Cast(value)->ValueOf()); | 91 return append(v8::Local<v8::StringObject>::Cast(value)->ValueOf()); |
| 90 if (value->IsSymbol()) return append(v8::Local<v8::Symbol>::Cast(value)); | 92 if (value->IsSymbol()) return append(v8::Local<v8::Symbol>::Cast(value)); |
| 91 if (value->IsSymbolObject()) | 93 if (value->IsSymbolObject()) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 } | 128 } |
| 127 uint32_t length = array->Length(); | 129 uint32_t length = array->Length(); |
| 128 if (length > m_arrayLimit) return false; | 130 if (length > m_arrayLimit) return false; |
| 129 if (m_visitedArrays.size() > maxStackDepthLimit) return false; | 131 if (m_visitedArrays.size() > maxStackDepthLimit) return false; |
| 130 | 132 |
| 131 bool result = true; | 133 bool result = true; |
| 132 m_arrayLimit -= length; | 134 m_arrayLimit -= length; |
| 133 m_visitedArrays.push_back(array); | 135 m_visitedArrays.push_back(array); |
| 134 for (uint32_t i = 0; i < length; ++i) { | 136 for (uint32_t i = 0; i < length; ++i) { |
| 135 if (i) m_builder.append(','); | 137 if (i) m_builder.append(','); |
| 136 if (!append(array->Get(i), IgnoreNull | IgnoreUndefined)) { | 138 v8::Local<v8::Value> value; |
| 139 if (!array->Get(m_context, i).ToLocal(&value)) continue; |
| 140 if (!append(value, IgnoreNull | IgnoreUndefined)) { |
| 137 result = false; | 141 result = false; |
| 138 break; | 142 break; |
| 139 } | 143 } |
| 140 } | 144 } |
| 141 m_visitedArrays.pop_back(); | 145 m_visitedArrays.pop_back(); |
| 142 return result; | 146 return result; |
| 143 } | 147 } |
| 144 | 148 |
| 145 bool append(v8::Local<v8::Symbol> symbol) { | 149 bool append(v8::Local<v8::Symbol> symbol) { |
| 146 m_builder.append("Symbol("); | 150 m_builder.append("Symbol("); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 158 String16 toString() { | 162 String16 toString() { |
| 159 if (m_tryCatch.HasCaught()) return String16(); | 163 if (m_tryCatch.HasCaught()) return String16(); |
| 160 return m_builder.toString(); | 164 return m_builder.toString(); |
| 161 } | 165 } |
| 162 | 166 |
| 163 uint32_t m_arrayLimit; | 167 uint32_t m_arrayLimit; |
| 164 v8::Isolate* m_isolate; | 168 v8::Isolate* m_isolate; |
| 165 String16Builder m_builder; | 169 String16Builder m_builder; |
| 166 std::vector<v8::Local<v8::Array>> m_visitedArrays; | 170 std::vector<v8::Local<v8::Array>> m_visitedArrays; |
| 167 v8::TryCatch m_tryCatch; | 171 v8::TryCatch m_tryCatch; |
| 172 v8::Local<v8::Context> m_context; |
| 168 }; | 173 }; |
| 169 | 174 |
| 170 } // namespace | 175 } // namespace |
| 171 | 176 |
| 172 V8ConsoleMessage::V8ConsoleMessage(V8MessageOrigin origin, double timestamp, | 177 V8ConsoleMessage::V8ConsoleMessage(V8MessageOrigin origin, double timestamp, |
| 173 const String16& message) | 178 const String16& message) |
| 174 : m_origin(origin), | 179 : m_origin(origin), |
| 175 m_timestamp(timestamp), | 180 m_timestamp(timestamp), |
| 176 m_message(message), | 181 m_message(message), |
| 177 m_lineNumber(0), | 182 m_lineNumber(0), |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 } | 334 } |
| 330 | 335 |
| 331 V8MessageOrigin V8ConsoleMessage::origin() const { return m_origin; } | 336 V8MessageOrigin V8ConsoleMessage::origin() const { return m_origin; } |
| 332 | 337 |
| 333 ConsoleAPIType V8ConsoleMessage::type() const { return m_type; } | 338 ConsoleAPIType V8ConsoleMessage::type() const { return m_type; } |
| 334 | 339 |
| 335 // static | 340 // static |
| 336 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI( | 341 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI( |
| 337 double timestamp, ConsoleAPIType type, | 342 double timestamp, ConsoleAPIType type, |
| 338 const std::vector<v8::Local<v8::Value>>& arguments, | 343 const std::vector<v8::Local<v8::Value>>& arguments, |
| 339 std::unique_ptr<V8StackTraceImpl> stackTrace, InspectedContext* context) { | 344 std::unique_ptr<V8StackTraceImpl> stackTrace, |
| 340 v8::Isolate* isolate = context->isolate(); | 345 InspectedContext* inspectedContext) { |
| 341 int contextId = context->contextId(); | 346 v8::Isolate* isolate = inspectedContext->isolate(); |
| 342 int contextGroupId = context->contextGroupId(); | 347 int contextId = inspectedContext->contextId(); |
| 343 V8InspectorImpl* inspector = context->inspector(); | 348 int contextGroupId = inspectedContext->contextGroupId(); |
| 349 V8InspectorImpl* inspector = inspectedContext->inspector(); |
| 350 v8::Local<v8::Context> context = inspectedContext->context(); |
| 344 | 351 |
| 345 std::unique_ptr<V8ConsoleMessage> message = wrapUnique( | 352 std::unique_ptr<V8ConsoleMessage> message = wrapUnique( |
| 346 new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16())); | 353 new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16())); |
| 347 if (stackTrace && !stackTrace->isEmpty()) { | 354 if (stackTrace && !stackTrace->isEmpty()) { |
| 348 message->m_url = toString16(stackTrace->topSourceURL()); | 355 message->m_url = toString16(stackTrace->topSourceURL()); |
| 349 message->m_lineNumber = stackTrace->topLineNumber(); | 356 message->m_lineNumber = stackTrace->topLineNumber(); |
| 350 message->m_columnNumber = stackTrace->topColumnNumber(); | 357 message->m_columnNumber = stackTrace->topColumnNumber(); |
| 351 } | 358 } |
| 352 message->m_stackTrace = std::move(stackTrace); | 359 message->m_stackTrace = std::move(stackTrace); |
| 353 message->m_type = type; | 360 message->m_type = type; |
| 354 message->m_contextId = contextId; | 361 message->m_contextId = contextId; |
| 355 for (size_t i = 0; i < arguments.size(); ++i) | 362 for (size_t i = 0; i < arguments.size(); ++i) |
| 356 message->m_arguments.push_back( | 363 message->m_arguments.push_back( |
| 357 wrapUnique(new v8::Global<v8::Value>(isolate, arguments.at(i)))); | 364 wrapUnique(new v8::Global<v8::Value>(isolate, arguments.at(i)))); |
| 358 if (arguments.size()) | 365 if (arguments.size()) |
| 359 message->m_message = V8ValueStringBuilder::toString(arguments[0], isolate); | 366 message->m_message = V8ValueStringBuilder::toString(arguments[0], context); |
| 360 | 367 |
| 361 V8ConsoleAPIType clientType = V8ConsoleAPIType::kLog; | 368 V8ConsoleAPIType clientType = V8ConsoleAPIType::kLog; |
| 362 if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount || | 369 if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount || |
| 363 type == ConsoleAPIType::kTimeEnd) | 370 type == ConsoleAPIType::kTimeEnd) |
| 364 clientType = V8ConsoleAPIType::kDebug; | 371 clientType = V8ConsoleAPIType::kDebug; |
| 365 else if (type == ConsoleAPIType::kError || type == ConsoleAPIType::kAssert) | 372 else if (type == ConsoleAPIType::kError || type == ConsoleAPIType::kAssert) |
| 366 clientType = V8ConsoleAPIType::kError; | 373 clientType = V8ConsoleAPIType::kError; |
| 367 else if (type == ConsoleAPIType::kWarning) | 374 else if (type == ConsoleAPIType::kWarning) |
| 368 clientType = V8ConsoleAPIType::kWarning; | 375 clientType = V8ConsoleAPIType::kWarning; |
| 369 else if (type == ConsoleAPIType::kInfo) | 376 else if (type == ConsoleAPIType::kInfo) |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 m_inspector->sessionForContextGroup(m_contextGroupId)) | 464 m_inspector->sessionForContextGroup(m_contextGroupId)) |
| 458 session->releaseObjectGroup("console"); | 465 session->releaseObjectGroup("console"); |
| 459 } | 466 } |
| 460 | 467 |
| 461 void V8ConsoleMessageStorage::contextDestroyed(int contextId) { | 468 void V8ConsoleMessageStorage::contextDestroyed(int contextId) { |
| 462 for (size_t i = 0; i < m_messages.size(); ++i) | 469 for (size_t i = 0; i < m_messages.size(); ++i) |
| 463 m_messages[i]->contextDestroyed(contextId); | 470 m_messages[i]->contextDestroyed(contextId); |
| 464 } | 471 } |
| 465 | 472 |
| 466 } // namespace v8_inspector | 473 } // namespace v8_inspector |
| OLD | NEW |