summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/app_remoting/js/application_context_menu.js
blob: a80ed856a3d352e6740f54a7c91a1ac544790850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview
 * Class representing the application's context menu.
 */

'use strict';

/** @suppress {duplicate} */
var remoting = remoting || {};

/**
 * @param {remoting.ContextMenuAdapter} adapter
 * @param {remoting.ClientPlugin} plugin
 * @param {remoting.ClientSession} clientSession
 * @param {remoting.WindowShape} windowShape
 *
 * @constructor
 * @implements {base.Disposable}
 */
remoting.ApplicationContextMenu = function(adapter, plugin, clientSession,
                                           windowShape) {
  /** @private */
  this.adapter_ = adapter;

  /** @private */
  this.clientSession_ = clientSession;

  this.adapter_.create(
      remoting.ApplicationContextMenu.kSendFeedbackId,
      l10n.getTranslationOrError(/*i18n-content*/'SEND_FEEDBACK'),
      false);
  this.adapter_.create(
      remoting.ApplicationContextMenu.kShowStatsId,
      l10n.getTranslationOrError(/*i18n-content*/'SHOW_STATS'),
      true);

  // TODO(kelvinp):Unhook this event on shutdown.
  this.adapter_.addListener(this.onClicked_.bind(this));

  /** @private {string} */
  this.hostId_ = '';

  /** @private */
  this.stats_ = new remoting.ConnectionStats(
      document.getElementById('statistics'), plugin, windowShape);
};

remoting.ApplicationContextMenu.prototype.dispose = function() {
  base.dispose(this.stats_);
  this.stats_ = null;
};

/**
 * @param {string} hostId
 */
remoting.ApplicationContextMenu.prototype.setHostId = function(hostId) {
  this.hostId_ = hostId;
};

/**
 * Add an indication of the connection RTT to the 'Show statistics' menu item.
 *
 * @param {number} rttMs The RTT of the connection, in ms.
 */
remoting.ApplicationContextMenu.prototype.updateConnectionRTT =
    function(rttMs) {
  var rttText =
      rttMs < 50  ? /*i18n-content*/'CONNECTION_QUALITY_GOOD' :
      rttMs < 100 ? /*i18n-content*/'CONNECTION_QUALITY_FAIR' :
                    /*i18n-content*/'CONNECTION_QUALITY_POOR';
  rttText = l10n.getTranslationOrError(rttText);
  this.adapter_.updateTitle(
      remoting.ApplicationContextMenu.kShowStatsId,
      l10n.getTranslationOrError(/*i18n-content*/'SHOW_STATS_WITH_RTT',
                                 rttText));
};

/** @param {OnClickData=} info */
remoting.ApplicationContextMenu.prototype.onClicked_ = function(info) {
  var menuId = /** @type {string} */ (info.menuItemId.toString());
  switch (menuId) {

    case remoting.ApplicationContextMenu.kSendFeedbackId:
      var windowAttributes = {
        bounds: {
          width: 400,
          height: 100,
          left: undefined,
          top: undefined
        },
        resizable: false
      };

      /** @type {remoting.ApplicationContextMenu} */
      var that = this;

      /** @param {chrome.app.window.AppWindow} consentWindow */
      var onCreate = function(consentWindow) {
        var onLoad = function() {
          var message = {
            method: 'init',
            appId: remoting.app.getApplicationId(),
            hostId: that.hostId_,
            connectionStats: JSON.stringify(that.stats_.mostRecent()),
            sessionId: that.clientSession_.getLogger().getSessionId()
          };
          consentWindow.contentWindow.postMessage(message, '*');
        };
        consentWindow.contentWindow.addEventListener('load', onLoad, false);
      };
      chrome.app.window.create(
          'feedback_consent.html', windowAttributes, onCreate);
      break;

    case remoting.ApplicationContextMenu.kShowStatsId:
      this.stats_.show(info.checked);
      break;
  }
};


/** @type {string} */
remoting.ApplicationContextMenu.kSendFeedbackId = 'send-feedback';

/** @type {string} */
remoting.ApplicationContextMenu.kShowStatsId = 'show-stats';