1 /* 2 * Copyright (C) 2020 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.server.vcn; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 import android.net.IpSecTransformState; 22 import android.net.vcn.FeatureFlags; 23 import android.net.vcn.FeatureFlagsImpl; 24 import android.os.Looper; 25 26 import java.util.Objects; 27 28 /** 29 * A simple class to pass around context information. 30 * 31 * @hide 32 */ 33 public class VcnContext { 34 @NonNull private final Context mContext; 35 @NonNull private final Looper mLooper; 36 @NonNull private final VcnNetworkProvider mVcnNetworkProvider; 37 @NonNull private final FeatureFlags mFeatureFlags; 38 private final boolean mIsInTestMode; 39 VcnContext( @onNull Context context, @NonNull Looper looper, @NonNull VcnNetworkProvider vcnNetworkProvider, boolean isInTestMode)40 public VcnContext( 41 @NonNull Context context, 42 @NonNull Looper looper, 43 @NonNull VcnNetworkProvider vcnNetworkProvider, 44 boolean isInTestMode) { 45 mContext = Objects.requireNonNull(context, "Missing context"); 46 mLooper = Objects.requireNonNull(looper, "Missing looper"); 47 mVcnNetworkProvider = Objects.requireNonNull(vcnNetworkProvider, "Missing networkProvider"); 48 mIsInTestMode = isInTestMode; 49 50 // Auto-generated class 51 mFeatureFlags = new FeatureFlagsImpl(); 52 } 53 54 @NonNull getContext()55 public Context getContext() { 56 return mContext; 57 } 58 59 @NonNull getLooper()60 public Looper getLooper() { 61 return mLooper; 62 } 63 64 @NonNull getVcnNetworkProvider()65 public VcnNetworkProvider getVcnNetworkProvider() { 66 return mVcnNetworkProvider; 67 } 68 isInTestMode()69 public boolean isInTestMode() { 70 return mIsInTestMode; 71 } 72 isFlagNetworkMetricMonitorEnabled()73 public boolean isFlagNetworkMetricMonitorEnabled() { 74 return mFeatureFlags.networkMetricMonitor(); 75 } 76 isFlagIpSecTransformStateEnabled()77 public boolean isFlagIpSecTransformStateEnabled() { 78 // TODO: b/328844044: Ideally this code should gate the behavior by checking the 79 // android.net.platform.flags.ipsec_transform_state flag but that flag is not accessible 80 // right now. We should either update the code when the flag is accessible or remove the 81 // legacy behavior after VIC SDK finalization 82 try { 83 new IpSecTransformState.Builder(); 84 return true; 85 } catch (Exception e) { 86 return false; 87 } 88 } 89 90 @NonNull getFeatureFlags()91 public FeatureFlags getFeatureFlags() { 92 return mFeatureFlags; 93 } 94 isFlagSafeModeTimeoutConfigEnabled()95 public boolean isFlagSafeModeTimeoutConfigEnabled() { 96 return mFeatureFlags.safeModeTimeoutConfig(); 97 } 98 99 /** 100 * Verifies that the caller is running on the VcnContext Thread. 101 * 102 * @throwsIllegalStateException if the caller is not running on the VcnContext Thread. 103 */ ensureRunningOnLooperThread()104 public void ensureRunningOnLooperThread() { 105 if (getLooper().getThread() != Thread.currentThread()) { 106 throw new IllegalStateException("Not running on VcnMgmtSvc thread"); 107 } 108 } 109 } 110