Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
not at google - send to devlin
2014/07/23 01:52:07
2014
zel
2014/07/23 03:12:53
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 var fileSystemNatives = requireNative('file_system_natives'); | |
| 5 var GetIsolatedFileSystem = fileSystemNatives.GetIsolatedFileSystem; | |
| 6 var sendRequest = require('sendRequest'); | |
| 7 var lastError = require('lastError'); | |
| 8 var GetModuleSystem = requireNative('v8_context').GetModuleSystem; | |
| 9 // TODO(sammc): Don't require extension. See http://crbug.com/235689. | |
| 10 var GetExtensionViews = requireNative('runtime').GetExtensionViews; | |
| 11 | |
|
not at google - send to devlin
2014/07/23 01:52:07
comment?
zel
2014/07/23 03:12:52
Done.
| |
| 12 function getFileBindingsForApi(apiName) { | |
| 13 // Fallback to using the current window if no background page is running. | |
| 14 var backgroundPage = GetExtensionViews(-1, 'BACKGROUND')[0] || window; | |
| 15 var backgroundPageModuleSystem = GetModuleSystem(backgroundPage); | |
| 16 | |
| 17 // All windows use the bindFileEntryCallback from the background page so their | |
| 18 // FileEntry objects have the background page's context as their own. This | |
| 19 // allows them to be used from other windows (including the background page) | |
| 20 // after the original window is closed. | |
| 21 if (window == backgroundPage) { | |
| 22 var bindFileEntryCallback = function(functionName, apiFunctions) { | |
| 23 apiFunctions.setCustomCallback(functionName, | |
| 24 function(name, request, response) { | |
| 25 if (request.callback && response) { | |
| 26 var callback = request.callback; | |
| 27 request.callback = null; | |
| 28 | |
| 29 var entries = []; | |
| 30 var hasError = false; | |
| 31 | |
| 32 var getEntryError = function(fileError) { | |
| 33 if (!hasError) { | |
| 34 hasError = true; | |
| 35 lastError.run( | |
| 36 apiName + '.' + functionName, | |
| 37 'Error getting fileEntry, code: ' + fileError.code, | |
| 38 request.stack, | |
| 39 callback); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 // Loop through the response entries and asynchronously get the | |
| 44 // FileEntry for each. We use hasError to ensure that only the first | |
| 45 // error is reported. Note that an error can occur either during the | |
| 46 // loop or in the asynchronous error callback to getFile. | |
| 47 $Array.forEach(response.entries, function(entry) { | |
| 48 if (hasError) | |
| 49 return; | |
| 50 var fileSystemId = entry.fileSystemId; | |
| 51 var baseName = entry.baseName; | |
| 52 var id = entry.id; | |
| 53 var fs = GetIsolatedFileSystem(fileSystemId); | |
| 54 | |
| 55 try { | |
| 56 var getEntryCallback = function(fileEntry) { | |
| 57 if (hasError) | |
| 58 return; | |
| 59 entryIdManager.registerEntry(id, fileEntry); | |
| 60 entries.push(fileEntry); | |
| 61 // Once all entries are ready, pass them to the callback. In the | |
| 62 // event of an error, this condition will never be satisfied so | |
| 63 // the callback will not be called with any entries. | |
| 64 if (entries.length == response.entries.length) { | |
| 65 if (response.multiple) { | |
| 66 sendRequest.safeCallbackApply( | |
| 67 apiName + '.' + functionName, request, callback, | |
| 68 [entries]); | |
| 69 } else { | |
| 70 sendRequest.safeCallbackApply( | |
| 71 apiName + '.' + functionName, request, callback, | |
| 72 [entries[0]]); | |
| 73 } | |
| 74 } | |
| 75 } | |
| 76 // TODO(koz): fs.root.getFile() makes a trip to the browser | |
| 77 // process, but it might be possible avoid that by calling | |
| 78 // WebDOMFileSystem::createV8Entry(). | |
| 79 if (entry.isDirectory) { | |
| 80 fs.root.getDirectory(baseName, {}, getEntryCallback, | |
| 81 getEntryError); | |
| 82 } else { | |
| 83 fs.root.getFile(baseName, {}, getEntryCallback, getEntryError); | |
| 84 } | |
| 85 } catch (e) { | |
| 86 if (!hasError) { | |
| 87 hasError = true; | |
| 88 lastError.run(apiName + '.' + functionName, | |
| 89 'Error getting fileEntry: ' + e.stack, | |
| 90 request.stack, | |
| 91 callback); | |
| 92 } | |
| 93 } | |
| 94 }); | |
| 95 } | |
| 96 }); | |
| 97 }; | |
| 98 var entryIdManager = require('entryIdManager'); | |
| 99 } else { | |
| 100 // Force the fileSystem API to be loaded in the background page. Using | |
| 101 // backgroundPageModuleSystem.require('fileSystem') is insufficient as | |
| 102 // requireNative is only allowed while lazily loading an API. | |
| 103 backgroundPage.chrome.fileSystem; | |
| 104 var bindFileEntryCallback = backgroundPageModuleSystem.require( | |
| 105 apiName).bindFileEntryCallback; | |
| 106 var entryIdManager = backgroundPageModuleSystem.require('entryIdManager'); | |
| 107 } | |
| 108 return {bindFileEntryCallback: bindFileEntryCallback, | |
| 109 entryIdManager: entryIdManager}; | |
| 110 } | |
| 111 | |
| 112 exports.getFileBindingsForApi = getFileBindingsForApi; | |
| OLD | NEW |