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 {string} inFileSystemId ID of the file system. |
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(inFileSystemId, entryPath, onSuccess, onError) { |
79 inFileSystemId, entryPath, onSuccess, onError) { | 100 if (inFileSystemId != FILE_SYSTEM_ID) { |
80 if (inFileSystemId != fileSystemId) { | |
81 onError('SECURITY_ERROR'); // enum ProviderError. | 101 onError('SECURITY_ERROR'); // enum ProviderError. |
82 return; | 102 return; |
83 } | 103 } |
84 | 104 |
85 if (entryPath == '/') { | 105 if (entryPath == '/') { |
86 onSuccess(TESTING_ROOT); | 106 onSuccess(TESTING_ROOT); |
87 return; | 107 return; |
88 } | 108 } |
89 | 109 |
90 if (entryPath == '/' + TESTING_TIRAMISU_FILE.name) { | 110 if (entryPath == '/' + TESTING_TIRAMISU_FILE.name) { |
91 onSuccess(TESTING_TIRAMISU_FILE); | 111 onSuccess(TESTING_TIRAMISU_FILE); |
92 return; | 112 return; |
93 } | 113 } |
94 | 114 |
95 if (entryPath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) { | 115 if (entryPath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) { |
96 onSuccess(TESTING_BROKEN_TIRAMISU_FILE); | 116 onSuccess(TESTING_BROKEN_TIRAMISU_FILE); |
97 return; | 117 return; |
98 } | 118 } |
99 | 119 |
100 onError('NOT_FOUND'); // enum ProviderError. | 120 onError('NOT_FOUND'); // enum ProviderError. |
101 } | 121 } |
102 | 122 |
103 /** | 123 /** |
104 * Requests opening a file at <code>filePath</code>. Further file operations | 124 * Requests opening a file at <code>filePath</code>. Further file operations |
105 * will be associated with the <code>requestId</code> | 125 * will be associated with the <code>requestId</code> |
106 * | 126 * |
107 * @param {number} inFileSystemId ID of the file system. | 127 * @param {string} inFileSystemId ID of the file system. |
108 * @param {number} requestId ID of the opening request. Used later for reading. | 128 * @param {number} requestId ID of the opening request. Used later for reading. |
109 * @param {string} filePath Path of the file to be opened. | 129 * @param {string} filePath Path of the file to be opened. |
110 * @param {string} mode Mode, either reading or writing. | 130 * @param {string} mode Mode, either reading or writing. |
111 * @param {boolean} create True to create if doesn't exist. | 131 * @param {boolean} create True to create if doesn't exist. |
112 * @param {function()} onSuccess Success callback. | 132 * @param {function()} onSuccess Success callback. |
113 * @param {function(string)} onError Error callback. | 133 * @param {function(string)} onError Error callback. |
114 */ | 134 */ |
115 function onOpenFileRequested( | 135 function onOpenFileRequested( |
116 inFileSystemId, requestId, filePath, mode, create, onSuccess, onError) { | 136 inFileSystemId, requestId, filePath, mode, create, onSuccess, onError) { |
117 if (inFileSystemId != fileSystemId || mode != 'READ' || create) { | 137 if (inFileSystemId != FILE_SYSTEM_ID || mode != 'READ' || create) { |
118 onError('SECURITY_ERROR'); // enum ProviderError. | 138 onError('SECURITY_ERROR'); // enum ProviderError. |
119 return; | 139 return; |
120 } | 140 } |
121 | 141 |
122 if (filePath == '/' + TESTING_TIRAMISU_FILE.name || | 142 if (filePath == '/' + TESTING_TIRAMISU_FILE.name || |
123 filePath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) { | 143 filePath == '/' + TESTING_BROKEN_TIRAMISU_FILE.name) { |
124 openedFiles[requestId] = filePath; | 144 openedFiles[requestId] = filePath; |
125 onSuccess(); | 145 onSuccess(); |
126 } else { | 146 } else { |
127 onError('NOT_FOUND'); // enum ProviderError. | 147 onError('NOT_FOUND'); // enum ProviderError. |
128 } | 148 } |
129 } | 149 } |
130 | 150 |
131 /** | 151 /** |
132 * Requests closing a file previously opened with <code>openRequestId</code>. | 152 * Requests closing a file previously opened with <code>openRequestId</code>. |
133 * | 153 * |
134 * @param {number} inFileSystemId ID of the file system. | 154 * @param {string} inFileSystemId ID of the file system. |
135 * @param {number} openRequestId ID of the request used to open the file. | 155 * @param {number} openRequestId ID of the request used to open the file. |
136 * @param {function()} onSuccess Success callback. | 156 * @param {function()} onSuccess Success callback. |
137 * @param {function(string)} onError Error callback. | 157 * @param {function(string)} onError Error callback. |
138 */ | 158 */ |
139 function onCloseFileRequested( | 159 function onCloseFileRequested( |
140 inFileSystemId, openRequestId, onSuccess, onError) { | 160 inFileSystemId, openRequestId, onSuccess, onError) { |
141 if (inFileSystemId != fileSystemId || !openedFiles[openRequestId]) { | 161 if (inFileSystemId != FILE_SYSTEM_ID || !openedFiles[openRequestId]) { |
142 onError('SECURITY_ERROR'); // enum ProviderError. | 162 onError('SECURITY_ERROR'); // enum ProviderError. |
143 return; | 163 return; |
144 } | 164 } |
145 | 165 |
146 delete openedFiles[requestId]; | 166 delete openedFiles[requestId]; |
147 onSuccess(); | 167 onSuccess(); |
148 } | 168 } |
149 | 169 |
150 /** | 170 /** |
151 * Requests reading contents of a file, previously opened with <code> | 171 * Requests reading contents of a file, previously opened with <code> |
152 * openRequestId</code>. | 172 * openRequestId</code>. |
153 * | 173 * |
154 * @param {number} inFileSystemId ID of the file system. | 174 * @param {string} inFileSystemId ID of the file system. |
155 * @param {number} openRequestId ID of the request used to open the file. | 175 * @param {number} openRequestId ID of the request used to open the file. |
156 * @param {number} offset Offset of the file. | 176 * @param {number} offset Offset of the file. |
157 * @param {number} length Number of bytes to read. | 177 * @param {number} length Number of bytes to read. |
158 * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a | 178 * @param {function(ArrayBuffer, boolean)} onSuccess Success callback with a |
159 * chunk of data, and information if more data will be provided later. | 179 * chunk of data, and information if more data will be provided later. |
160 * @param {function(string)} onError Error callback. | 180 * @param {function(string)} onError Error callback. |
161 */ | 181 */ |
162 function onReadFileRequested( | 182 function onReadFileRequested( |
163 inFileSystemId, openRequestId, offset, length, onSuccess, onError) { | 183 inFileSystemId, openRequestId, offset, length, onSuccess, onError) { |
164 var filePath = openedFiles[openRequestId]; | 184 var filePath = openedFiles[openRequestId]; |
165 if (inFileSystemId != fileSystemId || !filePath) { | 185 if (inFileSystemId != FILE_SYSTEM_ID || !filePath) { |
166 onError('SECURITY_ERROR'); // enum ProviderError. | 186 onError('SECURITY_ERROR'); // enum ProviderError. |
167 return; | 187 return; |
168 } | 188 } |
169 | 189 |
170 if (filePath == '/' + TESTING_TIRAMISU_FILE.name) { | 190 if (filePath == '/' + TESTING_TIRAMISU_FILE.name) { |
171 var textToSend = TESTING_TEXT.substr(offset, length); | 191 var textToSend = TESTING_TEXT.substr(offset, length); |
172 var textToSendInChunks = textToSend.split(/(?= )/); | 192 var textToSendInChunks = textToSend.split(/(?= )/); |
173 | 193 |
174 textToSendInChunks.forEach(function(item, index) { | 194 textToSendInChunks.forEach(function(item, index) { |
175 // Convert item (string) to an ArrayBuffer. | 195 // Convert item (string) to an ArrayBuffer. |
(...skipping 18 matching lines...) Expand all Loading... |
194 onError('INVALID_OPERATION'); // enum ProviderError. | 214 onError('INVALID_OPERATION'); // enum ProviderError. |
195 } | 215 } |
196 | 216 |
197 /** | 217 /** |
198 * Sets up the tests. Called once per all test cases. In case of a failure, | 218 * Sets up the tests. Called once per all test cases. In case of a failure, |
199 * the callback is not called. | 219 * the callback is not called. |
200 * | 220 * |
201 * @param {function()} callback Success callback. | 221 * @param {function()} callback Success callback. |
202 */ | 222 */ |
203 function setUp(callback) { | 223 function setUp(callback) { |
204 chrome.fileSystemProvider.mount('chocolate.zip', function(id) { | 224 chrome.fileSystemProvider.mount(FILE_SYSTEM_ID, 'chocolate.zip', function() { |
205 fileSystemId = id; | |
206 chrome.fileSystemProvider.onGetMetadataRequested.addListener( | 225 chrome.fileSystemProvider.onGetMetadataRequested.addListener( |
207 onGetMetadataRequested); | 226 onGetMetadataRequested); |
208 chrome.fileSystemProvider.onOpenFileRequested.addListener( | 227 chrome.fileSystemProvider.onOpenFileRequested.addListener( |
209 onOpenFileRequested); | 228 onOpenFileRequested); |
210 chrome.fileSystemProvider.onReadFileRequested.addListener( | 229 chrome.fileSystemProvider.onReadFileRequested.addListener( |
211 onReadFileRequested); | 230 onReadFileRequested); |
212 var volumeId = | 231 var volumeId = |
213 'provided:' + chrome.runtime.id + '-' + fileSystemId + '-user'; | 232 'provided:' + chrome.runtime.id + '-' + FILE_SYSTEM_ID + '-user'; |
214 | 233 |
215 chrome.fileBrowserPrivate.requestFileSystem( | 234 getVolumeInfo(FILE_SYSTEM_ID, function(volumeInfo) { |
216 volumeId, | 235 chrome.test.assertTrue(!!volumeInfo); |
217 function(inFileSystem) { | 236 chrome.fileBrowserPrivate.requestFileSystem( |
218 chrome.test.assertTrue(!!inFileSystem); | 237 volumeInfo.volumeId, |
| 238 function(inFileSystem) { |
| 239 chrome.test.assertTrue(!!inFileSystem); |
219 | 240 |
220 fileSystem = inFileSystem; | 241 fileSystem = inFileSystem; |
221 callback(); | 242 callback(); |
222 }); | 243 }); |
| 244 }); |
223 }, function() { | 245 }, function() { |
224 chrome.test.fail(); | 246 chrome.test.fail(); |
225 }); | 247 }); |
226 } | 248 } |
227 | 249 |
228 /** | 250 /** |
229 * Runs all of the test cases, one by one. | 251 * Runs all of the test cases, one by one. |
230 */ | 252 */ |
231 function runTests() { | 253 function runTests() { |
232 chrome.test.runTests([ | 254 chrome.test.runTests([ |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 }, | 305 }, |
284 function(error) { | 306 function(error) { |
285 chrome.test.fail(error.name); | 307 chrome.test.fail(error.name); |
286 }); | 308 }); |
287 } | 309 } |
288 ]); | 310 ]); |
289 } | 311 } |
290 | 312 |
291 // Setup and run all of the test cases. | 313 // Setup and run all of the test cases. |
292 setUp(runTests); | 314 setUp(runTests); |
OLD | NEW |