Chromium Code Reviews| 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 $Array.forEach(response.entries, function(entry) { |
| 39 // TODO(koz): fs.root.getFile() makes a trip to the browser process, | 37 var fileSystemId = entry.fileSystemId; |
| 40 // but it might be possible avoid that by calling | 38 var baseName = entry.baseName; |
| 41 // WebFrame::createFileEntry(). | 39 var id = entry.id; |
| 42 fs.root.getFile(baseName, {}, function(fileEntry) { | 40 var fs = GetIsolatedFileSystem(fileSystemId); |
|
benwells
2013/08/01 07:48:33
Should you check hasError here instead of in the e
Sam McNally
2013/08/02 05:35:31
Added a comment describing what's going on here.
| |
| 43 entryIdManager.registerEntry(id, fileEntry); | 41 |
| 44 callback(fileEntry); | 42 try { |
| 45 }, function(fileError) { | 43 // TODO(koz): fs.root.getFile() makes a trip to the browser process, |
| 46 lastError.run('fileSystem.' + functionName, | 44 // but it might be possible avoid that by calling |
| 47 'Error getting fileEntry, code: ' + fileError.code, | 45 // WebFrame::createFileEntry(). |
| 48 request.stack, | 46 fs.root.getFile(baseName, {}, function(fileEntry) { |
| 49 callback); | 47 entryIdManager.registerEntry(id, fileEntry); |
| 50 }); | 48 entries.push(fileEntry); |
| 51 } catch (e) { | 49 // Once all entries are ready, pass them to the callback. |
| 52 lastError.run('fileSystem.' + functionName, | 50 if (entries.length == response.entries.length) { |
| 53 'Error in event handler for onLaunched: ' + e.stack, | 51 if (response.multiple) { |
| 54 request.stack, | 52 callback(entries); |
| 55 callback); | 53 } else { |
| 56 } | 54 callback(entries[0]); |
| 55 } | |
| 56 } | |
| 57 }, function(fileError) { | |
| 58 if (!hasError) { | |
| 59 hasError = true; | |
| 60 lastError.run( | |
| 61 'fileSystem.' + functionName, | |
| 62 'Error getting fileEntry, code: ' + fileError.code, | |
| 63 request.stack, | |
| 64 callback); | |
| 65 } | |
| 66 }); | |
| 67 } catch (e) { | |
| 68 if (!hasError) { | |
| 69 hasError = true; | |
| 70 lastError.run('fileSystem.' + functionName, | |
| 71 'Error getting fileEntry: ' + e.stack, | |
| 72 request.stack, | |
| 73 callback); | |
| 74 } | |
| 75 } | |
| 76 }); | |
| 57 } | 77 } |
| 58 }); | 78 }); |
| 59 }; | 79 }; |
| 60 var entryIdManager = require('entryIdManager'); | 80 var entryIdManager = require('entryIdManager'); |
| 61 } else { | 81 } else { |
| 62 // Force the fileSystem API to be loaded in the background page. Using | 82 // Force the fileSystem API to be loaded in the background page. Using |
| 63 // backgroundPageModuleSystem.require('fileSystem') is insufficient as | 83 // backgroundPageModuleSystem.require('fileSystem') is insufficient as |
| 64 // requireNative is only allowed while lazily loading an API. | 84 // requireNative is only allowed while lazily loading an API. |
| 65 backgroundPage.chrome.fileSystem; | 85 backgroundPage.chrome.fileSystem; |
| 66 var bindFileEntryCallback = backgroundPageModuleSystem.require( | 86 var bindFileEntryCallback = backgroundPageModuleSystem.require( |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 | 160 |
| 141 fileSystem.chooseFile = function() { | 161 fileSystem.chooseFile = function() { |
| 142 console.log("chrome.fileSystem.chooseFile is deprecated"); | 162 console.log("chrome.fileSystem.chooseFile is deprecated"); |
| 143 console.log("Please use chrome.fileSystem.chooseEntry instead"); | 163 console.log("Please use chrome.fileSystem.chooseEntry instead"); |
| 144 $Function.apply(fileSystem.chooseEntry, this, arguments); | 164 $Function.apply(fileSystem.chooseEntry, this, arguments); |
| 145 }; | 165 }; |
| 146 }); | 166 }); |
| 147 | 167 |
| 148 exports.bindFileEntryCallback = bindFileEntryCallback; | 168 exports.bindFileEntryCallback = bindFileEntryCallback; |
| 149 exports.binding = binding.generate(); | 169 exports.binding = binding.generate(); |
| OLD | NEW |