Home | History | Annotate | Download | only in webapp
      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 /**
      6  * @fileoverview
      7  * Full-screen implementation for apps v1, using webkitRequestFullscreen.
      8  */
      9 
     10 'use strict';
     11 
     12 /** @suppress {duplicate} */
     13 var remoting = remoting || {};
     14 
     15 /**
     16  * @constructor
     17  * @implements {remoting.Fullscreen}
     18  */
     19 remoting.FullscreenAppsV1 = function() {
     20   /**
     21    * @type {string} Internal 'full-screen changed' event name
     22    * @private
     23    */
     24   this.kEventName_ = '_fullscreenchanged';
     25 
     26   /**
     27    * @type {base.EventSource}
     28    * @private
     29    */
     30   this.eventSource_ = new base.EventSource();
     31   this.eventSource_.defineEvents([this.kEventName_]);
     32 
     33   document.addEventListener(
     34       'webkitfullscreenchange',
     35       this.onFullscreenChanged_.bind(this),
     36       false);
     37 };
     38 
     39 remoting.FullscreenAppsV1.prototype.activate = function(
     40     fullscreen, opt_onDone) {
     41   if (opt_onDone) {
     42     if (this.isActive() == fullscreen) {
     43       opt_onDone();
     44     } else {
     45       /** @type {remoting.Fullscreen} */
     46       var that = this;
     47       var callbackAndRemoveListener = function() {
     48         that.removeListener(callbackAndRemoveListener);
     49         opt_onDone();
     50       };
     51       this.addListener(callbackAndRemoveListener);
     52     }
     53   }
     54 
     55   if (fullscreen) {
     56     document.body.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
     57   } else {
     58     document.webkitCancelFullScreen();
     59   }
     60 };
     61 
     62 remoting.FullscreenAppsV1.prototype.toggle = function() {
     63   this.activate(!this.isActive());
     64 };
     65 
     66 remoting.FullscreenAppsV1.prototype.isActive = function() {
     67   return document.webkitIsFullScreen;
     68 };
     69 
     70 remoting.FullscreenAppsV1.prototype.addListener = function(callback) {
     71   this.eventSource_.addEventListener(this.kEventName_, callback);
     72 };
     73 
     74 remoting.FullscreenAppsV1.prototype.removeListener = function(callback) {
     75   this.eventSource_.removeEventListener(this.kEventName_, callback);
     76 };
     77 
     78 /**
     79  * @private
     80  */
     81 remoting.FullscreenAppsV1.prototype.onFullscreenChanged_ = function() {
     82   // Querying full-screen immediately after the webkitfullscreenchange
     83   // event fires sometimes gives the wrong answer on Mac, perhaps due to
     84   // the time taken to animate presentation mode. Since I haven't been able
     85   // to isolate the exact repro steps, and we're not planning on using this
     86   // API for much longer, this hack will suffice for now.
     87   window.setTimeout(
     88       /** @this {remoting.FullscreenAppsV1} */
     89       function() {
     90         this.eventSource_.raiseEvent(this.kEventName_, this.isActive());
     91       }.bind(this),
     92       500);
     93 };
     94