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.
6 * Configuration of the Gallery window.
10 var windowCreateOptions = {
20 * Backgound object. This is necessary for AppWindowWrapper.
21 * @type {!BackgroundBase}
23 var background = new BackgroundBase();
26 * Wrapper of gallery window.
27 * @type {SingletonAppWindowWrapper}
29 var gallery = new SingletonAppWindowWrapper('gallery.html',
32 // Initializes the strings. This needs for the volume manager.
33 var loadTimeDataPromise = new Promise(function(fulfill, reject) {
34 chrome.fileManagerPrivate.getStrings(function(stringData) {
35 loadTimeData.data = stringData;
40 // Initializes the volume manager. This needs for isolated entries.
41 var volumeManagerPromise = new Promise(function(fulfill, reject) {
42 VolumeManager.getInstance(fulfill);
46 * Queue to serialize initialization.
49 window.initializePromise = Promise.all([loadTimeDataPromise,
50 volumeManagerPromise]);
52 // Registers the handlers.
53 chrome.app.runtime.onLaunched.addListener(onLaunched);
56 * Called when an app is launched.
58 * @param {!Object} launchData Launch data. See the manual of chrome.app.runtime
59 * .onLaunched for detail.
61 function onLaunched(launchData) {
62 // Skip if files are not selected.
63 if (!launchData || !launchData.items || launchData.items.length == 0)
66 window.initializePromise.then(function() {
67 var isolatedEntries = launchData.items.map(function(item) {
71 // Obtains entries in non-isolated file systems.
72 // The entries in launchData are stored in the isolated file system.
73 // We need to map the isolated entries to the normal entries to retrieve
74 // their parent directory.
75 chrome.fileManagerPrivate.resolveIsolatedEntries(
77 function(externalEntries) {
78 var urls = util.entriesToURLs(externalEntries);
79 openGalleryWindow(urls, false);
85 * Opens gallery window.
86 * @param {!Array.<string>} urls List of URL to show.
87 * @param {boolean} reopen True if reopen, false otherwise.
88 * @return {!Promise} Promise to be fulfilled on success, or rejected on error.
90 function openGalleryWindow(urls, reopen) {
91 return new Promise(function(fulfill, reject) {
92 util.URLsToEntries(urls).then(function(result) {
93 fulfill(util.entriesToURLs(result.entries));
95 }).then(function(urls) {
96 if (urls.length === 0)
97 return Promise.reject('No file to open.');
100 return new Promise(function(fulfill, reject) {
104 fulfill.bind(null, gallery));
105 }).then(function(gallery) {
106 var galleryDocument = gallery.rawAppWindow.contentWindow.document;
107 if (galleryDocument.readyState == 'complete')
110 return new Promise(function(fulfill, reject) {
111 galleryDocument.addEventListener(
112 'DOMContentLoaded', fulfill.bind(null, gallery));
115 }).then(function(gallery) {
116 gallery.rawAppWindow.focus();
117 return gallery.rawAppWindow.contentWindow.appID;
118 }).catch(function(error) {
119 console.error('Launch failed' + error.stack || error);
120 return Promise.reject(error);