summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/filesystem/resources/fs-test-util.js
blob: 80c11c1eb2b71c814b4ab26edb48eff66c3756f0 (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
function bindCallback(obj, callback, arg1, arg2, arg3)
{
    return function(arg) {
        if (arg == undefined)
            callback.apply(obj, [arg1, arg2, arg3]);
        else
            callback.apply(obj, [arg, arg1, arg2, arg3]);
    };
}

// Usage:
//   var helper = new JoinHelper;
//
//   helper.run(function() { /* do something that eventually calls helper.done(); */ });
//   helper.run(function() { /* do something that eventually calls helper.done(); */ });
//   ...
//   helper.join(joinCallback);
//
var JoinHelper = function()
{
    this.pendingOperations = [];
    this.pendingOperationCount = 0;
    this.joinCallback = null;

    this.run = function(operation)
    {
        this.pendingOperationCount++;
        operation();
    };

    // Call this when an operation is done.
    this.done = function()
    {
        this.pendingOperationCount--;
        if (this.pendingOperationCount == 0 && this.joinCallback)
            this.joinCallback();
    };

    // This eventually calls the joinCallback when helper.done() is called as many times as helper.run() is called.
    this.join = function(joinCallback)
    {
        if (this.pendingOperationCount == 0)
            joinCallback();
        else
            this.joinCallback = joinCallback;
    };
};

// Remove everything in the given directory.
function removeAllInDirectory(directory, successCallback, errorCallback) {
    var RemoveAllInDirectoryHelper = function(successCallback, errorCallback) {
        this.entriesCount = 0;
        this.done = false;
        this.reader = null;
        this.successCallback = successCallback;
        this.errorCallback = errorCallback;

        this.entryRemovedCallback = bindCallback(this, function(entry)
        {
            if (--this.entriesCount == 0 && this.successCallback && this.done) {
                this.successCallback();
                this.successCallback = null;
            }
        });

        this.entriesCallback = bindCallback(this, function(entries)
        {
            for (var i = 0; i < entries.length; ++i) {
                this.entriesCount++;
                if (entries[i].isDirectory)
                    entries[i].removeRecursively(this.entryRemovedCallback, this.errorCallback);
                else
                    entries[i].remove(this.entryRemovedCallback, this.errorCallback);
            }
            if (entries.length)
                this.reader.readEntries(this.entriesCallback, this.errorCallback);
            else if (this.entriesCount > 0)
                this.done = true;
            else if (this.successCallback)
                this.successCallback();
        });

        this.removeAllInDirectory = function(directory)
        {
            this.reader = directory.createReader();
            this.reader.readEntries(this.entriesCallback, this.errorCallback);
        };
    };

    var helper = new RemoveAllInDirectoryHelper(successCallback, errorCallback);
    helper.removeAllInDirectory(directory);
}