Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / ui / file_manager / audio_player / js / background.js
blob600c15b9f113e09329c57a177e0c994cfbe9754b
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.
5 /**
6 * Icon of the audio player.
7 * TODO(yoshiki): Consider providing an exact size icon, instead of relying
8 * on downsampling by ash.
10 * @type {string}
11 * @const
13 var AUDIO_PLAYER_ICON = 'icons/audio-player-64.png';
15 var AUDIO_PLAYER_APP_URL = 'audio_player.html';
17 /**
18 * Configuration of the audio player panel.
19 * @type {Object}
21 var audioPlayerCreateOptions = {
22 id: 'audio-player',
23 type: 'panel',
24 minHeight: 44 + 73, // 44px: track, 73px: controller
25 minWidth: 292,
26 height: 44 + 73, // collapsed
27 width: 292
30 /**
31 * @constructor
32 * @extends {BackgroundBase}
34 function AudioPlayerBackground() {
35 BackgroundBase.call(this);
38 AudioPlayerBackground.prototype.__proto__ = BackgroundBase.prototype;
40 /**
41 * Called when an app is restarted.
43 AudioPlayerBackground.prototype.onRestarted_ = function() {
44 audioPlayer.reopen(function() {
45 // If the audioPlayer is reopened, change its window's icon. Otherwise
46 // there is no reopened window so just skip the call of setIcon.
47 if (audioPlayer.rawAppWindow)
48 audioPlayer.setIcon(AUDIO_PLAYER_ICON);
49 });
53 /**
54 * Backgound object. This is necessary for AppWindowWrapper.
55 * @type {BackgroundBase}
57 var background = new AudioPlayerBackground();
59 /**
60 * Wrapper of audio player window.
61 * @type {SingletonAppWindowWrapper}
63 var audioPlayer = new SingletonAppWindowWrapper(AUDIO_PLAYER_APP_URL,
64 audioPlayerCreateOptions);
66 /**
67 * Opens player window.
68 * @param {!Array<string>} urls List of audios to play and index to start
69 * playing.
70 * @return {!Promise} Promise to be fulfilled on success, or rejected on error.
72 function open(urls) {
73 var position = 0;
74 var startUrl = (position < urls.length) ? urls[position] : '';
76 return new Promise(function(fulfill, reject) {
77 if (urls.length === 0) {
78 reject('No file to open.');
79 return;
82 // Gets the current list of the children of the parent.
83 window.webkitResolveLocalFileSystemURL(urls[0], function(fileEntry) {
84 fileEntry.getParent(function(parentEntry) {
85 var dirReader = parentEntry.createReader();
86 var entries = [];
88 // Call the reader.readEntries() until no more results are returned.
89 var readEntries = function() {
90 dirReader.readEntries(function(results) {
91 if (!results.length) {
92 fulfill(entries.sort(util.compareName));
93 } else {
94 entries = entries.concat(Array.prototype.slice.call(results, 0));
95 readEntries();
97 }, reject);
100 // Start reading.
101 readEntries();
102 }, reject);
103 }, reject);
104 }).then(function(entries) {
105 // Omits non-audio files.
106 var audioEntries = entries.filter(FileType.isAudio);
108 // Adjusts the position to start playing.
109 var maybePosition = util.entriesToURLs(audioEntries).indexOf(startUrl);
110 if (maybePosition !== -1)
111 position = maybePosition;
113 // Opens the audio player panel.
114 return new Promise(function(fulfill, reject) {
115 var urls = util.entriesToURLs(audioEntries);
116 audioPlayer.launch({items: urls, position: position},
117 false,
118 fulfill.bind(null, null));
120 }).then(function() {
121 audioPlayer.setIcon(AUDIO_PLAYER_ICON);
122 audioPlayer.rawAppWindow.focus();
123 return AUDIO_PLAYER_APP_URL;
124 }).catch(function(error) {
125 console.error('Launch failed' + error.stack || error);
126 return Promise.reject(error);
130 background.setLaunchHandler(open);