File: notification_platform_bridge_mac.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 (275 lines) | stat: -rw-r--r-- 11,661 bytes parent folder | download | duplicates (3)
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
// Copyright 2015 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/notifications/mac/notification_platform_bridge_mac.h"

#include <utility>

#include "base/barrier_closure.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "chrome/browser/notifications/mac/mac_notification_provider_factory.h"
#include "chrome/browser/notifications/mac/notification_dispatcher_mojo.h"
#include "chrome/browser/notifications/mac/notification_utils.h"
#include "chrome/browser/notifications/notification_common.h"
#include "chrome/browser/notifications/notification_display_service_impl.h"
#include "chrome/browser/notifications/platform_notification_service_impl.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/os_integration/mac/app_shim_registry.h"
#include "chrome/browser/web_applications/os_integration/mac/web_app_shortcut_mac.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/common/chrome_features.h"
#include "third_party/blink/public/common/notifications/notification_constants.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"

NotificationPlatformBridgeMac::NotificationPlatformBridgeMac(
    std::unique_ptr<NotificationDispatcherMac> banner_dispatcher,
    std::unique_ptr<NotificationDispatcherMac> alert_dispatcher,
    WebAppDispatcherFactory web_app_dispatcher_factory)
    : banner_dispatcher_(std::move(banner_dispatcher)),
      alert_dispatcher_(std::move(alert_dispatcher)),
      web_app_dispatcher_factory_(std::move(web_app_dispatcher_factory)) {}

NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() {
  // TODO(miguelg) do not remove banners if possible.
  banner_dispatcher_->CloseAllNotifications();
  alert_dispatcher_->CloseAllNotifications();
}

// static
std::unique_ptr<NotificationPlatformBridge>
NotificationPlatformBridge::Create() {
  auto banner_dispatcher = std::make_unique<NotificationDispatcherMojo>(
      std::make_unique<MacNotificationProviderFactory>(
          mac_notifications::NotificationStyle::kBanner));
  auto alert_dispatcher = std::make_unique<NotificationDispatcherMojo>(
      std::make_unique<MacNotificationProviderFactory>(
          mac_notifications::NotificationStyle::kAlert));
  auto create_dispatcher_for_web_app =
      base::BindRepeating([](const webapps::AppId& web_app_id)
                              -> std::unique_ptr<NotificationDispatcherMac> {
        return std::make_unique<NotificationDispatcherMojo>(
            std::make_unique<MacNotificationProviderFactory>(
                mac_notifications::NotificationStyle::kAppShim, web_app_id));
      });

  return std::make_unique<NotificationPlatformBridgeMac>(
      std::move(banner_dispatcher), std::move(alert_dispatcher),
      std::move(create_dispatcher_for_web_app));
}

// static
bool NotificationPlatformBridge::CanHandleType(
    NotificationHandler::Type notification_type) {
  return notification_type != NotificationHandler::Type::TRANSIENT;
}

void NotificationPlatformBridgeMac::Display(
    NotificationHandler::Type notification_type,
    Profile* profile,
    const message_center::Notification& notification,
    std::unique_ptr<NotificationCommon::Metadata> metadata) {
  NotificationDispatcherMac* dispatcher = nullptr;

  if (web_app::UseNotificationAttributionForWebAppShims() &&
      notification.notifier_id().web_app_id.has_value() &&
      AppShimRegistry::Get()->IsAppInstalledInProfile(
          *notification.notifier_id().web_app_id, profile->GetPath())) {
    dispatcher =
        GetOrCreateDispatcherForWebApp(*notification.notifier_id().web_app_id);
  }

  if (!dispatcher) {
    bool is_alert = IsAlertNotificationMac(notification);
    dispatcher = is_alert ? alert_dispatcher_.get() : banner_dispatcher_.get();
  }
  dispatcher->DisplayNotification(notification_type, profile, notification);
  CloseImpl(profile, notification.id(), dispatcher);
}

void NotificationPlatformBridgeMac::Close(Profile* profile,
                                          const std::string& notification_id) {
  CloseImpl(profile, notification_id);
}

void NotificationPlatformBridgeMac::CloseImpl(
    Profile* profile,
    const std::string& notification_id,
    NotificationDispatcherMac* excluded_dispatcher) {
  std::string profile_id = GetProfileId(profile);
  bool incognito = profile->IsOffTheRecord();

  if (banner_dispatcher_.get() != excluded_dispatcher) {
    banner_dispatcher_->CloseNotificationWithId(
        {notification_id, profile_id, incognito});
  }
  if (alert_dispatcher_.get() != excluded_dispatcher) {
    alert_dispatcher_->CloseNotificationWithId(
        {notification_id, profile_id, incognito});
  }
  for (auto& [app_id, dispatcher] : app_specific_dispatchers_) {
    if (dispatcher.get() == excluded_dispatcher) {
      continue;
    }
    dispatcher->CloseNotificationWithId(
        {notification_id, profile_id, incognito});
  }
}

void NotificationPlatformBridgeMac::GetDisplayed(
    Profile* profile,
    GetDisplayedNotificationsCallback callback) const {
  if (web_app::UseNotificationAttributionForWebAppShims()) {
    // We can't get all displayed notifications for all origins, since that
    // would involve starting up any app shim that might currently show
    // notifications. Fortunately we don't really need to implement this method
    // as this is only used for an initial state sync on Chrome start up. Before
    // for example a list of displayed notifications is returned via the web
    // API, additional calls to get displayed notifications for specific origins
    // happen.
    // TODO(crbug.com/40283098): Figure out how we can refactor the
    // various APIs to make this not be an issue.
    std::move(callback).Run({}, /*supports_synchronization=*/false);
    return;
  }

  std::string profile_id = GetProfileId(profile);
  bool incognito = profile->IsOffTheRecord();

  auto notifications = std::make_unique<std::set<std::string>>();
  std::set<std::string>* notifications_ptr = notifications.get();
  auto barrier_closure = base::BarrierClosure(
      2, base::BindOnce(
             [](std::unique_ptr<std::set<std::string>> notifications,
                GetDisplayedNotificationsCallback callback) {
               std::move(callback).Run(std::move(*notifications),
                                       /*supports_synchronization=*/true);
             },
             std::move(notifications), std::move(callback)));

  auto get_notifications_callback = base::BindRepeating(
      [](std::set<std::string>* notifications_ptr, base::OnceClosure callback,
         std::set<std::string> notifications, bool supports_synchronization) {
        notifications_ptr->insert(notifications.begin(), notifications.end());
        std::move(callback).Run();
      },
      notifications_ptr, barrier_closure);

  banner_dispatcher_->GetDisplayedNotificationsForProfileId(
      profile_id, incognito, get_notifications_callback);
  alert_dispatcher_->GetDisplayedNotificationsForProfileId(
      profile_id, incognito, get_notifications_callback);
}

void NotificationPlatformBridgeMac::GetDisplayedForOrigin(
    Profile* profile,
    const GURL& origin,
    GetDisplayedNotificationsCallback callback) const {
  std::string profile_id = GetProfileId(profile);
  bool incognito = profile->IsOffTheRecord();

  std::vector<webapps::AppId> web_app_ids;
  if (web_app::UseNotificationAttributionForWebAppShims()) {
    if (auto* web_app_provider =
            web_app::WebAppProvider::GetForWebApps(profile)) {
      web_app::WebAppRegistrar& registrar =
          web_app_provider->registrar_unsafe();
      for (const webapps::AppId& app_id : registrar.GetAppIds()) {
        if (!registrar.IsInstallState(
                app_id, {web_app::proto::INSTALLED_WITH_OS_INTEGRATION})) {
          continue;
        }
        if (!url::IsSameOriginWith(registrar.GetAppScope(app_id), origin)) {
          continue;
        }
        web_app_ids.push_back(app_id);
      }
    }
    // TODO(mek): filter by web_app_ids that actually could be displaying
    // notifications rather than all for the origin.
  }

  auto notifications = std::make_unique<std::set<std::string>>();
  std::set<std::string>* notifications_ptr = notifications.get();
  auto barrier_closure = base::BarrierClosure(
      2 + web_app_ids.size(),
      base::BindOnce(
          [](std::unique_ptr<std::set<std::string>> notifications,
             GetDisplayedNotificationsCallback callback) {
            std::move(callback).Run(std::move(*notifications),
                                    /*supports_synchronization=*/true);
          },
          std::move(notifications), std::move(callback)));

  auto get_notifications_callback =
      base::BindRepeating(
          [](std::set<std::string>* notifications_ptr,
             std::set<std::string> notifications,
             bool supports_synchronization) {
            notifications_ptr->insert(notifications.begin(),
                                      notifications.end());
          },
          notifications_ptr)
          .Then(barrier_closure);

  banner_dispatcher_->GetDisplayedNotificationsForProfileIdAndOrigin(
      profile_id, incognito, origin, get_notifications_callback);
  alert_dispatcher_->GetDisplayedNotificationsForProfileIdAndOrigin(
      profile_id, incognito, origin, get_notifications_callback);

  for (const webapps::AppId& web_app_id : web_app_ids) {
    auto* dispatcher = GetOrCreateDispatcherForWebApp(web_app_id);
    dispatcher->GetDisplayedNotificationsForProfileIdAndOrigin(
        profile_id, incognito, origin, get_notifications_callback);
  }
}

void NotificationPlatformBridgeMac::SetReadyCallback(
    NotificationBridgeReadyCallback callback) {
  std::move(callback).Run(true);
}

void NotificationPlatformBridgeMac::DisplayServiceShutDown(Profile* profile) {
  // Close all alerts and banners for |profile| on shutdown. We have to clean up
  // here instead of the destructor as mojo messages won't be delivered from
  // there as it's too late in the shutdown process. If the profile is null it
  // was the SystemNotificationHelper instance but we never show notifications
  // without a profile (Type::TRANSIENT) on macOS, so nothing to do here.
  if (profile)
    CloseAllNotificationsForProfile(profile);
}

void NotificationPlatformBridgeMac::AppShimWillTerminate(
    const webapps::AppId& web_app_id) {
  auto it = app_specific_dispatchers_.find(web_app_id);
  if (it == app_specific_dispatchers_.end()) {
    return;
  }
  it->second->UserInitiatedShutdown();
}

void NotificationPlatformBridgeMac::CloseAllNotificationsForProfile(
    Profile* profile) {
  DCHECK(profile);
  std::string profile_id = GetProfileId(profile);
  bool incognito = profile->IsOffTheRecord();

  banner_dispatcher_->CloseNotificationsWithProfileId(profile_id, incognito);
  alert_dispatcher_->CloseNotificationsWithProfileId(profile_id, incognito);
}

NotificationDispatcherMac*
NotificationPlatformBridgeMac::GetOrCreateDispatcherForWebApp(
    const webapps::AppId& web_app_id) const {
  auto& owned_dispatcher = app_specific_dispatchers_[web_app_id];
  if (!owned_dispatcher) {
    owned_dispatcher = web_app_dispatcher_factory_.Run(web_app_id);
  }
  return owned_dispatcher.get();
}