Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: ui/arc/notification/arc_notification_item_impl.cc

Issue 2723143002: Add unittests of ArcCustomNotificationView (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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_notification_item_impl.h"
6
7 #include <algorithm>
8 #include <utility>
9 #include <vector>
10
11 #include "base/memory/ptr_util.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/task_runner.h"
15 #include "base/task_scheduler/post_task.h"
16 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkPaint.h"
18 #include "ui/arc/notification/arc_custom_notification_view.h"
19 #include "ui/arc/notification/arc_notification_delegate.h"
20 #include "ui/gfx/codec/png_codec.h"
21 #include "ui/gfx/geometry/size.h"
22 #include "ui/gfx/image/image.h"
23 #include "ui/gfx/text_elider.h"
24 #include "ui/message_center/message_center_style.h"
25 #include "ui/message_center/notification.h"
26 #include "ui/message_center/notification_types.h"
27 #include "ui/message_center/notifier_settings.h"
28
29 namespace arc {
30
31 namespace {
32
33 constexpr char kNotifierId[] = "ARC_NOTIFICATION";
34 constexpr char kNotificationIdPrefix[] = "ARC_NOTIFICATION_";
35
36 } // anonymous namespace
37
38 ArcNotificationItemImpl::ArcNotificationItemImpl(
39 ArcNotificationManager* manager,
40 message_center::MessageCenter* message_center,
41 const std::string& notification_key,
42 const AccountId& profile_id)
43 : manager_(manager),
44 message_center_(message_center),
45 profile_id_(profile_id),
46 notification_key_(notification_key),
47 notification_id_(kNotificationIdPrefix + notification_key_),
48 weak_ptr_factory_(this) {}
49
50 ArcNotificationItemImpl::~ArcNotificationItemImpl() {
51 for (auto& observer : observers_)
52 observer.OnItemDestroying();
53 }
54
55 void ArcNotificationItemImpl::UpdateWithArcNotificationData(
56 mojom::ArcNotificationDataPtr data) {
57 DCHECK(CalledOnValidThread());
58 DCHECK_EQ(notification_key(), data->key);
59
60 if (HasPendingNotification()) {
61 CacheArcNotificationData(std::move(data));
62 return;
63 }
64
hidehiko 2017/03/02 15:38:16 Here, some code is dropped. Any background?
65 message_center::RichNotificationData rich_data;
66 rich_data.pinned = (data->no_clear || data->ongoing_event);
67 rich_data.priority = ConvertAndroidPriority(data->priority);
68 if (data->small_icon)
69 rich_data.small_image = gfx::Image::CreateFrom1xBitmap(*data->small_icon);
70 if (data->accessible_name.has_value())
71 rich_data.accessible_name = base::UTF8ToUTF16(*data->accessible_name);
72
73 message_center::NotifierId notifier_id(
74 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId);
75 notifier_id.profile_id = profile_id().GetUserEmail();
76
77 SetNotification(base::MakeUnique<message_center::Notification>(
78 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(),
79 base::UTF8ToUTF16(data->title), base::UTF8ToUTF16(data->message),
80 gfx::Image(),
81 base::UTF8ToUTF16("arc"), // display source
82 GURL(), // empty origin url, for system component
83 notifier_id, rich_data,
84 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr())));
85
86 pinned_ = rich_data.pinned;
87
88 if (!data->snapshot_image || data->snapshot_image->isNull()) {
89 snapshot_ = gfx::ImageSkia();
90 } else {
91 snapshot_ = gfx::ImageSkia(
92 gfx::ImageSkiaRep(*data->snapshot_image, data->snapshot_image_scale));
93 }
94
95 for (auto& observer : observers_)
96 observer.OnItemUpdated();
97
98 AddToMessageCenter();
99 }
100
101 void ArcNotificationItemImpl::OnClosedFromAndroid() {
102 being_removed_by_manager_ = true; // Closing is initiated by the manager.
103 message_center_->RemoveNotification(notification_id_, false /* by_user */);
104 }
105
106 void ArcNotificationItemImpl::Close(bool by_user) {
107 if (being_removed_by_manager_) {
108 // Closing is caused by the manager, so we don't need to nofify a close
109 // event to the manager.
110 return;
111 }
112
113 // Do not touch its any members afterwards, because this instance will be
114 // destroyed in the following call
115 manager_->SendNotificationRemovedFromChrome(notification_key_);
116 }
117
118 void ArcNotificationItemImpl::OpenSettings() {
119 manager_->OpenNotificationSettings(notification_key_);
120 }
121
122 // Converts from Android notification priority to Chrome notification priority.
123 // On Android, PRIORITY_DEFAULT does not pop up, so this maps PRIORITY_DEFAULT
124 // to Chrome's -1 to adapt that behavior. Also, this maps PRIORITY_LOW and _HIGH
125 // to -2 and 0 respectively to adjust the value with keeping the order among
126 // _LOW, _DEFAULT and _HIGH.
127 // static
128 int ArcNotificationItemImpl::ConvertAndroidPriority(int android_priority) {
129 switch (android_priority) {
130 case -2: // PRIORITY_MIN
131 case -1: // PRIORITY_LOW
132 return -2;
133 case 0: // PRIORITY_DEFAULT
134 return -1;
135 case 1: // PRIORITY_HIGH
136 return 0;
137 case 2: // PRIORITY_MAX
138 return 2;
139 default:
140 NOTREACHED() << "Invalid Priority: " << android_priority;
141 return 0;
142 }
143 }
144
145 bool ArcNotificationItemImpl::HasPendingNotification() {
146 return (notification_ != nullptr);
147 }
148
149 void ArcNotificationItemImpl::CacheArcNotificationData(
150 mojom::ArcNotificationDataPtr data) {
151 // If old |newer_data_| has been stored, discard the old one.
152 newer_data_ = std::move(data);
153 }
154
155 void ArcNotificationItemImpl::SetNotification(
156 std::unique_ptr<message_center::Notification> notification) {
157 notification_ = std::move(notification);
158 }
159
160 void ArcNotificationItemImpl::AddToMessageCenter() {
161 DCHECK(notification_);
162 message_center_->AddNotification(std::move(notification_));
163
164 if (newer_data_) {
165 // There is the newer data, so updates again.
166 UpdateWithArcNotificationData(std::move(newer_data_));
167 }
168 }
169
170 bool ArcNotificationItemImpl::CalledOnValidThread() const {
171 return thread_checker_.CalledOnValidThread();
172 }
173
174 void ArcNotificationItemImpl::OnImageDecoded(const SkBitmap& bitmap) {
175 DCHECK(thread_checker_.CalledOnValidThread());
176
177 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap);
178 notification_->set_icon(image);
179 AddToMessageCenter();
180 }
181
182 void ArcNotificationItemImpl::CloseFromCloseButton() {
183 // Needs to manually remove notification from MessageCenter because
184 // the floating close button is not part of MessageCenter.
185 message_center()->RemoveNotification(notification_id(), true);
186 Close(true);
187 }
188
189 void ArcNotificationItemImpl::AddObserver(Observer* observer) {
190 observers_.AddObserver(observer);
191 }
192
193 void ArcNotificationItemImpl::RemoveObserver(Observer* observer) {
194 observers_.RemoveObserver(observer);
195 }
196
197 void ArcNotificationItemImpl::IncrementWindowRefCount() {
198 ++window_ref_count_;
199 if (window_ref_count_ == 1)
200 manager()->CreateNotificationWindow(notification_key());
201 }
202
203 void ArcNotificationItemImpl::DecrementWindowRefCount() {
204 DCHECK_GT(window_ref_count_, 0);
205 --window_ref_count_;
206 if (window_ref_count_ == 0)
207 manager()->CloseNotificationWindow(notification_key());
208 }
209
210 bool ArcNotificationItemImpl::pinned() const {
211 return pinned_;
212 }
213
214 const gfx::ImageSkia& ArcNotificationItemImpl::snapshot() const {
215 return snapshot_;
216 }
217
218 const std::string& ArcNotificationItemImpl::notification_key() const {
219 return notification_key_;
220 }
221
222 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698