Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / remoting / webapp / fullscreen_v2.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 /**
6  * @fileoverview
7  * Full-screen implementation for apps v2, using chrome.AppWindow.
8  */
9
10 'use strict';
11
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
14
15 /**
16  * @constructor
17  * @implements {remoting.Fullscreen}
18  */
19 remoting.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
56 remoting.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
79 remoting.FullscreenAppsV2.prototype.toggle = function() {
80   this.activate(!this.isActive());
81 };
82
83 remoting.FullscreenAppsV2.prototype.isActive = function() {
84   return chrome.app.window.current().isFullscreen();
85 };
86
87 remoting.FullscreenAppsV2.prototype.addListener = function(callback) {
88   this.eventSource_.addEventListener(this.kEventName_, callback);
89 };
90
91 remoting.FullscreenAppsV2.prototype.removeListener = function(callback) {
92   this.eventSource_.removeEventListener(this.kEventName_, callback);
93 };
94
95 remoting.FullscreenAppsV2.prototype.syncWithMaximize = function(sync) {
96   if (sync && chrome.app.window.current().isMaximized()) {
97     this.activate(true);
98   }
99   this.hookingWindowEvents_ = sync;
100 };
101
102 remoting.FullscreenAppsV2.prototype.onFullscreened_ = function() {
103   this.notifyCallbacksOnRestore_ = true;
104   this.eventSource_.raiseEvent(this.kEventName_, true);
105 };
106
107 remoting.FullscreenAppsV2.prototype.onMaximized_ = function() {
108   if (this.hookingWindowEvents_) {
109     this.activate(true);
110   }
111 };
112
113 remoting.FullscreenAppsV2.prototype.onRestored_ = function() {
114   if (this.hookingWindowEvents_) {
115     this.activate(false);
116   }
117   if (this.notifyCallbacksOnRestore_) {
118     this.notifyCallbacksOnRestore_ = false;
119     this.eventSource_.raiseEvent(this.kEventName_, false);
120   }
121 };