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