Skip to content

Commit

Permalink
Cast Refactor: separate presentation setup/teardown from controllers.
Browse files Browse the repository at this point in the history
This is a pre-step. The new CastService object will be managing the
lifetime of start_presentation_context_ and presentation_manager_, and
so it is necessary to detangle the setup/lifetime of these objects from
info stored in the controllers. This first step is to demonstrate that
this initial decoupling breaks no existing behavior. The next step will
be to create an initial CastService that will take over the lifetimes
of start_presentation_context_, presentation_manager_, and
query_result_manager_ exposing those objects plus some new events to
the controllers. The step after that will be to create a CastToSink
method to the Controller that, given a sink id, will create a route to
that sink id, and hide the presentation objects from the controllers.

BUG=b:201430609

Change-Id: I59251c0de05afc9016a01bf24cf0699c3a69a357
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3544126
Reviewed-by: Takumi Fujimoto <takumif@chromium.org>
Reviewed-by: George Benz <gbj@google.com>
Commit-Queue: Brian Malcolm <bmalcolm@chromium.org>
Cr-Commit-Position: refs/heads/main@{#985584}
  • Loading branch information
Brian Malcolm authored and Chromium LUCI CQ committed Mar 26, 2022
1 parent 9280bd4 commit bfb9a18
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
24 changes: 9 additions & 15 deletions chrome/browser/ui/media_router/media_router_ui.cc
Expand Up @@ -126,8 +126,12 @@ MediaRouterUI::~MediaRouterUI() {
// If |start_presentation_context_| still exists, then it means presentation
// route request was never attempted.
if (start_presentation_context_) {
std::vector<MediaSinkWithCastModes> sinks;
if (query_result_manager_.get()) {
sinks = query_result_manager_->GetSinksWithCastModes();
}
bool presentation_sinks_available = std::any_of(
sinks_.begin(), sinks_.end(), [](const MediaSinkWithCastModes& sink) {
sinks.begin(), sinks.end(), [](const MediaSinkWithCastModes& sink) {
return base::Contains(sink.cast_modes, MediaCastMode::PRESENTATION);
});
if (presentation_sinks_available) {
Expand Down Expand Up @@ -188,11 +192,6 @@ void MediaRouterUI::InitWithDefaultMediaSource() {
presentation_manager_->HasDefaultPresentationRequest()) {
OnDefaultPresentationChanged(
&presentation_manager_->GetDefaultPresentationRequest());
} else {
// Register for MediaRoute updates without a media source.
routes_observer_ = std::make_unique<UIMediaRoutesObserver>(
GetMediaRouter(), base::BindRepeating(&MediaRouterUI::OnRoutesUpdated,
base::Unretained(this)));
}
}

Expand Down Expand Up @@ -424,6 +423,10 @@ void MediaRouterUI::InitCommon() {
initiator_,
base::BindRepeating(&MediaRouterUI::UpdateSinks, base::Unretained(this)));

routes_observer_ = std::make_unique<UIMediaRoutesObserver>(
GetMediaRouter(), base::BindRepeating(&MediaRouterUI::OnRoutesUpdated,
base::Unretained(this)));

StartObservingIssues();
}

Expand Down Expand Up @@ -460,22 +463,13 @@ void MediaRouterUI::OnDefaultPresentationChanged(
query_result_manager_->SetSourcesForCastMode(
MediaCastMode::PRESENTATION, sources,
presentation_request_->frame_origin);
// Register for MediaRoute updates.
routes_observer_ = std::make_unique<UIMediaRoutesObserver>(
GetMediaRouter(), base::BindRepeating(&MediaRouterUI::OnRoutesUpdated,
base::Unretained(this)));
UpdateModelHeader();
}

void MediaRouterUI::OnDefaultPresentationRemoved() {
presentation_request_.reset();
query_result_manager_->RemoveSourcesForCastMode(MediaCastMode::PRESENTATION);

// Register for MediaRoute updates.
routes_observer_ = std::make_unique<UIMediaRoutesObserver>(
GetMediaRouter(), base::BindRepeating(&MediaRouterUI::OnRoutesUpdated,
base::Unretained(this)));

UpdateModelHeader();
}

Expand Down
27 changes: 17 additions & 10 deletions chrome/browser/ui/media_router/query_result_manager.cc
Expand Up @@ -159,6 +159,22 @@ std::vector<MediaSource> QueryResultManager::GetSourcesForCastMode(
: cast_mode_it->second;
}

std::vector<MediaSinkWithCastModes> QueryResultManager::GetSinksWithCastModes()
const {
std::vector<MediaSinkWithCastModes> sinks;
for (const auto& sink_pair : sinks_with_sources_) {
MediaSinkWithCastModes sink_with_cast_modes(sink_pair.second.sink());
sink_with_cast_modes.cast_modes = sink_pair.second.GetCastModes();
sinks.push_back(sink_with_cast_modes);
}
for (const auto& sink : all_sinks_) {
if (!base::Contains(sinks_with_sources_, sink.id()))
sinks.push_back(MediaSinkWithCastModes(sink));
}

return sinks;
}

void QueryResultManager::RemoveOldSourcesForCastMode(
MediaCastMode cast_mode,
const std::vector<MediaSource>& new_sources) {
Expand Down Expand Up @@ -260,16 +276,7 @@ bool QueryResultManager::AreSourcesValidForCastMode(
}

void QueryResultManager::NotifyOnResultsUpdated() {
std::vector<MediaSinkWithCastModes> sinks;
for (const auto& sink_pair : sinks_with_sources_) {
MediaSinkWithCastModes sink_with_cast_modes(sink_pair.second.sink());
sink_with_cast_modes.cast_modes = sink_pair.second.GetCastModes();
sinks.push_back(sink_with_cast_modes);
}
for (const auto& sink : all_sinks_) {
if (!base::Contains(sinks_with_sources_, sink.id()))
sinks.push_back(MediaSinkWithCastModes(sink));
}
std::vector<MediaSinkWithCastModes> sinks = GetSinksWithCastModes();
for (QueryResultManager::Observer& observer : observers_)
observer.OnResultsUpdated(sinks);
}
Expand Down
3 changes: 3 additions & 0 deletions chrome/browser/ui/media_router/query_result_manager.h
Expand Up @@ -114,6 +114,9 @@ class QueryResultManager {
// vector if there is none.
std::vector<MediaSource> GetSourcesForCastMode(MediaCastMode cast_mode) const;

// Returns all of the currently known sinks with the cast modes they support
std::vector<MediaSinkWithCastModes> GetSinksWithCastModes() const;

private:
class MediaSourceMediaSinksObserver;
class AnyMediaSinksObserver;
Expand Down
Expand Up @@ -153,8 +153,15 @@ AccessCodeCastHandler::~AccessCodeCastHandler() {
// If |start_presentation_context_| still exists, then it means presentation
// route request was never attempted.
if (start_presentation_context_) {
if (sink_id_ &&
base::Contains(supported_cast_modes_, MediaCastMode::PRESENTATION)) {
std::vector<MediaSinkWithCastModes> sinks;
if (query_result_manager_.get()) {
sinks = query_result_manager_->GetSinksWithCastModes();
}
bool presentation_sinks_available = std::any_of(
sinks.begin(), sinks.end(), [](const MediaSinkWithCastModes& sink) {
return base::Contains(sink.cast_modes, MediaCastMode::PRESENTATION);
});
if (presentation_sinks_available) {
start_presentation_context_->InvokeErrorCallback(
blink::mojom::PresentationError(blink::mojom::PresentationErrorType::
PRESENTATION_REQUEST_CANCELLED,
Expand Down
Expand Up @@ -150,8 +150,6 @@ class AccessCodeCastHandler : public access_code_cast::mojom::PageHandler,

// The id of the media sink discovered from the access code;
absl::optional<MediaSink::Id> sink_id_;
// Set of cast modes supported by the discovered sink;
media_router::CastModeSet supported_cast_modes_;

// Monitors and reports sink availability.
std::unique_ptr<QueryResultManager> query_result_manager_;
Expand Down

0 comments on commit bfb9a18

Please sign in to comment.