1 /* 2 * Copyright (C) 2022 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.common.cache; 18 19 import android.content.Context; 20 21 import com.android.adservices.service.Flags; 22 import com.android.adservices.service.common.BinderFlagReader; 23 24 import java.net.URL; 25 import java.util.List; 26 import java.util.Map; 27 28 /** A factory that creates an implementation of {@link HttpCache} as needed */ 29 public class CacheProviderFactory { 30 31 /** 32 * @param context Application context 33 * @param flags Phenotype flags 34 * @return an implementation of {@link HttpCache} as needed based on the flags 35 */ create(Context context, Flags flags)36 public static HttpCache create(Context context, Flags flags) { 37 CacheEntryDao cacheEntryDao = CacheDatabase.getInstance(context).getCacheEntryDao(); 38 if (BinderFlagReader.readFlag(flags::getFledgeHttpCachingEnabled) 39 && cacheEntryDao != null) { 40 return new FledgeHttpCache( 41 cacheEntryDao, 42 flags.getFledgeHttpCacheMaxAgeSeconds(), 43 flags.getFledgeHttpCacheMaxEntries()); 44 } else { 45 return new NoOpCache(); 46 } 47 } 48 49 /** @return a {@link NoOpCache} version of {@link HttpCache} */ createNoOpCache()50 public static HttpCache createNoOpCache() { 51 return new NoOpCache(); 52 } 53 54 /** 55 * This cache is intended to be no-op and empty and used in scenarios where is caching is not 56 * really needed. This cache can be plugged into clients and can be swapped by specific 57 * implementation of {@link HttpCache} based on the caching use case. 58 */ 59 static class NoOpCache implements HttpCache { 60 /** gets nothing from cache, null */ 61 @Override get(URL url)62 public DBCacheEntry get(URL url) { 63 return null; 64 } 65 66 /** puts nothing into the cache */ 67 @Override put( URL url, String body, Map<String, List<String>> requestPropertiesMap, Map<String, List<String>> responseHeaders)68 public void put( 69 URL url, 70 String body, 71 Map<String, List<String>> requestPropertiesMap, 72 Map<String, List<String>> responseHeaders) {} 73 74 /** @return 0 */ 75 @Override getCachedEntriesCount()76 public long getCachedEntriesCount() { 77 return 0; 78 } 79 80 /** @return 0 */ 81 @Override getHitCount()82 public long getHitCount() { 83 return 0; 84 } 85 86 /** @return 0 */ 87 @Override getRequestCount()88 public long getRequestCount() { 89 return 0; 90 } 91 92 /** deletes nothing as there is nothing to delete */ 93 @Override delete()94 public void delete() {} 95 96 /** cleans up nothing as there is nothing to delete */ 97 @Override cleanUp()98 public void cleanUp() {} 99 100 /** no observers needed */ 101 @Override addObserver(CacheObserver observer)102 public void addObserver(CacheObserver observer) {} 103 104 /** no observers need to be notified */ 105 @Override notifyObservers(CacheEventType cacheEvent)106 public void notifyObservers(CacheEventType cacheEvent) {} 107 } 108 } 109