Trace Redaction - Consolidate Modifiers

Pull all modifiers into one file. The only modifiers that were not
pulled into the new file are the ones that are specific to the
process tree.

Change-Id: I74fe9180eb785c17f29d70518cf303e264e2d466
diff --git a/Android.bp b/Android.bp
index 2b9993f..45eb6ad 100644
--- a/Android.bp
+++ b/Android.bp
@@ -13610,6 +13610,7 @@
         "src/trace_redaction/filter_packet_using_allowlist.cc",
         "src/trace_redaction/filtering.cc",
         "src/trace_redaction/find_package_uid.cc",
+        "src/trace_redaction/modify.cc",
         "src/trace_redaction/modify_process_trees.cc",
         "src/trace_redaction/populate_allow_lists.cc",
         "src/trace_redaction/process_thread_timeline.cc",
diff --git a/src/trace_redaction/BUILD.gn b/src/trace_redaction/BUILD.gn
index 03fbe6a..5d0ed14 100644
--- a/src/trace_redaction/BUILD.gn
+++ b/src/trace_redaction/BUILD.gn
@@ -41,6 +41,8 @@
     "find_package_uid.cc",
     "find_package_uid.h",
     "frame_cookie.h",
+    "modify.cc",
+    "modify.h",
     "modify_process_trees.cc",
     "modify_process_trees.h",
     "populate_allow_lists.cc",
diff --git a/src/trace_redaction/modify.cc b/src/trace_redaction/modify.cc
new file mode 100644
index 0000000..10c018c
--- /dev/null
+++ b/src/trace_redaction/modify.cc
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "src/trace_redaction/modify.h"
+
+namespace perfetto::trace_redaction {
+
+PidCommModifier::~PidCommModifier() = default;
+
+void ClearComms::Modify(const Context& context,
+                        uint64_t ts,
+                        int32_t,
+                        int32_t* pid,
+                        std::string* comm) const {
+  PERFETTO_DCHECK(context.timeline);
+  PERFETTO_DCHECK(context.package_uid.has_value());
+  PERFETTO_DCHECK(pid);
+  PERFETTO_DCHECK(comm);
+
+  if (!context.timeline->PidConnectsToUid(ts, *pid, *context.package_uid)) {
+    comm->clear();
+  }
+}
+
+void DoNothing::Modify(const Context&,
+                       uint64_t,
+                       int32_t,
+                       int32_t*,
+                       std::string*) const {}
+
+}  // namespace perfetto::trace_redaction
diff --git a/src/trace_redaction/modify.h b/src/trace_redaction/modify.h
new file mode 100644
index 0000000..6178623
--- /dev/null
+++ b/src/trace_redaction/modify.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <cstdint>
+
+#include "src/trace_redaction/trace_redaction_framework.h"
+
+#ifndef SRC_TRACE_REDACTION_MODIFY_H_
+#define SRC_TRACE_REDACTION_MODIFY_H_
+
+namespace perfetto::trace_redaction {
+
+class PidCommModifier {
+ public:
+  virtual ~PidCommModifier();
+  virtual void Modify(const Context& context,
+                      uint64_t ts,
+                      int32_t cpu,
+                      int32_t* pid,
+                      std::string* comm) const = 0;
+};
+
+class ClearComms : public PidCommModifier {
+ public:
+  void Modify(const Context& context,
+              uint64_t ts,
+              int32_t cpu,
+              int32_t* pid,
+              std::string* comm) const override;
+};
+
+class DoNothing : public PidCommModifier {
+ public:
+  void Modify(const Context& context,
+              uint64_t ts,
+              int32_t cpu,
+              int32_t* pid,
+              std::string* comm) const override;
+};
+
+}  // namespace perfetto::trace_redaction
+
+#endif  // SRC_TRACE_REDACTION_MODIFY_H_
diff --git a/src/trace_redaction/redact_process_events.cc b/src/trace_redaction/redact_process_events.cc
index 0a408dc..3490c74 100644
--- a/src/trace_redaction/redact_process_events.cc
+++ b/src/trace_redaction/redact_process_events.cc
@@ -174,7 +174,7 @@
   shared_comm->assign(comm.data, comm.size);
 
   PERFETTO_DCHECK(modifier_);
-  RETURN_IF_ERROR(modifier_->Modify(context, ts, cpu, &pid, shared_comm));
+  modifier_->Modify(context, ts, cpu, &pid, shared_comm);
 
   auto* message = parent_message->set_sched_process_free();
   message->set_pid(pid);
@@ -233,7 +233,7 @@
   shared_comm->assign(comm.data, comm.size);
 
   PERFETTO_DCHECK(modifier_);
-  RETURN_IF_ERROR(modifier_->Modify(context, ts, cpu, &pid, shared_comm));
+  modifier_->Modify(context, ts, cpu, &pid, shared_comm);
 
   auto* message = parent_message->set_task_newtask();
   message->set_clone_flags(clone_flags);
@@ -297,7 +297,7 @@
   shared_comm->assign(old_comm.data, old_comm.size);
 
   PERFETTO_DCHECK(modifier_);
-  RETURN_IF_ERROR(modifier_->Modify(context, ts, cpu, &noop_pid, shared_comm));
+  modifier_->Modify(context, ts, cpu, &noop_pid, shared_comm);
 
   // Write the old-comm now so shared_comm can be used new-comm.
   message->set_oldcomm(*shared_comm);
@@ -305,7 +305,7 @@
   shared_comm->assign(new_comm.data, new_comm.size);
 
   PERFETTO_DCHECK(modifier_);
-  RETURN_IF_ERROR(modifier_->Modify(context, ts, cpu, &pid, shared_comm));
+  modifier_->Modify(context, ts, cpu, &pid, shared_comm);
 
   message->set_newcomm(*shared_comm);
 
diff --git a/src/trace_redaction/redact_process_events.h b/src/trace_redaction/redact_process_events.h
index 905d66a..42d47e3 100644
--- a/src/trace_redaction/redact_process_events.h
+++ b/src/trace_redaction/redact_process_events.h
@@ -98,7 +98,7 @@
                        protozero::ConstBytes event_bytes,
                        protos::pbzero::FtraceEvent* parent_message) const;
 
-  std::unique_ptr<SchedEventModifier> modifier_;
+  std::unique_ptr<PidCommModifier> modifier_;
 
   std::unique_ptr<PidFilter> filter_;
 };
diff --git a/src/trace_redaction/redact_sched_events.cc b/src/trace_redaction/redact_sched_events.cc
index 87f87a5..3e5dac5 100644
--- a/src/trace_redaction/redact_sched_events.cc
+++ b/src/trace_redaction/redact_sched_events.cc
@@ -106,8 +106,6 @@
 // collection of ftrace event messages) because data in a sched_switch message
 // is needed in order to know if the event should be added to the bundle.
 
-SchedEventModifier::~SchedEventModifier() = default;
-
 base::Status RedactSchedEvents::Transform(const Context& context,
                                           std::string* packet) const {
   PERFETTO_DCHECK(modifier_);
@@ -266,7 +264,7 @@
 
   scratch_str->assign(prev_comm.data, prev_comm.size);
 
-  RETURN_IF_ERROR(modifier_->Modify(context, ts, cpu, &prev_pid, scratch_str));
+  modifier_->Modify(context, ts, cpu, &prev_pid, scratch_str);
 
   message->set_prev_comm(*scratch_str);                // FieldNumber = 1
   message->set_prev_pid(prev_pid);                     // FieldNumber = 2
@@ -275,7 +273,7 @@
 
   scratch_str->assign(next_comm.data, next_comm.size);
 
-  RETURN_IF_ERROR(modifier_->Modify(context, ts, cpu, &next_pid, scratch_str));
+  modifier_->Modify(context, ts, cpu, &next_pid, scratch_str);
 
   message->set_next_comm(*scratch_str);              // FieldNumber = 5
   message->set_next_pid(next_pid);                   // FieldNumber = 6
@@ -332,7 +330,7 @@
 
   scratch_str->assign(comm.data, comm.size);
 
-  RETURN_IF_ERROR(modifier_->Modify(context, ts, cpu, &pid, scratch_str));
+  modifier_->Modify(context, ts, cpu, &pid, scratch_str);
 
   auto message = parent_message->set_sched_waking();
   message->set_comm(*scratch_str);                     // FieldNumber = 1
@@ -445,7 +443,7 @@
 
     scratch_str.assign(comm);
 
-    RETURN_IF_ERROR(modifier_->Modify(context, ts, cpu, &pid, &scratch_str));
+    modifier_->Modify(context, ts, cpu, &pid, &scratch_str);
 
     auto found = intern_table->Push(scratch_str.data(), scratch_str.size());
 
@@ -625,29 +623,4 @@
   return base::OkStatus();
 }
 
-// Switch event transformation: Clear the comm value if the thread/process is
-// not part of the target packet.
-base::Status ClearComms::Modify(const Context& context,
-                                uint64_t ts,
-                                int32_t,
-                                int32_t* pid,
-                                std::string* comm) const {
-  PERFETTO_DCHECK(pid);
-  PERFETTO_DCHECK(comm);
-
-  if (!context.timeline->PidConnectsToUid(ts, *pid, *context.package_uid)) {
-    comm->clear();
-  }
-
-  return base::OkStatus();
-}
-
-base::Status DoNothing::Modify(const Context&,
-                               uint64_t,
-                               int32_t,
-                               int32_t*,
-                               std::string*) const {
-  return base::OkStatus();
-}
-
 }  // namespace perfetto::trace_redaction
diff --git a/src/trace_redaction/redact_sched_events.h b/src/trace_redaction/redact_sched_events.h
index 47ff373..59652b3 100644
--- a/src/trace_redaction/redact_sched_events.h
+++ b/src/trace_redaction/redact_sched_events.h
@@ -18,6 +18,7 @@
 #define SRC_TRACE_REDACTION_REDACT_SCHED_EVENTS_H_
 
 #include "src/trace_redaction/filtering.h"
+#include "src/trace_redaction/modify.h"
 #include "src/trace_redaction/trace_redaction_framework.h"
 
 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
@@ -45,16 +46,6 @@
   std::vector<std::string_view> interned_comms_;
 };
 
-class SchedEventModifier {
- public:
-  virtual ~SchedEventModifier();
-  virtual base::Status Modify(const Context& context,
-                              uint64_t ts,
-                              int32_t cpu,
-                              int32_t* pid,
-                              std::string* comm) const = 0;
-};
-
 class RedactSchedEvents : public TransformPrimitive {
  public:
   base::Status Transform(const Context& context,
@@ -121,28 +112,10 @@
       protos::pbzero::FtraceEventBundle::CompactSched* compact_sched_message)
       const;
 
-  std::unique_ptr<SchedEventModifier> modifier_;
+  std::unique_ptr<PidCommModifier> modifier_;
   std::unique_ptr<PidFilter> filter_;
 };
 
-class ClearComms : public SchedEventModifier {
- public:
-  base::Status Modify(const Context& context,
-                      uint64_t ts,
-                      int32_t cpu,
-                      int32_t* pid,
-                      std::string* comm) const override;
-};
-
-class DoNothing : public SchedEventModifier {
- public:
-  base::Status Modify(const Context& context,
-                      uint64_t ts,
-                      int32_t cpu,
-                      int32_t* pid,
-                      std::string* comm) const override;
-};
-
 }  // namespace perfetto::trace_redaction
 
 #endif  // SRC_TRACE_REDACTION_REDACT_SCHED_EVENTS_H_
diff --git a/src/trace_redaction/redact_sched_events_unittest.cc b/src/trace_redaction/redact_sched_events_unittest.cc
index d3205a2..b335231 100644
--- a/src/trace_redaction/redact_sched_events_unittest.cc
+++ b/src/trace_redaction/redact_sched_events_unittest.cc
@@ -54,18 +54,19 @@
 constexpr auto kCommNone = "";
 
 template <int32_t new_pid>
-class ChangePidTo : public SchedEventModifier {
+class ChangePidTo : public PidCommModifier {
  public:
-  base::Status Modify(const Context& context,
-                      uint64_t ts,
-                      int32_t,
-                      int32_t* pid,
-                      std::string*) const override {
+  void Modify(const Context& context,
+              uint64_t ts,
+              int32_t,
+              int32_t* pid,
+              std::string*) const override {
+    PERFETTO_DCHECK(context.timeline);
+    PERFETTO_DCHECK(context.package_uid.has_value());
+    PERFETTO_DCHECK(pid);
     if (!context.timeline->PidConnectsToUid(ts, *pid, *context.package_uid)) {
       *pid = new_pid;
     }
-
-    return base::OkStatus();
   }
 };
 }  // namespace