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.
8 * Displays error message.
9 * @param {string} message Message id.
11 function showErrorMessage(message) {
12 var errorBanner = document.querySelector('#error');
13 errorBanner.textContent =
14 loadTimeData.getString(message);
15 errorBanner.setAttribute('visible', 'true');
17 // The window is hidden if the video has not loaded yet.
18 chrome.app.window.current().show();
22 * Handles playback (decoder) errors.
24 function onPlaybackError() {
25 showErrorMessage('GALLERY_VIDEO_DECODING_ERROR');
26 decodeErrorOccured = true;
28 // Disable inactivity watcher, and disable the ui, by hiding tools manually.
29 controls.inactivityWatcher.disabled = true;
30 document.querySelector('#video-player').setAttribute('disabled', 'true');
32 // Detach the video element, since it may be unreliable and reset stored
33 // current playback time.
35 controls.clearState();
37 // Avoid reusing a video element.
38 video.parentNode.removeChild(video);
43 * @param {Element} playerContainer Main container.
44 * @param {Element} videoContainer Container for the video element.
45 * @param {Element} controlsContainer Container for video controls.
48 function FullWindowVideoControls(
49 playerContainer, videoContainer, controlsContainer) {
50 VideoControls.call(this,
53 loadTimeData.getString.bind(loadTimeData),
54 this.toggleFullScreen_.bind(this),
57 this.playerContainer_ = playerContainer;
60 window.addEventListener('resize', this.updateStyle.bind(this));
62 document.addEventListener('keydown', function(e) {
63 if (e.keyIdentifier == 'U+0020') { // Space
64 this.togglePlayStateWithFeedback();
67 if (e.keyIdentifier == 'U+001B') { // Escape
68 util.toggleFullScreen(
69 chrome.app.window.current(),
70 false); // Leave the full screen mode.
75 // TODO(mtomasz): Simplify. crbug.com/254318.
76 videoContainer.addEventListener('click', function(e) {
78 this.toggleLoopedModeWithFeedback(true);
79 if (!this.isPlaying())
80 this.togglePlayStateWithFeedback();
82 this.togglePlayStateWithFeedback();
86 this.inactivityWatcher_ = new MouseInactivityWatcher(playerContainer);
87 this.__defineGetter__('inactivityWatcher', function() {
88 return this.inactivityWatcher_;
91 this.inactivityWatcher_.check();
94 FullWindowVideoControls.prototype = { __proto__: VideoControls.prototype };
97 * Toggles the full screen mode.
100 FullWindowVideoControls.prototype.toggleFullScreen_ = function() {
101 var appWindow = chrome.app.window.current();
102 util.toggleFullScreen(appWindow, !util.isFullScreen(appWindow));
105 // TODO(mtomasz): Convert it to class members: crbug.com/171191.
106 var decodeErrorOccured;
111 * Initializes the video player window.
113 function loadVideoPlayer() {
114 document.ondragstart = function(e) { e.preventDefault() };
116 chrome.fileBrowserPrivate.getStrings(function(strings) {
117 loadTimeData.data = strings;
119 var url = window.videoUrl;
120 document.title = window.videoTitle;
122 controls = new FullWindowVideoControls(
123 document.querySelector('#video-player'),
124 document.querySelector('#video-container'),
125 document.querySelector('#controls'));
127 var reloadVideo = function(e) {
128 if (decodeErrorOccured &&
129 // Ignore shortcut keys
130 !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) {
137 document.addEventListener('keydown', reloadVideo, true);
138 document.addEventListener('click', reloadVideo, true);
143 * Unloads the player.
146 if (!controls.getMedia())
149 controls.savePosition(true /* exiting */);
154 * Reloads the player.
155 * @param {string} url URL of the video file.
157 function loadVideo(url) {
158 // Re-enable ui and hide error message if already displayed.
159 document.querySelector('#video-player').removeAttribute('disabled');
160 document.querySelector('#error').removeAttribute('visible');
161 controls.inactivityWatcher.disabled = false;
162 decodeErrorOccured = false;
164 // Detach the previous video element, if exists.
166 video.parentNode.removeChild(video);
168 video = document.createElement('video');
169 document.querySelector('#video-container').appendChild(video);
170 controls.attachMedia(video);
174 video.addEventListener('loadedmetadata', function() {
175 // TODO: chrome.app.window soon will be able to resize the content area.
176 // Until then use approximate title bar height.
177 var TITLE_HEIGHT = 33;
179 var aspect = video.videoWidth / video.videoHeight;
180 var newWidth = video.videoWidth;
181 var newHeight = video.videoHeight + TITLE_HEIGHT;
183 var shrinkX = newWidth / window.screen.availWidth;
184 var shrinkY = newHeight / window.screen.availHeight;
185 if (shrinkX > 1 || shrinkY > 1) {
186 if (shrinkY > shrinkX) {
187 newHeight = newHeight / shrinkY;
188 newWidth = (newHeight - TITLE_HEIGHT) * aspect;
190 newWidth = newWidth / shrinkX;
191 newHeight = newWidth / aspect + TITLE_HEIGHT;
195 var oldLeft = window.screenX;
196 var oldTop = window.screenY;
197 var oldWidth = window.outerWidth;
198 var oldHeight = window.outerHeight;
200 if (!oldWidth && !oldHeight) {
201 oldLeft = window.screen.availWidth / 2;
202 oldTop = window.screen.availHeight / 2;
205 var appWindow = chrome.app.window.current();
206 appWindow.resizeTo(newWidth, newHeight);
207 appWindow.moveTo(oldLeft - (newWidth - oldWidth) / 2,
208 oldTop - (newHeight - oldHeight) / 2);
215 util.addPageLoadHandler(loadVideoPlayer);