summaryrefslogtreecommitdiff
path: root/chromium/ui/webui/resources/cr_components/chromeos/network/cellular_utils.js
blob: 27ab8f4fe33c9df2da23b73163b76cb13e13a37a (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
// Copyright 2021 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.

// clang-format off
// #import {MojoInterfaceProviderImpl} from 'chrome://resources/cr_components/chromeos/network/mojo_interface_provider.m.js';
// #import {OncMojo} from 'chrome://resources/cr_components/chromeos/network/onc_mojo.m.js';
// clang-format on

/**
 * Checks if the device has a currently active pSIM network.
 * @return {!Promise<boolean>}
 */
/* #export */ function hasActivePSimNetwork() {
  const mojom = chromeos.networkConfig.mojom;
  const networkConfig = network_config.MojoInterfaceProviderImpl.getInstance()
                            .getMojoServiceRemote();
  return networkConfig
      .getNetworkStateList({
        filter: mojom.FilterType.kActive,
        networkType: mojom.NetworkType.kCellular,
        limit: mojom.NO_LIMIT,
      })
      .then((response) => {
        // Filter out non-connected networks and check the remaining if they are
        // pSIM.
        return Promise.all(response.result
                               .filter(network => {
                                 return network.connectionState !==
                                     mojom.ConnectionStateType.kNotConnected;
                               })
                               .map(networkIsPSim_));
      })
      .then((networkIsPSimResults) => {
        return networkIsPSimResults.some((isPSimNetwork) => isPSimNetwork);
      });
}

/**
 * Returns whether a network is a pSIM network or not.
 * @private
 * @param {!chromeos.networkConfig.mojom.NetworkStateProperties} network
 * @return {!Promise<boolean>}
 */
function networkIsPSim_(network) {
  const networkConfig = network_config.MojoInterfaceProviderImpl.getInstance()
                            .getMojoServiceRemote();
  return networkConfig.getManagedProperties(network.guid).then((response) => {
    return !response.result.typeProperties.cellular.eid;
  });
}

/**
 * Returns number of phyical SIM and eSIM slots on the current device
 * @param {!chromeos.networkConfig.mojom.DeviceStateProperties|undefined}
 *     deviceState
 * @return {!{pSimSlots: number, eSimSlots: number}}
 */
/* #export */ function getSimSlotCount(deviceState) {
  let pSimSlots = 0;
  let eSimSlots = 0;

  if (!deviceState || !deviceState.simInfos) {
    return {pSimSlots, eSimSlots};
  }

  for (const simInfo of deviceState.simInfos) {
    if (simInfo.eid) {
      eSimSlots++;
      continue;
    }
    pSimSlots++;
  }

  return {pSimSlots, eSimSlots};
}

/**
 * Checks if the device is currently connected to a WiFi, Ethernet or Tether
 * network.
 * @return {!Promise<boolean>}
 */
/* #export */ function isConnectedToNonCellularNetwork() {
  const mojom = chromeos.networkConfig.mojom;
  const networkConfig = network_config.MojoInterfaceProviderImpl.getInstance()
                            .getMojoServiceRemote();
  return networkConfig
      .getNetworkStateList({
        filter: mojom.FilterType.kActive,
        networkType: mojom.NetworkType.kAll,
        limit: mojom.NO_LIMIT,
      })
      .then((response) => {
        // Filter for connected non-cellular networks.
        return response.result.some(network => {
          return network.connectionState ===
              mojom.ConnectionStateType.kOnline &&
              network.type !== mojom.NetworkType.kCellular;
        });
      });
}

/**
 * Determines if the current network is on the active sim slot.
 * TODO(cvandermerwe): Use this function in network-siminfo.
 * @param {?chromeos.networkConfig.mojom.NetworkStateProperties} networkState
 * @param {?chromeos.networkConfig.mojom.DeviceStateProperties} deviceState
 */
/* #export */ function isActiveSim(networkState, deviceState) {
  const mojom = chromeos.networkConfig.mojom;
  if (!networkState || networkState.type !== mojom.NetworkType.kCellular) {
    return false;
  }

  const iccid = networkState.typeState.cellular.iccid;
  if (!iccid || !deviceState || !deviceState.simInfos) {
    return false;
  }
  const isActiveSim = deviceState.simInfos.find(simInfo => {
    return simInfo.iccid === iccid && simInfo.isPrimary;
  });
  return !!isActiveSim;
}