| 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 12 matching lines...) Expand all Loading... |
| 23 // allows them to be used from other windows (including the background page) | 23 // allows them to be used from other windows (including the background page) |
| 24 // after the original window is closed. | 24 // after the original window is closed. |
| 25 if (window == backgroundPage) { | 25 if (window == backgroundPage) { |
| 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 fileSystemId = response.fileSystemId; | 33 var entries = []; |
| 34 var baseName = response.baseName; | 34 var hasError = false; |
| 35 var id = response.id; | |
| 36 var fs = GetIsolatedFileSystem(fileSystemId); | |
| 37 | 35 |
| 38 try { | 36 // Loop through the response entries and asynchronously get the |
| 39 // TODO(koz): fs.root.getFile() makes a trip to the browser process, | 37 // FileEntry for each. We use hasError to ensure that only the first |
| 40 // but it might be possible avoid that by calling | 38 // error is reported. Note that an error can occur either during the |
| 41 // WebFrame::createFileEntry(). | 39 // loop or in the asynchronous error callback to getFile. |
| 42 fs.root.getFile(baseName, {}, function(fileEntry) { | 40 $Array.forEach(response.entries, function(entry) { |
| 43 entryIdManager.registerEntry(id, fileEntry); | 41 if (hasError) |
| 44 callback(fileEntry); | 42 return; |
| 45 }, function(fileError) { | 43 var fileSystemId = entry.fileSystemId; |
| 46 lastError.run('fileSystem.' + functionName, | 44 var baseName = entry.baseName; |
| 47 'Error getting fileEntry, code: ' + fileError.code, | 45 var id = entry.id; |
| 48 request.stack, | 46 var fs = GetIsolatedFileSystem(fileSystemId); |
| 49 callback); | 47 |
| 50 }); | 48 try { |
| 51 } catch (e) { | 49 // TODO(koz): fs.root.getFile() makes a trip to the browser process, |
| 52 lastError.run('fileSystem.' + functionName, | 50 // but it might be possible avoid that by calling |
| 53 'Error in event handler for onLaunched: ' + e.stack, | 51 // WebFrame::createFileEntry(). |
| 54 request.stack, | 52 fs.root.getFile(baseName, {}, function(fileEntry) { |
| 55 callback); | 53 if (hasError) |
| 56 } | 54 return; |
| 55 entryIdManager.registerEntry(id, fileEntry); |
| 56 entries.push(fileEntry); |
| 57 // Once all entries are ready, pass them to the callback. In the |
| 58 // event of an error, this condition will never be satisfied so |
| 59 // the callback will not be called with any entries. |
| 60 if (entries.length == response.entries.length) { |
| 61 if (response.multiple) { |
| 62 callback(entries); |
| 63 } else { |
| 64 callback(entries[0]); |
| 65 } |
| 66 } |
| 67 }, function(fileError) { |
| 68 if (!hasError) { |
| 69 hasError = true; |
| 70 lastError.run( |
| 71 'fileSystem.' + functionName, |
| 72 'Error getting fileEntry, code: ' + fileError.code, |
| 73 request.stack, |
| 74 callback); |
| 75 } |
| 76 }); |
| 77 } catch (e) { |
| 78 if (!hasError) { |
| 79 hasError = true; |
| 80 lastError.run('fileSystem.' + functionName, |
| 81 'Error getting fileEntry: ' + e.stack, |
| 82 request.stack, |
| 83 callback); |
| 84 } |
| 85 } |
| 86 }); |
| 57 } | 87 } |
| 58 }); | 88 }); |
| 59 }; | 89 }; |
| 60 var entryIdManager = require('entryIdManager'); | 90 var entryIdManager = require('entryIdManager'); |
| 61 } else { | 91 } else { |
| 62 // Force the fileSystem API to be loaded in the background page. Using | 92 // Force the fileSystem API to be loaded in the background page. Using |
| 63 // backgroundPageModuleSystem.require('fileSystem') is insufficient as | 93 // backgroundPageModuleSystem.require('fileSystem') is insufficient as |
| 64 // requireNative is only allowed while lazily loading an API. | 94 // requireNative is only allowed while lazily loading an API. |
| 65 backgroundPage.chrome.fileSystem; | 95 backgroundPage.chrome.fileSystem; |
| 66 var bindFileEntryCallback = backgroundPageModuleSystem.require( | 96 var bindFileEntryCallback = backgroundPageModuleSystem.require( |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 170 |
| 141 fileSystem.chooseFile = function() { | 171 fileSystem.chooseFile = function() { |
| 142 console.log("chrome.fileSystem.chooseFile is deprecated"); | 172 console.log("chrome.fileSystem.chooseFile is deprecated"); |
| 143 console.log("Please use chrome.fileSystem.chooseEntry instead"); | 173 console.log("Please use chrome.fileSystem.chooseEntry instead"); |
| 144 $Function.apply(fileSystem.chooseEntry, this, arguments); | 174 $Function.apply(fileSystem.chooseEntry, this, arguments); |
| 145 }; | 175 }; |
| 146 }); | 176 }); |
| 147 | 177 |
| 148 exports.bindFileEntryCallback = bindFileEntryCallback; | 178 exports.bindFileEntryCallback = bindFileEntryCallback; |
| 149 exports.binding = binding.generate(); | 179 exports.binding = binding.generate(); |
| OLD | NEW |