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 package com.android.internal.net.ipsec.ike; 18 19 import android.app.AlarmManager; 20 import android.content.Context; 21 import android.net.IpSecManager; 22 import android.net.ipsec.ike.ChildSessionCallback; 23 import android.net.ipsec.ike.ChildSessionParams; 24 import android.os.Looper; 25 26 import com.android.internal.annotations.VisibleForTesting; 27 import com.android.internal.net.ipsec.ike.ChildSessionStateMachine.IChildSessionSmCallback; 28 import com.android.internal.net.ipsec.ike.utils.IpSecSpiGenerator; 29 import com.android.internal.net.ipsec.ike.utils.RandomnessFactory; 30 31 import java.util.concurrent.Executor; 32 33 /** Package private factory for making ChildSessionStateMachine. */ 34 // TODO: Make it a inner Creator class of ChildSessionStateMachine 35 final class ChildSessionStateMachineFactory { 36 37 private static IChildSessionFactoryHelper sChildSessionHelper = new ChildSessionFactoryHelper(); 38 39 /** Package private. */ makeChildSessionStateMachine( Looper looper, Context context, int ikeSessionUniqueId, AlarmManager alarmManager, RandomnessFactory randomFactory, IpSecSpiGenerator ipSecSpiGenerator, ChildSessionParams sessionParams, Executor userCbExecutor, ChildSessionCallback userCallbacks, IChildSessionSmCallback childSmCallback)40 static ChildSessionStateMachine makeChildSessionStateMachine( 41 Looper looper, 42 Context context, 43 int ikeSessionUniqueId, 44 AlarmManager alarmManager, 45 RandomnessFactory randomFactory, 46 IpSecSpiGenerator ipSecSpiGenerator, 47 ChildSessionParams sessionParams, 48 Executor userCbExecutor, 49 ChildSessionCallback userCallbacks, 50 IChildSessionSmCallback childSmCallback) { 51 return sChildSessionHelper.makeChildSessionStateMachine( 52 looper, 53 context, 54 ikeSessionUniqueId, 55 alarmManager, 56 randomFactory, 57 ipSecSpiGenerator, 58 sessionParams, 59 userCbExecutor, 60 userCallbacks, 61 childSmCallback); 62 } 63 64 @VisibleForTesting setChildSessionFactoryHelper(IChildSessionFactoryHelper helper)65 static void setChildSessionFactoryHelper(IChildSessionFactoryHelper helper) { 66 sChildSessionHelper = helper; 67 } 68 69 /** 70 * IChildSessionFactoryHelper provides a package private interface for constructing 71 * ChildSessionStateMachine. 72 * 73 * <p>IChildSessionFactoryHelper exists so that the interface is injectable for testing. 74 */ 75 interface IChildSessionFactoryHelper { makeChildSessionStateMachine( Looper looper, Context context, int ikeSessionUniqueId, AlarmManager alarmManager, RandomnessFactory randomFactory, IpSecSpiGenerator ipSecSpiGenerator, ChildSessionParams sessionParams, Executor userCbExecutor, ChildSessionCallback userCallbacks, IChildSessionSmCallback childSmCallback)76 ChildSessionStateMachine makeChildSessionStateMachine( 77 Looper looper, 78 Context context, 79 int ikeSessionUniqueId, 80 AlarmManager alarmManager, 81 RandomnessFactory randomFactory, 82 IpSecSpiGenerator ipSecSpiGenerator, 83 ChildSessionParams sessionParams, 84 Executor userCbExecutor, 85 ChildSessionCallback userCallbacks, 86 IChildSessionSmCallback childSmCallback); 87 } 88 89 /** 90 * ChildSessionFactoryHelper implements a method for constructing ChildSessionStateMachine. 91 * 92 * <p>Package private. 93 */ 94 static class ChildSessionFactoryHelper implements IChildSessionFactoryHelper { makeChildSessionStateMachine( Looper looper, Context context, int ikeSessionUniqueId, AlarmManager alarmManager, RandomnessFactory randomFactory, IpSecSpiGenerator ipSecSpiGenerator, ChildSessionParams sessionParams, Executor userCbExecutor, ChildSessionCallback userCallbacks, IChildSessionSmCallback childSmCallback)95 public ChildSessionStateMachine makeChildSessionStateMachine( 96 Looper looper, 97 Context context, 98 int ikeSessionUniqueId, 99 AlarmManager alarmManager, 100 RandomnessFactory randomFactory, 101 IpSecSpiGenerator ipSecSpiGenerator, 102 ChildSessionParams sessionParams, 103 Executor userCbExecutor, 104 ChildSessionCallback userCallbacks, 105 IChildSessionSmCallback childSmCallback) { 106 ChildSessionStateMachine childSession = 107 new ChildSessionStateMachine( 108 looper, 109 context, 110 ikeSessionUniqueId, 111 alarmManager, 112 randomFactory, 113 (IpSecManager) context.getSystemService(Context.IPSEC_SERVICE), 114 ipSecSpiGenerator, 115 sessionParams, 116 userCbExecutor, 117 userCallbacks, 118 childSmCallback); 119 childSession.start(); 120 return childSession; 121 } 122 } 123 } 124