File: journal.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (89 lines) | stat: -rw-r--r-- 3,077 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 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_RENDERER_ACTOR_JOURNAL_H_
#define CHROME_RENDERER_ACTOR_JOURNAL_H_

#include "base/memory/safe_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/types/pass_key.h"
#include "chrome/common/actor.mojom.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/pending_associated_remote.h"

namespace actor {

// A logging class that records actions taken be the actor.
// This class employs a buffering strategy to minimize the number of
// IPCs sent to the browser. The following strategy is employed:
// 1) If there is a pending event in the buffer, don't do anything just
//    wait for the previous event logged to trigger (the 200ms timeout).
// 2) If it has been more than 100ms since we last sent data to the browser
//    send it immediately.
// 3) Otherwise schedule a timer to send the contents of the buffer in 200ms.
class Journal {
 public:
  using TaskId = int32_t;

  Journal();
  ~Journal();

  // A pending async journal entry.
  class PendingAsyncEntry {
   public:
    // Creation of the event is only from the Journal itself. Use
    // `Journal::CreatePendingAsyncEntry` to create this object.
    PendingAsyncEntry(base::PassKey<Journal>,
                      base::SafeRef<Journal> journal,
                      TaskId task_id,
                      uint64_t trace_id,
                      std::string_view event_name);
    ~PendingAsyncEntry();

    // End an pending entry with additional details. This can only be called
    // once and will be automatically called from the destructor if it hasn't
    // been called.
    void EndEntry(std::string_view details);

   private:
    base::PassKey<Journal> pass_key_;
    bool terminated_ = false;
    base::SafeRef<Journal> journal_;
    TaskId task_id_;
    uint64_t trace_id_;
    std::string event_name_;
  };

  void Bind(mojo::PendingAssociatedRemote<mojom::JournalClient> client);
  void Log(TaskId task_id, std::string_view event, std::string_view details);

  // Create an async entry. This will log a Begin Entry event and when the
  // PendingAsyncEntry object is destroyed the End Entry will be logged.
  std::unique_ptr<PendingAsyncEntry> CreatePendingAsyncEntry(
      TaskId task_id,
      std::string_view event_name,
      std::string_view details);

  void AddEndEvent(base::PassKey<Journal>,
                   TaskId task_id,
                   uint64_t trace_id,
                   const std::string& event_name,
                   std::string_view details);

 private:
  void AddJournalEntry(mojom::JournalEntryPtr journal_entry);
  void SendLogBuffer();

  mojo::AssociatedRemote<mojom::JournalClient> client_;
  std::vector<mojom::JournalEntryPtr> log_buffer_;
  base::TimeTicks last_log_buffer_send_;
  uint64_t current_id_;

  base::WeakPtrFactory<Journal> weak_factory_{this};
};

}  // namespace actor

#endif  // CHROME_RENDERER_ACTOR_JOURNAL_H_