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

Side by Side Diff: ui/arc/notification/arc_custom_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: for comments in #2 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
(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 "base/memory/ptr_util.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/exo/notification_surface.h"
11 #include "ui/arc/notification/arc_custom_notification_view.h"
12 #include "ui/message_center/notification.h"
13 #include "ui/message_center/notification_types.h"
14
15 namespace arc {
16
17 namespace {
18
19 constexpr char kNotifierId[] = "ARC_NOTIFICATION";
20
21 class ArcNotificationDelegate : public message_center::NotificationDelegate {
22 public:
23 ArcNotificationDelegate() {}
24
25 std::unique_ptr<views::View> CreateCustomContent() override {
26 if (!surface_)
27 return std::unique_ptr<views::View>();
28
29 return base::WrapUnique(new ArcCustomNotificationView(surface_));
30 }
31
32 void set_notification_surface(exo::NotificationSurface* surface) {
33 surface_ = surface;
34 }
35
36 private:
37 // The destructor is private since this class is ref-counted.
38 ~ArcNotificationDelegate() override {}
39
40 exo::NotificationSurface* surface_ = nullptr;
41
42 DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate);
43 };
44
45 } // namespace
46
47 ArcCustomNotificationItem::ArcCustomNotificationItem(
48 ArcNotificationManager* manager,
49 message_center::MessageCenter* message_center,
50 const std::string& notification_key,
51 const AccountId& profile_id)
52 : ArcNotificationItem(manager,
53 message_center,
54 notification_key,
55 profile_id) {}
56
57 ArcCustomNotificationItem::~ArcCustomNotificationItem() {
58 exo::NotificationSurfaceRegistry::Get()->RemoveObserver(this);
59 }
60
61 void ArcCustomNotificationItem::UpdateWithArcNotificationData(
62 const mojom::ArcNotificationData& data) {
63 DCHECK(CalledOnValidThread());
64 DCHECK(notification_key() == data.key);
65
66 if (CacheArcNotificationData(data))
67 return;
68
69 message_center::RichNotificationData rich_data;
70 rich_data.pinned = (data.no_clear || data.ongoing_event);
71 rich_data.priority = ConvertAndroidPriority(data.priority);
72
73 message_center::NotifierId notifier_id(
74 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId);
75 notifier_id.profile_id = profile_id().GetUserEmail();
76
77 DCHECK(!data.title.is_null());
78 DCHECK(!data.message.is_null());
79 SetNotification(base::WrapUnique(new message_center::Notification(
80 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(),
81 base::UTF8ToUTF16(data.title.get()),
82 base::UTF8ToUTF16(data.message.get()), gfx::Image(),
83 base::UTF8ToUTF16("arc"), // display source
84 GURL(), // empty origin url, for system component
85 notifier_id, rich_data, new ArcNotificationDelegate())));
86
87 exo::NotificationSurfaceRegistry* surface_registry =
88 exo::NotificationSurfaceRegistry::Get();
89 exo::NotificationSurface* surface =
90 surface_registry->GetSurface(notification_key());
91 if (surface)
92 OnNotificationSurfaceAdded(surface);
93 else
94 surface_registry->AddObserver(this);
95 }
96
97 void ArcCustomNotificationItem::OnNotificationSurfaceAdded(
98 exo::NotificationSurface* surface) {
99 if (!pending_notification() ||
100 surface->notification_id() != notification_key())
101 return;
102
103 static_cast<ArcNotificationDelegate*>(pending_notification()->delegate())
104 ->set_notification_surface(surface);
105 AddToMessageCenter();
106 }
107
108 void ArcCustomNotificationItem::OnNotificationSurfaceRemoved(
109 exo::NotificationSurface* surface) {
110 if (surface->notification_id() != notification_key())
111 return;
112
113 OnClosedFromAndroid(false);
114 }
115
116 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698