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 exo::NotificationSurfaceRegistry* notification_surface_registry = nullptr; | |
22 | |
23 class ArcNotificationDelegate : public message_center::NotificationDelegate { | |
24 public: | |
25 ArcNotificationDelegate() {} | |
26 | |
27 std::unique_ptr<views::View> CreateCustomContent() override { | |
28 if (!surface_) | |
29 return std::unique_ptr<views::View>(); | |
dcheng
2016/06/22 02:23:56
return nullptr
xiyuan
2016/06/22 14:36:00
Done.
| |
30 | |
31 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.
| |
32 } | |
33 | |
34 void set_notification_surface(exo::NotificationSurface* surface) { | |
35 surface_ = surface; | |
36 } | |
37 | |
38 private: | |
39 // The destructor is private since this class is ref-counted. | |
40 ~ArcNotificationDelegate() override {} | |
41 | |
42 exo::NotificationSurface* surface_ = nullptr; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate); | |
45 }; | |
46 | |
47 } // namespace | |
48 | |
49 ArcCustomNotificationItem::ArcCustomNotificationItem( | |
50 ArcNotificationManager* manager, | |
51 message_center::MessageCenter* message_center, | |
52 const std::string& notification_key, | |
53 const AccountId& profile_id) | |
54 : ArcNotificationItem(manager, | |
55 message_center, | |
56 notification_key, | |
57 profile_id) {} | |
58 | |
59 ArcCustomNotificationItem::~ArcCustomNotificationItem() { | |
60 if (notification_surface_registry) | |
61 notification_surface_registry->RemoveObserver(this); | |
62 } | |
63 | |
64 // static | |
65 void ArcCustomNotificationItem::SetNotificationSurfaceRegistry( | |
66 exo::NotificationSurfaceRegistry* registry) { | |
67 notification_surface_registry = registry; | |
68 } | |
69 | |
70 void ArcCustomNotificationItem::UpdateWithArcNotificationData( | |
71 const mojom::ArcNotificationData& data) { | |
72 DCHECK(CalledOnValidThread()); | |
73 DCHECK(notification_key() == data.key); | |
dcheng
2016/06/22 02:23:56
Nit: DCHECK_EQ?
xiyuan
2016/06/22 14:35:59
Done.
| |
74 | |
75 if (CacheArcNotificationData(data)) | |
76 return; | |
77 | |
78 message_center::RichNotificationData rich_data; | |
79 rich_data.pinned = (data.no_clear || data.ongoing_event); | |
80 rich_data.priority = ConvertAndroidPriority(data.priority); | |
81 | |
82 message_center::NotifierId notifier_id( | |
83 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); | |
84 notifier_id.profile_id = profile_id().GetUserEmail(); | |
85 | |
86 DCHECK(!data.title.is_null()); | |
87 DCHECK(!data.message.is_null()); | |
88 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.
| |
89 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(), | |
90 base::UTF8ToUTF16(data.title.get()), | |
91 base::UTF8ToUTF16(data.message.get()), gfx::Image(), | |
92 base::UTF8ToUTF16("arc"), // display source | |
93 GURL(), // empty origin url, for system component | |
94 notifier_id, rich_data, new ArcNotificationDelegate()))); | |
95 | |
96 exo::NotificationSurface* surface = | |
97 notification_surface_registry->GetSurface(notification_key()); | |
98 if (surface) | |
99 OnNotificationSurfaceAdded(surface); | |
100 else | |
101 notification_surface_registry->AddObserver(this); | |
102 } | |
103 | |
104 void ArcCustomNotificationItem::OnNotificationSurfaceAdded( | |
105 exo::NotificationSurface* surface) { | |
106 if (!pending_notification() || | |
107 surface->notification_id() != notification_key()) | |
108 return; | |
109 | |
110 static_cast<ArcNotificationDelegate*>(pending_notification()->delegate()) | |
111 ->set_notification_surface(surface); | |
112 AddToMessageCenter(); | |
113 } | |
114 | |
115 void ArcCustomNotificationItem::OnNotificationSurfaceRemoved( | |
116 exo::NotificationSurface* surface) { | |
117 if (surface->notification_id() != notification_key()) | |
118 return; | |
119 | |
120 OnClosedFromAndroid(false); | |
121 } | |
122 | |
123 } // namespace arc | |
OLD | NEW |