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