Implement FallbackSignalStrategy.
[chromium-blink-merge.git] / remoting / webapp / crd / js / signal_strategy.js
blobf07613f9bd3a71175197341e38896592fce4788f
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.
5 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 /**
11  * Abstract interface for various signaling mechanisms.
12  *
13  * @interface
14  * @extends {base.Disposable}
15  */
16 remoting.SignalStrategy = function() {};
18 /**
19  * @enum {number} SignalStrategy states. Possible state transitions:
20  *    NOT_CONNECTED -> CONNECTING (connect() called).
21  *    CONNECTING -> HANDSHAKE (connected successfully).
22  *    HANDSHAKE -> CONNECTED (authenticated successfully).
23  *    CONNECTING -> FAILED (connection failed).
24  *    HANDSHAKE -> FAILED (authentication failed).
25  *    * -> CLOSED (dispose() called).
26  *
27  * Do not re-order these values without updating fallback_signal_strategy.js.
28  */
29 remoting.SignalStrategy.State = {
30   NOT_CONNECTED: 0,
31   CONNECTING: 1,
32   HANDSHAKE: 2,
33   CONNECTED: 3,
34   FAILED: 4,
35   CLOSED: 5
38 remoting.SignalStrategy.prototype.dispose = function() {};
40 /**
41  * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
42  *     incoming messages.
43  */
44 remoting.SignalStrategy.prototype.setIncomingStanzaCallback =
45     function(onIncomingStanzaCallback) {};
47 /**
48  * @param {string} server
49  * @param {string} username
50  * @param {string} authToken
51  */
52 remoting.SignalStrategy.prototype.connect =
53     function(server, username, authToken) {
56 /**
57  * Sends a message. Can be called only in CONNECTED state.
58  * @param {string} message
59  */
60 remoting.SignalStrategy.prototype.sendMessage = function(message) {};
62 /** @return {remoting.SignalStrategy.State} Current state */
63 remoting.SignalStrategy.prototype.getState = function() {};
65 /** @return {remoting.Error} Error when in FAILED state. */
66 remoting.SignalStrategy.prototype.getError = function() {};
68 /** @return {string} Current JID when in CONNECTED state. */
69 remoting.SignalStrategy.prototype.getJid = function() {};
71 /**
72  * Creates the appropriate signal strategy for the current environment.
73  * @param {function(remoting.SignalStrategy.State): void} onStateChangedCallback
74  * @return {remoting.SignalStrategy} New signal strategy object.
75  */
76 remoting.SignalStrategy.create = function(onStateChangedCallback) {
77   // Only use XMPP when TCP API is available and TLS support is enabled. That's
78   // not the case for V1 app (socket API is available only to platform apps)
79   // and for Chrome releases before 38.
80   if (chrome.socket && chrome.socket.secure) {
81     /**
82      * @param {function(remoting.SignalStrategy.State): void} onStateChanged
83      */
84     var xmppFactory = function(onStateChanged) {
85       return new remoting.XmppConnection(onStateChanged);
86     };
88     /**
89      * @param {function(remoting.SignalStrategy.State): void} onStateChanged
90      */
91     var wcsFactory = function(onStateChanged) {
92       return new remoting.WcsAdapter(onStateChanged);
93     };
95     /**
96      * @param {remoting.FallbackSignalStrategy.Progress} progress
97      */
98     var progressCallback = function(progress) {
99       console.log('FallbackSignalStrategy progress: ' + progress);
100     };
102     return new remoting.FallbackSignalStrategy(
103         xmppFactory, wcsFactory, onStateChangedCallback, progressCallback);
105   } else {
106     return new remoting.WcsAdapter(onStateChangedCallback);
107   }