electron/shell/browser/web_contents_permission_helper.cc
electron-roller[bot] 0dad99561b
chore: bump chromium to 123.0.6296.0 (main) (#41204)
* chore: bump chromium in DEPS to 123.0.6273.0

* chore: update patches

* chore: bump chromium in DEPS to 123.0.6274.0

* chore: update patches

* chore: bump chromium in DEPS to 123.0.6276.0

* chore: update patches

* WIP: 5239586: Change View::Layout() to take a PassKey.
https://chromium-review.googlesource.com/c/chromium/src/+/5239586

* WIP: 5239586: Change View::Layout() to take a PassKey.
https://chromium-review.googlesource.com/c/chromium/src/+/5239586

* chore: bump chromium in DEPS to 123.0.6278.0

* chore: bump chromium in DEPS to 123.0.6280.0

* chore: update patches

* chore: use net::CanonicalCookie::SecureAttribute()

renamed from IsSecure()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5245913

* refactor: handle multiple requested device ids

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5132210

* refactor: trigger View layouts async with View::InvalidateLayout()

Upstream has introduced a PassKey to restrict who can call Layout()
directly. I've opted for calling `InvalidateLayout()` which is the
approach that upstream recommends.

If for some reason this approach doesn't work for us, we could use
`DeprecatedLayoutImmediately()` as a stopgap.

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5239586

Xref: https://chromium.googlesource.com/chromium/src/+/main/ui/views/view.h#809

Xref: https://chromium.googlesource.com/chromium/src/+/main/docs/ui/learn/bestpractices/layout.md?pli=1#don_t-invoke-layout_directly

* chore: bump chromium in DEPS to 123.0.6282.0

* chore: bump chromium in DEPS to 123.0.6284.0

* chore: update patches

* refactor: remove use of blink::MainThreadIsolate() pt 1/3

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5249640

* refactor: remove use of blink::MainThreadIsolate() pt 2/3

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5249640

* refactor: remove use of blink::MainThreadIsolate() pt 3/3

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5249640

* chore: update enum name to ui::AXMode::kPDFPrinting

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5270301

* chore: rebuild filenames.libcxx.gni

* chore: sync with upstream rename of PortProvider.TaskForHandle()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5259103

* chore: bump chromium in DEPS to 123.0.6286.0

* chore: bump chromium in DEPS to 123.0.6288.0

* WebPreferences: Initialize in declaration.

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5277099

* chore: update webview_fullscreen.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5053508

Simple update to fix patch shear

* chore: update feat_configure_launch_options_for_service_process.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5254861

Simple update to fix patch shear

* chore: add IWC::Delegate::RecordResize() stub to fix FTBFS

https://chromium-review.googlesource.com/c/chromium/src/+/5268963

* chore: add FormControlType::kButtonPopover to the FormControlType converter

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5230929

* chore: e patches all

* chore: node script/gen-libc++-filenames.js

* chore: bump chromium in DEPS to 123.0.6290.0

* chore: bump chromium in DEPS to 123.0.6291.0

* chore: bump chromium in DEPS to 123.0.6292.0

* chore: bump chromium in DEPS to 123.0.6294.0

* chore: update fix_aspect_ratio_with_max_size.patch

Xref: fix_aspect_ratio_with_max_size.patch

note: simple absl::optional -> std::optional conversion

* chore: update feat_filter_out_non-shareable_windows_in_the_current_application_in.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5272337

* chore: update add_maximized_parameter_to_linuxui_getwindowframeprovider.patch

No manual changes; just adjusting line patch offsets

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5258688

* chore: update feat_configure_launch_options_for_service_process.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5281322

* chore: update fix_select_The_first_menu_item_when_opened_via_keyboard.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5279376

note: simple absl::optional -> std::optional conversion

* chore: update feat_allow_code_cache_in_custom_schemes.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5268792

* chore: script/export_all_patches.py

* chore: bump chromium in DEPS to 123.0.6296.0

* chore: update patches

* fixup! chore: update feat_allow_code_cache_in_custom_schemes.patch

* fix: restore MessagePort close event

* spec: fix CORB testing

Refs https://chromium-review.googlesource.com/c/chromium/src/+/5231506

* fix: use sync layout when content view changes

* fixup! chore: update feat_configure_launch_options_for_service_process.patch

* Add remote-cocoa support for context menus.

Refs https://chromium-review.googlesource.com/c/chromium/src/+/5259806

* Rename //net/base/mac directory to //net/base/apple (1/n)

Refs https://chromium-review.googlesource.com/c/chromium/src/+/5211389

* fixup! Add remote-cocoa support for context menus.

* [Clipboard] Don't add meta charset tag for async write() method on Mac.

Refs https://chromium-review.googlesource.com/c/chromium/src/+/5187335

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: clavin <clavin@electronjs.org>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
2024-02-14 12:33:32 -05:00

303 lines
12 KiB
C++

// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/web_contents_permission_helper.h"
#include <string_view>
#include <utility>
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents_user_data.h"
#include "shell/browser/electron_permission_manager.h"
// #include "shell/browser/media/media_stream_devices_controller.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/webrtc/media_stream_devices_controller.h"
#include "shell/browser/media/media_capture_devices_dispatcher.h"
namespace {
constexpr std::string_view MediaStreamTypeToString(
blink::mojom::MediaStreamType type) {
switch (type) {
case blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE:
return "audio";
case blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE:
return "video";
default:
return "unknown";
}
}
} // namespace
namespace electron {
namespace {
[[nodiscard]] content::DesktopMediaID GetScreenId(
const std::vector<std::string>& requested_video_device_ids) {
if (!requested_video_device_ids.empty() &&
!requested_video_device_ids.front().empty())
return content::DesktopMediaID::Parse(requested_video_device_ids.front());
// If the device id wasn't specified then this is a screen capture request
// (i.e. chooseDesktopMedia() API wasn't used to generate device id).
return content::DesktopMediaID(content::DesktopMediaID::TYPE_SCREEN,
-1 /* kFullDesktopScreenId */);
}
// Handles requests for legacy-style `navigator.getUserMedia(...)` calls.
// This includes desktop capture through the chromeMediaSource /
// chromeMediaSourceId constraints.
void HandleUserMediaRequest(const content::MediaStreamRequest& request,
content::MediaResponseCallback callback) {
blink::mojom::StreamDevicesSetPtr stream_devices_set =
blink::mojom::StreamDevicesSet::New();
stream_devices_set->stream_devices.emplace_back(
blink::mojom::StreamDevices::New());
blink::mojom::StreamDevices& devices = *stream_devices_set->stream_devices[0];
if (request.audio_type ==
blink::mojom::MediaStreamType::GUM_TAB_AUDIO_CAPTURE) {
devices.audio_device = blink::MediaStreamDevice(
blink::mojom::MediaStreamType::GUM_TAB_AUDIO_CAPTURE, "", "");
}
if (request.video_type ==
blink::mojom::MediaStreamType::GUM_TAB_VIDEO_CAPTURE) {
devices.video_device = blink::MediaStreamDevice(
blink::mojom::MediaStreamType::GUM_TAB_VIDEO_CAPTURE, "", "");
}
if (request.audio_type ==
blink::mojom::MediaStreamType::GUM_DESKTOP_AUDIO_CAPTURE) {
devices.audio_device = blink::MediaStreamDevice(
blink::mojom::MediaStreamType::GUM_DESKTOP_AUDIO_CAPTURE, "loopback",
"System Audio");
}
if (request.video_type ==
blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE) {
devices.video_device = blink::MediaStreamDevice(
blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE,
GetScreenId(request.requested_video_device_ids).ToString(), "Screen");
}
bool empty =
!devices.audio_device.has_value() && !devices.video_device.has_value();
std::move(callback).Run(
*stream_devices_set,
empty ? blink::mojom::MediaStreamRequestResult::NO_HARDWARE
: blink::mojom::MediaStreamRequestResult::OK,
nullptr);
}
void OnMediaStreamRequestResponse(
content::MediaResponseCallback callback,
const blink::mojom::StreamDevicesSet& stream_devices_set,
blink::mojom::MediaStreamRequestResult result,
bool blocked_by_permissions_policy,
ContentSetting audio_setting,
ContentSetting video_setting) {
std::move(callback).Run(stream_devices_set, result, nullptr);
}
void MediaAccessAllowed(const content::MediaStreamRequest& request,
content::MediaResponseCallback callback,
bool allowed) {
if (allowed) {
if (request.video_type ==
blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE ||
request.audio_type ==
blink::mojom::MediaStreamType::GUM_DESKTOP_AUDIO_CAPTURE ||
request.video_type ==
blink::mojom::MediaStreamType::GUM_TAB_VIDEO_CAPTURE ||
request.audio_type ==
blink::mojom::MediaStreamType::GUM_TAB_AUDIO_CAPTURE) {
HandleUserMediaRequest(request, std::move(callback));
} else if (request.video_type ==
blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE ||
request.audio_type ==
blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE) {
webrtc::MediaStreamDevicesController::RequestPermissions(
request, MediaCaptureDevicesDispatcher::GetInstance(),
base::BindOnce(&OnMediaStreamRequestResponse, std::move(callback)),
allowed);
} else if (request.video_type ==
blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE ||
request.video_type == blink::mojom::MediaStreamType::
DISPLAY_VIDEO_CAPTURE_THIS_TAB ||
request.video_type ==
blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE_SET ||
request.audio_type ==
blink::mojom::MediaStreamType::DISPLAY_AUDIO_CAPTURE) {
content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
request.render_process_id, request.render_frame_id);
if (!rfh)
return;
content::BrowserContext* browser_context = rfh->GetBrowserContext();
ElectronBrowserContext* electron_browser_context =
static_cast<ElectronBrowserContext*>(browser_context);
auto split_callback = base::SplitOnceCallback(std::move(callback));
if (electron_browser_context->ChooseDisplayMediaDevice(
request, std::move(split_callback.second)))
return;
std::move(split_callback.first)
.Run(blink::mojom::StreamDevicesSet(),
blink::mojom::MediaStreamRequestResult::NOT_SUPPORTED, nullptr);
} else {
std::move(callback).Run(
blink::mojom::StreamDevicesSet(),
blink::mojom::MediaStreamRequestResult::NOT_SUPPORTED, nullptr);
}
} else {
std::move(callback).Run(
blink::mojom::StreamDevicesSet(),
blink::mojom::MediaStreamRequestResult::PERMISSION_DENIED, nullptr);
}
}
void OnPermissionResponse(base::OnceCallback<void(bool)> callback,
blink::mojom::PermissionStatus status) {
if (status == blink::mojom::PermissionStatus::GRANTED)
std::move(callback).Run(true);
else
std::move(callback).Run(false);
}
} // namespace
WebContentsPermissionHelper::WebContentsPermissionHelper(
content::WebContents* web_contents)
: content::WebContentsUserData<WebContentsPermissionHelper>(*web_contents),
web_contents_(web_contents) {}
WebContentsPermissionHelper::~WebContentsPermissionHelper() = default;
void WebContentsPermissionHelper::RequestPermission(
content::RenderFrameHost* requesting_frame,
blink::PermissionType permission,
base::OnceCallback<void(bool)> callback,
bool user_gesture,
base::Value::Dict details) {
auto* permission_manager = static_cast<ElectronPermissionManager*>(
web_contents_->GetBrowserContext()->GetPermissionControllerDelegate());
auto origin = web_contents_->GetLastCommittedURL();
permission_manager->RequestPermissionWithDetails(
permission, requesting_frame, origin, false, std::move(details),
base::BindOnce(&OnPermissionResponse, std::move(callback)));
}
bool WebContentsPermissionHelper::CheckPermission(
blink::PermissionType permission,
base::Value::Dict details) const {
auto* rfh = web_contents_->GetPrimaryMainFrame();
auto* permission_manager = static_cast<ElectronPermissionManager*>(
web_contents_->GetBrowserContext()->GetPermissionControllerDelegate());
auto origin = web_contents_->GetLastCommittedURL();
return permission_manager->CheckPermissionWithDetails(permission, rfh, origin,
std::move(details));
}
void WebContentsPermissionHelper::RequestFullscreenPermission(
content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback) {
RequestPermission(
requesting_frame,
static_cast<blink::PermissionType>(PermissionType::FULLSCREEN),
std::move(callback));
}
void WebContentsPermissionHelper::RequestMediaAccessPermission(
const content::MediaStreamRequest& request,
content::MediaResponseCallback response_callback) {
auto callback = base::BindOnce(&MediaAccessAllowed, request,
std::move(response_callback));
base::Value::Dict details;
base::Value::List media_types;
if (request.audio_type ==
blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE) {
media_types.Append("audio");
}
if (request.video_type ==
blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE) {
media_types.Append("video");
}
details.Set("mediaTypes", std::move(media_types));
details.Set("securityOrigin", request.security_origin.spec());
// The permission type doesn't matter here, AUDIO_CAPTURE/VIDEO_CAPTURE
// are presented as same type in content_converter.h.
RequestPermission(content::RenderFrameHost::FromID(request.render_process_id,
request.render_frame_id),
blink::PermissionType::AUDIO_CAPTURE, std::move(callback),
false, std::move(details));
}
void WebContentsPermissionHelper::RequestWebNotificationPermission(
content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback) {
RequestPermission(requesting_frame, blink::PermissionType::NOTIFICATIONS,
std::move(callback));
}
void WebContentsPermissionHelper::RequestPointerLockPermission(
bool user_gesture,
bool last_unlocked_by_target,
base::OnceCallback<void(content::WebContents*, bool, bool, bool)>
callback) {
RequestPermission(
web_contents_->GetPrimaryMainFrame(),
static_cast<blink::PermissionType>(PermissionType::POINTER_LOCK),
base::BindOnce(std::move(callback), web_contents_, user_gesture,
last_unlocked_by_target),
user_gesture);
}
void WebContentsPermissionHelper::RequestKeyboardLockPermission(
bool esc_key_locked,
base::OnceCallback<void(content::WebContents*, bool, bool)> callback) {
RequestPermission(
web_contents_->GetPrimaryMainFrame(),
static_cast<blink::PermissionType>(PermissionType::KEYBOARD_LOCK),
base::BindOnce(std::move(callback), web_contents_, esc_key_locked));
}
void WebContentsPermissionHelper::RequestOpenExternalPermission(
content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback,
bool user_gesture,
const GURL& url) {
base::Value::Dict details;
details.Set("externalURL", url.spec());
RequestPermission(
requesting_frame,
static_cast<blink::PermissionType>(PermissionType::OPEN_EXTERNAL),
std::move(callback), user_gesture, std::move(details));
}
bool WebContentsPermissionHelper::CheckMediaAccessPermission(
const url::Origin& security_origin,
blink::mojom::MediaStreamType type) const {
base::Value::Dict details;
details.Set("securityOrigin", security_origin.GetURL().spec());
details.Set("mediaType", MediaStreamTypeToString(type));
// The permission type doesn't matter here, AUDIO_CAPTURE/VIDEO_CAPTURE
// are presented as same type in content_converter.h.
return CheckPermission(blink::PermissionType::AUDIO_CAPTURE,
std::move(details));
}
bool WebContentsPermissionHelper::CheckSerialAccessPermission(
const url::Origin& embedding_origin) const {
base::Value::Dict details;
details.Set("securityOrigin", embedding_origin.GetURL().spec());
return CheckPermission(
static_cast<blink::PermissionType>(PermissionType::SERIAL),
std::move(details));
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsPermissionHelper);
} // namespace electron