fix: pointer lock escape handling (#32369)

This commit is contained in:
Shelley Vohr 2022-02-09 10:40:50 +01:00 committed by GitHub
parent 841d223b3b
commit ac1d426c51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 25 deletions

View file

@ -1244,6 +1244,9 @@ bool WebContents::PlatformHandleKeyboardEvent(
content::KeyboardEventProcessingResult WebContents::PreHandleKeyboardEvent(
content::WebContents* source,
const content::NativeWebKeyboardEvent& event) {
if (exclusive_access_manager_->HandleUserKeyEvent(event))
return content::KeyboardEventProcessingResult::HANDLED;
if (event.GetType() == blink::WebInputEvent::Type::kRawKeyDown ||
event.GetType() == blink::WebInputEvent::Type::kKeyUp) {
bool prevent_default = Emit("before-input-event", event);
@ -1392,6 +1395,35 @@ void WebContents::FindReply(content::WebContents* web_contents,
Emit("found-in-page", result.GetHandle());
}
void WebContents::RequestExclusivePointerAccess(
content::WebContents* web_contents,
bool user_gesture,
bool last_unlocked_by_target,
bool allowed) {
if (allowed) {
exclusive_access_manager_->mouse_lock_controller()->RequestToLockMouse(
web_contents, user_gesture, last_unlocked_by_target);
} else {
web_contents->GotResponseToLockMouseRequest(
blink::mojom::PointerLockResult::kPermissionDenied);
}
}
void WebContents::RequestToLockMouse(content::WebContents* web_contents,
bool user_gesture,
bool last_unlocked_by_target) {
auto* permission_helper =
WebContentsPermissionHelper::FromWebContents(web_contents);
permission_helper->RequestPointerLockPermission(
user_gesture, last_unlocked_by_target,
base::BindOnce(&WebContents::RequestExclusivePointerAccess,
base::Unretained(this)));
}
void WebContents::LostMouseLock() {
exclusive_access_manager_->mouse_lock_controller()->LostMouseLock();
}
void WebContents::RequestKeyboardLock(content::WebContents* web_contents,
bool esc_key_locked) {
exclusive_access_manager_->keyboard_lock_controller()->RequestKeyboardLock(
@ -1424,14 +1456,6 @@ void WebContents::RequestMediaAccessPermission(
permission_helper->RequestMediaAccessPermission(request, std::move(callback));
}
void WebContents::RequestToLockMouse(content::WebContents* web_contents,
bool user_gesture,
bool last_unlocked_by_target) {
auto* permission_helper =
WebContentsPermissionHelper::FromWebContents(web_contents);
permission_helper->RequestPointerLockPermission(user_gesture);
}
content::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager(
content::WebContents* source) {
if (!dialog_manager_)

View file

@ -556,6 +556,14 @@ class WebContents : public ExclusiveAccessContext,
const gfx::Rect& selection_rect,
int active_match_ordinal,
bool final_update) override;
void RequestExclusivePointerAccess(content::WebContents* web_contents,
bool user_gesture,
bool last_unlocked_by_target,
bool allowed);
void RequestToLockMouse(content::WebContents* web_contents,
bool user_gesture,
bool last_unlocked_by_target) override;
void LostMouseLock() override;
void RequestKeyboardLock(content::WebContents* web_contents,
bool esc_key_locked) override;
void CancelKeyboardLockRequest(content::WebContents* web_contents) override;
@ -566,9 +574,6 @@ class WebContents : public ExclusiveAccessContext,
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback) override;
void RequestToLockMouse(content::WebContents* web_contents,
bool user_gesture,
bool last_unlocked_by_target) override;
content::JavaScriptDialogManager* GetJavaScriptDialogManager(
content::WebContents* source) override;
void OnAudioStateChanged(bool audible) override;

View file

@ -43,17 +43,6 @@ void MediaAccessAllowed(const content::MediaStreamRequest& request,
controller.Deny(blink::mojom::MediaStreamRequestResult::PERMISSION_DENIED);
}
void OnPointerLockResponse(content::WebContents* web_contents, bool allowed) {
if (web_contents) {
if (allowed)
web_contents->GotResponseToLockMouseRequest(
blink::mojom::PointerLockResult::kSuccess);
else
web_contents->GotResponseToLockMouseRequest(
blink::mojom::PointerLockResult::kPermissionDenied);
}
}
void OnPermissionResponse(base::OnceCallback<void(bool)> callback,
blink::mojom::PermissionStatus status) {
if (status == blink::mojom::PermissionStatus::GRANTED)
@ -157,10 +146,15 @@ void WebContentsPermissionHelper::RequestWebNotificationPermission(
}
void WebContentsPermissionHelper::RequestPointerLockPermission(
bool user_gesture) {
bool user_gesture,
bool last_unlocked_by_target,
base::OnceCallback<void(content::WebContents*, bool, bool, bool)>
callback) {
RequestPermission(
static_cast<content::PermissionType>(PermissionType::POINTER_LOCK),
base::BindOnce(&OnPointerLockResponse, web_contents_), user_gesture);
base::BindOnce(std::move(callback), web_contents_, user_gesture,
last_unlocked_by_target),
user_gesture);
}
void WebContentsPermissionHelper::RequestOpenExternalPermission(

View file

@ -36,9 +36,13 @@ class WebContentsPermissionHelper
void RequestFullscreenPermission(base::OnceCallback<void(bool)> callback);
void RequestMediaAccessPermission(const content::MediaStreamRequest& request,
content::MediaResponseCallback callback);
void RequestPointerLockPermission(
bool user_gesture,
bool last_unlocked_by_target,
base::OnceCallback<void(content::WebContents*, bool, bool, bool)>
callback);
void RequestWebNotificationPermission(
base::OnceCallback<void(bool)> callback);
void RequestPointerLockPermission(bool user_gesture);
void RequestOpenExternalPermission(base::OnceCallback<void(bool)> callback,
bool user_gesture,
const GURL& url);