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
3// found in the LICENSE file.
4
5#ifndef V8_INSPECTOR_V8CONSOLEMESSAGE_H_
6#define V8_INSPECTOR_V8CONSOLEMESSAGE_H_
7
8#include <deque>
9#include "include/v8.h"
10#include "src/inspector/protocol/Console.h"
11#include "src/inspector/protocol/Forward.h"
12#include "src/inspector/protocol/Runtime.h"
13
14namespace v8_inspector {
15
16class InspectedContext;
17class V8InspectorImpl;
18class V8InspectorSessionImpl;
19class V8StackTraceImpl;
20
21enum class V8MessageOrigin { kConsole, kException, kRevokedException };
22
23enum class ConsoleAPIType {
24  kLog,
25  kDebug,
26  kInfo,
27  kError,
28  kWarning,
29  kDir,
30  kDirXML,
31  kTable,
32  kTrace,
33  kStartGroup,
34  kStartGroupCollapsed,
35  kEndGroup,
36  kClear,
37  kAssert,
38  kTimeEnd,
39  kCount
40};
41
42class V8ConsoleMessage {
43 public:
44  ~V8ConsoleMessage();
45
46  static std::unique_ptr<V8ConsoleMessage> createForConsoleAPI(
47      double timestamp, ConsoleAPIType,
48      const std::vector<v8::Local<v8::Value>>& arguments,
49      std::unique_ptr<V8StackTraceImpl>, InspectedContext*);
50
51  static std::unique_ptr<V8ConsoleMessage> createForException(
52      double timestamp, const String16& detailedMessage, const String16& url,
53      unsigned lineNumber, unsigned columnNumber,
54      std::unique_ptr<V8StackTraceImpl>, int scriptId, v8::Isolate*,
55      const String16& message, int contextId, v8::Local<v8::Value> exception,
56      unsigned exceptionId);
57
58  static std::unique_ptr<V8ConsoleMessage> createForRevokedException(
59      double timestamp, const String16& message, unsigned revokedExceptionId);
60
61  V8MessageOrigin origin() const;
62  void reportToFrontend(protocol::Console::Frontend*) const;
63  void reportToFrontend(protocol::Runtime::Frontend*, V8InspectorSessionImpl*,
64                        bool generatePreview) const;
65  ConsoleAPIType type() const;
66  void contextDestroyed(int contextId);
67
68  int estimatedSize() const {
69    return m_v8Size + static_cast<int>(m_message.length() * sizeof(UChar));
70  }
71
72 private:
73  V8ConsoleMessage(V8MessageOrigin, double timestamp, const String16& message);
74
75  using Arguments = std::vector<std::unique_ptr<v8::Global<v8::Value>>>;
76  std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>>
77  wrapArguments(V8InspectorSessionImpl*, bool generatePreview) const;
78  std::unique_ptr<protocol::Runtime::RemoteObject> wrapException(
79      V8InspectorSessionImpl*, bool generatePreview) const;
80  void setLocation(const String16& url, unsigned lineNumber,
81                   unsigned columnNumber, std::unique_ptr<V8StackTraceImpl>,
82                   int scriptId);
83
84  V8MessageOrigin m_origin;
85  double m_timestamp;
86  String16 m_message;
87  String16 m_url;
88  unsigned m_lineNumber;
89  unsigned m_columnNumber;
90  std::unique_ptr<V8StackTraceImpl> m_stackTrace;
91  int m_scriptId;
92  int m_contextId;
93  ConsoleAPIType m_type;
94  unsigned m_exceptionId;
95  unsigned m_revokedExceptionId;
96  int m_v8Size = 0;
97  Arguments m_arguments;
98  String16 m_detailedMessage;
99};
100
101class V8ConsoleMessageStorage {
102 public:
103  V8ConsoleMessageStorage(V8InspectorImpl*, int contextGroupId);
104  ~V8ConsoleMessageStorage();
105
106  int contextGroupId() { return m_contextGroupId; }
107  const std::deque<std::unique_ptr<V8ConsoleMessage>>& messages() const {
108    return m_messages;
109  }
110
111  void addMessage(std::unique_ptr<V8ConsoleMessage>);
112  void contextDestroyed(int contextId);
113  void clear();
114
115 private:
116  V8InspectorImpl* m_inspector;
117  int m_contextGroupId;
118  int m_estimatedSize = 0;
119  std::deque<std::unique_ptr<V8ConsoleMessage>> m_messages;
120};
121
122}  // namespace v8_inspector
123
124#endif  // V8_INSPECTOR_V8CONSOLEMESSAGE_H_
125