• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31var ports = [];
32var isTempStorageCleared = false;
33/**
34 * @type {string}
35 */
36var tempStorageError;
37
38/**
39 * @param {!MessageEvent} event
40 */
41self.onconnect = function(event)
42{
43    var newPort = /** @type {!MessagePort} */ (event.ports[0]);
44    if (isTempStorageCleared) {
45        notifyTempStorageCleared(newPort);
46        return;
47    }
48
49    newPort.onmessage = handleMessage;
50    newPort.onerror = handleError;
51    ports.push(newPort);
52
53    if (ports.length === 1)
54        clearTempStorage();
55}
56
57function clearTempStorage()
58{
59    function didFail(e)
60    {
61        tempStorageError = "Failed to clear temp storage: " + e.message + " " + e.name;
62        console.error(tempStorageError);
63        didClearTempStorage();
64    }
65    /**
66     * @param {!FileSystem} fs
67     */
68    function didGetFS(fs)
69    {
70        fs.root.createReader().readEntries(didReadEntries, didFail);
71    }
72    /**
73     * @param {!Array.<!Entry>} entries
74     */
75    function didReadEntries(entries)
76    {
77        var remainingEntries = entries.length;
78        if (!remainingEntries) {
79            didClearTempStorage();
80            return;
81        }
82        function didDeleteEntry()
83        {
84            if (!--remainingEntries)
85                didClearTempStorage();
86        }
87        function failedToDeleteEntry(e)
88        {
89            tempStorageError = "Failed to delete entry: " + e.message + " " + e.name;
90            console.error(tempStorageError);
91            didDeleteEntry();
92        }
93        for (var i = 0; i < entries.length; i++) {
94            var entry = entries[i];
95            if (entry.isFile)
96                entry.remove(didDeleteEntry, failedToDeleteEntry);
97            else
98                entry.removeRecursively(didDeleteEntry, failedToDeleteEntry);
99        }
100    }
101    self.webkitRequestFileSystem(self.TEMPORARY, 10, didGetFS, didFail);
102}
103
104function didClearTempStorage()
105{
106  isTempStorageCleared = true;
107  for (var i = 0; i < ports.length; i++)
108      notifyTempStorageCleared(ports[i]);
109  ports = null;
110}
111
112/**
113 * @param {!MessagePort} port
114 */
115function notifyTempStorageCleared(port)
116{
117    port.postMessage({
118        type: "tempStorageCleared",
119        error: tempStorageError
120    });
121}
122
123function handleMessage(event)
124{
125    if (event.data.type === "disconnect")
126        removePort(event.target);
127}
128
129function handleError(event)
130{
131    console.error("Error: " + event.data);
132    removePort(event.target);
133}
134
135function removePort(port)
136{
137    if (!ports)
138        return;
139    var index = ports.indexOf(port);
140    ports.splice(index, 1);
141}
142