OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @type {number} | 8 * @type {DOMFileSystem} |
9 */ | |
10 var fileSystemId = 0; | |
11 | |
12 /** | |
13 * @type {?DOMFileSystem} | |
14 */ | 9 */ |
15 var fileSystem = null; | 10 var fileSystem = null; |
16 | 11 |
17 /** | 12 /** |
18 * Map of opened files, from a <code>openRequestId</code> to <code>filePath | 13 * Map of opened files, from a <code>openRequestId</code> to <code>filePath |
19 * </code>. | 14 * </code>. |
20 * @type {Object.<number, string>} | 15 * @type {Object.<number, string>} |
21 */ | 16 */ |
22 var openedFiles = {}; | 17 var openedFiles = {}; |
23 | 18 |
24 /** | 19 /** |
20 * @type {string} | |
21 * @const | |
22 */ | |
23 var FILE_SYSTEM_ID = 'chocolate-id'; | |
24 | |
25 /** | |
25 * @type {Object} | 26 * @type {Object} |
26 * @const | 27 * @const |
27 */ | 28 */ |
28 var TESTING_ROOT = Object.freeze({ | 29 var TESTING_ROOT = Object.freeze({ |
29 isDirectory: true, | 30 isDirectory: true, |
30 name: '', | 31 name: '', |
31 size: 0, | 32 size: 0, |
32 modificationTime: new Date(2014, 4, 28, 10, 39, 15) | 33 modificationTime: new Date(2014, 4, 28, 10, 39, 15) |
33 }); | 34 }); |
34 | 35 |
(...skipping 22 matching lines...) Expand all Loading... | |
57 * @const | 58 * @const |
58 */ | 59 */ |
59 var TESTING_BROKEN_TIRAMISU_FILE = Object.freeze({ | 60 var TESTING_BROKEN_TIRAMISU_FILE = Object.freeze({ |
60 isDirectory: false, | 61 isDirectory: false, |
61 name: 'broken-tiramisu.txt', | 62 name: 'broken-tiramisu.txt', |
62 size: TESTING_TEXT.length, | 63 size: TESTING_TEXT.length, |
63 modificationTime: new Date(2014, 1, 25, 7, 36, 12) | 64 modificationTime: new Date(2014, 1, 25, 7, 36, 12) |
64 }); | 65 }); |
65 | 66 |
66 /** | 67 /** |
68 * Gets volume information for the provided file system. | |
69 * | |
70 * @param {string} fileSystemId Id of the provided file system. | |
71 * @param {function(Object)} callback Callback to be called on result, with the | |
72 * volume information object in case of success, or null if not found. | |
73 */ | |
74 function getVolumeInfo(fileSystemId, callback) { | |
75 chrome.fileBrowserPrivate.getVolumeMetadataList(function(volumeList) { | |
76 for (var i = 0; i < volumeList.length; i++) { | |
77 if (volumeList[i].extensionId == chrome.runtime.id && | |
78 volumeList[i].fileSystemId == fileSystemId) { | |
79 callback(volumeList[i]); | |
80 return; | |
81 } | |
82 } | |
83 callback(null); | |
84 }); | |
85 } | |
86 | |
87 /** | |
67 * Returns metadata for the requested entry. | 88 * Returns metadata for the requested entry. |
68 * | 89 * |
69 * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event | 90 * To successfully acquire a DirectoryEntry, or even a DOMFileSystem, this event |
70 * must be implemented and return correct values. | 91 * must be implemented and return correct values. |
71 * | 92 * |
72 * @param {number} inFileSystemId ID of the file system. | 93 * @param {number} inFileSystemId ID of the file system. |
hirono
2014/05/21 10:09:54
nit: string
Same for followings.
mtomasz
2014/05/22 02:16:54
Done.
| |
73 * @param {string} entryPath Path of the requested entry. | 94 * @param {string} entryPath Path of the requested entry. |
74 * @param {function(Object)} onSuccess Success callback with metadata passed | 95 * @param {function(Object)} onSuccess Success callback with metadata passed |
75 * an argument. | 96 * an argument. |
76 * @param {function(string)} onError Error callback with an error code. | 97 * @param {function(string)} onError Error callback with an error code. |
77 */ | 98 */ |
78 function onGetMetadataRequested( | 99 function onGetMetadataRequested( |
79 inFileSystemId, entryPath, onSuccess, onError) { | 100 inFileSystemId, entryPath, onSuccess, onError) { |
80 if (inFileSystemId != fileSystemId) { | 101 if (inFileSystemId != FILE_SYSTEM_ID) { |
81 onError('SECURITY_ERROR'); // enum ProviderError. | 102 onError('SECURITY_ERROR'); // enum ProviderError. |
82 return; | 103 return; |
83 } | 104 } |
84 | 105 |
85 if (entryPath == '/') { | 106 if (entryPath == '/') { |
86 onSuccess(TESTING_ROOT); | 107 onSuccess(TESTING_ROOT); |
87 return; | 108 return; |
88 } | 109 } |
89 | 110 |
90 if (entryPath == '/' + TESTING_TIRAMISU_FILE.name) { | 111 if (entryPath == '/' + TESTING_TIRAMISU_FILE.name) { |
(...skipping 16 matching lines...) Expand all Loading... | |
107 * @param {number} inFileSystemId ID of the file system. | 128 * @param {number} inFileSystemId ID of the file system. |
108 * @param {number} requestId ID of the opening request. Used later for reading. | 129 * @param {number} requestId ID of the opening request. Used later for reading. |
109 * @param {string} filePath Path of the file to be opened. | 130 * @param {string} filePath Path of the file to be opened. |
110 * @param {string} mode Mode, either reading or writing. | 131 * @param {string} mode Mode, either reading or writing. |
111 * @param {boolean} create True to create if doesn't exist. | 132 * @param {boolean} create True to create if doesn't exist. |
112 * @param {function()} onSuccess Success callback. | 133 * @param {function()} onSuccess Success callback. |
113 * @param {function(string)} onError Error callback. | 134 * @param {function(string)} onError Error callback. |
114 */ | 135 */ |
115 function onOpenFileRequested( | 136 function onOpenFileRequested( |
116 inFileSystemId, requestId, filePath, mode, create, onSuccess, onError) { | 137 inFileSystemId, requestId, filePath, mode, create, onSuccess, onError) { |
117 if (inFileSystemId != fileSystemId || mode != 'READ' || create) { | 138 if (inFileSystemId != FILE_SYSTEM_ID || mode != 'READ' || create) { |
118 onError('SECURITY_ERROR'); // enum ProviderError. | 139 onError('SECURITY_ERROR'); // enum ProviderError. |
119 return; | 140 return; |
120 } | 141 } |
121 | 142 |
122 if (filePath == '/' + TESTING_TIRAMISU_FILE.name || | 143 if (filePath == '/' + TESTING_TIRAMISU_FILE.name || |
123 filePath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) { | 144 filePath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) { |
124 openedFiles[requestId] = filePath; | 145 openedFiles[requestId] = filePath; |
125 onSuccess(); | 146 onSuccess(); |
126 } else { | 147 } else { |
127 onError('NOT_FOUND'); // enum ProviderError. | 148 onError('NOT_FOUND'); // enum ProviderError. |
128 } | 149 } |
129 } | 150 } |
130 | 151 |
131 /** | 152 /** |
132 * Requests closing a file previously opened with <code>openRequestId</code>. | 153 * Requests closing a file previously opened with <code>openRequestId</code>. |
133 * | 154 * |
134 * @param {number} inFileSystemId ID of the file system. | 155 * @param {number} inFileSystemId ID of the file system. |
135 * @param {number} openRequestId ID of the request used to open the file. | 156 * @param {number} openRequestId ID of the request used to open the file. |
136 * @param {function()} onSuccess Success callback. | 157 * @param {function()} onSuccess Success callback. |
137 * @param {function(string)} onError Error callback. | 158 * @param {function(string)} onError Error callback. |
138 */ | 159 */ |
139 function onCloseFileRequested( | 160 function onCloseFileRequested( |
140 inFileSystemId, openRequestId, onSuccess, onError) { | 161 inFileSystemId, openRequestId, onSuccess, onError) { |
141 if (inFileSystemId != fileSystemId || !openedFiles[openRequestId]) { | 162 if (inFileSystemId != FILE_SYSTEM_ID || !openedFiles[openRequestId]) { |
142 onError('SECURITY_ERROR'); // enum ProviderError. | 163 onError('SECURITY_ERROR'); // enum ProviderError. |
143 return; | 164 return; |
144 } | 165 } |
145 | 166 |
146 delete openedFiles[requestId]; | 167 delete openedFiles[requestId]; |
147 onSuccess(); | 168 onSuccess(); |
148 } | 169 } |
149 | 170 |
150 /** | 171 /** |
151 * Requests reading contents of a file, previously opened with <code> | 172 * Requests reading contents of a file, previously opened with <code> |
152 * openRequestId</code>. | 173 * openRequestId</code>. |
153 * | 174 * |
154 * @param {number} inFileSystemId ID of the file system. | 175 * @param {number} inFileSystemId ID of the file system. |
155 * @param {number} openRequestId ID of the request used to open the file. | 176 * @param {number} openRequestId ID of the request used to open the file. |
156 * @param {number} offset Offset of the file. | 177 * @param {number} offset Offset of the file. |
157 * @param {number} length Number of bytes to read. | 178 * @param {number} length Number of bytes to read. |
158 * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a | 179 * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a |
159 * chunk of data, and information if more data will be provided later. | 180 * chunk of data, and information if more data will be provided later. |
160 * @param {function(string)} onError Error callback. | 181 * @param {function(string)} onError Error callback. |
161 */ | 182 */ |
162 function onReadFileRequested( | 183 function onReadFileRequested( |
163 inFileSystemId, openRequestId, offset, length, onSuccess, onError) { | 184 inFileSystemId, openRequestId, offset, length, onSuccess, onError) { |
164 var filePath = openedFiles[openRequestId]; | 185 var filePath = openedFiles[openRequestId]; |
165 if (inFileSystemId != fileSystemId || !filePath) { | 186 if (inFileSystemId != FILE_SYSTEM_ID || !filePath) { |
166 onError('SECURITY_ERROR'); // enum ProviderError. | 187 onError('SECURITY_ERROR'); // enum ProviderError. |
167 return; | 188 return; |
168 } | 189 } |
169 | 190 |
170 if (filePath == '/' + TESTING_TIRAMISU_FILE.name) { | 191 if (filePath == '/' + TESTING_TIRAMISU_FILE.name) { |
171 var textToSend = TESTING_TEXT.substr(offset, length); | 192 var textToSend = TESTING_TEXT.substr(offset, length); |
172 var textToSendInChunks = textToSend.split(/(?= )/); | 193 var textToSendInChunks = textToSend.split(/(?= )/); |
173 | 194 |
174 textToSendInChunks.forEach(function(item, index) { | 195 textToSendInChunks.forEach(function(item, index) { |
175 // Convert item (string) to an ArrayBuffer. | 196 // Convert item (string) to an ArrayBuffer. |
(...skipping 18 matching lines...) Expand all Loading... | |
194 onError('INVALID_OPERATION'); // enum ProviderError. | 215 onError('INVALID_OPERATION'); // enum ProviderError. |
195 } | 216 } |
196 | 217 |
197 /** | 218 /** |
198 * Sets up the tests. Called once per all test cases. In case of a failure, | 219 * Sets up the tests. Called once per all test cases. In case of a failure, |
199 * the callback is not called. | 220 * the callback is not called. |
200 * | 221 * |
201 * @param {function()} callback Success callback. | 222 * @param {function()} callback Success callback. |
202 */ | 223 */ |
203 function setUp(callback) { | 224 function setUp(callback) { |
204 chrome.fileSystemProvider.mount('chocolate.zip', function(id) { | 225 chrome.fileSystemProvider.mount(FILE_SYSTEM_ID, 'chocolate.zip', function() { |
205 fileSystemId = id; | |
206 chrome.fileSystemProvider.onGetMetadataRequested.addListener( | 226 chrome.fileSystemProvider.onGetMetadataRequested.addListener( |
207 onGetMetadataRequested); | 227 onGetMetadataRequested); |
208 chrome.fileSystemProvider.onOpenFileRequested.addListener( | 228 chrome.fileSystemProvider.onOpenFileRequested.addListener( |
209 onOpenFileRequested); | 229 onOpenFileRequested); |
210 chrome.fileSystemProvider.onReadFileRequested.addListener( | 230 chrome.fileSystemProvider.onReadFileRequested.addListener( |
211 onReadFileRequested); | 231 onReadFileRequested); |
212 var volumeId = | 232 var volumeId = |
213 'provided:' + chrome.runtime.id + '-' + fileSystemId + '-user'; | 233 'provided:' + chrome.runtime.id + '-' + FILE_SYSTEM_ID + '-user'; |
214 | 234 |
215 chrome.fileBrowserPrivate.requestFileSystem( | 235 getVolumeInfo(FILE_SYSTEM_ID, function(volumeInfo) { |
216 volumeId, | 236 chrome.test.assertTrue(!!volumeInfo); |
217 function(inFileSystem) { | 237 chrome.fileBrowserPrivate.requestFileSystem( |
218 chrome.test.assertTrue(!!inFileSystem); | 238 volumeInfo.volumeId, |
239 function(inFileSystem) { | |
240 chrome.test.assertTrue(!!inFileSystem); | |
219 | 241 |
220 fileSystem = inFileSystem; | 242 fileSystem = inFileSystem; |
221 callback(); | 243 callback(); |
222 }); | 244 }); |
245 }); | |
223 }, function() { | 246 }, function() { |
224 chrome.test.fail(); | 247 chrome.test.fail(); |
225 }); | 248 }); |
226 } | 249 } |
227 | 250 |
228 /** | 251 /** |
229 * Runs all of the test cases, one by one. | 252 * Runs all of the test cases, one by one. |
230 */ | 253 */ |
231 function runTests() { | 254 function runTests() { |
232 chrome.test.runTests([ | 255 chrome.test.runTests([ |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 }, | 306 }, |
284 function(error) { | 307 function(error) { |
285 chrome.test.fail(error.name); | 308 chrome.test.fail(error.name); |
286 }); | 309 }); |
287 } | 310 } |
288 ]); | 311 ]); |
289 } | 312 } |
290 | 313 |
291 // Setup and run all of the test cases. | 314 // Setup and run all of the test cases. |
292 setUp(runTests); | 315 setUp(runTests); |
OLD | NEW |