Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / ui / file_manager / audio_player / js / background.js
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.
4
5 /**
6  * Icon of the audio player.
7  * TODO(yoshiki): Consider providing an exact size icon, instead of relying
8  * on downsampling by ash.
9  *
10  * @type {string}
11  * @const
12  */
13 var AUDIO_PLAYER_ICON = 'audio_player/icons/audio-player-64.png';
14
15 /**
16  * Configuration of the audio player panel.
17  * @type {Object}
18  */
19 var audioPlayerCreateOptions = {
20   id: 'audio-player',
21   type: 'panel',
22   minHeight: 44 + 73,  // 44px: track, 73px: controller
23   minWidth: 292,
24   height: 44 + 73,  // collapsed
25   width: 292
26 };
27
28 /**
29  * Backgound object. This is necessary for AppWindowWrapper.
30  * @type {BackgroundBase}
31  */
32 var background = new BackgroundBase();
33
34 /**
35  * Wrapper of audio player window.
36  * @type {SingletonAppWindowWrapper}
37  */
38 var audioPlayer = new SingletonAppWindowWrapper('audio_player.html',
39                                                 audioPlayerCreateOptions);
40
41 /**
42  * Queue to serialize initialization.
43  * @type {AsyncUtil.Queue}
44  */
45 var initializeQueue = new AsyncUtil.Queue();
46
47 // Initializes the strings. This needs for the volume manager.
48 initializeQueue.run(function(fulfill) {
49   chrome.fileManagerPrivate.getStrings(function(stringData) {
50     loadTimeData.data = stringData;
51     fulfill();
52   });
53 });
54
55 // Initializes the volume manager. This needs for isolated entries.
56 initializeQueue.run(function(fulfill) {
57   VolumeManager.getInstance(fulfill);
58 });
59
60 // Registers the handlers.
61 chrome.app.runtime.onLaunched.addListener(onLaunched);
62 chrome.app.runtime.onRestarted.addListener(onRestarted);
63
64 /**
65  * Called when an app is launched.
66  * @param {Object} launchData Launch data.
67  */
68 function onLaunched(launchData) {
69   if (!launchData || !launchData.items || launchData.items.length == 0)
70     return;
71
72   var playlist = {};
73
74   initializeQueue.run(function(fulfill) {
75     var isolatedEntries = launchData.items.map(function(item) {
76       return item.entry;
77     });
78
79     chrome.fileManagerPrivate.resolveIsolatedEntries(isolatedEntries,
80         function(externalEntries) {
81           var urls = util.entriesToURLs(externalEntries);
82           playlist = {items: urls, position: 0};
83           fulfill();
84         });
85   });
86
87   initializeQueue.run(function(fulfill) {
88     open(playlist, false);
89     fulfill();
90   });
91 }
92
93 /**
94  * Called when an app is restarted.
95  */
96 function onRestarted() {
97   audioPlayer.reopen(function() {
98     // If the audioPlayer is reopened, change its window's icon. Otherwise
99     // there is no reopened window so just skip the call of setIcon.
100     if (audioPlayer.rawAppWindow)
101       audioPlayer.setIcon(AUDIO_PLAYER_ICON);
102   });
103 }
104
105 /**
106  * Opens player window.
107  * @param {Object} playlist List of audios to play and index to start playing.
108  * @param {Promise} Promise to be fulfilled on success, or rejected on error.
109  */
110 function open(playlist, reopen) {
111   var items = playlist.items;
112   var position = playlist.position;
113   var startUrl = (position < items.length) ? items[position] : '';
114
115   return new Promise(function(fulfill, reject) {
116     if (items.length === 0) {
117       reject('No file to open.');
118       return;
119     }
120
121     // Gets the current list of the children of the parent.
122     window.webkitResolveLocalFileSystemURL(items[0], function(fileEntry) {
123       fileEntry.getParent(function(parentEntry) {
124         var dirReader = parentEntry.createReader();
125         var entries = [];
126
127         // Call the reader.readEntries() until no more results are returned.
128         var readEntries = function() {
129            dirReader.readEntries(function(results) {
130             if (!results.length) {
131               fulfill(entries.sort(util.compareName));
132             } else {
133               entries = entries.concat(Array.prototype.slice.call(results, 0));
134               readEntries();
135             }
136           }, reject);
137         };
138
139         // Start reading.
140         readEntries();
141       }, reject);
142     }, reject);
143   }).then(function(entries) {
144     // Omits non-audio files.
145     var audioEntries = entries.filter(FileType.isAudio);
146
147     // Adjusts the position to start playing.
148     var maybePosition = util.entriesToURLs(audioEntries).indexOf(startUrl);
149     if (maybePosition !== -1)
150       position = maybePosition;
151
152     // Opens the audio player panel.
153     return new Promise(function(fulfill, reject) {
154       var urls = util.entriesToURLs(audioEntries);
155       audioPlayer.launch({items: urls, position: position}, reopen, fulfill);
156     });
157   }).then(function() {
158     audioPlayer.setIcon('icons/audio-player-64.png');
159     AppWindowWrapper.focusOnDesktop(audioPlayer.rawAppWindow);
160   }).catch(function(error) {
161     console.error('Launch failed' + error.stack || error);
162     return Promise.reject(error);
163   });
164 }