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_
|