xref: /aosp_15_r20/external/perfetto/src/trace_redaction/trace_redactor.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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_TRACE_REDACTION_TRACE_REDACTOR_H_
18 #define SRC_TRACE_REDACTION_TRACE_REDACTOR_H_
19 
20 #include <memory>
21 #include <string>
22 #include <string_view>
23 #include <vector>
24 
25 #include "perfetto/base/status.h"
26 #include "perfetto/trace_processor/trace_blob_view.h"
27 #include "src/trace_redaction/trace_redaction_framework.h"
28 
29 namespace perfetto::trace_redaction {
30 
31 // Removes sensitive information from Perfetto traces by executing collect,
32 // build, and transforms primtives in the correct order.
33 //
34 // The caller is responsible for adding all neccessary primitives. Primitives
35 // are not directly dependent on each other, but rather dependent on the
36 // information inside of the context.
37 class TraceRedactor {
38  public:
39   TraceRedactor();
40   virtual ~TraceRedactor();
41 
42   // Entry point for redacting a trace. Regardless of success/failure, `context`
43   // will contain the current state.
44   base::Status Redact(std::string_view source_filename,
45                       std::string_view dest_filename,
46                       Context* context) const;
47 
48   // T must be derived from trace_redaction::CollectPrimitive.
49   template <typename T>
emplace_collect()50   T* emplace_collect() {
51     auto uptr = std::make_unique<T>();
52     auto* ptr = uptr.get();
53     collectors_.push_back(std::move(uptr));
54     return ptr;
55   }
56 
57   // T must be derived from trace_redaction::BuildPrimitive.
58   template <typename T>
emplace_build()59   T* emplace_build() {
60     auto uptr = std::make_unique<T>();
61     auto* ptr = uptr.get();
62     builders_.push_back(std::move(uptr));
63     return ptr;
64   }
65 
66   // T must be derived from trace_redaction::TransformPrimitive.
67   template <typename T>
emplace_transform()68   T* emplace_transform() {
69     auto uptr = std::make_unique<T>();
70     auto* ptr = uptr.get();
71     transformers_.push_back(std::move(uptr));
72     return ptr;
73   }
74 
75   struct Config {
76     // Controls whether or not the verify primitive is added to the pipeline.
77     // This should always be enabled unless you know that your test content
78     // fails verification.
79     bool verify = true;
80   };
81 
82   static std::unique_ptr<TraceRedactor> CreateInstance(const Config& config);
83 
84  private:
85   // Run all collectors on a packet because moving to the next package.
86   //
87   // ```
88   //  with context:
89   //   for packet in packets:
90   //     for collector in collectors:
91   //       collector(context, packet)
92   // ```
93   base::Status Collect(Context* context,
94                        const trace_processor::TraceBlobView& view) const;
95 
96   // Runs builders once.
97   //
98   // ```
99   //  with context:
100   //   for builder in builders:
101   //      builder(context)
102   // ```
103   base::Status Build(Context* context) const;
104 
105   // Runs all transformers on a packet before moving to the next package.
106   //
107   // ```
108   //  with context:
109   //   for packet in packets:
110   //     for transform in transformers:
111   //       transform(context, packet)
112   // ```
113   base::Status Transform(const Context& context,
114                          const trace_processor::TraceBlobView& view,
115                          const std::string& dest_file) const;
116 
117   std::vector<std::unique_ptr<CollectPrimitive>> collectors_;
118   std::vector<std::unique_ptr<BuildPrimitive>> builders_;
119   std::vector<std::unique_ptr<TransformPrimitive>> transformers_;
120 };
121 
122 }  // namespace perfetto::trace_redaction
123 
124 #endif  // SRC_TRACE_REDACTION_TRACE_REDACTOR_H_
125