Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / crd / js / crd_connect.js
blobea4f7a83f1315de22ee7429707daadcac9149fc7
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 /**
6  * @fileoverview
7  * Functions related to the 'client screen' for Chromoting.
8  */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16  * Initiate an IT2Me connection.
17  */
18 remoting.connectIT2Me = function() {
19   var connector = remoting.app.getSessionConnector();
20   var accessCode = document.getElementById('access-code-entry').value;
21   remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
22   connector.connectIT2Me(accessCode);
25 /**
26  * Entry-point for Me2Me connections, handling showing of the host-upgrade nag
27  * dialog if necessary.
28  *
29  * @param {string} hostId The unique id of the host.
30  * @return {void} Nothing.
31  */
32 remoting.connectMe2Me = function(hostId) {
33   var host = remoting.hostList.getHostForId(hostId);
34   if (!host) {
35     remoting.app.onError(remoting.Error.HOST_IS_OFFLINE);
36     return;
37   }
38   var webappVersion = chrome.runtime.getManifest().version;
39   if (remoting.Host.needsUpdate(host, webappVersion)) {
40     var needsUpdateMessage =
41         document.getElementById('host-needs-update-message');
42     l10n.localizeElementFromTag(needsUpdateMessage,
43                                 /*i18n-content*/'HOST_NEEDS_UPDATE_TITLE',
44                                 host.hostName);
45     /** @type {Element} */
46     var connect = document.getElementById('host-needs-update-connect-button');
47     /** @type {Element} */
48     var cancel = document.getElementById('host-needs-update-cancel-button');
49     /** @param {Event} event */
50     var onClick = function(event) {
51       connect.removeEventListener('click', onClick, false);
52       cancel.removeEventListener('click', onClick, false);
53       if (event.target == connect) {
54         remoting.connectMe2MeHostVersionAcknowledged_(host);
55       } else {
56         remoting.setMode(remoting.AppMode.HOME);
57       }
58     }
59     connect.addEventListener('click', onClick, false);
60     cancel.addEventListener('click', onClick, false);
61     remoting.setMode(remoting.AppMode.CLIENT_HOST_NEEDS_UPGRADE);
62   } else {
63     remoting.connectMe2MeHostVersionAcknowledged_(host);
64   }
67 /**
68  * Shows PIN entry screen localized to include the host name, and registers
69  * a host-specific one-shot event handler for the form submission.
70  *
71  * @param {remoting.Host} host The Me2Me host to which to connect.
72  * @return {void} Nothing.
73  */
74 remoting.connectMe2MeHostVersionAcknowledged_ = function(host) {
75   /** @type {remoting.SessionConnector} */
76   var connector = remoting.app.getSessionConnector();
77   remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
79   /**
80    * @param {string} tokenUrl Token-issue URL received from the host.
81    * @param {string} hostPublicKey Host public key (DER and Base64 encoded).
82    * @param {string} scope OAuth scope to request the token for.
83    * @param {function(string, string):void} onThirdPartyTokenFetched Callback.
84    */
85   var fetchThirdPartyToken = function(
86       tokenUrl, hostPublicKey, scope, onThirdPartyTokenFetched) {
87     var thirdPartyTokenFetcher = new remoting.ThirdPartyTokenFetcher(
88         tokenUrl, hostPublicKey, scope, host.tokenUrlPatterns,
89         onThirdPartyTokenFetched);
90     thirdPartyTokenFetcher.fetchToken();
91   };
93   /**
94    * @param {boolean} supportsPairing
95    * @param {function(string):void} onPinFetched
96    */
97   var requestPin = function(supportsPairing, onPinFetched) {
98     /** @type {Element} */
99     var pinForm = document.getElementById('pin-form');
100     /** @type {Element} */
101     var pinCancel = document.getElementById('cancel-pin-entry-button');
102     /** @type {Element} */
103     var rememberPin = document.getElementById('remember-pin');
104     /** @type {Element} */
105     var rememberPinCheckbox = document.getElementById('remember-pin-checkbox');
106     /**
107      * Event handler for both the 'submit' and 'cancel' actions. Using
108      * a single handler for both greatly simplifies the task of making
109      * them one-shot. If separate handlers were used, each would have
110      * to unregister both itself and the other.
111      *
112      * @param {Event} event The click or submit event.
113      */
114     var onSubmitOrCancel = function(event) {
115       pinForm.removeEventListener('submit', onSubmitOrCancel, false);
116       pinCancel.removeEventListener('click', onSubmitOrCancel, false);
117       var pinField = document.getElementById('pin-entry');
118       var pin = pinField.value;
119       pinField.value = '';
120       if (event.target == pinForm) {
121         event.preventDefault();
123         // Set the focus away from the password field. This has to be done
124         // before the password field gets hidden, to work around a Blink
125         // clipboard-handling bug - http://crbug.com/281523.
126         document.getElementById('pin-connect-button').focus();
128         remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
129         onPinFetched(pin);
130         if (rememberPinCheckbox.checked) {
131           /** @type {boolean} */
132           remoting.pairingRequested = true;
133         }
134       } else {
135         remoting.setMode(remoting.AppMode.HOME);
136       }
137     };
138     pinForm.addEventListener('submit', onSubmitOrCancel, false);
139     pinCancel.addEventListener('click', onSubmitOrCancel, false);
140     rememberPin.hidden = !supportsPairing;
141     rememberPinCheckbox.checked = false;
142     var message = document.getElementById('pin-message');
143     l10n.localizeElement(message, host.hostName);
144     remoting.setMode(remoting.AppMode.CLIENT_PIN_PROMPT);
145   };
147   /** @param {Object} settings */
148   var connectMe2MeHostSettingsRetrieved = function(settings) {
149     /** @type {string} */
150     var clientId = '';
151     /** @type {string} */
152     var sharedSecret = '';
153     var pairingInfo = /** @type {Object} */ (settings['pairingInfo']);
154     if (pairingInfo) {
155       clientId = /** @type {string} */ (pairingInfo['clientId']);
156       sharedSecret = /** @type {string} */ (pairingInfo['sharedSecret']);
157     }
158     connector.connectMe2Me(host, requestPin, fetchThirdPartyToken,
159                                     clientId, sharedSecret);
160   }
162   remoting.HostSettings.load(host.hostId, connectMe2MeHostSettingsRetrieved);