Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Ken Rockot | cbead33 | 2019-11-21 04:35:47 | [diff] [blame] | 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 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 Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 11 | #include "base/functional/callback.h" |
Ken Rockot | cbead33 | 2019-11-21 04:35:47 | [diff] [blame] | 12 | #include "base/no_destructor.h" |
| 13 | #include "base/process/process_handle.h" |
Peter Kasting | 796cde2 | 2020-11-18 21:06:53 | [diff] [blame] | 14 | #include "base/types/pass_key.h" |
Ken Rockot | cbead33 | 2019-11-21 04:35:47 | [diff] [blame] | 15 | #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 | |
| 20 | namespace content { |
| 21 | |
| 22 | // Processes participating in tracing must register themselves with the global |
| 23 | // instance of this object. |
| 24 | class 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 Kasting | 796cde2 | 2020-11-18 21:06:53 | [diff] [blame] | 30 | ClientRegistration(base::PassKey<TracingServiceController>, |
Ken Rockot | cbead33 | 2019-11-21 04:35:47 | [diff] [blame] | 31 | 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 Thompson | 59bf58e | 2024-11-27 18:46:35 | [diff] [blame] | 76 | // Handles disconnection by the tracing service. |
| 77 | void OnTracingServiceDisconnected(); |
Ken Rockot | cbead33 | 2019-11-21 04:35:47 | [diff] [blame] | 78 | 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_ |