#include "chrome/browser/media/webrtc/desktop_capture_access_handler.h"
#include <memory>
#include <string>
#include <vector>
#include "base/check.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/media/webrtc/capture_policy_utils.h"
#include "chrome/browser/media/webrtc/desktop_capture_devices_util.h"
#include "chrome/browser/media/webrtc/desktop_media_picker_controller.h"
#include "chrome/browser/media/webrtc/desktop_media_picker_factory_impl.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
#include "chrome/browser/media/webrtc/native_desktop_media_list.h"
#include "chrome/browser/media/webrtc/tab_desktop_media_list.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/screen_capture_notification_ui.h"
#include "chrome/browser/ui/simple_message_box.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/desktop_capture.h"
#include "content/public/browser/desktop_streams_registry.h"
#include "content/public/browser/media_stream_request.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/switches.h"
#include "services/network/public/cpp/is_potentially_trustworthy.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"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/origin.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/shell.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/chromeos/policy/dlp/dlp_content_manager.h"
#include "ui/base/ui_base_features.h"
#endif
#if BUILDFLAG(IS_MAC)
#include "chrome/browser/media/webrtc/system_media_capture_permissions_mac.h"
#endif
BrowserThread;
ManifestLocation;
namespace {
std::u16string GetApplicationTitle(content::WebContents* web_contents,
const extensions::Extension* extension) { … }
bool ShouldDisplayNotification(const extensions::Extension* extension) { … }
bool HasNotificationExemption(const GURL& url) { … }
#if !BUILDFLAG(IS_ANDROID)
gfx::NativeWindow FindParentWindowForWebContents(
content::WebContents* web_contents) { … }
#endif
bool IsMediaTypeAllowed(AllowedScreenCaptureLevel allowed_capture_level,
content::DesktopMediaID::Type media_type) { … }
bool ShouldCaptureAudio(const content::DesktopMediaID& media_id,
const content::MediaStreamRequest& request) { … }
bool IsRequestApproved(content::WebContents* web_contents,
const content::MediaStreamRequest& request,
const extensions::Extension* extension,
bool is_allowlisted_extension) { … }
}
DesktopCaptureAccessHandler::DesktopCaptureAccessHandler()
: … { … }
DesktopCaptureAccessHandler::DesktopCaptureAccessHandler(
std::unique_ptr<DesktopMediaPickerFactory> picker_factory)
: … { … }
DesktopCaptureAccessHandler::~DesktopCaptureAccessHandler() = default;
void DesktopCaptureAccessHandler::ProcessScreenCaptureAccessRequest(
content::WebContents* web_contents,
const extensions::Extension* extension,
std::unique_ptr<PendingAccessRequest> pending_request) { … }
bool DesktopCaptureAccessHandler::SupportsStreamType(
content::WebContents* web_contents,
const blink::mojom::MediaStreamType type,
const extensions::Extension* extension) { … }
bool DesktopCaptureAccessHandler::CheckMediaAccessPermission(
content::RenderFrameHost* render_frame_host,
const url::Origin& security_origin,
blink::mojom::MediaStreamType type,
const extensions::Extension* extension) { … }
void DesktopCaptureAccessHandler::HandleRequest(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback,
const extensions::Extension* extension) { … }
void DesktopCaptureAccessHandler::ProcessChangeSourceRequest(
content::WebContents* web_contents,
std::unique_ptr<PendingAccessRequest> pending_request) { … }
void DesktopCaptureAccessHandler::UpdateMediaRequestState(
int render_process_id,
int render_frame_id,
int page_request_id,
blink::mojom::MediaStreamType stream_type,
content::MediaRequestState state) { … }
void DesktopCaptureAccessHandler::ProcessQueuedAccessRequest(
const RequestsQueue& queue,
content::WebContents* web_contents) { … }
void DesktopCaptureAccessHandler::OnPickerDialogResults(
base::WeakPtr<content::WebContents> web_contents,
const std::u16string& application_title,
content::DesktopMediaID media_id) { … }
void DesktopCaptureAccessHandler::WebContentsDestroyed(
content::WebContents* web_contents) { … }
void DesktopCaptureAccessHandler::DeletePendingAccessRequest(
int render_process_id,
int render_frame_id,
int page_request_id) { … }
void DesktopCaptureAccessHandler::AcceptRequest(
content::WebContents* web_contents,
std::unique_ptr<PendingAccessRequest> pending_request,
const content::DesktopMediaID& media_id,
bool capture_audio) { … }
#if BUILDFLAG(IS_CHROMEOS)
void DesktopCaptureAccessHandler::OnDlpRestrictionChecked(
base::WeakPtr<content::WebContents> web_contents,
std::unique_ptr<PendingAccessRequest> pending_request,
const content::DesktopMediaID& media_id,
bool capture_audio,
bool is_dlp_allowed) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!web_contents) {
return;
}
if (!is_dlp_allowed) {
std::move(pending_request->callback)
.Run(blink::mojom::StreamDevicesSet(),
blink::mojom::MediaStreamRequestResult::PERMISSION_DENIED,
nullptr);
return;
}
AcceptRequest(web_contents.get(), std::move(pending_request), media_id,
capture_audio);
}
#endif