| 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/non_persistent_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 AddNotificationHandler(NotificationCommon::NON_PERSISTENT, |
| 39 base::MakeUnique<NonPersistentNotificationHandler>()); |
| 40 AddNotificationHandler(NotificationCommon::PERSISTENT, |
| 41 base::MakeUnique<PersistentNotificationHandler>()); |
| 32 } | 42 } |
| 33 | 43 |
| 34 NativeNotificationDisplayService::~NativeNotificationDisplayService() {} | 44 NativeNotificationDisplayService::~NativeNotificationDisplayService() {} |
| 35 | 45 |
| 36 void NativeNotificationDisplayService::Display( | 46 void NativeNotificationDisplayService::Display( |
| 47 NotificationCommon::Type notification_type, |
| 37 const std::string& notification_id, | 48 const std::string& notification_id, |
| 38 const Notification& notification) { | 49 const Notification& notification) { |
| 39 notification_bridge_->Display(notification_id, GetProfileId(profile_), | 50 notification_bridge_->Display(notification_type, notification_id, |
| 51 GetProfileId(profile_), |
| 40 profile_->IsOffTheRecord(), notification); | 52 profile_->IsOffTheRecord(), notification); |
| 41 notification.delegate()->Display(); | 53 notification.delegate()->Display(); |
| 54 NotificationHandler* handler = GetNotificationHandler(notification_type); |
| 55 handler->RegisterNotification(notification_id, notification.delegate()); |
| 42 } | 56 } |
| 43 | 57 |
| 44 void NativeNotificationDisplayService::Close( | 58 void NativeNotificationDisplayService::Close( |
| 59 NotificationCommon::Type notification_type, |
| 45 const std::string& notification_id) { | 60 const std::string& notification_id) { |
| 61 NotificationHandler* handler = GetNotificationHandler(notification_type); |
| 46 notification_bridge_->Close(GetProfileId(profile_), notification_id); | 62 notification_bridge_->Close(GetProfileId(profile_), notification_id); |
| 63 |
| 64 // TODO(miguelg): Figure out something better here, passing an empty |
| 65 // origin works because only non persistent notifications care about |
| 66 // this method for JS generated close calls and they don't require |
| 67 // the origin. |
| 68 handler->OnClose(profile_, "", notification_id, false /* by user */); |
| 47 } | 69 } |
| 48 | 70 |
| 49 bool NativeNotificationDisplayService::GetDisplayed( | 71 bool NativeNotificationDisplayService::GetDisplayed( |
| 50 std::set<std::string>* notifications) const { | 72 std::set<std::string>* notifications) const { |
| 51 return notification_bridge_->GetDisplayed( | 73 return notification_bridge_->GetDisplayed( |
| 52 GetProfileId(profile_), profile_->IsOffTheRecord(), notifications); | 74 GetProfileId(profile_), profile_->IsOffTheRecord(), notifications); |
| 53 } | 75 } |
| 54 | 76 |
| 77 void NativeNotificationDisplayService::ProcessNotificationOperation( |
| 78 NotificationCommon::Operation operation, |
| 79 NotificationCommon::Type notification_type, |
| 80 const std::string& origin, |
| 81 const std::string& notification_id, |
| 82 int action_index) { |
| 83 NotificationHandler* handler = GetNotificationHandler(notification_type); |
| 84 CHECK(handler); |
| 85 switch (operation) { |
| 86 case NotificationCommon::CLICK: |
| 87 handler->OnClick(profile_, origin, notification_id, action_index); |
| 88 break; |
| 89 case NotificationCommon::CLOSE: |
| 90 handler->OnClose(profile_, origin, notification_id, true /* by_user */); |
| 91 break; |
| 92 case NotificationCommon::SETTINGS: |
| 93 handler->OpenSettings(profile_); |
| 94 break; |
| 95 } |
| 96 } |
| 97 |
| 98 void NativeNotificationDisplayService::AddNotificationHandler( |
| 99 NotificationCommon::Type notification_type, |
| 100 std::unique_ptr<NotificationHandler> handler) { |
| 101 DCHECK(handler); |
| 102 DCHECK_EQ(notification_handlers_.count(notification_type), 0u); |
| 103 notification_handlers_[notification_type] = std::move(handler); |
| 104 } |
| 105 |
| 106 void NativeNotificationDisplayService::RemoveNotificationHandler( |
| 107 NotificationCommon::Type notification_type) { |
| 108 notification_handlers_.erase(notification_type); |
| 109 } |
| 110 |
| 111 NotificationHandler* NativeNotificationDisplayService::GetNotificationHandler( |
| 112 NotificationCommon::Type notification_type) { |
| 113 DCHECK(notification_handlers_.find(notification_type) != |
| 114 notification_handlers_.end()) |
| 115 << notification_type << " is not registered."; |
| 116 return notification_handlers_[notification_type].get(); |
| 117 } |
| 118 |
| 55 bool NativeNotificationDisplayService::SupportsNotificationCenter() const { | 119 bool NativeNotificationDisplayService::SupportsNotificationCenter() const { |
| 56 return notification_bridge_->SupportsNotificationCenter(); | 120 return notification_bridge_->SupportsNotificationCenter(); |
| 57 } | 121 } |
| OLD | NEW |