Chromium Code Reviews| Index: ui/arc/notification/arc_custom_notification_item.cc |
| diff --git a/ui/arc/notification/arc_custom_notification_item.cc b/ui/arc/notification/arc_custom_notification_item.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f75dcfe67570752f5df790df062020643199103a |
| --- /dev/null |
| +++ b/ui/arc/notification/arc_custom_notification_item.cc |
| @@ -0,0 +1,123 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/arc/notification/arc_custom_notification_item.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "components/exo/notification_surface.h" |
| +#include "ui/arc/notification/arc_custom_notification_view.h" |
| +#include "ui/message_center/notification.h" |
| +#include "ui/message_center/notification_types.h" |
| + |
| +namespace arc { |
| + |
| +namespace { |
| + |
| +constexpr char kNotifierId[] = "ARC_NOTIFICATION"; |
| + |
| +exo::NotificationSurfaceRegistry* notification_surface_registry = nullptr; |
| + |
| +class ArcNotificationDelegate : public message_center::NotificationDelegate { |
| + public: |
| + ArcNotificationDelegate() {} |
| + |
| + std::unique_ptr<views::View> CreateCustomContent() override { |
| + if (!surface_) |
| + return std::unique_ptr<views::View>(); |
|
dcheng
2016/06/22 02:23:56
return nullptr
xiyuan
2016/06/22 14:36:00
Done.
|
| + |
| + return base::WrapUnique(new ArcCustomNotificationView(surface_)); |
|
dcheng
2016/06/22 02:23:56
base::MakeUnique<ArcCustomNotificationView>(surfac
xiyuan
2016/06/22 14:35:59
Done.
|
| + } |
| + |
| + void set_notification_surface(exo::NotificationSurface* surface) { |
| + surface_ = surface; |
| + } |
| + |
| + private: |
| + // The destructor is private since this class is ref-counted. |
| + ~ArcNotificationDelegate() override {} |
| + |
| + exo::NotificationSurface* surface_ = nullptr; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate); |
| +}; |
| + |
| +} // namespace |
| + |
| +ArcCustomNotificationItem::ArcCustomNotificationItem( |
| + ArcNotificationManager* manager, |
| + message_center::MessageCenter* message_center, |
| + const std::string& notification_key, |
| + const AccountId& profile_id) |
| + : ArcNotificationItem(manager, |
| + message_center, |
| + notification_key, |
| + profile_id) {} |
| + |
| +ArcCustomNotificationItem::~ArcCustomNotificationItem() { |
| + if (notification_surface_registry) |
| + notification_surface_registry->RemoveObserver(this); |
| +} |
| + |
| +// static |
| +void ArcCustomNotificationItem::SetNotificationSurfaceRegistry( |
| + exo::NotificationSurfaceRegistry* registry) { |
| + notification_surface_registry = registry; |
| +} |
| + |
| +void ArcCustomNotificationItem::UpdateWithArcNotificationData( |
| + const mojom::ArcNotificationData& data) { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(notification_key() == data.key); |
|
dcheng
2016/06/22 02:23:56
Nit: DCHECK_EQ?
xiyuan
2016/06/22 14:35:59
Done.
|
| + |
| + if (CacheArcNotificationData(data)) |
| + return; |
| + |
| + message_center::RichNotificationData rich_data; |
| + rich_data.pinned = (data.no_clear || data.ongoing_event); |
| + rich_data.priority = ConvertAndroidPriority(data.priority); |
| + |
| + message_center::NotifierId notifier_id( |
| + message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); |
| + notifier_id.profile_id = profile_id().GetUserEmail(); |
| + |
| + DCHECK(!data.title.is_null()); |
| + DCHECK(!data.message.is_null()); |
| + SetNotification(base::WrapUnique(new message_center::Notification( |
|
dcheng
2016/06/22 02:23:56
Nit: base::MakeUnique<message_center::Notification
xiyuan
2016/06/22 14:35:59
Done.
|
| + message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(), |
| + base::UTF8ToUTF16(data.title.get()), |
| + base::UTF8ToUTF16(data.message.get()), gfx::Image(), |
| + base::UTF8ToUTF16("arc"), // display source |
| + GURL(), // empty origin url, for system component |
| + notifier_id, rich_data, new ArcNotificationDelegate()))); |
| + |
| + exo::NotificationSurface* surface = |
| + notification_surface_registry->GetSurface(notification_key()); |
| + if (surface) |
| + OnNotificationSurfaceAdded(surface); |
| + else |
| + notification_surface_registry->AddObserver(this); |
| +} |
| + |
| +void ArcCustomNotificationItem::OnNotificationSurfaceAdded( |
| + exo::NotificationSurface* surface) { |
| + if (!pending_notification() || |
| + surface->notification_id() != notification_key()) |
| + return; |
| + |
| + static_cast<ArcNotificationDelegate*>(pending_notification()->delegate()) |
| + ->set_notification_surface(surface); |
| + AddToMessageCenter(); |
| +} |
| + |
| +void ArcCustomNotificationItem::OnNotificationSurfaceRemoved( |
| + exo::NotificationSurface* surface) { |
| + if (surface->notification_id() != notification_key()) |
| + return; |
| + |
| + OnClosedFromAndroid(false); |
| +} |
| + |
| +} // namespace arc |