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 android.annotation.NonNull; 20 21 import com.android.adservices.LoggerFactory; 22 import com.android.adservices.service.stats.AdServicesLogger; 23 import com.android.internal.annotations.VisibleForTesting; 24 import com.android.internal.util.Preconditions; 25 26 import com.google.common.collect.ImmutableList; 27 28 /** Factory for {@link AuctionServerPayloadFormatter} and {@link AuctionServerPayloadExtractor} */ 29 public class AuctionServerPayloadFormatterFactory { 30 private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger(); 31 32 @VisibleForTesting 33 static final String NO_IMPLEMENTATION_FOUND = "No %s implementation found for version %s"; 34 35 /** Returns an implementation for the {@link AuctionServerPayloadFormatter} */ 36 @NonNull createPayloadFormatter( int version, @NonNull ImmutableList<Integer> availableBucketSizes)37 public static AuctionServerPayloadFormatter createPayloadFormatter( 38 int version, @NonNull ImmutableList<Integer> availableBucketSizes) { 39 Preconditions.checkCollectionNotEmpty(availableBucketSizes, "available bucket sizes."); 40 41 if (version == AuctionServerPayloadFormatterV0.VERSION) { 42 return new AuctionServerPayloadFormatterV0(availableBucketSizes); 43 } else if (version == AuctionServerPayloadFormatterExcessiveMaxSize.VERSION) { 44 return new AuctionServerPayloadFormatterExcessiveMaxSize(); 45 } 46 47 String errMsg = 48 String.format( 49 NO_IMPLEMENTATION_FOUND, 50 AuctionServerPayloadFormatter.class.getName(), 51 version); 52 sLogger.e(errMsg); 53 throw new IllegalArgumentException(errMsg); 54 } 55 56 /** Returns an implementation for the {@link AuctionServerPayloadExtractor} */ 57 @NonNull createPayloadExtractor( int version, AdServicesLogger adServicesLogger)58 public static AuctionServerPayloadExtractor createPayloadExtractor( 59 int version, AdServicesLogger adServicesLogger) { 60 if (version == AuctionServerPayloadFormatterV0.VERSION) { 61 // Extract data does not need bucket size list nor payload metrics feature 62 return new AuctionServerPayloadFormatterV0(ImmutableList.of()); 63 } else if (version == AuctionServerPayloadFormatterExcessiveMaxSize.VERSION) { 64 return new AuctionServerPayloadFormatterExcessiveMaxSize(); 65 } 66 67 String errMsg = 68 String.format( 69 NO_IMPLEMENTATION_FOUND, 70 AuctionServerPayloadExtractor.class.getName(), 71 version); 72 sLogger.e(errMsg); 73 throw new IllegalArgumentException(errMsg); 74 } 75 } 76