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