Chromium Code Reviews| 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 | |
| 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 std::string icon_data_str(data.icon_data.storage().begin(), | |
| 154 data.icon_data.storage().end()); // copy | |
|
sky
2016/01/08 17:17:30
AFAICT you aren't using icon_data_str.
yoshiki
2016/01/11 21:25:27
Removed.
| |
| 155 // TODO(yoshiki): Remove decoding by passing a bitmap directly from Android. | |
| 156 base::PostTaskAndReplyWithResult( | |
| 157 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, | |
| 158 base::Bind(&DecodeImage, data.icon_data.storage()), | |
|
sky
2016/01/08 17:17:29
I believe this makes a copy. Is it worth making th
yoshiki
2016/01/11 21:25:27
Images must be small (80x80px), so let me keep it
| |
| 159 base::Bind(&ArcNotificationItem::OnImageDecoded, | |
| 160 weak_ptr_factory_.GetWeakPtr())); | |
| 161 } | |
| 162 | |
| 163 ArcNotificationItem::~ArcNotificationItem() {} | |
| 164 | |
| 165 void ArcNotificationItem::OnClosedFromAndroid() { | |
| 166 being_removed_by_manager_ = true; // Closing is initiated by the manager. | |
| 167 message_center_->RemoveNotification(notification_id_, true /* by_user */); | |
| 168 } | |
| 169 | |
| 170 void ArcNotificationItem::Close(bool by_user) { | |
| 171 if (being_removed_by_manager_) { | |
| 172 // Closing is caused by the manager, so we don't need to nofify a close | |
| 173 // event to the manager. | |
| 174 return; | |
| 175 } | |
| 176 | |
| 177 // Do not touch its any members afterwards, because this instance will be | |
| 178 // destroyed in the following call | |
| 179 manager_->SendNotificationRemovedFromChrome(notification_key_); | |
| 180 } | |
| 181 | |
| 182 void ArcNotificationItem::Click() { | |
| 183 manager_->SendNotificationClickedOnChrome(notification_key_); | |
| 184 } | |
| 185 | |
| 186 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) { | |
| 187 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 188 | |
| 189 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); | |
| 190 notification_->set_icon(image); | |
| 191 | |
| 192 DCHECK(notification_); | |
| 193 message_center_->AddNotification(std::move(notification_)); | |
| 194 | |
| 195 if (newer_data_) { | |
| 196 // There is the newer data, so re-updates again. | |
|
sky
2016/01/08 17:17:29
re-updates -> update
yoshiki
2016/01/11 21:25:27
Done.
| |
| 197 ArcNotificationDataPtr data(std::move(newer_data_)); | |
| 198 UpdateWithArcNotificationData(*data); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 } // namespace arc | |
| OLD | NEW |