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/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/notifications/in_page_notification_handler.h" |
8 #include "chrome/browser/notifications/notification.h" | 10 #include "chrome/browser/notifications/notification.h" |
| 11 #include "chrome/browser/notifications/notification_delegate.h" |
| 12 #include "chrome/browser/notifications/notification_handler.h" |
9 #include "chrome/browser/notifications/notification_platform_bridge.h" | 13 #include "chrome/browser/notifications/notification_platform_bridge.h" |
| 14 #include "chrome/browser/notifications/persistent_notification_handler.h" |
10 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
11 | 16 |
12 namespace { | 17 namespace { |
13 | 18 |
14 std::string GetProfileId(Profile* profile) { | 19 std::string GetProfileId(Profile* profile) { |
15 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
16 std::string profile_id = | 21 std::string profile_id = |
17 base::WideToUTF8(profile->GetPath().BaseName().value()); | 22 base::WideToUTF8(profile->GetPath().BaseName().value()); |
18 #elif defined(OS_POSIX) | 23 #elif defined(OS_POSIX) |
19 std::string profile_id = profile->GetPath().BaseName().value(); | 24 std::string profile_id = profile->GetPath().BaseName().value(); |
20 #endif | 25 #endif |
21 return profile_id; | 26 return profile_id; |
22 } | 27 } |
23 | 28 |
24 } // namespace | 29 } // namespace |
25 | 30 |
26 NativeNotificationDisplayService::NativeNotificationDisplayService( | 31 NativeNotificationDisplayService::NativeNotificationDisplayService( |
27 Profile* profile, | 32 Profile* profile, |
28 NotificationPlatformBridge* notification_bridge) | 33 NotificationPlatformBridge* notification_bridge) |
29 : profile_(profile), notification_bridge_(notification_bridge) { | 34 : profile_(profile), notification_bridge_(notification_bridge) { |
30 DCHECK(profile_); | 35 DCHECK(profile_); |
31 DCHECK(notification_bridge_); | 36 DCHECK(notification_bridge_); |
| 37 |
| 38 std::unique_ptr<InPageNotificationHandler> in_page( |
| 39 new InPageNotificationHandler()); |
| 40 std::unique_ptr<PersistentNotificationHandler> persistent( |
| 41 new PersistentNotificationHandler()); |
| 42 |
| 43 AddNotificationHandler(in_page->NotificationType(), std::move(in_page)); |
| 44 AddNotificationHandler(persistent->NotificationType(), std::move(persistent)); |
32 } | 45 } |
33 | 46 |
34 NativeNotificationDisplayService::~NativeNotificationDisplayService() {} | 47 NativeNotificationDisplayService::~NativeNotificationDisplayService() { |
| 48 notification_handlers_.clear(); |
| 49 } |
35 | 50 |
36 void NativeNotificationDisplayService::Display( | 51 void NativeNotificationDisplayService::Display( |
| 52 notification_operation_common::NotificationHandlerType notification_type, |
37 const std::string& notification_id, | 53 const std::string& notification_id, |
38 const Notification& notification) { | 54 const Notification& notification) { |
39 notification_bridge_->Display(notification_id, GetProfileId(profile_), | 55 notification_bridge_->Display(notification_type, notification_id, |
| 56 GetProfileId(profile_), |
40 profile_->IsOffTheRecord(), notification); | 57 profile_->IsOffTheRecord(), notification); |
41 notification.delegate()->Display(); | 58 notification.delegate()->Display(); |
| 59 NotificationHandler* handler = GetNotificationHandler(notification_type); |
| 60 handler->RegisterNotification( |
| 61 notification_id, base::WrapUnique(new Notification(notification))); |
42 } | 62 } |
43 | 63 |
44 void NativeNotificationDisplayService::Close( | 64 void NativeNotificationDisplayService::Close( |
| 65 notification_operation_common::NotificationHandlerType notification_type, |
45 const std::string& notification_id) { | 66 const std::string& notification_id) { |
| 67 NotificationHandler* handler = GetNotificationHandler(notification_type); |
46 notification_bridge_->Close(GetProfileId(profile_), notification_id); | 68 notification_bridge_->Close(GetProfileId(profile_), notification_id); |
| 69 handler->CloseNotification(profile_, notification_id); |
47 } | 70 } |
48 | 71 |
49 bool NativeNotificationDisplayService::GetDisplayed( | 72 bool NativeNotificationDisplayService::GetDisplayed( |
50 std::set<std::string>* notifications) const { | 73 std::set<std::string>* notifications) const { |
51 return notification_bridge_->GetDisplayed( | 74 return notification_bridge_->GetDisplayed( |
52 GetProfileId(profile_), profile_->IsOffTheRecord(), notifications); | 75 GetProfileId(profile_), profile_->IsOffTheRecord(), notifications); |
53 } | 76 } |
54 | 77 |
| 78 void NativeNotificationDisplayService::ProcessNotificationOperation( |
| 79 notification_operation_common::NotificationOperation operation, |
| 80 notification_operation_common::NotificationHandlerType notification_type, |
| 81 const std::string& origin, |
| 82 const std::string& notification_id, |
| 83 int action_index) { |
| 84 NotificationHandler* handler = GetNotificationHandler(notification_type); |
| 85 handler->HandleNotificationOperation(operation, profile_, origin, |
| 86 notification_id, action_index); |
| 87 } |
| 88 |
| 89 void NativeNotificationDisplayService::AddNotificationHandler( |
| 90 notification_operation_common::NotificationHandlerType notification_type, |
| 91 std::unique_ptr<NotificationHandler> handler) { |
| 92 DCHECK(handler); |
| 93 DCHECK_EQ(notification_handlers_.count(notification_type), 0u); |
| 94 notification_handlers_[notification_type] = std::move(handler); |
| 95 } |
| 96 |
| 97 void NativeNotificationDisplayService::RemoveNotificationHandler( |
| 98 notification_operation_common::NotificationHandlerType notification_type) { |
| 99 notification_handlers_.erase(notification_type); |
| 100 } |
| 101 |
| 102 NotificationHandler* NativeNotificationDisplayService::GetNotificationHandler( |
| 103 notification_operation_common::NotificationHandlerType notification_type) { |
| 104 DCHECK(notification_handlers_.find(notification_type) != |
| 105 notification_handlers_.end()) |
| 106 << notification_type << " NOT REGISTERED"; |
| 107 return notification_handlers_[notification_type].get(); |
| 108 } |
| 109 |
55 bool NativeNotificationDisplayService::SupportsNotificationCenter() const { | 110 bool NativeNotificationDisplayService::SupportsNotificationCenter() const { |
56 return notification_bridge_->SupportsNotificationCenter(); | 111 return notification_bridge_->SupportsNotificationCenter(); |
57 } | 112 } |
OLD | NEW |