Chromium Code Reviews| Index: ui/arc/notification/arc_notification_item_impl.cc |
| diff --git a/ui/arc/notification/arc_notification_item_impl.cc b/ui/arc/notification/arc_notification_item_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5f991f6e393d78381087457f58d37d6ea456b8a8 |
| --- /dev/null |
| +++ b/ui/arc/notification/arc_notification_item_impl.cc |
| @@ -0,0 +1,222 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/arc/notification/arc_notification_item_impl.h" |
| + |
| +#include <algorithm> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/task_runner.h" |
| +#include "base/task_scheduler/post_task.h" |
| +#include "third_party/skia/include/core/SkCanvas.h" |
| +#include "third_party/skia/include/core/SkPaint.h" |
| +#include "ui/arc/notification/arc_custom_notification_view.h" |
| +#include "ui/arc/notification/arc_notification_delegate.h" |
| +#include "ui/gfx/codec/png_codec.h" |
| +#include "ui/gfx/geometry/size.h" |
| +#include "ui/gfx/image/image.h" |
| +#include "ui/gfx/text_elider.h" |
| +#include "ui/message_center/message_center_style.h" |
| +#include "ui/message_center/notification.h" |
| +#include "ui/message_center/notification_types.h" |
| +#include "ui/message_center/notifier_settings.h" |
| + |
| +namespace arc { |
| + |
| +namespace { |
| + |
| +constexpr char kNotifierId[] = "ARC_NOTIFICATION"; |
| +constexpr char kNotificationIdPrefix[] = "ARC_NOTIFICATION_"; |
| + |
| +} // anonymous namespace |
| + |
| +ArcNotificationItemImpl::ArcNotificationItemImpl( |
| + ArcNotificationManager* manager, |
| + message_center::MessageCenter* message_center, |
| + const std::string& notification_key, |
| + const AccountId& profile_id) |
| + : manager_(manager), |
| + message_center_(message_center), |
| + profile_id_(profile_id), |
| + notification_key_(notification_key), |
| + notification_id_(kNotificationIdPrefix + notification_key_), |
| + weak_ptr_factory_(this) {} |
| + |
| +ArcNotificationItemImpl::~ArcNotificationItemImpl() { |
| + for (auto& observer : observers_) |
| + observer.OnItemDestroying(); |
| +} |
| + |
| +void ArcNotificationItemImpl::UpdateWithArcNotificationData( |
| + mojom::ArcNotificationDataPtr data) { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK_EQ(notification_key(), data->key); |
| + |
| + if (HasPendingNotification()) { |
| + CacheArcNotificationData(std::move(data)); |
| + return; |
| + } |
| + |
|
hidehiko
2017/03/02 15:38:16
Here, some code is dropped. Any background?
|
| + message_center::RichNotificationData rich_data; |
| + rich_data.pinned = (data->no_clear || data->ongoing_event); |
| + rich_data.priority = ConvertAndroidPriority(data->priority); |
| + if (data->small_icon) |
| + rich_data.small_image = gfx::Image::CreateFrom1xBitmap(*data->small_icon); |
| + if (data->accessible_name.has_value()) |
| + rich_data.accessible_name = base::UTF8ToUTF16(*data->accessible_name); |
| + |
| + message_center::NotifierId notifier_id( |
| + message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); |
| + notifier_id.profile_id = profile_id().GetUserEmail(); |
| + |
| + SetNotification(base::MakeUnique<message_center::Notification>( |
| + message_center::NOTIFICATION_TYPE_CUSTOM, notification_id(), |
| + base::UTF8ToUTF16(data->title), base::UTF8ToUTF16(data->message), |
| + gfx::Image(), |
| + base::UTF8ToUTF16("arc"), // display source |
| + GURL(), // empty origin url, for system component |
| + notifier_id, rich_data, |
| + new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr()))); |
| + |
| + pinned_ = rich_data.pinned; |
| + |
| + if (!data->snapshot_image || data->snapshot_image->isNull()) { |
| + snapshot_ = gfx::ImageSkia(); |
| + } else { |
| + snapshot_ = gfx::ImageSkia( |
| + gfx::ImageSkiaRep(*data->snapshot_image, data->snapshot_image_scale)); |
| + } |
| + |
| + for (auto& observer : observers_) |
| + observer.OnItemUpdated(); |
| + |
| + AddToMessageCenter(); |
| +} |
| + |
| +void ArcNotificationItemImpl::OnClosedFromAndroid() { |
| + being_removed_by_manager_ = true; // Closing is initiated by the manager. |
| + message_center_->RemoveNotification(notification_id_, false /* by_user */); |
| +} |
| + |
| +void ArcNotificationItemImpl::Close(bool by_user) { |
| + if (being_removed_by_manager_) { |
| + // Closing is caused by the manager, so we don't need to nofify a close |
| + // event to the manager. |
| + return; |
| + } |
| + |
| + // Do not touch its any members afterwards, because this instance will be |
| + // destroyed in the following call |
| + manager_->SendNotificationRemovedFromChrome(notification_key_); |
| +} |
| + |
| +void ArcNotificationItemImpl::OpenSettings() { |
| + manager_->OpenNotificationSettings(notification_key_); |
| +} |
| + |
| +// Converts from Android notification priority to Chrome notification priority. |
| +// On Android, PRIORITY_DEFAULT does not pop up, so this maps PRIORITY_DEFAULT |
| +// to Chrome's -1 to adapt that behavior. Also, this maps PRIORITY_LOW and _HIGH |
| +// to -2 and 0 respectively to adjust the value with keeping the order among |
| +// _LOW, _DEFAULT and _HIGH. |
| +// static |
| +int ArcNotificationItemImpl::ConvertAndroidPriority(int android_priority) { |
| + switch (android_priority) { |
| + case -2: // PRIORITY_MIN |
| + case -1: // PRIORITY_LOW |
| + return -2; |
| + case 0: // PRIORITY_DEFAULT |
| + return -1; |
| + case 1: // PRIORITY_HIGH |
| + return 0; |
| + case 2: // PRIORITY_MAX |
| + return 2; |
| + default: |
| + NOTREACHED() << "Invalid Priority: " << android_priority; |
| + return 0; |
| + } |
| +} |
| + |
| +bool ArcNotificationItemImpl::HasPendingNotification() { |
| + return (notification_ != nullptr); |
| +} |
| + |
| +void ArcNotificationItemImpl::CacheArcNotificationData( |
| + mojom::ArcNotificationDataPtr data) { |
| + // If old |newer_data_| has been stored, discard the old one. |
| + newer_data_ = std::move(data); |
| +} |
| + |
| +void ArcNotificationItemImpl::SetNotification( |
| + std::unique_ptr<message_center::Notification> notification) { |
| + notification_ = std::move(notification); |
| +} |
| + |
| +void ArcNotificationItemImpl::AddToMessageCenter() { |
| + DCHECK(notification_); |
| + message_center_->AddNotification(std::move(notification_)); |
| + |
| + if (newer_data_) { |
| + // There is the newer data, so updates again. |
| + UpdateWithArcNotificationData(std::move(newer_data_)); |
| + } |
| +} |
| + |
| +bool ArcNotificationItemImpl::CalledOnValidThread() const { |
| + return thread_checker_.CalledOnValidThread(); |
| +} |
| + |
| +void ArcNotificationItemImpl::OnImageDecoded(const SkBitmap& bitmap) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); |
| + notification_->set_icon(image); |
| + AddToMessageCenter(); |
| +} |
| + |
| +void ArcNotificationItemImpl::CloseFromCloseButton() { |
| + // Needs to manually remove notification from MessageCenter because |
| + // the floating close button is not part of MessageCenter. |
| + message_center()->RemoveNotification(notification_id(), true); |
| + Close(true); |
| +} |
| + |
| +void ArcNotificationItemImpl::AddObserver(Observer* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void ArcNotificationItemImpl::RemoveObserver(Observer* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +void ArcNotificationItemImpl::IncrementWindowRefCount() { |
| + ++window_ref_count_; |
| + if (window_ref_count_ == 1) |
| + manager()->CreateNotificationWindow(notification_key()); |
| +} |
| + |
| +void ArcNotificationItemImpl::DecrementWindowRefCount() { |
| + DCHECK_GT(window_ref_count_, 0); |
| + --window_ref_count_; |
| + if (window_ref_count_ == 0) |
| + manager()->CloseNotificationWindow(notification_key()); |
| +} |
| + |
| +bool ArcNotificationItemImpl::pinned() const { |
| + return pinned_; |
| +} |
| + |
| +const gfx::ImageSkia& ArcNotificationItemImpl::snapshot() const { |
| + return snapshot_; |
| +} |
| + |
| +const std::string& ArcNotificationItemImpl::notification_key() const { |
| + return notification_key_; |
| +} |
| + |
| +} // namespace arc |