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

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

Issue 2066853002: arc: Show custom notification via notification surface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@notification-exo
Patch Set: clean up Created 4 years, 6 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 <memory>
8 #include <vector> 9 #include <vector>
9 10
11 #include "base/memory/ptr_util.h"
10 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
12 #include "base/task_runner.h" 14 #include "base/task_runner.h"
13 #include "base/task_runner_util.h" 15 #include "base/task_runner_util.h"
14 #include "base/threading/worker_pool.h" 16 #include "base/threading/worker_pool.h"
15 #include "components/arc/bitmap/bitmap_type_converters.h" 17 #include "components/arc/bitmap/bitmap_type_converters.h"
16 #include "third_party/skia/include/core/SkCanvas.h" 18 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkPaint.h" 19 #include "third_party/skia/include/core/SkPaint.h"
20 #include "ui/arc/notification/arc_custom_notification_view.h"
18 #include "ui/gfx/codec/png_codec.h" 21 #include "ui/gfx/codec/png_codec.h"
19 #include "ui/gfx/geometry/size.h" 22 #include "ui/gfx/geometry/size.h"
20 #include "ui/gfx/image/image.h" 23 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/text_elider.h" 24 #include "ui/gfx/text_elider.h"
22 #include "ui/message_center/message_center_style.h" 25 #include "ui/message_center/message_center_style.h"
23 #include "ui/message_center/notification.h" 26 #include "ui/message_center/notification.h"
24 #include "ui/message_center/notification_types.h" 27 #include "ui/message_center/notification_types.h"
25 #include "ui/message_center/notifier_settings.h" 28 #include "ui/message_center/notifier_settings.h"
26 29
27 namespace arc { 30 namespace arc {
28 31
29 namespace { 32 namespace {
30 33
31 static const char kNotifierId[] = "ARC_NOTIFICATION"; 34 constexpr char kNotifierId[] = "ARC_NOTIFICATION";
35 constexpr char kNotificationIdPrefix[] = "ARC_NOTIFICATION_";
32 36
33 static const char kNotificationIdPrefix[] = "ARC_NOTIFICATION_"; 37 // Whether to use notification surface.
38 constexpr bool use_notification_surface = true;
34 39
35 SkBitmap DecodeImage(const std::vector<uint8_t>& data) { 40 SkBitmap DecodeImage(const std::vector<uint8_t>& data) {
36 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread()); 41 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread());
37 DCHECK(!data.empty()); // empty string should be handled in caller. 42 DCHECK(!data.empty()); // empty string should be handled in caller.
38 43
39 // We may decode an image in the browser process since it has been generated 44 // We may decode an image in the browser process since it has been generated
40 // in NotificationListerService in Android and should be safe. 45 // in NotificationListerService in Android and should be safe.
41 SkBitmap bitmap; 46 SkBitmap bitmap;
42 gfx::PNGCodec::Decode(&data[0], data.size(), &bitmap); 47 gfx::PNGCodec::Decode(&data[0], data.size(), &bitmap);
43 return bitmap; 48 return bitmap;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 void Click() override { 132 void Click() override {
128 if (item_) 133 if (item_)
129 item_->Click(); 134 item_->Click();
130 } 135 }
131 136
132 void ButtonClick(int button_index) override { 137 void ButtonClick(int button_index) override {
133 if (item_) 138 if (item_)
134 item_->ButtonClick(button_index); 139 item_->ButtonClick(button_index);
135 } 140 }
136 141
142 std::unique_ptr<views::View> CreateCustomContent() override {
143 if (!item_ || !surface_)
144 return std::unique_ptr<views::View>();
145
146 return base::WrapUnique(new ArcCustomNotificationView(surface_));
147 }
148
149 void set_notification_surface(exo::NotificationSurface* surface) {
150 surface_ = surface;
151 }
152
137 private: 153 private:
138 // The destructor is private since this class is ref-counted. 154 // The destructor is private since this class is ref-counted.
139 ~ArcNotificationDelegate() override {} 155 ~ArcNotificationDelegate() override {}
140 156
141 base::WeakPtr<ArcNotificationItem> item_; 157 base::WeakPtr<ArcNotificationItem> item_;
158 exo::NotificationSurface* surface_ = nullptr;
142 159
143 DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate); 160 DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate);
144 }; 161 };
145 162
146 } // anonymous namespace 163 } // anonymous namespace
147 164
148 ArcNotificationItem::ArcNotificationItem( 165 ArcNotificationItem::ArcNotificationItem(
149 ArcNotificationManager* manager, 166 ArcNotificationManager* manager,
150 message_center::MessageCenter* message_center, 167 message_center::MessageCenter* message_center,
151 const std::string& notification_key, 168 const std::string& notification_key,
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 rich_data.pinned = (data.no_clear || data.ongoing_event); 254 rich_data.pinned = (data.no_clear || data.ongoing_event);
238 255
239 rich_data.priority = convertAndroidPriority(data.priority); 256 rich_data.priority = convertAndroidPriority(data.priority);
240 257
241 // The identifier of the notifier, which is used to distinguish the notifiers 258 // The identifier of the notifier, which is used to distinguish the notifiers
242 // in the message center. 259 // in the message center.
243 message_center::NotifierId notifier_id( 260 message_center::NotifierId notifier_id(
244 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); 261 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId);
245 notifier_id.profile_id = profile_id_.GetUserEmail(); 262 notifier_id.profile_id = profile_id_.GetUserEmail();
246 263
264 if (use_notification_surface)
265 type = message_center::NOTIFICATION_TYPE_CUSTOM;
266
247 DCHECK(!data.title.is_null()); 267 DCHECK(!data.title.is_null());
248 DCHECK(!data.message.is_null()); 268 DCHECK(!data.message.is_null());
249 notification_.reset(new message_center::Notification( 269 notification_.reset(new message_center::Notification(
250 type, notification_id_, base::UTF8ToUTF16(data.title.get()), 270 type, notification_id_, base::UTF8ToUTF16(data.title.get()),
251 base::UTF8ToUTF16(data.message.get()), 271 base::UTF8ToUTF16(data.message.get()),
252 gfx::Image(), // icon image: Will be overriden later. 272 gfx::Image(), // icon image: Will be overriden later.
253 base::UTF8ToUTF16("arc"), // display source 273 base::UTF8ToUTF16("arc"), // display source
254 GURL(), // empty origin url, for system component 274 GURL(), // empty origin url, for system component
255 notifier_id, rich_data, 275 notifier_id, rich_data,
256 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr()))); 276 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr())));
257 277
278 if (use_notification_surface) {
279 ArcNotificationSurfaceCollection* surface_collection =
280 manager_->notification_surface_collection();
281 exo::NotificationSurface* surface =
282 surface_collection->GetSurface(notification_id_);
283 if (surface)
284 OnNotificationSurfaceAdded(surface);
285 else
286 surface_collection->AddObserver(this);
287 return;
yoshiki 2016/06/15 11:47:20 Please call AddToMessageCenter(). At least, we nee
xiyuan 2016/06/15 19:32:44 AddToMessageCenter() is called once the surface is
yoshiki 2016/06/16 02:44:50 I got it. Thank you for explanation.
288 }
289
258 DCHECK(!data.icon_data.is_null()); 290 DCHECK(!data.icon_data.is_null());
259 if (data.icon_data.size() == 0) { 291 if (data.icon_data.size() == 0) {
260 OnImageDecoded(SkBitmap()); // Pass an empty bitmap. 292 OnImageDecoded(SkBitmap()); // Pass an empty bitmap.
261 return; 293 return;
262 } 294 }
263 295
264 // TODO(yoshiki): Remove decoding by passing a bitmap directly from Android. 296 // TODO(yoshiki): Remove decoding by passing a bitmap directly from Android.
265 base::PostTaskAndReplyWithResult( 297 base::PostTaskAndReplyWithResult(
266 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, 298 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE,
267 base::Bind(&DecodeImage, data.icon_data.storage()), 299 base::Bind(&DecodeImage, data.icon_data.storage()),
268 base::Bind(&ArcNotificationItem::OnImageDecoded, 300 base::Bind(&ArcNotificationItem::OnImageDecoded,
269 weak_ptr_factory_.GetWeakPtr())); 301 weak_ptr_factory_.GetWeakPtr()));
270 } 302 }
271 303
272 ArcNotificationItem::~ArcNotificationItem() {} 304 ArcNotificationItem::~ArcNotificationItem() {
305 manager_->notification_surface_collection()->RemoveObserver(this);
306 }
273 307
274 void ArcNotificationItem::OnClosedFromAndroid(bool by_user) { 308 void ArcNotificationItem::OnClosedFromAndroid(bool by_user) {
275 being_removed_by_manager_ = true; // Closing is initiated by the manager. 309 being_removed_by_manager_ = true; // Closing is initiated by the manager.
276 message_center_->RemoveNotification(notification_id_, by_user); 310 message_center_->RemoveNotification(notification_id_, by_user);
277 } 311 }
278 312
279 void ArcNotificationItem::Close(bool by_user) { 313 void ArcNotificationItem::Close(bool by_user) {
280 if (being_removed_by_manager_) { 314 if (being_removed_by_manager_) {
281 // Closing is caused by the manager, so we don't need to nofify a close 315 // Closing is caused by the manager, so we don't need to nofify a close
282 // event to the manager. 316 // event to the manager.
283 return; 317 return;
284 } 318 }
285 319
286 // Do not touch its any members afterwards, because this instance will be 320 // Do not touch its any members afterwards, because this instance will be
287 // destroyed in the following call 321 // destroyed in the following call
288 manager_->SendNotificationRemovedFromChrome(notification_key_); 322 manager_->SendNotificationRemovedFromChrome(notification_key_);
289 } 323 }
290 324
291 void ArcNotificationItem::Click() { 325 void ArcNotificationItem::Click() {
292 manager_->SendNotificationClickedOnChrome(notification_key_); 326 manager_->SendNotificationClickedOnChrome(notification_key_);
293 } 327 }
294 328
295 void ArcNotificationItem::ButtonClick(int button_index) { 329 void ArcNotificationItem::ButtonClick(int button_index) {
296 manager_->SendNotificationButtonClickedOnChrome( 330 manager_->SendNotificationButtonClickedOnChrome(
297 notification_key_, button_index); 331 notification_key_, button_index);
298 } 332 }
299 333
334 void ArcNotificationItem::OnNotificationSurfaceAdded(
335 exo::NotificationSurface* surface) {
336 if (!notification_ || surface->notification_id() != notification_key_)
337 return;
yoshiki 2016/06/15 11:47:20 Ditto for calling AddTestMessageCenter().
xiyuan 2016/06/15 19:32:44 AddToMessageCenter() is called at the end. The con
338
339 static_cast<ArcNotificationDelegate*>(notification_->delegate())
340 ->set_notification_surface(surface);
341 AddToMessageCenter();
342 }
343
300 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) { 344 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) {
301 DCHECK(thread_checker_.CalledOnValidThread()); 345 DCHECK(thread_checker_.CalledOnValidThread());
302 346
303 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); 347 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap);
304 notification_->set_icon(image); 348 notification_->set_icon(image);
349 AddToMessageCenter();
350 }
305 351
352 void ArcNotificationItem::AddToMessageCenter() {
306 DCHECK(notification_); 353 DCHECK(notification_);
307 message_center_->AddNotification(std::move(notification_)); 354 message_center_->AddNotification(std::move(notification_));
308 355
309 if (newer_data_) { 356 if (newer_data_) {
310 // There is the newer data, so updates again. 357 // There is the newer data, so updates again.
311 mojom::ArcNotificationDataPtr data(std::move(newer_data_)); 358 mojom::ArcNotificationDataPtr data(std::move(newer_data_));
312 UpdateWithArcNotificationData(*data); 359 UpdateWithArcNotificationData(*data);
313 } 360 }
314 } 361 }
315 362
316 } // namespace arc 363 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698