Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / webapp / crd / js / desktop_remoting_activity.js
blob03f2ead9742c491d5c9a17a7661b9e0cbbd97dcd
1 // Copyright 2015 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 /** @suppress {duplicate} */
6 var remoting = remoting || {};
8 (function() {
10 'use strict';
12 /**
13  * This class provides common functionality to Me2MeActivity and It2MeActivity,
14  * e.g. constructing the relevant UX, and delegate the custom handling of the
15  * session state changes to the |parentActivity|.
16  *
17  * @param {remoting.Activity} parentActivity
18  * @implements {remoting.ClientSession.EventHandler}
19  * @implements {base.Disposable}
20  * @constructor
21  */
22 remoting.DesktopRemotingActivity = function(parentActivity) {
23   /** @private */
24   this.parentActivity_ = parentActivity;
25   /** @private {remoting.DesktopConnectedView} */
26   this.connectedView_ = null;
28   /** @private */
29   this.sessionFactory_ = new remoting.ClientSessionFactory(
30       document.querySelector('#client-container .client-plugin-container'),
31       [/* No special capabilities required. */]);
33   /** @private {remoting.ClientSession} */
34   this.session_ = null;
35   /** @private {remoting.ConnectingDialog} */
36   this.connectingDialog_ = remoting.modalDialogFactory.createConnectingDialog(
37       parentActivity.stop.bind(parentActivity));
40 /**
41  * Initiates a connection.
42  *
43  * @param {remoting.Host} host the Host to connect to.
44  * @param {remoting.CredentialsProvider} credentialsProvider
45  * @param {remoting.SessionLogger} logger
46  * @return {void} Nothing.
47  */
48 remoting.DesktopRemotingActivity.prototype.start =
49     function(host, credentialsProvider, logger) {
50   var that = this;
51   var useApiaryForLogging = host.loggingChannel === 'APIARY';
52   this.sessionFactory_.createSession(this, logger, useApiaryForLogging).then(
53     function(/** remoting.ClientSession */ session) {
54       that.session_ = session;
56       // Update the host version and the Mode for the legacy XMPP logger.
57       // TODO(kelvinp): Remove this block of code when we have migrated away
58       // from XMPP-based logging (crbug.com/523423).
59       session.getLogger().setHostVersion(host.hostVersion);
60       var Mode = remoting.ChromotingEvent.Mode;
61       if (that.parentActivity_ instanceof remoting.It2MeActivity) {
62         session.getLogger().setLogEntryMode(Mode.IT2ME);
63       } else if (that.parentActivity_ instanceof remoting.Me2MeActivity) {
64         session.getLogger().setLogEntryMode(Mode.ME2ME);
65       }
67       session.connect(host, credentialsProvider);
68   }).catch(remoting.Error.handler(
69     function(/** !remoting.Error */ error) {
70       that.parentActivity_.onConnectionFailed(error);
71   }));
74 remoting.DesktopRemotingActivity.prototype.stop = function() {
75   if (this.session_) {
76     this.session_.disconnect(remoting.Error.none());
77     console.log('Disconnected.');
78   }
81 /**
82  * @param {remoting.ConnectionInfo} connectionInfo
83  */
84 remoting.DesktopRemotingActivity.prototype.onConnected =
85     function(connectionInfo) {
86   this.connectingDialog_.hide();
87   remoting.setMode(remoting.AppMode.IN_SESSION);
88   if (!base.isAppsV2()) {
89     remoting.toolbar.center();
90     remoting.toolbar.preview();
91   }
93   this.connectedView_ = remoting.DesktopConnectedView.create(
94       document.getElementById('client-container'), connectionInfo);
96   // Apply the default or previously-specified keyboard remapping.
97   var remapping = connectionInfo.host().options.remapKeys;
98   if (base.isEmptyObject(remapping) && remoting.platformIsChromeOS()) {
99     // Under ChromeOS, remap the right Control key to the right Win/Cmd key.
100     remapping = {0x0700e4: 0x0700e7};
101   }
102   connectionInfo.plugin().setRemapKeys(remapping);
104   if (connectionInfo.plugin().hasCapability(
105           remoting.ClientSession.Capability.VIDEO_RECORDER)) {
106     var recorder = new remoting.VideoFrameRecorder();
107     connectionInfo.plugin().extensions().register(recorder);
108     this.connectedView_.setVideoFrameRecorder(recorder);
109   }
111   this.parentActivity_.onConnected(connectionInfo);
114 remoting.DesktopRemotingActivity.prototype.onDisconnected = function(reason) {
115   if (this.handleError_(reason)) {
116     return;
117   }
118   this.parentActivity_.onDisconnected(reason);
122  * @param {!remoting.Error} error
123  */
124 remoting.DesktopRemotingActivity.prototype.onConnectionFailed =
125     function(error) {
126   if (this.handleError_(error)) {
127     return;
128   }
129   this.parentActivity_.onConnectionFailed(error);
133  * @param {!remoting.Error} error The error to be localized and displayed.
134  * @return {boolean} returns true if the error is handled.
135  * @private
136  */
137 remoting.DesktopRemotingActivity.prototype.handleError_ = function(error) {
138   if (error.hasTag(remoting.Error.Tag.AUTHENTICATION_FAILED)) {
139     remoting.setMode(remoting.AppMode.HOME);
140     remoting.handleAuthFailureAndRelaunch();
141     return true;
142   }
143   return false;
146 remoting.DesktopRemotingActivity.prototype.dispose = function() {
147   base.dispose(this.connectedView_);
148   this.connectedView_ = null;
149   base.dispose(this.session_);
150   this.session_ = null;
151   this.connectingDialog_.hide();
154 /** @return {remoting.DesktopConnectedView} */
155 remoting.DesktopRemotingActivity.prototype.getConnectedView = function() {
156   return this.connectedView_;
160  * @return {remoting.ClientSession}.
161  */
162 remoting.DesktopRemotingActivity.prototype.getSession = function() {
163   return this.session_;
166 /** @return {remoting.ConnectingDialog} */
167 remoting.DesktopRemotingActivity.prototype.getConnectingDialog = function() {
168   return this.connectingDialog_;
171 })();