blob: bd853e35ddc9517086dc10e87b7e0afff24abb67 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2015 The Chromium Authors
oysteinef4ce0b062015-08-26 01:33:062// 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_BACKGROUND_TRACING_RULE_H_
6#define CONTENT_BROWSER_TRACING_BACKGROUND_TRACING_RULE_H_
7
Siddhartha24ef3c472017-08-22 00:19:208#include <memory>
Etienne Pierre-doray88cce9f2024-03-21 01:22:179#include <optional>
Siddhartha24ef3c472017-08-22 00:19:2010
Etienne Pierre-doray840a1172024-03-18 22:57:0011#include "base/observer_list_types.h"
Etienne Pierre-doray9f7ba73b2023-06-12 18:28:5012#include "base/timer/timer.h"
Etienne Pierre-doray9f7ba73b2023-06-12 18:28:5013#include "content/common/content_export.h"
14#include "third_party/perfetto/protos/perfetto/config/chrome/scenario_config.gen.h"
Siddhartha S7d37b8d42019-06-07 23:53:1215#include "third_party/perfetto/protos/perfetto/trace/chrome/chrome_metadata.pbzero.h"
oysteinef4ce0b062015-08-26 01:33:0616
oysteinef4ce0b062015-08-26 01:33:0617namespace content {
18
Etienne Pierre-doray840a1172024-03-18 22:57:0019class CONTENT_EXPORT BackgroundTracingRule : public base::CheckedObserver {
oysteinef4ce0b062015-08-26 01:33:0620 public:
Siddhartha S7d37b8d42019-06-07 23:53:1221 using MetadataProto =
22 perfetto::protos::pbzero::BackgroundTracingMetadata::TriggerRule;
Etienne Pierre-dorayb57b73942023-03-20 21:34:4323 // Returns true if the trigger caused a scenario to either begin recording or
24 // finalize the trace depending on the config.
25 using RuleTriggeredCallback =
26 base::RepeatingCallback<bool(const BackgroundTracingRule*)>;
Siddhartha S7d37b8d42019-06-07 23:53:1227
oysteinef4ce0b062015-08-26 01:33:0628 BackgroundTracingRule();
oysteinefa00aa02016-05-18 00:34:1429
Peter Boström828b9022021-09-21 02:28:4330 BackgroundTracingRule(const BackgroundTracingRule&) = delete;
31 BackgroundTracingRule& operator=(const BackgroundTracingRule&) = delete;
32
Etienne Pierre-doray840a1172024-03-18 22:57:0033 ~BackgroundTracingRule() override;
oysteinef4ce0b062015-08-26 01:33:0634
Etienne Pierre-dorayb57b73942023-03-20 21:34:4335 virtual void Install(RuleTriggeredCallback);
36 virtual void Uninstall();
Etienne Pierre-doray9f7ba73b2023-06-12 18:28:5037 virtual perfetto::protos::gen::TriggerRule ToProtoForTesting() const;
Siddhartha S7d37b8d42019-06-07 23:53:1238 virtual void GenerateMetadataProto(MetadataProto* out) const;
oysteinef4ce0b062015-08-26 01:33:0639
oysteineb6461a2c2015-10-30 22:35:4940 // Probability that we should allow a tigger to happen.
41 double trigger_chance() const { return trigger_chance_; }
Arthur Sonzognic686e8f2024-01-11 08:36:3742 std::optional<base::TimeDelta> delay() const { return delay_; }
oysteineb6461a2c2015-10-30 22:35:4943
Etienne Pierre-doray9f7ba73b2023-06-12 18:28:5044 static std::unique_ptr<BackgroundTracingRule> Create(
45 const perfetto::protos::gen::TriggerRule& config);
Etienne Pierre-doraya83afef2024-04-17 16:49:3846 static bool Append(
47 const std::vector<perfetto::protos::gen::TriggerRule>& configs,
48 std::vector<std::unique_ptr<BackgroundTracingRule>>& rules);
Etienne Pierre-doray9f7ba73b2023-06-12 18:28:5049
Etienne Pierre-doraycf6f2442024-12-03 18:23:4750 const std::string& rule_name() const { return rule_name_; }
Etienne Pierre-doray88cce9f2024-03-21 01:22:1751 std::optional<int32_t> triggered_value() const { return triggered_value_; }
Etienne Pierre-doray226632b2025-02-11 20:51:2152 uint64_t flow_id() const { return flow_id_; }
Stephen Nuskoa30b22e2019-08-27 21:10:0653
ssid3ae598b2020-12-24 22:48:3954 bool is_crash() const { return is_crash_; }
55
Etienne Pierre-doray226632b2025-02-11 20:51:2156 bool OnRuleTriggered(std::optional<int32_t> value, uint64_t flow_id);
Etienne Pierre-doray840a1172024-03-18 22:57:0057
Stephen Nuskoa30b22e2019-08-27 21:10:0658 protected:
Etienne Pierre-doraycf6f2442024-12-03 18:23:4759 virtual std::string GetDefaultRuleName() const;
Stephen Nuskoa30b22e2019-08-27 21:10:0660
Etienne Pierre-dorayb57b73942023-03-20 21:34:4361 virtual void DoInstall() = 0;
62 virtual void DoUninstall() = 0;
Etienne Pierre-dorayb57b73942023-03-20 21:34:4363
64 bool installed() const { return installed_; }
65
oysteinef4ce0b062015-08-26 01:33:0666 private:
Etienne Pierre-doray9f7ba73b2023-06-12 18:28:5067 void Setup(const perfetto::protos::gen::TriggerRule& config);
Etienne Pierre-doray0aff0eb2022-12-13 22:36:5068
Etienne Pierre-dorayb57b73942023-03-20 21:34:4369 RuleTriggeredCallback trigger_callback_;
70 bool installed_ = false;
ssid3ae598b2020-12-24 22:48:3971 double trigger_chance_ = 1.0;
Arthur Sonzognic686e8f2024-01-11 08:36:3772 std::optional<base::TimeDelta> delay_;
73 std::optional<base::TimeDelta> activation_delay_;
Etienne Pierre-doray6cf25ff2023-10-31 16:18:3274 base::OneShotTimer trigger_timer_;
75 base::OneShotTimer activation_timer_;
Etienne Pierre-doraycf6f2442024-12-03 18:23:4776 std::string rule_name_;
Etienne Pierre-doray88cce9f2024-03-21 01:22:1777 std::optional<int32_t> triggered_value_;
Etienne Pierre-doray226632b2025-02-11 20:51:2178 uint64_t flow_id_;
ssid3ae598b2020-12-24 22:48:3979 bool is_crash_ = false;
oysteinef4ce0b062015-08-26 01:33:0680};
81
82} // namespace content
83
84#endif // CONTENT_BROWSER_TRACING_BACKGROUND_TRACING_RULE_H_