1 /* 2 * Copyright 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 package android.adservices.ondevicepersonalization; 18 19 import android.os.OutcomeReceiver; 20 21 import androidx.annotation.NonNull; 22 23 import java.lang.reflect.InvocationTargetException; 24 25 public class IsolatedServiceExceptionSafetyTestImpl extends IsolatedService { 26 27 @NonNull 28 @Override onRequest(@onNull RequestToken requestToken)29 public IsolatedWorker onRequest(@NonNull RequestToken requestToken) { 30 return new SampleWorker(); 31 } 32 33 static class SampleWorker implements IsolatedWorker { 34 35 @Override onExecute( @onNull ExecuteInput input, @NonNull OutcomeReceiver<ExecuteOutput, IsolatedServiceException> receiver)36 public void onExecute( 37 @NonNull ExecuteInput input, 38 @NonNull OutcomeReceiver<ExecuteOutput, IsolatedServiceException> receiver) { 39 String exName = input.getAppParams().getString("ex", "n/a"); 40 constructAndThrowException(exName); 41 } 42 43 @Override onDownloadCompleted( @onNull DownloadCompletedInput input, @NonNull OutcomeReceiver<DownloadCompletedOutput, IsolatedServiceException> receiver)44 public void onDownloadCompleted( 45 @NonNull DownloadCompletedInput input, 46 @NonNull 47 OutcomeReceiver<DownloadCompletedOutput, IsolatedServiceException> 48 receiver) { 49 KeyValueStore downloadedContents = input.getDownloadedContents(); 50 String exName = new String(downloadedContents.get("ex")); 51 constructAndThrowException(exName); 52 } 53 54 @Override onRender( @onNull RenderInput input, @NonNull OutcomeReceiver<RenderOutput, IsolatedServiceException> receiver)55 public void onRender( 56 @NonNull RenderInput input, 57 @NonNull OutcomeReceiver<RenderOutput, IsolatedServiceException> receiver) { 58 String exName = input.getRenderingConfig().getKeys().get(0); 59 constructAndThrowException(exName); 60 } 61 62 @Override onEvent( @onNull EventInput input, @NonNull OutcomeReceiver<EventOutput, IsolatedServiceException> receiver)63 public void onEvent( 64 @NonNull EventInput input, 65 @NonNull OutcomeReceiver<EventOutput, IsolatedServiceException> receiver) { 66 String exName = input.getParameters().getString("ex", "n/a"); 67 constructAndThrowException(exName); 68 } 69 70 @Override onTrainingExamples( @onNull TrainingExamplesInput input, @NonNull OutcomeReceiver<TrainingExamplesOutput, IsolatedServiceException> receiver)71 public void onTrainingExamples( 72 @NonNull TrainingExamplesInput input, 73 @NonNull 74 OutcomeReceiver<TrainingExamplesOutput, IsolatedServiceException> 75 receiver) { 76 String exName = input.getTaskName(); 77 constructAndThrowException(exName); 78 } 79 80 @Override onWebTrigger( @onNull WebTriggerInput input, @NonNull OutcomeReceiver<WebTriggerOutput, IsolatedServiceException> receiver)81 public void onWebTrigger( 82 @NonNull WebTriggerInput input, 83 @NonNull OutcomeReceiver<WebTriggerOutput, IsolatedServiceException> receiver) { 84 String exName = input.getAppPackageName(); 85 constructAndThrowException(exName); 86 } 87 constructAndThrowException(String exName)88 private static void constructAndThrowException(String exName) { 89 Class<?> aClass; 90 try { 91 aClass = Class.forName(exName); 92 throw (RuntimeException) aClass.getDeclaredConstructor().newInstance(); 93 } catch (ClassNotFoundException 94 | NoSuchMethodException 95 | InvocationTargetException 96 | InstantiationException 97 | IllegalAccessException e) { 98 throw new RuntimeException(e); 99 } 100 } 101 } 102 } 103