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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 /** | 94 /** |
95 * Promise to be fulfilled with single application window. | 95 * Promise to be fulfilled with single application window. |
96 * @param {AppWindow} | 96 * @type {Promise} |
97 */ | 97 */ |
98 var appWindowPromise = null; | 98 var appWindowPromise = Promise.resolve(null); |
| 99 |
| 100 /** |
| 101 * Promise to be fulfilled with the current window is closed. |
| 102 * @type {Promise} |
| 103 */ |
| 104 var closingPromise = Promise.resolve(null); |
99 | 105 |
100 chrome.app.runtime.onLaunched.addListener(function(launchData) { | 106 chrome.app.runtime.onLaunched.addListener(function(launchData) { |
101 // Skip if files are not selected. | 107 // Skip if files are not selected. |
102 if (!launchData || !launchData.items || launchData.items.length == 0) | 108 if (!launchData || !launchData.items || launchData.items.length == 0) |
103 return; | 109 return; |
104 | 110 |
105 // Obtains entries in non-isolated file systems. | 111 // Obtains entries in non-isolated file systems. |
106 // The entries in launchData are stored in the isolated file system. | 112 // 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 | 113 // We need to map the isolated entries to the normal entries to retrieve their |
108 // parent directory. | 114 // parent directory. |
(...skipping 20 matching lines...) Expand all Loading... |
129 // Store the selected and all entries to the launchData. | 135 // Store the selected and all entries to the launchData. |
130 launchData.entriesPromise = Promise.all([selectedEntriesPromise, | 136 launchData.entriesPromise = Promise.all([selectedEntriesPromise, |
131 allEntriesPromise]).then( | 137 allEntriesPromise]).then( |
132 function(args) { | 138 function(args) { |
133 return Object.freeze({ | 139 return Object.freeze({ |
134 selectedEntries: args[0], | 140 selectedEntries: args[0], |
135 allEntries: args[1] | 141 allEntries: args[1] |
136 }); | 142 }); |
137 }); | 143 }); |
138 | 144 |
139 // Close previous window. | 145 // If there is the previous window, close the window. |
140 var closePromise; | 146 appWindowPromise = appWindowPromise.then(function(appWindow) { |
141 if (appWindowPromise) { | 147 if (appWindow) { |
142 closePromise = appWindowPromise.then(function(appWindow) { | 148 appWindow.close(); |
143 return new Promise(function(fulfill) { | 149 return closingPromise; |
144 appWindow.close(); | 150 } |
145 appWindow.onClosed.addListener(fulfill); | 151 }); |
146 }); | 152 |
147 }); | 153 // Create a new window. |
148 } else { | 154 appWindowPromise = appWindowPromise.then(function() { |
149 closePromise = Promise.resolve(); | |
150 } | |
151 var createdWindowPromise = closePromise.then(function() { | |
152 return new Promise(function(fulfill) { | 155 return new Promise(function(fulfill) { |
153 chrome.app.window.create( | 156 chrome.app.window.create( |
154 'gallery.html', | 157 'gallery.html', |
155 { | 158 { |
156 id: 'gallery', | 159 id: 'gallery', |
157 minWidth: 160, | 160 minWidth: 160, |
158 minHeight: 100, | 161 minHeight: 100, |
159 frame: 'none' | 162 frame: 'none' |
160 }, | 163 }, |
161 function(appWindow) { | 164 function(appWindow) { |
162 appWindow.contentWindow.addEventListener( | 165 appWindow.contentWindow.addEventListener( |
163 'load', fulfill.bind(null, appWindow)); | 166 'load', fulfill.bind(null, appWindow)); |
| 167 closingPromise = new Promise(function(fulfill) { |
| 168 appWindow.onClosed.addListener(fulfill); |
| 169 }); |
164 }); | 170 }); |
165 }); | 171 }); |
166 }); | 172 }); |
| 173 |
| 174 // Initialize the window document. |
167 appWindowPromise = Promise.all([ | 175 appWindowPromise = Promise.all([ |
168 createdWindowPromise, | 176 appWindowPromise, |
169 backgroundComponentsPromise, | 177 backgroundComponentsPromise, |
170 ]).then(function(args) { | 178 ]).then(function(args) { |
171 args[0].contentWindow.initialize(args[1]); | 179 args[0].contentWindow.initialize(args[1]); |
172 return args[0]; | 180 return args[0]; |
173 }); | 181 }); |
174 | 182 |
175 // Open entries. | 183 // Open entries. |
176 Promise.all([ | 184 Promise.all([ |
177 appWindowPromise, | 185 appWindowPromise, |
178 allEntriesPromise, | 186 allEntriesPromise, |
179 selectedEntriesPromise | 187 selectedEntriesPromise |
180 ]).then(function(args) { | 188 ]).then(function(args) { |
181 args[0].contentWindow.loadEntries(args[1], args[2]); | 189 args[0].contentWindow.loadEntries(args[1], args[2]); |
182 }).catch(function(error) { | 190 }).catch(function(error) { |
183 console.error(error.stack || error); | 191 console.error(error.stack || error); |
184 }); | 192 }); |
185 }); | 193 }); |
OLD | NEW |