OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/notifications/native_notification_display_service.h" | 5 #include "chrome/browser/notifications/native_notification_display_service.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/nullable_string16.h" | |
13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/notifications/message_center_display_service.h" | 14 #include "chrome/browser/notifications/message_center_display_service.h" |
16 #include "chrome/browser/notifications/non_persistent_notification_handler.h" | |
17 #include "chrome/browser/notifications/notification.h" | 15 #include "chrome/browser/notifications/notification.h" |
18 #include "chrome/browser/notifications/notification_delegate.h" | 16 #include "chrome/browser/notifications/notification_delegate.h" |
19 #include "chrome/browser/notifications/notification_handler.h" | 17 #include "chrome/browser/notifications/notification_handler.h" |
20 #include "chrome/browser/notifications/notification_platform_bridge.h" | 18 #include "chrome/browser/notifications/notification_platform_bridge.h" |
21 #include "chrome/browser/notifications/persistent_notification_handler.h" | |
22 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
23 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/notification_event_dispatcher.h" | 21 #include "content/public/browser/notification_event_dispatcher.h" |
25 #include "extensions/features/features.h" | |
26 | |
27 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
28 #include "chrome/browser/extensions/api/notifications/extension_notification_han
dler.h" | |
29 #endif | |
30 | 22 |
31 namespace { | 23 namespace { |
32 | 24 |
33 std::string GetProfileId(Profile* profile) { | 25 std::string GetProfileId(Profile* profile) { |
34 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
35 std::string profile_id = | 27 std::string profile_id = |
36 base::WideToUTF8(profile->GetPath().BaseName().value()); | 28 base::WideToUTF8(profile->GetPath().BaseName().value()); |
37 #elif defined(OS_POSIX) | 29 #elif defined(OS_POSIX) |
38 std::string profile_id = profile->GetPath().BaseName().value(); | 30 std::string profile_id = profile->GetPath().BaseName().value(); |
39 #endif | 31 #endif |
40 return profile_id; | 32 return profile_id; |
41 } | 33 } |
42 | 34 |
43 } // namespace | 35 } // namespace |
44 | 36 |
45 NativeNotificationDisplayService::NativeNotificationDisplayService( | 37 NativeNotificationDisplayService::NativeNotificationDisplayService( |
46 Profile* profile, | 38 Profile* profile, |
47 NotificationPlatformBridge* notification_bridge) | 39 NotificationPlatformBridge* notification_bridge) |
48 : profile_(profile), | 40 : NotificationDisplayService(profile), |
| 41 profile_(profile), |
49 notification_bridge_(notification_bridge), | 42 notification_bridge_(notification_bridge), |
50 notification_bridge_ready_(false), | 43 notification_bridge_ready_(false), |
51 weak_factory_(this) { | 44 weak_factory_(this) { |
52 DCHECK(profile_); | 45 DCHECK(profile_); |
53 DCHECK(notification_bridge_); | 46 DCHECK(notification_bridge_); |
54 | 47 |
55 notification_bridge->SetReadyCallback(base::BindOnce( | 48 notification_bridge->SetReadyCallback(base::BindOnce( |
56 &NativeNotificationDisplayService::OnNotificationPlatformBridgeReady, | 49 &NativeNotificationDisplayService::OnNotificationPlatformBridgeReady, |
57 weak_factory_.GetWeakPtr())); | 50 weak_factory_.GetWeakPtr())); |
58 | |
59 AddNotificationHandler(NotificationCommon::NON_PERSISTENT, | |
60 base::MakeUnique<NonPersistentNotificationHandler>()); | |
61 AddNotificationHandler(NotificationCommon::PERSISTENT, | |
62 base::MakeUnique<PersistentNotificationHandler>()); | |
63 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
64 AddNotificationHandler( | |
65 NotificationCommon::EXTENSION, | |
66 base::MakeUnique<extensions::ExtensionNotificationHandler>()); | |
67 #endif | |
68 } | 51 } |
69 | 52 |
70 NativeNotificationDisplayService::~NativeNotificationDisplayService() = default; | 53 NativeNotificationDisplayService::~NativeNotificationDisplayService() = default; |
71 | 54 |
72 void NativeNotificationDisplayService::OnNotificationPlatformBridgeReady( | 55 void NativeNotificationDisplayService::OnNotificationPlatformBridgeReady( |
73 bool success) { | 56 bool success) { |
74 UMA_HISTOGRAM_BOOLEAN("Notifications.UsingNativeNotificationCenter", success); | 57 UMA_HISTOGRAM_BOOLEAN("Notifications.UsingNativeNotificationCenter", success); |
75 if (success) { | 58 if (success) { |
76 notification_bridge_ready_ = true; | 59 notification_bridge_ready_ = true; |
77 } else { | 60 } else { |
78 message_center_display_service_ = | 61 message_center_display_service_ = |
79 base::MakeUnique<MessageCenterDisplayService>( | 62 base::MakeUnique<MessageCenterDisplayService>( |
80 profile_, g_browser_process->notification_ui_manager()); | 63 profile_, g_browser_process->notification_ui_manager()); |
81 } | 64 } |
82 | 65 |
83 while (!actions_.empty()) { | 66 while (!actions_.empty()) { |
84 std::move(actions_.front()).Run(); | 67 std::move(actions_.front()).Run(); |
85 actions_.pop(); | 68 actions_.pop(); |
86 } | 69 } |
87 } | 70 } |
88 | 71 |
89 void NativeNotificationDisplayService::Display( | 72 void NativeNotificationDisplayService::Display( |
90 NotificationCommon::Type notification_type, | 73 NotificationCommon::Type notification_type, |
91 const std::string& notification_id, | 74 const std::string& notification_id, |
92 const Notification& notification) { | 75 const Notification& notification) { |
93 if (notification_bridge_ready_) { | 76 if (notification_bridge_ready_) { |
94 notification_bridge_->Display(notification_type, notification_id, | 77 notification_bridge_->Display(notification_type, notification_id, |
95 GetProfileId(profile_), | 78 GetProfileId(profile_), |
96 profile_->IsOffTheRecord(), notification); | 79 profile_->IsOffTheRecord(), notification); |
97 // Unlike all other notifications non persistent notifications require | 80 NotificationHandler* handler = GetNotificationHandler(notification_type); |
98 // an event after the notification has been displayed. | 81 handler->OnShow(profile_, notification_id); |
99 // TODO(miguelg) create an OnShow notification handler instead. | |
100 if (notification_type == NotificationCommon::NON_PERSISTENT) { | |
101 content::NotificationEventDispatcher::GetInstance() | |
102 ->DispatchNonPersistentShowEvent(notification_id); | |
103 } | |
104 } else if (message_center_display_service_) { | 82 } else if (message_center_display_service_) { |
105 message_center_display_service_->Display(notification_type, notification_id, | 83 message_center_display_service_->Display(notification_type, notification_id, |
106 notification); | 84 notification); |
107 } else { | 85 } else { |
108 actions_.push(base::BindOnce(&NativeNotificationDisplayService::Display, | 86 actions_.push(base::BindOnce(&NativeNotificationDisplayService::Display, |
109 weak_factory_.GetWeakPtr(), notification_type, | 87 weak_factory_.GetWeakPtr(), notification_type, |
110 notification_id, notification)); | 88 notification_id, notification)); |
111 } | 89 } |
112 } | 90 } |
113 | 91 |
(...skipping 24 matching lines...) Expand all Loading... |
138 return notification_bridge_->GetDisplayed( | 116 return notification_bridge_->GetDisplayed( |
139 GetProfileId(profile_), profile_->IsOffTheRecord(), callback); | 117 GetProfileId(profile_), profile_->IsOffTheRecord(), callback); |
140 } else if (message_center_display_service_) { | 118 } else if (message_center_display_service_) { |
141 message_center_display_service_->GetDisplayed(callback); | 119 message_center_display_service_->GetDisplayed(callback); |
142 } else { | 120 } else { |
143 actions_.push( | 121 actions_.push( |
144 base::BindOnce(&NativeNotificationDisplayService::GetDisplayed, | 122 base::BindOnce(&NativeNotificationDisplayService::GetDisplayed, |
145 weak_factory_.GetWeakPtr(), callback)); | 123 weak_factory_.GetWeakPtr(), callback)); |
146 } | 124 } |
147 } | 125 } |
148 | |
149 void NativeNotificationDisplayService::ProcessNotificationOperation( | |
150 NotificationCommon::Operation operation, | |
151 NotificationCommon::Type notification_type, | |
152 const std::string& origin, | |
153 const std::string& notification_id, | |
154 int action_index, | |
155 const base::NullableString16& reply) { | |
156 NotificationHandler* handler = GetNotificationHandler(notification_type); | |
157 CHECK(handler); | |
158 switch (operation) { | |
159 case NotificationCommon::CLICK: | |
160 handler->OnClick(profile_, origin, notification_id, action_index, reply); | |
161 break; | |
162 case NotificationCommon::CLOSE: | |
163 handler->OnClose(profile_, origin, notification_id, true /* by_user */); | |
164 break; | |
165 case NotificationCommon::SETTINGS: | |
166 handler->OpenSettings(profile_); | |
167 break; | |
168 } | |
169 } | |
170 | |
171 void NativeNotificationDisplayService::AddNotificationHandler( | |
172 NotificationCommon::Type notification_type, | |
173 std::unique_ptr<NotificationHandler> handler) { | |
174 DCHECK(handler); | |
175 DCHECK_EQ(notification_handlers_.count(notification_type), 0u); | |
176 notification_handlers_[notification_type] = std::move(handler); | |
177 } | |
178 | |
179 NotificationHandler* NativeNotificationDisplayService::GetNotificationHandler( | |
180 NotificationCommon::Type notification_type) { | |
181 DCHECK(notification_handlers_.find(notification_type) != | |
182 notification_handlers_.end()) | |
183 << notification_type << " is not registered."; | |
184 return notification_handlers_[notification_type].get(); | |
185 } | |
OLD | NEW |