1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * @param {Object.<string, string>} stringData String data.
9 * @param {VolumeManager} volumeManager Volume manager.
11 function BackgroundComponents(stringData, volumeManager) {
14 * @type {Object.<string, string>}
16 this.stringData = stringData;
20 * @type {VolumeManager}
22 this.volumeManager = volumeManager;
28 * Loads background component.
29 * @return {Promise} Promise fulfilled with BackgroundComponents.
31 BackgroundComponents.load = function() {
32 var stringDataPromise = new Promise(function(fulfill) {
33 chrome.fileBrowserPrivate.getStrings(function(stringData) {
34 loadTimeData.data = stringData;
39 // VolumeManager should be obtained after stringData initialized.
40 var volumeManagerPromise = stringDataPromise.then(function() {
41 return new Promise(function(fulfill) {
42 VolumeManager.getInstance(fulfill);
46 return Promise.all([stringDataPromise, volumeManagerPromise]).then(
48 return new BackgroundComponents(args[0], args[1]);
53 * Promise to be fulfilled with singleton instance of background components.
56 var backgroundComponentsPromise = BackgroundComponents.load();
59 * Resolves file system names and obtains entries.
60 * @param {Array.<FileEntry>} entries Names of isolated file system.
61 * @return {Promise} Promise to be fulfilled with an entry array.
63 function resolveEntries(entries) {
64 return new Promise(function(fulfill, reject) {
65 chrome.fileBrowserPrivate.resolveIsolatedEntries(entries,
66 function(externalEntries) {
67 if (!chrome.runtime.lastError)
68 fulfill(externalEntries);
70 reject(chrome.runtime.lastError);
76 * Obtains child entries.
77 * @param {DirectoryEntry} entry Directory entry.
78 * @return {Promise} Promise to be fulfilled with child entries.
80 function getChildren(entry) {
81 var reader = entry.createReader();
82 var readEntries = function() {
83 return new Promise(reader.readEntries.bind(reader)).then(function(entries) {
84 if (entries.length === 0)
86 return readEntries().then(function(nextEntries) {
87 return entries.concat(nextEntries);
95 * Promise to be fulfilled with single application window.
98 var appWindowPromise = null;
100 chrome.app.runtime.onLaunched.addListener(function(launchData) {
101 // Skip if files are not selected.
102 if (!launchData || !launchData.items || launchData.items.length == 0)
105 // Obtains entries in non-isolated file systems.
106 // The entries in launchData are stored in the isolated file system.
107 // We need to map the isolated entries to the normal entries to retrieve their
109 var isolatedEntries = launchData.items.map(function(item) {
112 var selectedEntriesPromise = backgroundComponentsPromise.then(function() {
113 return resolveEntries(isolatedEntries);
116 // If only 1 entry is selected, retrieve entries in the same directory.
117 // Otherwise, just use the selectedEntries as an entry set.
118 var allEntriesPromise = selectedEntriesPromise.then(function(entries) {
119 if (entries.length === 1) {
120 var parent = new Promise(entries[0].getParent.bind(entries[0]));
121 return parent.then(getChildren).then(function(entries) {
122 return entries.filter(FileType.isImageOrVideo);
129 // Store the selected and all entries to the launchData.
130 launchData.entriesPromise = Promise.all([selectedEntriesPromise,
131 allEntriesPromise]).then(
133 return Object.freeze({
134 selectedEntries: args[0],
139 // Close previous window.
141 if (appWindowPromise) {
142 closePromise = appWindowPromise.then(function(appWindow) {
143 return new Promise(function(fulfill) {
145 appWindow.onClosed.addListener(fulfill);
149 closePromise = Promise.resolve();
151 var createdWindowPromise = closePromise.then(function() {
152 return new Promise(function(fulfill) {
153 chrome.app.window.create(
161 function(appWindow) {
162 appWindow.contentWindow.addEventListener(
163 'load', fulfill.bind(null, appWindow));
167 appWindowPromise = Promise.all([
168 createdWindowPromise,
169 backgroundComponentsPromise,
170 ]).then(function(args) {
171 args[0].contentWindow.initialize(args[1]);
179 selectedEntriesPromise
180 ]).then(function(args) {
181 args[0].contentWindow.loadEntries(args[1], args[2]);
182 }).catch(function(error) {
183 console.error(error.stack || error);