Skip to content

Commit

Permalink
fix: pass rfh instances through to the permission helper (#35985)
Browse files Browse the repository at this point in the history
fix: pass rfh instances through to the permission helper (#35419)

Co-authored-by: Samuel Attard <sam@electronjs.org>
  • Loading branch information
miniak and MarshallOfSound committed Oct 11, 2022
1 parent a03ec15 commit 080bc57
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/api/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ win.webContents.session.setCertificateVerifyProc((request, callback) => {
* `notifications` - Request notification creation and the ability to display them in the user's system tray.
* `midi` - Request MIDI access in the `webmidi` API.
* `midiSysex` - Request the use of system exclusive messages in the `webmidi` API.
* `pointerLock` - Request to directly interpret mouse movements as an input method. Click [here](https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API) to know more.
* `pointerLock` - Request to directly interpret mouse movements as an input method. Click [here](https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API) to know more. These requests always appear to originate from the main frame.
* `fullscreen` - Request for the app to enter fullscreen mode.
* `openExternal` - Request to open links in external applications.
* `unknown` - An unrecognized permission request
Expand Down
2 changes: 1 addition & 1 deletion shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ void WebContents::EnterFullscreenModeForTab(
auto callback =
base::BindRepeating(&WebContents::OnEnterFullscreenModeForTab,
base::Unretained(this), requesting_frame, options);
permission_helper->RequestFullscreenPermission(callback);
permission_helper->RequestFullscreenPermission(requesting_frame, callback);
}

void WebContents::OnEnterFullscreenModeForTab(
Expand Down
18 changes: 16 additions & 2 deletions shell/browser/electron_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "content/public/browser/tts_controller.h"
#include "content/public/browser/tts_platform.h"
#include "content/public/browser/url_loader_request_interceptor.h"
#include "content/public/browser/weak_document_ptr.h"
#include "content/public/common/content_descriptors.h"
#include "content/public/common/content_paths.h"
#include "content/public/common/content_switches.h"
Expand Down Expand Up @@ -987,7 +988,7 @@ void ElectronBrowserClient::WebNotificationAllowed(
return;
}
permission_helper->RequestWebNotificationPermission(
base::BindOnce(std::move(callback), web_contents->IsAudioMuted()));
rfh, base::BindOnce(std::move(callback), web_contents->IsAudioMuted()));
}

void ElectronBrowserClient::RenderProcessHostDestroyed(
Expand Down Expand Up @@ -1022,6 +1023,7 @@ void OnOpenExternal(const GURL& escaped_url, bool allowed) {

void HandleExternalProtocolInUI(
const GURL& url,
content::WeakDocumentPtr document_ptr,
content::WebContents::OnceGetter web_contents_getter,
bool has_user_gesture) {
content::WebContents* web_contents = std::move(web_contents_getter).Run();
Expand All @@ -1033,9 +1035,18 @@ void HandleExternalProtocolInUI(
if (!permission_helper)
return;

content::RenderFrameHost* rfh = document_ptr.AsRenderFrameHostIfValid();
if (!rfh) {
// If the render frame host is not valid it means it was a top level
// navigation and the frame has already been disposed of. In this case we
// take the current main frame and declare it responsible for the
// transition.
rfh = web_contents->GetPrimaryMainFrame();
}

GURL escaped_url(base::EscapeExternalHandlerValue(url.spec()));
auto callback = base::BindOnce(&OnOpenExternal, escaped_url);
permission_helper->RequestOpenExternalPermission(std::move(callback),
permission_helper->RequestOpenExternalPermission(rfh, std::move(callback),
has_user_gesture, url);
}

Expand All @@ -1055,6 +1066,9 @@ bool ElectronBrowserClient::HandleExternalProtocol(
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&HandleExternalProtocolInUI, url,
initiator_document
? initiator_document->GetWeakDocumentPtr()
: content::WeakDocumentPtr(),
std::move(web_contents_getter), has_user_gesture));
return true;
}
Expand Down
17 changes: 13 additions & 4 deletions shell/browser/web_contents_permission_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ WebContentsPermissionHelper::WebContentsPermissionHelper(
WebContentsPermissionHelper::~WebContentsPermissionHelper() = default;

void WebContentsPermissionHelper::RequestPermission(
content::RenderFrameHost* requesting_frame,
blink::PermissionType permission,
base::OnceCallback<void(bool)> callback,
bool user_gesture,
const base::DictionaryValue* details) {
auto* rfh = web_contents_->GetMainFrame();
auto* permission_manager = static_cast<ElectronPermissionManager*>(
web_contents_->GetBrowserContext()->GetPermissionControllerDelegate());
auto origin = web_contents_->GetLastCommittedURL();
permission_manager->RequestPermissionWithDetails(
permission, rfh, origin, false, details,
permission, requesting_frame, origin, false, details,
base::BindOnce(&OnPermissionResponse, std::move(callback)));
}

Expand All @@ -86,8 +86,10 @@ bool WebContentsPermissionHelper::CheckPermission(
}

void WebContentsPermissionHelper::RequestFullscreenPermission(
content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback) {
RequestPermission(
requesting_frame,
static_cast<blink::PermissionType>(PermissionType::FULLSCREEN),
std::move(callback));
}
Expand All @@ -113,13 +115,17 @@ void WebContentsPermissionHelper::RequestMediaAccessPermission(

// The permission type doesn't matter here, AUDIO_CAPTURE/VIDEO_CAPTURE
// are presented as same type in content_converter.h.
RequestPermission(blink::PermissionType::AUDIO_CAPTURE, std::move(callback),
RequestPermission(content::RenderFrameHost::FromID(request.render_process_id,
request.render_frame_id),
blink::PermissionType::AUDIO_CAPTURE, std::move(callback),
false, &details);
}

void WebContentsPermissionHelper::RequestWebNotificationPermission(
content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback) {
RequestPermission(blink::PermissionType::NOTIFICATIONS, std::move(callback));
RequestPermission(requesting_frame, blink::PermissionType::NOTIFICATIONS,
std::move(callback));
}

void WebContentsPermissionHelper::RequestPointerLockPermission(
Expand All @@ -128,19 +134,22 @@ void WebContentsPermissionHelper::RequestPointerLockPermission(
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::RequestOpenExternalPermission(
content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback,
bool user_gesture,
const GURL& url) {
base::DictionaryValue details;
details.SetString("externalURL", url.spec());
RequestPermission(
requesting_frame,
static_cast<blink::PermissionType>(PermissionType::OPEN_EXTERNAL),
std::move(callback), user_gesture, &details);
}
Expand Down
10 changes: 7 additions & 3 deletions shell/browser/web_contents_permission_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class WebContentsPermissionHelper
};

// Asynchronous Requests
void RequestFullscreenPermission(base::OnceCallback<void(bool)> callback);
void RequestFullscreenPermission(content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback);
void RequestMediaAccessPermission(const content::MediaStreamRequest& request,
content::MediaResponseCallback callback);
void RequestPointerLockPermission(
Expand All @@ -42,8 +43,10 @@ class WebContentsPermissionHelper
base::OnceCallback<void(content::WebContents*, bool, bool, bool)>
callback);
void RequestWebNotificationPermission(
content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback);
void RequestOpenExternalPermission(base::OnceCallback<void(bool)> callback,
void RequestOpenExternalPermission(content::RenderFrameHost* requesting_frame,
base::OnceCallback<void(bool)> callback,
bool user_gesture,
const GURL& url);

Expand All @@ -56,7 +59,8 @@ class WebContentsPermissionHelper
explicit WebContentsPermissionHelper(content::WebContents* web_contents);
friend class content::WebContentsUserData<WebContentsPermissionHelper>;

void RequestPermission(blink::PermissionType permission,
void RequestPermission(content::RenderFrameHost* requesting_frame,
blink::PermissionType permission,
base::OnceCallback<void(bool)> callback,
bool user_gesture = false,
const base::DictionaryValue* details = nullptr);
Expand Down

0 comments on commit 080bc57

Please sign in to comment.