1 /* 2 * Copyright (C) 2023 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 static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_ERROR_REPORTED__ERROR_CODE__PERSIST_AD_SELECTION_RESULT_PARSING_RESPONSE_DATA_COMPRESSION_NOT_FOUND; 20 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_ERROR_REPORTED__PPAPI_NAME__PERSIST_AD_SELECTION_RESULT; 21 22 import android.adservices.adselection.SellerConfiguration; 23 import android.annotation.NonNull; 24 import android.annotation.Nullable; 25 26 import com.android.adservices.LoggerFactory; 27 import com.android.adservices.errorlogging.ErrorLogUtil; 28 import com.android.adservices.service.stats.AdServicesLogger; 29 import com.android.internal.annotations.VisibleForTesting; 30 import com.android.internal.util.Preconditions; 31 32 import com.google.common.collect.ImmutableList; 33 34 import java.util.Objects; 35 36 /** Factory for {@link AuctionServerPayloadFormatter} and {@link AuctionServerPayloadExtractor} */ 37 public class AuctionServerPayloadFormatterFactory { 38 private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger(); 39 40 @VisibleForTesting 41 static final String NO_IMPLEMENTATION_FOUND = "No %s implementation found for version %s"; 42 43 /** Returns an implementation for the {@link AuctionServerPayloadFormatter} */ 44 @NonNull createPayloadFormatter( int version, @NonNull ImmutableList<Integer> availableBucketSizes, @Nullable SellerConfiguration sellerConfiguration)45 public static AuctionServerPayloadFormatter createPayloadFormatter( 46 int version, 47 @NonNull ImmutableList<Integer> availableBucketSizes, 48 @Nullable SellerConfiguration sellerConfiguration) { 49 Preconditions.checkCollectionNotEmpty(availableBucketSizes, "available bucket sizes."); 50 51 if (version == AuctionServerPayloadFormatterV0.VERSION) { 52 sLogger.v("Using AuctionServerPayloadFormatterV0 formatter"); 53 return new AuctionServerPayloadFormatterV0(availableBucketSizes); 54 } else if (version == AuctionServerPayloadFormatterExcessiveMaxSize.VERSION) { 55 sLogger.v("Using AuctionServerPayloadFormatterExcessiveMaxSize formatter"); 56 return new AuctionServerPayloadFormatterExcessiveMaxSize(); 57 } else if (version == AuctionServerPayloadFormatterExactSize.VERSION) { 58 if (Objects.nonNull(sellerConfiguration)) { 59 sLogger.v("Using AuctionServerPayloadFormatterExactSize formatter"); 60 return new AuctionServerPayloadFormatterExactSize( 61 sellerConfiguration.getMaximumPayloadSizeBytes()); 62 } else { 63 sLogger.v("Using AuctionServerPayloadFormatterExcessiveMaxSize formatter"); 64 return new AuctionServerPayloadFormatterExcessiveMaxSize(); 65 } 66 } 67 68 String errMsg = 69 String.format( 70 NO_IMPLEMENTATION_FOUND, 71 AuctionServerPayloadFormatter.class.getName(), 72 version); 73 sLogger.e(errMsg); 74 throw new IllegalArgumentException(errMsg); 75 } 76 77 /** Returns an implementation for the {@link AuctionServerPayloadExtractor} */ 78 @NonNull createPayloadExtractor( int version, AdServicesLogger adServicesLogger)79 public static AuctionServerPayloadExtractor createPayloadExtractor( 80 int version, AdServicesLogger adServicesLogger) { 81 if (version == AuctionServerPayloadFormatterV0.VERSION) { 82 // Extract data does not need bucket size list nor payload metrics feature 83 return new AuctionServerPayloadFormatterV0(ImmutableList.of()); 84 } else if (version == AuctionServerPayloadFormatterExcessiveMaxSize.VERSION) { 85 return new AuctionServerPayloadFormatterExcessiveMaxSize(); 86 } else if (version == AuctionServerPayloadFormatterExactSize.VERSION) { 87 // Extract data not need target size 88 return new AuctionServerPayloadFormatterExactSize(/* targetPayloadSizeBytes= */ 0); 89 } 90 91 String errMsg = 92 String.format( 93 NO_IMPLEMENTATION_FOUND, 94 AuctionServerPayloadExtractor.class.getName(), 95 version); 96 sLogger.e(errMsg); 97 IllegalArgumentException exception = new IllegalArgumentException(errMsg); 98 ErrorLogUtil.e( 99 exception, 100 AD_SERVICES_ERROR_REPORTED__ERROR_CODE__PERSIST_AD_SELECTION_RESULT_PARSING_RESPONSE_DATA_COMPRESSION_NOT_FOUND, 101 AD_SERVICES_ERROR_REPORTED__PPAPI_NAME__PERSIST_AD_SELECTION_RESULT); 102 throw exception; 103 } 104 } 105