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> |
| 9 | |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 10 | #include "base/trace_event/trace_event_impl.h" |
| 11 | #include "base/values.h" |
| 12 | #include "content/common/content_export.h" |
| 13 | |
| 14 | namespace content { |
oysteine | f4ce0b06 | 2015-08-26 01:33:06 | [diff] [blame] | 15 | class BackgroundTracingConfig; |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 16 | |
| 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. |
| 21 | class BackgroundTracingManager { |
| 22 | public: |
| 23 | CONTENT_EXPORT static BackgroundTracingManager* GetInstance(); |
| 24 | |
Gabriel Charette | d699aa1e | 2017-07-21 15:20:28 | [diff] [blame] | 25 | // 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 Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 29 | // BackgroundTracingManager that you've finished processing the trace data |
| 30 | // and whether we were successful or not. |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 31 | // |
| 32 | // Example: |
| 33 | // |
simonhatch | 0cc9fee | 2015-06-15 22:26:19 | [diff] [blame] | 34 | // void Upload(const scoped_refptr<base::RefCountedString>& data, |
Oystein Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 35 | // FinishedProcessingCallback done_callback) { |
Gabriel Charette | d699aa1e | 2017-07-21 15:20:28 | [diff] [blame] | 36 | // base::PostTaskWithTraitsAndReply( |
| 37 | // FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, |
Oystein Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 38 | // base::BindOnce(&DoUploadInBackground, data), |
| 39 | // std::move(done_callback)); |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 40 | // } |
| 41 | // |
Oystein Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 42 | using FinishedProcessingCallback = base::OnceCallback<void(bool success)>; |
Gabriel Charette | d699aa1e | 2017-07-21 15:20:28 | [diff] [blame] | 43 | using ReceiveCallback = |
Oystein Eftevaag | 02ba9a4 | 2018-06-02 00:43:50 | [diff] [blame^] | 44 | base::RepeatingCallback<void(const scoped_refptr<base::RefCountedString>&, |
| 45 | std::unique_ptr<const base::DictionaryValue>, |
| 46 | FinishedProcessingCallback)>; |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 47 | |
| 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. |
oysteine | af2b800 | 2015-06-06 05:02:28 | [diff] [blame] | 63 | enum DataFiltering { |
| 64 | NO_DATA_FILTERING, |
| 65 | ANONYMIZE_DATA, |
| 66 | }; |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 67 | virtual bool SetActiveScenario( |
| 68 | std::unique_ptr<BackgroundTracingConfig> config, |
Oystein Eftevaag | 3b4f379 | 2018-05-23 18:16:22 | [diff] [blame] | 69 | ReceiveCallback receive_callback, |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 70 | DataFiltering data_filtering) = 0; |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 71 | |
| 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 | |
oysteine | 5377260 | 2015-11-02 22:41:27 | [diff] [blame] | 91 | virtual bool HasActiveScenario() = 0; |
| 92 | |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 93 | virtual void InvalidateTriggerHandlesForTesting() = 0; |
fmeawad | 743fcdf | 2015-06-03 23:55:34 | [diff] [blame] | 94 | virtual void FireTimerForTesting() = 0; |
fmeawad | 743fcdf | 2015-06-03 23:55:34 | [diff] [blame] | 95 | |
simonhatch | 0b01619 | 2015-05-22 19:14:02 | [diff] [blame] | 96 | protected: |
| 97 | virtual ~BackgroundTracingManager() {} |
| 98 | }; |
| 99 | |
| 100 | } // namespace content |
| 101 | |
| 102 | #endif // CONTENT_PUBLIC_BROWSER_BACKGROUND_TRACING_MANAGER_H_ |