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 // Stores the app windows OLNY for test purpose. | 7 // Stores the app windows OLNY for test purpose. |
8 // We SHOULD NOT use it as it is except for test, since the files which have | 8 // We SHOULD NOT use it as it is except for test, since the files which have |
9 // the same name will be overridden each other. | 9 // the same name will be overridden each other. |
10 var appWindowsForTest = {}; | 10 var appWindowsForTest = {}; |
11 | 11 |
12 chrome.app.runtime.onLaunched.addListener(function(launchData) { | 12 var initializeQueue = new AsyncUtil.Queue(); |
13 | |
14 // Initializes the strings. This needs for the volume manager. | |
15 initializeQueue.run(function(fulfill) { | |
16 chrome.fileBrowserPrivate.getStrings(function(stringData) { | |
17 loadTimeData.data = stringData; | |
18 fulfill(); | |
19 }.wrap()); | |
20 }.wrap()); | |
21 | |
22 // Initializes the volume manager. This needs for isolated entries. | |
23 initializeQueue.run(function(fulfill) { | |
24 VolumeManager.getInstance(fulfill); | |
25 }.wrap()); | |
26 | |
27 chrome.app.runtime.onLaunched.addListener(onLaunched); | |
28 | |
29 /** | |
30 * Called when an app is launched. | |
31 * @param {Object} launchData Launch data. | |
32 */ | |
33 function onLaunched(launchData) { | |
13 if (!launchData || !launchData.items || launchData.items.length == 0) | 34 if (!launchData || !launchData.items || launchData.items.length == 0) |
14 return; | 35 return; |
15 | 36 |
16 var getFilePromises = launchData.items.map(function(item) { | 37 var videos = []; |
17 var entry = item.entry; | 38 |
18 return new Promise(function(fullfill, reject) { | 39 initializeQueue.run(function(fulfill) { |
19 entry.file( | 40 var isolatedEntries = launchData.items.map(function(item) { |
20 function(file) { | 41 return item.entry; |
21 fullfill({ | 42 }); |
43 | |
44 chrome.fileBrowserPrivate.resolveIsolatedEntries(isolatedEntries, | |
45 function(externalEntries) { | |
46 videos = externalEntries.map(function(entry) { | |
47 return { | |
hirono
2014/07/23 04:19:13
nit: Can we do object.freeze?
yoshiki
2014/07/23 05:01:54
Done.
| |
22 entry: entry, | 48 entry: entry, |
23 file: file, | 49 title: entry.name, |
24 fileUrl: window.URL.createObjectURL(file) | 50 url: entry.toURL(), |
25 }); | 51 }; |
26 }, | |
27 function() { | |
28 fullfill({entry: entry, file: null, fileUrl: null}); | |
29 }); | 52 }); |
30 }); | 53 fulfill(); |
31 }); | 54 }.wrap()); |
55 }.wrap()); | |
32 | 56 |
33 Promise.all(getFilePromises).then(function(results) { | 57 initializeQueue.run(function(fulfill) { |
34 results = results.filter(function(result) { return result.file !== null; }); | 58 if (videos.length > 0) { |
35 if (results.length > 0) | 59 open(videos); |
36 open(results); | 60 } else { |
37 }.wrap(), | 61 // TODO(yoshiki): handle error in a smarter way. |
38 function() { | 62 open('', 'error'); // Empty URL shows the error message. |
39 // TODO(yoshiki): handle error in a smarter way. | 63 } |
40 open('', 'error'); // Empty URL shows the error message. | 64 fulfill(); |
41 }.wrap()); | 65 }.wrap()); |
42 }.wrap()); | 66 } |
43 | 67 |
44 /** | 68 /** |
45 * Opens player window. | 69 * Opens player window. |
46 * @param {Array.<Object>} videos List of videos to play. | 70 * @param {Array.<Object>} videos List of videos to play. |
47 **/ | 71 */ |
48 function open(videos) { | 72 function open(videos) { |
49 chrome.app.window.create('video_player.html', { | 73 chrome.app.window.create('video_player.html', { |
50 id: 'video', | 74 id: 'video', |
51 frame: 'none', | 75 frame: 'none', |
52 singleton: false, | 76 singleton: false, |
53 minWidth: 480, | 77 minWidth: 480, |
54 minHeight: 270 | 78 minHeight: 270 |
55 }, | 79 }, |
56 function(createdWindow) { | 80 function(createdWindow) { |
57 // Stores the window for test purpose. | 81 // Stores the window for test purpose. |
58 appWindowsForTest[videos[0].entry.name] = createdWindow; | 82 appWindowsForTest[videos[0].entry.name] = createdWindow; |
59 | 83 |
60 createdWindow.setIcon('images/200/icon.png'); | 84 createdWindow.setIcon('images/200/icon.png'); |
61 createdWindow.contentWindow.videos = videos; | 85 createdWindow.contentWindow.videos = videos; |
62 chrome.runtime.sendMessage({ready: true}, function() {}); | 86 chrome.runtime.sendMessage({ready: true}, function() {}); |
63 }.wrap()); | 87 }.wrap()); |
64 } | 88 } |
OLD | NEW |