-
Notifications
You must be signed in to change notification settings - Fork 8
/
hangout_session.js
81 lines (69 loc) · 2.22 KB
/
hangout_session.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview
* Class to communicate with the background scripts via chrome runtime
* messages to
* 1. Forward session state notifications
* 2. Closes the window when the session terminates
*/
'use strict';
/** @suppress {duplicate} */
var remoting = remoting || {};
/**
* @constructor
* @param {string} senderId id of the current tab or window.
*/
remoting.HangoutSession = function(senderId) {
/**
* @private
* @type {chrome.runtime.Port}
*/
this.port_ = null;
/**
* @private
* @type {string}
*/
this.senderId_ = senderId;
};
remoting.HangoutSession.prototype.init = function() {
var portName = 'it2me.helper.webapp@' + this.senderId_;
this.port_ = chrome.runtime.connect({name: portName});
remoting.hangoutSessionEvents.addEventListener(
remoting.hangoutSessionEvents.sessionStateChanged,
this.onSessionStateChanged_.bind(this));
};
/**
* @param {remoting.ClientSession.State=} state
*/
remoting.HangoutSession.prototype.onSessionStateChanged_ = function(state) {
var State = remoting.ClientSession.State;
try {
this.port_.postMessage({method: 'sessionStateChanged', state: state});
} catch (/** @type {Error} */ error) {
// postMessage will throw an exception if the port is disconnected.
// We can safely ignore this exception.
console.error(error);
} finally {
if (state === State.FAILED || state === State.CLOSED) {
// close the current window
if (base.isAppsV2()) {
chrome.app.window.current().close();
} else {
window.close();
}
}
}
};
/**
* remoting.clientSession does not exist until the session is connected.
* hangoutSessionEvents serves as a global event source to plumb session
* state changes until we cleanup clientSession and sessionConnector.
* @type {base.EventSourceImpl}
*/
remoting.hangoutSessionEvents = new base.EventSourceImpl();
/** @type {string} */
remoting.hangoutSessionEvents.sessionStateChanged = "sessionStateChanged";
remoting.hangoutSessionEvents.defineEvents(
[remoting.hangoutSessionEvents.sessionStateChanged]);