1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/media/capture_access_handler_base.h"
#include <algorithm>
#include <string>
#include <utility>
#include "build/build_config.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/webui_url_constants.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/buildflags/buildflags.h"
#include "ui/gfx/native_widget_types.h"
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/common/extension.h"
#endif
#if defined(USE_AURA)
#include "ui/aura/window.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "base/hash/sha1.h"
#include "base/strings/string_number_conversions.h"
#endif // BUILDFLAG(IS_CHROMEOS)
using content::BrowserThread;
// Tracks MEDIA_DESKTOP/TAB_VIDEO_CAPTURE sessions. Sessions are removed when
// MEDIA_REQUEST_STATE_CLOSING is encountered.
struct CaptureAccessHandlerBase::Session {
Session(int request_process_id,
int request_frame_id,
int page_request_id,
bool is_trusted,
bool is_capturing_link_secure,
content::DesktopMediaID::Type capturing_type,
gfx::NativeWindow target_window)
: request_process_id(request_process_id),
request_frame_id(request_frame_id),
page_request_id(page_request_id),
is_trusted(is_trusted),
is_capturing_link_secure(is_capturing_link_secure),
capturing_type(capturing_type),
target_window(target_window) {
SetWebContents(request_process_id, request_frame_id);
}
void SetWebContents(int render_process_id, int render_frame_id) {
auto* web_contents = content::WebContents::FromRenderFrameHost(
content::RenderFrameHost::FromID(render_process_id, render_frame_id));
// Use the outermost WebContents in the WebContents tree, if possible.
// If we can't find the WebContents, clear |target_web_contents|.
target_web_contents =
web_contents ? web_contents->GetOutermostWebContents()->GetWeakPtr()
: nullptr;
}
content::WebContents* GetWebContents() const {
return target_web_contents.get();
}
int request_process_id;
int request_frame_id;
int page_request_id;
// Extensions control the routing of the captured MediaStream content.
// Therefore, only built-in extensions (and certain whitelisted ones) can be
// trusted to set-up secure links.
bool is_trusted;
// This is true only if all connected video sinks are reported secure.
bool is_capturing_link_secure;
// Reflects what this session is capturing. If the whole display is captured,
// then |capturing_type| is TYPE_SCREEN. If a specific window is specified,
// then |capturing_type| is TYPE_WINDOW and |target_window| is set. If a
// specific tab is the target, then |capturing_type| is TYPE_WEB_CONTENTS and
// |target_web_contents| is set.
content::DesktopMediaID::Type capturing_type;
gfx::NativeWindow target_window;
base::WeakPtr<content::WebContents> target_web_contents;
};
CaptureAccessHandlerBase::PendingAccessRequest::PendingAccessRequest(
std::unique_ptr<DesktopMediaPicker> picker,
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback,
std::u16string application_title,
bool should_display_notification,
bool is_allowlisted_extension)
: picker(std::move(picker)),
request(request),
callback(std::move(callback)),
application_title(std::move(application_title)),
should_display_notification(should_display_notification),
is_allowlisted_extension(is_allowlisted_extension) {}
CaptureAccessHandlerBase::PendingAccessRequest::~PendingAccessRequest() =
default;
CaptureAccessHandlerBase::CaptureAccessHandlerBase() = default;
CaptureAccessHandlerBase::~CaptureAccessHandlerBase() = default;
void CaptureAccessHandlerBase::AddCaptureSession(int render_process_id,
int render_frame_id,
int page_request_id,
bool is_trusted) {
Session session = {
render_process_id, render_frame_id, page_request_id, is_trusted, true,
// Assume that the target is the same tab that is
// requesting capture, not the display or any particular
// window. This can be changed by calling UpdateTarget().
content::DesktopMediaID::TYPE_WEB_CONTENTS, gfx::NativeWindow()};
sessions_.push_back(std::move(session));
}
void CaptureAccessHandlerBase::RemoveCaptureSession(int render_process_id,
int render_frame_id,
int page_request_id) {
auto it = FindSession(render_process_id, render_frame_id, page_request_id);
if (it != sessions_.end())
sessions_.erase(it);
}
std::list<CaptureAccessHandlerBase::Session>::iterator
CaptureAccessHandlerBase::FindSession(int render_process_id,
int render_frame_id,
int page_request_id) {
return std::ranges::find_if(
sessions_, [render_process_id, render_frame_id,
page_request_id](const Session& session) {
return session.request_process_id == render_process_id &&
session.request_frame_id == render_frame_id &&
session.page_request_id == page_request_id;
});
}
void CaptureAccessHandlerBase::UpdateMediaRequestState(
int render_process_id,
int render_frame_id,
int page_request_id,
blink::mojom::MediaStreamType stream_type,
content::MediaRequestState state) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
switch (stream_type) {
case blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE:
case blink::mojom::MediaStreamType::GUM_TAB_VIDEO_CAPTURE:
case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE:
case blink::mojom::MediaStreamType::DISPLAY_AUDIO_CAPTURE:
case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_THIS_TAB:
case blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_SET:
break;
default:
return;
}
if (state == content::MEDIA_REQUEST_STATE_DONE) {
if (FindSession(render_process_id, render_frame_id, page_request_id) ==
sessions_.end()) {
AddCaptureSession(render_process_id, render_frame_id, page_request_id,
false);
DVLOG(2) << "Add new session while UpdateMediaRequestState"
<< " render_process_id: " << render_process_id
<< " render_frame_id: " << render_frame_id
<< " page_request_id: " << page_request_id;
}
} else if (state == content::MEDIA_REQUEST_STATE_CLOSING) {
RemoveCaptureSession(render_process_id, render_frame_id, page_request_id);
DVLOG(2) << "Remove session: "
<< " render_process_id: " << render_process_id
<< " render_frame_id: " << render_frame_id
<< " page_request_id: " << page_request_id;
}
}
void CaptureAccessHandlerBase::UpdateExtensionTrusted(
const content::MediaStreamRequest& request,
bool is_allowlisted_extension) {
UpdateTrusted(request, is_allowlisted_extension ||
IsBuiltInFeedbackUI(request.security_origin));
}
void CaptureAccessHandlerBase::UpdateTrusted(
const content::MediaStreamRequest& request,
bool is_trusted) {
auto it = FindSession(request.render_process_id, request.render_frame_id,
request.page_request_id);
if (it != sessions_.end()) {
it->is_trusted = is_trusted;
DVLOG(2) << "CaptureAccessHandlerBase::UpdateTrusted"
<< " render_process_id: " << request.render_process_id
<< " render_frame_id: " << request.render_frame_id
<< " page_request_id: " << request.page_request_id
<< " is_trusted: " << is_trusted;
return;
}
AddCaptureSession(request.render_process_id, request.render_frame_id,
request.page_request_id, is_trusted);
DVLOG(2) << "Add new session while UpdateTrusted"
<< " render_process_id: " << request.render_process_id
<< " render_frame_id: " << request.render_frame_id
<< " page_request_id: " << request.page_request_id
<< " is_trusted: " << is_trusted;
}
void CaptureAccessHandlerBase::UpdateTarget(
const content::MediaStreamRequest& request,
const content::DesktopMediaID& target) {
DVLOG(2) << __func__ << " requestor: " << request.render_process_id << ":"
<< request.render_frame_id << ", target: " << target.ToString();
auto it = FindSession(request.render_process_id, request.render_frame_id,
request.page_request_id);
if (it == sessions_.end()) {
DLOG(WARNING) << "UpdateTarget() can not find the session.";
return;
}
// Update target fields.
it->capturing_type = target.type;
if (target.type == content::DesktopMediaID::TYPE_WINDOW) {
// If this is the Chrome window, then any tab in this window could be
// captured.
// TODO(crbug.com/41396679): Implement this for MacOS.
#if defined(USE_AURA)
it->target_window = content::DesktopMediaID::GetNativeWindowById(target);
#endif
} else if (target.type == content::DesktopMediaID::TYPE_WEB_CONTENTS) {
// Specific tab captured.
it->SetWebContents(target.web_contents_id.render_process_id,
target.web_contents_id.main_render_frame_id);
}
}
bool CaptureAccessHandlerBase::IsInsecureCapturingInProgress(
int render_process_id,
int render_frame_id) {
DVLOG(3) << __func__ << " checking for " << render_process_id << ":"
<< render_frame_id;
if (sessions_.empty())
return false;
// Check if the frame is being captured insecurely as the target of any
// session.
for (const Session& session : sessions_) {
if (MatchesSession(session, render_process_id, render_frame_id))
if (!session.is_trusted || !session.is_capturing_link_secure)
return true;
}
return false;
}
// static
bool CaptureAccessHandlerBase::MatchesSession(const Session& session,
int target_process_id,
int target_frame_id) {
switch (session.capturing_type) {
case content::DesktopMediaID::TYPE_NONE:
// Session has no target, so it doesn't match.
return false;
case content::DesktopMediaID::TYPE_SCREEN:
// Capturing the whole screen.
return true;
case content::DesktopMediaID::TYPE_WINDOW:
#if defined(USE_AURA)
{
// Check if the window hosting this frame is the target.
auto* host =
content::RenderFrameHost::FromID(target_process_id, target_frame_id);
if (!host)
return false;
gfx::NativeWindow window = host->GetNativeView()->GetRootWindow();
return window && window == session.target_window;
}
#else
// Unable to determine the window.
// TODO(crbug.com/41396679): Implement this for MacOS.
return false;
#endif // defined(USE_AURA)
case content::DesktopMediaID::TYPE_WEB_CONTENTS:
// Check that the target is the frame selected.
auto* target_web_contents = session.GetWebContents();
if (!target_web_contents)
return false;
auto* web_contents = content::WebContents::FromRenderFrameHost(
content::RenderFrameHost::FromID(target_process_id, target_frame_id));
if (!web_contents)
return false;
return target_web_contents == web_contents->GetOutermostWebContents();
}
NOTREACHED();
}
void CaptureAccessHandlerBase::UpdateVideoScreenCaptureStatus(
int render_process_id,
int render_frame_id,
int page_request_id,
bool is_secure) {
auto it = FindSession(render_process_id, render_frame_id, page_request_id);
if (it != sessions_.end()) {
it->is_capturing_link_secure = is_secure;
DVLOG(2) << "UpdateVideoScreenCaptureStatus:"
<< " render_process_id: " << render_process_id
<< " render_frame_id: " << render_frame_id
<< " page_request_id: " << page_request_id
<< " is_capturing_link_secure: " << is_secure;
}
}
#if BUILDFLAG(ENABLE_EXTENSIONS)
bool CaptureAccessHandlerBase::IsExtensionAllowedForScreenCapture(
const extensions::Extension* extension) {
if (!extension)
return false;
#if BUILDFLAG(IS_CHROMEOS)
std::string hash = base::SHA1HashString(extension->id());
std::string hex_hash = base::HexEncode(hash);
// crbug.com/446688
return hex_hash == "4F25792AF1AA7483936DE29C07806F203C7170A0" ||
hex_hash == "BD8781D757D830FC2E85470A1B6E8A718B7EE0D9" ||
hex_hash == "4AC2B6C63C6480D150DFDA13E4A5956EB1D0DDBB" ||
hex_hash == "81986D4F846CEDDDB962643FA501D1780DD441BB";
#else
return false;
#endif // BUILDFLAG(IS_CHROMEOS)
}
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
bool CaptureAccessHandlerBase::IsBuiltInFeedbackUI(const GURL& origin) {
return origin.spec() == chrome::kChromeUIFeedbackURL;
}
|