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 * Mock implementation of SessionConnector for testing. | 7 * Mock implementation of SessionConnector for testing. |
8 * @suppress {checkTypes} | 8 * @suppress {checkTypes} |
9 */ | 9 */ |
10 | 10 |
11 'use strict'; | 11 'use strict'; |
12 | 12 |
13 /** @suppress {duplicate} */ | 13 /** @suppress {duplicate} */ |
14 var remoting = remoting || {}; | 14 var remoting = remoting || {}; |
15 | 15 |
16 /** | 16 /** |
| 17 * @param {HTMLElement} clientContainer Container element for the client view. |
| 18 * @param {function(remoting.ClientSession):void} onConnected Callback on |
| 19 * success. |
| 20 * @param {function(remoting.Error):void} onError Callback on error. |
| 21 * @param {function(string, string):boolean} onExtensionMessage The handler for |
| 22 * protocol extension messages. Returns true if a message is recognized; |
| 23 * false otherwise. |
| 24 * @param {function(remoting.Error):void} onConnectionFailed Callback for when |
| 25 * the connection fails. |
| 26 * @param {Array<string>} requiredCapabilities Connector capabilities |
| 27 * required by this application. |
| 28 * @param {string} defaultRemapKeys The default set of key mappings for the |
| 29 * client session to use. |
17 * @constructor | 30 * @constructor |
18 * @implements {remoting.SessionConnector} | 31 * @implements {remoting.SessionConnector} |
19 */ | 32 */ |
20 remoting.MockSessionConnector = function(clientContainer, onConnected, onError, | 33 remoting.MockSessionConnector = function(clientContainer, onConnected, onError, |
21 onExtensionMessage) { | 34 onExtensionMessage, |
| 35 onConnectionFailed, |
| 36 requiredCapabilities, |
| 37 defaultRemapKeys) { |
22 this.clientContainer_ = clientContainer; | 38 this.clientContainer_ = clientContainer; |
| 39 /** @type {function(remoting.ClientSession):void} */ |
23 this.onConnected_ = onConnected; | 40 this.onConnected_ = onConnected; |
24 this.onError = onError; | 41 this.onError = onError; |
25 this.onExtensionMessage_ = onExtensionMessage; | 42 this.onExtensionMessage_ = onExtensionMessage; |
| 43 this.onConnectionFailed_ = onConnectionFailed; |
| 44 this.requiredCapabilities_ = requiredCapabilities; |
| 45 this.defaultRemapKeys_ = defaultRemapKeys; |
| 46 |
| 47 /** @type {remoting.DesktopConnectedView.Mode} */ |
| 48 this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME; |
| 49 |
26 this.reset(); | 50 this.reset(); |
27 }; | 51 }; |
28 | 52 |
29 remoting.MockSessionConnector.prototype.reset = function() { | 53 remoting.MockSessionConnector.prototype.reset = function() { |
30 this.pairingRequested_ = false; | 54 /** @type {string} @private */ |
| 55 this.hostId_ = ''; |
31 }; | 56 }; |
32 | 57 |
33 remoting.MockSessionConnector.prototype.connectMe2Me = | 58 remoting.MockSessionConnector.prototype.connectMe2Me = |
34 function(host, fetchPin, fetchThirdPartyToken, | 59 function(host, fetchPin, fetchThirdPartyToken, |
35 clientPairingId, clientPairedSecret) { | 60 clientPairingId, clientPairedSecret) { |
36 this.mode_ = remoting.ClientSession.Mode.ME2ME; | 61 this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME; |
37 this.connect_(); | 62 this.connect_(); |
38 }; | 63 }; |
39 | 64 |
40 remoting.MockSessionConnector.prototype.retryConnectMe2Me = | 65 remoting.MockSessionConnector.prototype.retryConnectMe2Me = |
41 function(host, fetchPin, fetchThirdPartyToken, | 66 function(host, fetchPin, fetchThirdPartyToken, |
42 clientPairingId, clientPairedSecret) { | 67 clientPairingId, clientPairedSecret) { |
43 this.mode_ = remoting.ClientSession.Mode.ME2ME; | 68 this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME; |
44 this.connect_(); | 69 this.connect_(); |
45 }; | 70 }; |
46 | 71 |
47 remoting.MockSessionConnector.prototype.connectMe2App = | 72 remoting.MockSessionConnector.prototype.connectMe2App = |
48 function(host, fetchThirdPartyToken) { | 73 function(host, fetchThirdPartyToken) { |
49 this.mode_ = remoting.ClientSession.Mode.ME2APP; | 74 this.mode_ = remoting.DesktopConnectedView.Mode.APP_REMOTING; |
50 this.connect_(); | 75 this.connect_(); |
51 }; | 76 }; |
52 | 77 |
53 remoting.MockSessionConnector.prototype.updatePairingInfo = | 78 remoting.MockSessionConnector.prototype.updatePairingInfo = |
54 function(clientId, sharedSecret) { | 79 function(clientId, sharedSecret) { |
55 }; | 80 }; |
56 | 81 |
57 remoting.MockSessionConnector.prototype.connectIT2Me = | 82 remoting.MockSessionConnector.prototype.connectIT2Me = |
58 function(accessCode) { | 83 function(accessCode) { |
59 this.mode_ = remoting.ClientSession.Mode.ME2ME; | 84 this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME; |
60 this.connect_(); | 85 this.connect_(); |
61 }; | 86 }; |
62 | 87 |
63 remoting.MockSessionConnector.prototype.reconnect = function() { | 88 remoting.MockSessionConnector.prototype.reconnect = function() { |
64 base.debug.assert(this.mode_ == remoting.ClientSession.Mode.ME2ME); | 89 base.debug.assert(this.mode_ == remoting.DesktopConnectedView.Mode.ME2ME); |
65 this.connect_(); | 90 this.connect_(); |
66 }; | 91 }; |
67 | 92 |
68 remoting.MockSessionConnector.prototype.cancel = function() { | 93 remoting.MockSessionConnector.prototype.cancel = function() { |
69 }; | 94 }; |
70 | 95 |
71 remoting.MockSessionConnector.prototype.getConnectionMode = function() { | 96 remoting.MockSessionConnector.prototype.getConnectionMode = function() { |
72 return this.mode_; | 97 return this.mode_; |
73 }; | 98 }; |
74 | 99 |
75 remoting.MockSessionConnector.prototype.getHostId = function() { | 100 remoting.MockSessionConnector.prototype.getHostId = function() { |
76 return this.hostId_; | 101 return this.hostId_; |
77 }; | 102 }; |
78 | 103 |
79 remoting.MockSessionConnector.prototype.requestPairing = function() { | |
80 this.pairingRequested_ = true; | |
81 }; | |
82 | |
83 remoting.MockSessionConnector.prototype.pairingRequested = function() { | |
84 return this.pairingRequested_; | |
85 }; | |
86 | |
87 remoting.MockSessionConnector.prototype.connect_ = function() { | 104 remoting.MockSessionConnector.prototype.connect_ = function() { |
88 var signalling = new remoting.MockSignalStrategy(); | 105 var signalling = new remoting.MockSignalStrategy(); |
89 signalling.setStateForTesting(remoting.SignalStrategy.State.CONNECTED); | 106 signalling.setStateForTesting(remoting.SignalStrategy.State.CONNECTED); |
90 var hostName = 'Mock host'; | 107 var hostName = 'Mock host'; |
91 var accessCode = ''; | 108 var accessCode = ''; |
92 var authenticationMethods = ''; | 109 var authenticationMethods = ''; |
93 var hostId = ''; | 110 var hostId = ''; |
94 var hostJid = ''; | 111 var hostJid = ''; |
95 var hostPublicKey = ''; | 112 var hostPublicKey = ''; |
96 var clientPairingId = ''; | 113 var clientPairingId = ''; |
97 var clientPairedSecret = ''; | 114 var clientPairedSecret = ''; |
| 115 |
| 116 /** |
| 117 * @param {boolean} offerPairing |
| 118 * @param {function(string):void} callback |
| 119 */ |
98 var fetchPin = function(offerPairing, callback) { | 120 var fetchPin = function(offerPairing, callback) { |
99 window.setTimeout(function() { callback('') }, 0); | 121 window.setTimeout(function() { callback('') }, 0); |
100 }; | 122 }; |
101 var fetchThirdPartyToken = function(tokenUrl, scope, callback) { | 123 |
| 124 /** |
| 125 * @param {string} tokenUrl |
| 126 * @param {string} hostPublicKey |
| 127 * @param {string} scope |
| 128 * @param {function(string, string):void} callback |
| 129 */ |
| 130 var fetchThirdPartyToken = function(tokenUrl, hostPublicKey, scope, |
| 131 callback) { |
102 window.setTimeout(function() { callback('', '') }, 0); | 132 window.setTimeout(function() { callback('', '') }, 0); |
103 }; | 133 }; |
104 | 134 |
105 var clientSession = new remoting.ClientSession( | 135 var clientSession = new remoting.ClientSession( |
106 signalling, this.clientContainer_, hostName, | 136 signalling, this.clientContainer_, hostName, |
107 accessCode, fetchPin, fetchThirdPartyToken, | 137 accessCode, fetchPin, fetchThirdPartyToken, |
108 authenticationMethods, hostId, hostJid, hostPublicKey, | 138 authenticationMethods, hostId, hostJid, hostPublicKey, |
109 this.mode_, clientPairingId, clientPairedSecret); | 139 this.mode_, clientPairingId, clientPairedSecret); |
110 | 140 |
| 141 var that = this; |
| 142 /** @param {remoting.ClientSession.StateEvent} event */ |
111 var onStateChange = function(event) { | 143 var onStateChange = function(event) { |
112 if (event.current == remoting.ClientSession.State.CONNECTED) { | 144 if (event.current == remoting.ClientSession.State.CONNECTED) { |
113 this.onConnected_(clientSession); | 145 that.onConnected_(clientSession); |
114 } | 146 } |
115 }.bind(this); | 147 }; |
116 | 148 |
117 clientSession.addEventListener( | 149 clientSession.addEventListener( |
118 remoting.ClientSession.Events.stateChanged, | 150 remoting.ClientSession.Events.stateChanged, |
119 onStateChange); | 151 onStateChange); |
120 clientSession.createPluginAndConnect(this.onExtensionMessage_); | 152 clientSession.createPluginAndConnect(this.onExtensionMessage_); |
121 }; | 153 }; |
122 | 154 |
123 | 155 |
124 /** | 156 /** |
125 * @constructor | 157 * @constructor |
126 * @extends {remoting.SessionConnectorFactory} | 158 * @extends {remoting.SessionConnectorFactory} |
127 */ | 159 */ |
128 remoting.MockSessionConnectorFactory = function() {}; | 160 remoting.MockSessionConnectorFactory = function() {}; |
129 | 161 |
| 162 /** |
| 163 * @param {HTMLElement} clientContainer Container element for the client view. |
| 164 * @param {function(remoting.ClientSession):void} onConnected Callback on |
| 165 * success. |
| 166 * @param {function(remoting.Error):void} onError Callback on error. |
| 167 * @param {function(string, string):boolean} onExtensionMessage The handler for |
| 168 * protocol extension messages. Returns true if a message is recognized; |
| 169 * false otherwise. |
| 170 * @param {function(remoting.Error):void} onConnectionFailed Callback for when |
| 171 * the connection fails. |
| 172 * @param {Array<string>} requiredCapabilities Connector capabilities |
| 173 * required by this application. |
| 174 * @param {string} defaultRemapKeys The default set of key mappings to use |
| 175 * in the client session. |
| 176 * @return {remoting.MockSessionConnector} |
| 177 */ |
130 remoting.MockSessionConnectorFactory.prototype.createConnector = | 178 remoting.MockSessionConnectorFactory.prototype.createConnector = |
131 function(clientContainer, onConnected, onError, onExtensionMessage) { | 179 function(clientContainer, onConnected, onError, onExtensionMessage, |
| 180 onConnectionFailed, requiredCapabilities, defaultRemapKeys) { |
132 return new remoting.MockSessionConnector( | 181 return new remoting.MockSessionConnector( |
133 clientContainer, onConnected, onError, onExtensionMessage); | 182 clientContainer, onConnected, onError, onExtensionMessage, |
| 183 onConnectionFailed, requiredCapabilities, defaultRemapKeys); |
134 }; | 184 }; |
OLD | NEW |