1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.internal; 17 18 import android.content.Context; 19 import com.google.android.libraries.mobiledatadownload.SilentFeedback; 20 import com.google.android.libraries.mobiledatadownload.internal.util.FileGroupUtil; 21 import com.google.common.collect.ImmutableMap; 22 import com.google.common.collect.ImmutableSet; 23 import com.google.common.util.concurrent.ListenableFuture; 24 import com.google.mobiledatadownload.internal.MetadataProto.DataFile; 25 import com.google.mobiledatadownload.internal.MetadataProto.DataFileGroupInternal.AllowedReaders; 26 import com.google.mobiledatadownload.internal.MetadataProto.DeltaFile; 27 import com.google.mobiledatadownload.internal.MetadataProto.NewFileKey; 28 import com.google.mobiledatadownload.internal.MetadataProto.SharedFile; 29 import java.util.List; 30 31 /** Stores and provides access to shared file metadata. */ 32 public interface SharedFilesMetadata { 33 34 /** 35 * Creates a NewFileKey object from the given DataFile, based on the current FileKeyVersion. 36 * 37 * @param file - a DataFile whose key you wish to construct. 38 * @param allowedReadersEnum - {@link AllowedReaders} signifies who has access to the file. 39 */ 40 // TODO(b/127490978): Replace all usage of {@code #createKeyFromDataFile} once all users have 41 // been migrated to use only the non-deprecated fields from the returned value. createKeyFromDataFileForCurrentVersion( Context context, DataFile file, AllowedReaders allowedReadersEnum, SilentFeedback silentFeedback)42 public static NewFileKey createKeyFromDataFileForCurrentVersion( 43 Context context, 44 DataFile file, 45 AllowedReaders allowedReadersEnum, 46 SilentFeedback silentFeedback) { 47 NewFileKey.Builder newFileKeyBuilder = NewFileKey.newBuilder(); 48 String checksum = FileGroupUtil.getFileChecksum(file); 49 50 switch (Migrations.getCurrentVersion(context, silentFeedback)) { 51 case NEW_FILE_KEY: 52 newFileKeyBuilder 53 .setUrlToDownload(file.getUrlToDownload()) 54 .setByteSize(file.getByteSize()) 55 .setChecksum(checksum) 56 .setAllowedReaders(allowedReadersEnum); 57 break; 58 case ADD_DOWNLOAD_TRANSFORM: 59 newFileKeyBuilder 60 .setUrlToDownload(file.getUrlToDownload()) 61 .setByteSize(file.getByteSize()) 62 .setChecksum(checksum) 63 .setAllowedReaders(allowedReadersEnum); 64 if (file.hasDownloadTransforms()) { 65 newFileKeyBuilder.setDownloadTransforms(file.getDownloadTransforms()); 66 } 67 break; 68 case USE_CHECKSUM_ONLY: 69 newFileKeyBuilder.setChecksum(checksum).setAllowedReaders(allowedReadersEnum); 70 } 71 72 return newFileKeyBuilder.build(); 73 } 74 75 /** 76 * Creates a NewFileKey object from the given DataFile. 77 * 78 * @param file - a DataFile whose key you wish to construct. 79 * @param allowedReadersEnum - {@link AllowedReaders} signifies who has access to the file. 80 */ createKeyFromDataFile(DataFile file, AllowedReaders allowedReadersEnum)81 public static NewFileKey createKeyFromDataFile(DataFile file, AllowedReaders allowedReadersEnum) { 82 NewFileKey.Builder newFileKeyBuilder = 83 NewFileKey.newBuilder() 84 .setUrlToDownload(file.getUrlToDownload()) 85 .setByteSize(file.getByteSize()) 86 .setChecksum(FileGroupUtil.getFileChecksum(file)) 87 .setAllowedReaders(allowedReadersEnum); 88 if (file.hasDownloadTransforms()) { 89 newFileKeyBuilder.setDownloadTransforms(file.getDownloadTransforms()); 90 } 91 return newFileKeyBuilder.build(); 92 } 93 94 /** 95 * Returns a temporary FileKey that can be used to interact with the MddFileDownloader to download 96 * a delta file. 97 * 98 * @param deltaFile - a DeltaFile whose key you wish to construct. 99 * @param allowedReadersEnum - {@link AllowedReaders} signifies who has access to the file. 100 */ createTempKeyForDeltaFile( DeltaFile deltaFile, AllowedReaders allowedReadersEnum)101 public static NewFileKey createTempKeyForDeltaFile( 102 DeltaFile deltaFile, AllowedReaders allowedReadersEnum) { 103 NewFileKey newFileKey = 104 NewFileKey.newBuilder() 105 .setUrlToDownload(deltaFile.getUrlToDownload()) 106 .setByteSize(deltaFile.getByteSize()) 107 .setChecksum(deltaFile.getChecksum()) 108 .setAllowedReaders(allowedReadersEnum) 109 .build(); 110 111 return newFileKey; 112 } 113 114 /** 115 * Makes any changes that should be made before accessing the internal state of this store. 116 * 117 * <p>Other methods in this class do not call or check if this method was already called before 118 * trying to access internal state. It is expected from the caller to call this before anything 119 * else. 120 * 121 * @return a future that resolves to false if init failed, signalling caller to clear internal 122 * storage. 123 */ 124 // TODO(b/124072754): Change to package private once all code is refactored. init()125 public ListenableFuture<Boolean> init(); 126 127 /** Return {@link SharedFile} associated with the given key. */ read(NewFileKey newFileKey)128 public ListenableFuture<SharedFile> read(NewFileKey newFileKey); 129 130 /** 131 * Returns all known {@link SharedFile}s for the given set of {@link NewFileKey}s 132 * 133 * <p>The map will contain a SharedFile entry if it exists. 134 */ readAll( ImmutableSet<NewFileKey> newFileKeys)135 public ListenableFuture<ImmutableMap<NewFileKey, SharedFile>> readAll( 136 ImmutableSet<NewFileKey> newFileKeys); 137 138 /** 139 * Map the key "newFileKey" to the value "sharedFile". Returns a future resolving to true if the 140 * operation succeeds, false if it fails. 141 */ write(NewFileKey newFileKey, SharedFile sharedFile)142 public ListenableFuture<Boolean> write(NewFileKey newFileKey, SharedFile sharedFile); 143 144 /** 145 * Remove the value stored at "newFileKey". Returns a future resolving to true if the operation 146 * succeeds, false if it fails. 147 */ remove(NewFileKey newFileKey)148 public ListenableFuture<Boolean> remove(NewFileKey newFileKey); 149 150 /** Return all keys in the store. */ getAllFileKeys()151 public ListenableFuture<List<NewFileKey>> getAllFileKeys(); 152 153 /** Clear the store. */ clear()154 public ListenableFuture<Void> clear(); 155 } 156