Chromium Code Reviews| Index: ui/file_manager/gallery/js/background.js |
| diff --git a/ui/file_manager/gallery/js/background.js b/ui/file_manager/gallery/js/background.js |
| index 96a86daa4154eec4478d0acd8d6dd790136b0b82..30cd741298b46b831325997d173cccfd5d22c692 100644 |
| --- a/ui/file_manager/gallery/js/background.js |
| +++ b/ui/file_manager/gallery/js/background.js |
| @@ -4,44 +4,144 @@ |
| 'use strict'; |
| -var stringData = new Promise(function(fulfill) { |
| - chrome.fileBrowserPrivate.getStrings(function(stringData) { |
| - loadTimeData.data = stringData; |
| - fulfill(stringData); |
| +/** |
| + * @param {Object.<string, string>} stringData String data. |
| + * @param {VolumeManager} volumeManager Volume manager. |
| + */ |
| +function BackgroundComponents(stringData, volumeManager) { |
| + /** |
| + * String data. |
| + * @type {Object.<string, string>} |
| + */ |
| + this.stringData = stringData; |
| + |
| + /** |
| + * Volume manager. |
| + * @type {VolumeManager} |
| + */ |
| + this.volumeManager = volumeManager; |
| + |
| + Object.freeze(this); |
| +} |
| + |
| +/** |
| + * Loads background component. |
| + * @return {Promise} Promise fulfilled with BackgroundComponents. |
| + */ |
| +BackgroundComponents.load = function() { |
| + var stringDataPromise = new Promise(function(fulfill) { |
| + chrome.fileBrowserPrivate.getStrings(function(stringData) { |
| + loadTimeData.data = stringData; |
| + fulfill(stringData); |
| + }); |
| }); |
| -}); |
| -// VolumeManager should be obtained after stringData initialized. |
| -var volumeManager = stringData.then(function() { |
| - return new Promise(function(fulfill) { |
| - VolumeManager.getInstance(fulfill); |
| + // VolumeManager should be obtained after stringData initialized. |
| + var volumeManagerPromise = stringDataPromise.then(function() { |
| + return new Promise(function(fulfill) { |
| + VolumeManager.getInstance(fulfill); |
| + }); |
| }); |
| -}); |
| -var backgroundComponent = Promise.all([stringData, volumeManager]). |
| - then(function(args) { |
| - return { |
| - stringData: args[0], |
| - volumeManager: args[1] |
| - }; |
| + return Promise.all([stringDataPromise, volumeManagerPromise]).then( |
| + function(args) { |
| + return new BackgroundComponents(args[0], args[1]); |
| + }); |
| +}; |
| + |
| +/** |
| + * Promise to be fulfilled with singleton instance of background components. |
| + * @type {Promise} |
| + */ |
| +var backgroundComponentPromise = BackgroundComponents.load(); |
|
mtomasz
2014/05/16 03:29:21
nit: backgroundComponentPromise -> backgroundCompo
hirono
2014/05/16 04:12:48
Done.
|
| + |
| +/** |
| + * Resolves file system names and obtains entries. |
| + * @param {Array.<FileEntry>} entries Names of isolated file system. |
| + * @return {Promise} Promise to be fulfilled with an entry array. |
| + */ |
| +function resolveEntries(entries) { |
| + return new Promise(function(fulfill, reject) { |
| + chrome.fileBrowserPrivate.resolveIsolatedEntries(entries, |
| + function(externalEntries) { |
| + if (!chrome.runtime.lastError) |
| + fulfill(externalEntries); |
| + else |
| + reject(chrome.runtime.lastError); |
| + }); |
| + }); |
| +} |
| + |
| +/** |
| + * Obtains child entries. |
| + * @param {DirectoryEntry} entry Directory entry. |
| + * @return {Promise} Promise to be fulfilled with child entries. |
| + */ |
| +function getChildren(entry) { |
| + var reader = entry.createReader(); |
| + var readEntries = function() { |
| + return new Promise(reader.readEntries.bind(reader)).then(function(entries) { |
| + if (entries.length === 0) |
| + return []; |
| + return readEntries().then(function(nextEntries) { |
| + return entries.concat(nextEntries); |
| + }); |
| }); |
| + }; |
| + return readEntries(); |
| +} |
| chrome.app.runtime.onLaunched.addListener(function(launchData) { |
| // Skip if files are not selected. |
| if (!launchData || !launchData.items || launchData.items.length == 0) |
| return; |
| + // Obtains entries in non-isolated file systems. |
| + // The entries in launchData is stored in the isolated file system. |
|
mtomasz
2014/05/16 03:29:21
nit: is -> are
hirono
2014/05/16 04:12:48
Done.
|
| + // We need to map the isolated entries to the normal entries to retrieve their |
| + // parent directory. |
| + var isolatedEntries = launchData.items.map(function(item) { |
| + return item.entry; |
| + }); |
| + var selectedEntriesPromise = backgroundComponentPromise.then(function() { |
| + return resolveEntries(isolatedEntries); |
| + }); |
| + |
| + // If only 1 entry is selected, retrieve entries in the same directory. |
| + // Otherwise, just use the selectedEntries as an entry set. |
| + var allEntriesPromise = selectedEntriesPromise.then(function(entries) { |
| + if (entries.length === 1) { |
| + var parent = new Promise(entries[0].getParent.bind(entries[0])); |
| + return parent.then(getChildren).then(function(entries) { |
| + return entries.filter(FileType.isImageOrVideo); |
| + }); |
| + } else { |
| + return entries; |
| + } |
| + }); |
| + |
| + // Store the selected and all entries to the launchData. |
| + launchData.entriesPromise = Promise.all([selectedEntriesPromise, |
| + allEntriesPromise]).then( |
| + function(args) { |
| + return Object.freeze({ |
| + selectedEntries: args[0], |
| + allEntries: args[1] |
| + }); |
| + }); |
| + |
| // Open application window. |
| chrome.app.window.create( |
| 'gallery.html', |
| { |
| - id: 'video', |
| + id: 'gallery', |
| minWidth: 160, |
| minHeight: 100, |
| frame: 'none' |
| }, |
| function(appWindow) { |
| appWindow.contentWindow.launchData = launchData; |
| - appWindow.contentWindow.backgroundComponent = backgroundComponent; |
| + appWindow.contentWindow.backgroundComponentPromise = |
| + backgroundComponentPromise; |
| }); |
| }); |