blob: 0163bb1d54a0eb45b0540d41ff870cbba9d1cced [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2019 The Chromium Authors
Ken Rockotcbead332019-11-21 04:35:472// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_TRACING_TRACING_SERVICE_CONTROLLER_H_
6#define CONTENT_BROWSER_TRACING_TRACING_SERVICE_CONTROLLER_H_
7
8#include <map>
9#include <memory>
10
Avi Drissmanadac21992023-01-11 23:46:3911#include "base/functional/callback.h"
Ken Rockotcbead332019-11-21 04:35:4712#include "base/no_destructor.h"
13#include "base/process/process_handle.h"
Peter Kasting796cde22020-11-18 21:06:5314#include "base/types/pass_key.h"
Ken Rockotcbead332019-11-21 04:35:4715#include "mojo/public/cpp/bindings/pending_receiver.h"
16#include "mojo/public/cpp/bindings/remote.h"
17#include "services/tracing/public/mojom/traced_process.mojom.h"
18#include "services/tracing/public/mojom/tracing_service.mojom.h"
19
20namespace content {
21
22// Processes participating in tracing must register themselves with the global
23// instance of this object.
24class TracingServiceController {
25 public:
26 // An object held for the duration of any client process's registration with
27 // the tracing service.
28 class ClientRegistration {
29 public:
Peter Kasting796cde22020-11-18 21:06:5330 ClientRegistration(base::PassKey<TracingServiceController>,
Ken Rockotcbead332019-11-21 04:35:4731 base::OnceClosure unregister);
32 ~ClientRegistration();
33
34 private:
35 base::OnceClosure unregister_;
36 };
37
38 TracingServiceController(const TracingServiceController&) = delete;
39 ~TracingServiceController();
40 TracingServiceController& operator=(const TracingServiceController&) = delete;
41
42 // Gets the global instance of this object. Safe to call from any sequence,
43 // but see individual methods for other potential constraints.
44 static TracingServiceController& Get();
45
46 // A callback each process host must provide to implement how a TracedProcess
47 // interface is bound in the corresponding client process. Callbacks are
48 // always called from the UI thread.
49 using EnableTracingCallback = base::RepeatingCallback<void(
50 mojo::PendingReceiver<tracing::mojom::TracedProcess>)>;
51
52 // Registers a new client process with the tracing system. Any time the
53 // tracing service is started, |callback| will be invoked to connect the
54 // client process to the service.
55 //
56 // The process will remain registered as long as the returned
57 // ClientRegistration object remains alive.
58 //
59 // Safe to call from any sequence.
60 std::unique_ptr<ClientRegistration> RegisterClient(
61 base::ProcessId pid,
62 EnableTracingCallback callback);
63
64 // Retrieves a remote interface to the tracing service, which is started
65 // lazily if needed. Public content API consumers can use
66 // |content::GetTracingService()|.
67 //
68 // Must only be called on the UI thread.
69 tracing::mojom::TracingService& GetService();
70
71 private:
72 friend class base::NoDestructor<TracingServiceController>;
73
74 TracingServiceController();
75
Greg Thompson59bf58e2024-11-27 18:46:3576 // Handles disconnection by the tracing service.
77 void OnTracingServiceDisconnected();
Ken Rockotcbead332019-11-21 04:35:4778 void RegisterClientOnUIThread(base::ProcessId pid,
79 EnableTracingCallback callback);
80 void RemoveClient(base::ProcessId pid);
81
82 // NOTE: This state is accessed only from the UI thread.
83 mojo::Remote<tracing::mojom::TracingService> service_;
84 std::map<base::ProcessId, EnableTracingCallback> clients_;
85};
86
87} // namespace content
88
89#endif // CONTENT_BROWSER_TRACING_TRACING_SERVICE_CONTROLLER_H_