| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 fileSystem API. | 5 // Custom binding for the fileSystem API. |
| 6 | 6 |
| 7 var binding = require('binding').Binding.create('fileSystem'); | 7 var binding = require('binding').Binding.create('fileSystem'); |
| 8 | 8 |
| 9 var fileSystemNatives = requireNative('file_system_natives'); | 9 var fileSystemNatives = requireNative('file_system_natives'); |
| 10 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; | 10 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 var bindFileEntryCallback = function(functionName, apiFunctions) { | 26 var bindFileEntryCallback = function(functionName, apiFunctions) { |
| 27 apiFunctions.setCustomCallback(functionName, | 27 apiFunctions.setCustomCallback(functionName, |
| 28 function(name, request, response) { | 28 function(name, request, response) { |
| 29 if (request.callback && response) { | 29 if (request.callback && response) { |
| 30 var callback = request.callback; | 30 var callback = request.callback; |
| 31 request.callback = null; | 31 request.callback = null; |
| 32 | 32 |
| 33 var entries = []; | 33 var entries = []; |
| 34 var hasError = false; | 34 var hasError = false; |
| 35 | 35 |
| 36 var getEntryError = function(fileError) { |
| 37 if (!hasError) { |
| 38 hasError = true; |
| 39 lastError.run( |
| 40 'fileSystem.' + functionName, |
| 41 'Error getting fileEntry, code: ' + fileError.code, |
| 42 request.stack, |
| 43 callback); |
| 44 } |
| 45 } |
| 46 |
| 36 // Loop through the response entries and asynchronously get the | 47 // Loop through the response entries and asynchronously get the |
| 37 // FileEntry for each. We use hasError to ensure that only the first | 48 // FileEntry for each. We use hasError to ensure that only the first |
| 38 // error is reported. Note that an error can occur either during the | 49 // error is reported. Note that an error can occur either during the |
| 39 // loop or in the asynchronous error callback to getFile. | 50 // loop or in the asynchronous error callback to getFile. |
| 40 $Array.forEach(response.entries, function(entry) { | 51 $Array.forEach(response.entries, function(entry) { |
| 41 if (hasError) | 52 if (hasError) |
| 42 return; | 53 return; |
| 43 var fileSystemId = entry.fileSystemId; | 54 var fileSystemId = entry.fileSystemId; |
| 44 var baseName = entry.baseName; | 55 var baseName = entry.baseName; |
| 45 var id = entry.id; | 56 var id = entry.id; |
| 46 var fs = GetIsolatedFileSystem(fileSystemId); | 57 var fs = GetIsolatedFileSystem(fileSystemId); |
| 47 | 58 |
| 48 try { | 59 try { |
| 49 // TODO(koz): fs.root.getFile() makes a trip to the browser process, | 60 var getEntryCallback = function(fileEntry) { |
| 50 // but it might be possible avoid that by calling | |
| 51 // WebFrame::createFileEntry(). | |
| 52 fs.root.getFile(baseName, {}, function(fileEntry) { | |
| 53 if (hasError) | 61 if (hasError) |
| 54 return; | 62 return; |
| 55 entryIdManager.registerEntry(id, fileEntry); | 63 entryIdManager.registerEntry(id, fileEntry); |
| 56 entries.push(fileEntry); | 64 entries.push(fileEntry); |
| 57 // Once all entries are ready, pass them to the callback. In the | 65 // Once all entries are ready, pass them to the callback. In the |
| 58 // event of an error, this condition will never be satisfied so | 66 // event of an error, this condition will never be satisfied so |
| 59 // the callback will not be called with any entries. | 67 // the callback will not be called with any entries. |
| 60 if (entries.length == response.entries.length) { | 68 if (entries.length == response.entries.length) { |
| 61 if (response.multiple) { | 69 if (response.multiple) { |
| 62 callback(entries); | 70 callback(entries); |
| 63 } else { | 71 } else { |
| 64 callback(entries[0]); | 72 callback(entries[0]); |
| 65 } | 73 } |
| 66 } | 74 } |
| 67 }, function(fileError) { | 75 } |
| 68 if (!hasError) { | 76 // TODO(koz): fs.root.getFile() makes a trip to the browser process, |
| 69 hasError = true; | 77 // but it might be possible avoid that by calling |
| 70 lastError.run( | 78 // WebFrame::createFileEntry(). |
| 71 'fileSystem.' + functionName, | 79 if (entry.isDirectory) { |
| 72 'Error getting fileEntry, code: ' + fileError.code, | 80 fs.root.getDirectory(baseName, {}, getEntryCallback, |
| 73 request.stack, | 81 getEntryError); |
| 74 callback); | 82 } else { |
| 75 } | 83 fs.root.getFile(baseName, {}, getEntryCallback, getEntryError); |
| 76 }); | 84 } |
| 77 } catch (e) { | 85 } catch (e) { |
| 78 if (!hasError) { | 86 if (!hasError) { |
| 79 hasError = true; | 87 hasError = true; |
| 80 lastError.run('fileSystem.' + functionName, | 88 lastError.run('fileSystem.' + functionName, |
| 81 'Error getting fileEntry: ' + e.stack, | 89 'Error getting fileEntry: ' + e.stack, |
| 82 request.stack, | 90 request.stack, |
| 83 callback); | 91 callback); |
| 84 } | 92 } |
| 85 } | 93 } |
| 86 }); | 94 }); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 178 |
| 171 fileSystem.chooseFile = function() { | 179 fileSystem.chooseFile = function() { |
| 172 console.log("chrome.fileSystem.chooseFile is deprecated"); | 180 console.log("chrome.fileSystem.chooseFile is deprecated"); |
| 173 console.log("Please use chrome.fileSystem.chooseEntry instead"); | 181 console.log("Please use chrome.fileSystem.chooseEntry instead"); |
| 174 $Function.apply(fileSystem.chooseEntry, this, arguments); | 182 $Function.apply(fileSystem.chooseEntry, this, arguments); |
| 175 }; | 183 }; |
| 176 }); | 184 }); |
| 177 | 185 |
| 178 exports.bindFileEntryCallback = bindFileEntryCallback; | 186 exports.bindFileEntryCallback = bindFileEntryCallback; |
| 179 exports.binding = binding.generate(); | 187 exports.binding = binding.generate(); |
| OLD | NEW |