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
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310

ash / webui / diagnostics_ui / resources / diagnostics_utils.ts [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {loadTimeData} from 'chrome://resources/ash/common/load_time_data.m.js';
import {assert, assertNotReached} from 'chrome://resources/js/assert.js';

import {NavigationView, RoutineProperties} from './diagnostics_types.js';
import {LockType, Network, NetworkState, NetworkType} from './network_health_provider.mojom-webui.js';
import {RoutineGroup} from './routine_group.js';
import {RoutineType} from './system_routine_controller.mojom-webui.js';

/**
 * Converts a KiB storage value to GiB and returns a fixed-point string
 * to the desired number of decimal places.
 */
export function convertKibToGibDecimalString(
    value: number, numDecimalPlaces: number): string {
  return (value / 2 ** 20).toFixed(numDecimalPlaces);
}

/** Converts a KiB storage value to MiB. */
export function convertKibToMib(value: number): number {
  // 1024 KiB is 1 MiB.
  return value / (2 ** 10);
}

/** Returns an icon from the diagnostics icon set. */
export function getDiagnosticsIcon(id: string): string {
  return `diagnostics:${id}`;
}

/** Returns an icon from the navigation icon set. */
export function getNavigationIcon(id: string): string {
  return `navigation-selector:${id}`;
}

/**
 * Converts ID into matching navigation view. ID matches the 'id' field provided
 * to the navigation-view-panel {SelectorItem} array.
 */
export function getNavigationViewForPageId(id: string): NavigationView {
  switch (id) {
    case 'system':
      return NavigationView.SYSTEM;
    case 'connectivity':
      return NavigationView.CONNECTIVITY;
    case 'input':
      return NavigationView.INPUT;
    default:
      assertNotReached();
  }
}

export function getNetworkType(type: NetworkType): string {
  switch (type) {
    case NetworkType.kWiFi:
      return loadTimeData.getString('wifiLabel');
    case NetworkType.kEthernet:
      return loadTimeData.getString('ethernetLabel');
    case NetworkType.kCellular:
      return loadTimeData.getString('cellularLabel');
  }
  assertNotReached();
}

export function getNetworkState(state: NetworkState): string {
  switch (state) {
    case NetworkState.kOnline:
      return loadTimeData.getString('networkStateOnlineText');
    case NetworkState.kConnected:
      return loadTimeData.getString('networkStateConnectedText');
    case NetworkState.kPortal:
      return loadTimeData.getString('networkStatePortalText');
    case NetworkState.kConnecting:
      return loadTimeData.getString('networkStateConnectingText');
    case NetworkState.kNotConnected:
      return loadTimeData.getString('networkStateNotConnectedText');
    case NetworkState.kDisabled:
      return loadTimeData.getString('networkStateDisabledText');
  }
}

export function getLockType(lockType: LockType): string {
  switch (lockType) {
    case LockType.kSimPuk:
      return 'sim-puk';
    case LockType.kSimPin:
      return 'sim-pin';
    case LockType.kNetworkPin:
      return 'network-pin';
    case LockType.kNone:
      return '';
  }
}

/**
 * @param blocking If a routine is blocking, the remaining routines
 * will be skipped. For non-blocking routines, we'll continue running them
 * and display a 'WARNING' badge to signal that a non-blocking routine failed.
 */
export function createRoutine(
    routine: RoutineType, blocking: boolean): RoutineProperties {
  return {routine, blocking};
}

export function getRoutineGroups(type: NetworkType): RoutineGroup[] {
  const localNetworkGroup = new RoutineGroup(
      [
        createRoutine(RoutineType.kGatewayCanBePinged, false),
        createRoutine(RoutineType.kLanConnectivity, true),
        createRoutine(RoutineType.kArcPing, false),
      ],
      'localNetworkGroupLabel');

  const nameResolutionGroup = new RoutineGroup(
      [
        createRoutine(RoutineType.kDnsResolverPresent, true),
        createRoutine(RoutineType.kDnsResolution, true),
        createRoutine(RoutineType.kDnsLatency, true),
        createRoutine(RoutineType.kArcDnsResolution, false),
      ],
      'nameResolutionGroupLabel');

  const wifiGroup = new RoutineGroup(
      [
        createRoutine(RoutineType.kSignalStrength, false),
        createRoutine(RoutineType.kCaptivePortal, false),
        createRoutine(RoutineType.kHasSecureWiFiConnection, false),
      ],
      'wifiGroupLabel');
  const internetConnectivityGroup = new RoutineGroup(
      [
        createRoutine(RoutineType.kHttpsFirewall, true),
        createRoutine(RoutineType.kHttpFirewall, true),
        createRoutine(RoutineType.kHttpsLatency, true),
        createRoutine(RoutineType.kArcHttp, false),
      ],
      'internetConnectivityGroupLabel');

  const groupsToAdd = type === NetworkType.kWiFi ?
      [wifiGroup, internetConnectivityGroup] :
      [internetConnectivityGroup];

  const networkRoutineGroups = [
    localNetworkGroup,
    nameResolutionGroup,
  ];

  return networkRoutineGroups.concat(groupsToAdd);
}

export function getSubnetMaskFromRoutingPrefix(prefix: number): string {
  // TODO(wenyu): Handle IPv6 type with prefix range of [1, 128].

  assert(prefix >= 0 && prefix <= 32);

  // A routing prefix can not be 0. Zero indicates an unset value.
  if (prefix === 0) {
    return '';
  }

  const zeroes = 32 - prefix;
  // Note: 0xffffffff is 32 bits, all set to 1.
  // Use << to knock off |zeroes| number of bits and then use that same number
  // to replace those bits with zeroes.
  // Ex: 11111111 11111111 11111111 11111111 becomes
  // 11111111 11111111 11111111 00000000.
  let mask = (0xffffffff >> zeroes) << zeroes;

  const pieces = new Array(4);
  for (let i = 0; i < 4; i++) {
    // Note: & is binary and. 0xff is 8 ones "11111111".
    // Use & with the mask to select the bits from the other number.
    // Repeat to split the 32 bit number into four 8-bit numbers
    pieces[3 - i] = mask & 0xff;
    mask = mask >> 8;
  }

  return pieces.join('.');
}


export function formatMacAddress(macAddress: string): string {
  return `${loadTimeData.getString('macAddressLabel')}: ${macAddress}`;
}

/**
 * Resolves a networking routine type to its corresponding localized failure
 * message.
 */
export function getRoutineFailureMessage(routineType: RoutineType): string {
  switch (routineType) {
    case RoutineType.kCaptivePortal:
      return loadTimeData.getString('captivePortalFailedText');
    case RoutineType.kDnsLatency:
      return loadTimeData.getString('dnsLatencyFailedText');
    case RoutineType.kDnsResolution:
      return loadTimeData.getString('dnsResolutionFailedText');
    case RoutineType.kDnsResolverPresent:
      return loadTimeData.getString('dnsResolverPresentFailedText');
    case RoutineType.kGatewayCanBePinged:
      return loadTimeData.getString('gatewayCanBePingedFailedText');
    case RoutineType.kHasSecureWiFiConnection:
      return loadTimeData.getString('hasSecureWiFiConnectionFailedText');
    case RoutineType.kHttpFirewall:
      return loadTimeData.getString('httpFirewallFailedText');
    case RoutineType.kHttpsFirewall:
      return loadTimeData.getString('httpsFirewallFailedText');
    case RoutineType.kHttpsLatency:
      return loadTimeData.getString('httpsLatencyFailedText');
    case RoutineType.kLanConnectivity:
      return loadTimeData.getString('lanConnectivityFailedText');
    case RoutineType.kSignalStrength:
      return loadTimeData.getString('signalStrengthFailedText');
    case RoutineType.kArcHttp:
      return loadTimeData.getString('arcHttpFailedText');
    case RoutineType.kArcPing:
      return loadTimeData.getString('arcPingFailedText');
    case RoutineType.kArcDnsResolution:
      return loadTimeData.getString('arcDnsResolutionFailedText');
    case RoutineType.kBatteryCharge:
    case RoutineType.kBatteryDischarge:
    case RoutineType.kCpuCache:
    case RoutineType.kCpuStress:
    case RoutineType.kCpuFloatingPoint:
    case RoutineType.kCpuPrime:
    case RoutineType.kMemory:
    default:
      // Values should always be found in the enum.
      assertNotReached();
  }
}

export function isConnectedOrOnline(state: NetworkState): boolean {
  switch (state) {
    case NetworkState.kOnline:
    case NetworkState.kConnected:
    case NetworkState.kConnecting:
      return true;
    default:
      return false;
  }
}

export function isNetworkMissingNameServers(network: Network): boolean {
  return !network.ipConfig || !network.ipConfig.nameServers ||
      network.ipConfig.nameServers.length === 0;
}

/** Removes '0.0.0.0' from list of name servers. */
export function filterNameServers(network: Network): void {
  if (network?.ipConfig?.nameServers) {
    network.ipConfig.nameServers =
        network.ipConfig.nameServers.filter((n: string) => n !== '0.0.0.0');
  }
}

/*
 * If true network state text is appended to network and connectivity card
 * title.
 * @type {boolean}
 */
let displayStateInTitle = false;

/**
 * Test helper function to allow change if state text is appended to the card
 * title.
 */
export function setDisplayStateInTitleForTesting(state: boolean): void {
  displayStateInTitle = state;
}

/**
 * Build common string for network title for network and connectivity card.
 * Current network state is included for debugging when `displayStateInTitle`
 * is true.
 */
export function getNetworkCardTitle(
    networkType: string, networkState: string): string {
  let titleForCard = `${networkType}`;
  if (displayStateInTitle) {
    titleForCard = `${titleForCard} (${networkState})`;
  }

  return `${titleForCard}`;
}

export function getSignalStrength(value: number): string {
  assert(typeof value === 'number');
  assert(value >= 0 && value <= 100);

  if (value <= 1) {
    return '';
  }

  if (value <= 25) {
    return loadTimeData.getStringF('signalStrength_Weak', value);
  }

  if (value <= 50) {
    return loadTimeData.getStringF('signalStrength_Average', value);
  }

  if (value <= 75) {
    return loadTimeData.getStringF('signalStrength_Good', value);
  }

  return loadTimeData.getStringF('signalStrength_Excellent', value);
}