Commit 2d77868a authored by Jazz Xu's avatar Jazz Xu Committed by Commit Bot

CrOS GMC: Show GMC button if there's playing media.

This CL adds MediaNotificationProviderImpl as a
MediaNotificationServiceObserver and it will inform its own observer
when MediaNotificationListChange so that the Zenith button living in ash
can show/hide correspondingly.
This CL also modifies MediaNotificationService such that it can show
session from all profiles when needed.

Bug: 1121723

Change-Id: I778976a154dd00dda9f83b300dced364cadfb56f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2369653
Commit-Queue: Jazz Xu <jazzhsu@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarTommy Steimel <steimel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#801894}
parent 218717e9
...@@ -5,11 +5,23 @@ ...@@ -5,11 +5,23 @@
#include "chrome/browser/ui/ash/media_notification_provider_impl.h" #include "chrome/browser/ui/ash/media_notification_provider_impl.h"
#include "ash/public/cpp/media_notification_provider_observer.h" #include "ash/public/cpp/media_notification_provider_observer.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/ui/global_media_controls/media_notification_service.h"
#include "chrome/browser/ui/global_media_controls/media_notification_service_factory.h"
#include "components/session_manager/core/session_manager.h"
#include "ui/views/view.h" #include "ui/views/view.h"
MediaNotificationProviderImpl::MediaNotificationProviderImpl() = default; MediaNotificationProviderImpl::MediaNotificationProviderImpl() {
session_manager::SessionManager::Get()->AddObserver(this);
}
MediaNotificationProviderImpl::~MediaNotificationProviderImpl() {
if (session_manager::SessionManager::Get())
session_manager::SessionManager::Get()->RemoveObserver(this);
MediaNotificationProviderImpl::~MediaNotificationProviderImpl() = default; if (service_)
service_->RemoveObserver(this);
}
void MediaNotificationProviderImpl::AddObserver( void MediaNotificationProviderImpl::AddObserver(
ash::MediaNotificationProviderObserver* observer) { ash::MediaNotificationProviderObserver* observer) {
...@@ -22,11 +34,15 @@ void MediaNotificationProviderImpl::RemoveObserver( ...@@ -22,11 +34,15 @@ void MediaNotificationProviderImpl::RemoveObserver(
} }
bool MediaNotificationProviderImpl::HasActiveNotifications() { bool MediaNotificationProviderImpl::HasActiveNotifications() {
return false; if (!service_)
return false;
return service_->HasActiveNotifications();
} }
bool MediaNotificationProviderImpl::HasFrozenNotifications() { bool MediaNotificationProviderImpl::HasFrozenNotifications() {
return false; if (!service_)
return false;
return service_->HasFrozenNotifications();
} }
std::unique_ptr<views::View> std::unique_ptr<views::View>
...@@ -38,3 +54,21 @@ std::unique_ptr<views::View> ...@@ -38,3 +54,21 @@ std::unique_ptr<views::View>
MediaNotificationProviderImpl::GetActiveMediaNotificationView() { MediaNotificationProviderImpl::GetActiveMediaNotificationView() {
return std::make_unique<views::View>(); return std::make_unique<views::View>();
} }
void MediaNotificationProviderImpl::OnNotificationListChanged() {
for (auto& observer : observers_)
observer.OnNotificationListChanged();
}
void MediaNotificationProviderImpl::OnUserProfileLoaded(
const AccountId& account_id) {
Profile* profile =
chromeos::ProfileHelper::Get()->GetProfileByAccountId(account_id);
user_manager::User* user =
chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
if (user_manager::UserManager::Get()->GetPrimaryUser() == user) {
service_ = MediaNotificationServiceFactory::GetForProfile(profile);
service_->AddObserver(this);
}
}
...@@ -7,8 +7,15 @@ ...@@ -7,8 +7,15 @@
#include "ash/public/cpp/media_notification_provider.h" #include "ash/public/cpp/media_notification_provider.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "chrome/browser/ui/global_media_controls/media_notification_service_observer.h"
#include "components/session_manager/core/session_manager_observer.h"
class MediaNotificationProviderImpl : public ash::MediaNotificationProvider { class MediaNotificationService;
class MediaNotificationProviderImpl
: public ash::MediaNotificationProvider,
public MediaNotificationServiceObserver,
public session_manager::SessionManagerObserver {
public: public:
MediaNotificationProviderImpl(); MediaNotificationProviderImpl();
~MediaNotificationProviderImpl() override; ~MediaNotificationProviderImpl() override;
...@@ -22,8 +29,17 @@ class MediaNotificationProviderImpl : public ash::MediaNotificationProvider { ...@@ -22,8 +29,17 @@ class MediaNotificationProviderImpl : public ash::MediaNotificationProvider {
std::unique_ptr<views::View> GetMediaNotificationListView() override; std::unique_ptr<views::View> GetMediaNotificationListView() override;
std::unique_ptr<views::View> GetActiveMediaNotificationView() override; std::unique_ptr<views::View> GetActiveMediaNotificationView() override;
// MediaNotificationServiceObserver implementations.
void OnNotificationListChanged() override;
void OnMediaDialogOpenedOrClosed() override {}
// SessionManagerobserver implementation.
void OnUserProfileLoaded(const AccountId& account_id) override;
private: private:
base::ObserverList<ash::MediaNotificationProviderObserver> observers_; base::ObserverList<ash::MediaNotificationProviderObserver> observers_;
MediaNotificationService* service_ = nullptr;
}; };
#endif // CHROME_BROWSER_UI_ASH_MEDIA_NOTIFICATION_PROVIDER_IMPL_H_ #endif // CHROME_BROWSER_UI_ASH_MEDIA_NOTIFICATION_PROVIDER_IMPL_H_
...@@ -274,7 +274,8 @@ void MediaNotificationService::Session::MarkActiveIfNecessary() { ...@@ -274,7 +274,8 @@ void MediaNotificationService::Session::MarkActiveIfNecessary() {
owner_->OnSessionBecameActive(id_); owner_->OnSessionBecameActive(id_);
} }
MediaNotificationService::MediaNotificationService(Profile* profile) MediaNotificationService::MediaNotificationService(Profile* profile,
bool show_from_all_profiles)
: overlay_media_notifications_manager_(this) { : overlay_media_notifications_manager_(this) {
if (base::FeatureList::IsEnabled(media::kGlobalMediaControlsForCast) && if (base::FeatureList::IsEnabled(media::kGlobalMediaControlsForCast) &&
media_router::MediaRouterEnabled(profile)) { media_router::MediaRouterEnabled(profile)) {
...@@ -286,9 +287,6 @@ MediaNotificationService::MediaNotificationService(Profile* profile) ...@@ -286,9 +287,6 @@ MediaNotificationService::MediaNotificationService(Profile* profile)
base::Unretained(this))); base::Unretained(this)));
} }
const base::UnguessableToken& source_id =
content::MediaSession::GetSourceId(profile);
// Connect to the controller manager so we can create media controllers for // Connect to the controller manager so we can create media controllers for
// media sessions. // media sessions.
content::GetMediaSessionService().BindMediaControllerManager( content::GetMediaSessionService().BindMediaControllerManager(
...@@ -297,13 +295,26 @@ MediaNotificationService::MediaNotificationService(Profile* profile) ...@@ -297,13 +295,26 @@ MediaNotificationService::MediaNotificationService(Profile* profile)
// Connect to receive audio focus events. // Connect to receive audio focus events.
content::GetMediaSessionService().BindAudioFocusManager( content::GetMediaSessionService().BindAudioFocusManager(
audio_focus_remote_.BindNewPipeAndPassReceiver()); audio_focus_remote_.BindNewPipeAndPassReceiver());
audio_focus_remote_->AddSourceObserver(
source_id, audio_focus_observer_receiver_.BindNewPipeAndPassRemote());
audio_focus_remote_->GetSourceFocusRequests( if (show_from_all_profiles) {
source_id, audio_focus_remote_->AddObserver(
base::BindOnce(&MediaNotificationService::OnReceivedAudioFocusRequests, audio_focus_observer_receiver_.BindNewPipeAndPassRemote());
weak_ptr_factory_.GetWeakPtr()));
audio_focus_remote_->GetFocusRequests(
base::BindOnce(&MediaNotificationService::OnReceivedAudioFocusRequests,
weak_ptr_factory_.GetWeakPtr()));
} else {
const base::UnguessableToken& source_id =
content::MediaSession::GetSourceId(profile);
audio_focus_remote_->AddSourceObserver(
source_id, audio_focus_observer_receiver_.BindNewPipeAndPassRemote());
audio_focus_remote_->GetSourceFocusRequests(
source_id,
base::BindOnce(&MediaNotificationService::OnReceivedAudioFocusRequests,
weak_ptr_factory_.GetWeakPtr()));
}
} }
MediaNotificationService::~MediaNotificationService() { MediaNotificationService::~MediaNotificationService() {
......
...@@ -47,7 +47,7 @@ class MediaNotificationService ...@@ -47,7 +47,7 @@ class MediaNotificationService
public media_message_center::MediaNotificationController, public media_message_center::MediaNotificationController,
public MediaNotificationContainerObserver { public MediaNotificationContainerObserver {
public: public:
explicit MediaNotificationService(Profile* profile); MediaNotificationService(Profile* profile, bool show_from_all_profiles);
MediaNotificationService(const MediaNotificationService&) = delete; MediaNotificationService(const MediaNotificationService&) = delete;
MediaNotificationService& operator=(const MediaNotificationService&) = delete; MediaNotificationService& operator=(const MediaNotificationService&) = delete;
~MediaNotificationService() override; ~MediaNotificationService() override;
......
...@@ -34,7 +34,12 @@ MediaNotificationService* MediaNotificationServiceFactory::GetForProfile( ...@@ -34,7 +34,12 @@ MediaNotificationService* MediaNotificationServiceFactory::GetForProfile(
KeyedService* MediaNotificationServiceFactory::BuildServiceInstanceFor( KeyedService* MediaNotificationServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const { content::BrowserContext* context) const {
return new MediaNotificationService(Profile::FromBrowserContext(context)); bool show_from_all_profiles = false;
#if defined(CHROME_OS)
show_from_all_profiles = true;
#endif
return new MediaNotificationService(Profile::FromBrowserContext(context),
show_from_all_profiles);
} }
content::BrowserContext* content::BrowserContext*
......
...@@ -136,7 +136,7 @@ class MediaNotificationServiceTest : public testing::Test { ...@@ -136,7 +136,7 @@ class MediaNotificationServiceTest : public testing::Test {
void SetUp() override { void SetUp() override {
media_router::MediaRouterFactory::GetInstance()->SetTestingFactory( media_router::MediaRouterFactory::GetInstance()->SetTestingFactory(
&profile_, base::BindRepeating(&media_router::MockMediaRouter::Create)); &profile_, base::BindRepeating(&media_router::MockMediaRouter::Create));
service_ = std::make_unique<MediaNotificationService>(&profile_); service_ = std::make_unique<MediaNotificationService>(&profile_, false);
service_->AddObserver(&observer_); service_->AddObserver(&observer_);
} }
......
...@@ -87,7 +87,7 @@ class MediaToolbarButtonControllerTest : public testing::Test { ...@@ -87,7 +87,7 @@ class MediaToolbarButtonControllerTest : public testing::Test {
MediaToolbarButtonControllerTest() MediaToolbarButtonControllerTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME, : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME,
base::test::TaskEnvironment::MainThreadType::UI), base::test::TaskEnvironment::MainThreadType::UI),
service_(&profile_) {} service_(&profile_, false) {}
~MediaToolbarButtonControllerTest() override = default; ~MediaToolbarButtonControllerTest() override = default;
void SetUp() override { void SetUp() override {
......
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