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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 * @type {Promise} | 96 * @type {Promise} |
97 */ | 97 */ |
98 var appWindowPromise = Promise.resolve(null); | 98 var appWindowPromise = Promise.resolve(null); |
99 | 99 |
100 /** | 100 /** |
101 * Promise to be fulfilled with the current window is closed. | 101 * Promise to be fulfilled with the current window is closed. |
102 * @type {Promise} | 102 * @type {Promise} |
103 */ | 103 */ |
104 var closingPromise = Promise.resolve(null); | 104 var closingPromise = Promise.resolve(null); |
105 | 105 |
106 chrome.app.runtime.onLaunched.addListener(function(launchData) { | 106 /** |
107 // Skip if files are not selected. | 107 * Launches the application with entries. |
108 if (!launchData || !launchData.items || launchData.items.length == 0) | 108 * |
109 return; | 109 * @param {Promise} selectedEntriesPromise Promise to be fulfilled with the |
110 | 110 * entries that are stored in the exteranl file system (not in the isolated |
111 // Obtains entries in non-isolated file systems. | 111 * file system). |
112 // The entries in launchData are stored in the isolated file system. | 112 */ |
113 // We need to map the isolated entries to the normal entries to retrieve their | 113 function launch(selectedEntriesPromise) { |
114 // parent directory. | |
115 var isolatedEntries = launchData.items.map(function(item) { | |
116 return item.entry; | |
117 }); | |
118 var selectedEntriesPromise = backgroundComponentsPromise.then(function() { | |
119 return resolveEntries(isolatedEntries); | |
120 }); | |
121 | |
122 // If only 1 entry is selected, retrieve entries in the same directory. | |
123 // Otherwise, just use the selectedEntries as an entry set. | |
124 var allEntriesPromise = selectedEntriesPromise.then(function(entries) { | |
125 if (entries.length === 1) { | |
126 var parent = new Promise(entries[0].getParent.bind(entries[0])); | |
127 return parent.then(getChildren).then(function(entries) { | |
128 return entries.filter(FileType.isImageOrVideo); | |
129 }); | |
130 } else { | |
131 return entries; | |
132 } | |
133 }); | |
134 | |
135 // Store the selected and all entries to the launchData. | |
136 launchData.entriesPromise = Promise.all([selectedEntriesPromise, | |
137 allEntriesPromise]).then( | |
138 function(args) { | |
139 return Object.freeze({ | |
140 selectedEntries: args[0], | |
141 allEntries: args[1] | |
142 }); | |
143 }); | |
144 | |
145 // If there is the previous window, close the window. | 114 // If there is the previous window, close the window. |
146 appWindowPromise = appWindowPromise.then(function(appWindow) { | 115 appWindowPromise = appWindowPromise.then(function(appWindow) { |
147 if (appWindow) { | 116 if (appWindow) { |
148 appWindow.close(); | 117 appWindow.close(); |
149 return closingPromise; | 118 return closingPromise; |
150 } | 119 } |
151 }); | 120 }); |
152 | 121 |
153 // Create a new window. | 122 // Create a new window. |
154 appWindowPromise = appWindowPromise.then(function() { | 123 appWindowPromise = appWindowPromise.then(function() { |
(...skipping 18 matching lines...) Expand all Loading... | |
173 | 142 |
174 // Initialize the window document. | 143 // Initialize the window document. |
175 appWindowPromise = Promise.all([ | 144 appWindowPromise = Promise.all([ |
176 appWindowPromise, | 145 appWindowPromise, |
177 backgroundComponentsPromise, | 146 backgroundComponentsPromise, |
178 ]).then(function(args) { | 147 ]).then(function(args) { |
179 args[0].contentWindow.initialize(args[1]); | 148 args[0].contentWindow.initialize(args[1]); |
180 return args[0]; | 149 return args[0]; |
181 }); | 150 }); |
182 | 151 |
152 | |
153 // If only 1 entry is selected, retrieve entries in the same directory. | |
154 // Otherwise, just use the selectedEntries as an entry set. | |
155 var allEntriesPromise = selectedEntriesPromise.then(function(entries) { | |
156 if (entries.length === 1) { | |
157 var parentPromise = new Promise(entries[0].getParent.bind(entries[0])); | |
158 return parentPromise.then(getChildren).then(function(entries) { | |
159 return entries.filter(FileType.isImageOrVideo); | |
160 }); | |
161 } else { | |
162 return entries; | |
163 } | |
164 }); | |
165 | |
183 // Open entries. | 166 // Open entries. |
184 Promise.all([ | 167 return Promise.all([ |
185 appWindowPromise, | 168 appWindowPromise, |
186 allEntriesPromise, | 169 allEntriesPromise, |
187 selectedEntriesPromise | 170 selectedEntriesPromise |
188 ]).then(function(args) { | 171 ]).then(function(args) { |
189 args[0].contentWindow.loadEntries(args[1], args[2]); | 172 args[0].contentWindow.loadEntries(args[1], args[2]); |
190 }).catch(function(error) { | 173 }); |
174 } | |
175 | |
176 chrome.app.runtime.onLaunched.addListener(function(launchData) { | |
177 // Skip if files are not selected. | |
178 if (!launchData || !launchData.items || launchData.items.length == 0) | |
yoshiki
2014/06/02 21:30:16
nit: ===
hirono
2014/06/03 01:48:41
Done.
| |
179 return; | |
180 | |
181 // Obtains entries in non-isolated file systems. | |
182 // The entries in launchData are stored in the isolated file system. | |
183 // We need to map the isolated entries to the normal entries to retrieve their | |
184 // parent directory. | |
185 var isolatedEntries = launchData.items.map(function(item) { | |
186 return item.entry; | |
187 }); | |
188 var selectedEntriesPromise = backgroundComponentsPromise.then(function() { | |
189 return resolveEntries(isolatedEntries); | |
190 }); | |
191 | |
192 launch(selectedEntriesPromise).catch(function(error) { | |
191 console.error(error.stack || error); | 193 console.error(error.stack || error); |
192 }); | 194 }); |
193 }); | 195 }); |
196 | |
197 // If is is run in the browser test, wait for the test resources are installed | |
198 // as a component extension, and then load the test resources. | |
199 if (chrome.test) { | |
200 chrome.runtime.onMessageExternal.addListener(function(message) { | |
201 if (message.name !== 'testResourceLoaded') | |
202 return; | |
203 var script = document.createElement('script'); | |
204 script.src = | |
205 'chrome-extension://ejhcmmdhhpdhhgmifplfmjobgegbibkn' + | |
206 '/gallery/test_loader.js'; | |
207 document.documentElement.appendChild(script); | |
208 }); | |
209 } | |
OLD | NEW |