summaryrefslogtreecommitdiff
path: root/java/com/google/android/libraries/mobiledatadownload/file/integration/downloader/DownloadDestinationOpener.java
blob: 1a3322a555a2c2eb2b9136a4d83341d3d104c1f6 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * 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.file.integration.downloader;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import android.net.Uri;
import com.google.android.downloader.DownloadDestination;
import com.google.android.downloader.DownloadMetadata;
import com.google.android.libraries.mobiledatadownload.file.OpenContext;
import com.google.android.libraries.mobiledatadownload.file.Opener;
import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
import com.google.android.libraries.mobiledatadownload.file.common.ReleasableResource;
import com.google.android.libraries.mobiledatadownload.file.common.UnsupportedFileStorageOperation;
import com.google.android.libraries.mobiledatadownload.file.openers.RandomAccessFileOpener;
import com.google.common.base.Optional;
import com.google.common.util.concurrent.ListenableFuture;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * A MobStore Opener for <internal>'s {@link DownloadDestination}.
 *
 * <p>This creates a {@link DownloadDestination} that supports writing data and connects to a shared
 * PDS instance to handle metadata updates.
 *
 * <pre>{@code
 * Downloader downloader = new Downloader(...);
 * DownloadMetadataStore metadataStore = ...;
 * SynchronousFileStorage storage = new SynchronousFileStorage(...);
 *
 * DownloadDestination destination = storage.open(
 *   targetFileUri,
 *   DownloadDestinationOpener.create(metadataStore));
 *
 * downloader.execute(
 *   downloader
 *     .newRequestBuilder(urlToDownload, destination)
 *     .build());
 * }</pre>
 */
public final class DownloadDestinationOpener implements Opener<DownloadDestination> {
  private static final long TIMEOUT_MS = 1000;

  /** Implementation of {@link DownloadDestination} created by the opener. */
  private static final class DownloadDestinationImpl implements DownloadDestination {
    // We need to touch two underlying files (metadata from DownloadMetadataStore and the downloaded
    // file). Define a lock to keep the access of these files synchronized.
    private final Object lock = new Object();

    private final Uri onDeviceUri;
    private final DownloadMetadataStore metadataStore;
    private final SynchronousFileStorage fileStorage;

    private DownloadDestinationImpl(
            Uri onDeviceUri, SynchronousFileStorage fileStorage, DownloadMetadataStore metadataStore) {
      this.onDeviceUri = onDeviceUri;
      this.metadataStore = metadataStore;
      this.fileStorage = fileStorage;
    }

    @Override
    public long numExistingBytes() throws IOException {
      return fileStorage.fileSize(onDeviceUri);
    }

    @Override
    public DownloadMetadata readMetadata() throws IOException {
      synchronized (lock) {
        Optional<DownloadMetadata> existingMetadata =
                blockingGet(metadataStore.read(onDeviceUri), "Failed to read metadata.");

        // Return existing metadata, or a new instance.
        return existingMetadata.or(DownloadMetadata::create);
      }
    }

    @Override
    public WritableByteChannel openByteChannel(long byteOffset, DownloadMetadata metadata)
            throws IOException {
      // Ensure that metadata is not null
      checkArgument(metadata != null, "Received null metadata to store");
      // Check that offset is in range
      long fileSize = numExistingBytes();
      checkArgument(
              byteOffset >= 0 && byteOffset <= fileSize,
              "Offset for write (%s) out of range of existing file size (%s bytes)",
              byteOffset,
              fileSize);

      synchronized (lock) {
        // Update metadata first.
        blockingGet(metadataStore.upsert(onDeviceUri, metadata), "Failed to update metadata.");

        // Use ReleasableResource to ensure channel is setup properly before returning it.
        try (ReleasableResource<RandomAccessFile> file =
                     ReleasableResource.create(
                             fileStorage.open(onDeviceUri, RandomAccessFileOpener.createForReadWrite()))) {
          // Get channel and seek to correct offset.
          FileChannel channel = file.get().getChannel();
          channel.position(byteOffset);

          // Release ownership -- caller is responsible for closing the channel.
          file.release();

          return channel;
        }
      }
    }

    @Override
    public void clear() throws IOException {
      synchronized (lock) {
        // clear metadata and delete file.
        blockingGet(metadataStore.delete(onDeviceUri), "Failed to clear metadata.");

        fileStorage.deleteFile(onDeviceUri);
      }
    }

    /**
     * Helper method for async call error handling.
     *
     * <p>Exceptions due to an async call failure are handled and wrapped in an IOException.
     */
    private static <V> V blockingGet(ListenableFuture<V> future, String errorMessage)
            throws IOException {
      try {
        return future.get(TIMEOUT_MS, MILLISECONDS);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IOException(errorMessage, e.getCause());
      } catch (ExecutionException e) {
        throw new IOException(errorMessage, e.getCause());
      } catch (TimeoutException | CancellationException e) {
        throw new IOException(errorMessage, e);
      }
    }
  }

  private final DownloadMetadataStore metadataStore;

  private DownloadDestinationOpener(DownloadMetadataStore metadataStore) {
    this.metadataStore = metadataStore;
  }

  @Override
  public DownloadDestination open(OpenContext openContext) throws IOException {
    if (openContext.hasTransforms()) {
      throw new UnsupportedFileStorageOperation(
              "Transforms are not supported by this Opener: " + openContext.originalUri());
    }

    // Check whether or not the file uri is a directory.
    if (openContext.storage().isDirectory(openContext.originalUri())) {
      throw new IOException(
              new IllegalArgumentException("Requested file download is already a directory."));
    }

    return new DownloadDestinationImpl(
            openContext.originalUri(), openContext.storage(), metadataStore);
  }

  public static DownloadDestinationOpener create(DownloadMetadataStore metadataStore) {
    return new DownloadDestinationOpener(metadataStore);
  }
}