Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: keyboard.lock() should use permissions helper #40369

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ win.webContents.session.setCertificateVerifyProc((request, callback) => {
* `midiSysex` - Request the use of system exclusive messages in the [Web MIDI API](https://developer.mozilla.org/en-US/docs/Web/API/Web_MIDI_API).
* `notifications` - Request notification creation and the ability to display them in the user's system tray using the [Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/notification)
* `pointerLock` - Request to directly interpret mouse movements as an input method via the [Pointer Lock API](https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API). These requests always appear to originate from the main frame.
* `keyboardLock` - Request capture of keypresses for any or all of the keys on the physical keyboard via the [Keyboard Lock API](https://developer.mozilla.org/en-US/docs/Web/API/Keyboard/lock). These requests always appear to originate from the main frame.
* `openExternal` - Request to open links in external applications.
* `window-management` - Request access to enumerate screens using the [`getScreenDetails`](https://developer.chrome.com/en/articles/multi-screen-window-placement/) API.
* `unknown` - An unrecognized permission request.
Expand Down
29 changes: 21 additions & 8 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1517,11 +1517,10 @@ 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) {
void WebContents::OnRequestToLockMouse(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);
Expand All @@ -1538,18 +1537,32 @@ void WebContents::RequestToLockMouse(content::WebContents* web_contents,
WebContentsPermissionHelper::FromWebContents(web_contents);
permission_helper->RequestPointerLockPermission(
user_gesture, last_unlocked_by_target,
base::BindOnce(&WebContents::RequestExclusivePointerAccess,
base::BindOnce(&WebContents::OnRequestToLockMouse,
base::Unretained(this)));
}

void WebContents::LostMouseLock() {
exclusive_access_manager_.mouse_lock_controller()->LostMouseLock();
}

void WebContents::OnRequestKeyboardLock(content::WebContents* web_contents,
bool esc_key_locked,
bool allowed) {
if (allowed) {
exclusive_access_manager_.keyboard_lock_controller()->RequestKeyboardLock(
web_contents, esc_key_locked);
} else {
web_contents->GotResponseToKeyboardLockRequest(false);
}
}

void WebContents::RequestKeyboardLock(content::WebContents* web_contents,
bool esc_key_locked) {
exclusive_access_manager_.keyboard_lock_controller()->RequestKeyboardLock(
web_contents, esc_key_locked);
auto* permission_helper =
WebContentsPermissionHelper::FromWebContents(web_contents);
permission_helper->RequestKeyboardLockPermission(
esc_key_locked, base::BindOnce(&WebContents::OnRequestKeyboardLock,
base::Unretained(this)));
}

void WebContents::CancelKeyboardLockRequest(
Expand Down
11 changes: 7 additions & 4 deletions shell/browser/api/electron_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,17 @@ 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 OnRequestToLockMouse(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 OnRequestKeyboardLock(content::WebContents* web_contents,
bool esc_key_locked,
bool allowed);
void RequestKeyboardLock(content::WebContents* web_contents,
bool esc_key_locked) override;
void CancelKeyboardLockRequest(content::WebContents* web_contents) override;
Expand Down
9 changes: 9 additions & 0 deletions shell/browser/web_contents_permission_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ void WebContentsPermissionHelper::RequestPointerLockPermission(
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,
Expand Down
6 changes: 5 additions & 1 deletion shell/browser/web_contents_permission_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class WebContentsPermissionHelper
OPEN_EXTERNAL,
SERIAL,
HID,
USB
USB,
KEYBOARD_LOCK
};

// Asynchronous Requests
Expand All @@ -44,6 +45,9 @@ class WebContentsPermissionHelper
bool last_unlocked_by_target,
base::OnceCallback<void(content::WebContents*, bool, bool, bool)>
callback);
void RequestKeyboardLockPermission(
bool esc_key_locked,
base::OnceCallback<void(content::WebContents*, bool, bool)> callback);
void RequestWebNotificationPermission(
content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback);
Expand Down
2 changes: 2 additions & 0 deletions shell/common/gin_converters/content_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ v8::Local<v8::Value> Converter<blink::PermissionType>::ToV8(
switch (static_cast<PermissionType>(val)) {
case PermissionType::POINTER_LOCK:
return StringToV8(isolate, "pointerLock");
case PermissionType::KEYBOARD_LOCK:
return StringToV8(isolate, "keyboardLock");
case PermissionType::FULLSCREEN:
return StringToV8(isolate, "fullscreen");
case PermissionType::OPEN_EXTERNAL:
Expand Down