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.
7 * Class to communicate with the background scripts via chrome runtime
9 * 1. Forward session state notifications
10 * 2. Closes the window when the session terminates
15 /** @suppress {duplicate} */
16 var remoting = remoting || {};
20 * @param {string} senderId id of the current tab or window.
22 remoting.HangoutSession = function(senderId) {
25 * @type {chrome.runtime.Port}
33 this.senderId_ = senderId;
36 remoting.HangoutSession.prototype.init = function() {
37 var portName = 'it2me.helper.webapp@' + this.senderId_;
38 this.port_ = chrome.runtime.connect({name: portName});
40 remoting.hangoutSessionEvents.addEventListener(
41 remoting.hangoutSessionEvents.sessionStateChanged,
42 this.onSessionStateChanged_.bind(this));
46 * @param {remoting.ClientSession.State} state
48 remoting.HangoutSession.prototype.onSessionStateChanged_ = function(state) {
49 var State = remoting.ClientSession.State;
51 this.port_.postMessage({method: 'sessionStateChanged', state: state});
53 // postMessage will throw an exception if the port is disconnected.
54 // We can safely ignore this exception.
55 var error = /** @type {Error} */ e;
58 if (state === State.FAILED || state === State.CLOSED) {
59 // close the current window
60 if (base.isAppsV2()) {
61 chrome.app.window.current().close();
71 * remoting.clientSession does not exist until the session is connected.
72 * hangoutSessionEvents serves as a global event source to plumb session
73 * state changes until we cleanup clientSession and sessionConnector.
74 * @type {base.EventSource}
76 remoting.hangoutSessionEvents = new base.EventSource();
79 remoting.hangoutSessionEvents.sessionStateChanged = "sessionStateChanged";
81 remoting.hangoutSessionEvents.defineEvents(
82 [remoting.hangoutSessionEvents.sessionStateChanged]);