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.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.net.Uri; 23 24 import com.android.adservices.LoggerFactory; 25 import com.android.adservices.service.common.httpclient.AdServicesHttpClientRequest; 26 import com.android.internal.annotations.VisibleForTesting; 27 28 import com.google.common.collect.ImmutableBiMap; 29 import com.google.common.collect.ImmutableList; 30 import com.google.common.collect.ImmutableMap; 31 32 import java.lang.annotation.Retention; 33 import java.lang.annotation.RetentionPolicy; 34 import java.util.List; 35 import java.util.Map; 36 import java.util.Optional; 37 import java.util.stream.Collectors; 38 39 /** Provides JS version related payload helper methods. */ 40 public class JsVersionHelper { 41 private static final LoggerFactory.Logger LOGGER = LoggerFactory.getFledgeLogger(); 42 public static final Long DEFAULT_JS_VERSION_IF_ABSENT = 0L; 43 44 @VisibleForTesting 45 public static final String VERSION_HEADER_NAME_FORMAT = "X_FLEDGE_%s_VERSION"; 46 47 /** JS payload name. */ 48 @Retention(RetentionPolicy.SOURCE) 49 @IntDef( 50 prefix = "JS_PAYLOAD_TYPE_", 51 value = { 52 JS_PAYLOAD_TYPE_BUYER_BIDDING_LOGIC_JS, 53 }) 54 public @interface JsPayloadType {} 55 56 public static final int JS_PAYLOAD_TYPE_BUYER_BIDDING_LOGIC_JS = 1; 57 58 @VisibleForTesting 59 static final String JS_PAYLOAD_NAME_BUYER_BIDDING_LOGIC_JS = "BUYER_BIDDING_LOGIC"; 60 61 @VisibleForTesting 62 static final ImmutableBiMap<Integer, String> JS_PAYLOAD_TYPE_HEADER_NAME_MAP = 63 ImmutableBiMap.<Integer, String>builder() 64 .put( 65 JS_PAYLOAD_TYPE_BUYER_BIDDING_LOGIC_JS, 66 String.format( 67 VERSION_HEADER_NAME_FORMAT, 68 JS_PAYLOAD_NAME_BUYER_BIDDING_LOGIC_JS)) 69 .build(); 70 71 /** Returns the URI with appended version query parameter. */ getRequestWithVersionHeader( @onNull Uri uri, @NonNull Map<Integer, Long> jsVersionMap, boolean useCache)72 public static AdServicesHttpClientRequest getRequestWithVersionHeader( 73 @NonNull Uri uri, @NonNull Map<Integer, Long> jsVersionMap, boolean useCache) { 74 ImmutableMap<String, String> requestProperties = 75 ImmutableMap.copyOf( 76 jsVersionMap.entrySet().stream() 77 .collect( 78 Collectors.toMap( 79 e -> getVersionHeaderName(e.getKey()), 80 e -> Long.toString(e.getValue())))); 81 return AdServicesHttpClientRequest.builder() 82 .setUri(uri) 83 .setRequestProperties(requestProperties) 84 .setUseCache(useCache) 85 .setResponseHeaderKeys(requestProperties.keySet()) 86 .build(); 87 } 88 89 /** Returns a payload header contains the js version attribute. */ constructVersionHeader( @sPayloadType int jsPayloadType, Long version)90 public static ImmutableMap<String, List<String>> constructVersionHeader( 91 @JsPayloadType int jsPayloadType, Long version) { 92 final long nonNullVersion = Optional.ofNullable(version).orElse(0L); 93 return ImmutableMap.of( 94 getVersionHeaderName(jsPayloadType), 95 ImmutableList.of(Long.toString(nonNullVersion))); 96 } 97 98 @VisibleForTesting getVersionHeaderName(@sPayloadType int jsPayloadType)99 public static String getVersionHeaderName(@JsPayloadType int jsPayloadType) { 100 return JS_PAYLOAD_TYPE_HEADER_NAME_MAP.get(jsPayloadType); 101 } 102 103 @Nullable getJsPayloadType(@onNull String versionHeaderName)104 static Integer getJsPayloadType(@NonNull String versionHeaderName) { 105 return JS_PAYLOAD_TYPE_HEADER_NAME_MAP.inverse().get(versionHeaderName); 106 } 107 } 108