OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/arc/notification/arc_custom_notification_item.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/exo/notification_surface.h" |
| 11 #include "ui/arc/notification/arc_custom_notification_view.h" |
| 12 #include "ui/message_center/notification.h" |
| 13 #include "ui/message_center/notification_types.h" |
| 14 |
| 15 namespace arc { |
| 16 |
| 17 namespace { |
| 18 |
| 19 constexpr char kNotifierId[] = "ARC_NOTIFICATION"; |
| 20 |
| 21 class ArcNotificationDelegate : public message_center::NotificationDelegate { |
| 22 public: |
| 23 ArcNotificationDelegate() {} |
| 24 |
| 25 std::unique_ptr<views::View> CreateCustomContent() override { |
| 26 if (!surface_) |
| 27 return nullptr; |
| 28 |
| 29 return base::MakeUnique<ArcCustomNotificationView>(surface_); |
| 30 } |
| 31 |
| 32 void set_notification_surface(exo::NotificationSurface* surface) { |
| 33 surface_ = surface; |
| 34 } |
| 35 |
| 36 private: |
| 37 // The destructor is private since this class is ref-counted. |
| 38 ~ArcNotificationDelegate() override {} |
| 39 |
| 40 exo::NotificationSurface* surface_ = nullptr; |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate); |
| 43 }; |
| 44 |
| 45 } // namespace |
| 46 |
| 47 ArcCustomNotificationItem::ArcCustomNotificationItem( |
| 48 ArcNotificationManager* manager, |
| 49 message_center::MessageCenter* message_center, |
| 50 const std::string& notification_key, |
| 51 const AccountId& profile_id) |
| 52 : ArcNotificationItem(manager, |
| 53 message_center, |
| 54 notification_key, |
| 55 profile_id) {} |
| 56 |
| 57 ArcCustomNotificationItem::~ArcCustomNotificationItem() { |
| 58 if (ArcNotificationSurfaceManager::Get()) |
| 59 ArcNotificationSurfaceManager::Get()->RemoveObserver(this); |
| 60 } |
| 61 |
| 62 void ArcCustomNotificationItem::UpdateWithArcNotificationData( |
| 63 const mojom::ArcNotificationData& data) { |
| 64 DCHECK(CalledOnValidThread()); |
| 65 DCHECK_EQ(notification_key(), data.key); |
| 66 |
| 67 if (CacheArcNotificationData(data)) |
| 68 return; |
| 69 |
| 70 message_center::RichNotificationData rich_data; |
| 71 rich_data.pinned = (data.no_clear || data.ongoing_event); |
| 72 rich_data.priority = ConvertAndroidPriority(data.priority); |
| 73 |
| 74 message_center::NotifierId notifier_id( |
| 75 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); |
| 76 notifier_id.profile_id = profile_id().GetUserEmail(); |
| 77 |
| 78 DCHECK(!data.title.is_null()); |
| 79 DCHECK(!data.message.is_null()); |
| 80 SetNotification(base::MakeUnique<message_center::Notification>( |
| 81 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(), |
| 82 base::UTF8ToUTF16(data.title.get()), |
| 83 base::UTF8ToUTF16(data.message.get()), gfx::Image(), |
| 84 base::UTF8ToUTF16("arc"), // display source |
| 85 GURL(), // empty origin url, for system component |
| 86 notifier_id, rich_data, new ArcNotificationDelegate())); |
| 87 |
| 88 exo::NotificationSurface* surface = |
| 89 ArcNotificationSurfaceManager::Get()->GetSurface(notification_key()); |
| 90 if (surface) |
| 91 OnNotificationSurfaceAdded(surface); |
| 92 else |
| 93 ArcNotificationSurfaceManager::Get()->AddObserver(this); |
| 94 } |
| 95 |
| 96 void ArcCustomNotificationItem::OnNotificationSurfaceAdded( |
| 97 exo::NotificationSurface* surface) { |
| 98 if (!pending_notification() || |
| 99 surface->notification_id() != notification_key()) { |
| 100 return; |
| 101 } |
| 102 |
| 103 static_cast<ArcNotificationDelegate*>(pending_notification()->delegate()) |
| 104 ->set_notification_surface(surface); |
| 105 AddToMessageCenter(); |
| 106 } |
| 107 |
| 108 void ArcCustomNotificationItem::OnNotificationSurfaceRemoved( |
| 109 exo::NotificationSurface* surface) { |
| 110 if (surface->notification_id() != notification_key()) |
| 111 return; |
| 112 |
| 113 OnClosedFromAndroid(false); |
| 114 } |
| 115 |
| 116 } // namespace arc |
OLD | NEW |