1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/send_tab_to_self/desktop_notification_handler.h"
6 
7 #include <string>
8 #include <utility>
9 
10 #include "base/guid.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/notifications/notification_display_service.h"
13 #include "chrome/browser/notifications/notification_display_service_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sync/send_tab_to_self_sync_service_factory.h"
16 #include "chrome/browser/ui/browser_navigator.h"
17 #include "chrome/browser/ui/browser_navigator_params.h"
18 #include "components/send_tab_to_self/send_tab_to_self_entry.h"
19 #include "components/send_tab_to_self/send_tab_to_self_model.h"
20 #include "components/send_tab_to_self/send_tab_to_self_sync_service.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/page_transition_types.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/gfx/image/image.h"
25 #include "ui/message_center/public/cpp/notification.h"
26 #include "ui/strings/grit/ui_strings.h"
27 
28 namespace send_tab_to_self {
29 
30 namespace {
31 
32 const char kDesktopNotificationSharedPrefix[] = "shared";
33 
34 }  // namespace
35 
DesktopNotificationHandler(Profile * profile)36 DesktopNotificationHandler::DesktopNotificationHandler(Profile* profile)
37     : profile_(profile) {}
38 
39 DesktopNotificationHandler::~DesktopNotificationHandler() = default;
40 
DisplayNewEntries(const std::vector<const SendTabToSelfEntry * > & new_entries)41 void DesktopNotificationHandler::DisplayNewEntries(
42     const std::vector<const SendTabToSelfEntry*>& new_entries) {
43   for (const SendTabToSelfEntry* entry : new_entries) {
44     const base::string16 device_info = l10n_util::GetStringFUTF16(
45         IDS_MESSAGE_NOTIFICATION_SEND_TAB_TO_SELF_DEVICE_INFO,
46         base::UTF8ToUTF16(entry->GetDeviceName()));
47     const GURL& url = entry->GetURL();
48     message_center::RichNotificationData optional_fields;
49     // Set the notification to be persistent
50     optional_fields.never_timeout = true;
51     // Declare a notification
52     message_center::Notification notification(
53         message_center::NOTIFICATION_TYPE_SIMPLE, entry->GetGUID(),
54         base::UTF8ToUTF16(entry->GetTitle()), device_info, gfx::Image(),
55         base::UTF8ToUTF16(url.host()), url, message_center::NotifierId(url),
56         optional_fields, /*delegate=*/nullptr);
57     NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
58         NotificationHandler::Type::SEND_TAB_TO_SELF, notification,
59         /*metadata=*/nullptr);
60   }
61 }
62 
DismissEntries(const std::vector<std::string> & guids)63 void DesktopNotificationHandler::DismissEntries(
64     const std::vector<std::string>& guids) {
65   for (const std::string& guid : guids) {
66     NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
67         NotificationHandler::Type::SEND_TAB_TO_SELF, guid);
68   }
69 }
70 
OnClose(Profile * profile,const GURL & origin,const std::string & notification_id,bool by_user,base::OnceClosure completed_closure)71 void DesktopNotificationHandler::OnClose(Profile* profile,
72                                          const GURL& origin,
73                                          const std::string& notification_id,
74                                          bool by_user,
75                                          base::OnceClosure completed_closure) {
76   if (notification_id.find(kDesktopNotificationSharedPrefix)) {
77     SendTabToSelfSyncServiceFactory::GetForProfile(profile)
78         ->GetSendTabToSelfModel()
79         ->DismissEntry(notification_id);
80   }
81   std::move(completed_closure).Run();
82 }
83 
OnClick(Profile * profile,const GURL & origin,const std::string & notification_id,const base::Optional<int> & action_index,const base::Optional<base::string16> & reply,base::OnceClosure completed_closure)84 void DesktopNotificationHandler::OnClick(
85     Profile* profile,
86     const GURL& origin,
87     const std::string& notification_id,
88     const base::Optional<int>& action_index,
89     const base::Optional<base::string16>& reply,
90     base::OnceClosure completed_closure) {
91   if (notification_id.find(kDesktopNotificationSharedPrefix)) {
92     // Launch a new tab for the notification's |origin|,
93     // and close the activated notification.
94     NavigateParams params(profile, origin, ui::PAGE_TRANSITION_LINK);
95     params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
96     params.window_action = NavigateParams::SHOW_WINDOW;
97     Navigate(&params);
98     NotificationDisplayServiceFactory::GetForProfile(profile)->Close(
99         NotificationHandler::Type::SEND_TAB_TO_SELF, notification_id);
100     // Marks the the entry as opened in SendTabToSelfModel
101     SendTabToSelfSyncServiceFactory::GetForProfile(profile)
102         ->GetSendTabToSelfModel()
103         ->MarkEntryOpened(notification_id);
104   }
105   std::move(completed_closure).Run();
106 }
107 
DisplaySendingConfirmation(const SendTabToSelfEntry & entry,const std::string & target_device_name)108 void DesktopNotificationHandler::DisplaySendingConfirmation(
109     const SendTabToSelfEntry& entry,
110     const std::string& target_device_name) {
111   const base::string16 confirm_str = l10n_util::GetStringFUTF16(
112       IDS_MESSAGE_NOTIFICATION_SEND_TAB_TO_SELF_CONFIRMATION_SUCCESS,
113       base::UTF8ToUTF16(target_device_name));
114   const GURL& url = entry.GetURL();
115   message_center::Notification notification(
116       message_center::NOTIFICATION_TYPE_SIMPLE,
117       kDesktopNotificationSharedPrefix + entry.GetGUID(), confirm_str,
118       base::UTF8ToUTF16(entry.GetTitle()), gfx::Image(),
119       base::UTF8ToUTF16(url.host()), url, message_center::NotifierId(url),
120       message_center::RichNotificationData(), /*delegate=*/nullptr);
121   NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
122       NotificationHandler::Type::SEND_TAB_TO_SELF, notification,
123       /*metadata=*/nullptr);
124 }
125 
DisplayFailureMessage(const GURL & url)126 void DesktopNotificationHandler::DisplayFailureMessage(const GURL& url) {
127   message_center::Notification notification(
128       message_center::NOTIFICATION_TYPE_SIMPLE,
129       kDesktopNotificationSharedPrefix + base::GenerateGUID(),
130       l10n_util::GetStringUTF16(
131           IDS_MESSAGE_NOTIFICATION_SEND_TAB_TO_SELF_CONFIRMATION_FAILURE_TITLE),
132       l10n_util::GetStringUTF16(
133           IDS_MESSAGE_NOTIFICATION_SEND_TAB_TO_SELF_CONFIRMATION_FAILURE_MESSAGE),
134       gfx::Image(), base::UTF8ToUTF16(url.host()), url,
135       message_center::NotifierId(url), message_center::RichNotificationData(),
136       /*delegate=*/nullptr);
137   NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
138       NotificationHandler::Type::SEND_TAB_TO_SELF, notification,
139       /*metadata=*/nullptr);
140 }
141 
GetProfile() const142 const Profile* DesktopNotificationHandler::GetProfile() const {
143   return profile_;
144 }
145 
146 }  // namespace send_tab_to_self
147