• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
12chrome.app.runtime.onLaunched.addListener(function(launchData) {
13  if (!launchData || !launchData.items || launchData.items.length == 0)
14    return;
15
16  var getFilePromises = launchData.items.map(function(item) {
17    var entry = item.entry;
18    return new Promise(function(fullfill, reject) {
19      entry.file(
20          function(file) {
21            fullfill({
22              entry: entry,
23              file: file,
24              fileUrl: window.URL.createObjectURL(file)
25            });
26          },
27          function() {
28            fullfill({entry: entry, file: null, fileUrl: null});
29          });
30    });
31  });
32
33  Promise.all(getFilePromises).then(function(results) {
34    results = results.filter(function(result) { return result.file !== null; });
35    if (results.length > 0)
36      open(results);
37  }.wrap(),
38  function() {
39    // TODO(yoshiki): handle error in a smarter way.
40    open('', 'error');  // Empty URL shows the error message.
41  }.wrap());
42}.wrap());
43
44/**
45 * Opens player window.
46 * @param {Array.<Object>} videos List of videos to play.
47 **/
48function open(videos) {
49  chrome.app.window.create('video_player.html', {
50    id: 'video',
51    frame: 'none',
52    singleton: false,
53    minWidth: 160,
54    minHeight: 100
55  },
56  function(createdWindow) {
57    // Stores the window for test purpose.
58    appWindowsForTest[videos[0].entry.name] = createdWindow;
59
60    createdWindow.setIcon('images/200/icon.png');
61    createdWindow.contentWindow.videos = videos;
62    chrome.runtime.sendMessage({ready: true}, function() {});
63  }.wrap());
64}
65