1 /* 2 * Copyright (C) 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 com.android.adservices.service.adselection; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 22 import com.android.adservices.concurrency.AdServicesExecutors; 23 import com.android.adservices.data.adselection.AdSelectionServerDatabase; 24 import com.android.adservices.service.Flags; 25 import com.android.adservices.service.adselection.encryption.AdSelectionEncryptionKeyManager; 26 import com.android.adservices.service.adselection.encryption.ObliviousHttpEncryptor; 27 import com.android.adservices.service.adselection.encryption.ObliviousHttpEncryptorImpl; 28 import com.android.adservices.service.adselection.encryption.ProtectedServersEncryptionConfigManager; 29 import com.android.adservices.service.adselection.encryption.ProtectedServersEncryptionConfigManagerBase; 30 import com.android.adservices.service.common.CoordinatorOriginUriValidator; 31 import com.android.adservices.service.common.cache.CacheProviderFactory; 32 import com.android.adservices.service.common.httpclient.AdServicesHttpsClient; 33 import com.android.adservices.service.stats.AdServicesLoggerImpl; 34 35 /** Factory for {@link MultiCloudSupportStrategy} */ 36 public class MultiCloudSupportStrategyFactory { 37 38 private static class MultiCloudEnabledStrategy implements MultiCloudSupportStrategy { 39 private final String mAllowlist; 40 MultiCloudEnabledStrategy(String allowlist)41 MultiCloudEnabledStrategy(String allowlist) { 42 this.mAllowlist = allowlist; 43 } 44 45 @Override getCoordinatorOriginUriValidator()46 public CoordinatorOriginUriValidator getCoordinatorOriginUriValidator() { 47 return CoordinatorOriginUriValidator.createEnabledInstance(mAllowlist); 48 } 49 50 @Override getEncryptionConfigManager( @onNull Context context, @NonNull Flags flags, @NonNull AdServicesHttpsClient adServicesHttpsClient)51 public ProtectedServersEncryptionConfigManagerBase getEncryptionConfigManager( 52 @NonNull Context context, 53 @NonNull Flags flags, 54 @NonNull AdServicesHttpsClient adServicesHttpsClient) { 55 return new ProtectedServersEncryptionConfigManager( 56 AdSelectionServerDatabase.getInstance(context) 57 .protectedServersEncryptionConfigDao(), 58 flags, 59 adServicesHttpsClient, 60 AdServicesExecutors.getLightWeightExecutor(), 61 AdServicesLoggerImpl.getInstance()); 62 } 63 64 @Override 65 // TODO(b/297025763) : Use process stable flags getObliviousHttpEncryptor( @onNull Context context, @NonNull Flags flags)66 public ObliviousHttpEncryptor getObliviousHttpEncryptor( 67 @NonNull Context context, @NonNull Flags flags) { 68 return new ObliviousHttpEncryptorImpl( 69 getProtectedServersEncryptionConfigManager(context, flags), 70 AdSelectionServerDatabase.getInstance(context).encryptionContextDao(), 71 AdServicesExecutors.getLightWeightExecutor()); 72 } 73 74 private ProtectedServersEncryptionConfigManagerBase getProtectedServersEncryptionConfigManager( @onNull Context context, @NonNull Flags flags)75 getProtectedServersEncryptionConfigManager( 76 @NonNull Context context, @NonNull Flags flags) { 77 AdServicesHttpsClient adServicesHttpsClient = 78 new AdServicesHttpsClient( 79 AdServicesExecutors.getBlockingExecutor(), 80 CacheProviderFactory.create(context, flags)); 81 return new ProtectedServersEncryptionConfigManager( 82 AdSelectionServerDatabase.getInstance(context) 83 .protectedServersEncryptionConfigDao(), 84 flags, 85 adServicesHttpsClient, 86 AdServicesExecutors.getLightWeightExecutor(), 87 AdServicesLoggerImpl.getInstance()); 88 } 89 } 90 91 private static class MultiCloudDisabledStrategy implements MultiCloudSupportStrategy { 92 @Override getCoordinatorOriginUriValidator()93 public CoordinatorOriginUriValidator getCoordinatorOriginUriValidator() { 94 return CoordinatorOriginUriValidator.createDisabledInstance(); 95 } 96 97 @Override 98 // TODO(b/297025763) : Use process stable flags getObliviousHttpEncryptor( @ndroidx.annotation.NonNull Context context, @androidx.annotation.NonNull Flags flags)99 public ObliviousHttpEncryptor getObliviousHttpEncryptor( 100 @androidx.annotation.NonNull Context context, 101 @androidx.annotation.NonNull Flags flags) { 102 return new ObliviousHttpEncryptorImpl( 103 getProtectedServersEncryptionConfigManager(context, flags), 104 AdSelectionServerDatabase.getInstance(context).encryptionContextDao(), 105 AdServicesExecutors.getLightWeightExecutor()); 106 } 107 108 @Override getEncryptionConfigManager( @onNull Context context, @NonNull Flags flags, @NonNull AdServicesHttpsClient adServicesHttpsClient)109 public ProtectedServersEncryptionConfigManagerBase getEncryptionConfigManager( 110 @NonNull Context context, 111 @NonNull Flags flags, 112 @NonNull AdServicesHttpsClient adServicesHttpsClient) { 113 return new AdSelectionEncryptionKeyManager( 114 AdSelectionServerDatabase.getInstance(context).encryptionKeyDao(), 115 flags, 116 adServicesHttpsClient, 117 AdServicesExecutors.getLightWeightExecutor(), 118 AdServicesLoggerImpl.getInstance()); 119 } 120 121 private ProtectedServersEncryptionConfigManagerBase getProtectedServersEncryptionConfigManager( @onNull Context context, @NonNull Flags flags)122 getProtectedServersEncryptionConfigManager( 123 @NonNull Context context, @NonNull Flags flags) { 124 AdServicesHttpsClient adServicesHttpsClient = 125 new AdServicesHttpsClient( 126 AdServicesExecutors.getBlockingExecutor(), 127 CacheProviderFactory.create(context, flags)); 128 return new AdSelectionEncryptionKeyManager( 129 AdSelectionServerDatabase.getInstance(context).encryptionKeyDao(), 130 flags, 131 adServicesHttpsClient, 132 AdServicesExecutors.getLightWeightExecutor(), 133 AdServicesLoggerImpl.getInstance()); 134 } 135 } 136 137 /** Get the strategy corresponding to whether multi-cloud feature is enabled or not. */ getStrategy( boolean multiCloudEnabled, String allowlist)138 public static MultiCloudSupportStrategy getStrategy( 139 boolean multiCloudEnabled, String allowlist) { 140 if (multiCloudEnabled) { 141 return new MultiCloudEnabledStrategy(allowlist); 142 } 143 144 return new MultiCloudDisabledStrategy(); 145 } 146 147 148 } 149