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 "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/strings/nullable_string16.h" | 8 #include "base/strings/nullable_string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/notifications/message_center_display_service.h" | |
10 #include "chrome/browser/notifications/non_persistent_notification_handler.h" | 12 #include "chrome/browser/notifications/non_persistent_notification_handler.h" |
11 #include "chrome/browser/notifications/notification.h" | 13 #include "chrome/browser/notifications/notification.h" |
12 #include "chrome/browser/notifications/notification_delegate.h" | 14 #include "chrome/browser/notifications/notification_delegate.h" |
13 #include "chrome/browser/notifications/notification_handler.h" | 15 #include "chrome/browser/notifications/notification_handler.h" |
14 #include "chrome/browser/notifications/notification_platform_bridge.h" | 16 #include "chrome/browser/notifications/notification_platform_bridge.h" |
15 #include "chrome/browser/notifications/persistent_notification_handler.h" | 17 #include "chrome/browser/notifications/persistent_notification_handler.h" |
16 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/common/features.h" | |
Peter Beverloo
2017/04/25 15:16:55
Unused?
Tom (Use chromium acct)
2017/04/25 18:35:20
Used for ENABLE_NATIVE_NOTIFICATIONS
Edit: and re
| |
17 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
18 #include "extensions/features/features.h" | 21 #include "extensions/features/features.h" |
19 | 22 |
20 #if BUILDFLAG(ENABLE_EXTENSIONS) | 23 #if BUILDFLAG(ENABLE_EXTENSIONS) |
21 #include "chrome/browser/extensions/api/notifications/extension_notification_han dler.h" | 24 #include "chrome/browser/extensions/api/notifications/extension_notification_han dler.h" |
22 #endif | 25 #endif |
23 | 26 |
27 #if BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) && defined(OS_LINUX) | |
28 #include "chrome/browser/notifications/notification_platform_bridge_linux.h" | |
29 #endif | |
30 | |
24 namespace { | 31 namespace { |
25 | 32 |
26 std::string GetProfileId(Profile* profile) { | 33 std::string GetProfileId(Profile* profile) { |
27 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
28 std::string profile_id = | 35 std::string profile_id = |
29 base::WideToUTF8(profile->GetPath().BaseName().value()); | 36 base::WideToUTF8(profile->GetPath().BaseName().value()); |
30 #elif defined(OS_POSIX) | 37 #elif defined(OS_POSIX) |
31 std::string profile_id = profile->GetPath().BaseName().value(); | 38 std::string profile_id = profile->GetPath().BaseName().value(); |
32 #endif | 39 #endif |
33 return profile_id; | 40 return profile_id; |
34 } | 41 } |
35 | 42 |
36 } // namespace | 43 } // namespace |
37 | 44 |
38 NativeNotificationDisplayService::NativeNotificationDisplayService( | 45 NativeNotificationDisplayService::NativeNotificationDisplayService( |
39 Profile* profile, | 46 Profile* profile, |
40 NotificationPlatformBridge* notification_bridge) | 47 NotificationPlatformBridge* notification_bridge) |
41 : profile_(profile), notification_bridge_(notification_bridge) { | 48 : profile_(profile), |
49 notification_bridge_(notification_bridge), | |
50 weak_factory_(this) { | |
42 DCHECK(profile_); | 51 DCHECK(profile_); |
43 DCHECK(notification_bridge_); | 52 DCHECK(notification_bridge_); |
44 | 53 |
54 #if BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS) && defined(OS_LINUX) | |
55 auto* platform_bridge_linux = static_cast<NotificationPlatformBridgeLinux*>( | |
56 g_browser_process->notification_platform_bridge()); | |
57 platform_bridge_linux->CheckConnection( | |
58 base::BindOnce(&NativeNotificationDisplayService:: | |
59 OnNotificationPlatformBridgeInitialized, | |
60 weak_factory_.GetWeakPtr())); | |
61 notification_bridge_connected_ = false; | |
Peter Beverloo
2017/04/25 15:16:55
We can avoid the compile-time ifdef by adding a ne
Tom (Use chromium acct)
2017/04/25 18:35:20
Done.
| |
62 #else | |
63 notification_bridge_connected_ = true; | |
64 #endif | |
65 | |
45 AddNotificationHandler(NotificationCommon::NON_PERSISTENT, | 66 AddNotificationHandler(NotificationCommon::NON_PERSISTENT, |
46 base::MakeUnique<NonPersistentNotificationHandler>()); | 67 base::MakeUnique<NonPersistentNotificationHandler>()); |
47 AddNotificationHandler(NotificationCommon::PERSISTENT, | 68 AddNotificationHandler(NotificationCommon::PERSISTENT, |
48 base::MakeUnique<PersistentNotificationHandler>()); | 69 base::MakeUnique<PersistentNotificationHandler>()); |
49 #if BUILDFLAG(ENABLE_EXTENSIONS) | 70 #if BUILDFLAG(ENABLE_EXTENSIONS) |
50 AddNotificationHandler(NotificationCommon::EXTENSION, | 71 AddNotificationHandler(NotificationCommon::EXTENSION, |
51 base::MakeUnique<ExtensionNotificationHandler>()); | 72 base::MakeUnique<ExtensionNotificationHandler>()); |
52 #endif | 73 #endif |
53 } | 74 } |
54 | 75 |
55 NativeNotificationDisplayService::~NativeNotificationDisplayService() {} | 76 NativeNotificationDisplayService::~NativeNotificationDisplayService() {} |
56 | 77 |
78 void NativeNotificationDisplayService::OnNotificationPlatformBridgeInitialized( | |
79 bool success) { | |
80 if (success) { | |
81 notification_bridge_connected_ = true; | |
82 } else { | |
83 message_center_display_service_ = | |
84 base::MakeUnique<MessageCenterDisplayService>( | |
85 profile_, g_browser_process->notification_ui_manager()); | |
86 } | |
87 | |
88 while (!actions_.empty()) { | |
89 actions_.front().Run(); | |
90 actions_.pop(); | |
91 } | |
92 } | |
93 | |
57 void NativeNotificationDisplayService::Display( | 94 void NativeNotificationDisplayService::Display( |
58 NotificationCommon::Type notification_type, | 95 NotificationCommon::Type notification_type, |
59 const std::string& notification_id, | 96 const std::string& notification_id, |
60 const Notification& notification) { | 97 const Notification& notification) { |
61 notification_bridge_->Display(notification_type, notification_id, | 98 if (notification_bridge_connected_) { |
62 GetProfileId(profile_), | 99 notification_bridge_->Display(notification_type, notification_id, |
63 profile_->IsOffTheRecord(), notification); | 100 GetProfileId(profile_), |
64 notification.delegate()->Display(); | 101 profile_->IsOffTheRecord(), notification); |
65 NotificationHandler* handler = GetNotificationHandler(notification_type); | 102 notification.delegate()->Display(); |
66 handler->RegisterNotification(notification_id, notification.delegate()); | 103 NotificationHandler* handler = GetNotificationHandler(notification_type); |
104 handler->RegisterNotification(notification_id, notification.delegate()); | |
105 } else if (message_center_display_service_) { | |
106 message_center_display_service_->Display(notification_type, notification_id, | |
107 notification); | |
108 } else { | |
109 actions_.push(base::Bind(&NativeNotificationDisplayService::Display, | |
110 weak_factory_.GetWeakPtr(), notification_type, | |
111 notification_id, notification)); | |
112 } | |
67 } | 113 } |
68 | 114 |
69 void NativeNotificationDisplayService::Close( | 115 void NativeNotificationDisplayService::Close( |
70 NotificationCommon::Type notification_type, | 116 NotificationCommon::Type notification_type, |
71 const std::string& notification_id) { | 117 const std::string& notification_id) { |
72 NotificationHandler* handler = GetNotificationHandler(notification_type); | 118 if (notification_bridge_connected_) { |
73 notification_bridge_->Close(GetProfileId(profile_), notification_id); | 119 NotificationHandler* handler = GetNotificationHandler(notification_type); |
120 notification_bridge_->Close(GetProfileId(profile_), notification_id); | |
74 | 121 |
75 // TODO(miguelg): Figure out something better here, passing an empty | 122 // TODO(miguelg): Figure out something better here, passing an empty |
76 // origin works because only non persistent notifications care about | 123 // origin works because only non persistent notifications care about |
77 // this method for JS generated close calls and they don't require | 124 // this method for JS generated close calls and they don't require |
78 // the origin. | 125 // the origin. |
79 handler->OnClose(profile_, "", notification_id, false /* by user */); | 126 handler->OnClose(profile_, "", notification_id, false /* by user */); |
127 } else if (message_center_display_service_) { | |
128 message_center_display_service_->Close(notification_type, notification_id); | |
129 } else { | |
130 actions_.push(base::Bind(&NativeNotificationDisplayService::Close, | |
131 weak_factory_.GetWeakPtr(), notification_type, | |
132 notification_id)); | |
133 } | |
80 } | 134 } |
81 | 135 |
82 void NativeNotificationDisplayService::GetDisplayed( | 136 void NativeNotificationDisplayService::GetDisplayed( |
83 const DisplayedNotificationsCallback& callback) { | 137 const DisplayedNotificationsCallback& callback) { |
84 return notification_bridge_->GetDisplayed( | 138 if (notification_bridge_connected_) { |
85 GetProfileId(profile_), profile_->IsOffTheRecord(), callback); | 139 return notification_bridge_->GetDisplayed( |
140 GetProfileId(profile_), profile_->IsOffTheRecord(), callback); | |
141 } else if (message_center_display_service_) { | |
142 message_center_display_service_->GetDisplayed(callback); | |
143 } else { | |
144 actions_.push(base::Bind(&NativeNotificationDisplayService::GetDisplayed, | |
145 weak_factory_.GetWeakPtr(), callback)); | |
146 } | |
86 } | 147 } |
87 | 148 |
88 void NativeNotificationDisplayService::ProcessNotificationOperation( | 149 void NativeNotificationDisplayService::ProcessNotificationOperation( |
89 NotificationCommon::Operation operation, | 150 NotificationCommon::Operation operation, |
90 NotificationCommon::Type notification_type, | 151 NotificationCommon::Type notification_type, |
91 const std::string& origin, | 152 const std::string& origin, |
92 const std::string& notification_id, | 153 const std::string& notification_id, |
93 int action_index, | 154 int action_index, |
94 const base::NullableString16& reply) { | 155 const base::NullableString16& reply) { |
95 NotificationHandler* handler = GetNotificationHandler(notification_type); | 156 NotificationHandler* handler = GetNotificationHandler(notification_type); |
(...skipping 24 matching lines...) Expand all Loading... | |
120 notification_handlers_.erase(notification_type); | 181 notification_handlers_.erase(notification_type); |
121 } | 182 } |
122 | 183 |
123 NotificationHandler* NativeNotificationDisplayService::GetNotificationHandler( | 184 NotificationHandler* NativeNotificationDisplayService::GetNotificationHandler( |
124 NotificationCommon::Type notification_type) { | 185 NotificationCommon::Type notification_type) { |
125 DCHECK(notification_handlers_.find(notification_type) != | 186 DCHECK(notification_handlers_.find(notification_type) != |
126 notification_handlers_.end()) | 187 notification_handlers_.end()) |
127 << notification_type << " is not registered."; | 188 << notification_type << " is not registered."; |
128 return notification_handlers_[notification_type].get(); | 189 return notification_handlers_[notification_type].get(); |
129 } | 190 } |
OLD | NEW |