| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/arc/notification/arc_notification_manager.h" | 5 #include "ui/arc/notification/arc_notification_manager.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/system/toast/toast_manager.h" | 8 #include "ash/system/toast/toast_manager.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "ui/arc/notification/arc_custom_notification_item.h" | 11 #include "ui/arc/notification/arc_custom_notification_item.h" |
| 12 #include "ui/arc/notification/arc_notification_item.h" | 12 #include "ui/arc/notification/arc_notification_item.h" |
| 13 | 13 |
| 14 namespace arc { | 14 namespace arc { |
| 15 | 15 |
| 16 ArcNotificationManager::ArcNotificationManager(ArcBridgeService* bridge_service, | 16 ArcNotificationManager::ArcNotificationManager(ArcBridgeService* bridge_service, |
| 17 const AccountId& main_profile_id) | 17 const AccountId& main_profile_id) |
| 18 : ArcNotificationManager(bridge_service, | 18 : ArcNotificationManager(bridge_service, |
| 19 main_profile_id, | 19 main_profile_id, |
| 20 message_center::MessageCenter::Get()) {} | 20 message_center::MessageCenter::Get()) {} |
| 21 | 21 |
| 22 ArcNotificationManager::ArcNotificationManager( | 22 ArcNotificationManager::ArcNotificationManager( |
| 23 ArcBridgeService* bridge_service, | 23 ArcBridgeService* bridge_service, |
| 24 const AccountId& main_profile_id, | 24 const AccountId& main_profile_id, |
| 25 message_center::MessageCenter* message_center) | 25 message_center::MessageCenter* message_center) |
| 26 : ArcService(bridge_service), | 26 : ArcService(bridge_service), |
| 27 main_profile_id_(main_profile_id), | 27 main_profile_id_(main_profile_id), |
| 28 message_center_(message_center), | 28 message_center_(message_center), |
| 29 binding_(this) { | 29 binding_(this) { |
| 30 arc_bridge_service()->AddObserver(this); | 30 arc_bridge_service()->notifications()->AddObserver(this); |
| 31 } | 31 } |
| 32 | 32 |
| 33 ArcNotificationManager::~ArcNotificationManager() { | 33 ArcNotificationManager::~ArcNotificationManager() { |
| 34 arc_bridge_service()->RemoveObserver(this); | 34 arc_bridge_service()->notifications()->RemoveObserver(this); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void ArcNotificationManager::OnNotificationsInstanceReady() { | 37 void ArcNotificationManager::OnInstanceReady() { |
| 38 DCHECK(!ready_); | 38 DCHECK(!ready_); |
| 39 | 39 |
| 40 auto notifications_instance = arc_bridge_service()->notifications_instance(); | 40 auto notifications_instance = |
| 41 arc_bridge_service()->notifications()->instance(); |
| 41 if (!notifications_instance) { | 42 if (!notifications_instance) { |
| 42 VLOG(2) << "Request to refresh app list when bridge service is not ready."; | 43 VLOG(2) << "Request to refresh app list when bridge service is not ready."; |
| 43 return; | 44 return; |
| 44 } | 45 } |
| 45 | 46 |
| 46 notifications_instance->Init(binding_.CreateInterfacePtrAndBind()); | 47 notifications_instance->Init(binding_.CreateInterfacePtrAndBind()); |
| 47 ready_ = true; | 48 ready_ = true; |
| 48 } | 49 } |
| 49 | 50 |
| 50 void ArcNotificationManager::OnNotificationsInstanceClosed() { | 51 void ArcNotificationManager::OnInstanceClosed() { |
| 51 DCHECK(ready_); | 52 DCHECK(ready_); |
| 52 while (!items_.empty()) { | 53 while (!items_.empty()) { |
| 53 auto it = items_.begin(); | 54 auto it = items_.begin(); |
| 54 std::unique_ptr<ArcNotificationItem> item = std::move(it->second); | 55 std::unique_ptr<ArcNotificationItem> item = std::move(it->second); |
| 55 items_.erase(it); | 56 items_.erase(it); |
| 56 item->OnClosedFromAndroid(false /* by_user */); | 57 item->OnClosedFromAndroid(false /* by_user */); |
| 57 } | 58 } |
| 58 ready_ = false; | 59 ready_ = false; |
| 59 } | 60 } |
| 60 | 61 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 VLOG(3) << "Chrome requests to remove a notification (key: " << key | 103 VLOG(3) << "Chrome requests to remove a notification (key: " << key |
| 103 << "), but it is already gone."; | 104 << "), but it is already gone."; |
| 104 return; | 105 return; |
| 105 } | 106 } |
| 106 | 107 |
| 107 // The removed ArcNotificationItem needs to live in this scope, since the | 108 // The removed ArcNotificationItem needs to live in this scope, since the |
| 108 // given argument |key| may be a part of the removed item. | 109 // given argument |key| may be a part of the removed item. |
| 109 std::unique_ptr<ArcNotificationItem> item = std::move(it->second); | 110 std::unique_ptr<ArcNotificationItem> item = std::move(it->second); |
| 110 items_.erase(it); | 111 items_.erase(it); |
| 111 | 112 |
| 112 auto notifications_instance = arc_bridge_service()->notifications_instance(); | 113 auto notifications_instance = |
| 114 arc_bridge_service()->notifications()->instance(); |
| 113 | 115 |
| 114 // On shutdown, the ARC channel may quit earlier then notifications. | 116 // On shutdown, the ARC channel may quit earlier then notifications. |
| 115 if (!notifications_instance) { | 117 if (!notifications_instance) { |
| 116 VLOG(2) << "ARC Notification (key: " << key | 118 VLOG(2) << "ARC Notification (key: " << key |
| 117 << ") is closed, but the ARC channel has already gone."; | 119 << ") is closed, but the ARC channel has already gone."; |
| 118 return; | 120 return; |
| 119 } | 121 } |
| 120 | 122 |
| 121 notifications_instance->SendNotificationEventToAndroid( | 123 notifications_instance->SendNotificationEventToAndroid( |
| 122 key, mojom::ArcNotificationEvent::CLOSED); | 124 key, mojom::ArcNotificationEvent::CLOSED); |
| 123 } | 125 } |
| 124 | 126 |
| 125 void ArcNotificationManager::SendNotificationClickedOnChrome( | 127 void ArcNotificationManager::SendNotificationClickedOnChrome( |
| 126 const std::string& key) { | 128 const std::string& key) { |
| 127 if (items_.find(key) == items_.end()) { | 129 if (items_.find(key) == items_.end()) { |
| 128 VLOG(3) << "Chrome requests to fire a click event on notification (key: " | 130 VLOG(3) << "Chrome requests to fire a click event on notification (key: " |
| 129 << key << "), but it is gone."; | 131 << key << "), but it is gone."; |
| 130 return; | 132 return; |
| 131 } | 133 } |
| 132 | 134 |
| 133 auto notifications_instance = arc_bridge_service()->notifications_instance(); | 135 auto notifications_instance = |
| 136 arc_bridge_service()->notifications()->instance(); |
| 134 | 137 |
| 135 // On shutdown, the ARC channel may quit earlier then notifications. | 138 // On shutdown, the ARC channel may quit earlier then notifications. |
| 136 if (!notifications_instance) { | 139 if (!notifications_instance) { |
| 137 VLOG(2) << "ARC Notification (key: " << key | 140 VLOG(2) << "ARC Notification (key: " << key |
| 138 << ") is clicked, but the ARC channel has already gone."; | 141 << ") is clicked, but the ARC channel has already gone."; |
| 139 return; | 142 return; |
| 140 } | 143 } |
| 141 | 144 |
| 142 notifications_instance->SendNotificationEventToAndroid( | 145 notifications_instance->SendNotificationEventToAndroid( |
| 143 key, mojom::ArcNotificationEvent::BODY_CLICKED); | 146 key, mojom::ArcNotificationEvent::BODY_CLICKED); |
| 144 } | 147 } |
| 145 | 148 |
| 146 void ArcNotificationManager::SendNotificationButtonClickedOnChrome( | 149 void ArcNotificationManager::SendNotificationButtonClickedOnChrome( |
| 147 const std::string& key, int button_index) { | 150 const std::string& key, |
| 151 int button_index) { |
| 148 if (items_.find(key) == items_.end()) { | 152 if (items_.find(key) == items_.end()) { |
| 149 VLOG(3) << "Chrome requests to fire a click event on notification (key: " | 153 VLOG(3) << "Chrome requests to fire a click event on notification (key: " |
| 150 << key << "), but it is gone."; | 154 << key << "), but it is gone."; |
| 151 return; | 155 return; |
| 152 } | 156 } |
| 153 | 157 |
| 154 auto notifications_instance = arc_bridge_service()->notifications_instance(); | 158 auto notifications_instance = |
| 159 arc_bridge_service()->notifications()->instance(); |
| 155 | 160 |
| 156 // On shutdown, the ARC channel may quit earlier then notifications. | 161 // On shutdown, the ARC channel may quit earlier then notifications. |
| 157 if (!notifications_instance) { | 162 if (!notifications_instance) { |
| 158 VLOG(2) << "ARC Notification (key: " << key | 163 VLOG(2) << "ARC Notification (key: " << key |
| 159 << ")'s button is clicked, but the ARC channel has already gone."; | 164 << ")'s button is clicked, but the ARC channel has already gone."; |
| 160 return; | 165 return; |
| 161 } | 166 } |
| 162 | 167 |
| 163 arc::mojom::ArcNotificationEvent command; | 168 arc::mojom::ArcNotificationEvent command; |
| 164 switch (button_index) { | 169 switch (button_index) { |
| 165 case 0: | 170 case 0: |
| 166 command = mojom::ArcNotificationEvent::BUTTON_1_CLICKED; | 171 command = mojom::ArcNotificationEvent::BUTTON_1_CLICKED; |
| 167 break; | 172 break; |
| 168 case 1: | 173 case 1: |
| 169 command = mojom::ArcNotificationEvent::BUTTON_2_CLICKED; | 174 command = mojom::ArcNotificationEvent::BUTTON_2_CLICKED; |
| 170 break; | 175 break; |
| 171 case 2: | 176 case 2: |
| 172 command = mojom::ArcNotificationEvent::BUTTON_3_CLICKED; | 177 command = mojom::ArcNotificationEvent::BUTTON_3_CLICKED; |
| 173 break; | 178 break; |
| 174 case 3: | 179 case 3: |
| 175 command = mojom::ArcNotificationEvent::BUTTON_4_CLICKED; | 180 command = mojom::ArcNotificationEvent::BUTTON_4_CLICKED; |
| 176 break; | 181 break; |
| 177 case 4: | 182 case 4: |
| 178 command = mojom::ArcNotificationEvent::BUTTON_5_CLICKED; | 183 command = mojom::ArcNotificationEvent::BUTTON_5_CLICKED; |
| 179 break; | 184 break; |
| 180 default: | 185 default: |
| 181 VLOG(3) << "Invalid button index (key: " << key << ", index: " << | 186 VLOG(3) << "Invalid button index (key: " << key |
| 182 button_index << ")."; | 187 << ", index: " << button_index << ")."; |
| 183 return; | 188 return; |
| 184 } | 189 } |
| 185 | 190 |
| 186 notifications_instance->SendNotificationEventToAndroid(key, command); | 191 notifications_instance->SendNotificationEventToAndroid(key, command); |
| 187 } | 192 } |
| 188 | 193 |
| 189 void ArcNotificationManager::OnToastPosted(mojom::ArcToastDataPtr data) { | 194 void ArcNotificationManager::OnToastPosted(mojom::ArcToastDataPtr data) { |
| 190 ash::Shell::GetInstance()->toast_manager()->Show( | 195 ash::Shell::GetInstance()->toast_manager()->Show( |
| 191 ash::ToastData(data->id, data->text, data->duration, data->dismiss_text)); | 196 ash::ToastData(data->id, data->text, data->duration, data->dismiss_text)); |
| 192 } | 197 } |
| 193 | 198 |
| 194 void ArcNotificationManager::OnToastCancelled(mojom::ArcToastDataPtr data) { | 199 void ArcNotificationManager::OnToastCancelled(mojom::ArcToastDataPtr data) { |
| 195 ash::Shell::GetInstance()->toast_manager()->Cancel(data->id); | 200 ash::Shell::GetInstance()->toast_manager()->Cancel(data->id); |
| 196 } | 201 } |
| 197 | 202 |
| 198 } // namespace arc | 203 } // namespace arc |
| OLD | NEW |