blob: b4cd53f742d9c76cc2549403a0f94c27a9b47821 [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>
9
simonhatch0b016192015-05-22 19:14:0210#include "base/trace_event/trace_event_impl.h"
11#include "base/values.h"
12#include "content/common/content_export.h"
13
14namespace content {
oysteinef4ce0b062015-08-26 01:33:0615class BackgroundTracingConfig;
simonhatch0b016192015-05-22 19:14:0216
17// BackgroundTracingManager is used on the browser process to trigger the
18// collection of trace data and upload the results. Only the browser UI thread
19// is allowed to interact with the BackgroundTracingManager. All callbacks are
20// called on the UI thread.
21class BackgroundTracingManager {
22 public:
23 CONTENT_EXPORT static BackgroundTracingManager* GetInstance();
24
Gabriel Charetted699aa1e2017-07-21 15:20:2825 // ReceiveCallback will be called on the UI thread every time the
26 // BackgroundTracingManager finalizes a trace. The first parameter of this
27 // callback is the trace data. The second is metadata that was generated and
28 // embedded into the trace. The third is a callback to notify the
Oystein Eftevaag3b4f3792018-05-23 18:16:2229 // BackgroundTracingManager that you've finished processing the trace data
30 // and whether we were successful or not.
simonhatch0b016192015-05-22 19:14:0231 //
32 // Example:
33 //
simonhatch0cc9fee2015-06-15 22:26:1934 // void Upload(const scoped_refptr<base::RefCountedString>& data,
Oystein Eftevaag3b4f3792018-05-23 18:16:2235 // FinishedProcessingCallback done_callback) {
Gabriel Charetted699aa1e2017-07-21 15:20:2836 // base::PostTaskWithTraitsAndReply(
37 // FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND},
Oystein Eftevaag3b4f3792018-05-23 18:16:2238 // base::BindOnce(&DoUploadInBackground, data),
39 // std::move(done_callback));
simonhatch0b016192015-05-22 19:14:0240 // }
41 //
Oystein Eftevaag3b4f3792018-05-23 18:16:2242 using FinishedProcessingCallback = base::OnceCallback<void(bool success)>;
Gabriel Charetted699aa1e2017-07-21 15:20:2843 using ReceiveCallback =
Oystein Eftevaag02ba9a42018-06-02 00:43:5044 base::RepeatingCallback<void(const scoped_refptr<base::RefCountedString>&,
45 std::unique_ptr<const base::DictionaryValue>,
46 FinishedProcessingCallback)>;
simonhatch0b016192015-05-22 19:14:0247
48 // Set the triggering rules for when to start recording.
49 //
50 // In preemptive mode, recording begins immediately and any calls to
51 // TriggerNamedEvent() will potentially trigger the trace to finalize and get
52 // uploaded to the specified upload_sink. Once the trace has been uploaded,
53 // tracing will be enabled again.
54 //
55 // In reactive mode, recording begins when TriggerNamedEvent() is called, and
56 // continues until either the next call to TriggerNamedEvent, or a timeout
57 // occurs. Tracing will not be re-enabled after the trace is finalized and
58 // uploaded to the upload_sink.
59 //
60 // Calls to SetActiveScenario() with a config will fail if tracing is
61 // currently on. Use WhenIdle to register a callback to get notified when
62 // the manager is idle and a config can be set again.
oysteineaf2b8002015-06-06 05:02:2863 enum DataFiltering {
64 NO_DATA_FILTERING,
65 ANONYMIZE_DATA,
66 };
dcheng6003e0b2016-04-09 18:42:3467 virtual bool SetActiveScenario(
68 std::unique_ptr<BackgroundTracingConfig> config,
Oystein Eftevaag3b4f3792018-05-23 18:16:2269 ReceiveCallback receive_callback,
dcheng6003e0b2016-04-09 18:42:3470 DataFiltering data_filtering) = 0;
simonhatch0b016192015-05-22 19:14:0271
72 // Notifies the caller when the manager is idle (not recording or uploading),
73 // so that a call to SetActiveScenario() is likely to succeed.
74 typedef base::Callback<void()> IdleCallback;
75 virtual void WhenIdle(IdleCallback idle_callback) = 0;
76
77 typedef base::Callback<void(bool)> StartedFinalizingCallback;
78 typedef int TriggerHandle;
79
80 // Notifies that a manual trigger event has occurred, and we may need to
81 // either begin recording or finalize the trace, depending on the config.
82 // If the trigger specified isn't active in the config, this will do nothing.
83 virtual void TriggerNamedEvent(
84 TriggerHandle trigger_handle,
85 StartedFinalizingCallback started_callback) = 0;
86
87 // Registers a manual trigger handle, and returns a TriggerHandle which can
88 // be passed to DidTriggerHappen().
89 virtual TriggerHandle RegisterTriggerType(const char* trigger_name) = 0;
90
oysteine53772602015-11-02 22:41:2791 virtual bool HasActiveScenario() = 0;
92
simonhatch0b016192015-05-22 19:14:0293 virtual void InvalidateTriggerHandlesForTesting() = 0;
fmeawad743fcdf2015-06-03 23:55:3494 virtual void FireTimerForTesting() = 0;
fmeawad743fcdf2015-06-03 23:55:3495
simonhatch0b016192015-05-22 19:14:0296 protected:
97 virtual ~BackgroundTracingManager() {}
98};
99
100} // namespace content
101
102#endif // CONTENT_PUBLIC_BROWSER_BACKGROUND_TRACING_MANAGER_H_