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; |
11 var lastError = require('lastError'); | 11 var lastError = require('lastError'); |
12 var sendRequest = require('sendRequest').sendRequest; | 12 var sendRequest = require('sendRequest').sendRequest; |
13 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; | 13 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; |
14 // TODO(sammc): Don't require extension. See http://crbug.com/235689. | 14 // TODO(sammc): Don't require extension. See http://crbug.com/235689. |
15 var GetExtensionViews = requireNative('extension').GetExtensionViews; | 15 var GetExtensionViews = requireNative('extension').GetExtensionViews; |
16 | 16 |
17 // Fallback to using the current window if no background page is running. | 17 // Fallback to using the current window if no background page is running. |
18 var backgroundPage = GetExtensionViews(-1, 'BACKGROUND')[0] || window; | 18 var backgroundPage = GetExtensionViews(-1, 'BACKGROUND')[0] || window; |
19 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); | 19 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); |
20 | 20 |
21 // All windows use the bindFileEntryCallback from the background page so their | 21 // All windows use the bindFileEntryCallback from the background page so their |
22 // FileEntry objects have the background page's context as their own. This | 22 // FileEntry objects have the background page's context as their own. This |
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 && response) { |
Matt Giuca
2013/07/19 02:28:11
response.entries?
(Otherwise, why response && resp
Sam McNally
2013/07/19 04:22:14
Done.
| |
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; | 35 var maybeDone = function() { |
Matt Giuca
2013/07/19 02:28:11
Comment here to explain the fan-out.
Sam McNally
2013/07/19 04:22:14
Done.
| |
36 var fs = GetIsolatedFileSystem(fileSystemId); | 36 if (entries.length == response.entries.length) { |
37 if (response.multiple) { | |
38 callback(entries); | |
39 } else { | |
40 callback(entries[0]); | |
41 } | |
42 } | |
43 }; | |
37 | 44 |
38 try { | 45 $Array.forEach(response.entries, function(entry) { |
39 // TODO(koz): fs.root.getFile() makes a trip to the browser process, | 46 var fileSystemId = entry.fileSystemId; |
40 // but it might be possible avoid that by calling | 47 var baseName = entry.baseName; |
41 // WebFrame::createFileEntry(). | 48 var id = entry.id; |
42 fs.root.getFile(baseName, {}, function(fileEntry) { | 49 var fs = GetIsolatedFileSystem(fileSystemId); |
43 entryIdManager.registerEntry(id, fileEntry); | 50 |
44 callback(fileEntry); | 51 try { |
45 }, function(fileError) { | 52 // TODO(koz): fs.root.getFile() makes a trip to the browser process, |
46 lastError.run('fileSystem.' + functionName, | 53 // but it might be possible avoid that by calling |
47 'Error getting fileEntry, code: ' + fileError.code, | 54 // WebFrame::createFileEntry(). |
48 request.stack, | 55 fs.root.getFile(baseName, {}, function(fileEntry) { |
49 callback); | 56 entryIdManager.registerEntry(id, fileEntry); |
50 }); | 57 entries.push(fileEntry); |
51 } catch (e) { | 58 maybeDone(); |
Matt Giuca
2013/07/19 02:28:11
Why is maybeDone a function? I expected it to be u
Sam McNally
2013/07/19 04:22:14
Done.
| |
52 lastError.run('fileSystem.' + functionName, | 59 }, function(fileError) { |
53 'Error in event handler for onLaunched: ' + e.stack, | 60 if (!hasError) { |
54 request.stack, | 61 hasError = true; |
55 callback); | 62 lastError.run( |
56 } | 63 'fileSystem.' + functionName, |
64 'Error getting fileEntry, code: ' + fileError.code, | |
65 request.stack, | |
66 callback); | |
67 } | |
68 }); | |
69 } catch (e) { | |
70 if (!hasError) { | |
71 hasError = true; | |
72 lastError.run('fileSystem.' + functionName, | |
73 'Error getting fileEntry: ' + e.stack, | |
74 request.stack, | |
75 callback); | |
76 } | |
77 } | |
78 }); | |
57 } | 79 } |
58 }); | 80 }); |
59 }; | 81 }; |
60 var entryIdManager = require('entryIdManager'); | 82 var entryIdManager = require('entryIdManager'); |
61 } else { | 83 } else { |
62 // Force the fileSystem API to be loaded in the background page. Using | 84 // Force the fileSystem API to be loaded in the background page. Using |
63 // backgroundPageModuleSystem.require('fileSystem') is insufficient as | 85 // backgroundPageModuleSystem.require('fileSystem') is insufficient as |
64 // requireNative is only allowed while lazily loading an API. | 86 // requireNative is only allowed while lazily loading an API. |
65 backgroundPage.chrome.fileSystem; | 87 backgroundPage.chrome.fileSystem; |
66 var bindFileEntryCallback = backgroundPageModuleSystem.require( | 88 var bindFileEntryCallback = backgroundPageModuleSystem.require( |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 | 162 |
141 fileSystem.chooseFile = function() { | 163 fileSystem.chooseFile = function() { |
142 console.log("chrome.fileSystem.chooseFile is deprecated"); | 164 console.log("chrome.fileSystem.chooseFile is deprecated"); |
143 console.log("Please use chrome.fileSystem.chooseEntry instead"); | 165 console.log("Please use chrome.fileSystem.chooseEntry instead"); |
144 $Function.apply(fileSystem.chooseEntry, this, arguments); | 166 $Function.apply(fileSystem.chooseEntry, this, arguments); |
145 }; | 167 }; |
146 }); | 168 }); |
147 | 169 |
148 exports.bindFileEntryCallback = bindFileEntryCallback; | 170 exports.bindFileEntryCallback = bindFileEntryCallback; |
149 exports.binding = binding.generate(); | 171 exports.binding = binding.generate(); |
OLD | NEW |