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 // The URL may be very large, but we care only about the prefix. | |
44 var uriLowerCase = uri.substr(0, 32).toLowerCase(); | |
hirono
2014/08/29 06:02:41
Sorry I re-thought of this. Using regexp is better
mtomasz
2014/08/29 06:17:48
Done.
| |
45 | |
46 return uriLowerCase.indexOf('data:image/png;') == 0 || | |
47 uriLowerCase.indexOf('data:image/jpeg;') == 0 || | |
48 uriLowerCase.indexOf('data:image/webp;') == 0; | |
49 } | |
50 | |
51 /** | |
27 * Annotates an entry metadata by serializing its modifiedTime value. | 52 * Annotates an entry metadata by serializing its modifiedTime value. |
28 * @param {EntryMetadata} metadata Input metadata. | 53 * @param {EntryMetadata} metadata Input metadata. |
29 * @return {EntryMetadata} metadata Annotated metadata, which can be passed | 54 * @return {EntryMetadata} metadata Annotated metadata, which can be passed |
30 * back to the C++ layer. | 55 * back to the C++ layer. |
31 */ | 56 */ |
32 function annotateMetadata(metadata) { | 57 function annotateMetadata(metadata) { |
33 var result = { | 58 var result = { |
34 isDirectory: metadata.isDirectory, | 59 isDirectory: metadata.isDirectory, |
35 name: metadata.name, | 60 name: metadata.name, |
36 size: metadata.size, | 61 size: metadata.size, |
37 modificationTime: annotateDate(metadata.modificationTime) | 62 modificationTime: annotateDate(metadata.modificationTime) |
38 }; | 63 }; |
39 if ('mimeType' in metadata) | 64 if ('mimeType' in metadata) |
40 result.mimeType = metadata.mimeType; | 65 result.mimeType = metadata.mimeType; |
66 if ('thumbnail' in metadata) | |
67 result.thumbnail = metadata.thumbnail; | |
41 return result; | 68 return result; |
42 } | 69 } |
43 | 70 |
44 /** | 71 /** |
45 * Massages arguments of an event raised by the File System Provider API. | 72 * Massages arguments of an event raised by the File System Provider API. |
46 * @param {Array.<*>} args Input arguments. | 73 * @param {Array.<*>} args Input arguments. |
47 * @param {function(Array.<*>)} dispatch Closure to be called with massaged | 74 * @param {function(Array.<*>)} dispatch Closure to be called with massaged |
48 * arguments. | 75 * arguments. |
49 */ | 76 */ |
50 function massageArgumentsDefault(args, dispatch) { | 77 function massageArgumentsDefault(args, dispatch) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 eventBindings.registerArgumentMassager( | 161 eventBindings.registerArgumentMassager( |
135 'fileSystemProvider.onUnmountRequested', | 162 'fileSystemProvider.onUnmountRequested', |
136 massageArgumentsDefault); | 163 massageArgumentsDefault); |
137 | 164 |
138 eventBindings.registerArgumentMassager( | 165 eventBindings.registerArgumentMassager( |
139 'fileSystemProvider.onGetMetadataRequested', | 166 'fileSystemProvider.onGetMetadataRequested', |
140 function(args, dispatch) { | 167 function(args, dispatch) { |
141 var executionStart = Date.now(); | 168 var executionStart = Date.now(); |
142 var options = args[0]; | 169 var options = args[0]; |
143 var onSuccessCallback = function(metadata) { | 170 var onSuccessCallback = function(metadata) { |
171 // It is invalid to return a thumbnail when it's not requested. The | |
172 // restriction is added in order to avoid fetching the thumbnail while | |
173 // it's not needed. | |
174 if (!options.thumbnail && metadata.thumbnail) { | |
175 fileSystemProviderInternal.operationRequestedError( | |
176 options.fileSystemId, options.requestId, 'FAILED', | |
177 Date.now() - executionStart); | |
178 throw new Error('Thumbnail data provided, but not requested.'); | |
179 } | |
180 | |
181 // Check the format and size. Note, that in the C++ layer, there is | |
182 // another sanity check to avoid passing any evil URL. | |
183 if ('thumbnail' in metadata && !verifyImageURI(metadata.thumbnail)) | |
184 throw new Error('Thumbnail format invalid.'); | |
185 if ('thumbnail' in metadata && | |
186 metadata.thumbnail.length > METADATA_THUMBNAIL_SIZE_LIMIT) { | |
187 throw new Error('Thumbnail data too large.'); | |
188 } | |
189 | |
144 fileSystemProviderInternal.getMetadataRequestedSuccess( | 190 fileSystemProviderInternal.getMetadataRequestedSuccess( |
145 options.fileSystemId, | 191 options.fileSystemId, |
146 options.requestId, | 192 options.requestId, |
147 annotateMetadata(metadata), | 193 annotateMetadata(metadata), |
148 Date.now() - executionStart); | 194 Date.now() - executionStart); |
149 }; | 195 }; |
150 var onErrorCallback = function(error) { | 196 var onErrorCallback = function(error) { |
151 fileSystemProviderInternal.operationRequestedError( | 197 fileSystemProviderInternal.operationRequestedError( |
152 options.fileSystemId, options.requestId, error, | 198 options.fileSystemId, options.requestId, error, |
153 Date.now() - executionStart); | 199 Date.now() - executionStart); |
154 } | 200 } |
155 dispatch([options, onSuccessCallback, onErrorCallback]); | 201 dispatch([options, onSuccessCallback, onErrorCallback]); |
156 }); | 202 }); |
157 | 203 |
158 eventBindings.registerArgumentMassager( | 204 eventBindings.registerArgumentMassager( |
159 'fileSystemProvider.onReadDirectoryRequested', | 205 'fileSystemProvider.onReadDirectoryRequested', |
160 function(args, dispatch) { | 206 function(args, dispatch) { |
161 var executionStart = Date.now(); | 207 var executionStart = Date.now(); |
162 var options = args[0]; | 208 var options = args[0]; |
163 var onSuccessCallback = function(entries, hasNext) { | 209 var onSuccessCallback = function(entries, hasNext) { |
164 var annotatedEntries = entries.map(annotateMetadata); | 210 var annotatedEntries = entries.map(annotateMetadata); |
211 // It is invalid to return a thumbnail when it's not requested. | |
212 annotatedEntries.forEach(function(metadata) { | |
213 if (metadata.thumbnail) { | |
214 fileSystemProviderInternal.operationRequestedError( | |
215 options.fileSystemId, options.requestId, 'FAILED', | |
216 Date.now() - executionStart); | |
217 throw new Error( | |
218 'Thumbnails must not be provided when reading a directory.'); | |
219 } | |
220 }); | |
165 fileSystemProviderInternal.readDirectoryRequestedSuccess( | 221 fileSystemProviderInternal.readDirectoryRequestedSuccess( |
166 options.fileSystemId, options.requestId, annotatedEntries, hasNext, | 222 options.fileSystemId, options.requestId, annotatedEntries, hasNext, |
167 Date.now() - executionStart); | 223 Date.now() - executionStart); |
168 }; | 224 }; |
169 var onErrorCallback = function(error) { | 225 var onErrorCallback = function(error) { |
170 fileSystemProviderInternal.operationRequestedError( | 226 fileSystemProviderInternal.operationRequestedError( |
171 options.fileSystemId, options.requestId, error, | 227 options.fileSystemId, options.requestId, error, |
172 Date.now() - executionStart); | 228 Date.now() - executionStart); |
173 } | 229 } |
174 dispatch([options, onSuccessCallback, onErrorCallback]); | 230 dispatch([options, onSuccessCallback, onErrorCallback]); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 | 282 |
227 eventBindings.registerArgumentMassager( | 283 eventBindings.registerArgumentMassager( |
228 'fileSystemProvider.onWriteFileRequested', | 284 'fileSystemProvider.onWriteFileRequested', |
229 massageArgumentsDefault); | 285 massageArgumentsDefault); |
230 | 286 |
231 eventBindings.registerArgumentMassager( | 287 eventBindings.registerArgumentMassager( |
232 'fileSystemProvider.onAbortRequested', | 288 'fileSystemProvider.onAbortRequested', |
233 massageArgumentsDefault); | 289 massageArgumentsDefault); |
234 | 290 |
235 exports.binding = binding.generate(); | 291 exports.binding = binding.generate(); |
OLD | NEW |