OLD | NEW |
---|---|
(Empty) | |
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 | |
31 var ports = []; | |
32 var isTempStorageCleared = false; | |
33 | |
34 self.onconnect = function (event) { | |
pfeldman
2014/01/14 16:26:28
/**
* @param {Event} event
*/
self.onconnect = f
yurys
2014/01/15 08:30:11
Done.
| |
35 var newPort = event.source; | |
36 if (isTempStorageCleared) { | |
37 notifyTempStorageCleared(newPort); | |
38 return; | |
39 } | |
40 | |
41 newPort.onmessage = handleMessage; | |
42 newPort.onerror = handleError; | |
43 ports.push(newPort); | |
44 | |
45 if (ports.length === 1) | |
46 clearTempStorage(); | |
47 } | |
48 | |
49 function clearTempStorage() | |
50 { | |
51 function didFail(e) | |
52 { | |
53 console.error("Failed to clear temp storage: " + e.message + " " + e.nam e); | |
54 didClearTempStorage(); | |
pfeldman
2014/01/14 16:26:28
"Failed to clear temp storage: " + e.message + " "
yurys
2014/01/15 08:30:11
Done.
| |
55 } | |
56 function didGetFS(fs) | |
pfeldman
2014/01/14 16:26:28
Annotate please
yurys
2014/01/15 08:30:11
Done.
| |
57 { | |
58 fs.root.createReader().readEntries(didReadEntries, didFail); | |
59 } | |
60 function didReadEntries(entries) | |
pfeldman
2014/01/14 16:26:28
ditto
yurys
2014/01/15 08:30:11
Done.
| |
61 { | |
62 var remainingEntries = entries.length; | |
63 if (!remainingEntries) { | |
64 didClearTempStorage(); | |
65 return; | |
66 } | |
67 function didDeleteEntry() | |
68 { | |
69 if (!--remainingEntries) | |
70 didClearTempStorage(); | |
alph
2014/01/14 15:46:34
Can you call the callback right after you enumerat
yurys
2014/01/14 15:58:59
This would result in a race when one of a clients
| |
71 } | |
72 function failedToDeleteEntry(e) | |
73 { | |
74 console.error("Failed to delete entry: " + e.message + " " + e.name) ; | |
75 didDeleteEntry(); | |
pfeldman
2014/01/14 16:26:28
ditto
yurys
2014/01/15 08:30:11
Done.
| |
76 } | |
77 for (var i = 0; i < entries.length; i++) { | |
78 var entry = entries[i]; | |
79 if (entry.isFile) | |
80 entry.remove(didDeleteEntry, failedToDeleteEntry); | |
81 else | |
82 entry.removeRecursively(didDeleteEntry, failedToDeleteEntry); | |
83 } | |
84 } | |
85 self.webkitRequestFileSystem(self.TEMPORARY, 10, didGetFS, didFail); | |
86 } | |
87 | |
88 function didClearTempStorage() | |
89 { | |
90 isTempStorageCleared = true; | |
91 for (var i = 0; i < ports.length; i++) | |
92 notifyTempStorageCleared(ports[i]); | |
93 ports = null; | |
94 } | |
95 | |
96 function notifyTempStorageCleared(port) | |
97 { | |
98 port.postMessage({ | |
99 type: "tempStorageCleared" | |
100 }); | |
101 } | |
102 | |
103 function handleMessage(event) | |
104 { | |
105 if (event.data.type === "disconnect") | |
106 removePort(event.target); | |
107 } | |
108 | |
109 function handleError(event) | |
110 { | |
111 console.error("Error: " + event.data); | |
112 removePort(event.target); | |
113 } | |
114 | |
115 function removePort(port) | |
116 { | |
117 if (!ports) | |
118 return; | |
119 var index = ports.indexOf(port); | |
120 ports.splice(index, 1); | |
121 } | |
OLD | NEW |