File: network_state.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (797 lines) | stat: -rw-r--r-- 27,255 bytes parent folder | download | duplicates (7)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/network/network_state.h"

#include <stddef.h>

#include <memory>
#include <optional>
#include <type_traits>
#include <utility>

#include "ash/constants/ash_features.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chromeos/ash/components/network/cellular_utils.h"
#include "chromeos/ash/components/network/device_state.h"
#include "chromeos/ash/components/network/network_config.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/components/network/network_profile_handler.h"
#include "chromeos/ash/components/network/network_type_pattern.h"
#include "chromeos/ash/components/network/network_ui_data.h"
#include "chromeos/ash/components/network/network_util.h"
#include "chromeos/ash/components/network/shill_property_util.h"
#include "chromeos/ash/components/network/tether_constants.h"
#include "chromeos/components/onc/onc_utils.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "net/http/http_status_code.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {

namespace {

namespace network_config {
namespace mojom = ::chromeos::network_config::mojom;
}

// TODO(tbarzic): Add payment portal method values to shill/dbus-constants.
constexpr char kPaymentPortalMethodPost[] = "POST";

// |dict| may be an empty value, in which case return an empty string.
std::string GetStringFromDictionary(
    const std::optional<base::Value::Dict>& dict,
    const char* key) {
  const std::string* stringp =
      dict.has_value() ? dict->FindString(key) : nullptr;
  return stringp ? *stringp : std::string();
}

bool IsValidConnectionState(const std::string& connection_state) {
  return connection_state == shill::kStateIdle ||
         connection_state == shill::kStateAssociation ||
         connection_state == shill::kStateConfiguration ||
         connection_state == shill::kStateReady ||
         connection_state == shill::kStateNoConnectivity ||
         connection_state == shill::kStateRedirectFound ||
         connection_state == shill::kStatePortalSuspected ||
         connection_state == shill::kStateOnline ||
         connection_state == shill::kStateFailure ||
         connection_state == shill::kStateDisconnecting;
}

}  // namespace

NetworkState::NetworkState(const std::string& path)
    : ManagedState(MANAGED_TYPE_NETWORK, path) {}

NetworkState::~NetworkState() = default;

bool NetworkState::PropertyChanged(const std::string& key,
                                   const base::Value& value) {
  // Keep care that these properties are the same as in |GetProperties|.
  if (ManagedStatePropertyChanged(key, value)) {
    return true;
  }
  if (key == shill::kSignalStrengthProperty) {
    int signal_strength = signal_strength_;
    if (!GetIntegerValue(key, value, &signal_strength)) {
      return false;
    }
    if (signal_strength_ > 0 && abs(signal_strength - signal_strength_) <
                                    kSignalStrengthChangeThreshold) {
      return false;
    }
    signal_strength_ = signal_strength;
    return true;
  } else if (key == shill::kWifiSignalStrengthRssiProperty) {
    int rssi = rssi_;
    if (!GetIntegerValue(key, value, &rssi)) {
      return false;
    }
    if (rssi == rssi_) {
      return false;
    }
    rssi_ = rssi;
    return true;
  } else if (key == shill::kStateProperty) {
    std::string connection_state;
    if (!GetStringValue(key, value, &connection_state)) {
      return false;
    }
    SetConnectionState(connection_state);
    return true;
  } else if (key == shill::kVisibleProperty) {
    return GetBooleanValue(key, value, &visible_);
  } else if (key == shill::kConnectableProperty) {
    return GetBooleanValue(key, value, &connectable_);
  } else if (key == shill::kErrorProperty) {
    std::string error;
    if (!GetStringValue(key, value, &error)) {
      return false;
    }
    if (ErrorIsValid(error)) {
      last_error_ = error;
    }
    return true;
  } else if (key == shill::kWifiFrequency) {
    return GetIntegerValue(key, value, &frequency_);
  } else if (key == shill::kActivationTypeProperty) {
    return GetStringValue(key, value, &activation_type_);
  } else if (key == shill::kActivationStateProperty) {
    return GetStringValue(key, value, &activation_state_);
  } else if (key == shill::kRoamingStateProperty) {
    return GetStringValue(key, value, &roaming_);
  } else if (key == shill::kCellularAllowRoamingProperty) {
    return GetBooleanValue(key, value, &allow_roaming_);
  } else if (key == shill::kPaymentPortalProperty) {
    const base::Value::Dict* value_dict = value.GetIfDict();
    if (!value_dict) {
      return false;
    }
    const std::string* portal_url_value =
        value_dict->FindString(shill::kPaymentPortalURL);
    if (!portal_url_value) {
      return false;
    }
    payment_url_ = *portal_url_value;
    // If payment portal uses post method, set up post data.
    const std::string* portal_method_value =
        value_dict->FindString(shill::kPaymentPortalMethod);
    payment_method_ =
        portal_method_value ? *portal_method_value : std::string();

    const std::string* portal_post_data_value =
        value_dict->FindString(shill::kPaymentPortalPostData);
    if (payment_method_ == kPaymentPortalMethodPost && portal_post_data_value) {
      payment_post_data_ = *portal_post_data_value;
    }
    return true;
  } else if (key == shill::kSecurityClassProperty) {
    return GetStringValue(key, value, &security_class_);
  } else if (key == shill::kEapMethodProperty) {
    return GetStringValue(key, value, &eap_method_);
  } else if (key == shill::kEapKeyMgmtProperty) {
    return GetStringValue(key, value, &eap_key_mgmt_);
  } else if (key == shill::kNetworkTechnologyProperty) {
    return GetStringValue(key, value, &network_technology_);
  } else if (key == shill::kDeviceProperty) {
    return GetStringValue(key, value, &device_path_);
  } else if (key == shill::kGuidProperty) {
    return GetStringValue(key, value, &guid_);
  } else if (key == shill::kProfileProperty) {
    return GetStringValue(key, value, &profile_path_);
  } else if (key == shill::kWifiHexSsid) {
    std::string ssid_hex;
    if (!GetStringValue(key, value, &ssid_hex)) {
      return false;
    }
    raw_ssid_.clear();
    return base::HexStringToBytes(ssid_hex, &raw_ssid_);
  } else if (key == shill::kWifiBSsid) {
    return GetStringValue(key, value, &bssid_);
  } else if (key == shill::kPriorityProperty) {
    return GetIntegerValue(key, value, &priority_);
  } else if (key == shill::kWifiHiddenSsid) {
    return GetBooleanValue(key, value, &hidden_ssid_);
  } else if (key == shill::kPasspointIDProperty) {
    return GetStringValue(key, value, &passpoint_id_);
  } else if (key == shill::kMeteredProperty) {
    return GetBooleanValue(key, value, &metered_);
  } else if (key == shill::kOutOfCreditsProperty) {
    return GetBooleanValue(key, value, &cellular_out_of_credits_);
  } else if (key == shill::kIccidProperty) {
    return GetStringValue(key, value, &iccid_);
  } else if (key == shill::kEidProperty) {
    return GetStringValue(key, value, &eid_);
  } else if (key == shill::kProxyConfigProperty) {
    const std::string* proxy_config_str = value.GetIfString();
    if (!proxy_config_str) {
      NET_LOG(ERROR) << "Failed to parse " << path() << "." << key;
      return false;
    }

    if ((*proxy_config_str).empty()) {
      proxy_config_.reset();
      return true;
    }
    std::optional<base::Value::Dict> proxy_config =
        chromeos::onc::ReadDictionaryFromJson(*proxy_config_str);
    if (!proxy_config.has_value()) {
      NET_LOG(ERROR) << "Failed to parse " << path() << "." << key;
      proxy_config_.reset();
    } else {
      proxy_config_ = std::move(proxy_config.value());
    }
    return true;
  } else if (key == shill::kProviderProperty) {
    const std::string* vpn_provider_type =
        value.is_dict() ? value.GetDict().FindString(shill::kTypeProperty)
                        : nullptr;
    if (!vpn_provider_type) {
      NET_LOG(ERROR) << "Failed to parse " << path() << "." << key;
      return false;
    }
    std::string vpn_provider_id;
    if (*vpn_provider_type == shill::kProviderThirdPartyVpn ||
        *vpn_provider_type == shill::kProviderArcVpn) {
      // If the network uses a third-party or Arc VPN provider,
      // |shill::kHostProperty| contains the extension ID or Arc package name.
      const std::string* host =
          value.GetDict().FindString(shill::kHostProperty);
      if (!host) {
        NET_LOG(ERROR) << "Failed to parse " << path() << "." << key;
        return false;
      }
      vpn_provider_id = *host;
    }
    SetVpnProvider(vpn_provider_id, *vpn_provider_type);
    return true;
  } else if (key == shill::kUIDataProperty) {
    std::unique_ptr<NetworkUIData> ui_data =
        shill_property_util::GetUIDataFromValue(value);
    if (!ui_data) {
      return false;
    }
    onc_source_ = ui_data->onc_source();
    return true;
  } else if (key == shill::kProbeUrlProperty) {
    std::string probe_url_string;
    if (!GetStringValue(key, value, &probe_url_string)) {
      return false;
    }
    probe_url_ = GURL(probe_url_string);
    return true;
  } else if (key == shill::kUplinkSpeedPropertyKbps) {
    uint32_t max_uplink_speed_kbps = 0;
    if (!GetUInt32Value(key, value, &max_uplink_speed_kbps)) {
      return false;
    }
    if (max_uplink_speed_kbps_.has_value() &&
        max_uplink_speed_kbps == max_uplink_speed_kbps_.value()) {
      return false;
    }
    max_uplink_speed_kbps_ = max_uplink_speed_kbps;
    return true;
  } else if (key == shill::kDownlinkSpeedPropertyKbps) {
    uint32_t max_downlink_speed_kbps = 0;
    if (!GetUInt32Value(key, value, &max_downlink_speed_kbps)) {
      return false;
    }
    if (max_downlink_speed_kbps_.has_value() &&
        max_downlink_speed_kbps == max_downlink_speed_kbps_.value()) {
      return false;
    }
    max_downlink_speed_kbps_ = max_downlink_speed_kbps;
    return true;
  } else if (key == shill::kNetworkConfigProperty) {
    network_config_ = NetworkConfig::ParseFromServicePropertyValue(value);
    return true;
  }
  return false;
}

bool NetworkState::InitialPropertiesReceived(
    const base::Value::Dict& properties) {
  NET_LOG(EVENT) << "InitialPropertiesReceived: " << NetworkId(this)
                 << " State: " << connection_state_ << " Visible: " << visible_;
  if (!properties.contains(shill::kTypeProperty)) {
    NET_LOG(ERROR) << "NetworkState has no type: " << NetworkId(this);
    return false;
  }

  // By convention, all visible WiFi networks have a SignalStrength > 0.
  if (type() == shill::kTypeWifi && visible() && signal_strength_ <= 0) {
    signal_strength_ = 1;
  }

  // Any change to connection state or portal properties will trigger a complete
  // property update, so we update captive portal state here.
  UpdateCaptivePortalState(properties);

  // Ensure that the network has a valid name.
  return UpdateName(properties);
}

void NetworkState::GetStateProperties(base::Value::Dict* dictionary) const {
  ManagedState::GetStateProperties(dictionary);

  // Properties shared by all types.
  dictionary->Set(shill::kGuidProperty, guid());
  dictionary->Set(shill::kSecurityClassProperty, security_class());
  dictionary->Set(shill::kProfileProperty, profile_path());
  dictionary->Set(shill::kPriorityProperty, priority_);

  if (visible()) {
    dictionary->Set(shill::kStateProperty, connection_state());
  }
  if (!device_path().empty()) {
    dictionary->Set(shill::kDeviceProperty, device_path());
  }

  // VPN properties.
  if (NetworkTypePattern::VPN().MatchesType(type()) && vpn_provider()) {
    // Shill sends VPN provider properties in a nested dictionary. |dictionary|
    // must replicate that nested structure.
    std::string provider_type = vpn_provider()->type;
    auto provider_property =
        base::Value::Dict().Set(shill::kTypeProperty, provider_type);
    if (provider_type == shill::kProviderThirdPartyVpn ||
        provider_type == shill::kProviderArcVpn) {
      provider_property.Set(shill::kHostProperty, vpn_provider()->id);
    }
    dictionary->Set(shill::kProviderProperty, std::move(provider_property));
  }

  // Tether properties
  if (NetworkTypePattern::Tether().MatchesType(type())) {
    dictionary->Set(kTetherBatteryPercentage, battery_percentage());
    dictionary->Set(kTetherCarrier, tether_carrier());
    dictionary->Set(kTetherHasConnectedToHost, tether_has_connected_to_host());
    dictionary->Set(kTetherSignalStrength, signal_strength());

    // All Tether networks are connectable.
    dictionary->Set(shill::kConnectableProperty, connectable());

    // Tether networks do not share some of the wireless/mobile properties added
    // below; exit early to avoid having these properties applied.
    return;
  }

  // Wireless properties
  if (!NetworkTypePattern::Wireless().MatchesType(type())) {
    return;
  }

  if (visible()) {
    dictionary->Set(shill::kConnectableProperty, connectable());
    dictionary->Set(shill::kSignalStrengthProperty, signal_strength());
  }

  // Wifi properties
  if (NetworkTypePattern::WiFi().MatchesType(type())) {
    dictionary->Set(shill::kWifiBSsid, bssid_);
    dictionary->Set(shill::kEapMethodProperty, eap_method());
    dictionary->Set(shill::kWifiFrequency, frequency_);
    dictionary->Set(shill::kWifiHexSsid, GetHexSsid());
  }

  // Mobile properties
  if (NetworkTypePattern::Mobile().MatchesType(type())) {
    dictionary->Set(shill::kNetworkTechnologyProperty, network_technology());
    dictionary->Set(shill::kActivationStateProperty, activation_state());
    dictionary->Set(shill::kRoamingStateProperty, roaming_);
    dictionary->Set(shill::kOutOfCreditsProperty, cellular_out_of_credits());
  }

  // Cellular properties
  if (NetworkTypePattern::Cellular().MatchesType(type())) {
    dictionary->Set(shill::kIccidProperty, iccid());
    dictionary->Set(shill::kEidProperty, eid());
  }
}

bool NetworkState::IsActive() const {
  return IsConnectingOrConnected() ||
         activation_state() == shill::kActivationStateActivating;
}

void NetworkState::IPConfigPropertiesChanged(
    const base::Value::Dict& properties) {
  if (properties.empty()) {
    ipv4_config_.reset();
    return;
  }
  ipv4_config_ = properties.Clone();
}

std::string NetworkState::GetIpAddress() const {
  return GetStringFromDictionary(ipv4_config_, shill::kAddressProperty);
}

std::string NetworkState::GetGateway() const {
  return GetStringFromDictionary(ipv4_config_, shill::kGatewayProperty);
}

GURL NetworkState::GetWebProxyAutoDiscoveryUrl() const {
  std::string url = GetStringFromDictionary(
      ipv4_config_, shill::kWebProxyAutoDiscoveryUrlProperty);
  if (url.empty()) {
    return GURL();
  }
  GURL gurl(url);
  if (!gurl.is_valid()) {
    NET_LOG(ERROR) << "Invalid WebProxyAutoDiscoveryUrl: " << NetworkId(this)
                   << ": " << url;
    return GURL();
  }
  return gurl;
}

std::string NetworkState::GetVpnProviderType() const {
  return vpn_provider_ ? vpn_provider_->type : std::string();
}

bool NetworkState::RequiresActivation() const {
  return type() == shill::kTypeCellular &&
         (activation_state() == shill::kActivationStateNotActivated ||
          activation_state() == shill::kActivationStatePartiallyActivated);
}

bool NetworkState::SecurityRequiresPassphraseOnly() const {
  return type() == shill::kTypeWifi &&
         (security_class_ == shill::kSecurityClassPsk ||
          security_class_ == shill::kSecurityClassWep);
}

const std::string& NetworkState::GetError() const {
  return last_error_;
}

void NetworkState::ClearError() {
  last_error_.clear();
}

std::string NetworkState::connection_state() const {
  if (!visible()) {
    return shill::kStateIdle;
  }

  return connection_state_;
}

void NetworkState::SetConnectionState(const std::string& connection_state) {
  DCHECK(IsValidConnectionState(connection_state)) << connection_state;

  if (connection_state == connection_state_) {
    return;
  }
  const std::string prev_connection_state = connection_state_;
  connection_state_ = connection_state;
  if (StateIsConnected(connection_state_) ||
      StateIsConnecting(prev_connection_state)) {
    // If connected or previously connecting, clear |connect_requested_|.
    connect_requested_ = false;
  } else if (StateIsConnected(prev_connection_state) &&
             StateIsConnecting(connection_state_)) {
    // If transitioning from a connected state to a connecting state, set
    // |connect_requested_| so that the UI knows the connecting state is
    // important (i.e. not a normal auto connect).
    connect_requested_ = true;
  }

  // TODO(b/336931625): Polish the relationship between |portal_state_|
  // and |connection_state_|.
  if (portal_state_ == PortalState::kUnknown &&
      connection_state_ == shill::kStateOnline) {
    portal_state_ = PortalState::kOnline;
  }
}

bool NetworkState::IsManagedByPolicy() const {
  return onc_source_ == ::onc::ONCSource::ONC_SOURCE_DEVICE_POLICY ||
         onc_source_ == ::onc::ONCSource::ONC_SOURCE_USER_POLICY;
}

bool NetworkState::IndicateRoaming() const {
  return type() == shill::kTypeCellular &&
         roaming_ == shill::kRoamingStateRoaming && !provider_requires_roaming_;
}

bool NetworkState::IsDynamicWep() const {
  return security_class_ == shill::kSecurityClassWep &&
         eap_key_mgmt_ == shill::kKeyManagementIEEE8021X;
}

bool NetworkState::IsConnectedState() const {
  return visible() && StateIsConnected(connection_state_);
}

bool NetworkState::IsConnectingState() const {
  return visible() &&
         (connect_requested_ || StateIsConnecting(connection_state_));
}

bool NetworkState::IsConnectingOrConnected() const {
  return visible() &&
         (connect_requested_ || StateIsConnecting(connection_state_) ||
          StateIsConnected(connection_state_));
}

bool NetworkState::IsOnline() const {
  return connection_state() == shill::kStateOnline;
}

bool NetworkState::IsInProfile() const {
  // kTypeEthernetEap is always saved. We need this check because it does
  // not show up in the visible list, but its properties may not be available
  // when it first shows up in ServiceCompleteList. See crbug.com/355117.
  return !profile_path_.empty() || type() == shill::kTypeEthernetEap;
}

bool NetworkState::IsNonProfileType() const {
  return type() == kTypeTether || IsNonShillCellularNetwork();
}

bool NetworkState::IsPrivate() const {
  return !profile_path_.empty() &&
         profile_path_ != NetworkProfileHandler::GetSharedProfilePath();
}

bool NetworkState::IsNonShillCellularNetwork() const {
  return type() == shill::kTypeCellular &&
         cellular_utils::IsStubCellularServicePath(path());
}

bool NetworkState::IsSecure() const {
  return !security_class_.empty() &&
         security_class_ != shill::kSecurityClassNone;
}

std::string NetworkState::GetHexSsid() const {
  return base::HexEncode(raw_ssid());
}

std::string NetworkState::GetDnsServersAsString() const {
  const base::Value::List* list =
      ipv4_config_.has_value()
          ? ipv4_config_->FindList(shill::kNameServersProperty)
          : nullptr;
  if (!list) {
    return std::string();
  }
  std::string result;
  for (const auto& v : *list) {
    if (!result.empty()) {
      result += ",";
    }
    result += v.GetString();
  }
  return result;
}

std::string NetworkState::GetNetmask() const {
  int prefixlen = ipv4_config_->FindInt(shill::kPrefixlenProperty).value_or(-1);
  return network_util::PrefixLengthToNetmask(prefixlen);
}

std::string NetworkState::GetSpecifier() const {
  if (!update_received()) {
    NET_LOG(ERROR) << "GetSpecifier called before update: " << NetworkId(this);
    return std::string();
  }
  if (type() == shill::kTypeWifi) {
    return name() + "_" + security_class_;
  }
  if (type() == shill::kTypeCellular && !iccid().empty()) {
    return iccid();
  }
  if (!name().empty()) {
    return type() + "_" + name();
  }
  return type();  // For unnamed networks, i.e. Ethernet.
}

void NetworkState::SetGuid(const std::string& guid) {
  guid_ = guid;
}

network_config::mojom::ActivationStateType
NetworkState::GetMojoActivationState() const {
  using network_config::mojom::ActivationStateType;
  if (activation_state_.empty()) {
    return ActivationStateType::kUnknown;
  }
  if (activation_state_ == shill::kActivationStateActivated) {
    return ActivationStateType::kActivated;
  }
  if (activation_state_ == shill::kActivationStateActivating) {
    return ActivationStateType::kActivating;
  }
  if (activation_state_ == shill::kActivationStateNotActivated) {
    return ActivationStateType::kNotActivated;
  }
  if (activation_state_ == shill::kActivationStatePartiallyActivated) {
    return ActivationStateType::kPartiallyActivated;
  }
  NET_LOG(ERROR) << "Unexpected shill activation state: " << activation_state_;
  return ActivationStateType::kUnknown;
}

network_config::mojom::SecurityType NetworkState::GetMojoSecurity() const {
  using network_config::mojom::SecurityType;
  if (!IsSecure()) {
    return SecurityType::kNone;
  }
  if (IsDynamicWep()) {
    return SecurityType::kWep8021x;
  }

  if (security_class_ == shill::kSecurityClassWep) {
    return SecurityType::kWepPsk;
  }
  if (security_class_ == shill::kSecurityClassPsk) {
    return SecurityType::kWpaPsk;
  }
  if (security_class_ == shill::kSecurityClass8021x) {
    return SecurityType::kWpaEap;
  }
  NET_LOG(ERROR) << "Unsupported shill security class: " << security_class_;
  return SecurityType::kNone;
}

NetworkState::NetworkTechnologyType NetworkState::GetNetworkTechnologyType()
    const {
  const std::string& network_type = type();
  if (network_type == shill::kTypeCellular) {
    return NetworkTechnologyType::kCellular;
  }
  if (network_type == shill::kTypeEthernet) {
    return NetworkTechnologyType::kEthernet;
  }
  if (network_type == shill::kTypeEthernetEap) {
    return NetworkTechnologyType::kEthernet;
  }
  if (network_type == kTypeTether) {
    return NetworkTechnologyType::kTether;
  }
  if (network_type == shill::kTypeVPN) {
    return NetworkTechnologyType::kVPN;
  }
  if (network_type == shill::kTypeWifi) {
    return NetworkTechnologyType::kWiFi;
  }
  NOTREACHED() << "Unknown network type: " << network_type;
}

// static
bool NetworkState::StateIsConnected(const std::string& connection_state) {
  return (connection_state == shill::kStateReady ||
          connection_state == shill::kStateOnline ||
          StateIsPortalled(connection_state));
}

// static
bool NetworkState::StateIsConnecting(const std::string& connection_state) {
  return (connection_state == shill::kStateAssociation ||
          connection_state == shill::kStateConfiguration);
}

// static
bool NetworkState::StateIsPortalled(const std::string& connection_state) {
  return (connection_state == shill::kStateNoConnectivity ||
          connection_state == shill::kStateRedirectFound ||
          connection_state == shill::kStatePortalSuspected);
}

// static
bool NetworkState::ErrorIsValid(const std::string& error) {
  return !error.empty() && error != shill::kErrorNoFailure;
}

// static
std::unique_ptr<NetworkState> NetworkState::CreateNonShillCellularNetwork(
    const std::string& iccid,
    const std::string& eid,
    const std::string& guid,
    bool is_managed,
    const std::string& cellular_device_path) {
  std::string path = cellular_utils::GenerateStubCellularServicePath(iccid);
  auto new_state = std::make_unique<NetworkState>(path);
  new_state->set_type(shill::kTypeCellular);
  new_state->set_update_received();
  new_state->set_visible(true);
  new_state->device_path_ = cellular_device_path;
  new_state->iccid_ = iccid;
  new_state->eid_ = eid;
  new_state->guid_ = guid;
  if (is_managed) {
    new_state->onc_source_ = ::onc::ONCSource::ONC_SOURCE_DEVICE_POLICY;
  }
  new_state->activation_state_ = shill::kActivationStateActivated;
  return new_state;
}

// Private methods.

bool NetworkState::UpdateName(const base::Value::Dict& properties) {
  std::string updated_name =
      shill_property_util::GetNameFromProperties(path(), properties);
  if (updated_name != name()) {
    set_name(updated_name);
    return true;
  }
  return false;
}

void NetworkState::UpdateCaptivePortalState(
    const base::Value::Dict& properties) {
  if (!IsConnectedState()) {
    // Unconnected networks are in an unknown portal state and should not
    // update histograms.
    portal_state_ = PortalState::kUnknown;
    return;
  }

  if (connection_state_ == shill::kStateNoConnectivity) {
    portal_state_ = PortalState::kNoInternet;
  } else if (connection_state_ == shill::kStateRedirectFound) {
    portal_state_ = PortalState::kPortal;
  } else if (connection_state_ == shill::kStatePortalSuspected) {
    portal_state_ = PortalState::kPortalSuspected;
  } else if (connection_state_ == shill::kStateOnline) {
    portal_state_ = PortalState::kOnline;
  } else {
    NET_LOG(ERROR) << "Unexpected captive portal state for: " << NetworkId(this)
                   << " = " << portal_state_;
    portal_state_ = PortalState::kUnknown;
  }

  base::UmaHistogramEnumeration("Network.CaptivePortalResult", portal_state_);
  if (portal_state_ != PortalState::kOnline) {
    NET_LOG(EVENT) << "Shill captive portal state for: " << NetworkId(this)
                   << " = " << portal_state_;
  }
}

void NetworkState::SetVpnProvider(const std::string& id,
                                  const std::string& type) {
  // |type| is required but |id| is only set for ThirdParty and Arc VPNs.
  if (type.empty()) {
    vpn_provider_ = nullptr;
    return;
  }
  if (!vpn_provider_) {
    vpn_provider_ = std::make_unique<VpnProviderInfo>();
  }
  vpn_provider_->id = id;
  vpn_provider_->type = type;
}

std::ostream& operator<<(std::ostream& out,
                         const NetworkState::PortalState state) {
  using State = NetworkState::PortalState;
  switch (state) {
#define PRINT(s)    \
  case State::k##s: \
    return out << #s;
    PRINT(Unknown)
    PRINT(Online)
    PRINT(PortalSuspected)
    PRINT(Portal)
    PRINT(NoInternet)
#undef PRINT
  }

  return out << "PortalState("
             << static_cast<std::underlying_type_t<State>>(state) << ")";
}

std::ostream& operator<<(std::ostream& out,
                         const NetworkState::NetworkTechnologyType type) {
  using Type = NetworkState::NetworkTechnologyType;
  switch (type) {
#define PRINT(s)   \
  case Type::k##s: \
    return out << #s;
    PRINT(Cellular)
    PRINT(Ethernet)
    PRINT(EthernetEap)
    PRINT(WiFi)
    PRINT(Tether)
    PRINT(VPN)
    PRINT(Unknown)
#undef PRINT
  }

  return out << "NetworkTechnologyType("
             << static_cast<std::underlying_type_t<Type>>(type) << ")";
}

}  // namespace ash