OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "components/arc/notification/arc_notification_item.h" | |
6 | |
7 #include "base/strings/string16.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "content/public/child/image_decoder_utils.h" | |
11 #include "ui/gfx/geometry/size.h" | |
12 #include "ui/gfx/image/image.h" | |
13 #include "ui/message_center/notification.h" | |
14 #include "ui/message_center/notification_types.h" | |
15 #include "ui/message_center/notifier_settings.h" | |
16 | |
17 namespace { | |
18 | |
19 // static | |
20 static const char* kNotificationIdPrefix = "ARC_NOTIFICATION_"; | |
21 | |
22 SkBitmap DecodeImage(const std::vector<uint8_t>& data) { | |
23 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
24 DCHECK(!data.empty()); // empty string should be handled in caller. | |
25 | |
26 // We may decode an image in the browser process since it has been generated | |
27 // in ARC and should be safe. | |
28 return content::DecodeImage(&data[0], gfx::Size(), data.size()); | |
29 } | |
30 | |
31 class ArcNotificationDelegate : public message_center::NotificationDelegate { | |
32 public: | |
33 explicit ArcNotificationDelegate( | |
34 base::WeakPtr<arc::ArcNotificationItem> item) | |
35 : item_(item) {} | |
36 | |
37 void Close(bool by_user) override { | |
38 if (item_) | |
39 item_->Close(by_user); | |
40 } | |
41 | |
42 // Indicates all notifications have a click handler. This changes the mouse | |
43 // cursor on hover. | |
44 // TODO(yoshiki): Return the correct value according to the content intent | |
45 // and the flags. | |
46 bool HasClickedListener() override { return true; } | |
47 | |
48 void Click() override { | |
49 if (item_) | |
50 item_->Click(); | |
51 } | |
52 | |
53 private: | |
54 // The destractor is private since this class is ref-counted. | |
55 ~ArcNotificationDelegate() override {} | |
56 | |
57 base::WeakPtr<arc::ArcNotificationItem> item_; | |
hidehiko
2015/12/17 07:47:51
nit: Oops, I overlooked. DISALLOW_COPY_AND_ASSIGN.
yoshiki
2015/12/17 19:15:14
Done.
| |
58 }; | |
59 | |
60 } // anonymous namespace | |
61 | |
62 namespace arc { | |
63 | |
64 ArcNotificationItem::ArcNotificationItem( | |
65 ArcNotificationManager* manager, | |
66 message_center::MessageCenter* message_center, | |
67 const arc::ArcNotificationData& data, | |
68 const AccountId& profile_id) | |
69 : manager_(manager), | |
70 message_center_(message_center), | |
71 profile_id_(profile_id), | |
72 notification_key_(data.key), | |
73 notification_id_(kNotificationIdPrefix + notification_key_), | |
74 weak_ptr_factory_(this) { | |
75 // This must be initialized after ArcBridgeService. | |
hidehiko
2015/12/17 07:47:51
This comment looks stale...?
Or, maybe taking ArcN
yoshiki
2015/12/17 19:15:14
It was slate. Removed.
| |
76 } | |
77 | |
78 void ArcNotificationItem::UpdateWithArcNotificationData( | |
79 const arc::ArcNotificationData& data) { | |
80 DCHECK(notification_key_ == data.key); | |
81 | |
82 // Stores the latest data to the |newer_data_| property and returns, if the | |
83 // previous decode is still in progress. | |
84 if (notification_) { | |
elijahtaylor1
2015/12/16 02:34:37
I'm glad there's a comment here, but you could be
yoshiki
2015/12/17 19:15:13
Done.
| |
85 newer_data_ = data.Clone(); | |
hidehiko
2015/12/17 07:47:51
This will keep only newest data here, and (intenti
yoshiki
2015/12/17 19:15:13
Done.
| |
86 return; | |
87 } | |
88 | |
89 message_center::RichNotificationData rich_data; | |
90 message_center::NotificationType type; | |
91 switch (data.type) { | |
92 case arc::ARC_NOTIFICATION_TYPE_BASIC: | |
93 type = message_center::NOTIFICATION_TYPE_SIMPLE; | |
94 break; | |
95 case arc::ARC_NOTIFICATION_TYPE_IMAGE: | |
96 // TODO(yoshiki): Implement this types. | |
97 type = message_center::NOTIFICATION_TYPE_SIMPLE; | |
98 break; | |
99 case arc::ARC_NOTIFICATION_TYPE_PROGRESS: | |
100 type = message_center::NOTIFICATION_TYPE_PROGRESS; | |
101 rich_data.timestamp = base::Time::UnixEpoch() + | |
102 base::TimeDelta::FromMilliseconds(data.time); | |
103 rich_data.progress = std::max( | |
104 0, std::min(100, static_cast<int>(std::round( | |
105 static_cast<float>(data.progress_current) / | |
106 data.progress_max * 100)))); | |
107 break; | |
108 } | |
109 | |
110 message_center::NotifierId notifier_id( | |
elijahtaylor1
2015/12/16 02:34:37
I don't have any idea what this is/does, can you a
yoshiki
2015/12/17 19:15:13
Done.
| |
111 message_center::NotifierId::SYSTEM_COMPONENT, notification_id_); | |
112 notifier_id.profile_id = profile_id_.GetUserEmail(); | |
113 | |
114 DCHECK(!data.title.is_null()); | |
115 DCHECK(!data.message.is_null()); | |
116 notification_.reset(new message_center::Notification( | |
117 type, notification_id_, base::UTF8ToUTF16(data.title.get()), | |
118 base::UTF8ToUTF16(data.message.get()), | |
119 gfx::Image(), // Will be overriden by decoded image. | |
120 base::UTF8ToUTF16("arc"), // display source | |
121 GURL(), // origin url | |
elijahtaylor1
2015/12/16 02:34:37
can you state here why empty is ok?
yoshiki
2015/12/17 19:15:14
Done.
| |
122 notifier_id, rich_data, | |
123 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr()))); | |
124 | |
125 DCHECK(!data.icon_data.is_null()); | |
126 if (data.icon_data.size() == 0) { | |
127 OnImageDecoded(SkBitmap()); // Passes an empty bitmap. | |
128 return; | |
129 } | |
130 | |
131 std::string icon_data_str(data.icon_data.storage().begin(), | |
132 data.icon_data.storage().end()); // copy | |
133 // TODO(yoshiki): Remove decoding by passing a bitmap directly from Android. | |
hidehiko
2015/12/17 07:47:51
Thank you for offline chat.
I thought, the simple
yoshiki
2015/12/17 19:15:13
Correct, we'll be able to omit the async task from
| |
134 base::PostTaskAndReplyWithResult( | |
135 content::BrowserThread::GetBlockingPool(), | |
136 FROM_HERE, | |
137 base::Bind(&DecodeImage, data.icon_data.storage()), | |
138 base::Bind(&ArcNotificationItem::OnImageDecoded, | |
139 weak_ptr_factory_.GetWeakPtr())); | |
140 } | |
141 | |
142 ArcNotificationItem::~ArcNotificationItem() {} | |
143 | |
144 void ArcNotificationItem::OnClosedFromAndroid() { | |
145 message_center_->RemoveNotification(notification_id_, true /* by_user */); | |
146 } | |
147 | |
148 void ArcNotificationItem::Close(bool by_user) { | |
149 manager_->SendNotificationRemovedFromChrome(notification_key_); | |
150 } | |
151 | |
152 void ArcNotificationItem::Click() { | |
153 manager_->SendNotificationClickedOnChrome(notification_key_); | |
154 } | |
155 | |
156 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) { | |
157 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); | |
158 notification_->set_icon(image); | |
159 | |
160 DCHECK(notification_); | |
161 message_center_->AddNotification(notification_.Pass()); | |
hidehiko
2015/12/17 07:47:51
Maybe: std::move(notification_) ?
yoshiki
2015/12/17 19:15:13
Done.
| |
162 DCHECK(!notification_); // |notification_| becomes null. | |
hidehiko
2015/12/17 07:47:51
nit: To be honest, this is redundant.
yoshiki
2015/12/17 19:15:13
Done.
| |
163 | |
164 if (newer_data_) { | |
hidehiko
2015/12/17 07:47:51
Could you add brief comment what this is for?
yoshiki
2015/12/17 19:15:13
Done.
| |
165 arc::ArcNotificationDataPtr data(std::move(newer_data_)); | |
166 DCHECK(!newer_data_); // |newer_data_| becomes null. | |
167 UpdateWithArcNotificationData(*data); | |
168 } | |
169 } | |
170 | |
171 } // namespace arc | |
OLD | NEW |