summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/dlc/VerifyAction.java
blob: e96a62eaea69e392d189969e82c7d30b75c311d1 (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
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.dlc;

import android.content.Context;
import android.util.Log;

import org.mozilla.gecko.dlc.catalog.DownloadContent;
import org.mozilla.gecko.dlc.catalog.DownloadContentCatalog;

import java.io.File;

/**
 * Verify: Validate downloaded content. Does it still exist and does it have the correct checksum?
 */
public class VerifyAction extends BaseAction {
    private static final String LOGTAG = "DLCVerifyAction";

    @Override
    public void perform(Context context, DownloadContentCatalog catalog) {
        Log.d(LOGTAG, "Verifying catalog..");

        for (DownloadContent content : catalog.getDownloadedContent()) {
            try {
                File destinationFile = getDestinationFile(context, content);

                if (!destinationFile.exists()) {
                    Log.d(LOGTAG, "Downloaded content does not exist anymore: " + content);

                    // This file does not exist even though it is marked as downloaded in the catalog. Scheduling a
                    // download to fetch it again.
                    catalog.scheduleDownload(content);
                    continue;
                }

                if (!verify(destinationFile, content.getChecksum())) {
                    catalog.scheduleDownload(content);
                    Log.d(LOGTAG, "Wrong checksum. Scheduling download: " + content);
                    continue;
                }

                Log.v(LOGTAG, "Content okay: " + content);
            } catch (UnrecoverableDownloadContentException e) {
                Log.w(LOGTAG, "Unrecoverable exception while verifying downloaded file", e);
            } catch (RecoverableDownloadContentException e) {
                // That's okay, we are just verifying already existing content. No log.
            }
        }

        if (catalog.hasScheduledDownloads()) {
            startDownloads(context);
        }

        Log.v(LOGTAG, "Done");
    }

    protected void startDownloads(Context context) {
        DownloadContentService.startDownloads(context);
    }
}