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 <memory> | |
8 #include <string> | |
9 #include <utility> | |
10 | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/strings/string16.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "ui/arc/notification/arc_custom_notification_view.h" | |
15 #include "ui/message_center/notification.h" | |
16 #include "ui/message_center/notification_types.h" | |
17 #include "ui/message_center/views/custom_notification_content_view_delegate.h" | |
18 | |
19 namespace arc { | |
20 | |
21 namespace { | |
22 | |
23 constexpr char kNotifierId[] = "ARC_NOTIFICATION"; | |
24 | |
25 class ArcNotificationDelegate : public message_center::NotificationDelegate { | |
26 public: | |
27 explicit ArcNotificationDelegate(ArcCustomNotificationItem* item) | |
28 : item_(item) {} | |
29 | |
30 std::unique_ptr<message_center::CustomContent> CreateCustomContent() | |
31 override { | |
32 auto view = base::MakeUnique<ArcCustomNotificationView>(item_); | |
33 auto content_view_delegate = view->CreateContentViewDelegate(); | |
34 return base::MakeUnique<message_center::CustomContent>( | |
35 std::move(view), std::move(content_view_delegate)); | |
36 } | |
37 | |
38 private: | |
39 // The destructor is private since this class is ref-counted. | |
40 ~ArcNotificationDelegate() override {} | |
41 | |
42 ArcCustomNotificationItem* const item_; | |
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 | |
60 ArcCustomNotificationItem::~ArcCustomNotificationItem() { | |
61 for (auto& observer : observers_) | |
62 observer.OnItemDestroying(); | |
63 } | |
64 | |
65 void ArcCustomNotificationItem::UpdateWithArcNotificationData( | |
66 mojom::ArcNotificationDataPtr data) { | |
67 DCHECK(CalledOnValidThread()); | |
68 DCHECK_EQ(notification_key(), data->key); | |
69 | |
70 if (HasPendingNotification()) { | |
71 CacheArcNotificationData(std::move(data)); | |
72 return; | |
73 } | |
74 | |
75 message_center::RichNotificationData rich_data; | |
76 rich_data.pinned = (data->no_clear || data->ongoing_event); | |
77 rich_data.priority = ConvertAndroidPriority(data->priority); | |
78 if (data->small_icon) | |
79 rich_data.small_image = gfx::Image::CreateFrom1xBitmap(*data->small_icon); | |
80 if (data->accessible_name.has_value()) | |
81 rich_data.accessible_name = base::UTF8ToUTF16(*data->accessible_name); | |
82 | |
83 message_center::NotifierId notifier_id( | |
84 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); | |
85 notifier_id.profile_id = profile_id().GetUserEmail(); | |
86 | |
87 SetNotification(base::MakeUnique<message_center::Notification>( | |
88 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(), | |
89 base::UTF8ToUTF16(data->title), base::UTF8ToUTF16(data->message), | |
90 gfx::Image(), | |
91 base::UTF8ToUTF16("arc"), // display source | |
92 GURL(), // empty origin url, for system component | |
93 notifier_id, rich_data, new ArcNotificationDelegate(this))); | |
94 | |
95 pinned_ = rich_data.pinned; | |
96 | |
97 if (!data->snapshot_image || data->snapshot_image->isNull()) { | |
98 snapshot_ = gfx::ImageSkia(); | |
99 } else { | |
100 snapshot_ = gfx::ImageSkia(gfx::ImageSkiaRep( | |
101 *data->snapshot_image, data->snapshot_image_scale)); | |
102 } | |
103 | |
104 for (auto& observer : observers_) | |
105 observer.OnItemUpdated(); | |
106 | |
107 AddToMessageCenter(); | |
108 } | |
109 | |
110 void ArcCustomNotificationItem::CloseFromCloseButton() { | |
111 // Needs to manually remove notification from MessageCenter because | |
112 // the floating close button is not part of MessageCenter. | |
113 message_center()->RemoveNotification(notification_id(), true); | |
114 Close(true); | |
115 } | |
116 | |
117 void ArcCustomNotificationItem::AddObserver(Observer* observer) { | |
118 observers_.AddObserver(observer); | |
119 } | |
120 | |
121 void ArcCustomNotificationItem::RemoveObserver(Observer* observer) { | |
122 observers_.RemoveObserver(observer); | |
123 } | |
124 | |
125 void ArcCustomNotificationItem::IncrementWindowRefCount() { | |
126 ++window_ref_count_; | |
127 if (window_ref_count_ == 1) | |
128 manager()->CreateNotificationWindow(notification_key()); | |
129 } | |
130 | |
131 void ArcCustomNotificationItem::DecrementWindowRefCount() { | |
132 DCHECK_GT(window_ref_count_, 0); | |
133 --window_ref_count_; | |
134 if (window_ref_count_ == 0) | |
135 manager()->CloseNotificationWindow(notification_key()); | |
136 } | |
137 | |
138 } // namespace arc | |
OLD | NEW |