• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_V8_CONSOLE_MESSAGE_H_
6 #define V8_INSPECTOR_V8_CONSOLE_MESSAGE_H_
7 
8 #include <deque>
9 #include <map>
10 #include <memory>
11 #include <set>
12 
13 #include "include/v8.h"
14 #include "src/inspector/protocol/Console.h"
15 #include "src/inspector/protocol/Forward.h"
16 #include "src/inspector/protocol/Runtime.h"
17 
18 namespace v8_inspector {
19 
20 class InspectedContext;
21 class V8InspectorImpl;
22 class V8InspectorSessionImpl;
23 class V8StackTraceImpl;
24 
25 enum class V8MessageOrigin { kConsole, kException, kRevokedException };
26 
27 enum class ConsoleAPIType {
28   kLog,
29   kDebug,
30   kInfo,
31   kError,
32   kWarning,
33   kDir,
34   kDirXML,
35   kTable,
36   kTrace,
37   kStartGroup,
38   kStartGroupCollapsed,
39   kEndGroup,
40   kClear,
41   kAssert,
42   kTimeEnd,
43   kCount
44 };
45 
46 class V8ConsoleMessage {
47  public:
48   ~V8ConsoleMessage();
49 
50   static std::unique_ptr<V8ConsoleMessage> createForConsoleAPI(
51       v8::Local<v8::Context> v8Context, int contextId, int groupId,
52       V8InspectorImpl* inspector, double timestamp, ConsoleAPIType,
53       const std::vector<v8::Local<v8::Value>>& arguments,
54       const String16& consoleContext, std::unique_ptr<V8StackTraceImpl>);
55 
56   static std::unique_ptr<V8ConsoleMessage> createForException(
57       double timestamp, const String16& detailedMessage, const String16& url,
58       unsigned lineNumber, unsigned columnNumber,
59       std::unique_ptr<V8StackTraceImpl>, int scriptId, v8::Isolate*,
60       const String16& message, int contextId, v8::Local<v8::Value> exception,
61       unsigned exceptionId);
62 
63   static std::unique_ptr<V8ConsoleMessage> createForRevokedException(
64       double timestamp, const String16& message, unsigned revokedExceptionId);
65 
66   V8MessageOrigin origin() const;
67   void reportToFrontend(protocol::Console::Frontend*) const;
68   void reportToFrontend(protocol::Runtime::Frontend*, V8InspectorSessionImpl*,
69                         bool generatePreview) const;
70   ConsoleAPIType type() const;
71   void contextDestroyed(int contextId);
72 
estimatedSize()73   int estimatedSize() const {
74     return m_v8Size + static_cast<int>(m_message.length() * sizeof(UChar));
75   }
76 
77  private:
78   V8ConsoleMessage(V8MessageOrigin, double timestamp, const String16& message);
79 
80   using Arguments = std::vector<std::unique_ptr<v8::Global<v8::Value>>>;
81   std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>>
82   wrapArguments(V8InspectorSessionImpl*, bool generatePreview) const;
83   std::unique_ptr<protocol::Runtime::RemoteObject> wrapException(
84       V8InspectorSessionImpl*, bool generatePreview) const;
85   void setLocation(const String16& url, unsigned lineNumber,
86                    unsigned columnNumber, std::unique_ptr<V8StackTraceImpl>,
87                    int scriptId);
88 
89   V8MessageOrigin m_origin;
90   double m_timestamp;
91   String16 m_message;
92   String16 m_url;
93   unsigned m_lineNumber;
94   unsigned m_columnNumber;
95   std::unique_ptr<V8StackTraceImpl> m_stackTrace;
96   int m_scriptId;
97   int m_contextId;
98   ConsoleAPIType m_type;
99   unsigned m_exceptionId;
100   unsigned m_revokedExceptionId;
101   int m_v8Size = 0;
102   Arguments m_arguments;
103   String16 m_detailedMessage;
104   String16 m_consoleContext;
105 };
106 
107 class V8ConsoleMessageStorage {
108  public:
109   V8ConsoleMessageStorage(V8InspectorImpl*, int contextGroupId);
110   ~V8ConsoleMessageStorage();
111 
contextGroupId()112   int contextGroupId() { return m_contextGroupId; }
messages()113   const std::deque<std::unique_ptr<V8ConsoleMessage>>& messages() const {
114     return m_messages;
115   }
116 
117   void addMessage(std::unique_ptr<V8ConsoleMessage>);
118   void contextDestroyed(int contextId);
119   void clear();
120 
121   bool shouldReportDeprecationMessage(int contextId, const String16& method);
122   int count(int contextId, const String16& id);
123   bool countReset(int contextId, const String16& id);
124   void time(int contextId, const String16& id);
125   double timeLog(int contextId, const String16& id);
126   double timeEnd(int contextId, const String16& id);
127   bool hasTimer(int contextId, const String16& id);
128 
129  private:
130   V8InspectorImpl* m_inspector;
131   int m_contextGroupId;
132   int m_estimatedSize = 0;
133   std::deque<std::unique_ptr<V8ConsoleMessage>> m_messages;
134 
135   struct PerContextData {
136     std::set<String16> m_reportedDeprecationMessages;
137     // Corresponds to https://console.spec.whatwg.org/#count-map
138     std::map<String16, int> m_count;
139     // Corresponds to https://console.spec.whatwg.org/#timer-table
140     std::map<String16, double> m_time;
141   };
142   std::map<int, PerContextData> m_data;
143 };
144 
145 }  // namespace v8_inspector
146 
147 #endif  // V8_INSPECTOR_V8_CONSOLE_MESSAGE_H_
148