Commit 159ac40e authored by Xiyuan Xia's avatar Xiyuan Xia Committed by Commit Bot

Use AshMessageCenterClient to get arc app id

Restores the arc app notification badging on shelf icons by re-wiring
ArcNotificationManager get app id code path through mojo.

- Change ArcNotificationManager to handle get app id asynchronously;
- Add GetArcAppIdByPackageName to AshMessageCenterClient mojo interface;
- ArcNotificationManager use the mojo call to get app id;

Bug: 768439
Change-Id: I199636b374d539677d93e35be1fd4f7b9b900bca
Reviewed-on: https://chromium-review.googlesource.com/1012123Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Reviewed-by: default avatarYoshiki Iguchi <yoshiki@chromium.org>
Commit-Queue: Xiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553603}
parent 48ec838d
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "ash/session/session_controller.h" #include "ash/session/session_controller.h"
#include "ash/shell.h" #include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h" #include "ash/strings/grit/ash_strings.h"
#include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/unguessable_token.h" #include "base/unguessable_token.h"
#include "components/signin/core/account_id/account_id.h" #include "components/signin/core/account_id/account_id.h"
...@@ -160,6 +161,9 @@ void MessageCenterController::SetArcNotificationsInstance( ...@@ -160,6 +161,9 @@ void MessageCenterController::SetArcNotificationsInstance(
->GetPrimaryUserSession() ->GetPrimaryUserSession()
->user_info->account_id, ->user_info->account_id,
message_center::MessageCenter::Get()); message_center::MessageCenter::Get());
arc_notification_manager_->set_get_app_id_callback(
base::BindRepeating(&MessageCenterController::GetArcAppIdByPackageName,
base::Unretained(this)));
} }
arc_notification_manager_->SetInstance(std::move(arc_notification_instance)); arc_notification_manager_->SetInstance(std::move(arc_notification_instance));
} }
...@@ -226,4 +230,11 @@ void MessageCenterController::OnGotNotifierList( ...@@ -226,4 +230,11 @@ void MessageCenterController::OnGotNotifierList(
notifier_id_->SetNotifierList(ui_data); notifier_id_->SetNotifierList(ui_data);
} }
void MessageCenterController::GetArcAppIdByPackageName(
const std::string& package_name,
arc::ArcNotificationManager::GetAppIdResponseCallback callback) {
DCHECK(client_.is_bound());
client_->GetArcAppIdByPackageName(package_name, std::move(callback));
}
} // namespace ash } // namespace ash
...@@ -78,6 +78,11 @@ class ASH_EXPORT MessageCenterController ...@@ -78,6 +78,11 @@ class ASH_EXPORT MessageCenterController
// Callback for GetNotifierList. // Callback for GetNotifierList.
void OnGotNotifierList(std::vector<mojom::NotifierUiDataPtr> ui_data); void OnGotNotifierList(std::vector<mojom::NotifierUiDataPtr> ui_data);
// Handles get app id calls from ArcNotificationManager.
void GetArcAppIdByPackageName(
const std::string& package_name,
arc::ArcNotificationManager::GetAppIdResponseCallback callback);
std::unique_ptr<FullscreenNotificationBlocker> std::unique_ptr<FullscreenNotificationBlocker>
fullscreen_notification_blocker_; fullscreen_notification_blocker_;
std::unique_ptr<InactiveUserNotificationBlocker> std::unique_ptr<InactiveUserNotificationBlocker>
......
...@@ -67,6 +67,11 @@ class TestAshMessageCenterClient : public mojom::AshMessageCenterClient { ...@@ -67,6 +67,11 @@ class TestAshMessageCenterClient : public mojom::AshMessageCenterClient {
std::move(callback).Run(std::move(ui_data)); std::move(callback).Run(std::move(ui_data));
} }
void GetArcAppIdByPackageName(
const std::string& package_name,
GetArcAppIdByPackageNameCallback callback) override {
std::move(callback).Run(std::string());
}
private: private:
bool no_notifiers_ = false; bool no_notifiers_ = false;
......
...@@ -89,4 +89,7 @@ interface AshMessageCenterClient { ...@@ -89,4 +89,7 @@ interface AshMessageCenterClient {
// Asks the client for a list of notifiers and associated UI information to be // Asks the client for a list of notifiers and associated UI information to be
// displayed in a settings panel. // displayed in a settings panel.
GetNotifierList() => (array<NotifierUiData> notifiers); GetNotifierList() => (array<NotifierUiData> notifiers);
// Gets ARC app id from a given package name.
GetArcAppIdByPackageName(string package_name) => (string app_id);
}; };
...@@ -7,10 +7,12 @@ ...@@ -7,10 +7,12 @@
#include "ash/public/interfaces/constants.mojom.h" #include "ash/public/interfaces/constants.mojom.h"
#include "base/i18n/string_compare.h" #include "base/i18n/string_compare.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "chrome/browser/chromeos/arc/arc_session_manager.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/notifications/arc_application_notifier_controller.h" #include "chrome/browser/notifications/arc_application_notifier_controller.h"
#include "chrome/browser/notifications/extension_notifier_controller.h" #include "chrome/browser/notifications/extension_notifier_controller.h"
#include "chrome/browser/notifications/web_page_notifier_controller.h" #include "chrome/browser/notifications/web_page_notifier_controller.h"
#include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h"
#include "components/user_manager/user_manager.h" #include "components/user_manager/user_manager.h"
#include "content/public/common/service_manager_connection.h" #include "content/public/common/service_manager_connection.h"
#include "services/service_manager/public/cpp/connector.h" #include "services/service_manager/public/cpp/connector.h"
...@@ -164,6 +166,14 @@ void ChromeAshMessageCenterClient::GetNotifierList( ...@@ -164,6 +166,14 @@ void ChromeAshMessageCenterClient::GetNotifierList(
std::move(callback).Run(std::move(notifiers)); std::move(callback).Run(std::move(notifiers));
} }
void ChromeAshMessageCenterClient::GetArcAppIdByPackageName(
const std::string& package_name,
GetArcAppIdByPackageNameCallback callback) {
std::move(callback).Run(
ArcAppListPrefs::Get(arc::ArcSessionManager::Get()->profile())
->GetAppIdByPackageName(package_name));
}
void ChromeAshMessageCenterClient::OnIconImageUpdated( void ChromeAshMessageCenterClient::OnIconImageUpdated(
const NotifierId& notifier_id, const NotifierId& notifier_id,
const gfx::ImageSkia& image) { const gfx::ImageSkia& image) {
......
...@@ -42,6 +42,9 @@ class ChromeAshMessageCenterClient : public ash::mojom::AshMessageCenterClient, ...@@ -42,6 +42,9 @@ class ChromeAshMessageCenterClient : public ash::mojom::AshMessageCenterClient,
void SetNotifierEnabled(const message_center::NotifierId& notifier_id, void SetNotifierEnabled(const message_center::NotifierId& notifier_id,
bool enabled) override; bool enabled) override;
void GetNotifierList(GetNotifierListCallback callback) override; void GetNotifierList(GetNotifierListCallback callback) override;
void GetArcAppIdByPackageName(
const std::string& package_name,
GetArcAppIdByPackageNameCallback callback) override;
// NotifierController::Observer: // NotifierController::Observer:
void OnIconImageUpdated(const message_center::NotifierId& notifier_id, void OnIconImageUpdated(const message_center::NotifierId& notifier_id,
......
...@@ -100,6 +100,9 @@ ArcNotificationManager::ArcNotificationManager( ...@@ -100,6 +100,9 @@ ArcNotificationManager::ArcNotificationManager(
ArcNotificationManager::~ArcNotificationManager() { ArcNotificationManager::~ArcNotificationManager() {
instance_owner_->holder()->RemoveObserver(this); instance_owner_->holder()->RemoveObserver(this);
instance_owner_->holder()->SetHost(nullptr); instance_owner_->holder()->SetHost(nullptr);
// Ensures that any callback tied to |instance_owner_| is not invoked.
instance_owner_.reset();
} }
void ArcNotificationManager::SetInstance( void ArcNotificationManager::SetInstance(
...@@ -149,9 +152,10 @@ void ArcNotificationManager::OnNotificationPosted( ...@@ -149,9 +152,10 @@ void ArcNotificationManager::OnNotificationPosted(
DCHECK(result.second); DCHECK(result.second);
it = result.first; it = result.first;
} }
const std::string app_id =
data->package_name ? GetAppId(data->package_name.value()) : std::string(); GetAppId(data->package_name.value_or(std::string()),
it->second->OnUpdatedFromAndroid(std::move(data), app_id); base::BindOnce(&ArcNotificationManager::OnGotAppId,
weak_ptr_factory_.GetWeakPtr(), std::move(data)));
} }
void ArcNotificationManager::OnNotificationUpdated( void ArcNotificationManager::OnNotificationUpdated(
...@@ -166,8 +170,9 @@ void ArcNotificationManager::OnNotificationUpdated( ...@@ -166,8 +170,9 @@ void ArcNotificationManager::OnNotificationUpdated(
if (it == items_.end()) if (it == items_.end())
return; return;
const std::string app_id = GetAppId(data->package_name.value()); GetAppId(data->package_name.value_or(std::string()),
it->second->OnUpdatedFromAndroid(std::move(data), app_id); base::BindOnce(&ArcNotificationManager::OnGotAppId,
weak_ptr_factory_.GetWeakPtr(), std::move(data)));
} }
void ArcNotificationManager::OnNotificationRemoved(const std::string& key) { void ArcNotificationManager::OnNotificationRemoved(const std::string& key) {
...@@ -369,12 +374,24 @@ bool ArcNotificationManager::ShouldIgnoreNotification( ...@@ -369,12 +374,24 @@ bool ArcNotificationManager::ShouldIgnoreNotification(
return false; return false;
} }
std::string ArcNotificationManager::GetAppId( void ArcNotificationManager::GetAppId(const std::string& package_name,
const std::string& package_name) const { GetAppIdResponseCallback callback) const {
if (get_app_id_callback_.is_null()) if (get_app_id_callback_.is_null() || package_name.empty()) {
return std::string(); std::move(callback).Run(std::string());
return;
}
get_app_id_callback_.Run(package_name, std::move(callback));
}
void ArcNotificationManager::OnGotAppId(mojom::ArcNotificationDataPtr data,
const std::string& app_id) {
const std::string& key = data->key;
auto it = items_.find(key);
if (it == items_.end())
return;
return get_app_id_callback_.Run(package_name); it->second->OnUpdatedFromAndroid(std::move(data), app_id);
} }
} // namespace arc } // namespace arc
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include "base/memory/weak_ptr.h"
#include "components/arc/common/notifications.mojom.h" #include "components/arc/common/notifications.mojom.h"
#include "components/arc/connection_holder.h" #include "components/arc/connection_holder.h"
#include "components/arc/connection_observer.h" #include "components/arc/connection_observer.h"
...@@ -37,10 +38,13 @@ class ArcNotificationManager ...@@ -37,10 +38,13 @@ class ArcNotificationManager
ConnectionHolder<mojom::NotificationsInstance, mojom::NotificationsHost>* ConnectionHolder<mojom::NotificationsInstance, mojom::NotificationsHost>*
GetConnectionHolderForTest(); GetConnectionHolderForTest();
void set_get_app_id_callback( using GetAppIdResponseCallback =
base::RepeatingCallback<std::string(const std::string&)> base::OnceCallback<void(const std::string& app_id)>;
get_app_id_callback) { using GetAppIdCallback =
get_app_id_callback_ = std::move(get_app_id_callback); base::RepeatingCallback<void(const std::string& package_name,
GetAppIdResponseCallback callback)>;
void set_get_app_id_callback(const GetAppIdCallback& get_app_id_callback) {
get_app_id_callback_ = get_app_id_callback;
} }
// ConnectionObserver<mojom::NotificationsInstance> implementation: // ConnectionObserver<mojom::NotificationsInstance> implementation:
...@@ -69,8 +73,14 @@ class ArcNotificationManager ...@@ -69,8 +73,14 @@ class ArcNotificationManager
bool ShouldIgnoreNotification(mojom::ArcNotificationData* data); bool ShouldIgnoreNotification(mojom::ArcNotificationData* data);
// Calls |get_app_id_callback_| to retrieve the app id from ArcAppListPrefs. // Calls |get_app_id_callback_| to retrieve the app id. |callback| will be
std::string GetAppId(const std::string& package_name) const; // invoked with the app id or an empty string.
void GetAppId(const std::string& package_name,
GetAppIdResponseCallback callback) const;
// Invoked when |get_app_id_callback_| gets back the app id.
void OnGotAppId(mojom::ArcNotificationDataPtr data,
const std::string& app_id);
const AccountId main_profile_id_; const AccountId main_profile_id_;
message_center::MessageCenter* const message_center_; message_center::MessageCenter* const message_center_;
...@@ -79,14 +89,14 @@ class ArcNotificationManager ...@@ -79,14 +89,14 @@ class ArcNotificationManager
std::unordered_map<std::string, std::unique_ptr<ArcNotificationItem>>; std::unordered_map<std::string, std::unique_ptr<ArcNotificationItem>>;
ItemMap items_; ItemMap items_;
base::RepeatingCallback<std::string(const std::string&)> get_app_id_callback_; GetAppIdCallback get_app_id_callback_;
bool ready_ = false; bool ready_ = false;
// Put as a last member to ensure that any callback tied to the elements
// is not invoked.
std::unique_ptr<InstanceOwner> instance_owner_; std::unique_ptr<InstanceOwner> instance_owner_;
base::WeakPtrFactory<ArcNotificationManager> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(ArcNotificationManager); DISALLOW_COPY_AND_ASSIGN(ArcNotificationManager);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment