Index: ui/file_manager/audio_player/js/background.js |
diff --git a/ui/file_manager/audio_player/js/background.js b/ui/file_manager/audio_player/js/background.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ba25239063145fe25d8563675a7ebbc446a82b09 |
--- /dev/null |
+++ b/ui/file_manager/audio_player/js/background.js |
@@ -0,0 +1,153 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+'use strict'; |
+ |
+/** |
+ * Icon of the audio player. |
+ * TODO(yoshiki): Consider providing an exact size icon, instead of relying |
+ * on downsampling by ash. |
+ * |
+ * @type {string} |
+ * @const |
+ */ |
+var AUDIO_PLAYER_ICON = 'audio_player/icons/audio-player-64.png'; |
+ |
+// Stores the app windows OLNY for test purpose. |
+// We SHOULD NOT use it as it is except for test, since the files which have |
+// the same name will be overridden each other. |
+var appWindowsForTest = {}; |
+ |
+var audioPlayerCreateOptions = { |
+ id: 'audio-player', |
+ type: 'panel', |
+ minHeight: 44 + 73, // 44px: track, 73px: controller |
+ minWidth: 292, |
+ height: 44 + 73, // collapsed |
+ width: 292 |
+}; |
+ |
+var audioPlayer = new SingletonAppWindowWrapper('audio_player.html', |
+ audioPlayerCreateOptions); |
+ |
+var background = new BackgroundBase(); |
+ |
+var initializeQueue = new AsyncUtil.Queue(); |
+ |
+// Initializes the strings. This needs for the volume manager. |
+initializeQueue.run(function(fulfill) { |
+ chrome.fileManagerPrivate.getStrings(function(stringData) { |
+ loadTimeData.data = stringData; |
+ fulfill(); |
+ }); |
+}); |
+ |
+// Initializes the volume manager. This needs for isolated entries. |
+initializeQueue.run(function(fulfill) { |
+ VolumeManager.getInstance(fulfill); |
+}); |
+ |
+// Registers the handlers. |
+chrome.app.runtime.onLaunched.addListener(onLaunched); |
+chrome.app.runtime.onRestarted.addListener(onRestarted); |
+ |
+/** |
+ * Called when an app is launched. |
+ * @param {Object} launchData Launch data. |
+ */ |
+function onLaunched(launchData) { |
+ if (!launchData || !launchData.items || launchData.items.length == 0) |
+ return; |
+ |
+ var playlist = {}; |
+ |
+ initializeQueue.run(function(fulfill) { |
+ var isolatedEntries = launchData.items.map(function(item) { |
+ return item.entry; |
+ }); |
+ |
+ chrome.fileManagerPrivate.resolveIsolatedEntries(isolatedEntries, |
+ function(externalEntries) { |
+ var urls = util.entriesToURLs(externalEntries); |
+ playlist = {items: urls, position: 0}; |
+ fulfill(); |
+ }); |
+ }); |
+ |
+ initializeQueue.run(function(fulfill) { |
+ open(playlist, false); |
+ fulfill(); |
+ }); |
+} |
+ |
+/** |
+ * Called when an app is restarted. |
+ */ |
+function onRestarted() { |
+ audioPlayer.reopen(function() { |
+ // If the audioPlayer is reopened, change its window's icon. Otherwise |
+ // there is no reopened window so just skip the call of setIcon. |
+ if (audioPlayer.rawAppWindow) |
+ audioPlayer.setIcon(AUDIO_PLAYER_ICON); |
+ }); |
+} |
+ |
+/** |
+ * Opens player window. |
+ * @param {Object} playlist List of audios to play and index to start playing. |
+ * @param {Promise} Promise to be fulfilled on success, or rejected on error. |
+ */ |
+function open(playlist, reopen) { |
+ var items = playlist.items; |
+ var position = playlist.position; |
+ var startUrl = (position < items.length) ? items[position] : ''; |
+ |
+ return new Promise(function(fulfill, reject) { |
+ if (items.length === 0) { |
+ reject('No file to open.'); |
+ return; |
+ } |
+ |
+ window.webkitResolveLocalFileSystemURL(items[0], function(fileEntry) { |
+ fileEntry.getParent(fulfill, reject); |
+ }); |
+ }).then(function(parentEntry) { |
+ return new Promise(function(fulfill, reject) { |
+ var dirReader = parentEntry.createReader(); |
+ var entries = []; |
+ |
+ // Call the reader.readEntries() until no more results are returned. |
+ var readEntries = function() { |
+ dirReader.readEntries(function(results) { |
+ if (!results.length) { |
+ fulfill(entries); |
+ } else { |
+ entries = entries.concat(Array.prototype.slice.call(results, 0)); |
+ readEntries(); |
+ } |
+ }, reject); |
+ }; |
+ |
+ // Start reading. |
+ readEntries(); |
+ }); |
+ }).then(function(entries) { |
+ var audioEntries = entries.filter(FileType.isAudio).sort(util.compareName); |
+ var maybePosition = util.entriesToURLs(audioEntries).indexOf(startUrl); |
+ if (maybePosition !== -1) |
+ position = maybePosition; |
+ |
+ return new Promise(function(fulfill, reject) { |
+ var urls = util.entriesToURLs(audioEntries); |
+ audioPlayer.launch({items: urls, position: position}, reopen, fulfill); |
+ }); |
+ }).then(function() { |
+ var appWindow = audioPlayer.rawAppWindow; |
+ appWindow.setIcon('icons/audio-player-64.png'); |
+ AppWindowWrapper.focusOnDesktop(appWindow); |
+ }).catch(function(error) { |
+ console.error('Launch failed' + error.stack || error); |
+ return Promise.reject(error); |
+ }); |
+} |