OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_impl.h" |
| 6 |
| 7 #include <utility> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "ui/arc/notification/arc_custom_notification_view.h" |
| 13 #include "ui/arc/notification/arc_notification_delegate.h" |
| 14 #include "ui/gfx/geometry/size.h" |
| 15 #include "ui/gfx/image/image.h" |
| 16 #include "ui/message_center/message_center_style.h" |
| 17 #include "ui/message_center/notification.h" |
| 18 #include "ui/message_center/notification_types.h" |
| 19 #include "ui/message_center/notifier_settings.h" |
| 20 |
| 21 namespace arc { |
| 22 |
| 23 namespace { |
| 24 |
| 25 constexpr char kNotifierId[] = "ARC_NOTIFICATION"; |
| 26 constexpr char kNotificationIdPrefix[] = "ARC_NOTIFICATION_"; |
| 27 |
| 28 // Converts from Android notification priority to Chrome notification priority. |
| 29 // On Android, PRIORITY_DEFAULT does not pop up, so this maps PRIORITY_DEFAULT |
| 30 // to Chrome's -1 to adapt that behavior. Also, this maps PRIORITY_LOW and |
| 31 // _HIGH to -2 and 0 respectively to adjust the value with keeping the order |
| 32 // among _LOW, _DEFAULT and _HIGH. static |
| 33 // TODO(yoshiki): rewrite this conversion as typemap |
| 34 int ConvertAndroidPriority(mojom::ArcNotificationPriority android_priority) { |
| 35 switch (android_priority) { |
| 36 case mojom::ArcNotificationPriority::MIN: |
| 37 case mojom::ArcNotificationPriority::LOW: |
| 38 return message_center::MIN_PRIORITY; |
| 39 case mojom::ArcNotificationPriority::DEFAULT: |
| 40 return message_center::LOW_PRIORITY; |
| 41 case mojom::ArcNotificationPriority::HIGH: |
| 42 return message_center::DEFAULT_PRIORITY; |
| 43 case mojom::ArcNotificationPriority::MAX: |
| 44 return message_center::MAX_PRIORITY; |
| 45 } |
| 46 |
| 47 NOTREACHED() << "Invalid Priority: " << android_priority; |
| 48 return message_center::DEFAULT_PRIORITY; |
| 49 } |
| 50 |
| 51 } // anonymous namespace |
| 52 |
| 53 ArcNotificationItemImpl::ArcNotificationItemImpl( |
| 54 ArcNotificationManager* manager, |
| 55 message_center::MessageCenter* message_center, |
| 56 const std::string& notification_key, |
| 57 const AccountId& profile_id) |
| 58 : manager_(manager), |
| 59 message_center_(message_center), |
| 60 profile_id_(profile_id), |
| 61 notification_key_(notification_key), |
| 62 notification_id_(kNotificationIdPrefix + notification_key_), |
| 63 weak_ptr_factory_(this) {} |
| 64 |
| 65 ArcNotificationItemImpl::~ArcNotificationItemImpl() { |
| 66 for (auto& observer : observers_) |
| 67 observer.OnItemDestroying(); |
| 68 } |
| 69 |
| 70 void ArcNotificationItemImpl::OnUpdatedFromAndroid( |
| 71 mojom::ArcNotificationDataPtr data) { |
| 72 DCHECK(CalledOnValidThread()); |
| 73 DCHECK_EQ(notification_key_, data->key); |
| 74 |
| 75 message_center::RichNotificationData rich_data; |
| 76 rich_data.pinned = (data->no_clear || data->ongoing_event); |
| 77 rich_data.priority = ConvertAndroidPriority(data->priority); |
| 78 if (data->small_icon) |
| 79 rich_data.small_image = gfx::Image::CreateFrom1xBitmap(*data->small_icon); |
| 80 if (data->accessible_name.has_value()) |
| 81 rich_data.accessible_name = base::UTF8ToUTF16(*data->accessible_name); |
| 82 |
| 83 message_center::NotifierId notifier_id( |
| 84 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); |
| 85 notifier_id.profile_id = profile_id_.GetUserEmail(); |
| 86 |
| 87 auto notification = base::MakeUnique<message_center::Notification>( |
| 88 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id_, |
| 89 base::UTF8ToUTF16(data->title), base::UTF8ToUTF16(data->message), |
| 90 gfx::Image(), |
| 91 base::UTF8ToUTF16("arc"), // display source |
| 92 GURL(), // empty origin url, for system component |
| 93 notifier_id, rich_data, |
| 94 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr())); |
| 95 notification->set_timestamp(base::Time::FromJavaTime(data->time)); |
| 96 |
| 97 pinned_ = rich_data.pinned; |
| 98 expand_state_ = data->expand_state; |
| 99 shown_contents_ = data->shown_contents; |
| 100 |
| 101 if (!data->snapshot_image || data->snapshot_image->isNull()) { |
| 102 snapshot_ = gfx::ImageSkia(); |
| 103 } else { |
| 104 snapshot_ = gfx::ImageSkia( |
| 105 gfx::ImageSkiaRep(*data->snapshot_image, data->snapshot_image_scale)); |
| 106 } |
| 107 |
| 108 for (auto& observer : observers_) |
| 109 observer.OnItemUpdated(); |
| 110 |
| 111 message_center_->AddNotification(std::move(notification)); |
| 112 } |
| 113 |
| 114 void ArcNotificationItemImpl::OnClosedFromAndroid() { |
| 115 being_removed_by_manager_ = true; // Closing is initiated by the manager. |
| 116 message_center_->RemoveNotification(notification_id_, false /* by_user */); |
| 117 } |
| 118 |
| 119 void ArcNotificationItemImpl::Close(bool by_user) { |
| 120 if (being_removed_by_manager_) { |
| 121 // Closing is caused by the manager, so we don't need to nofify a close |
| 122 // event to the manager. |
| 123 return; |
| 124 } |
| 125 |
| 126 // Do not touch its any members afterwards, because this instance will be |
| 127 // destroyed in the following call |
| 128 manager_->SendNotificationRemovedFromChrome(notification_key_); |
| 129 } |
| 130 |
| 131 void ArcNotificationItemImpl::Click() { |
| 132 manager_->SendNotificationClickedOnChrome(notification_key_); |
| 133 } |
| 134 |
| 135 void ArcNotificationItemImpl::OpenSettings() { |
| 136 manager_->OpenNotificationSettings(notification_key_); |
| 137 } |
| 138 |
| 139 bool ArcNotificationItemImpl::IsOpeningSettingsSupported() const { |
| 140 return manager_->IsOpeningSettingsSupported(); |
| 141 } |
| 142 |
| 143 void ArcNotificationItemImpl::ToggleExpansion() { |
| 144 manager_->SendNotificationToggleExpansionOnChrome(notification_key_); |
| 145 } |
| 146 |
| 147 bool ArcNotificationItemImpl::CalledOnValidThread() const { |
| 148 return thread_checker_.CalledOnValidThread(); |
| 149 } |
| 150 |
| 151 void ArcNotificationItemImpl::AddObserver(Observer* observer) { |
| 152 observers_.AddObserver(observer); |
| 153 } |
| 154 |
| 155 void ArcNotificationItemImpl::RemoveObserver(Observer* observer) { |
| 156 observers_.RemoveObserver(observer); |
| 157 } |
| 158 |
| 159 void ArcNotificationItemImpl::IncrementWindowRefCount() { |
| 160 ++window_ref_count_; |
| 161 if (window_ref_count_ == 1) |
| 162 manager_->CreateNotificationWindow(notification_key_); |
| 163 } |
| 164 |
| 165 void ArcNotificationItemImpl::DecrementWindowRefCount() { |
| 166 DCHECK_GT(window_ref_count_, 0); |
| 167 --window_ref_count_; |
| 168 if (window_ref_count_ == 0) |
| 169 manager_->CloseNotificationWindow(notification_key_); |
| 170 } |
| 171 |
| 172 bool ArcNotificationItemImpl::GetPinned() const { |
| 173 return pinned_; |
| 174 } |
| 175 |
| 176 const gfx::ImageSkia& ArcNotificationItemImpl::GetSnapshot() const { |
| 177 return snapshot_; |
| 178 } |
| 179 |
| 180 mojom::ArcNotificationExpandState ArcNotificationItemImpl::GetExpandState() |
| 181 const { |
| 182 return expand_state_; |
| 183 } |
| 184 |
| 185 mojom::ArcNotificationShownContents ArcNotificationItemImpl::GetShownContents() |
| 186 const { |
| 187 return shown_contents_; |
| 188 } |
| 189 |
| 190 const std::string& ArcNotificationItemImpl::GetNotificationKey() const { |
| 191 return notification_key_; |
| 192 } |
| 193 |
| 194 } // namespace arc |
OLD | NEW |