summaryrefslogtreecommitdiff
path: root/chromium/ui/webui/resources/cr_elements/chromeos/network/cr_network_icon.js
blob: a6359a1c92abddf5ca7023cd78f4b9dc4f300f3c (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2015 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 Polymer element for rendering network icons based on ONC
 * state properties.
 */

Polymer({
  is: 'cr-network-icon',

  properties: {
    /**
     * If set, the ONC properties will be used to display the icon. This may
     * either be the complete set of NetworkProperties or the subset of
     * NetworkStateProperties.
     * @type {!CrOnc.NetworkProperties|!CrOnc.NetworkStateProperties|undefined}
     */
    networkState: Object,

    /**
     * If set, the device state for the network type. Otherwise it defaults to
     * null rather than undefined so that it does not block computed bindings.
     * @type {?CrOnc.DeviceStateProperties}
     */
    deviceState: {
      type: Object,
      value: null,
    },

    /**
     * If true, the icon is part of a list of networks and may be displayed
     * differently, e.g. the disconnected image will never be shown for
     * list items.
     */
    isListItem: {
      type: Boolean,
      value: false,
    },
  },

  /**
   * Number of network icons for different cellular or wifi network signal
   * strengths.
   * @private @const
   */
  networkIconCount_: 5,

  /**
   * @return {string} The name of the svg icon image to show.
   * @private
   */
  getIconClass_: function() {
    if (!this.networkState) {
      return '';
    }
    const type = this.networkState.Type;
    if (type == CrOnc.Type.ETHERNET) {
      return 'ethernet';
    }
    if (type == CrOnc.Type.VPN) {
      return 'vpn';
    }

    const prefix = (type == CrOnc.Type.CELLULAR || type == CrOnc.Type.TETHER) ?
        'cellular-' :
        'wifi-';
    if (!this.isListItem && !this.networkState.GUID) {
      const deviceState = this.deviceState;
      if (!deviceState || deviceState.State == 'Enabled' ||
          deviceState.State == 'Enabling') {
        return prefix + 'no-network';
      }
      return prefix + 'off';
    }

    const connectionState = this.networkState.ConnectionState;
    if (connectionState == CrOnc.ConnectionState.CONNECTING) {
      return prefix + 'connecting';
    }

    if (!this.isListItem &&
        (!connectionState ||
         connectionState == CrOnc.ConnectionState.NOT_CONNECTED)) {
      return prefix + 'not-connected';
    }

    const strength = CrOnc.getSignalStrength(this.networkState);
    return prefix + this.strengthToIndex_(strength).toString(10);
  },

  /**
   * @param {number} strength The signal strength from [0 - 100].
   * @return {number} An index from 0 to |this.networkIconCount_ - 1|
   * corresponding to |strength|.
   * @private
   */
  strengthToIndex_: function(strength) {
    if (strength <= 0) {
      return 0;
    }

    if (strength >= 100) {
      return this.networkIconCount_ - 1;
    }

    const zeroBasedIndex =
        Math.trunc((strength - 1) * (this.networkIconCount_ - 1) / 100);
    return zeroBasedIndex + 1;
  },

  /**
   * @return {boolean}
   * @private
   */
  showTechnology_: function() {
    return this.getTechnology_() != '';
  },

  /**
   * @return {string}
   * @private
   */
  getTechnology_: function() {
    const networkState = this.networkState;
    if (!networkState) {
      return '';
    }
    const type = networkState.Type;
    if (type == CrOnc.Type.WI_MAX) {
      return 'network:4g';
    }
    if (type == CrOnc.Type.CELLULAR && networkState.Cellular) {
      const technology =
          this.getTechnologyId_(networkState.Cellular.NetworkTechnology);
      if (technology != '') {
        return 'network:' + technology;
      }
    }
    return '';
  },

  /**
   * @param {string|undefined} networkTechnology
   * @return {string}
   * @private
   */
  getTechnologyId_: function(networkTechnology) {
    switch (networkTechnology) {
      case CrOnc.NetworkTechnology.CDMA1XRTT:
        return 'badge-1x';
      case CrOnc.NetworkTechnology.EDGE:
        return 'badge-edge';
      case CrOnc.NetworkTechnology.EVDO:
        return 'badge-evdo';
      case CrOnc.NetworkTechnology.GPRS:
      case CrOnc.NetworkTechnology.GSM:
        return 'badge-gsm';
      case CrOnc.NetworkTechnology.HSPA:
        return 'badge-hspa';
      case CrOnc.NetworkTechnology.HSPA_PLUS:
        return 'badge-hspa-plus';
      case CrOnc.NetworkTechnology.LTE:
        return 'badge-lte';
      case CrOnc.NetworkTechnology.LTE_ADVANCED:
        return 'badge-lte-advanced';
      case CrOnc.NetworkTechnology.UMTS:
        return 'badge-3g';
    }
    return '';
  },

  /**
   * @return {boolean}
   * @private
   */
  showSecure_: function() {
    const networkState = this.networkState;
    if (!this.networkState) {
      return false;
    }
    if (networkState.Type != CrOnc.Type.WI_FI || !networkState.WiFi) {
      return false;
    }
    if (!this.isListItem &&
        networkState.ConnectionState == CrOnc.ConnectionState.NOT_CONNECTED) {
      return false;
    }
    const security = CrOnc.getStateOrActiveString(networkState.WiFi.Security);
    return !!security && security != 'None';
  },
});