summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/dlc/CleanupAction.java
blob: e44704c6c1d417ab9891fedecd28c27805be745a (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
/* -*- 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 org.mozilla.gecko.dlc.catalog.DownloadContent;
import org.mozilla.gecko.dlc.catalog.DownloadContentCatalog;

import java.io.File;

/**
 * CleanupAction: Remove content that is no longer needed.
 */
public class CleanupAction extends BaseAction {
    @Override
    public void perform(Context context, DownloadContentCatalog catalog) {
        for (DownloadContent content : catalog.getContentToDelete()) {
            if (!content.isAssetArchive()) {
                continue; // We do not know how to clean up this content. But this means we didn't
                          // download it anyways.
            }

            try {
                File file = getDestinationFile(context, content);

                if (!file.exists()) {
                    // File does not exist. As good as deleting.
                    catalog.remove(content);
                    return;
                }

                if (file.delete()) {
                    // File has been deleted. Now remove it from the catalog.
                    catalog.remove(content);
                }
            } catch (UnrecoverableDownloadContentException e) {
                // We can't recover. Pretend the content is removed. It probably never existed in
                // the first place.
                catalog.remove(content);
            } catch (RecoverableDownloadContentException e) {
                // Try again next time.
            }
        }
    }
}