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 var sendRequest = require('sendRequest'); |
8 | 9 |
9 var fileSystemNatives = requireNative('file_system_natives'); | 10 var getFileBindingsForApi = |
10 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; | 11 require('fileEntryBindingUtil').getFileBindingsForApi; |
11 var lastError = require('lastError'); | 12 var fileBindings = getFileBindingsForApi('fileSystem'); |
12 var sendRequest = require('sendRequest'); | 13 var bindFileEntryCallback = fileBindings.bindFileEntryCallback; |
13 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; | 14 var entryIdManager = fileBindings.entryIdManager; |
14 // TODO(sammc): Don't require extension. See http://crbug.com/235689. | |
15 var GetExtensionViews = requireNative('runtime').GetExtensionViews; | |
16 | |
17 // Fallback to using the current window if no background page is running. | |
18 var backgroundPage = GetExtensionViews(-1, 'BACKGROUND')[0] || window; | |
19 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); | |
20 | |
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 | |
23 // allows them to be used from other windows (including the background page) | |
24 // after the original window is closed. | |
25 if (window == backgroundPage) { | |
26 var bindFileEntryCallback = function(functionName, apiFunctions) { | |
27 apiFunctions.setCustomCallback(functionName, | |
28 function(name, request, response) { | |
29 if (request.callback && response) { | |
30 var callback = request.callback; | |
31 request.callback = null; | |
32 | |
33 var entries = []; | |
34 var hasError = false; | |
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 | |
47 // Loop through the response entries and asynchronously get the | |
48 // FileEntry for each. We use hasError to ensure that only the first | |
49 // error is reported. Note that an error can occur either during the | |
50 // loop or in the asynchronous error callback to getFile. | |
51 $Array.forEach(response.entries, function(entry) { | |
52 if (hasError) | |
53 return; | |
54 var fileSystemId = entry.fileSystemId; | |
55 var baseName = entry.baseName; | |
56 var id = entry.id; | |
57 var fs = GetIsolatedFileSystem(fileSystemId); | |
58 | |
59 try { | |
60 var getEntryCallback = function(fileEntry) { | |
61 if (hasError) | |
62 return; | |
63 entryIdManager.registerEntry(id, fileEntry); | |
64 entries.push(fileEntry); | |
65 // Once all entries are ready, pass them to the callback. In the | |
66 // event of an error, this condition will never be satisfied so | |
67 // the callback will not be called with any entries. | |
68 if (entries.length == response.entries.length) { | |
69 if (response.multiple) { | |
70 sendRequest.safeCallbackApply( | |
71 'fileSystem.' + functionName, request, callback, | |
72 [entries]); | |
73 } else { | |
74 sendRequest.safeCallbackApply( | |
75 'fileSystem.' + functionName, request, callback, | |
76 [entries[0]]); | |
77 } | |
78 } | |
79 } | |
80 // TODO(koz): fs.root.getFile() makes a trip to the browser process, | |
81 // but it might be possible avoid that by calling | |
82 // WebDOMFileSystem::createV8Entry(). | |
83 if (entry.isDirectory) { | |
84 fs.root.getDirectory(baseName, {}, getEntryCallback, | |
85 getEntryError); | |
86 } else { | |
87 fs.root.getFile(baseName, {}, getEntryCallback, getEntryError); | |
88 } | |
89 } catch (e) { | |
90 if (!hasError) { | |
91 hasError = true; | |
92 lastError.run('fileSystem.' + functionName, | |
93 'Error getting fileEntry: ' + e.stack, | |
94 request.stack, | |
95 callback); | |
96 } | |
97 } | |
98 }); | |
99 } | |
100 }); | |
101 }; | |
102 var entryIdManager = require('entryIdManager'); | |
103 } else { | |
104 // Force the fileSystem API to be loaded in the background page. Using | |
105 // backgroundPageModuleSystem.require('fileSystem') is insufficient as | |
106 // requireNative is only allowed while lazily loading an API. | |
107 backgroundPage.chrome.fileSystem; | |
108 var bindFileEntryCallback = backgroundPageModuleSystem.require( | |
109 'fileSystem').bindFileEntryCallback; | |
110 var entryIdManager = backgroundPageModuleSystem.require('entryIdManager'); | |
111 } | |
112 | 15 |
113 binding.registerCustomHook(function(bindingsAPI) { | 16 binding.registerCustomHook(function(bindingsAPI) { |
114 var apiFunctions = bindingsAPI.apiFunctions; | 17 var apiFunctions = bindingsAPI.apiFunctions; |
115 var fileSystem = bindingsAPI.compiledApi; | 18 var fileSystem = bindingsAPI.compiledApi; |
116 | 19 |
117 function bindFileEntryFunction(functionName) { | 20 function bindFileEntryFunction(functionName) { |
118 apiFunctions.setUpdateArgumentsPostValidate( | 21 apiFunctions.setUpdateArgumentsPostValidate( |
119 functionName, function(fileEntry, callback) { | 22 functionName, function(fileEntry, callback) { |
120 var fileSystemName = fileEntry.filesystem.name; | 23 var fileSystemName = fileEntry.filesystem.name; |
121 var relativePath = $String.slice(fileEntry.fullPath, 1); | 24 var relativePath = $String.slice(fileEntry.fullPath, 1); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 | 94 |
192 fileSystem.chooseFile = function() { | 95 fileSystem.chooseFile = function() { |
193 console.log("chrome.fileSystem.chooseFile is deprecated"); | 96 console.log("chrome.fileSystem.chooseFile is deprecated"); |
194 console.log("Please use chrome.fileSystem.chooseEntry instead"); | 97 console.log("Please use chrome.fileSystem.chooseEntry instead"); |
195 $Function.apply(fileSystem.chooseEntry, this, arguments); | 98 $Function.apply(fileSystem.chooseEntry, this, arguments); |
196 }; | 99 }; |
197 }); | 100 }); |
198 | 101 |
199 exports.bindFileEntryCallback = bindFileEntryCallback; | 102 exports.bindFileEntryCallback = bindFileEntryCallback; |
200 exports.binding = binding.generate(); | 103 exports.binding = binding.generate(); |
OLD | NEW |