summaryrefslogtreecommitdiff
path: root/chromium/weblayer/browser/java/org/chromium/weblayer_private/DownloadImpl.java
blob: 694e8839498e1700a10778e9619876b14ad21618 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.weblayer_private;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.RemoteException;
import android.text.TextUtils;

import androidx.core.app.NotificationCompat;

import org.chromium.base.ContentUriUtils;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.components.browser_ui.notifications.NotificationManagerProxy;
import org.chromium.components.browser_ui.notifications.NotificationManagerProxyImpl;
import org.chromium.components.browser_ui.notifications.NotificationMetadata;
import org.chromium.components.browser_ui.notifications.PendingIntentProvider;
import org.chromium.components.browser_ui.notifications.channels.ChannelsInitializer;
import org.chromium.components.browser_ui.util.DownloadUtils;
import org.chromium.weblayer_private.interfaces.APICallException;
import org.chromium.weblayer_private.interfaces.DownloadError;
import org.chromium.weblayer_private.interfaces.DownloadState;
import org.chromium.weblayer_private.interfaces.IClientDownload;
import org.chromium.weblayer_private.interfaces.IDownload;
import org.chromium.weblayer_private.interfaces.IDownloadCallbackClient;
import org.chromium.weblayer_private.interfaces.StrictModeWorkaround;

import java.io.File;
import java.util.HashMap;

/**
 * Implementation of IDownload.
 */
@JNINamespace("weblayer")
public final class DownloadImpl extends IDownload.Stub {
    private static final String DOWNLOADS_PREFIX = "org.chromium.weblayer.downloads";

    // These actions have to be synchronized with the receiver defined in AndroidManifest.xml.
    private static final String OPEN_INTENT = DOWNLOADS_PREFIX + ".OPEN";
    private static final String DELETE_INTENT = DOWNLOADS_PREFIX + ".DELETE";
    private static final String PAUSE_INTENT = DOWNLOADS_PREFIX + ".PAUSE";
    private static final String RESUME_INTENT = DOWNLOADS_PREFIX + ".RESUME";
    private static final String CANCEL_INTENT = DOWNLOADS_PREFIX + ".CANCEL";

    private static final String EXTRA_NOTIFICATION_ID = DOWNLOADS_PREFIX + ".NOTIFICATION_ID";
    private static final String EXTRA_NOTIFICATION_LOCATION =
            DOWNLOADS_PREFIX + ".NOTIFICATION_LOCATION";
    private static final String EXTRA_NOTIFICATION_MIME_TYPE =
            DOWNLOADS_PREFIX + ".NOTIFICATION_MIME_TYPE";
    private static final String EXTRA_NOTIFICATION_PROFILE =
            DOWNLOADS_PREFIX + ".NOTIFICATION_PROFILE";
    // The intent prefix is used as the notification's tag since it's guaranteed not to conflict
    // with intent prefixes used by other subsystems that display notifications.
    private static final String NOTIFICATION_TAG = DOWNLOADS_PREFIX;
    private static final String TAG = "DownloadImpl";

    private final String mProfileName;
    private final IDownloadCallbackClient mClient;
    private final IClientDownload mClientDownload;
    // WARNING: DownloadImpl may outlive the native side, in which case this member is set to 0.
    private long mNativeDownloadImpl;
    private boolean mDisableNotification;

    private final int mNotificationId;
    private static final HashMap<Integer, DownloadImpl> sMap = new HashMap<Integer, DownloadImpl>();

    /**
     * @return a string that prefixes all intents that can be handled by {@link forwardIntent}.
     */
    public static String getIntentPrefix() {
        return DOWNLOADS_PREFIX;
    }

    public static void forwardIntent(
            Context context, Intent intent, ProfileManager profileManager) {
        if (intent.getAction().equals(OPEN_INTENT)) {
            String location = intent.getStringExtra(EXTRA_NOTIFICATION_LOCATION);
            if (TextUtils.isEmpty(location)) {
                Log.d(TAG, "Didn't find location for open intent");
                return;
            }

            String mimeType = intent.getStringExtra(EXTRA_NOTIFICATION_MIME_TYPE);

            Intent openIntent = new Intent(Intent.ACTION_VIEW);
            if (TextUtils.isEmpty(mimeType)) {
                openIntent.setData(getDownloadUri(location));
            } else {
                openIntent.setDataAndType(getDownloadUri(location), mimeType);
            }
            openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            openIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                context.startActivity(openIntent);
            } catch (ActivityNotFoundException ex) {
                // TODO: show some UI that there were no apps to handle this?
            }

            return;
        }

        String profileName = intent.getStringExtra(EXTRA_NOTIFICATION_PROFILE);

        ProfileImpl profile = profileManager.getProfile(profileName);
        if (!profile.areDownloadsInitialized()) {
            profile.addDownloadNotificationIntent(intent);
        } else {
            handleIntent(intent);
        }
    }

    public static void handleIntent(Intent intent) {
        int id = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
        DownloadImpl download = sMap.get(id);
        if (download == null) {
            Log.d(TAG, "Didn't find download for " + id);
            // TODO(jam): handle download resumption after restart
            return;
        }

        if (intent.getAction().equals(PAUSE_INTENT)) {
            download.pause();
        } else if (intent.getAction().equals(RESUME_INTENT)) {
            download.resume();
        } else if (intent.getAction().equals(CANCEL_INTENT)) {
            download.cancel();
        } else if (intent.getAction().equals(DELETE_INTENT)) {
            sMap.remove(id);
        }
    }

    public DownloadImpl(
            String profileName, IDownloadCallbackClient client, long nativeDownloadImpl, int id) {
        mProfileName = profileName;
        mClient = client;
        mNativeDownloadImpl = nativeDownloadImpl;
        mNotificationId = id;
        if (mClient == null) {
            mClientDownload = null;
        } else {
            try {
                mClientDownload = mClient.createClientDownload(this);
            } catch (RemoteException e) {
                throw new APICallException(e);
            }
        }
        DownloadImplJni.get().setJavaDownload(mNativeDownloadImpl, DownloadImpl.this);
    }

    public IClientDownload getClientDownload() {
        return mClientDownload;
    }

    @DownloadState
    private static int implStateToJavaType(@ImplDownloadState int type) {
        switch (type) {
            case ImplDownloadState.IN_PROGRESS:
                return DownloadState.IN_PROGRESS;
            case ImplDownloadState.COMPLETE:
                return DownloadState.COMPLETE;
            case ImplDownloadState.PAUSED:
                return DownloadState.PAUSED;
            case ImplDownloadState.CANCELLED:
                return DownloadState.CANCELLED;
            case ImplDownloadState.FAILED:
                return DownloadState.FAILED;
        }
        assert false;
        return DownloadState.FAILED;
    }

    @DownloadError
    private static int implErrorToJavaType(@ImplDownloadError int type) {
        switch (type) {
            case ImplDownloadError.NO_ERROR:
                return DownloadError.NO_ERROR;
            case ImplDownloadError.SERVER_ERROR:
                return DownloadError.SERVER_ERROR;
            case ImplDownloadError.SSL_ERROR:
                return DownloadError.SSL_ERROR;
            case ImplDownloadError.CONNECTIVITY_ERROR:
                return DownloadError.CONNECTIVITY_ERROR;
            case ImplDownloadError.NO_SPACE:
                return DownloadError.NO_SPACE;
            case ImplDownloadError.FILE_ERROR:
                return DownloadError.FILE_ERROR;
            case ImplDownloadError.CANCELLED:
                return DownloadError.CANCELLED;
            case ImplDownloadError.OTHER_ERROR:
                return DownloadError.OTHER_ERROR;
        }
        assert false;
        return DownloadError.OTHER_ERROR;
    }

    @Override
    @DownloadState
    public int getState() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        return implStateToJavaType(DownloadImplJni.get().getState(mNativeDownloadImpl));
    }

    @Override
    public long getTotalBytes() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        return DownloadImplJni.get().getTotalBytes(mNativeDownloadImpl);
    }

    @Override
    public long getReceivedBytes() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        return DownloadImplJni.get().getReceivedBytes(mNativeDownloadImpl);
    }

    @Override
    public void pause() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        DownloadImplJni.get().pause(mNativeDownloadImpl);
        updateNotification();
    }

    @Override
    public void resume() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        DownloadImplJni.get().resume(mNativeDownloadImpl);
        updateNotification();
    }

    @Override
    public void cancel() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        DownloadImplJni.get().cancel(mNativeDownloadImpl);
        updateNotification();
    }

    @Override
    public String getLocation() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        return DownloadImplJni.get().getLocation(mNativeDownloadImpl);
    }

    @Override
    public String getFileNameToReportToUser() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        return DownloadImplJni.get().getFileNameToReportToUser(mNativeDownloadImpl);
    }

    @Override
    public String getMimeType() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        return DownloadImplJni.get().getMimeTypeImpl(mNativeDownloadImpl);
    }

    @Override
    @DownloadError
    public int getError() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        return implErrorToJavaType(DownloadImplJni.get().getError(mNativeDownloadImpl));
    }

    @Override
    public void disableNotification() {
        StrictModeWorkaround.apply();
        throwIfNativeDestroyed();
        mDisableNotification = true;

        NotificationManagerProxy notificationManager = getNotificationManager();
        notificationManager.cancel(NOTIFICATION_TAG, mNotificationId);
    }

    private void throwIfNativeDestroyed() {
        if (mNativeDownloadImpl == 0) {
            throw new IllegalStateException("Using Download after native destroyed");
        }
    }

    private Intent createIntent() {
        // Because the intent is using classes from the implementation's class loader,
        // we need to use the constructor which doesn't take the app's context.
        if (WebLayerFactoryImpl.getClientMajorVersion() >= 83) return WebLayerImpl.createIntent();

        try {
            return mClient.createIntent();
        } catch (RemoteException e) {
            throw new APICallException(e);
        }
    }

    public void downloadStarted() {
        if (mDisableNotification) return;

        // TODO(jam): create a foreground service while the download is running to avoid the process
        // being shut down if the user switches apps.

        sMap.put(Integer.valueOf(mNotificationId), this);

        updateNotification();
    }

    public void downloadProgressChanged() {
        updateNotification();
    }

    public void downloadCompleted() {
        updateNotification();
    }

    public void downloadFailed() {
        updateNotification();
    }

    private void updateNotification() {
        NotificationManagerProxy notificationManager = getNotificationManager();
        if (mDisableNotification || notificationManager == null) return;

        Context context = ContextUtils.getApplicationContext();

        Intent deleteIntent = createIntent();
        deleteIntent.setAction(DELETE_INTENT);
        deleteIntent.putExtra(EXTRA_NOTIFICATION_ID, mNotificationId);
        deleteIntent.putExtra(EXTRA_NOTIFICATION_PROFILE, mProfileName);
        PendingIntentProvider deletePendingIntent =
                PendingIntentProvider.getBroadcast(context, mNotificationId, deleteIntent, 0);

        ChannelsInitializer channelsInitializer = new ChannelsInitializer(notificationManager,
                WebLayerNotificationChannels.getInstance(), context.getResources());

        @DownloadState
        int state = getState();
        String channelId = state == DownloadState.COMPLETE
                ? WebLayerNotificationChannels.ChannelId.COMPLETED_DOWNLOADS
                : WebLayerNotificationChannels.ChannelId.ACTIVE_DOWNLOADS;

        WebLayerNotificationBuilder builder =
                new WebLayerNotificationBuilder(context, channelId, channelsInitializer,
                        new NotificationMetadata(0, NOTIFICATION_TAG, mNotificationId));
        builder.setOngoing(true)
                .setDeleteIntent(deletePendingIntent)
                .setPriorityBeforeO(NotificationCompat.PRIORITY_DEFAULT);

        // The filename might not have been available initially.
        String name = getFileNameToReportToUser();
        if (!TextUtils.isEmpty(name)) {
            builder.setContentTitle(name);
        }

        if (state == DownloadState.CANCELLED) {
            notificationManager.cancel(NOTIFICATION_TAG, mNotificationId);
            mDisableNotification = true;
            return;
        }

        Resources resources = context.getResources();

        if (state == DownloadState.COMPLETE) {
            Intent openIntent = createIntent();
            openIntent.setAction(OPEN_INTENT);
            openIntent.putExtra(EXTRA_NOTIFICATION_ID, mNotificationId);
            openIntent.putExtra(EXTRA_NOTIFICATION_PROFILE, mProfileName);
            openIntent.putExtra(EXTRA_NOTIFICATION_LOCATION, getLocation());
            openIntent.putExtra(EXTRA_NOTIFICATION_MIME_TYPE, getMimeType());
            PendingIntentProvider openPendingIntent =
                    PendingIntentProvider.getBroadcast(context, mNotificationId, openIntent, 0);

            String contextText =
                    resources.getString(R.string.download_notification_completed_with_size,
                            DownloadUtils.getStringForBytes(context, getTotalBytes()));
            builder.setContentText(contextText)
                    .setOngoing(false)
                    .setSmallIcon(android.R.drawable.stat_sys_download_done)
                    .setContentIntent(openPendingIntent)
                    .setAutoCancel(true)
                    .setProgress(0, 0, false);
        } else if (state == DownloadState.FAILED) {
            builder.setContentText(resources.getString(R.string.download_notification_failed))
                    .setOngoing(false)
                    .setSmallIcon(android.R.drawable.stat_sys_download_done)
                    .setProgress(0, 0, false);
        } else if (state == DownloadState.IN_PROGRESS) {
            Intent pauseIntent = createIntent();
            pauseIntent.setAction(PAUSE_INTENT);
            pauseIntent.putExtra(EXTRA_NOTIFICATION_ID, mNotificationId);
            pauseIntent.putExtra(EXTRA_NOTIFICATION_PROFILE, mProfileName);
            PendingIntentProvider pausePendingIntent =
                    PendingIntentProvider.getBroadcast(context, mNotificationId, pauseIntent, 0);

            long bytes = getReceivedBytes();
            long totalBytes = getTotalBytes();
            boolean indeterminate = totalBytes == -1;
            int progressCurrent = -1;
            if (!indeterminate && totalBytes != 0) {
                progressCurrent = (int) (bytes * 100 / totalBytes);
            }

            String contentText;
            String bytesString = DownloadUtils.getStringForBytes(context, bytes);
            if (indeterminate) {
                contentText =
                        resources.getString(R.string.download_ui_indeterminate_bytes, bytesString);
            } else {
                String totalString = DownloadUtils.getStringForBytes(context, totalBytes);
                contentText = resources.getString(
                        R.string.download_ui_determinate_bytes, bytesString, totalString);
            }
            builder.setContentText(contentText)
                    .addAction(0 /* no icon */,
                            resources.getString(R.string.download_notification_pause_button),
                            pausePendingIntent, 0 /* no action for UMA */)
                    .setSmallIcon(android.R.drawable.stat_sys_download)
                    .setProgress(100, progressCurrent, indeterminate);
        } else if (state == DownloadState.PAUSED) {
            Intent resumeIntent = createIntent();
            resumeIntent.setAction(RESUME_INTENT);
            resumeIntent.putExtra(EXTRA_NOTIFICATION_ID, mNotificationId);
            resumeIntent.putExtra(EXTRA_NOTIFICATION_PROFILE, mProfileName);
            PendingIntentProvider resumePendingIntent =
                    PendingIntentProvider.getBroadcast(context, mNotificationId, resumeIntent, 0);
            builder.setContentText(resources.getString(R.string.download_notification_paused))
                    .addAction(0 /* no icon */,
                            resources.getString(R.string.download_notification_resume_button),
                            resumePendingIntent, 0 /* no action for UMA */)
                    .setSmallIcon(android.R.drawable.ic_media_pause)
                    .setProgress(0, 0, false);
        }

        if (state == DownloadState.IN_PROGRESS || state == DownloadState.PAUSED) {
            Intent cancelIntent = createIntent();
            cancelIntent.setAction(CANCEL_INTENT);
            cancelIntent.putExtra(EXTRA_NOTIFICATION_ID, mNotificationId);
            cancelIntent.putExtra(EXTRA_NOTIFICATION_PROFILE, mProfileName);
            PendingIntentProvider cancelPendingIntent =
                    PendingIntentProvider.getBroadcast(context, mNotificationId, cancelIntent, 0);
            builder.addAction(0 /* no icon */,
                    resources.getString(R.string.download_notification_cancel_button),
                    cancelPendingIntent, 0 /* no action for UMA */);
        }

        notificationManager.notify(builder.buildChromeNotification());
    }

    /**
     * Returns the notification manager.
     */
    private static NotificationManagerProxy getNotificationManager() {
        return new NotificationManagerProxyImpl(ContextUtils.getApplicationContext());
    }

    private static Uri getDownloadUri(String location) {
        if (ContentUriUtils.isContentUri(location)) return Uri.parse(location);
        return ContentUriUtils.getContentUriFromFile(new File(location));
    }

    @CalledByNative
    private void onNativeDestroyed() {
        mNativeDownloadImpl = 0;
        sMap.remove(mNotificationId);
        // TODO: this should likely notify delegate in some way.
    }

    @NativeMethods
    interface Natives {
        void setJavaDownload(long nativeDownloadImpl, DownloadImpl caller);
        int getState(long nativeDownloadImpl);
        long getTotalBytes(long nativeDownloadImpl);
        long getReceivedBytes(long nativeDownloadImpl);
        void pause(long nativeDownloadImpl);
        void resume(long nativeDownloadImpl);
        void cancel(long nativeDownloadImpl);
        String getLocation(long nativeDownloadImpl);
        String getFileNameToReportToUser(long nativeDownloadImpl);
        String getMimeTypeImpl(long nativeDownloadImpl);
        int getError(long nativeDownloadImpl);
    }
}