Add ability for NetLogLogger to gather data from more than just NetLog
[chromium-blink-merge.git] / ui / file_manager / gallery / js / background.js
blob4d631471479c32f2ed4f1819bae8b4b09d22aaea
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.
5 /**
6 * Configuration of the Gallery window.
7 * @const
8 * @type {Object}
9 */
10 var windowCreateOptions = {
11 id: 'gallery',
12 innerBounds: {
13 minWidth: 820,
14 minHeight: 554
16 frame: 'none'
19 /**
20 * Backgound object. This is necessary for AppWindowWrapper.
21 * @type {!BackgroundBase}
23 var background = new BackgroundBase();
25 /**
26 * Wrapper of gallery window.
27 * @type {SingletonAppWindowWrapper}
29 var gallery = new SingletonAppWindowWrapper('gallery.html',
30 windowCreateOptions);
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;
36 fulfill(true);
37 });
38 });
40 // Initializes the volume manager. This needs for isolated entries.
41 var volumeManagerPromise = new Promise(function(fulfill, reject) {
42 VolumeManager.getInstance(fulfill);
43 });
45 /**
46 * Queue to serialize initialization.
47 * @type {!Promise}
49 window.initializePromise = Promise.all([loadTimeDataPromise,
50 volumeManagerPromise]);
52 // Registers the handlers.
53 chrome.app.runtime.onLaunched.addListener(onLaunched);
55 /**
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)
64 return;
66 window.initializePromise.then(function() {
67 var isolatedEntries = launchData.items.map(function(item) {
68 return item.entry;
69 });
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(
76 isolatedEntries,
77 function(externalEntries) {
78 var urls = util.entriesToURLs(externalEntries);
79 openGalleryWindow(urls, false);
80 });
81 });
84 /**
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));
94 }).catch(reject);
95 }).then(function(urls) {
96 if (urls.length === 0)
97 return Promise.reject('No file to open.');
99 // Opens a window.
100 return new Promise(function(fulfill, reject) {
101 gallery.launch(
102 {urls: urls},
103 reopen,
104 fulfill.bind(null, gallery));
105 }).then(function(gallery) {
106 var galleryDocument = gallery.rawAppWindow.contentWindow.document;
107 if (galleryDocument.readyState == 'complete')
108 return gallery;
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);