Chromium Code Reviews| Index: ui/arc/notification/arc_notification_manager.cc |
| diff --git a/ui/arc/notification/arc_notification_manager.cc b/ui/arc/notification/arc_notification_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6334cc407ba1caf3390eddd0ca455b321ec74cd6 |
| --- /dev/null |
| +++ b/ui/arc/notification/arc_notification_manager.cc |
| @@ -0,0 +1,91 @@ |
| +// Copyright 2016 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_manager.h" |
| + |
| +#include "base/stl_util.h" |
| +#include "ui/arc/notification/arc_notification_item.h" |
| + |
| +namespace arc { |
| + |
| +ArcNotificationManager::ArcNotificationManager(ArcBridgeService* arc_bridge, |
| + const AccountId& main_profile_id) |
| + : arc_bridge_(arc_bridge), |
| + main_profile_id_(main_profile_id), |
| + binding_(this) { |
| + // This must be initialized after ArcBridgeService. |
| + DCHECK(arc_bridge_); |
| + DCHECK_EQ(arc_bridge_, ArcBridgeService::Get()); |
| + arc_bridge_->AddObserver(this); |
| +} |
| + |
| +ArcNotificationManager::~ArcNotificationManager() { |
| + // This should be free'd before ArcBridgeService. |
| + DCHECK(ArcBridgeService::Get()); |
| + DCHECK_EQ(arc_bridge_, ArcBridgeService::Get()); |
| + arc_bridge_->RemoveObserver(this); |
| +} |
| + |
| +void ArcNotificationManager::OnNotificationsInstanceReady() { |
| + NotificationsInstance* notifications_instance = |
| + arc_bridge_->notifications_instance(); |
| + if (!notifications_instance) { |
| + VLOG(2) << "Request to refresh app list when bridge service is not ready."; |
| + return; |
| + } |
| + |
| + NotificationsHostPtr host; |
| + binding_.Bind(mojo::GetProxy(&host)); |
| + notifications_instance->Init(std::move(host)); |
| +} |
| + |
| +void ArcNotificationManager::OnNotificationPosted(ArcNotificationDataPtr data) { |
| + ArcNotificationItem* item = items_.get(data->key); |
| + if (!item) { |
| + // Show a notification on the primary loged-in user's desktop. |
| + // TODO(yoshiki): Reconsider when ARC supports multi-user. |
| + item = new ArcNotificationItem(this, message_center::MessageCenter::Get(), |
| + *data, main_profile_id_); |
| + items_.set(data->key, make_scoped_ptr(item)); |
| + } |
| + item->UpdateWithArcNotificationData(*data); |
| +} |
| + |
| +void ArcNotificationManager::OnNotificationRemoved(const mojo::String& key) { |
| + Items::iterator it = items_.find(key.get()); |
| + if (it != items_.end()) { |
| + scoped_ptr<ArcNotificationItem> item(items_.take_and_erase(it)); |
| + item->OnClosedFromAndroid(); |
| + } else { |
| + VLOG(3) << "Android requests to remove a notification (key: " << key |
| + << "), but it is already gone."; |
| + } |
|
stevenjb
2016/01/05 16:59:06
nit: invert logic and early exit if not found.
yoshiki
2016/01/06 08:50:17
Done.
|
| +} |
| + |
| +void ArcNotificationManager::SendNotificationRemovedFromChrome( |
| + const std::string& key) { |
| + Items::iterator it = items_.find(key); |
| + if (it != items_.end()) { |
| + scoped_ptr<ArcNotificationItem> item(items_.take_and_erase(it)); |
| + |
| + arc_bridge_->notifications_instance()->SendNotificationEventToAndroid( |
| + key, ARC_NOTIFICATION_EVENT_CLOSED); |
| + } else { |
| + VLOG(3) << "Chrome requests to remove a notification (key: " << key |
| + << "), but it is already gone."; |
| + } |
|
stevenjb
2016/01/05 16:59:06
same here and below.
yoshiki
2016/01/06 08:50:17
Done.
|
| +} |
| + |
| +void ArcNotificationManager::SendNotificationClickedOnChrome( |
| + const std::string& key) { |
| + if (items_.contains(key)) { |
| + arc_bridge_->notifications_instance()->SendNotificationEventToAndroid( |
| + key, ARC_NOTIFICATION_EVENT_BODY_CLICKED); |
| + } else { |
| + VLOG(3) << "Chrome requests to fire a click event on notification (key: " |
| + << key << "), but it is gone."; |
| + } |
| +} |
| + |
| +} // namespace arc |