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