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 DCHECK(item_); | |
30 } | |
31 | |
32 std::unique_ptr<message_center::CustomContent> CreateCustomContent() | |
33 override { | |
34 auto view = base::MakeUnique<ArcCustomNotificationView>(item_); | |
35 auto content_view_delegate = view->CreateContentViewDelegate(); | |
36 return base::MakeUnique<message_center::CustomContent>( | |
37 std::move(view), std::move(content_view_delegate)); | |
38 } | |
39 | |
40 void Close(bool by_user) override { item_->Close(by_user); } | |
41 | |
42 void Click() override { item_->Click(); } | |
43 | |
44 private: | |
45 // The destructor is private since this class is ref-counted. | |
46 ~ArcNotificationDelegate() override {} | |
47 | |
48 ArcCustomNotificationItem* const item_; | |
49 | |
50 DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate); | |
51 }; | |
52 | |
53 } // namespace | |
54 | |
55 ArcCustomNotificationItem::ArcCustomNotificationItem( | |
56 ArcNotificationManager* manager, | |
57 message_center::MessageCenter* message_center, | |
58 const std::string& notification_key, | |
59 const AccountId& profile_id) | |
60 : ArcNotificationItem(manager, | |
61 message_center, | |
62 notification_key, | |
63 profile_id) { | |
64 } | |
65 | |
66 ArcCustomNotificationItem::~ArcCustomNotificationItem() { | |
67 for (auto& observer : observers_) | |
68 observer.OnItemDestroying(); | |
69 } | |
70 | |
71 void ArcCustomNotificationItem::UpdateWithArcNotificationData( | |
72 mojom::ArcNotificationDataPtr data) { | |
73 DCHECK(CalledOnValidThread()); | |
74 DCHECK_EQ(notification_key(), data->key); | |
75 | |
76 if (HasPendingNotification()) { | |
77 CacheArcNotificationData(std::move(data)); | |
78 return; | |
79 } | |
80 | |
81 message_center::RichNotificationData rich_data; | |
82 rich_data.pinned = (data->no_clear || data->ongoing_event); | |
83 rich_data.priority = ConvertAndroidPriority(data->priority); | |
84 if (data->small_icon) | |
85 rich_data.small_image = gfx::Image::CreateFrom1xBitmap(*data->small_icon); | |
86 if (data->accessible_name.has_value()) | |
87 rich_data.accessible_name = base::UTF8ToUTF16(*data->accessible_name); | |
88 | |
89 message_center::NotifierId notifier_id( | |
90 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); | |
91 notifier_id.profile_id = profile_id().GetUserEmail(); | |
92 | |
93 auto notification = base::MakeUnique<message_center::Notification>( | |
94 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(), | |
95 base::UTF8ToUTF16(data->title), base::UTF8ToUTF16(data->message), | |
96 gfx::Image(), | |
97 base::UTF8ToUTF16("arc"), // display source | |
98 GURL(), // empty origin url, for system component | |
99 notifier_id, rich_data, new ArcNotificationDelegate(this)); | |
100 notification->set_timestamp(base::Time::FromJavaTime(data->time)); | |
101 SetNotification(std::move(notification)); | |
102 | |
103 pinned_ = rich_data.pinned; | |
104 expand_state_ = data->expand_state; | |
105 shown_contents_ = data->shown_contents; | |
106 | |
107 if (!data->snapshot_image || data->snapshot_image->isNull()) { | |
108 snapshot_ = gfx::ImageSkia(); | |
109 } else { | |
110 snapshot_ = gfx::ImageSkia(gfx::ImageSkiaRep( | |
111 *data->snapshot_image, data->snapshot_image_scale)); | |
112 } | |
113 | |
114 for (auto& observer : observers_) | |
115 observer.OnItemUpdated(); | |
116 | |
117 AddToMessageCenter(); | |
118 } | |
119 | |
120 void ArcCustomNotificationItem::AddObserver(Observer* observer) { | |
121 observers_.AddObserver(observer); | |
122 } | |
123 | |
124 void ArcCustomNotificationItem::RemoveObserver(Observer* observer) { | |
125 observers_.RemoveObserver(observer); | |
126 } | |
127 | |
128 void ArcCustomNotificationItem::IncrementWindowRefCount() { | |
129 ++window_ref_count_; | |
130 if (window_ref_count_ == 1) | |
131 manager()->CreateNotificationWindow(notification_key()); | |
132 } | |
133 | |
134 void ArcCustomNotificationItem::DecrementWindowRefCount() { | |
135 DCHECK_GT(window_ref_count_, 0); | |
136 --window_ref_count_; | |
137 if (window_ref_count_ == 0) | |
138 manager()->CloseNotificationWindow(notification_key()); | |
139 } | |
140 | |
141 } // namespace arc | |
OLD | NEW |