OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // Custom binding for the fileSystemProvider API. | 5 // Custom binding for the fileSystemProvider API. |
6 | 6 |
7 var binding = require('binding').Binding.create('fileSystemProvider'); | 7 var binding = require('binding').Binding.create('fileSystemProvider'); |
8 var fileSystemProviderInternal = | 8 var fileSystemProviderInternal = |
9 require('binding').Binding.create('fileSystemProviderInternal').generate(); | 9 require('binding').Binding.create('fileSystemProviderInternal').generate(); |
10 var eventBindings = require('event_bindings'); | 10 var eventBindings = require('event_bindings'); |
11 var fileSystemNatives = requireNative('file_system_natives'); | 11 var fileSystemNatives = requireNative('file_system_natives'); |
12 var GetDOMError = fileSystemNatives.GetDOMError; | 12 var GetDOMError = fileSystemNatives.GetDOMError; |
13 | 13 |
14 /** | 14 /** |
15 * Maximum size of the thumbnail in bytes. | |
16 * @type {number} | |
17 * @const | |
18 */ | |
19 var METADATA_THUMBNAIL_SIZE_LIMIT = 32 * 1024 * 1024; | |
20 | |
21 /** | |
15 * Annotates a date with its serialized value. | 22 * Annotates a date with its serialized value. |
16 * @param {Date} date Input date. | 23 * @param {Date} date Input date. |
17 * @return {Date} Date with an extra <code>value</code> attribute. | 24 * @return {Date} Date with an extra <code>value</code> attribute. |
18 */ | 25 */ |
19 function annotateDate(date) { | 26 function annotateDate(date) { |
20 // Copy in case the input date is frozen. | 27 // Copy in case the input date is frozen. |
21 var result = new Date(date.getTime()); | 28 var result = new Date(date.getTime()); |
22 result.value = result.toString(); | 29 result.value = result.toString(); |
23 return result; | 30 return result; |
24 } | 31 } |
25 | 32 |
26 /** | 33 /** |
34 * Verifies if the passed image URI is valid. | |
35 * @param {*} uri Image URI. | |
36 * @return {boolean} True if valid, valse otherwise. | |
37 */ | |
38 function verifyImageURI(uri) { | |
39 // The URI is specified by a user, so the type may be incorrect. | |
40 if (typeof uri != 'string' && !(uri instanceof String)) | |
41 return false; | |
42 | |
43 var uriLowerCase = uri.toLowerCase(); | |
hirono
2014/08/29 03:58:32
uri.substr(0, 20).toLowerCase() may be good for pe
mtomasz
2014/08/29 05:20:26
Good point. I optimized it in C++, but forgot abou
| |
44 return uriLowerCase.indexOf('data:image/png;') == 0 || | |
45 uriLowerCase.indexOf('data:image/jpeg;') == 0 || | |
46 uriLowerCase.indexOf('data:image/webp;') == 0; | |
47 } | |
48 | |
49 /** | |
27 * Annotates an entry metadata by serializing its modifiedTime value. | 50 * Annotates an entry metadata by serializing its modifiedTime value. |
28 * @param {EntryMetadata} metadata Input metadata. | 51 * @param {EntryMetadata} metadata Input metadata. |
29 * @return {EntryMetadata} metadata Annotated metadata, which can be passed | 52 * @return {EntryMetadata} metadata Annotated metadata, which can be passed |
30 * back to the C++ layer. | 53 * back to the C++ layer. |
31 */ | 54 */ |
32 function annotateMetadata(metadata) { | 55 function annotateMetadata(metadata) { |
33 var result = { | 56 var result = { |
34 isDirectory: metadata.isDirectory, | 57 isDirectory: metadata.isDirectory, |
35 name: metadata.name, | 58 name: metadata.name, |
36 size: metadata.size, | 59 size: metadata.size, |
37 modificationTime: annotateDate(metadata.modificationTime) | 60 modificationTime: annotateDate(metadata.modificationTime) |
38 }; | 61 }; |
39 if ('mimeType' in metadata) | 62 if ('mimeType' in metadata) |
40 result.mimeType = metadata.mimeType; | 63 result.mimeType = metadata.mimeType; |
64 if ('thumbnail' in metadata) | |
65 result.thumbnail = metadata.thumbnail; | |
41 return result; | 66 return result; |
42 } | 67 } |
43 | 68 |
44 /** | 69 /** |
45 * Massages arguments of an event raised by the File System Provider API. | 70 * Massages arguments of an event raised by the File System Provider API. |
46 * @param {Array.<*>} args Input arguments. | 71 * @param {Array.<*>} args Input arguments. |
47 * @param {function(Array.<*>)} dispatch Closure to be called with massaged | 72 * @param {function(Array.<*>)} dispatch Closure to be called with massaged |
48 * arguments. | 73 * arguments. |
49 */ | 74 */ |
50 function massageArgumentsDefault(args, dispatch) { | 75 function massageArgumentsDefault(args, dispatch) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 eventBindings.registerArgumentMassager( | 159 eventBindings.registerArgumentMassager( |
135 'fileSystemProvider.onUnmountRequested', | 160 'fileSystemProvider.onUnmountRequested', |
136 massageArgumentsDefault); | 161 massageArgumentsDefault); |
137 | 162 |
138 eventBindings.registerArgumentMassager( | 163 eventBindings.registerArgumentMassager( |
139 'fileSystemProvider.onGetMetadataRequested', | 164 'fileSystemProvider.onGetMetadataRequested', |
140 function(args, dispatch) { | 165 function(args, dispatch) { |
141 var executionStart = Date.now(); | 166 var executionStart = Date.now(); |
142 var options = args[0]; | 167 var options = args[0]; |
143 var onSuccessCallback = function(metadata) { | 168 var onSuccessCallback = function(metadata) { |
169 // It is invalid to return a thumbnail when it's not requested. The | |
170 // restriction is added in order to avoid fetching the thumbnail while | |
171 // it's not needed. | |
172 if (!options.thumbnail && metadata.thumbnail) { | |
173 fileSystemProviderInternal.operationRequestedError( | |
174 options.fileSystemId, options.requestId, 'FAILED', | |
175 Date.now() - executionStart); | |
176 throw new Error('Thumbnail data provided, but not requested.'); | |
177 } | |
178 | |
179 // Check the format and size. Note, that in the C++ layer, there is | |
180 // another sanity check to avoid passing any evil URL. | |
181 if ('thumbnail' in metadata && !verifyImageURI(metadata.thumbnail)) | |
182 throw new Error('Thumbnail format invalid.'); | |
183 if ('thumbnail' in metadata && | |
184 metadata.thumbnail.length > METADATA_THUMBNAIL_SIZE_LIMIT) { | |
185 throw new Error('Thumbnail data too large.'); | |
186 } | |
187 | |
144 fileSystemProviderInternal.getMetadataRequestedSuccess( | 188 fileSystemProviderInternal.getMetadataRequestedSuccess( |
145 options.fileSystemId, | 189 options.fileSystemId, |
146 options.requestId, | 190 options.requestId, |
147 annotateMetadata(metadata), | 191 annotateMetadata(metadata), |
148 Date.now() - executionStart); | 192 Date.now() - executionStart); |
149 }; | 193 }; |
150 var onErrorCallback = function(error) { | 194 var onErrorCallback = function(error) { |
151 fileSystemProviderInternal.operationRequestedError( | 195 fileSystemProviderInternal.operationRequestedError( |
152 options.fileSystemId, options.requestId, error, | 196 options.fileSystemId, options.requestId, error, |
153 Date.now() - executionStart); | 197 Date.now() - executionStart); |
154 } | 198 } |
155 dispatch([options, onSuccessCallback, onErrorCallback]); | 199 dispatch([options, onSuccessCallback, onErrorCallback]); |
156 }); | 200 }); |
157 | 201 |
158 eventBindings.registerArgumentMassager( | 202 eventBindings.registerArgumentMassager( |
159 'fileSystemProvider.onReadDirectoryRequested', | 203 'fileSystemProvider.onReadDirectoryRequested', |
160 function(args, dispatch) { | 204 function(args, dispatch) { |
161 var executionStart = Date.now(); | 205 var executionStart = Date.now(); |
162 var options = args[0]; | 206 var options = args[0]; |
163 var onSuccessCallback = function(entries, hasNext) { | 207 var onSuccessCallback = function(entries, hasNext) { |
164 var annotatedEntries = entries.map(annotateMetadata); | 208 var annotatedEntries = entries.map(annotateMetadata); |
209 // It is invalid to return a thumbnail when it's not requested. | |
210 annotatedEntries.forEach(function(metadata) { | |
211 if (metadata.thumbnail) { | |
212 fileSystemProviderInternal.operationRequestedError( | |
213 options.fileSystemId, options.requestId, 'FAILED', | |
214 Date.now() - executionStart); | |
215 throw new Error( | |
216 'Thumbnails must not be provided when reading a directory.'); | |
217 } | |
218 }); | |
165 fileSystemProviderInternal.readDirectoryRequestedSuccess( | 219 fileSystemProviderInternal.readDirectoryRequestedSuccess( |
166 options.fileSystemId, options.requestId, annotatedEntries, hasNext, | 220 options.fileSystemId, options.requestId, annotatedEntries, hasNext, |
167 Date.now() - executionStart); | 221 Date.now() - executionStart); |
168 }; | 222 }; |
169 var onErrorCallback = function(error) { | 223 var onErrorCallback = function(error) { |
170 fileSystemProviderInternal.operationRequestedError( | 224 fileSystemProviderInternal.operationRequestedError( |
171 options.fileSystemId, options.requestId, error, | 225 options.fileSystemId, options.requestId, error, |
172 Date.now() - executionStart); | 226 Date.now() - executionStart); |
173 } | 227 } |
174 dispatch([options, onSuccessCallback, onErrorCallback]); | 228 dispatch([options, onSuccessCallback, onErrorCallback]); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 | 280 |
227 eventBindings.registerArgumentMassager( | 281 eventBindings.registerArgumentMassager( |
228 'fileSystemProvider.onWriteFileRequested', | 282 'fileSystemProvider.onWriteFileRequested', |
229 massageArgumentsDefault); | 283 massageArgumentsDefault); |
230 | 284 |
231 eventBindings.registerArgumentMassager( | 285 eventBindings.registerArgumentMassager( |
232 'fileSystemProvider.onAbortRequested', | 286 'fileSystemProvider.onAbortRequested', |
233 massageArgumentsDefault); | 287 massageArgumentsDefault); |
234 | 288 |
235 exports.binding = binding.generate(); | 289 exports.binding = binding.generate(); |
OLD | NEW |