1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TRACED_PROBES_ANDROID_SYSTEM_PROPERTY_ANDROID_SYSTEM_PROPERTY_DATA_SOURCE_H_
18 #define SRC_TRACED_PROBES_ANDROID_SYSTEM_PROPERTY_ANDROID_SYSTEM_PROPERTY_DATA_SOURCE_H_
19 
20 #include <memory>
21 #include <optional>
22 #include <vector>
23 
24 #include "perfetto/ext/base/weak_ptr.h"
25 #include "perfetto/ext/tracing/core/trace_writer.h"
26 #include "src/traced/probes/probes_data_source.h"
27 
28 namespace perfetto {
29 
30 namespace base {
31 class TaskRunner;
32 }
33 
34 class AndroidSystemPropertyDataSource : public ProbesDataSource {
35  public:
36   static const ProbesDataSource::Descriptor descriptor;
37 
38   AndroidSystemPropertyDataSource(base::TaskRunner* task_runner,
39                                   const DataSourceConfig& ds_config,
40                                   TracingSessionID session_id,
41                                   std::unique_ptr<TraceWriter> writer);
42 
43   // ProbesDataSource implementation.
44   void Start() override;
45   void Flush(FlushRequestID, std::function<void()> callback) override;
46 
47   // Virtual for testing.
48   virtual const std::optional<std::string> ReadProperty(
49       const std::string& name);
50 
51  private:
52   void Tick();
53   base::WeakPtr<AndroidSystemPropertyDataSource> GetWeakPtr() const;
54   void WriteState();
55 
56   base::TaskRunner* const task_runner_;
57   std::unique_ptr<TraceWriter> writer_;
58   uint32_t poll_period_ms_ = 0;
59   std::vector<std::string> property_names_;
60   base::WeakPtrFactory<AndroidSystemPropertyDataSource>
61       weak_factory_;  // Keep last.
62 };
63 
64 }  // namespace perfetto
65 
66 #endif  // SRC_TRACED_PROBES_ANDROID_SYSTEM_PROPERTY_ANDROID_SYSTEM_PROPERTY_DATA_SOURCE_H_
67