File: mobile_data_notifications.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 (175 lines) | stat: -rw-r--r-- 6,784 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
// 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/mobile_data_notifications.h"

#include <string>

#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/notification_utils.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/notifications/system_notification_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/system/system_tray_client_impl.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "chromeos/ash/components/network/network_connection_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_type_pattern.h"
#include "components/prefs/pref_service.h"
#include "components/session_manager/core/session_manager.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "ui/base/l10n/l10n_util.h"

using ::ash::NetworkHandler;
using ::ash::NetworkState;
using ::ash::NetworkStateHandler;
using session_manager::SessionManager;
using user_manager::UserManager;

namespace {

const char kMobileDataNotificationId[] =
    "chrome://settings/internet/mobile_data";
const char kNotifierMobileData[] = "ash.mobile-data";

void MobileDataNotificationClicked(const std::string& network_id) {
  SystemTrayClientImpl::Get()->ShowNetworkSettings(network_id);
}

constexpr int kNotificationCheckDelayInSeconds = 2;

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// MobileDataNotifications

MobileDataNotifications::MobileDataNotifications() {
  network_state_handler_observer_.Observe(
      NetworkHandler::Get()->network_state_handler());
  NetworkHandler::Get()->network_connection_handler()->AddObserver(this);
  UserManager::Get()->AddSessionStateObserver(this);
  SessionManager::Get()->AddObserver(this);
}

MobileDataNotifications::~MobileDataNotifications() {
  if (NetworkHandler::IsInitialized()) {
    NetworkHandler::Get()->network_connection_handler()->RemoveObserver(this);
  }
  UserManager::Get()->RemoveSessionStateObserver(this);
  SessionManager::Get()->RemoveObserver(this);
}

void MobileDataNotifications::ActiveNetworksChanged(
    const std::vector<const NetworkState*>& active_networks) {
  if (SessionManager::Get()->IsUserSessionBlocked()) {
    return;
  }
  ShowOptionalMobileDataNotificationImpl(active_networks);
}

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

void MobileDataNotifications::ConnectSucceeded(
    const std::string& service_path) {
  // We delay because it might take some time before the default network
  // changes after a connection is established.
  DelayedShowOptionalMobileDataNotification();
}

void MobileDataNotifications::ConnectFailed(const std::string& service_path,
                                            const std::string& error_name) {
  // We delay because it might take some time before the default network
  // changes after a connection request fails.
  DelayedShowOptionalMobileDataNotification();
}

void MobileDataNotifications::ActiveUserChanged(
    user_manager::User* active_user) {
  ShowOptionalMobileDataNotification();
}

void MobileDataNotifications::OnSessionStateChanged() {
  TRACE_EVENT0("ui", "MobileDataNotifications::OnSessionStateChanged");
  ShowOptionalMobileDataNotification();
}

void MobileDataNotifications::ShowOptionalMobileDataNotification() {
  if (SessionManager::Get()->IsUserSessionBlocked()) {
    return;
  }

  NetworkStateHandler::NetworkStateList active_networks;
  NetworkHandler::Get()->network_state_handler()->GetActiveNetworkListByType(
      ash::NetworkTypePattern::NonVirtual(), &active_networks);
  ShowOptionalMobileDataNotificationImpl(active_networks);
}

void MobileDataNotifications::ShowOptionalMobileDataNotificationImpl(
    const std::vector<const NetworkState*>& active_networks) {
  const NetworkState* first_active_network = nullptr;
  for (const auto* network : active_networks) {
    if (network->IsConnectingState()) {
      return;  // Don not show notification while connecting.
    }
    if (!first_active_network) {
      first_active_network = network;
    }
  }
  if (!first_active_network ||
      first_active_network->type() != shill::kTypeCellular) {
    return;
  }

  // Check if we've shown this notification before.
  PrefService* prefs = ProfileManager::GetActiveUserProfile()->GetPrefs();
  if (!prefs->GetBoolean(prefs::kShowMobileDataNotification)) {
    return;
  }

  // Prevent the notification from showing up in the future and stop any running
  // timers.
  prefs->SetBoolean(prefs::kShowMobileDataNotification, false);
  one_shot_notification_check_delay_.Stop();

  // Display a one-time notification on first use of Mobile Data connection.
  message_center::Notification notification = ash::CreateSystemNotification(
      message_center::NOTIFICATION_TYPE_SIMPLE, kMobileDataNotificationId,
      l10n_util::GetStringUTF16(IDS_MOBILE_DATA_NOTIFICATION_TITLE),
      l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE),
      std::u16string() /* display_source */, GURL(),
      message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
                                 kNotifierMobileData,
                                 ash::NotificationCatalogName::kMobileData),
      message_center::RichNotificationData(),
      base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
          base::BindRepeating(&MobileDataNotificationClicked,
                              first_active_network->guid())),
      kNotificationMobileDataIcon,
      message_center::SystemNotificationWarningLevel::NORMAL);

  SystemNotificationHelper::GetInstance()->Display(notification);
}

void MobileDataNotifications::DelayedShowOptionalMobileDataNotification() {
  if (one_shot_notification_check_delay_.IsRunning()) {
    one_shot_notification_check_delay_.Reset();
    return;
  }
  one_shot_notification_check_delay_.Start(
      FROM_HERE, base::Seconds(kNotificationCheckDelayInSeconds),
      base::BindOnce(
          &MobileDataNotifications::ShowOptionalMobileDataNotification,
          // Callbacks won't run after this object is destroyed by using weak
          // pointers. Weak pointers are not thread safe but it's safe to use
          // here because timers run in a sequenced task runner.
          weak_factory_.GetWeakPtr()));
}