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