File: network_state_notifier.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (721 lines) | stat: -rw-r--r-- 28,734 bytes parent folder | download | duplicates (6)
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
// 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 "chrome/browser/ui/ash/network/network_state_notifier.h"

#include <string>

#include "ash/constants/ash_features.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/notification_utils.h"
#include "ash/public/cpp/system_tray_client.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/notifications/system_notification_helper.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/components/network/cellular_esim_profile_handler.h"
#include "chromeos/ash/components/network/network_configuration_handler.h"
#include "chromeos/ash/components/network/network_connect.h"
#include "chromeos/ash/components/network/network_connection_handler.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/components/network/network_name_util.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/shill_property_util.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/chromeos/devicetype_utils.h"
#include "ui/chromeos/shill_error.h"
#include "ui/chromeos/strings/grit/ui_chromeos_strings.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/message_center/public/cpp/notification.h"

namespace ash {

namespace {

const int kMinTimeBetweenOutOfCreditsNotifySeconds = 10 * 60;

const char kNotifierNetwork[] = "ash.network";
const char kNotifierNetworkError[] = "ash.network.error";

// Ignore in-progress errors and disconnect errors (which may occur when a new
// connect request occurs while a previous connect is in-progress).
bool ShillErrorIsIgnored(const std::string& shill_error) {
  return shill_error == shill::kErrorResultInProgress ||
         shill_error == shill::kErrorDisconnect;
}

// Returns true if |shill_error| is known to be a configuration error.
bool IsConfigurationError(const std::string& shill_error) {
  if (shill_error.empty()) {
    return false;
  }
  return shill_error == shill::kErrorPinMissing ||
         shill_error == shill::kErrorBadPassphrase ||
         shill_error == shill::kErrorResultInvalidPassphrase ||
         shill_error == shill::kErrorBadWEPKey;
}

std::string GetStringFromDictionary(
    const std::optional<base::Value::Dict>& dict,
    const std::string& key) {
  const std::string* v = dict ? dict->FindString(key) : nullptr;
  return v ? *v : std::string();
}

// Error messages based on |error_name|, not network_state->GetError().
std::u16string GetConnectErrorString(const std::string& error_name) {
  if (error_name == NetworkConnectionHandler::kErrorNotFound) {
    return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_CONNECT_FAILED);
  }
  if (error_name == NetworkConnectionHandler::kErrorConfigureFailed) {
    return l10n_util::GetStringUTF16(
        IDS_CHROMEOS_NETWORK_ERROR_CONFIGURE_FAILED);
  }
  if (error_name == NetworkConnectionHandler::kErrorCertLoadTimeout) {
    return l10n_util::GetStringUTF16(
        IDS_CHROMEOS_NETWORK_ERROR_CERTIFICATES_NOT_LOADED);
  }
  if (error_name == NetworkConnectionHandler::kErrorActivateFailed) {
    return l10n_util::GetStringUTF16(
        IDS_CHROMEOS_NETWORK_ERROR_ACTIVATION_FAILED);
  }
  if (error_name == NetworkConnectionHandler::kErrorSimPinPukLocked) {
    return l10n_util::GetStringUTF16(IDS_NETWORK_LIST_SIM_CARD_LOCKED);
  }
  if (error_name == NetworkConnectionHandler::kErrorSimCarrierLocked) {
    return l10n_util::GetStringUTF16(IDS_NETWORK_LIST_SIM_CARD_CARRIER_LOCKED);
  }
  return std::u16string();
}

const gfx::VectorIcon& GetErrorNotificationVectorIcon(
    const std::string& network_type) {
  if (network_type == shill::kTypeVPN) {
    return kNotificationVpnIcon;
  }
  if (network_type == shill::kTypeCellular) {
    return kNotificationMobileDataOffIcon;
  }
  return kNotificationWifiOffIcon;
}

// |identifier| may be a service path or guid.
void ShowErrorNotification(const std::string& identifier,
                           const std::string& notification_id,
                           const NotificationCatalogName& catalog_name,
                           const std::string& network_type,
                           const std::u16string& title,
                           const std::u16string& message,
                           base::RepeatingClosure callback) {
  NET_LOG(ERROR) << "ShowErrorNotification: " << identifier << ": "
                 << base::UTF16ToUTF8(title);
  message_center::Notification notification = CreateSystemNotification(
      message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, title, message,
      std::u16string() /* display_source */, GURL(),
      message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
                                 kNotifierNetworkError, catalog_name),
      message_center::RichNotificationData(),
      new message_center::HandleNotificationClickDelegate(std::move(callback)),
      GetErrorNotificationVectorIcon(network_type),
      message_center::SystemNotificationWarningLevel::WARNING);
  SystemNotificationHelper::GetInstance()->Display(notification);
}

bool ShouldConnectFailedNotificationBeShown(const std::string& error_name,
                                            const NetworkState* network_state) {
  // Only show a notification for certain errors. Other failures are expected
  // to be handled by the UI that initiated the connect request.
  // Note: kErrorConnectFailed may also cause the configure dialog to be
  // displayed, but we rely on the notification system to show additional
  // details if available.
  if (error_name != NetworkConnectionHandler::kErrorConnectFailed &&
      error_name != NetworkConnectionHandler::kErrorNotFound &&
      error_name != NetworkConnectionHandler::kErrorConfigureFailed &&
      error_name != NetworkConnectionHandler::kErrorCertLoadTimeout &&
      error_name != NetworkConnectionHandler::kErrorSimPinPukLocked &&
      error_name != NetworkConnectionHandler::kErrorSimCarrierLocked) {
    return false;
  }

  // When a connection to a Tether network fails, the Tether component shows its
  // own error notification. If this is the case, there is no need to show an
  // additional notification for the failure to connect to the underlying Wi-Fi
  // network.
  if (network_state && !network_state->tether_guid().empty()) {
    return false;
  }

  // Otherwise, the connection failed notification should be shown.
  return true;
}

const NetworkState* GetNetworkStateForGuid(const std::string& guid) {
  return guid.empty() ? nullptr
                      : NetworkHandler::Get()
                            ->network_state_handler()
                            ->GetNetworkStateFromGuid(guid);
}

bool IsSimLockConnectionFailure(const std::string& connection_error_name,
                                const NetworkState* network_state) {
  if (connection_error_name ==
      NetworkConnectionHandler::kErrorSimPinPukLocked) {
    return true;
  }

  return network_state && network_state->GetError() == shill::kErrorSimLocked;
}

}  // namespace

const char NetworkStateNotifier::kNetworkConnectNotificationId[] =
    "chrome://settings/internet/connect";
const char NetworkStateNotifier::kNetworkActivateNotificationId[] =
    "chrome://settings/internet/activate";
const char NetworkStateNotifier::kNetworkOutOfCreditsNotificationId[] =
    "chrome://settings/internet/out-of-credits";
const char NetworkStateNotifier::kNetworkCarrierUnlockNotificationId[] =
    "chrome://settings/internet/carrier-unlock";

NetworkStateNotifier::NetworkStateNotifier() {
  if (!NetworkHandler::IsInitialized()) {
    return;
  }
  NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
  network_state_handler_observer_.Observe(handler);
  NetworkStateHandler::NetworkStateList active_networks;
  handler->GetActiveNetworkListByType(NetworkTypePattern::Default(),
                                      &active_networks);
  ActiveNetworksChanged(active_networks);
  NetworkHandler::Get()->network_connection_handler()->AddObserver(this);
}

NetworkStateNotifier::~NetworkStateNotifier() {
  if (!NetworkHandler::IsInitialized()) {
    return;
  }
  NetworkHandler::Get()->network_connection_handler()->RemoveObserver(this);
}

void NetworkStateNotifier::ConnectToNetworkRequested(
    const std::string& service_path) {
  const NetworkState* network =
      NetworkHandler::Get()->network_state_handler()->GetNetworkState(
          service_path);
  if (network && network->type() == shill::kTypeVPN) {
    connected_vpn_.reset();
  }

  RemoveConnectNotification();
}

void NetworkStateNotifier::NetworkConnectionStateChanged(
    const NetworkState* network) {
  if (!network->IsConnectedState() ||
      connect_error_notification_network_guid_.empty() ||
      connect_error_notification_network_guid_ != network->guid()) {
    return;
  }
  RemoveConnectNotification();
}

void NetworkStateNotifier::NetworkIdentifierTransitioned(
    const std::string& old_service_path,
    const std::string& new_service_path,
    const std::string& old_guid,
    const std::string& new_guid) {
  if (old_guid == new_guid ||
      old_guid != connect_error_notification_network_guid_) {
    return;
  }

  connect_error_notification_network_guid_ = new_guid;
}

void NetworkStateNotifier::OnShuttingDown() {
  network_state_handler_observer_.Reset();
}

void NetworkStateNotifier::ConnectSucceeded(const std::string& service_path) {
  RemoveConnectNotification();
}

void NetworkStateNotifier::ConnectFailed(const std::string& service_path,
                                         const std::string& error_name) {
  const NetworkState* network =
      NetworkHandler::Get()->network_state_handler()->GetNetworkState(
          service_path);
  if (!ShouldConnectFailedNotificationBeShown(error_name, network)) {
    NET_LOG(EVENT) << "Skipping notification for: "
                   << NetworkPathId(service_path) << " Error: " << error_name;
    return;
  }
  ShowNetworkConnectErrorForGuid(error_name, network ? network->guid() : "");
}

void NetworkStateNotifier::DisconnectRequested(
    const std::string& service_path) {
  const NetworkState* network =
      NetworkHandler::Get()->network_state_handler()->GetNetworkState(
          service_path);
  if (network && network->type() == shill::kTypeVPN) {
    connected_vpn_.reset();
  }
}

void NetworkStateNotifier::ActiveNetworksChanged(
    const std::vector<const NetworkState*>& active_networks) {
  const NetworkState* active_vpn = nullptr;
  std::string active_non_vpn_network_guid;
  // NOTE: The list of 'active' networks includes disconnected networks that
  // were connected or connecting.
  for (const auto* network : active_networks) {
    if (network->type() == shill::kTypeVPN) {
      // Make sure that if there is an edge case with two active VPNs that we
      // track the first active one.
      if (!active_vpn) {
        active_vpn = network;
      }
    } else if (active_non_vpn_network_guid.empty()) {
      // We are only interested in the "default" (first active) non virtual
      // network.
      if (network->IsConnectingOrConnected()) {
        active_non_vpn_network_guid = network->guid();
      }
    }
  }
  UpdateVpnConnectionState(active_vpn);

  // If the default network changes, allow the out of credits notification to be
  // shown again. A delay prevents the notification from being shown too
  // frequently (see below).
  if (active_non_vpn_network_guid != active_non_vpn_network_guid_) {
    active_non_vpn_network_guid_ = active_non_vpn_network_guid;
    did_show_out_of_credits_ = false;
    UpdateCellularOutOfCredits();
  }
}

void NetworkStateNotifier::NetworkPropertiesUpdated(
    const NetworkState* network) {
  if (network->type() != shill::kTypeCellular) {
    return;
  }
  if (network->cellular_out_of_credits()) {
    UpdateCellularOutOfCredits();
  }
  UpdateCellularActivating(network);
}

void NetworkStateNotifier::UpdateVpnConnectionState(
    const NetworkState* active_vpn) {
  if (!active_vpn || !active_vpn->IsConnectingOrConnected()) {
    // No connecting or connected VPN. If we were tracking a connected VPN,
    // show a notification.
    if (connected_vpn_) {
      ShowVpnDisconnectedNotification(connected_vpn_.get());
      connected_vpn_.reset();
    }
    return;
  }
  if (connected_vpn_ && active_vpn->guid() == connected_vpn_->guid) {
    // The connected VPN is unchanged and still connected or connecting. If the
    // VPN goes from connected -> connecting -> connected, we do not want to
    // show a notification.
    return;
  }

  // Do not track ARC VPNs for showing disconnect notifications. Also make sure
  // |connected_vpn_| is cleared when connected to an ARC VPN.
  if (active_vpn->GetVpnProviderType() == shill::kProviderArcVpn) {
    connected_vpn_.reset();
    return;
  }

  // If we are connected to a new VPN, track it as the connected VPN. Do not
  // show a notification, even if we were tracking a different connected VPN,
  // since we are still connected to a VPN (i.e. just replace |connected_vpn_|).
  if (active_vpn->IsConnectedState()) {
    connected_vpn_ =
        std::make_unique<VpnDetails>(active_vpn->guid(), active_vpn->name());
  } else {
    // |active_vpn| is connecting. Clear |connected_vpn_| so that we do not
    // show a notification for the previous VPN.
    connected_vpn_.reset();
  }
}

void NetworkStateNotifier::UpdateCellularOutOfCredits() {
  // Only show the notification once (reset when the primary network changes).
  if (did_show_out_of_credits_) {
    return;
  }

  NetworkStateHandler::NetworkStateList active_networks;
  NetworkHandler::Get()->network_state_handler()->GetActiveNetworkListByType(
      NetworkTypePattern::NonVirtual(), &active_networks);
  const NetworkState* primary_network = nullptr;
  for (const auto* network : active_networks) {
    // Don't display notification if any network is connecting.
    if (network->IsConnectingState()) {
      return;
    }
    if (!primary_network) {
      primary_network = network;
    }
  }

  if (!primary_network ||
      !primary_network->Matches(NetworkTypePattern::Cellular()) ||
      !primary_network->cellular_out_of_credits()) {
    return;
  }

  did_show_out_of_credits_ = true;
  base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_;
  if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) {
    out_of_credits_notify_time_ = base::Time::Now();
    std::u16string error_msg =
        l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_CREDITS_BODY,
                                   base::UTF8ToUTF16(primary_network->name()));
    ShowErrorNotification(
        NetworkId(primary_network), kNetworkOutOfCreditsNotificationId,
        NotificationCatalogName::kNetworkOutOfCredits, primary_network->type(),
        l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_TITLE), error_msg,
        base::BindRepeating(&NetworkStateNotifier::ShowCarrierAccountDetail,
                            weak_ptr_factory_.GetWeakPtr(),
                            primary_network->guid()));
  }
}

void NetworkStateNotifier::UpdateCellularActivating(
    const NetworkState* cellular) {
  const std::string cellular_guid = cellular->guid();
  // Keep track of any activating cellular network.
  std::string activation_state = cellular->activation_state();
  if (activation_state == shill::kActivationStateActivating) {
    cellular_activating_guids_.insert(cellular_guid);
    return;
  }
  // Only display a notification if this network was activating and is now
  // activated.
  if (!cellular_activating_guids_.count(cellular_guid) ||
      activation_state != shill::kActivationStateActivated) {
    return;
  }

  cellular_activating_guids_.erase(cellular_guid);
  message_center::Notification notification = CreateSystemNotification(
      message_center::NOTIFICATION_TYPE_SIMPLE, kNetworkActivateNotificationId,
      l10n_util::GetStringUTF16(IDS_NETWORK_CELLULAR_ACTIVATED_TITLE),
      l10n_util::GetStringFUTF16(IDS_NETWORK_CELLULAR_ACTIVATED,
                                 base::UTF8ToUTF16((cellular->name()))),
      std::u16string() /* display_source */, GURL(),
      message_center::NotifierId(
          message_center::NotifierType::SYSTEM_COMPONENT, kNotifierNetwork,
          NotificationCatalogName::kNetworkCellularActivated),
      {},
      new message_center::HandleNotificationClickDelegate(
          base::BindRepeating(&NetworkStateNotifier::ShowNetworkSettings,
                              weak_ptr_factory_.GetWeakPtr(), cellular_guid)),
      kNotificationMobileDataIcon,
      message_center::SystemNotificationWarningLevel::WARNING);
  SystemNotificationHelper::GetInstance()->Display(notification);
}

void NetworkStateNotifier::ShowNetworkConnectErrorForGuid(
    const std::string& error_name,
    const std::string& guid) {
  const NetworkState* network = GetNetworkStateForGuid(guid);
  if (!network) {
    ShowConnectErrorNotification(error_name,
                                 /*service_path=*/std::string(),
                                 /*shill_properties=*/std::nullopt);
    return;
  }
  // Get the up-to-date properties for the network and display the error.
  NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
      network->path(),
      base::BindRepeating(&NetworkStateNotifier::OnConnectErrorGetProperties,
                          weak_ptr_factory_.GetWeakPtr(), error_name));
}

void NetworkStateNotifier::ShowMobileActivationErrorForGuid(
    const std::string& guid) {
  const NetworkState* cellular = GetNetworkStateForGuid(guid);
  if (!cellular || cellular->type() != shill::kTypeCellular) {
    NET_LOG(ERROR) << "ShowMobileActivationError without Cellular network: "
                   << guid;
    return;
  }
  message_center::Notification notification = CreateSystemNotification(
      message_center::NOTIFICATION_TYPE_SIMPLE, kNetworkActivateNotificationId,
      l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE),
      l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION,
                                 base::UTF8ToUTF16((cellular->name()))),
      std::u16string() /* display_source */, GURL(),
      message_center::NotifierId(
          message_center::NotifierType::SYSTEM_COMPONENT, kNotifierNetworkError,
          NotificationCatalogName::kNetworkActivationError),
      {},
      new message_center::HandleNotificationClickDelegate(base::BindRepeating(
          &NetworkStateNotifier::ShowNetworkSettings,
          weak_ptr_factory_.GetWeakPtr(), cellular->guid())),
      kNotificationMobileDataOffIcon,
      message_center::SystemNotificationWarningLevel::WARNING);
  SystemNotificationHelper::GetInstance()->Display(notification);
}

void NetworkStateNotifier::RemoveConnectNotification() {
  SystemNotificationHelper::GetInstance()->Close(kNetworkConnectNotificationId);
  connect_error_notification_network_guid_.clear();
}

void NetworkStateNotifier::RemoveCarrierUnlockNotification() {
  SystemNotificationHelper::GetInstance()->Close(
      kNetworkCarrierUnlockNotificationId);
}

void NetworkStateNotifier::OnConnectErrorGetProperties(
    const std::string& error_name,
    const std::string& service_path,
    std::optional<base::Value::Dict> shill_properties) {
  if (!shill_properties) {
    ShowConnectErrorNotification(error_name, service_path,
                                 std::move(shill_properties));
    return;
  }

  std::string state =
      GetStringFromDictionary(shill_properties, shill::kStateProperty);
  if (NetworkState::StateIsConnected(state) ||
      NetworkState::StateIsConnecting(state)) {
    NET_LOG(EVENT) << "Skipping connect error notification. State: " << state;
    // Network is no longer in an error state. This can happen if an
    // unexpected idle state transition occurs, see http://crbug.com/333955.
    return;
  }
  ShowConnectErrorNotification(error_name, service_path,
                               std::move(shill_properties));
}

void NetworkStateNotifier::ShowConnectErrorNotification(
    const std::string& error_name,
    const std::string& service_path,
    std::optional<base::Value::Dict> shill_properties) {
  std::u16string error = GetConnectErrorString(error_name);
  NET_LOG(DEBUG) << "Notify: " << NetworkPathId(service_path)
                 << ": Connect error: " << error_name << ": "
                 << base::UTF16ToUTF8(error);

  const NetworkState* network =
      NetworkHandler::Get()->network_state_handler()->GetNetworkState(
          service_path);
  std::string guid = network ? network->guid() : "";
  std::string log_id =
      network ? NetworkId(network) : NetworkPathId(service_path);

  if (error.empty()) {
    std::string shill_error =
        GetStringFromDictionary(shill_properties, shill::kErrorProperty);
    if (!NetworkState::ErrorIsValid(shill_error)) {
      shill_error = GetStringFromDictionary(shill_properties,
                                            shill::kPreviousErrorProperty);
      NET_LOG(DEBUG) << "Notify: " << log_id
                     << ": Service.PreviousError: " << shill_error;
      if (!NetworkState::ErrorIsValid(shill_error)) {
        shill_error.clear();
      }
    } else {
      NET_LOG(DEBUG) << "Notify: " << log_id
                     << ": Service.Error: " << shill_error;
    }

    if (network) {
      // Log all error values for debugging.
      NET_LOG(DEBUG) << "Notify: " << log_id
                     << ": Network.GetError(): " << network->GetError()
                     << " shill_connect_error: "
                     << network->shill_connect_error();
      if (shill_error.empty()) {
        shill_error = network->GetError();
      }
      if (shill_error.empty()) {
        shill_error = network->shill_connect_error();
      }
    }

    if (ShillErrorIsIgnored(shill_error)) {
      NET_LOG(DEBUG) << "Notify: " << log_id
                     << ": Ignoring error: " << error_name;
      return;
    }
    error = ui::shill_error::GetShillErrorString(shill_error, guid);
    if (error.empty()) {
      if (error_name == NetworkConnectionHandler::kErrorConnectFailed &&
          network && !network->connectable()) {
        // Connect failure on non connectable network with no additional
        // information. We expect the UI to show configuration UI so do not
        // show an additional (and unhelpful) notification.
        return;
      }
      error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN);
    }
  }
  NET_LOG(ERROR) << "Notify: " << log_id
                 << ": Connect error: " + base::UTF16ToUTF8(error);

  CellularESimProfileHandler* cellular_esim_profile_handler =
      NetworkHandler::Get()->cellular_esim_profile_handler();
  std::string network_name;
  if (network) {
    std::optional<std::string> esim_name =
        network_name_util::GetESimProfileName(cellular_esim_profile_handler,
                                              network);
    if (esim_name) {
      network_name = *esim_name;
    }
  }
  if (network_name.empty() && shill_properties) {
    network_name = shill_property_util::GetNameFromProperties(
        service_path, shill_properties.value());
  }

  std::string network_error_details =
      GetStringFromDictionary(shill_properties, shill::kErrorDetailsProperty);

  std::u16string error_msg;
  if (!network_error_details.empty()) {
    // network_name shouldn't be empty if network_error_details is set.
    error_msg = l10n_util::GetStringFUTF16(
        IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE,
        base::UTF8ToUTF16(network_name), error,
        base::UTF8ToUTF16(network_error_details));
  } else if (network_name.empty()) {
    error_msg = l10n_util::GetStringFUTF16(
        IDS_NETWORK_CONNECTION_ERROR_MESSAGE_NO_NAME, error);
  } else {
    error_msg =
        l10n_util::GetStringFUTF16(IDS_NETWORK_CONNECTION_ERROR_MESSAGE,
                                   base::UTF8ToUTF16(network_name), error);
  }

  std::string network_type =
      GetStringFromDictionary(shill_properties, shill::kTypeProperty);

  base::RepeatingClosure on_click;
  if (IsSimLockConnectionFailure(error_name, network)) {
    on_click = base::BindRepeating(&NetworkStateNotifier::ShowSimUnlockSettings,
                                   weak_ptr_factory_.GetWeakPtr());
  } else if (features::IsApnRevampEnabled() && network &&
             network->GetError() == shill::kErrorInvalidAPN) {
    on_click = base::BindRepeating(&NetworkStateNotifier::ShowApnSettings,
                                   weak_ptr_factory_.GetWeakPtr(), guid);
  } else {
    on_click = base::BindRepeating(&NetworkStateNotifier::ShowNetworkSettings,
                                   weak_ptr_factory_.GetWeakPtr(), guid);
  }

  connect_error_notification_network_guid_ = guid;
  ShowErrorNotification(
      NetworkPathId(service_path), kNetworkConnectNotificationId,
      NotificationCatalogName::kNetworkConnectionError, network_type,
      l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), error_msg,
      std::move(on_click));
}

void NetworkStateNotifier::ShowVpnDisconnectedNotification(VpnDetails* vpn) {
  DCHECK(vpn);
  std::u16string error_msg = l10n_util::GetStringFUTF16(
      IDS_NETWORK_VPN_CONNECTION_LOST_BODY, base::UTF8ToUTF16(vpn->name));
  ShowErrorNotification(
      NetworkGuidId(vpn->guid), kNetworkConnectNotificationId,
      NotificationCatalogName::kNetworkVPNConnectionLost, shill::kTypeVPN,
      l10n_util::GetStringUTF16(IDS_NETWORK_VPN_CONNECTION_LOST_TITLE),
      error_msg,
      base::BindRepeating(&NetworkStateNotifier::ShowNetworkSettings,
                          weak_ptr_factory_.GetWeakPtr(), vpn->guid));
}

void NetworkStateNotifier::ShowCarrierUnlockNotification() {
  std::u16string message =
      l10n_util::GetStringUTF16(IDS_NETWORK_CARRIER_UNLOCK_BODY);
  message_center::Notification notification = CreateSystemNotification(
      message_center::NOTIFICATION_TYPE_SIMPLE,
      kNetworkCarrierUnlockNotificationId,
      l10n_util::GetStringFUTF16(IDS_NETWORK_CARRIER_UNLOCK_TITLE,
                                 ui::GetChromeOSDeviceName()),
      message, std::u16string() /* display_source */, GURL(),
      message_center::NotifierId(
          message_center::NotifierType::SYSTEM_COMPONENT, kNotifierNetworkError,
          NotificationCatalogName::kNetworkCarrierUnlock),
      message_center::RichNotificationData(),
      new message_center::HandleNotificationClickDelegate(
          base::BindRepeating(&NetworkStateNotifier::ShowMobileDataSubpage,
                              weak_ptr_factory_.GetWeakPtr())),
      gfx::VectorIcon::EmptyIcon(),
      message_center::SystemNotificationWarningLevel::NORMAL);
  SystemNotificationHelper::GetInstance()->Display(notification);
}

void NetworkStateNotifier::ShowNetworkSettings(const std::string& network_id) {
  if (!system_tray_client_) {
    return;
  }
  const NetworkState* network = GetNetworkStateForGuid(network_id);
  if (!network) {
    return;
  }
  std::string error = network->GetError();
  if (!error.empty()) {
    NET_LOG(ERROR) << "Notify ShowNetworkSettings: " << NetworkId(network)
                   << ": Error: " << error;
  }
  if (!NetworkTypePattern::Primitive(network->type())
           .MatchesPattern(NetworkTypePattern::Mobile()) &&
      IsConfigurationError(error)) {
    system_tray_client_->ShowNetworkConfigure(network_id);
  } else {
    system_tray_client_->ShowNetworkSettings(network_id);
  }
}

void NetworkStateNotifier::ShowSimUnlockSettings() {
  if (!system_tray_client_) {
    return;
  }

  NET_LOG(USER) << "Opening SIM unlock settings";
  system_tray_client_->ShowSettingsSimUnlock();
}

void NetworkStateNotifier::ShowMobileDataSubpage() {
  if (!system_tray_client_) {
    return;
  }

  NET_LOG(USER) << "Opening Mobile data subpage";
  system_tray_client_->ShowMobileDataSubpage();
}

void NetworkStateNotifier::ShowApnSettings(const std::string& network_id) {
  CHECK(features::IsApnRevampEnabled());
  if (!system_tray_client_) {
    return;
  }

  NET_LOG(USER) << "Opening APN subpage for network: " << network_id;
  system_tray_client_->ShowApnSubpage(network_id);
}

void NetworkStateNotifier::ShowCarrierAccountDetail(
    const std::string& network_id) {
  NetworkConnect::Get()->ShowCarrierAccountDetail(network_id);
}

}  // namespace ash