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