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.
6 * Icon of the video player.
7 * TODO(yoshiki): Consider providing an exact size icon, instead of relying
8 * on downsampling by ash.
13 var ICON_IMAGE = 'images/icon/video-player-64.png';
16 * Configuration of the video player panel.
19 var windowCreateOptions = {
26 * Backgound object. This is necessary for AppWindowWrapper.
27 * @type {BackgroundBase}
29 var background = new BackgroundBase();
32 * Queue to serialize initialization.
33 * @type {AsyncUtil.Queue}
35 var initializeQueue = new AsyncUtil.Queue();
37 // Initializes the strings. This needs for the volume manager.
38 initializeQueue.run(function(fulfill) {
39 chrome.fileManagerPrivate.getStrings(function(stringData) {
40 loadTimeData.data = stringData;
45 // Initializes the volume manager. This needs for isolated entries.
46 initializeQueue.run(function(fulfill) {
47 VolumeManager.getInstance(fulfill);
50 // Registers the handlers.
51 chrome.app.runtime.onLaunched.addListener(onLaunched);
54 * Called when an app is launched.
55 * @param {Object} launchData Launch data.
57 function onLaunched(launchData) {
58 if (!launchData || !launchData.items || launchData.items.length == 0)
63 initializeQueue.run(function(fulfill) {
64 var isolatedEntries = launchData.items.map(function(item) {
68 chrome.fileManagerPrivate.resolveIsolatedEntries(isolatedEntries,
69 function(externalEntries) {
70 var urls = util.entriesToURLs(externalEntries);
71 playlist = {items: urls, position: 0};
76 initializeQueue.run(function(fulfill) {
77 openVideoPlayerWindow(playlist, false);
82 var generateWindowId = (function() {
85 return 'VIDEO_PLAYER_APP_' + seq++;
90 * Opens player window.
91 * @param {Object} playlist List of videos to play and index to start playing.
92 * @param {boolean} reopen True if reopen, false otherwise.
93 * @return {Promise} Promise to be fulfilled on success, or rejected on error.
95 function openVideoPlayerWindow(playlist, reopen) {
96 var items = playlist.items;
97 var position = playlist.position;
98 var startUrl = (position < items.length) ? items[position] : '';
101 return new Promise(function(fulfill, reject) {
102 util.URLsToEntries(items).then(function(result) {
103 fulfill(result.entries);
104 }.wrap()).catch(reject);
105 }.wrap()).then(function(entries) {
106 if (entries.length === 0)
107 return Promise.reject('No file to open.');
109 // Adjusts the position to start playing.
110 var maybePosition = util.entriesToURLs(entries).indexOf(startUrl);
111 if (maybePosition !== -1)
112 position = maybePosition;
114 windowId = generateWindowId();
116 // Opens the video player window.
117 return new Promise(function(fulfill, reject) {
118 var urls = util.entriesToURLs(entries);
119 var videoPlayer = new AppWindowWrapper('video_player.html',
121 windowCreateOptions);
124 {items: urls, position: position},
126 fulfill.bind(null, videoPlayer));
128 }.wrap()).then(function(videoPlayer) {
129 var appWindow = videoPlayer.rawAppWindow;
132 appWindow.contentWindow.loadMockCastExtensionForTest = true;
134 videoPlayer.setIcon(ICON_IMAGE);
138 }.wrap()).catch(function(error) {
139 console.error('Launch failed' + error.stack || error);
140 return Promise.reject(error);