Validate triggers when added to service

Triggers are currently validated on the manager side, validate in
service as well to ensure invalid triggers cannot be added.

Test: presubmit
Bug: 425360073
Flag: EXEMPT - bugfix
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:e57d3597f0eb2e0bdabbf174cf57b41caee0627a)
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:57d3f4da1ff871e18db8795fb2dd2e9857422884
Merged-In: I422171ef7cb8519f5c138f1b39ae9a008a0df7ae
Change-Id: I422171ef7cb8519f5c138f1b39ae9a008a0df7ae
diff --git a/service/java/com/android/os/profiling/ProfilingService.java b/service/java/com/android/os/profiling/ProfilingService.java
index 8d9cbf1..4e017f5 100644
--- a/service/java/com/android/os/profiling/ProfilingService.java
+++ b/service/java/com/android/os/profiling/ProfilingService.java
@@ -167,7 +167,12 @@
     public String mSystemTriggeredTraceUniqueSessionName = null;
     private long mLastStartedSystemTriggeredTraceMs = 0;
 
-    // Map of uid + package name to a sparse array of trigger objects.
+    /**
+     * Map of uid + package name to a sparse array of trigger objects.
+     *
+     * Adding items to this data structure must only be done using
+     * {@link #addTrigger(ProfilingTriggerData, boolean)} which validates the added triggers.
+     */
     @VisibleForTesting
     public ProcessMap<SparseArray<ProfilingTriggerData>> mAppTriggers = new ProcessMap<>();
     @VisibleForTesting
@@ -590,7 +595,13 @@
         // Populate in memory app triggers store
         for (int i = 0; i < wrapper.getTriggersCount(); i++) {
             ProfilingTriggersWrapper.ProfilingTrigger triggerProto = wrapper.getTriggers(i);
-            addTrigger(new ProfilingTriggerData(triggerProto), false);
+            try {
+                addTrigger(new ProfilingTriggerData(triggerProto), false);
+            } catch (IllegalArgumentException e) {
+                // If this exception is thrown, then a trigger was added with an unsupported type.
+                // Ignore and continue.
+                if (DEBUG) Log.w(TAG, "Trigger loaded from storage with invalid type", e);
+            }
         }
 
         mAppTriggersLoaded = true;
@@ -1816,6 +1827,11 @@
             return;
         }
 
+        if (!ProfilingTrigger.isValidRequestTriggerType(trigger.getTriggerType())) {
+            Log.w(TAG, "Attempted to add invalid profiling trigger.");
+            throw new IllegalArgumentException("Trigger type is not supported");
+        }
+
         SparseArray<ProfilingTriggerData> perProcessTriggers = mAppTriggers.get(
                 trigger.getPackageName(), trigger.getUid());