summaryrefslogtreecommitdiff
path: root/java/com/google/android/libraries/mobiledatadownload/internal/SharedFilesMetadata.java
blob: c5e6019c50c0c1d50059005c01c7b5bedaf27d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.libraries.mobiledatadownload.internal;

import android.content.Context;
import com.google.android.libraries.mobiledatadownload.SilentFeedback;
import com.google.android.libraries.mobiledatadownload.internal.util.FileGroupUtil;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.mobiledatadownload.internal.MetadataProto.DataFile;
import com.google.mobiledatadownload.internal.MetadataProto.DataFileGroupInternal.AllowedReaders;
import com.google.mobiledatadownload.internal.MetadataProto.DeltaFile;
import com.google.mobiledatadownload.internal.MetadataProto.NewFileKey;
import com.google.mobiledatadownload.internal.MetadataProto.SharedFile;
import java.util.List;

/** Stores and provides access to shared file metadata. */
public interface SharedFilesMetadata {

  /**
   * Creates a NewFileKey object from the given DataFile, based on the current FileKeyVersion.
   *
   * @param file - a DataFile whose key you wish to construct.
   * @param allowedReadersEnum - {@link AllowedReaders} signifies who has access to the file.
   */
  // TODO(b/127490978): Replace all usage of {@code #createKeyFromDataFile} once all users have
  // been migrated to use only the non-deprecated fields from the returned value.
  public static NewFileKey createKeyFromDataFileForCurrentVersion(
      Context context,
      DataFile file,
      AllowedReaders allowedReadersEnum,
      SilentFeedback silentFeedback) {
    NewFileKey.Builder newFileKeyBuilder = NewFileKey.newBuilder();
    String checksum = FileGroupUtil.getFileChecksum(file);

    switch (Migrations.getCurrentVersion(context, silentFeedback)) {
      case NEW_FILE_KEY:
        newFileKeyBuilder
            .setUrlToDownload(file.getUrlToDownload())
            .setByteSize(file.getByteSize())
            .setChecksum(checksum)
            .setAllowedReaders(allowedReadersEnum);
        break;
      case ADD_DOWNLOAD_TRANSFORM:
        newFileKeyBuilder
            .setUrlToDownload(file.getUrlToDownload())
            .setByteSize(file.getByteSize())
            .setChecksum(checksum)
            .setAllowedReaders(allowedReadersEnum);
        if (file.hasDownloadTransforms()) {
          newFileKeyBuilder.setDownloadTransforms(file.getDownloadTransforms());
        }
        break;
      case USE_CHECKSUM_ONLY:
        newFileKeyBuilder.setChecksum(checksum).setAllowedReaders(allowedReadersEnum);
    }

    return newFileKeyBuilder.build();
  }

  /**
   * Creates a NewFileKey object from the given DataFile.
   *
   * @param file - a DataFile whose key you wish to construct.
   * @param allowedReadersEnum - {@link AllowedReaders} signifies who has access to the file.
   */
  public static NewFileKey createKeyFromDataFile(DataFile file, AllowedReaders allowedReadersEnum) {
    NewFileKey.Builder newFileKeyBuilder =
        NewFileKey.newBuilder()
            .setUrlToDownload(file.getUrlToDownload())
            .setByteSize(file.getByteSize())
            .setChecksum(FileGroupUtil.getFileChecksum(file))
            .setAllowedReaders(allowedReadersEnum);
    if (file.hasDownloadTransforms()) {
      newFileKeyBuilder.setDownloadTransforms(file.getDownloadTransforms());
    }
    return newFileKeyBuilder.build();
  }

  /**
   * Returns a temporary FileKey that can be used to interact with the MddFileDownloader to download
   * a delta file.
   *
   * @param deltaFile - a DeltaFile whose key you wish to construct.
   * @param allowedReadersEnum - {@link AllowedReaders} signifies who has access to the file.
   */
  public static NewFileKey createTempKeyForDeltaFile(
      DeltaFile deltaFile, AllowedReaders allowedReadersEnum) {
    NewFileKey newFileKey =
        NewFileKey.newBuilder()
            .setUrlToDownload(deltaFile.getUrlToDownload())
            .setByteSize(deltaFile.getByteSize())
            .setChecksum(deltaFile.getChecksum())
            .setAllowedReaders(allowedReadersEnum)
            .build();

    return newFileKey;
  }

  /**
   * Makes any changes that should be made before accessing the internal state of this store.
   *
   * <p>Other methods in this class do not call or check if this method was already called before
   * trying to access internal state. It is expected from the caller to call this before anything
   * else.
   *
   * @return a future that resolves to false if init failed, signalling caller to clear internal
   *     storage.
   */
  // TODO(b/124072754): Change to package private once all code is refactored.
  public ListenableFuture<Boolean> init();

  /** Return {@link SharedFile} associated with the given key. */
  public ListenableFuture<SharedFile> read(NewFileKey newFileKey);

  /**
   * Map the key "newFileKey" to the value "sharedFile". Returns a future resolving to true if the
   * operation succeeds, false if it fails.
   */
  public ListenableFuture<Boolean> write(NewFileKey newFileKey, SharedFile sharedFile);

  /**
   * Remove the value stored at "newFileKey". Returns a future resolving to true if the operation
   * succeeds, false if it fails.
   */
  public ListenableFuture<Boolean> remove(NewFileKey newFileKey);

  /** Return all keys in the store. */
  public ListenableFuture<List<NewFileKey>> getAllFileKeys();

  /** Clear the store. */
  public ListenableFuture<Void> clear();
}