OLD | NEW |
---|---|
(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_notification_item.h" | |
6 | |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
10 #include "base/strings/string16.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "base/task_runner.h" | |
13 #include "base/task_runner_util.h" | |
14 #include "base/threading/worker_pool.h" | |
15 #include "ui/gfx/codec/png_codec.h" | |
16 #include "ui/gfx/geometry/size.h" | |
17 #include "ui/gfx/image/image.h" | |
18 #include "ui/message_center/message_center_style.h" | |
19 #include "ui/message_center/notification.h" | |
20 #include "ui/message_center/notification_types.h" | |
21 #include "ui/message_center/notifier_settings.h" | |
22 | |
23 namespace arc { | |
24 | |
25 namespace { | |
26 | |
27 // static | |
dcheng
2016/01/12 01:25:48
Nit: // static is redundant here and line 30
yoshiki
2016/01/12 04:50:25
Done.
| |
28 static const char kNotifierId[] = "ARC_NOTIFICATION"; | |
29 | |
30 // static | |
31 static const char kNotificationIdPrefix[] = "ARC_NOTIFICATION_"; | |
32 | |
33 SkBitmap DecodeImage(const std::vector<uint8_t>& data) { | |
34 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread()); | |
35 DCHECK(!data.empty()); // empty string should be handled in caller. | |
36 | |
37 // We may decode an image in the browser process since it has been generated | |
38 // in NotificationListerService in Android and should be safe. | |
39 SkBitmap bitmap; | |
40 gfx::PNGCodec::Decode(&data[0], data.size(), &bitmap); | |
41 return bitmap; | |
42 } | |
43 | |
44 class ArcNotificationDelegate : public message_center::NotificationDelegate { | |
45 public: | |
46 explicit ArcNotificationDelegate(base::WeakPtr<ArcNotificationItem> item) | |
47 : item_(item) {} | |
48 | |
49 void Close(bool by_user) override { | |
50 if (item_) | |
51 item_->Close(by_user); | |
52 } | |
53 | |
54 // Indicates all notifications have a click handler. This changes the mouse | |
55 // cursor on hover. | |
56 // TODO(yoshiki): Return the correct value according to the content intent | |
57 // and the flags. | |
58 bool HasClickedListener() override { return true; } | |
59 | |
60 void Click() override { | |
61 if (item_) | |
62 item_->Click(); | |
63 } | |
64 | |
65 private: | |
66 // The destructor is private since this class is ref-counted. | |
67 ~ArcNotificationDelegate() override {} | |
68 | |
69 base::WeakPtr<ArcNotificationItem> item_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(ArcNotificationDelegate); | |
72 }; | |
73 | |
74 } // anonymous namespace | |
75 | |
76 ArcNotificationItem::ArcNotificationItem( | |
77 ArcNotificationManager* manager, | |
78 message_center::MessageCenter* message_center, | |
79 const ArcNotificationData& data, | |
80 const AccountId& profile_id) | |
81 : manager_(manager), | |
82 message_center_(message_center), | |
83 profile_id_(profile_id), | |
84 notification_key_(data.key), | |
85 notification_id_(kNotificationIdPrefix + notification_key_), | |
86 weak_ptr_factory_(this) {} | |
87 | |
88 void ArcNotificationItem::UpdateWithArcNotificationData( | |
89 const ArcNotificationData& data) { | |
90 DCHECK(thread_checker_.CalledOnValidThread()); | |
91 DCHECK(notification_key_ == data.key); | |
92 | |
93 // Check if a decode task is on-going or not. If |notification_| is non-null, | |
94 // a decode task is on-going asynchronously. Otherwise, there is no task. | |
95 // TODO(yoshiki): Refactor and remove this check by omitting image decoding | |
96 // from here. | |
97 if (notification_) { | |
98 // Store the latest data to the |newer_data_| property and returns, if the | |
99 // previous decode is still in progress. | |
100 // If old |newer_data_| has been stored, discard the old one. | |
101 newer_data_ = data.Clone(); | |
102 return; | |
103 } | |
104 | |
105 message_center::RichNotificationData rich_data; | |
106 message_center::NotificationType type; | |
107 | |
108 switch (data.type) { | |
109 case ARC_NOTIFICATION_TYPE_BASIC: | |
110 type = message_center::NOTIFICATION_TYPE_SIMPLE; | |
111 break; | |
112 case ARC_NOTIFICATION_TYPE_IMAGE: | |
113 // TODO(yoshiki): Implement this types. | |
114 type = message_center::NOTIFICATION_TYPE_SIMPLE; | |
115 LOG(ERROR) << "Unsupported notification type: image"; | |
116 break; | |
117 case ARC_NOTIFICATION_TYPE_PROGRESS: | |
118 type = message_center::NOTIFICATION_TYPE_PROGRESS; | |
119 rich_data.timestamp = base::Time::UnixEpoch() + | |
120 base::TimeDelta::FromMilliseconds(data.time); | |
121 rich_data.progress = std::max( | |
122 0, std::min(100, static_cast<int>(std::round( | |
123 static_cast<float>(data.progress_current) / | |
124 data.progress_max * 100)))); | |
125 break; | |
126 } | |
127 DCHECK(0 <= data.type && data.type <= ARC_NOTIFICATION_TYPE_MAX) | |
128 << "Unsupported notification type: " << data.type; | |
129 | |
130 // The identifier of the notifier, which is used to distinguish the notifiers | |
131 // in the message center. | |
132 message_center::NotifierId notifier_id( | |
133 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); | |
134 notifier_id.profile_id = profile_id_.GetUserEmail(); | |
135 | |
136 DCHECK(!data.title.is_null()); | |
137 DCHECK(!data.message.is_null()); | |
138 notification_.reset(new message_center::Notification( | |
139 type, notification_id_, base::UTF8ToUTF16(data.title.get()), | |
140 base::UTF8ToUTF16(data.message.get()), | |
141 gfx::Image(), // icon image: Will be overriden later. | |
142 base::UTF8ToUTF16("arc"), // display source | |
143 GURL(), // empty origin url, for system component | |
144 notifier_id, rich_data, | |
145 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr()))); | |
146 | |
147 DCHECK(!data.icon_data.is_null()); | |
148 if (data.icon_data.size() == 0) { | |
149 OnImageDecoded(SkBitmap()); // Pass an empty bitmap. | |
150 return; | |
151 } | |
152 | |
153 // TODO(yoshiki): Remove decoding by passing a bitmap directly from Android. | |
154 base::PostTaskAndReplyWithResult( | |
155 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, | |
156 base::Bind(&DecodeImage, data.icon_data.storage()), | |
157 base::Bind(&ArcNotificationItem::OnImageDecoded, | |
158 weak_ptr_factory_.GetWeakPtr())); | |
159 } | |
160 | |
161 ArcNotificationItem::~ArcNotificationItem() {} | |
162 | |
163 void ArcNotificationItem::OnClosedFromAndroid() { | |
164 being_removed_by_manager_ = true; // Closing is initiated by the manager. | |
165 message_center_->RemoveNotification(notification_id_, true /* by_user */); | |
166 } | |
167 | |
168 void ArcNotificationItem::Close(bool by_user) { | |
169 if (being_removed_by_manager_) { | |
170 // Closing is caused by the manager, so we don't need to nofify a close | |
171 // event to the manager. | |
172 return; | |
173 } | |
174 | |
175 // Do not touch its any members afterwards, because this instance will be | |
176 // destroyed in the following call | |
177 manager_->SendNotificationRemovedFromChrome(notification_key_); | |
178 } | |
179 | |
180 void ArcNotificationItem::Click() { | |
181 manager_->SendNotificationClickedOnChrome(notification_key_); | |
182 } | |
183 | |
184 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) { | |
185 DCHECK(thread_checker_.CalledOnValidThread()); | |
186 | |
187 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); | |
188 notification_->set_icon(image); | |
189 | |
190 DCHECK(notification_); | |
191 message_center_->AddNotification(std::move(notification_)); | |
192 | |
193 if (newer_data_) { | |
194 // There is the newer data, so updates again. | |
195 ArcNotificationDataPtr data(std::move(newer_data_)); | |
196 UpdateWithArcNotificationData(*data); | |
197 } | |
198 } | |
199 | |
200 } // namespace arc | |
OLD | NEW |