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 v2, using chrome.AppWindow.
8 */
9
10'use strict';
11
12/** @suppress {duplicate} */
13var remoting = remoting || {};
14
15/**
16 * @constructor
17 * @implements {remoting.Fullscreen}
18 */
19remoting.FullscreenAppsV2 = function() {
20  /**
21   * @type {boolean} True if maximize/restore events are being hooked.
22   * @private
23   */
24  this.hookingWindowEvents_ = false;
25
26  /**
27   * @type {boolean} True if the next onRestored event should cause callbacks
28   *     to be notified of a full-screen changed event. onRestored fires when
29   *     full-screen mode is exited and also when the window is restored from
30   *     being minimized; callbacks should not be notified of the latter.
31   * @private
32   */
33  this.notifyCallbacksOnRestore_ = this.isActive();
34
35  /**
36   * @type {string} Internal 'full-screen changed' event name
37   * @private
38   */
39  this.kEventName_ = '_fullscreenchanged';
40
41  /**
42   * @type {base.EventSource}
43   * @private
44   */
45  this.eventSource_ = new base.EventSource();
46  this.eventSource_.defineEvents([this.kEventName_]);
47
48  chrome.app.window.current().onFullscreened.addListener(
49      this.onFullscreened_.bind(this));
50  chrome.app.window.current().onMaximized.addListener(
51      this.onMaximized_.bind(this));
52  chrome.app.window.current().onRestored.addListener(
53      this.onRestored_.bind(this));
54};
55
56remoting.FullscreenAppsV2.prototype.activate = function(
57    fullscreen, opt_onDone) {
58  if (opt_onDone) {
59    if (this.isActive() == fullscreen) {
60      opt_onDone();
61    } else {
62      /** @type {remoting.Fullscreen} */
63      var that = this;
64      var callbackAndRemoveListener = function() {
65        that.removeListener(callbackAndRemoveListener);
66        opt_onDone();
67      };
68      this.addListener(callbackAndRemoveListener);
69    }
70  }
71
72  if (fullscreen) {
73    chrome.app.window.current().fullscreen();
74  } else if (this.isActive()) {
75    chrome.app.window.current().restore();
76  }
77};
78
79remoting.FullscreenAppsV2.prototype.toggle = function() {
80  this.activate(!this.isActive());
81};
82
83remoting.FullscreenAppsV2.prototype.isActive = function() {
84  return chrome.app.window.current().isFullscreen();
85};
86
87remoting.FullscreenAppsV2.prototype.addListener = function(callback) {
88  this.eventSource_.addEventListener(this.kEventName_, callback);
89};
90
91remoting.FullscreenAppsV2.prototype.removeListener = function(callback) {
92  this.eventSource_.removeEventListener(this.kEventName_, callback);
93};
94
95remoting.FullscreenAppsV2.prototype.syncWithMaximize = function(sync) {
96  if (sync && chrome.app.window.current().isMaximized()) {
97    chrome.app.window.current().restore();
98    this.activate(true);
99  }
100  this.hookingWindowEvents_ = sync;
101};
102
103remoting.FullscreenAppsV2.prototype.onFullscreened_ = function() {
104  this.notifyCallbacksOnRestore_ = true;
105  this.eventSource_.raiseEvent(this.kEventName_, true);
106  document.body.classList.add('fullscreen');
107};
108
109remoting.FullscreenAppsV2.prototype.onMaximized_ = function() {
110  if (this.hookingWindowEvents_) {
111    chrome.app.window.current().restore();
112    this.activate(true);
113  }
114};
115
116remoting.FullscreenAppsV2.prototype.onRestored_ = function() {
117  // TODO(jamiewalch): ChromeOS generates a spurious onRestore event if
118  // fullscreen() is called from an onMaximized handler (crbug.com/394819),
119  // so ignore the callback if the window is still full-screen.
120  if (this.isActive()) {
121    return;
122  }
123
124  document.body.classList.remove('fullscreen');
125  if (this.hookingWindowEvents_) {
126    this.activate(false);
127  }
128  if (this.notifyCallbacksOnRestore_) {
129    this.notifyCallbacksOnRestore_ = false;
130    this.eventSource_.raiseEvent(this.kEventName_, false);
131  }
132};
133