1 /*
2  * Copyright (C) 2019 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_PACKAGES_LIST_PACKAGES_LIST_DATA_SOURCE_H_
18 #define SRC_TRACED_PROBES_PACKAGES_LIST_PACKAGES_LIST_DATA_SOURCE_H_
19 
20 #include <functional>
21 #include <memory>
22 #include <set>
23 
24 #include "perfetto/base/task_runner.h"
25 #include "perfetto/ext/base/scoped_file.h"
26 
27 #include "perfetto/ext/tracing/core/basic_types.h"
28 #include "perfetto/tracing/core/data_source_config.h"
29 #include "protos/perfetto/config/android/packages_list_config.pbzero.h"
30 #include "protos/perfetto/trace/android/packages_list.pbzero.h"
31 
32 #include "src/traced/probes/probes_data_source.h"
33 
34 namespace perfetto {
35 
36 class TraceWriter;
37 
38 bool ParsePackagesListStream(protos::pbzero::PackagesList* packages_list,
39                              const base::ScopedFstream& fs,
40                              const std::set<std::string>& package_name_filter);
41 
42 class PackagesListDataSource : public ProbesDataSource {
43  public:
44   static const ProbesDataSource::Descriptor descriptor;
45 
46   PackagesListDataSource(const DataSourceConfig& ds_config,
47                          TracingSessionID session_id,
48                          std::unique_ptr<TraceWriter> writer);
49   // ProbesDataSource implementation.
50   void Start() override;
51   void Flush(FlushRequestID, std::function<void()> callback) override;
52 
53   ~PackagesListDataSource() override;
54 
55  private:
56   // If empty, include all package names. std::set over std::unordered_set as
57   // this should be trivially small (or empty) in practice, and the latter uses
58   // ever so slightly more memory.
59   std::set<std::string> package_name_filter_;
60   std::unique_ptr<TraceWriter> writer_;
61 };
62 
63 }  // namespace perfetto
64 
65 #endif  // SRC_TRACED_PROBES_PACKAGES_LIST_PACKAGES_LIST_DATA_SOURCE_H_
66