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

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

Issue 2308663002: Migrate ArcBitmap to use typemapping (Closed)
Patch Set: Created 4 years, 3 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/arc/notification/arc_notification_item.h" 5 #include "ui/arc/notification/arc_notification_item.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/task_runner.h" 13 #include "base/task_runner.h"
14 #include "base/task_runner_util.h" 14 #include "base/task_runner_util.h"
15 #include "base/threading/worker_pool.h" 15 #include "base/threading/worker_pool.h"
16 #include "components/arc/bitmap/bitmap_type_converters.h"
17 #include "third_party/skia/include/core/SkCanvas.h" 16 #include "third_party/skia/include/core/SkCanvas.h"
18 #include "third_party/skia/include/core/SkPaint.h" 17 #include "third_party/skia/include/core/SkPaint.h"
19 #include "ui/gfx/codec/png_codec.h" 18 #include "ui/gfx/codec/png_codec.h"
20 #include "ui/gfx/geometry/size.h" 19 #include "ui/gfx/geometry/size.h"
21 #include "ui/gfx/image/image.h" 20 #include "ui/gfx/image/image.h"
22 #include "ui/gfx/text_elider.h" 21 #include "ui/gfx/text_elider.h"
23 #include "ui/message_center/message_center_style.h" 22 #include "ui/message_center/message_center_style.h"
24 #include "ui/message_center/notification.h" 23 #include "ui/message_center/notification.h"
25 #include "ui/message_center/notification_types.h" 24 #include "ui/message_center/notification_types.h"
26 #include "ui/message_center/notifier_settings.h" 25 #include "ui/message_center/notifier_settings.h"
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 const std::string& notification_key, 128 const std::string& notification_key,
130 const AccountId& profile_id) 129 const AccountId& profile_id)
131 : manager_(manager), 130 : manager_(manager),
132 message_center_(message_center), 131 message_center_(message_center),
133 profile_id_(profile_id), 132 profile_id_(profile_id),
134 notification_key_(notification_key), 133 notification_key_(notification_key),
135 notification_id_(kNotificationIdPrefix + notification_key_), 134 notification_id_(kNotificationIdPrefix + notification_key_),
136 weak_ptr_factory_(this) {} 135 weak_ptr_factory_(this) {}
137 136
138 void ArcNotificationItem::UpdateWithArcNotificationData( 137 void ArcNotificationItem::UpdateWithArcNotificationData(
139 const mojom::ArcNotificationData& data) { 138 mojom::ArcNotificationDataPtr data) {
139 LOG(ERROR) << data->type;
xiyuan 2016/09/07 22:09:00 nit: remove ?
yoshiki 2016/09/09 16:45:30 Oh sorry. Done.
140 DCHECK(thread_checker_.CalledOnValidThread()); 140 DCHECK(thread_checker_.CalledOnValidThread());
141 DCHECK(notification_key_ == data.key); 141 DCHECK(notification_key_ == data->key);
142 142
143 // Check if a decode task is on-going or not. If |notification_| is non-null, 143 // Check if a decode task is on-going or not. If |notification_| is non-null,
144 // a decode task is on-going asynchronously. Otherwise, there is no task. 144 // a decode task is on-going asynchronously. Otherwise, there is no task and
145 // cache the latest data to the |newer_data_| property.
145 // TODO(yoshiki): Refactor and remove this check by omitting image decoding 146 // TODO(yoshiki): Refactor and remove this check by omitting image decoding
146 // from here. 147 // from here.
147 if (CacheArcNotificationData(data)) 148 if (HasPendingNotification()) {
149 CacheArcNotificationData(std::move(data));
148 return; 150 return;
151 }
149 152
150 message_center::RichNotificationData rich_data; 153 message_center::RichNotificationData rich_data;
151 message_center::NotificationType type; 154 message_center::NotificationType type;
152 155
153 switch (data.type) { 156 switch (data->type) {
154 case mojom::ArcNotificationType::BASIC: 157 case mojom::ArcNotificationType::BASIC:
155 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; 158 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT;
156 break; 159 break;
157 case mojom::ArcNotificationType::LIST: 160 case mojom::ArcNotificationType::LIST:
158 type = message_center::NOTIFICATION_TYPE_MULTIPLE; 161 type = message_center::NOTIFICATION_TYPE_MULTIPLE;
159 162
160 if (data.texts.is_null()) 163 LOG(ERROR) << "0";
xiyuan 2016/09/07 22:09:00 nit: remove this and other debugging logs ?
yoshiki 2016/09/09 16:45:30 Done.
164 if (data->texts.is_null())
161 break; 165 break;
166 LOG(ERROR) << "1";
162 167
163 for (size_t i = 0; 168 for (size_t i = 0;
164 i < std::min(data.texts.size(), 169 i < std::min(data->texts.size(),
165 message_center::kNotificationMaximumItems - 1); 170 message_center::kNotificationMaximumItems - 1);
166 i++) { 171 i++) {
167 rich_data.items.emplace_back( 172 rich_data.items.emplace_back(
168 base::string16(), base::UTF8ToUTF16(data.texts.at(i).get())); 173 base::string16(), base::UTF8ToUTF16(data->texts.at(i).get()));
169 } 174 }
175 LOG(ERROR) << "2";
170 176
171 if (data.texts.size() > message_center::kNotificationMaximumItems) { 177 if (data->texts.size() > message_center::kNotificationMaximumItems) {
172 // Show an elipsis as the 5th item if there are more than 5 items. 178 // Show an elipsis as the 5th item if there are more than 5 items.
173 rich_data.items.emplace_back(base::string16(), gfx::kEllipsisUTF16); 179 rich_data.items.emplace_back(base::string16(), gfx::kEllipsisUTF16);
174 } else if (data.texts.size() == 180 LOG(ERROR) << "3";
181 } else if (data->texts.size() ==
175 message_center::kNotificationMaximumItems) { 182 message_center::kNotificationMaximumItems) {
176 // Show the 5th item if there are exact 5 items. 183 // Show the 5th item if there are exact 5 items.
177 rich_data.items.emplace_back( 184 rich_data.items.emplace_back(
178 base::string16(), 185 base::string16(),
179 base::UTF8ToUTF16(data.texts.at(data.texts.size() - 1).get())); 186 base::UTF8ToUTF16(data->texts.at(data->texts.size() - 1).get()));
187 LOG(ERROR) << "3";
180 } 188 }
181 break; 189 break;
182 case mojom::ArcNotificationType::IMAGE: 190 case mojom::ArcNotificationType::IMAGE:
191 LOG(ERROR) << "0";
183 type = message_center::NOTIFICATION_TYPE_IMAGE; 192 type = message_center::NOTIFICATION_TYPE_IMAGE;
193 LOG(ERROR) << "1";
184 194
185 if (!data.big_picture.is_null()) { 195 if (data->big_picture && !data->big_picture->isNull()) {
186 rich_data.image = gfx::Image::CreateFrom1xBitmap( 196 rich_data.image = gfx::Image::CreateFrom1xBitmap(
187 CropImage(data.big_picture.To<SkBitmap>())); 197 CropImage(*data->big_picture));
188 } 198 }
199 LOG(ERROR) << "2";
189 break; 200 break;
190 case mojom::ArcNotificationType::PROGRESS: 201 case mojom::ArcNotificationType::PROGRESS:
202 LOG(ERROR) << "0";
191 type = message_center::NOTIFICATION_TYPE_PROGRESS; 203 type = message_center::NOTIFICATION_TYPE_PROGRESS;
192 rich_data.timestamp = base::Time::UnixEpoch() + 204 rich_data.timestamp = base::Time::UnixEpoch() +
193 base::TimeDelta::FromMilliseconds(data.time); 205 base::TimeDelta::FromMilliseconds(data->time);
206 LOG(ERROR) << "1";
194 rich_data.progress = std::max( 207 rich_data.progress = std::max(
195 0, std::min(100, static_cast<int>(std::round( 208 0, std::min(100, static_cast<int>(std::round(
196 static_cast<float>(data.progress_current) / 209 static_cast<float>(data->progress_current) /
197 data.progress_max * 100)))); 210 data->progress_max * 100))));
211 LOG(ERROR) << "2";
198 break; 212 break;
199 } 213 }
200 DCHECK(IsKnownEnumValue(data.type)) << "Unsupported notification type: " 214 DCHECK(IsKnownEnumValue(data->type)) << "Unsupported notification type: "
201 << data.type; 215 << data->type;
202 216
203 for (size_t i = 0; i < data.buttons.size(); i++) { 217 LOG(ERROR) << "2";
218 for (size_t i = 0; i < data->buttons.size(); i++) {
204 rich_data.buttons.emplace_back( 219 rich_data.buttons.emplace_back(
205 base::UTF8ToUTF16(data.buttons.at(i)->label.get())); 220 base::UTF8ToUTF16(data->buttons.at(i)->label.get()));
206 } 221 }
207 222
223 LOG(ERROR) << "3";
208 // If the client is old (version < 1), both |no_clear| and |ongoing_event| 224 // If the client is old (version < 1), both |no_clear| and |ongoing_event|
209 // are false. 225 // are false.
210 rich_data.pinned = (data.no_clear || data.ongoing_event); 226 rich_data.pinned = (data->no_clear || data->ongoing_event);
211 227
212 rich_data.priority = ConvertAndroidPriority(data.priority); 228 rich_data.priority = ConvertAndroidPriority(data->priority);
213 rich_data.small_image = ConvertAndroidSmallIcon(data.small_icon); 229 if (data->small_icon)
230 rich_data.small_image = gfx::Image::CreateFrom1xBitmap(*data->small_icon);
231 LOG(ERROR) << "4";
214 232
215 // The identifier of the notifier, which is used to distinguish the notifiers 233 // The identifier of the notifier, which is used to distinguish the notifiers
216 // in the message center. 234 // in the message center.
217 message_center::NotifierId notifier_id( 235 message_center::NotifierId notifier_id(
218 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); 236 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId);
219 notifier_id.profile_id = profile_id_.GetUserEmail(); 237 notifier_id.profile_id = profile_id_.GetUserEmail();
220 238
221 DCHECK(!data.title.is_null()); 239 LOG(ERROR) << "5";
222 DCHECK(!data.message.is_null()); 240 DCHECK(!data->title.is_null());
241 DCHECK(!data->message.is_null());
223 SetNotification(base::MakeUnique<message_center::Notification>( 242 SetNotification(base::MakeUnique<message_center::Notification>(
224 type, notification_id_, base::UTF8ToUTF16(data.title.get()), 243 type, notification_id_, base::UTF8ToUTF16(data->title.get()),
225 base::UTF8ToUTF16(data.message.get()), 244 base::UTF8ToUTF16(data->message.get()),
226 gfx::Image(), // icon image: Will be overriden later. 245 gfx::Image(), // icon image: Will be overriden later.
227 base::UTF8ToUTF16("arc"), // display source 246 base::UTF8ToUTF16("arc"), // display source
228 GURL(), // empty origin url, for system component 247 GURL(), // empty origin url, for system component
229 notifier_id, rich_data, 248 notifier_id, rich_data,
230 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr()))); 249 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr())));
231 250
232 DCHECK(!data.icon_data.is_null()); 251 DCHECK(!data->icon_data.is_null());
233 if (data.icon_data.size() == 0) { 252 if (data->icon_data.size() == 0) {
234 OnImageDecoded(SkBitmap()); // Pass an empty bitmap. 253 OnImageDecoded(SkBitmap()); // Pass an empty bitmap.
235 return; 254 return;
236 } 255 }
256 LOG(ERROR) << "6";
237 257
238 // TODO(yoshiki): Remove decoding by passing a bitmap directly from Android. 258 // TODO(yoshiki): Remove decoding by passing a bitmap directly from Android.
239 base::PostTaskAndReplyWithResult( 259 base::PostTaskAndReplyWithResult(
240 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, 260 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE,
241 base::Bind(&DecodeImage, data.icon_data.storage()), 261 base::Bind(&DecodeImage, data->icon_data.storage()),
242 base::Bind(&ArcNotificationItem::OnImageDecoded, 262 base::Bind(&ArcNotificationItem::OnImageDecoded,
243 weak_ptr_factory_.GetWeakPtr())); 263 weak_ptr_factory_.GetWeakPtr()));
264 LOG(ERROR) << "7";
244 } 265 }
245 266
246 ArcNotificationItem::~ArcNotificationItem() {} 267 ArcNotificationItem::~ArcNotificationItem() {}
247 268
248 void ArcNotificationItem::OnClosedFromAndroid(bool by_user) { 269 void ArcNotificationItem::OnClosedFromAndroid(bool by_user) {
249 being_removed_by_manager_ = true; // Closing is initiated by the manager. 270 being_removed_by_manager_ = true; // Closing is initiated by the manager.
250 message_center_->RemoveNotification(notification_id_, by_user); 271 message_center_->RemoveNotification(notification_id_, by_user);
251 } 272 }
252 273
253 void ArcNotificationItem::Close(bool by_user) { 274 void ArcNotificationItem::Close(bool by_user) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 case 1: // PRIORITY_HIGH 308 case 1: // PRIORITY_HIGH
288 return 0; 309 return 0;
289 case 2: // PRIORITY_MAX 310 case 2: // PRIORITY_MAX
290 return 2; 311 return 2;
291 default: 312 default:
292 NOTREACHED() << "Invalid Priority: " << android_priority; 313 NOTREACHED() << "Invalid Priority: " << android_priority;
293 return 0; 314 return 0;
294 } 315 }
295 } 316 }
296 317
297 // static 318 bool ArcNotificationItem::HasPendingNotification() {
298 gfx::Image ArcNotificationItem::ConvertAndroidSmallIcon( 319 return (notification_ != nullptr);
299 const mojom::ArcBitmapPtr& arc_bitmap) {
300 if (arc_bitmap.is_null())
301 return gfx::Image();
302
303 return gfx::Image::CreateFrom1xBitmap(arc_bitmap.To<SkBitmap>());
304 } 320 }
305 321
306 bool ArcNotificationItem::CacheArcNotificationData( 322 void ArcNotificationItem::CacheArcNotificationData(
307 const mojom::ArcNotificationData& data) { 323 mojom::ArcNotificationDataPtr data) {
308 if (!notification_)
309 return false;
310
311 // Store the latest data to the |newer_data_| property if there is a pending
312 // |notification_|.
313 // If old |newer_data_| has been stored, discard the old one. 324 // If old |newer_data_| has been stored, discard the old one.
314 newer_data_ = data.Clone(); 325 newer_data_ = std::move(data);
315 return true;
316 } 326 }
317 327
318 void ArcNotificationItem::SetNotification( 328 void ArcNotificationItem::SetNotification(
319 std::unique_ptr<message_center::Notification> notification) { 329 std::unique_ptr<message_center::Notification> notification) {
320 notification_ = std::move(notification); 330 notification_ = std::move(notification);
321 } 331 }
322 332
323 void ArcNotificationItem::AddToMessageCenter() { 333 void ArcNotificationItem::AddToMessageCenter() {
324 DCHECK(notification_); 334 DCHECK(notification_);
325 message_center_->AddNotification(std::move(notification_)); 335 message_center_->AddNotification(std::move(notification_));
326 336
327 if (newer_data_) { 337 if (newer_data_) {
328 // There is the newer data, so updates again. 338 // There is the newer data, so updates again.
329 mojom::ArcNotificationDataPtr data(std::move(newer_data_)); 339 UpdateWithArcNotificationData(std::move(newer_data_));
330 UpdateWithArcNotificationData(*data);
331 } 340 }
332 } 341 }
333 342
334 bool ArcNotificationItem::CalledOnValidThread() const { 343 bool ArcNotificationItem::CalledOnValidThread() const {
335 return thread_checker_.CalledOnValidThread(); 344 return thread_checker_.CalledOnValidThread();
336 } 345 }
337 346
338 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) { 347 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) {
339 DCHECK(thread_checker_.CalledOnValidThread()); 348 DCHECK(thread_checker_.CalledOnValidThread());
340 349
341 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); 350 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap);
342 notification_->set_icon(image); 351 notification_->set_icon(image);
343 AddToMessageCenter(); 352 AddToMessageCenter();
344 } 353 }
345 354
346 } // namespace arc 355 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698