#include "chrome/browser/media/webrtc/tab_capture_access_handler.h"
#include <memory>
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h"
#include "chrome/browser/media/webrtc/fake_desktop_media_picker_factory.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/mediastream/media_stream_request.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/chromeos/policy/dlp/dlp_content_manager.h"
#include "chrome/browser/chromeos/policy/dlp/test/mock_dlp_content_manager.h"
#endif
namespace {
constexpr char kOrigin[] = …;
constexpr blink::mojom::MediaStreamRequestResult kInvalidResult = …;
}
class TabCaptureAccessHandlerTest : public ChromeRenderViewHostTestHarness { … };
TEST_F(TabCaptureAccessHandlerTest, PermissionGiven) { … }
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(TabCaptureAccessHandlerTest, DlpRestricted) {
const content::DesktopMediaID source(
content::DesktopMediaID::TYPE_WEB_CONTENTS,
content::DesktopMediaID::kNullId,
content::WebContentsMediaCaptureId(
web_contents()->GetPrimaryMainFrame()->GetProcess()->GetID(),
web_contents()->GetPrimaryMainFrame()->GetRoutingID()));
policy::MockDlpContentManager mock_dlp_content_manager;
policy::ScopedDlpContentObserverForTesting scoped_dlp_content_manager(
&mock_dlp_content_manager);
EXPECT_CALL(mock_dlp_content_manager, CheckScreenShareRestriction)
.WillOnce([](const content::DesktopMediaID& media_id,
const std::u16string& application_title,
base::OnceCallback<void(bool)> callback) {
std::move(callback).Run(false);
});
extensions::TabCaptureRegistry::Get(profile())->AddRequest(
web_contents(), "", false,
GURL(kOrigin), source, process_id(), main_frame_id());
blink::mojom::MediaStreamRequestResult result = kInvalidResult;
blink::mojom::StreamDevices devices;
ProcessRequest(source, &result, &devices);
EXPECT_EQ(blink::mojom::MediaStreamRequestResult::PERMISSION_DENIED, result);
EXPECT_FALSE(devices.video_device.has_value());
EXPECT_FALSE(devices.audio_device.has_value());
}
TEST_F(TabCaptureAccessHandlerTest, DlpNotRestricted) {
const content::DesktopMediaID source(
content::DesktopMediaID::TYPE_WEB_CONTENTS,
content::DesktopMediaID::kNullId,
content::WebContentsMediaCaptureId(
web_contents()->GetPrimaryMainFrame()->GetProcess()->GetID(),
web_contents()->GetPrimaryMainFrame()->GetRoutingID()));
policy::MockDlpContentManager mock_dlp_content_manager;
policy::ScopedDlpContentObserverForTesting scoped_dlp_content_manager(
&mock_dlp_content_manager);
EXPECT_CALL(mock_dlp_content_manager, CheckScreenShareRestriction)
.WillOnce([](const content::DesktopMediaID& media_id,
const std::u16string& application_title,
base::OnceCallback<void(bool)> callback) {
std::move(callback).Run(true);
});
extensions::TabCaptureRegistry::Get(profile())->AddRequest(
web_contents(), "", false,
GURL(kOrigin), source, process_id(), main_frame_id());
blink::mojom::MediaStreamRequestResult result = kInvalidResult;
blink::mojom::StreamDevices devices;
ProcessRequest(source, &result, &devices);
EXPECT_EQ(blink::mojom::MediaStreamRequestResult::OK, result);
EXPECT_TRUE(devices.video_device.has_value());
EXPECT_FALSE(devices.audio_device.has_value());
}
TEST_F(TabCaptureAccessHandlerTest, DlpWebContentsDestroyed) {
const content::DesktopMediaID source(
content::DesktopMediaID::TYPE_WEB_CONTENTS,
content::DesktopMediaID::kNullId,
content::WebContentsMediaCaptureId(
web_contents()->GetPrimaryMainFrame()->GetProcess()->GetID(),
web_contents()->GetPrimaryMainFrame()->GetRoutingID()));
policy::MockDlpContentManager mock_dlp_content_manager;
policy::ScopedDlpContentObserverForTesting scoped_dlp_content_manager(
&mock_dlp_content_manager);
EXPECT_CALL(mock_dlp_content_manager, CheckScreenShareRestriction)
.WillOnce([&](const content::DesktopMediaID& media_id,
const std::u16string& application_title,
base::OnceCallback<void(bool)> callback) {
DeleteContents();
std::move(callback).Run(true);
});
extensions::TabCaptureRegistry::Get(profile())->AddRequest(
web_contents(), "", false,
GURL(kOrigin), source, process_id(), main_frame_id());
blink::mojom::MediaStreamRequestResult result = kInvalidResult;
blink::mojom::StreamDevices devices;
ProcessRequest(source, &result, &devices, false);
EXPECT_EQ(kInvalidResult, result);
EXPECT_FALSE(devices.video_device.has_value());
EXPECT_FALSE(devices.audio_device.has_value());
}
#endif