simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 1 | // 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 | |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 8 | #include <memory> |
Siddhartha S | 52314f2 | 2019-03-27 20:07:16 | [diff] [blame] | 9 | #include <string> |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 10 | |
Siddhartha S | 52314f2 | 2019-03-27 20:07:16 | [diff] [blame] | 11 | #include "base/strings/string_piece.h" |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 12 | #include "base/trace_event/trace_event_impl.h" |
| 13 | #include "base/values.h" |
| 14 | #include "content/common/content_export.h" |
| 15 | |
| 16 | namespace content { |
oysteine | f4ce0b06 | 2015-08-26 01:33:06 | [diff] [blame] | 17 | class BackgroundTracingConfig; |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 18 | |
| 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. |
| 23 | class BackgroundTracingManager { |
| 24 | public: |
| 25 | CONTENT_EXPORT static BackgroundTracingManager* GetInstance(); |
| 26 | |
Gabriel Charette | d699aa1e | 2017-07-21 15:20:28 | [diff] [blame] | 27 | // 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 Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 31 | // BackgroundTracingManager that you've finished processing the trace data |
| 32 | // and whether we were successful or not. |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 33 | // |
| 34 | // Example: |
| 35 | // |
simonhatch | 0cc9fee | 2015-06-15 22:26:19 | [diff] [blame] | 36 | // void Upload(const scoped_refptr<base::RefCountedString>& data, |
Oystein Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 37 | // FinishedProcessingCallback done_callback) { |
Sami Kyostila | 807cd4d | 2019-08-01 16:15:07 | [diff] [blame] | 38 | // base::PostTaskAndReply( |
Gabriel Charette | b10aeebc | 2018-07-26 20:15:00 | [diff] [blame] | 39 | // FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT}, |
Oystein Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 40 | // base::BindOnce(&DoUploadInBackground, data), |
| 41 | // std::move(done_callback)); |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 42 | // } |
| 43 | // |
Oystein Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 44 | using FinishedProcessingCallback = base::OnceCallback<void(bool success)>; |
Gabriel Charette | d699aa1e | 2017-07-21 15:20:28 | [diff] [blame] | 45 | using ReceiveCallback = |
Oystein Eftevaag | 2386e05 | 2019-10-02 21:08:09 | [diff] [blame] | 46 | base::RepeatingCallback<void(std::unique_ptr<std::string>, |
Oystein Eftevaag | 02ba9a4 | 2018-06-02 00:43:50 | [diff] [blame] | 47 | FinishedProcessingCallback)>; |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 48 | |
| 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. |
oysteine | af2b800 | 2015-06-06 05:02:28 | [diff] [blame] | 64 | enum DataFiltering { |
| 65 | NO_DATA_FILTERING, |
| 66 | ANONYMIZE_DATA, |
| 67 | }; |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 68 | virtual bool SetActiveScenario( |
| 69 | std::unique_ptr<BackgroundTracingConfig> config, |
Oystein Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 70 | ReceiveCallback receive_callback, |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 71 | DataFiltering data_filtering) = 0; |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 72 | |
| 73 | // Notifies the caller when the manager is idle (not recording or uploading), |
| 74 | // so that a call to SetActiveScenario() is likely to succeed. |
danakj | d46bdf4 | 2019-11-27 22:07:27 | [diff] [blame] | 75 | using IdleCallback = base::RepeatingCallback<void()>; |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 76 | virtual void WhenIdle(IdleCallback idle_callback) = 0; |
| 77 | |
danakj | d46bdf4 | 2019-11-27 22:07:27 | [diff] [blame] | 78 | using StartedFinalizingCallback = base::OnceCallback<void(bool)>; |
| 79 | using TriggerHandle = int; |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 80 | |
| 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 | |
oysteine | 5377260 | 2015-11-02 22:41:27 | [diff] [blame] | 92 | virtual bool HasActiveScenario() = 0; |
| 93 | |
Siddhartha S | 52314f2 | 2019-03-27 20:07:16 | [diff] [blame] | 94 | // 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 Bergeron | 252d9260 | 2018-10-25 14:31:21 | [diff] [blame] | 103 | // For tests |
Oystein Eftevaag | 3460b6b1 | 2019-05-15 16:40:15 | [diff] [blame] | 104 | virtual void AbortScenarioForTesting() = 0; |
| 105 | virtual void SetTraceToUploadForTesting( |
| 106 | std::unique_ptr<std::string> trace_data) = 0; |
fmeawad | 743fcdf | 2015-06-03 23:55:34 | [diff] [blame] | 107 | |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 108 | protected: |
| 109 | virtual ~BackgroundTracingManager() {} |
| 110 | }; |
| 111 | |
| 112 | } // namespace content |
| 113 | |
| 114 | #endif // CONTENT_PUBLIC_BROWSER_BACKGROUND_TRACING_MANAGER_H_ |