OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @param {Object.<string, string>} stringData String data. | 8 * @param {Object.<string, string>} stringData String data. |
9 * @param {VolumeManager} volumeManager Volume manager. | 9 * @param {VolumeManager} volumeManager Volume manager. |
10 */ | 10 */ |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 if (entries.length === 0) | 84 if (entries.length === 0) |
85 return []; | 85 return []; |
86 return readEntries().then(function(nextEntries) { | 86 return readEntries().then(function(nextEntries) { |
87 return entries.concat(nextEntries); | 87 return entries.concat(nextEntries); |
88 }); | 88 }); |
89 }); | 89 }); |
90 }; | 90 }; |
91 return readEntries(); | 91 return readEntries(); |
92 } | 92 } |
93 | 93 |
| 94 /** |
| 95 * Promise to be fulfilled with single application window. |
| 96 * @param {AppWindow} |
| 97 */ |
| 98 var appWindowPromise = null; |
| 99 |
94 chrome.app.runtime.onLaunched.addListener(function(launchData) { | 100 chrome.app.runtime.onLaunched.addListener(function(launchData) { |
95 // Skip if files are not selected. | 101 // Skip if files are not selected. |
96 if (!launchData || !launchData.items || launchData.items.length == 0) | 102 if (!launchData || !launchData.items || launchData.items.length == 0) |
97 return; | 103 return; |
98 | 104 |
99 // Obtains entries in non-isolated file systems. | 105 // Obtains entries in non-isolated file systems. |
100 // The entries in launchData are stored in the isolated file system. | 106 // The entries in launchData are stored in the isolated file system. |
101 // We need to map the isolated entries to the normal entries to retrieve their | 107 // We need to map the isolated entries to the normal entries to retrieve their |
102 // parent directory. | 108 // parent directory. |
103 var isolatedEntries = launchData.items.map(function(item) { | 109 var isolatedEntries = launchData.items.map(function(item) { |
(...skipping 19 matching lines...) Expand all Loading... |
123 // Store the selected and all entries to the launchData. | 129 // Store the selected and all entries to the launchData. |
124 launchData.entriesPromise = Promise.all([selectedEntriesPromise, | 130 launchData.entriesPromise = Promise.all([selectedEntriesPromise, |
125 allEntriesPromise]).then( | 131 allEntriesPromise]).then( |
126 function(args) { | 132 function(args) { |
127 return Object.freeze({ | 133 return Object.freeze({ |
128 selectedEntries: args[0], | 134 selectedEntries: args[0], |
129 allEntries: args[1] | 135 allEntries: args[1] |
130 }); | 136 }); |
131 }); | 137 }); |
132 | 138 |
133 // Open application window. | 139 // Close previous window. |
134 chrome.app.window.create( | 140 var closePromise; |
135 'gallery.html', | 141 if (appWindowPromise) { |
136 { | 142 closePromise = appWindowPromise.then(function(appWindow) { |
137 id: 'gallery', | 143 return new Promise(function(fulfill) { |
138 minWidth: 160, | 144 appWindow.close(); |
139 minHeight: 100, | 145 appWindow.onClosed.addListener(fulfill); |
140 frame: 'none' | |
141 }, | |
142 function(appWindow) { | |
143 appWindow.contentWindow.launchData = launchData; | |
144 appWindow.contentWindow.backgroundComponentsPromise = | |
145 backgroundComponentsPromise; | |
146 }); | 146 }); |
| 147 }); |
| 148 } else { |
| 149 closePromise = Promise.resolve(); |
| 150 } |
| 151 var createdWindowPromise = closePromise.then(function() { |
| 152 return new Promise(function(fulfill) { |
| 153 chrome.app.window.create( |
| 154 'gallery.html', |
| 155 { |
| 156 id: 'gallery', |
| 157 minWidth: 160, |
| 158 minHeight: 100, |
| 159 frame: 'none' |
| 160 }, |
| 161 function(appWindow) { |
| 162 appWindow.contentWindow.addEventListener( |
| 163 'load', fulfill.bind(null, appWindow)); |
| 164 }); |
| 165 }); |
| 166 }); |
| 167 appWindowPromise = Promise.all([ |
| 168 createdWindowPromise, |
| 169 backgroundComponentsPromise, |
| 170 ]).then(function(args) { |
| 171 args[0].contentWindow.initialize(args[1]); |
| 172 return args[0]; |
| 173 }); |
| 174 |
| 175 // Open entries. |
| 176 Promise.all([ |
| 177 appWindowPromise, |
| 178 allEntriesPromise, |
| 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); |
| 184 }); |
147 }); | 185 }); |
OLD | NEW |