Commit 9beb97e8 authored by mark a. foltz's avatar mark a. foltz Committed by Commit Bot

[Media Router] Don't show cloud sinks for incognito windows.

The Cloud MRP does not support casting to cloud sinks from incognito. This patch
filters them from the Cast dialog.

Other fixes:
 - Use base::EraseIf consistently
 - Replace Equals() with == in MediaSinkWithCastModes
 - Misc IWYU

Internal bug: b/37546693

Bug: 921094
Change-Id: I1ba7bf52e37f67ed4f6b6df3579ae109932db615
Reviewed-on: https://chromium-review.googlesource.com/c/1406309Reviewed-by: default avatarTakumi Fujimoto <takumif@chromium.org>
Commit-Queue: mark a. foltz <mfoltz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#622174}
parent 753eeddd
......@@ -8,9 +8,11 @@
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/atomic_sequence_num.h"
#include "base/macros.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
......@@ -288,14 +290,14 @@ std::vector<MediaSinkWithCastModes> MediaRouterUIBase::GetEnabledSinks() const {
// provider-specific behavior, but we currently do not have a way to
// communicate dialog-specific information to/from the
// WiredDisplayMediaRouteProvider.
std::vector<MediaSinkWithCastModes> enabled_sinks;
std::vector<MediaSinkWithCastModes> enabled_sinks(sinks_);
const std::string display_sink_id =
WiredDisplayMediaRouteProvider::GetSinkIdForDisplay(
display_observer_->GetCurrentDisplay());
for (const MediaSinkWithCastModes& sink : sinks_) {
if (sink.sink.id() != display_sink_id)
enabled_sinks.push_back(sink);
}
base::EraseIf(enabled_sinks,
[&display_sink_id](const MediaSinkWithCastModes& sink) {
return sink.sink.id() == display_sink_id;
});
return enabled_sinks;
}
......
......@@ -14,7 +14,8 @@ MediaSinkWithCastModes::MediaSinkWithCastModes(
MediaSinkWithCastModes::~MediaSinkWithCastModes() {}
bool MediaSinkWithCastModes::Equals(const MediaSinkWithCastModes& other) const {
bool MediaSinkWithCastModes::operator==(
const MediaSinkWithCastModes& other) const {
return sink.Equals(other.sink) && cast_modes == other.cast_modes;
}
......
......@@ -20,11 +20,10 @@ struct MediaSinkWithCastModes {
explicit MediaSinkWithCastModes(const MediaSink& sink);
MediaSinkWithCastModes(const MediaSinkWithCastModes& other);
~MediaSinkWithCastModes();
bool operator==(const MediaSinkWithCastModes& other) const;
MediaSink sink;
CastModeSet cast_modes;
bool Equals(const MediaSinkWithCastModes& other) const;
};
} // namespace media_router
......
......@@ -81,7 +81,7 @@ MATCHER_P(VectorEquals, expected, "") {
return false;
}
for (size_t i = 0; i < expected.size(); ++i) {
if (!expected[i].Equals(arg[i])) {
if (!(expected[i] == arg[i])) {
return false;
}
}
......
......@@ -16,6 +16,7 @@
#include "chrome/browser/ui/media_router/ui_media_sink.h"
#include "chrome/common/media_router/route_request_result.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_context.h"
#include "ui/base/l10n/l10n_util.h"
namespace media_router {
......@@ -88,6 +89,18 @@ std::vector<MediaSinkWithCastModes> MediaRouterViewsUI::GetEnabledSinks()
return base::StartsWith(sink.sink.id(),
"pseudo:", base::CompareCase::SENSITIVE);
});
// Filter out cloud sinks if the window is incognito. Casting to cloud sinks
// from incognito is not currently supported by the Cloud MRP. This is not
// the best place to do this, but the Media Router browser service and
// extension process are shared between normal and incognito, so incognito
// behaviors around sink availability have to be handled at the UI layer.
if (initiator()->GetBrowserContext()->IsOffTheRecord()) {
base::EraseIf(sinks, [](const MediaSinkWithCastModes& sink) {
return sink.sink.IsMaybeCloudSink();
});
}
return sinks;
}
......
......@@ -30,6 +30,7 @@ class MediaRouterViewsUI : public MediaRouterUIBase,
base::OnceCallback<void(const ui::SelectedFileInfo*)> callback) override;
// MediaRouterUIBase:
// Also filters cloud sinks in incognito windows.
std::vector<MediaSinkWithCastModes> GetEnabledSinks() const override;
private:
......@@ -41,6 +42,8 @@ class MediaRouterViewsUI : public MediaRouterUIBase,
FRIEND_TEST_ALL_PREFIXES(MediaRouterViewsUITest, DisconnectingState);
FRIEND_TEST_ALL_PREFIXES(MediaRouterViewsUITest, AddAndRemoveIssue);
FRIEND_TEST_ALL_PREFIXES(MediaRouterViewsUITest, ShowDomainForHangouts);
FRIEND_TEST_ALL_PREFIXES(MediaRouterViewsUIIncognitoTest,
HidesCloudSinksForIncognito);
// MediaRouterUIBase:
void InitCommon(content::WebContents* initiator) override;
......
......@@ -4,6 +4,11 @@
#include "chrome/browser/ui/views/media_router/media_router_views_ui.h"
#include <initializer_list>
#include <memory>
#include <utility>
#include <vector>
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/media/router/media_sinks_observer.h"
#include "chrome/browser/media/router/test/mock_media_router.h"
......@@ -15,6 +20,7 @@
#include "chrome/common/media_router/route_request_result.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/browser_context.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -347,4 +353,36 @@ TEST_F(MediaRouterViewsUITest, ShowDomainForHangouts) {
ui_->RemoveObserver(&observer);
}
class MediaRouterViewsUIIncognitoTest : public MediaRouterViewsUITest {
protected:
content::BrowserContext* GetBrowserContext() override {
return static_cast<Profile*>(MediaRouterViewsUITest::GetBrowserContext())
->GetOffTheRecordProfile();
}
};
TEST_F(MediaRouterViewsUIIncognitoTest, HidesCloudSinksForIncognito) {
const std::string domain1 = "domain1.com";
MediaSinkWithCastModes hangout(
MediaSink("sink1", "Hangout", SinkIconType::HANGOUT));
MediaSinkWithCastModes meeting(
MediaSink("sink2", "Meeting", SinkIconType::MEETING));
MediaSinkWithCastModes eduReceiver(
MediaSink("sink3", "Cast for EDU", SinkIconType::EDUCATION));
MediaSinkWithCastModes chromeCast(
MediaSink("sink4", "Living Room TV", SinkIconType::CAST));
chromeCast.cast_modes = {MediaCastMode::TAB_MIRROR};
for (MediaSinkWithCastModes* sink :
std::initializer_list<MediaSinkWithCastModes*>{&hangout, &meeting,
&eduReceiver}) {
sink->sink.set_domain(domain1);
sink->cast_modes = {MediaCastMode::TAB_MIRROR};
}
ui_->OnResultsUpdated({hangout, meeting, eduReceiver, chromeCast});
EXPECT_EQ(std::vector<MediaSinkWithCastModes>{chromeCast},
ui_->GetEnabledSinks());
}
} // namespace media_router
......@@ -26,6 +26,17 @@ MediaSink::~MediaSink() = default;
MediaSink& MediaSink::operator=(const MediaSink& other) = default;
MediaSink& MediaSink::operator=(MediaSink&& other) noexcept = default;
bool MediaSink::IsMaybeCloudSink() const {
switch (icon_type_) {
case SinkIconType::MEETING:
case SinkIconType::HANGOUT:
case SinkIconType::EDUCATION:
return true;
default:
return false;
}
}
bool MediaSink::Equals(const MediaSink& other) const {
return sink_id_ == other.sink_id_;
}
......
......@@ -80,6 +80,10 @@ class MediaSink {
}
MediaRouteProviderId provider_id() const { return provider_id_; }
// Returns true if the sink is from the Cloud MRP; however, as this is based
// solely on the icon type, is not guaranteed to be correct 100% of the time.
bool IsMaybeCloudSink() const;
// This method only compares IDs.
bool Equals(const MediaSink& other) const;
......
......@@ -8,6 +8,19 @@
namespace media_router {
TEST(MediaSinkTest, IsMaybeCloudSink) {
MediaSink meeting("sinkId", "Sink", SinkIconType::MEETING,
MediaRouteProviderId::EXTENSION);
MediaSink eduReceiver("sinkId2", "Sink", SinkIconType::EDUCATION,
MediaRouteProviderId::EXTENSION);
MediaSink chromeCast("sinkId3", "Sink", SinkIconType::CAST,
MediaRouteProviderId::EXTENSION);
EXPECT_TRUE(meeting.IsMaybeCloudSink());
EXPECT_TRUE(eduReceiver.IsMaybeCloudSink());
EXPECT_FALSE(chromeCast.IsMaybeCloudSink());
}
TEST(MediaSinkTest, Equals) {
MediaSink sink1("sinkId", "Sink", SinkIconType::CAST,
MediaRouteProviderId::EXTENSION);
......
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