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);
91 return readEntries().then(function(entries) {
92 return entries.sort(function(a, b) {
93 return a.name.localeCompare(b.name);
99 * Promise to be fulfilled with single application window.
100 * This can be null when the window is not opened.
103 var appWindowPromise = null;
106 * Promise to be fulfilled with entries that are used for reopening the
107 * application window.
110 var reopenEntriesPromsie = null;
113 * Launches the application with entries.
115 * @param {Promise} selectedEntriesPromise Promise to be fulfilled with the
116 * entries that are stored in the external file system (not in the isolated
118 * @return {Promise} Promise to be fulfilled after the application is launched.
120 function launch(selectedEntriesPromise) {
121 // If there is the previous window, close the window.
122 if (appWindowPromise) {
123 reopenEntriesPromsie = selectedEntriesPromise;
124 appWindowPromise.then(function(appWindow) {
127 return Promise.reject('The window has already opened.');
129 reopenEntriesPromsie = null;
131 // Create a new window.
132 appWindowPromise = new Promise(function(fulfill) {
133 chrome.app.window.create(
143 function(appWindow) {
144 appWindow.contentWindow.addEventListener(
145 'load', fulfill.bind(null, appWindow));
146 appWindow.onClosed.addListener(function() {
147 appWindowPromise = null;
148 if (reopenEntriesPromsie)
149 launch(reopenEntriesPromsie);
154 // If only 1 entry is selected, retrieve entries in the same directory.
155 // Otherwise, just use the selectedEntries as an entry set.
156 var allEntriesPromise = selectedEntriesPromise.then(function(entries) {
157 if (entries.length === 1) {
158 var parentPromise = new Promise(entries[0].getParent.bind(entries[0]));
159 return parentPromise.then(getChildren).then(function(entries) {
160 return entries.filter(FileType.isImage);
167 // Initialize the window document.
170 backgroundComponentsPromise,
171 ]).then(function(args) {
172 args[0].contentWindow.initialize(args[1]);
175 selectedEntriesPromise
176 ]).then(function(entries) {
177 args[0].contentWindow.loadEntries(entries[0], entries[1]);
182 chrome.app.runtime.onLaunched.addListener(function(launchData) {
183 // Skip if files are not selected.
184 if (!launchData || !launchData.items || launchData.items.length === 0)
187 // Obtains entries in non-isolated file systems.
188 // The entries in launchData are stored in the isolated file system.
189 // We need to map the isolated entries to the normal entries to retrieve their
191 var isolatedEntries = launchData.items.map(function(item) {
194 var selectedEntriesPromise = backgroundComponentsPromise.then(function() {
195 return resolveEntries(isolatedEntries);
198 launch(selectedEntriesPromise).catch(function(error) {
199 console.error(error.stack || error);
203 // If is is run in the browser test, wait for the test resources are installed
204 // as a component extension, and then load the test resources.
206 chrome.runtime.onMessageExternal.addListener(function(message) {
207 if (message.name !== 'testResourceLoaded')
209 var script = document.createElement('script');
211 'chrome-extension://ejhcmmdhhpdhhgmifplfmjobgegbibkn' +
212 '/gallery/test_loader.js';
213 document.documentElement.appendChild(script);