File: devtools_log_reader.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (105 lines) | stat: -rw-r--r-- 3,681 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_TEST_CHROMEDRIVER_LOG_REPLAY_DEVTOOLS_LOG_READER_H_
#define CHROME_TEST_CHROMEDRIVER_LOG_REPLAY_DEVTOOLS_LOG_READER_H_

#include <fstream>
#include <memory>
#include <string>

#include "base/files/file_path.h"

// Represents one DevTools entry (command or response) in the log.
//
// These appear in the log in the following format:
// [<timestamp>][DEBUG]: DevTools <protocol_type> <event_type> <command_name>
//     (id=<id>) <payload>
// where:
//
// <protocol_type> is either HTTP or WebSocket
//
// <event_type> is either "Command:" (for WebSocket only), "Request:" (for HTTP
//   only), "Response:", or "Event:"
//
// <command_name> is the command for WebSocket (like "DOM.getDocument") or a url
// for HTTP (like "http://localhost:38845/json")
//
// <id> is a sequential number to identify WebSocket commands with their
// responses
//
// <socket_id> identifies the WebSocket instance this entry came from, if any.
//
// <payload> is either the parameters in case of a WebSocket command, or the
// response in case of any response. It is always a JSON, and always spans
// multiple lines.
class LogEntry {
 public:
  // Build this LogEntry using the header line of an entry in the log. This
  // doesn't initialize the payload member. The payload must be parsed from
  // lines after the header.
  explicit LogEntry(std::istringstream& header_stream);
  ~LogEntry();

  enum EventType {
    kRequest,  // Command or Request depending on HTTP or WebSocket client
    kResponse,
    kEvent
  };
  enum Protocol { kHTTP, kWebSocket };

  EventType event_type;
  Protocol protocol_type;
  std::string command_name;
  std::string payload;
  int id;
  std::string session_id;
  std::string socket_id;
  bool error;
};

// Reads a log file for DevTools entries.
class DevToolsLogReader {
 public:
  // Initialize the log reader using a path to a log file to read from.
  explicit DevToolsLogReader(const base::FilePath& log_path);
  ~DevToolsLogReader();

  // Get the next DevTools entry in the log of the specified protocol type.
  //
  // This returns commands, responses, and events separately. If there are
  // no remaining entries of the specified type, or if there is some other
  // problem encountered like a truncated JSON, nullptr is returned.
  std::unique_ptr<LogEntry> GetNext(LogEntry::Protocol protocol_type);
  // Undo the previous GetNext call.
  //
  // "Gives back" the unique_ptr to the LogEntry object to the log reader,
  // to be returned the next time GetNext is called.
  void UndoGetNext(std::unique_ptr<LogEntry> next);

 private:
  std::unique_ptr<LogEntry> peeked;
  std::ifstream log_file;
  // Starting with |header_line|, parse a JSON string out of the log file.
  //
  // will parse either list or dictionary-type JSON strings, depending on the
  // starting character.
  std::string GetJSONString(std::istringstream& header_line);

  // Return whether |line| is a header of a DevTools entry.
  //
  // This only parses out the first two words of the line, meaning that the
  // stream can be re-used to parse the specifics of the entry after calling
  // this.
  bool IsHeader(std::istringstream& line) const;

  // Count (number of opening_char) - (number of closing_char) in |line|.
  //
  // Used to check for the end of JSON parameters. Ignores characters inside of
  // non-escaped quotes.
  int CountChar(const std::string& line,
                char opening_char,
                char closing_char) const;
};

#endif  // CHROME_TEST_CHROMEDRIVER_LOG_REPLAY_DEVTOOLS_LOG_READER_H_