File: tether_notification_presenter.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 (391 lines) | stat: -rw-r--r-- 15,755 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
// Copyright 2017 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/tether_notification_presenter.h"

#include <algorithm>
#include <string>

#include "ash/constants/ash_features.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/network_icon_image_source.h"
#include "ash/public/cpp/notification_utils.h"
#include "ash/webui/settings/public/constants/routes.mojom.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/settings_window_manager_chromeos.h"
#include "chrome/common/url_constants.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/network/network_connect.h"
#include "chromeos/ash/components/tether/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "ui/message_center/public/cpp/notifier_id.h"

namespace ash::tether {

namespace {

const char kNotifierTether[] = "ash.tether";

// Mean value of NetworkState's signal_strength() range.
const int kMediumSignalStrength = 50;

// Dimensions of Tether notification icon in pixels.
constexpr gfx::Size kTetherSignalIconSize(18, 18);

// Handles clicking and closing of a notification via callbacks.
class TetherNotificationDelegate
    : public message_center::HandleNotificationClickDelegate {
 public:
  TetherNotificationDelegate(ButtonClickCallback click,
                             base::RepeatingClosure close)
      : HandleNotificationClickDelegate(click), close_callback_(close) {}

  TetherNotificationDelegate(const TetherNotificationDelegate&) = delete;
  TetherNotificationDelegate& operator=(const TetherNotificationDelegate&) =
      delete;

  // NotificationDelegate:
  void Close(bool by_user) override {
    if (!close_callback_.is_null()) {
      close_callback_.Run();
    }
  }

 private:
  ~TetherNotificationDelegate() override = default;

  base::RepeatingClosure close_callback_;
};

class SettingsUiDelegateImpl
    : public TetherNotificationPresenter::SettingsUiDelegate {
 public:
  SettingsUiDelegateImpl() = default;
  ~SettingsUiDelegateImpl() override = default;

  void ShowSettingsSubPageForProfile(Profile* profile,
                                     const std::string& sub_page) override {
    chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(profile,
                                                                 sub_page);
  }
};

// Returns the icon to use for a network with the given signal strength, which
// should range from 0 to 100 (inclusive).
const gfx::ImageSkia GetImageForSignalStrength(int signal_strength) {
  // Convert the [0, 100] range to [0, 4], since there are 5 distinct signal
  // strength icons (0 bars to 4 bars).
  int normalized_signal_strength = std::clamp(signal_strength / 25, 0, 4);

  return gfx::CanvasImageSource::MakeImageSkia<
      network_icon::SignalStrengthImageSource>(
      network_icon::BARS, gfx::kGoogleBlue500, kTetherSignalIconSize,
      normalized_signal_strength, 5);
}

}  // namespace

// static
constexpr const char TetherNotificationPresenter::kActiveHostNotificationId[] =
    "cros_tether_notification_ids.active_host";

// static
constexpr const char
    TetherNotificationPresenter::kPotentialHotspotNotificationId[] =
        "cros_tether_notification_ids.potential_hotspot";

// static
constexpr const char
    TetherNotificationPresenter::kSetupRequiredNotificationId[] =
        "cros_tether_notification_ids.setup_required";

// static
constexpr const char* const
    TetherNotificationPresenter::kIdsWhichOpenTetherSettingsOnClick[] = {
        TetherNotificationPresenter::kActiveHostNotificationId,
        TetherNotificationPresenter::kPotentialHotspotNotificationId,
        TetherNotificationPresenter::kSetupRequiredNotificationId};

TetherNotificationPresenter::TetherNotificationPresenter(
    Profile* profile,
    NetworkConnect* network_connect)
    : profile_(profile),
      network_connect_(network_connect),
      settings_ui_delegate_(base::WrapUnique(new SettingsUiDelegateImpl())) {}

TetherNotificationPresenter::~TetherNotificationPresenter() = default;

// static
void TetherNotificationPresenter::RegisterProfilePrefs(
    PrefRegistrySimple* pref_registry) {
  pref_registry->RegisterBooleanPref(prefs::kNotificationsEnabled, true);
}

void TetherNotificationPresenter::NotifyPotentialHotspotNearby(
    const std::string& device_id,
    const std::string& device_name,
    int signal_strength) {
  PA_LOG(VERBOSE) << "Displaying \"potential hotspot nearby\" notification for "
                  << "device with name \"" << device_name << "\". "
                  << "Notification ID = " << kPotentialHotspotNotificationId;

  hotspot_nearby_device_id_ = std::make_unique<std::string>(device_id);

  message_center::RichNotificationData rich_notification_data;
  rich_notification_data.buttons.push_back(
      message_center::ButtonInfo(l10n_util::GetStringUTF16(
          IDS_TETHER_NOTIFICATION_WIFI_AVAILABLE_ONE_DEVICE_CONNECT)));

  ShowNotification(CreateNotification(
      kPotentialHotspotNotificationId,
      NotificationCatalogName::kTetherPotentialHotspot,
      l10n_util::GetStringUTF16(
          IDS_TETHER_NOTIFICATION_WIFI_AVAILABLE_ONE_DEVICE_TITLE),
      l10n_util::GetStringFUTF16(
          IDS_TETHER_NOTIFICATION_WIFI_AVAILABLE_ONE_DEVICE_MESSAGE,
          base::ASCIIToUTF16(device_name)),
      GetImageForSignalStrength(signal_strength), rich_notification_data));
}

void TetherNotificationPresenter::NotifyMultiplePotentialHotspotsNearby() {
  PA_LOG(VERBOSE) << "Displaying \"potential hotspot nearby\" notification for "
                  << "multiple devices. Notification ID = "
                  << kPotentialHotspotNotificationId;

  hotspot_nearby_device_id_.reset();

  ShowNotification(CreateNotification(
      kPotentialHotspotNotificationId,
      NotificationCatalogName::kTetherPotentialHotspot,
      l10n_util::GetStringUTF16(
          IDS_TETHER_NOTIFICATION_WIFI_AVAILABLE_MULTIPLE_DEVICES_TITLE),
      l10n_util::GetStringUTF16(
          IDS_TETHER_NOTIFICATION_WIFI_AVAILABLE_MULTIPLE_DEVICES_MESSAGE),
      GetImageForSignalStrength(kMediumSignalStrength),
      {} /* rich_notification_data */));
}

NotificationPresenter::PotentialHotspotNotificationState
TetherNotificationPresenter::GetPotentialHotspotNotificationState() {
  if (showing_notification_id_ != kPotentialHotspotNotificationId) {
    return NotificationPresenter::PotentialHotspotNotificationState::
        NO_HOTSPOT_NOTIFICATION_SHOWN;
  }

  return hotspot_nearby_device_id_
             ? NotificationPresenter::PotentialHotspotNotificationState::
                   SINGLE_HOTSPOT_NEARBY_SHOWN
             : NotificationPresenter::PotentialHotspotNotificationState::
                   MULTIPLE_HOTSPOTS_NEARBY_SHOWN;
}

void TetherNotificationPresenter::RemovePotentialHotspotNotification() {
  RemoveNotificationIfVisible(kPotentialHotspotNotificationId);
}

void TetherNotificationPresenter::NotifySetupRequired(
    const std::string& device_name,
    int signal_strength) {
  PA_LOG(VERBOSE) << "Displaying \"setup required\" notification. Notification "
                  << "ID = " << kSetupRequiredNotificationId;

  // Persist this notification until acted upon or dismissed, so that the user
  // is aware that they need to complete setup on their phone.
  message_center::RichNotificationData rich_notification_data;
  rich_notification_data.never_timeout = true;

  ShowNotification(CreateNotification(
      kSetupRequiredNotificationId,
      NotificationCatalogName::kTetherSetupRequired,
      l10n_util::GetStringFUTF16(IDS_TETHER_NOTIFICATION_SETUP_REQUIRED_TITLE,
                                 base::ASCIIToUTF16(device_name)),
      l10n_util::GetStringFUTF16(IDS_TETHER_NOTIFICATION_SETUP_REQUIRED_MESSAGE,
                                 base::ASCIIToUTF16(device_name)),
      GetImageForSignalStrength(signal_strength), rich_notification_data));
}

void TetherNotificationPresenter::RemoveSetupRequiredNotification() {
  RemoveNotificationIfVisible(kSetupRequiredNotificationId);
}

void TetherNotificationPresenter::NotifyConnectionToHostFailed() {
  const std::string id = kActiveHostNotificationId;
  PA_LOG(VERBOSE) << "Displaying \"connection attempt failed\" notification. "
                  << "Notification ID = " << id;

  ShowNotification(CreateSystemNotificationPtr(
      message_center::NotificationType::NOTIFICATION_TYPE_SIMPLE, id,
      features::IsInstantHotspotRebrandEnabled()
          ? l10n_util::GetStringUTF16(
                IDS_TETHER_NOTIFICATION_CONNECTION_FAILED_TITLE)
          : l10n_util::GetStringUTF16(
                IDS_TETHER_NOTIFICATION_CONNECTION_FAILED_TITLE_LEGACY),
      l10n_util::GetStringUTF16(
          IDS_TETHER_NOTIFICATION_CONNECTION_FAILED_MESSAGE),
      std::u16string() /* display_source */, GURL() /* origin_url */,
      message_center::NotifierId(
          message_center::NotifierType::SYSTEM_COMPONENT, kNotifierTether,
          NotificationCatalogName::kTetherConnectionError),
      {} /* rich_notification_data */,
      new message_center::HandleNotificationClickDelegate(base::BindRepeating(
          &TetherNotificationPresenter::OnNotificationClicked,
          weak_ptr_factory_.GetWeakPtr(), id)),
      kNotificationCellularAlertIcon,
      message_center::SystemNotificationWarningLevel::WARNING));
}

void TetherNotificationPresenter::RemoveConnectionToHostFailedNotification() {
  RemoveNotificationIfVisible(kActiveHostNotificationId);
}

void TetherNotificationPresenter::OnNotificationClicked(
    const std::string& notification_id,
    std::optional<int> button_index) {
  if (button_index) {
    DCHECK_EQ(kPotentialHotspotNotificationId, notification_id);
    DCHECK_EQ(0, *button_index);
    DCHECK(hotspot_nearby_device_id_);
    UMA_HISTOGRAM_ENUMERATION(
        "InstantTethering.NotificationInteractionType",
        TetherNotificationPresenter::NOTIFICATION_BUTTON_TAPPED_HOST_NEARBY,
        TetherNotificationPresenter::NOTIFICATION_INTERACTION_TYPE_MAX);
    PA_LOG(VERBOSE) << "\"Potential hotspot nearby\" notification button was "
                    << "clicked.";
    network_connect_->ConnectToNetworkId(*hotspot_nearby_device_id_);
    RemoveNotificationIfVisible(kPotentialHotspotNotificationId);
    return;
  }

  UMA_HISTOGRAM_ENUMERATION(
      "InstantTethering.NotificationInteractionType",
      GetMetricValueForClickOnNotificationBody(notification_id),
      TetherNotificationPresenter::NOTIFICATION_INTERACTION_TYPE_MAX);

  OpenSettingsAndRemoveNotification(
      chromeos::settings::mojom::kMobileDataNetworksSubpagePath,
      notification_id);
}

TetherNotificationPresenter::NotificationInteractionType
TetherNotificationPresenter::GetMetricValueForClickOnNotificationBody(
    const std::string& clicked_notification_id) const {
  if (clicked_notification_id == kPotentialHotspotNotificationId &&
      hotspot_nearby_device_id_.get()) {
    return TetherNotificationPresenter::
        NOTIFICATION_BODY_TAPPED_SINGLE_HOST_NEARBY;
  }
  if (clicked_notification_id == kPotentialHotspotNotificationId &&
      !hotspot_nearby_device_id_.get()) {
    return TetherNotificationPresenter::
        NOTIFICATION_BODY_TAPPED_MULTIPLE_HOSTS_NEARBY;
  }
  if (clicked_notification_id == kSetupRequiredNotificationId) {
    return TetherNotificationPresenter::NOTIFICATION_BODY_TAPPED_SETUP_REQUIRED;
  }
  if (clicked_notification_id == kActiveHostNotificationId) {
    return TetherNotificationPresenter::
        NOTIFICATION_BODY_TAPPED_CONNECTION_FAILED;
  }
  NOTREACHED();
}

void TetherNotificationPresenter::OnNotificationClosed(
    const std::string& notification_id) {
  if (showing_notification_id_ == notification_id) {
    showing_notification_id_.clear();
  }
}

std::unique_ptr<message_center::Notification>
TetherNotificationPresenter::CreateNotification(
    const std::string& id,
    const NotificationCatalogName& catalog_name,
    const std::u16string& title,
    const std::u16string& message,
    const gfx::ImageSkia& small_image,
    const message_center::RichNotificationData& rich_notification_data) {
  auto notification = std::make_unique<message_center::Notification>(
      message_center::NotificationType::NOTIFICATION_TYPE_SIMPLE, id, title,
      message, ui::ImageModel(), std::u16string() /* display_source */,
      GURL() /* origin_url */,
      message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
                                 kNotifierTether, catalog_name),
      rich_notification_data,
      new TetherNotificationDelegate(
          base::BindRepeating(
              &TetherNotificationPresenter::OnNotificationClicked,
              weak_ptr_factory_.GetWeakPtr(), id),
          base::BindRepeating(
              &TetherNotificationPresenter::OnNotificationClosed,
              weak_ptr_factory_.GetWeakPtr(), id)));
  notification->SetSmallImage(gfx::Image(small_image));
  if (base::FeatureList::IsEnabled(ash::features::kInstantHotspotRebrand)) {
    notification->set_never_timeout(true);
  }
  return notification;
}

void TetherNotificationPresenter::SetSettingsUiDelegateForTesting(
    std::unique_ptr<SettingsUiDelegate> settings_ui_delegate) {
  settings_ui_delegate_ = std::move(settings_ui_delegate);
}

void TetherNotificationPresenter::ShowNotification(
    std::unique_ptr<message_center::Notification> notification) {
  if (!AreNotificationsEnabled()) {
    PA_LOG(INFO) << "Not showing notification with ID [" << notification->id()
                 << "] since user has notifications disabled.";
    return;
  }

  showing_notification_id_ = notification->id();
  NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
      NotificationHandler::Type::TRANSIENT, *notification,
      /*metadata=*/nullptr);
}

void TetherNotificationPresenter::OpenSettingsAndRemoveNotification(
    const std::string& settings_subpage,
    const std::string& notification_id) {
  PA_LOG(VERBOSE) << "Notification with ID " << notification_id
                  << " was clicked. "
                  << "Opening settings subpage: " << settings_subpage;

  settings_ui_delegate_->ShowSettingsSubPageForProfile(profile_,
                                                       settings_subpage);
  RemoveNotificationIfVisible(notification_id);
}

void TetherNotificationPresenter::RemoveNotificationIfVisible(
    const std::string& notification_id) {
  if (notification_id == kPotentialHotspotNotificationId) {
    hotspot_nearby_device_id_.reset();
  }

  NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
      NotificationHandler::Type::TRANSIENT, notification_id);
}

bool TetherNotificationPresenter::AreNotificationsEnabled() {
  return profile_->GetPrefs()->GetBoolean(prefs::kNotificationsEnabled);
}

}  // namespace ash::tether