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 */ 36 // TODO(b/311183933): Remove passed in Context from static method. 37 @SuppressWarnings("AvoidStaticContext") create(Context context, Flags flags)38 public static HttpCache create(Context context, Flags flags) { 39 CacheEntryDao cacheEntryDao = CacheDatabase.getInstance().getCacheEntryDao(); 40 if (BinderFlagReader.readFlag(flags::getFledgeHttpCachingEnabled) 41 && cacheEntryDao != null) { 42 return new FledgeHttpCache( 43 cacheEntryDao, 44 flags.getFledgeHttpCacheMaxAgeSeconds(), 45 flags.getFledgeHttpCacheMaxEntries()); 46 } else { 47 return new NoOpCache(); 48 } 49 } 50 51 /** @return a {@link NoOpCache} version of {@link HttpCache} */ createNoOpCache()52 public static HttpCache createNoOpCache() { 53 return new NoOpCache(); 54 } 55 56 /** 57 * This cache is intended to be no-op and empty and used in scenarios where is caching is not 58 * really needed. This cache can be plugged into clients and can be swapped by specific 59 * implementation of {@link HttpCache} based on the caching use case. 60 */ 61 static class NoOpCache implements HttpCache { 62 /** gets nothing from cache, null */ 63 @Override get(URL url)64 public DBCacheEntry get(URL url) { 65 return null; 66 } 67 68 /** puts nothing into the cache */ 69 @Override put( URL url, String body, Map<String, List<String>> requestPropertiesMap, Map<String, List<String>> responseHeaders)70 public void put( 71 URL url, 72 String body, 73 Map<String, List<String>> requestPropertiesMap, 74 Map<String, List<String>> responseHeaders) {} 75 76 /** @return 0 */ 77 @Override getCachedEntriesCount()78 public long getCachedEntriesCount() { 79 return 0; 80 } 81 82 /** @return 0 */ 83 @Override getHitCount()84 public long getHitCount() { 85 return 0; 86 } 87 88 /** @return 0 */ 89 @Override getRequestCount()90 public long getRequestCount() { 91 return 0; 92 } 93 94 /** deletes nothing as there is nothing to delete */ 95 @Override delete()96 public void delete() {} 97 98 /** cleans up nothing as there is nothing to delete */ 99 @Override cleanUp()100 public void cleanUp() {} 101 102 /** no observers needed */ 103 @Override addObserver(CacheObserver observer)104 public void addObserver(CacheObserver observer) {} 105 106 /** no observers need to be notified */ 107 @Override notifyObservers(CacheEventType cacheEvent)108 public void notifyObservers(CacheEventType cacheEvent) {} 109 } 110 } 111