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

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

Issue 2319893002: Merge "arc: Defer notification surface creation" (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_custom_notification_item.h" 5 #include "ui/arc/notification/arc_custom_notification_item.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "components/exo/notification_surface.h" 10 #include "components/arc/bitmap/bitmap_type_converters.h"
11 #include "ui/arc/notification/arc_custom_notification_view.h" 11 #include "ui/arc/notification/arc_custom_notification_view.h"
12 #include "ui/message_center/notification.h" 12 #include "ui/message_center/notification.h"
13 #include "ui/message_center/notification_types.h" 13 #include "ui/message_center/notification_types.h"
14 14
15 namespace arc { 15 namespace arc {
16 16
17 namespace { 17 namespace {
18 18
19 constexpr char kNotifierId[] = "ARC_NOTIFICATION"; 19 constexpr char kNotifierId[] = "ARC_NOTIFICATION";
20 20
21 class ArcNotificationDelegate : public message_center::NotificationDelegate { 21 class ArcNotificationDelegate : public message_center::NotificationDelegate {
22 public: 22 public:
23 explicit ArcNotificationDelegate(ArcCustomNotificationItem* item) 23 explicit ArcNotificationDelegate(ArcCustomNotificationItem* item)
24 : item_(item) {} 24 : item_(item) {}
25 25
26 std::unique_ptr<views::View> CreateCustomContent() override { 26 std::unique_ptr<views::View> CreateCustomContent() override {
27 if (!surface_) 27 return base::MakeUnique<ArcCustomNotificationView>(item_);
28 return nullptr;
29
30 return base::MakeUnique<ArcCustomNotificationView>(item_, surface_);
31 }
32
33 void set_notification_surface(exo::NotificationSurface* surface) {
34 surface_ = surface;
35 } 28 }
36 29
37 private: 30 private:
38 // The destructor is private since this class is ref-counted. 31 // The destructor is private since this class is ref-counted.
39 ~ArcNotificationDelegate() override {} 32 ~ArcNotificationDelegate() override {}
40 33
41 ArcCustomNotificationItem* const item_; 34 ArcCustomNotificationItem* const item_;
42 exo::NotificationSurface* surface_ = nullptr;
43 35
44 DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate); 36 DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate);
45 }; 37 };
46 38
47 } // namespace 39 } // namespace
48 40
49 ArcCustomNotificationItem::ArcCustomNotificationItem( 41 ArcCustomNotificationItem::ArcCustomNotificationItem(
50 ArcNotificationManager* manager, 42 ArcNotificationManager* manager,
51 message_center::MessageCenter* message_center, 43 message_center::MessageCenter* message_center,
52 const std::string& notification_key, 44 const std::string& notification_key,
53 const AccountId& profile_id) 45 const AccountId& profile_id)
54 : ArcNotificationItem(manager, 46 : ArcNotificationItem(manager,
55 message_center, 47 message_center,
56 notification_key, 48 notification_key,
57 profile_id) { 49 profile_id) {
58 ArcNotificationSurfaceManager::Get()->AddObserver(this);
59 } 50 }
60 51
61 ArcCustomNotificationItem::~ArcCustomNotificationItem() { 52 ArcCustomNotificationItem::~ArcCustomNotificationItem() {
62 if (ArcNotificationSurfaceManager::Get())
63 ArcNotificationSurfaceManager::Get()->RemoveObserver(this);
64
65 FOR_EACH_OBSERVER(Observer, observers_, OnItemDestroying()); 53 FOR_EACH_OBSERVER(Observer, observers_, OnItemDestroying());
66 } 54 }
67 55
68 void ArcCustomNotificationItem::UpdateWithArcNotificationData( 56 void ArcCustomNotificationItem::UpdateWithArcNotificationData(
69 const mojom::ArcNotificationData& data) { 57 const mojom::ArcNotificationData& data) {
70 DCHECK(CalledOnValidThread()); 58 DCHECK(CalledOnValidThread());
71 DCHECK_EQ(notification_key(), data.key); 59 DCHECK_EQ(notification_key(), data.key);
72 60
73 if (CacheArcNotificationData(data)) 61 if (CacheArcNotificationData(data))
74 return; 62 return;
(...skipping 10 matching lines...) Expand all
85 DCHECK(!data.title.is_null()); 73 DCHECK(!data.title.is_null());
86 DCHECK(!data.message.is_null()); 74 DCHECK(!data.message.is_null());
87 SetNotification(base::MakeUnique<message_center::Notification>( 75 SetNotification(base::MakeUnique<message_center::Notification>(
88 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(), 76 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(),
89 base::UTF8ToUTF16(data.title.get()), 77 base::UTF8ToUTF16(data.title.get()),
90 base::UTF8ToUTF16(data.message.get()), gfx::Image(), 78 base::UTF8ToUTF16(data.message.get()), gfx::Image(),
91 base::UTF8ToUTF16("arc"), // display source 79 base::UTF8ToUTF16("arc"), // display source
92 GURL(), // empty origin url, for system component 80 GURL(), // empty origin url, for system component
93 notifier_id, rich_data, new ArcNotificationDelegate(this))); 81 notifier_id, rich_data, new ArcNotificationDelegate(this)));
94 82
95 exo::NotificationSurface* surface = 83 pinned_ = rich_data.pinned;
96 ArcNotificationSurfaceManager::Get()->GetSurface(notification_key());
97 if (surface)
98 OnNotificationSurfaceAdded(surface);
99 84
100 pinned_ = rich_data.pinned; 85 if (data.snapshot_image.is_null()) {
101 FOR_EACH_OBSERVER(Observer, observers_, OnItemPinnedChanged()); 86 snapshot_ = gfx::ImageSkia();
87 } else {
88 snapshot_ = gfx::ImageSkia(gfx::ImageSkiaRep(
89 data.snapshot_image.To<SkBitmap>(), data.snapshot_image_scale));
90 }
91
92 FOR_EACH_OBSERVER(Observer, observers_, OnItemUpdated());
93
94 AddToMessageCenter();
102 } 95 }
103 96
104 void ArcCustomNotificationItem::CloseFromCloseButton() { 97 void ArcCustomNotificationItem::CloseFromCloseButton() {
105 // Needs to manually remove notification from MessageCenter because 98 // Needs to manually remove notification from MessageCenter because
106 // the floating close button is not part of MessageCenter. 99 // the floating close button is not part of MessageCenter.
107 message_center()->RemoveNotification(notification_id(), true); 100 message_center()->RemoveNotification(notification_id(), true);
108 Close(true); 101 Close(true);
109 } 102 }
110 103
111 void ArcCustomNotificationItem::AddObserver(Observer* observer) { 104 void ArcCustomNotificationItem::AddObserver(Observer* observer) {
112 observers_.AddObserver(observer); 105 observers_.AddObserver(observer);
113 } 106 }
114 107
115 void ArcCustomNotificationItem::RemoveObserver(Observer* observer) { 108 void ArcCustomNotificationItem::RemoveObserver(Observer* observer) {
116 observers_.RemoveObserver(observer); 109 observers_.RemoveObserver(observer);
117 } 110 }
118 111
119 void ArcCustomNotificationItem::OnNotificationSurfaceAdded( 112 void ArcCustomNotificationItem::IncrementWindowRefCount() {
120 exo::NotificationSurface* surface) { 113 ++window_ref_count_;
121 if (!pending_notification() || 114 if (window_ref_count_ == 1)
122 surface->notification_id() != notification_key()) { 115 manager()->CreateNotificationWindow(notification_key());
123 return;
124 }
125
126 static_cast<ArcNotificationDelegate*>(pending_notification()->delegate())
127 ->set_notification_surface(surface);
128 AddToMessageCenter();
129 } 116 }
130 117
131 void ArcCustomNotificationItem::OnNotificationSurfaceRemoved( 118 void ArcCustomNotificationItem::DecrementWindowRefCount() {
132 exo::NotificationSurface* surface) { 119 DCHECK_GT(window_ref_count_, 0);
133 if (surface->notification_id() != notification_key()) 120 --window_ref_count_;
134 return; 121 if (window_ref_count_ == 0)
135 122 manager()->CloseNotificationWindow(notification_key());
136 FOR_EACH_OBSERVER(Observer, observers_, OnItemNotificationSurfaceRemoved());
137 } 123 }
138 124
139 } // namespace arc 125 } // namespace arc
OLDNEW
« no previous file with comments | « ui/arc/notification/arc_custom_notification_item.h ('k') | ui/arc/notification/arc_custom_notification_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698