background.js revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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// Stores the app windows OLNY for test purpose. 8// We SHOULD NOT use it as it is except for test, since the files which have 9// the same name will be overridden each other. 10var appWindowsForTest = {}; 11 12var initializeQueue = new AsyncUtil.Queue(); 13 14// Initializes the strings. This needs for the volume manager. 15initializeQueue.run(function(fulfill) { 16 chrome.fileManagerPrivate.getStrings(function(stringData) { 17 loadTimeData.data = stringData; 18 fulfill(); 19 }.wrap()); 20}.wrap()); 21 22// Initializes the volume manager. This needs for isolated entries. 23initializeQueue.run(function(fulfill) { 24 VolumeManager.getInstance(fulfill); 25}.wrap()); 26 27chrome.app.runtime.onLaunched.addListener(onLaunched); 28 29/** 30 * Called when an app is launched. 31 * @param {Object} launchData Launch data. 32 */ 33function onLaunched(launchData) { 34 if (!launchData || !launchData.items || launchData.items.length == 0) 35 return; 36 37 var videos = []; 38 39 initializeQueue.run(function(fulfill) { 40 var isolatedEntries = launchData.items.map(function(item) { 41 return item.entry; 42 }); 43 44 chrome.fileManagerPrivate.resolveIsolatedEntries(isolatedEntries, 45 function(externalEntries) { 46 videos = externalEntries.map(function(entry) { 47 return Object.freeze({ 48 entry: entry, 49 title: entry.name, 50 url: entry.toURL(), 51 }); 52 }); 53 fulfill(); 54 }.wrap()); 55 }.wrap()); 56 57 initializeQueue.run(function(fulfill) { 58 if (videos.length > 0) { 59 open(videos); 60 } else { 61 // TODO(yoshiki): handle error in a smarter way. 62 open('', 'error'); // Empty URL shows the error message. 63 } 64 fulfill(); 65 }.wrap()); 66} 67 68/** 69 * Opens player window. 70 * @param {Array.<Object>} videos List of videos to play. 71 * @return {Promise} Promise to be fulfilled on success, or rejected on error. 72 */ 73function open(videos) { 74 return new Promise(function(fulfill, reject) { 75 chrome.app.window.create('video_player.html', { 76 id: 'video', 77 frame: 'none', 78 singleton: false, 79 minWidth: 480, 80 minHeight: 270 81 }, 82 fulfill); 83 }).then(function(createdWindow) { 84 // Stores the window for test purpose. 85 appWindowsForTest[videos[0].entry.name] = createdWindow; 86 87 createdWindow.contentWindow.videos = videos; 88 createdWindow.setIcon('images/icon/video-player-64.png'); 89 90 if (chrome.test) 91 createdWindow.contentWindow.loadMockCastExtensionForTest = true; 92 93 chrome.runtime.sendMessage({ready: true}); 94 }).catch(function(error) { 95 console.error('Launch failed', error.stack || error); 96 return Promise.reject(error); 97 }); 98} 99 100// If is is run in the browser test, wait for the test resources are installed 101// as a component extension, and then load the test resources. 102if (chrome.test) { 103 /** @type {string} */ 104 window.testExtensionId = 'ljoplibgfehghmibaoaepfagnmbbfiga'; 105 chrome.runtime.onMessageExternal.addListener(function(message) { 106 if (message.name !== 'testResourceLoaded') 107 return; 108 var script = document.createElement('script'); 109 script.src = 110 'chrome-extension://' + window.testExtensionId + 111 '/common/test_loader.js'; 112 document.documentElement.appendChild(script); 113 }); 114} 115