Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / file_manager / video_player / js / background.js
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.
10 var appWindowsForTest = {};
11
12 var initializeQueue = new AsyncUtil.Queue();
13
14 // Initializes the strings. This needs for the volume manager.
15 initializeQueue.run(function(fulfill) {
16   chrome.fileBrowserPrivate.getStrings(function(stringData) {
17     loadTimeData.data = stringData;
18     fulfill();
19   }.wrap());
20 }.wrap());
21
22 // Initializes the volume manager. This needs for isolated entries.
23 initializeQueue.run(function(fulfill) {
24   VolumeManager.getInstance(fulfill);
25 }.wrap());
26
27 chrome.app.runtime.onLaunched.addListener(onLaunched);
28
29 /**
30  * Called when an app is launched.
31  * @param {Object} launchData Launch data.
32  */
33 function 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.fileBrowserPrivate.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  */
72 function open(videos) {
73   chrome.app.window.create('video_player.html', {
74     id: 'video',
75     frame: 'none',
76     singleton: false,
77     minWidth: 480,
78     minHeight: 270
79   },
80   function(createdWindow) {
81     // Stores the window for test purpose.
82     appWindowsForTest[videos[0].entry.name] = createdWindow;
83
84     createdWindow.setIcon('images/icon/video-player-64.png');
85     createdWindow.contentWindow.videos = videos;
86     chrome.runtime.sendMessage({ready: true}, function() {});
87   }.wrap());
88 }