Home | History | Annotate | Download | only in webapp
      1 // Copyright (c) 2012 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.
      4 
      5 'use strict';
      6 
      7 /** @suppress {duplicate} */
      8 var remoting = remoting || {};
      9 
     10 function onLoad() {
     11   var goHome = function() {
     12     remoting.setMode(remoting.AppMode.HOME);
     13   };
     14   var goEnterAccessCode = function() {
     15     // We don't need a token until we authenticate, but asking for one here
     16     // handles the token-expired case earlier, avoiding asking the user for
     17     // the access code both before and after re-authentication.
     18     remoting.identity.callWithToken(
     19         /** @param {string} token */
     20         function(token) {
     21           remoting.setMode(remoting.AppMode.CLIENT_UNCONNECTED);
     22         },
     23         remoting.showErrorMessage);
     24   };
     25   var goFinishedIT2Me = function() {
     26     if (remoting.currentMode == remoting.AppMode.CLIENT_CONNECT_FAILED_IT2ME) {
     27       remoting.setMode(remoting.AppMode.CLIENT_UNCONNECTED);
     28     } else {
     29       remoting.setMode(remoting.AppMode.HOME);
     30     }
     31   };
     32   /** @param {Event} event The event. */
     33   var sendAccessCode = function(event) {
     34     remoting.connectIT2Me();
     35     event.preventDefault();
     36   };
     37   var reconnect = function() {
     38     remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
     39     remoting.connector.reconnect();
     40   };
     41   var doAuthRedirect = function() {
     42     remoting.oauth2.doAuthRedirect();
     43   };
     44   /** @param {Event} event The event. */
     45   var stopDaemon = function(event) {
     46     remoting.hostSetupDialog.showForStop();
     47     event.stopPropagation();
     48   };
     49   var cancelAccessCode = function() {
     50     remoting.setMode(remoting.AppMode.HOME);
     51     document.getElementById('access-code-entry').value = '';
     52   };
     53   /** @type {Array.<{event: string, id: string, fn: function(Event):void}>} */
     54   var actions = [
     55       { event: 'click', id: 'sign-out', fn: remoting.signOut },
     56       { event: 'click', id: 'toolbar-disconnect', fn: remoting.disconnect },
     57       { event: 'click', id: 'send-ctrl-alt-del',
     58         fn: remoting.sendCtrlAltDel },
     59       { event: 'click', id: 'send-print-screen',
     60         fn: remoting.sendPrintScreen },
     61       { event: 'click', id: 'auth-button', fn: doAuthRedirect },
     62       { event: 'click', id: 'share-button', fn: remoting.tryShare },
     63       { event: 'click', id: 'access-mode-button', fn: goEnterAccessCode },
     64       { event: 'click', id: 'cancel-share-button', fn: remoting.cancelShare },
     65       { event: 'click', id: 'stop-sharing-button', fn: remoting.cancelShare },
     66       { event: 'click', id: 'host-finished-button', fn: goHome },
     67       { event: 'click', id: 'client-finished-it2me-button', fn: goHome },
     68       { event: 'click', id: 'client-finished-me2me-button', fn: goHome },
     69       { event: 'click', id: 'client-reconnect-button', fn: reconnect },
     70       { event: 'click', id: 'cancel-access-code-button', fn: cancelAccessCode},
     71       { event: 'click', id: 'cancel-connect-button', fn: goHome },
     72       { event: 'click', id: 'toolbar-stub',
     73         fn: function() { remoting.toolbar.toggle(); } },
     74       { event: 'click', id: 'start-daemon',
     75         fn: function() { remoting.hostSetupDialog.showForStart(); } },
     76       { event: 'click', id: 'change-daemon-pin',
     77         fn: function() { remoting.hostSetupDialog.showForPin(); } },
     78       { event: 'click', id: 'stop-daemon', fn: stopDaemon },
     79       { event: 'submit', id: 'access-code-form', fn: sendAccessCode },
     80       { event: 'click', id: 'get-started-it2me',
     81         fn: remoting.showIT2MeUiAndSave },
     82       { event: 'click', id: 'get-started-me2me',
     83         fn: remoting.showMe2MeUiAndSave },
     84       { event: 'click', id: 'daemon-pin-cancel', fn: goHome },
     85       { event: 'click', id: 'host-config-done-dismiss', fn: goHome },
     86       { event: 'click', id: 'host-config-error-dismiss', fn: goHome },
     87       { event: 'click', id: 'token-refresh-error-ok', fn: goHome },
     88       { event: 'click', id: 'token-refresh-error-sign-in', fn: doAuthRedirect },
     89       { event: 'click', id: 'open-paired-client-manager-dialog',
     90         fn: remoting.setMode.bind(null,
     91                                   remoting.AppMode.HOME_MANAGE_PAIRINGS) },
     92       { event: 'click', id: 'close-paired-client-manager-dialog', fn: goHome }
     93   ];
     94 
     95   for (var i = 0; i < actions.length; ++i) {
     96     var action = actions[i];
     97     var element = document.getElementById(action.id);
     98     if (element) {
     99       element.addEventListener(action.event, action.fn, false);
    100     } else {
    101       console.error('Could not set ' + action.event +
    102                     ' event handler on element ' + action.id +
    103                     ': element not found.');
    104     }
    105   }
    106   remoting.init();
    107 
    108   window.addEventListener('resize', remoting.onResize, false);
    109   if (!remoting.isAppsV2) {
    110     window.addEventListener('beforeunload', remoting.promptClose, false);
    111     window.addEventListener('unload', remoting.disconnect, false);
    112   }
    113 }
    114 
    115 window.addEventListener('load', onLoad, false);
    116