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

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

Issue 2845003002: Merge ArcNotificationItem and ArcCustomNotificationItem (Closed)
Patch Set: Created 3 years, 7 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>
hidehiko 2017/05/01 12:29:18 Looks unnecessary?
yoshiki 2017/05/08 06:24:53 Done.
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"
hidehiko 2017/05/01 12:29:18 ditto for task_runner.h to SkPaint.h?
yoshiki 2017/05/08 06:24:53 Done.
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"
hidehiko 2017/05/01 12:29:18 Ditto.
yoshiki 2017/05/08 06:24:53 Done.
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 // Converts from Android notification priority to Chrome notification priority.
37 // On Android, PRIORITY_DEFAULT does not pop up, so this maps PRIORITY_DEFAULT
38 // to Chrome's -1 to adapt that behavior. Also, this maps PRIORITY_LOW and
39 // _HIGH to -2 and 0 respectively to adjust the value with keeping the order
40 // among _LOW, _DEFAULT and _HIGH. static
41 // TODO(yoshiki): rewrite this conversion as typemap
42 int ConvertAndroidPriority(mojom::ArcNotificationPriority android_priority) {
43 switch (android_priority) {
44 case mojom::ArcNotificationPriority::MIN:
45 case mojom::ArcNotificationPriority::LOW:
46 return message_center::MIN_PRIORITY;
47 case mojom::ArcNotificationPriority::DEFAULT:
48 return message_center::LOW_PRIORITY;
49 case mojom::ArcNotificationPriority::HIGH:
50 return message_center::DEFAULT_PRIORITY;
51 case mojom::ArcNotificationPriority::MAX:
52 return message_center::MAX_PRIORITY;
53
54 // fall-through
hidehiko 2017/05/01 12:29:18 Not a fall-through here...?
yoshiki 2017/05/08 06:24:53 Removed
55 default:
56 NOTREACHED() << "Invalid Priority: " << android_priority;
hidehiko 2017/05/01 12:29:18 Can this be out from switch, so some compiler capt
yoshiki 2017/05/08 06:24:53 Done.
57 return message_center::DEFAULT_PRIORITY;
58 }
59 }
60
61 } // anonymous namespace
62
63 ArcNotificationItemImpl::ArcNotificationItemImpl(
64 ArcNotificationManager* manager,
65 message_center::MessageCenter* message_center,
66 const std::string& notification_key,
67 const AccountId& profile_id)
68 : manager_(manager),
69 message_center_(message_center),
70 profile_id_(profile_id),
71 notification_key_(notification_key),
72 notification_id_(kNotificationIdPrefix + notification_key_),
73 weak_ptr_factory_(this) {}
74
75 ArcNotificationItemImpl::~ArcNotificationItemImpl() {
76 for (auto& observer : observers_)
77 observer.OnItemDestroying();
78 }
79
80 void ArcNotificationItemImpl::UpdateWithArcNotificationData(
81 mojom::ArcNotificationDataPtr data) {
82 DCHECK(CalledOnValidThread());
83 DCHECK_EQ(notification_key_, data->key);
84
85 if (HasPendingNotification()) {
hidehiko 2017/05/08 09:34:41 Is this no longer needed? The original code still
yoshiki 2017/05/08 09:41:53 This was used by NON-CUSTOM arc notification, but
hidehiko 2017/05/08 10:12:39 Got it. Thank you for explanation. Description loo
yoshiki 2017/05/08 14:31:18 Actually I had updated, but let me make it clearer
86 CacheArcNotificationData(std::move(data));
87 return;
88 }
89
90 message_center::RichNotificationData rich_data;
91 rich_data.pinned = (data->no_clear || data->ongoing_event);
92 rich_data.priority = ConvertAndroidPriority(data->priority);
93 if (data->small_icon)
94 rich_data.small_image = gfx::Image::CreateFrom1xBitmap(*data->small_icon);
95 if (data->accessible_name.has_value())
96 rich_data.accessible_name = base::UTF8ToUTF16(*data->accessible_name);
97
98 message_center::NotifierId notifier_id(
99 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId);
100 notifier_id.profile_id = profile_id().GetUserEmail();
101
102 auto notification = base::MakeUnique<message_center::Notification>(
103 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(),
104 base::UTF8ToUTF16(data->title), base::UTF8ToUTF16(data->message),
105 gfx::Image(),
106 base::UTF8ToUTF16("arc"), // display source
107 GURL(), // empty origin url, for system component
108 notifier_id, rich_data,
109 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr()));
110 notification->set_timestamp(base::Time::FromJavaTime(data->time));
111 SetNotification(std::move(notification));
112
113 pinned_ = rich_data.pinned;
114 expand_state_ = data->expand_state;
115 shown_contents_ = data->shown_contents;
116
117 if (!data->snapshot_image || data->snapshot_image->isNull()) {
118 snapshot_ = gfx::ImageSkia();
119 } else {
120 snapshot_ = gfx::ImageSkia(
121 gfx::ImageSkiaRep(*data->snapshot_image, data->snapshot_image_scale));
122 }
123
124 for (auto& observer : observers_)
125 observer.OnItemUpdated();
126
127 AddToMessageCenter();
128 }
129
130 void ArcNotificationItemImpl::OnClosedFromAndroid() {
131 being_removed_by_manager_ = true; // Closing is initiated by the manager.
132 message_center_->RemoveNotification(notification_id_, false /* by_user */);
133 }
134
135 void ArcNotificationItemImpl::Close(bool by_user) {
136 if (being_removed_by_manager_) {
137 // Closing is caused by the manager, so we don't need to nofify a close
138 // event to the manager.
139 return;
140 }
141
142 // Do not touch its any members afterwards, because this instance will be
143 // destroyed in the following call
144 manager_->SendNotificationRemovedFromChrome(notification_key_);
145 }
146
147 void ArcNotificationItemImpl::Click() {
148 manager_->SendNotificationClickedOnChrome(notification_key_);
149 }
150
151 void ArcNotificationItemImpl::OpenSettings() {
152 manager_->OpenNotificationSettings(notification_key_);
153 }
154
155 bool ArcNotificationItemImpl::IsOpeningSettingsSupported() const {
156 return manager_->IsOpeningSettingsSupported();
157 }
158
159 void ArcNotificationItemImpl::ToggleExpansion() {
160 manager_->SendNotificationToggleExpansionOnChrome(notification_key_);
161 }
162
163 bool ArcNotificationItemImpl::HasPendingNotification() {
164 return (notification_ != nullptr);
165 }
166
167 void ArcNotificationItemImpl::CacheArcNotificationData(
168 mojom::ArcNotificationDataPtr data) {
169 // If old |newer_data_| has been stored, discard the old one.
170 newer_data_ = std::move(data);
171 }
172
173 void ArcNotificationItemImpl::SetNotification(
174 std::unique_ptr<message_center::Notification> notification) {
175 notification_ = std::move(notification);
176 }
177
178 void ArcNotificationItemImpl::AddToMessageCenter() {
179 DCHECK(notification_);
180 message_center_->AddNotification(std::move(notification_));
181
182 if (newer_data_) {
183 // There is the newer data, so updates again.
184 UpdateWithArcNotificationData(std::move(newer_data_));
185 }
186 }
187
188 bool ArcNotificationItemImpl::CalledOnValidThread() const {
189 return thread_checker_.CalledOnValidThread();
190 }
191
192 void ArcNotificationItemImpl::OnImageDecoded(const SkBitmap& bitmap) {
hidehiko 2017/05/01 12:29:18 No caller?
yoshiki 2017/05/08 06:24:53 Removed.
193 DCHECK(thread_checker_.CalledOnValidThread());
194
195 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap);
196 notification_->set_icon(image);
197 AddToMessageCenter();
198 }
199
200 void ArcNotificationItemImpl::CloseFromCloseButton() {
201 // Needs to manually remove notification from MessageCenter because
202 // the floating close button is not part of MessageCenter.
203 message_center()->RemoveNotification(notification_id(), true);
204 Close(true);
205 }
206
207 void ArcNotificationItemImpl::AddObserver(Observer* observer) {
208 observers_.AddObserver(observer);
209 }
210
211 void ArcNotificationItemImpl::RemoveObserver(Observer* observer) {
212 observers_.RemoveObserver(observer);
213 }
214
215 void ArcNotificationItemImpl::IncrementWindowRefCount() {
216 ++window_ref_count_;
217 if (window_ref_count_ == 1)
218 manager()->CreateNotificationWindow(notification_key_);
219 }
220
221 void ArcNotificationItemImpl::DecrementWindowRefCount() {
222 DCHECK_GT(window_ref_count_, 0);
223 --window_ref_count_;
224 if (window_ref_count_ == 0)
225 manager()->CloseNotificationWindow(notification_key_);
226 }
227
228 bool ArcNotificationItemImpl::GetPinned() const {
229 return pinned_;
230 }
231
232 const gfx::ImageSkia& ArcNotificationItemImpl::GetSnapshot() const {
233 return snapshot_;
234 }
235
236 mojom::ArcNotificationExpandState ArcNotificationItemImpl::GetExpandState()
237 const {
238 return expand_state_;
239 }
240
241 mojom::ArcNotificationShownContents ArcNotificationItemImpl::GetShownContents()
242 const {
243 return shown_contents_;
244 }
245
246 const std::string& ArcNotificationItemImpl::GetNotificationKey() const {
247 return notification_key_;
248 }
249
250 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698