blob: 7d64786f909192d8a583fb9035875a60d1439f89 [file] [log] [blame]
simonhatch0b016192015-05-22 19:14:021// Copyright 2015 The Chromium Authors. All rights reserved.
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_PUBLIC_BROWSER_BACKGROUND_TRACING_MANAGER_H_
6#define CONTENT_PUBLIC_BROWSER_BACKGROUND_TRACING_MANAGER_H_
7
dcheng6003e0b2016-04-09 18:42:348#include <memory>
Siddhartha S52314f22019-03-27 20:07:169#include <string>
dcheng6003e0b2016-04-09 18:42:3410
Siddhartha S52314f22019-03-27 20:07:1611#include "base/strings/string_piece.h"
simonhatch0b016192015-05-22 19:14:0212#include "base/trace_event/trace_event_impl.h"
13#include "base/values.h"
14#include "content/common/content_export.h"
15
16namespace content {
oysteinef4ce0b062015-08-26 01:33:0617class BackgroundTracingConfig;
simonhatch0b016192015-05-22 19:14:0218
19// BackgroundTracingManager is used on the browser process to trigger the
20// collection of trace data and upload the results. Only the browser UI thread
21// is allowed to interact with the BackgroundTracingManager. All callbacks are
22// called on the UI thread.
23class BackgroundTracingManager {
24 public:
25 CONTENT_EXPORT static BackgroundTracingManager* GetInstance();
26
Gabriel Charetted699aa1e2017-07-21 15:20:2827 // ReceiveCallback will be called on the UI thread every time the
28 // BackgroundTracingManager finalizes a trace. The first parameter of this
29 // callback is the trace data. The second is metadata that was generated and
30 // embedded into the trace. The third is a callback to notify the
Oystein Eftevaag3b4f3792018-05-23 18:16:2231 // BackgroundTracingManager that you've finished processing the trace data
32 // and whether we were successful or not.
simonhatch0b016192015-05-22 19:14:0233 //
34 // Example:
35 //
simonhatch0cc9fee2015-06-15 22:26:1936 // void Upload(const scoped_refptr<base::RefCountedString>& data,
Oystein Eftevaag3b4f3792018-05-23 18:16:2237 // FinishedProcessingCallback done_callback) {
Sami Kyostila807cd4d2019-08-01 16:15:0738 // base::PostTaskAndReply(
Gabriel Charetteb10aeebc2018-07-26 20:15:0039 // FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
Oystein Eftevaag3b4f3792018-05-23 18:16:2240 // base::BindOnce(&DoUploadInBackground, data),
41 // std::move(done_callback));
simonhatch0b016192015-05-22 19:14:0242 // }
43 //
Oystein Eftevaag3b4f3792018-05-23 18:16:2244 using FinishedProcessingCallback = base::OnceCallback<void(bool success)>;
Gabriel Charetted699aa1e2017-07-21 15:20:2845 using ReceiveCallback =
Oystein Eftevaag2386e052019-10-02 21:08:0946 base::RepeatingCallback<void(std::unique_ptr<std::string>,
Oystein Eftevaag02ba9a42018-06-02 00:43:5047 FinishedProcessingCallback)>;
simonhatch0b016192015-05-22 19:14:0248
49 // Set the triggering rules for when to start recording.
50 //
51 // In preemptive mode, recording begins immediately and any calls to
52 // TriggerNamedEvent() will potentially trigger the trace to finalize and get
53 // uploaded to the specified upload_sink. Once the trace has been uploaded,
54 // tracing will be enabled again.
55 //
56 // In reactive mode, recording begins when TriggerNamedEvent() is called, and
57 // continues until either the next call to TriggerNamedEvent, or a timeout
58 // occurs. Tracing will not be re-enabled after the trace is finalized and
59 // uploaded to the upload_sink.
60 //
61 // Calls to SetActiveScenario() with a config will fail if tracing is
62 // currently on. Use WhenIdle to register a callback to get notified when
63 // the manager is idle and a config can be set again.
oysteineaf2b8002015-06-06 05:02:2864 enum DataFiltering {
65 NO_DATA_FILTERING,
66 ANONYMIZE_DATA,
67 };
dcheng6003e0b2016-04-09 18:42:3468 virtual bool SetActiveScenario(
69 std::unique_ptr<BackgroundTracingConfig> config,
Oystein Eftevaag3b4f3792018-05-23 18:16:2270 ReceiveCallback receive_callback,
dcheng6003e0b2016-04-09 18:42:3471 DataFiltering data_filtering) = 0;
simonhatch0b016192015-05-22 19:14:0272
73 // Notifies the caller when the manager is idle (not recording or uploading),
74 // so that a call to SetActiveScenario() is likely to succeed.
danakjd46bdf42019-11-27 22:07:2775 using IdleCallback = base::RepeatingCallback<void()>;
simonhatch0b016192015-05-22 19:14:0276 virtual void WhenIdle(IdleCallback idle_callback) = 0;
77
danakjd46bdf42019-11-27 22:07:2778 using StartedFinalizingCallback = base::OnceCallback<void(bool)>;
79 using TriggerHandle = int;
simonhatch0b016192015-05-22 19:14:0280
81 // Notifies that a manual trigger event has occurred, and we may need to
82 // either begin recording or finalize the trace, depending on the config.
83 // If the trigger specified isn't active in the config, this will do nothing.
84 virtual void TriggerNamedEvent(
85 TriggerHandle trigger_handle,
86 StartedFinalizingCallback started_callback) = 0;
87
88 // Registers a manual trigger handle, and returns a TriggerHandle which can
89 // be passed to DidTriggerHappen().
90 virtual TriggerHandle RegisterTriggerType(const char* trigger_name) = 0;
91
oysteine53772602015-11-02 22:41:2792 virtual bool HasActiveScenario() = 0;
93
Siddhartha S52314f22019-03-27 20:07:1694 // Returns true whether a trace is ready to be uploaded.
95 virtual bool HasTraceToUpload() = 0;
96
97 // Returns the latest trace created for uploading in a serialized proto of
98 // message type perfetto::Trace.
99 // TODO(ssid): This should also return the trigger for the trace along with
100 // the serialized trace proto.
101 virtual std::string GetLatestTraceToUpload() = 0;
102
Etienne Bergeron252d92602018-10-25 14:31:21103 // For tests
Oystein Eftevaag3460b6b12019-05-15 16:40:15104 virtual void AbortScenarioForTesting() = 0;
105 virtual void SetTraceToUploadForTesting(
106 std::unique_ptr<std::string> trace_data) = 0;
fmeawad743fcdf2015-06-03 23:55:34107
simonhatch0b016192015-05-22 19:14:02108 protected:
109 virtual ~BackgroundTracingManager() {}
110};
111
112} // namespace content
113
114#endif // CONTENT_PUBLIC_BROWSER_BACKGROUND_TRACING_MANAGER_H_