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/bind.h" |
7 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
8 #include "base/strings/nullable_string16.h" | 9 #include "base/strings/nullable_string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/notifications/message_center_display_service.h" |
10 #include "chrome/browser/notifications/non_persistent_notification_handler.h" | 13 #include "chrome/browser/notifications/non_persistent_notification_handler.h" |
11 #include "chrome/browser/notifications/notification.h" | 14 #include "chrome/browser/notifications/notification.h" |
12 #include "chrome/browser/notifications/notification_delegate.h" | 15 #include "chrome/browser/notifications/notification_delegate.h" |
13 #include "chrome/browser/notifications/notification_handler.h" | 16 #include "chrome/browser/notifications/notification_handler.h" |
14 #include "chrome/browser/notifications/notification_platform_bridge.h" | 17 #include "chrome/browser/notifications/notification_platform_bridge.h" |
15 #include "chrome/browser/notifications/persistent_notification_handler.h" | 18 #include "chrome/browser/notifications/persistent_notification_handler.h" |
16 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
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 |
(...skipping 11 matching lines...) Expand all Loading... |
31 std::string profile_id = profile->GetPath().BaseName().value(); | 34 std::string profile_id = profile->GetPath().BaseName().value(); |
32 #endif | 35 #endif |
33 return profile_id; | 36 return profile_id; |
34 } | 37 } |
35 | 38 |
36 } // namespace | 39 } // namespace |
37 | 40 |
38 NativeNotificationDisplayService::NativeNotificationDisplayService( | 41 NativeNotificationDisplayService::NativeNotificationDisplayService( |
39 Profile* profile, | 42 Profile* profile, |
40 NotificationPlatformBridge* notification_bridge) | 43 NotificationPlatformBridge* notification_bridge) |
41 : profile_(profile), notification_bridge_(notification_bridge) { | 44 : profile_(profile), |
| 45 notification_bridge_(notification_bridge), |
| 46 notification_bridge_ready_(false), |
| 47 weak_factory_(this) { |
42 DCHECK(profile_); | 48 DCHECK(profile_); |
43 DCHECK(notification_bridge_); | 49 DCHECK(notification_bridge_); |
44 | 50 |
| 51 notification_bridge->SetReadyCallback(base::BindOnce( |
| 52 &NativeNotificationDisplayService::OnNotificationPlatformBridgeReady, |
| 53 weak_factory_.GetWeakPtr())); |
| 54 |
45 AddNotificationHandler(NotificationCommon::NON_PERSISTENT, | 55 AddNotificationHandler(NotificationCommon::NON_PERSISTENT, |
46 base::MakeUnique<NonPersistentNotificationHandler>()); | 56 base::MakeUnique<NonPersistentNotificationHandler>()); |
47 AddNotificationHandler(NotificationCommon::PERSISTENT, | 57 AddNotificationHandler(NotificationCommon::PERSISTENT, |
48 base::MakeUnique<PersistentNotificationHandler>()); | 58 base::MakeUnique<PersistentNotificationHandler>()); |
49 #if BUILDFLAG(ENABLE_EXTENSIONS) | 59 #if BUILDFLAG(ENABLE_EXTENSIONS) |
50 AddNotificationHandler(NotificationCommon::EXTENSION, | 60 AddNotificationHandler(NotificationCommon::EXTENSION, |
51 base::MakeUnique<ExtensionNotificationHandler>()); | 61 base::MakeUnique<ExtensionNotificationHandler>()); |
52 #endif | 62 #endif |
53 } | 63 } |
54 | 64 |
55 NativeNotificationDisplayService::~NativeNotificationDisplayService() {} | 65 NativeNotificationDisplayService::~NativeNotificationDisplayService() = default; |
| 66 |
| 67 void NativeNotificationDisplayService::OnNotificationPlatformBridgeReady( |
| 68 bool success) { |
| 69 if (success) { |
| 70 notification_bridge_ready_ = true; |
| 71 } else { |
| 72 message_center_display_service_ = |
| 73 base::MakeUnique<MessageCenterDisplayService>( |
| 74 profile_, g_browser_process->notification_ui_manager()); |
| 75 } |
| 76 |
| 77 while (!actions_.empty()) { |
| 78 std::move(actions_.front()).Run(); |
| 79 actions_.pop(); |
| 80 } |
| 81 } |
56 | 82 |
57 void NativeNotificationDisplayService::Display( | 83 void NativeNotificationDisplayService::Display( |
58 NotificationCommon::Type notification_type, | 84 NotificationCommon::Type notification_type, |
59 const std::string& notification_id, | 85 const std::string& notification_id, |
60 const Notification& notification) { | 86 const Notification& notification) { |
61 notification_bridge_->Display(notification_type, notification_id, | 87 if (notification_bridge_ready_) { |
62 GetProfileId(profile_), | 88 notification_bridge_->Display(notification_type, notification_id, |
63 profile_->IsOffTheRecord(), notification); | 89 GetProfileId(profile_), |
64 notification.delegate()->Display(); | 90 profile_->IsOffTheRecord(), notification); |
65 NotificationHandler* handler = GetNotificationHandler(notification_type); | 91 notification.delegate()->Display(); |
66 handler->RegisterNotification(notification_id, notification.delegate()); | 92 NotificationHandler* handler = GetNotificationHandler(notification_type); |
| 93 handler->RegisterNotification(notification_id, notification.delegate()); |
| 94 } else if (message_center_display_service_) { |
| 95 message_center_display_service_->Display(notification_type, notification_id, |
| 96 notification); |
| 97 } else { |
| 98 actions_.push(base::BindOnce(&NativeNotificationDisplayService::Display, |
| 99 weak_factory_.GetWeakPtr(), notification_type, |
| 100 notification_id, notification)); |
| 101 } |
67 } | 102 } |
68 | 103 |
69 void NativeNotificationDisplayService::Close( | 104 void NativeNotificationDisplayService::Close( |
70 NotificationCommon::Type notification_type, | 105 NotificationCommon::Type notification_type, |
71 const std::string& notification_id) { | 106 const std::string& notification_id) { |
72 NotificationHandler* handler = GetNotificationHandler(notification_type); | 107 if (notification_bridge_ready_) { |
73 notification_bridge_->Close(GetProfileId(profile_), notification_id); | 108 NotificationHandler* handler = GetNotificationHandler(notification_type); |
| 109 notification_bridge_->Close(GetProfileId(profile_), notification_id); |
74 | 110 |
75 // TODO(miguelg): Figure out something better here, passing an empty | 111 // TODO(miguelg): Figure out something better here, passing an empty |
76 // origin works because only non persistent notifications care about | 112 // origin works because only non persistent notifications care about |
77 // this method for JS generated close calls and they don't require | 113 // this method for JS generated close calls and they don't require |
78 // the origin. | 114 // the origin. |
79 handler->OnClose(profile_, "", notification_id, false /* by user */); | 115 handler->OnClose(profile_, "", notification_id, false /* by user */); |
| 116 } else if (message_center_display_service_) { |
| 117 message_center_display_service_->Close(notification_type, notification_id); |
| 118 } else { |
| 119 actions_.push(base::BindOnce(&NativeNotificationDisplayService::Close, |
| 120 weak_factory_.GetWeakPtr(), notification_type, |
| 121 notification_id)); |
| 122 } |
80 } | 123 } |
81 | 124 |
82 void NativeNotificationDisplayService::GetDisplayed( | 125 void NativeNotificationDisplayService::GetDisplayed( |
83 const DisplayedNotificationsCallback& callback) { | 126 const DisplayedNotificationsCallback& callback) { |
84 return notification_bridge_->GetDisplayed( | 127 if (notification_bridge_ready_) { |
85 GetProfileId(profile_), profile_->IsOffTheRecord(), callback); | 128 return notification_bridge_->GetDisplayed( |
| 129 GetProfileId(profile_), profile_->IsOffTheRecord(), callback); |
| 130 } else if (message_center_display_service_) { |
| 131 message_center_display_service_->GetDisplayed(callback); |
| 132 } else { |
| 133 actions_.push( |
| 134 base::BindOnce(&NativeNotificationDisplayService::GetDisplayed, |
| 135 weak_factory_.GetWeakPtr(), callback)); |
| 136 } |
86 } | 137 } |
87 | 138 |
88 void NativeNotificationDisplayService::ProcessNotificationOperation( | 139 void NativeNotificationDisplayService::ProcessNotificationOperation( |
89 NotificationCommon::Operation operation, | 140 NotificationCommon::Operation operation, |
90 NotificationCommon::Type notification_type, | 141 NotificationCommon::Type notification_type, |
91 const std::string& origin, | 142 const std::string& origin, |
92 const std::string& notification_id, | 143 const std::string& notification_id, |
93 int action_index, | 144 int action_index, |
94 const base::NullableString16& reply) { | 145 const base::NullableString16& reply) { |
95 NotificationHandler* handler = GetNotificationHandler(notification_type); | 146 NotificationHandler* handler = GetNotificationHandler(notification_type); |
(...skipping 24 matching lines...) Expand all Loading... |
120 notification_handlers_.erase(notification_type); | 171 notification_handlers_.erase(notification_type); |
121 } | 172 } |
122 | 173 |
123 NotificationHandler* NativeNotificationDisplayService::GetNotificationHandler( | 174 NotificationHandler* NativeNotificationDisplayService::GetNotificationHandler( |
124 NotificationCommon::Type notification_type) { | 175 NotificationCommon::Type notification_type) { |
125 DCHECK(notification_handlers_.find(notification_type) != | 176 DCHECK(notification_handlers_.find(notification_type) != |
126 notification_handlers_.end()) | 177 notification_handlers_.end()) |
127 << notification_type << " is not registered."; | 178 << notification_type << " is not registered."; |
128 return notification_handlers_[notification_type].get(); | 179 return notification_handlers_[notification_type].get(); |
129 } | 180 } |
OLD | NEW |