File: desktop_notification_handler.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 (147 lines) | stat: -rw-r--r-- 6,309 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 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/send_tab_to_self/desktop_notification_handler.h"

#include <string>
#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "base/uuid.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/sync/send_tab_to_self_sync_service_factory.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "components/send_tab_to_self/metrics_util.h"
#include "components/send_tab_to_self/send_tab_to_self_entry.h"
#include "components/send_tab_to_self/send_tab_to_self_model.h"
#include "components/send_tab_to_self/send_tab_to_self_sync_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/strings/grit/ui_strings.h"

namespace send_tab_to_self {

namespace {

const char kDesktopNotificationSharedPrefix[] = "shared";

}  // namespace

DesktopNotificationHandler::DesktopNotificationHandler(Profile* profile)
    : profile_(profile) {}

DesktopNotificationHandler::~DesktopNotificationHandler() = default;

void DesktopNotificationHandler::DisplayNewEntries(
    const std::vector<const SendTabToSelfEntry*>& new_entries) {
  for (const SendTabToSelfEntry* entry : new_entries) {
    const std::u16string device_info = l10n_util::GetStringFUTF16(
        IDS_MESSAGE_NOTIFICATION_SEND_TAB_TO_SELF_DEVICE_INFO,
        base::UTF8ToUTF16(entry->GetDeviceName()));
    const GURL& url = entry->GetURL();
    message_center::RichNotificationData optional_fields;
    // Set the notification to be persistent
    optional_fields.never_timeout = true;
    // Declare a notification
    message_center::Notification notification(
        message_center::NOTIFICATION_TYPE_SIMPLE, entry->GetGUID(),
        base::UTF8ToUTF16(entry->GetTitle()), device_info, ui::ImageModel(),
        base::UTF8ToUTF16(url.host()), url, message_center::NotifierId(url),
        optional_fields, /*delegate=*/nullptr);
    NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
        NotificationHandler::Type::SEND_TAB_TO_SELF, notification,
        /*metadata=*/nullptr);
  }
}

void DesktopNotificationHandler::DismissEntries(
    const std::vector<std::string>& guids) {
  for (const std::string& guid : guids) {
    NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
        NotificationHandler::Type::SEND_TAB_TO_SELF, guid);
  }
}

void DesktopNotificationHandler::OnClose(Profile* profile,
                                         const GURL& origin,
                                         const std::string& notification_id,
                                         bool by_user,
                                         base::OnceClosure completed_closure) {
  if (notification_id.find(kDesktopNotificationSharedPrefix)) {
    SendTabToSelfSyncServiceFactory::GetForProfile(profile)
        ->GetSendTabToSelfModel()
        ->DismissEntry(notification_id);
    send_tab_to_self::RecordNotificationDismissed();
  }
  std::move(completed_closure).Run();
}

void DesktopNotificationHandler::OnClick(
    Profile* profile,
    const GURL& origin,
    const std::string& notification_id,
    const std::optional<int>& action_index,
    const std::optional<std::u16string>& reply,
    base::OnceClosure completed_closure) {
  if (notification_id.find(kDesktopNotificationSharedPrefix)) {
    // Launch a new tab for the notification's |origin|,
    // and close the activated notification.
    NavigateParams params(profile, origin, ui::PAGE_TRANSITION_LINK);
    params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
    params.window_action = NavigateParams::SHOW_WINDOW;
    Navigate(&params);
    NotificationDisplayServiceFactory::GetForProfile(profile)->Close(
        NotificationHandler::Type::SEND_TAB_TO_SELF, notification_id);

    // Marks the the entry as opened in SendTabToSelfModel
    SendTabToSelfSyncServiceFactory::GetForProfile(profile)
        ->GetSendTabToSelfModel()
        ->MarkEntryOpened(notification_id);
    send_tab_to_self::RecordNotificationOpened();
  }
  std::move(completed_closure).Run();
}

void DesktopNotificationHandler::DisplaySendingConfirmation(
    const SendTabToSelfEntry& entry,
    const std::string& target_device_name) {
  const std::u16string confirm_str = l10n_util::GetStringFUTF16(
      IDS_MESSAGE_NOTIFICATION_SEND_TAB_TO_SELF_CONFIRMATION_SUCCESS,
      base::UTF8ToUTF16(target_device_name));
  const GURL& url = entry.GetURL();
  message_center::Notification notification(
      message_center::NOTIFICATION_TYPE_SIMPLE,
      kDesktopNotificationSharedPrefix + entry.GetGUID(), confirm_str,
      base::UTF8ToUTF16(entry.GetTitle()), ui::ImageModel(),
      base::UTF8ToUTF16(url.host()), url, message_center::NotifierId(url),
      message_center::RichNotificationData(), /*delegate=*/nullptr);
  NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
      NotificationHandler::Type::SEND_TAB_TO_SELF, notification,
      /*metadata=*/nullptr);
}

void DesktopNotificationHandler::DisplayFailureMessage(const GURL& url) {
  message_center::Notification notification(
      message_center::NOTIFICATION_TYPE_SIMPLE,
      kDesktopNotificationSharedPrefix +
          base::Uuid::GenerateRandomV4().AsLowercaseString(),
      l10n_util::GetStringUTF16(
          IDS_MESSAGE_NOTIFICATION_SEND_TAB_TO_SELF_CONFIRMATION_FAILURE_TITLE),
      l10n_util::GetStringUTF16(
          IDS_MESSAGE_NOTIFICATION_SEND_TAB_TO_SELF_CONFIRMATION_FAILURE_MESSAGE),
      ui::ImageModel(), base::UTF8ToUTF16(url.host()), url,
      message_center::NotifierId(url), message_center::RichNotificationData(),
      /*delegate=*/nullptr);
  NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
      NotificationHandler::Type::SEND_TAB_TO_SELF, notification,
      /*metadata=*/nullptr);
}

}  // namespace send_tab_to_self