chore: modernize ListValue usage in permission manager (#34662)

This commit is contained in:
Jeremy Rose 2022-06-29 10:09:48 -07:00 committed by GitHub
parent 4ddd03b1b3
commit 0d4e417594
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 59 deletions

View file

@ -137,8 +137,7 @@ void ElectronPermissionManager::RequestPermission(
bool user_gesture, bool user_gesture,
StatusCallback response_callback) { StatusCallback response_callback) {
RequestPermissionWithDetails(permission, render_frame_host, requesting_origin, RequestPermissionWithDetails(permission, render_frame_host, requesting_origin,
user_gesture, nullptr, user_gesture, {}, std::move(response_callback));
std::move(response_callback));
} }
void ElectronPermissionManager::RequestPermissionWithDetails( void ElectronPermissionManager::RequestPermissionWithDetails(
@ -146,11 +145,11 @@ void ElectronPermissionManager::RequestPermissionWithDetails(
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin, const GURL& requesting_origin,
bool user_gesture, bool user_gesture,
const base::DictionaryValue* details, base::Value::Dict details,
StatusCallback response_callback) { StatusCallback response_callback) {
RequestPermissionsWithDetails( RequestPermissionsWithDetails(
std::vector<blink::PermissionType>(1, permission), render_frame_host, std::vector<blink::PermissionType>(1, permission), render_frame_host,
user_gesture, details, user_gesture, std::move(details),
base::BindOnce(PermissionRequestResponseCallbackWrapper, base::BindOnce(PermissionRequestResponseCallbackWrapper,
std::move(response_callback))); std::move(response_callback)));
} }
@ -162,14 +161,14 @@ void ElectronPermissionManager::RequestPermissions(
bool user_gesture, bool user_gesture,
StatusesCallback response_callback) { StatusesCallback response_callback) {
RequestPermissionsWithDetails(permissions, render_frame_host, user_gesture, RequestPermissionsWithDetails(permissions, render_frame_host, user_gesture,
nullptr, std::move(response_callback)); {}, std::move(response_callback));
} }
void ElectronPermissionManager::RequestPermissionsWithDetails( void ElectronPermissionManager::RequestPermissionsWithDetails(
const std::vector<blink::PermissionType>& permissions, const std::vector<blink::PermissionType>& permissions,
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
bool user_gesture, bool user_gesture,
const base::DictionaryValue* details, base::Value::Dict details,
StatusesCallback response_callback) { StatusesCallback response_callback) {
if (permissions.empty()) { if (permissions.empty()) {
std::move(response_callback).Run({}); std::move(response_callback).Run({});
@ -204,13 +203,11 @@ void ElectronPermissionManager::RequestPermissionsWithDetails(
const auto callback = const auto callback =
base::BindRepeating(&ElectronPermissionManager::OnPermissionResponse, base::BindRepeating(&ElectronPermissionManager::OnPermissionResponse,
base::Unretained(this), request_id, i); base::Unretained(this), request_id, i);
auto mutable_details = details.Set("requestingUrl",
details == nullptr ? base::DictionaryValue() : details->Clone(); render_frame_host->GetLastCommittedURL().spec());
mutable_details.SetStringKey( details.Set("isMainFrame", render_frame_host->GetParent() == nullptr);
"requestingUrl", render_frame_host->GetLastCommittedURL().spec()); request_handler_.Run(web_contents, permission, callback,
mutable_details.SetBoolKey("isMainFrame", base::Value(std::move(details)));
render_frame_host->GetParent() == nullptr);
request_handler_.Run(web_contents, permission, callback, mutable_details);
} }
} }
@ -241,17 +238,17 @@ void ElectronPermissionManager::RequestPermissionsFromCurrentDocument(
base::OnceCallback<void(const std::vector<blink::mojom::PermissionStatus>&)> base::OnceCallback<void(const std::vector<blink::mojom::PermissionStatus>&)>
callback) { callback) {
RequestPermissionsWithDetails(permissions, render_frame_host, user_gesture, RequestPermissionsWithDetails(permissions, render_frame_host, user_gesture,
nullptr, std::move(callback)); {}, std::move(callback));
} }
blink::mojom::PermissionStatus ElectronPermissionManager::GetPermissionStatus( blink::mojom::PermissionStatus ElectronPermissionManager::GetPermissionStatus(
blink::PermissionType permission, blink::PermissionType permission,
const GURL& requesting_origin, const GURL& requesting_origin,
const GURL& embedding_origin) { const GURL& embedding_origin) {
base::DictionaryValue details; base::Value::Dict details;
details.SetString("embeddingOrigin", embedding_origin.spec()); details.Set("embeddingOrigin", embedding_origin.spec());
bool granted = CheckPermissionWithDetails(permission, nullptr, bool granted = CheckPermissionWithDetails(permission, {}, requesting_origin,
requesting_origin, &details); std::move(details));
return granted ? blink::mojom::PermissionStatus::GRANTED return granted ? blink::mojom::PermissionStatus::GRANTED
: blink::mojom::PermissionStatus::DENIED; : blink::mojom::PermissionStatus::DENIED;
} }
@ -273,7 +270,7 @@ bool ElectronPermissionManager::CheckPermissionWithDetails(
blink::PermissionType permission, blink::PermissionType permission,
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin, const GURL& requesting_origin,
const base::DictionaryValue* details) const { base::Value::Dict details) const {
if (check_handler_.is_null()) { if (check_handler_.is_null()) {
return true; return true;
} }
@ -281,27 +278,24 @@ bool ElectronPermissionManager::CheckPermissionWithDetails(
render_frame_host render_frame_host
? content::WebContents::FromRenderFrameHost(render_frame_host) ? content::WebContents::FromRenderFrameHost(render_frame_host)
: nullptr; : nullptr;
auto mutable_details =
details == nullptr ? base::DictionaryValue() : details->Clone();
if (render_frame_host) { if (render_frame_host) {
mutable_details.SetStringKey( details.Set("requestingUrl",
"requestingUrl", render_frame_host->GetLastCommittedURL().spec()); render_frame_host->GetLastCommittedURL().spec());
} }
mutable_details.SetBoolKey( details.Set("isMainFrame",
"isMainFrame", render_frame_host && render_frame_host->GetParent() == nullptr);
render_frame_host && render_frame_host->GetParent() == nullptr);
switch (permission) { switch (permission) {
case blink::PermissionType::AUDIO_CAPTURE: case blink::PermissionType::AUDIO_CAPTURE:
mutable_details.SetStringKey("mediaType", "audio"); details.Set("mediaType", "audio");
break; break;
case blink::PermissionType::VIDEO_CAPTURE: case blink::PermissionType::VIDEO_CAPTURE:
mutable_details.SetStringKey("mediaType", "video"); details.Set("mediaType", "video");
break; break;
default: default:
break; break;
} }
return check_handler_.Run(web_contents, permission, requesting_origin, return check_handler_.Run(web_contents, permission, requesting_origin,
mutable_details); base::Value(std::move(details)));
} }
bool ElectronPermissionManager::CheckDevicePermission( bool ElectronPermissionManager::CheckDevicePermission(

View file

@ -15,7 +15,6 @@
#include "shell/browser/electron_browser_context.h" #include "shell/browser/electron_browser_context.h"
namespace base { namespace base {
class DictionaryValue;
class Value; class Value;
} // namespace base } // namespace base
@ -67,7 +66,7 @@ class ElectronPermissionManager : public content::PermissionControllerDelegate {
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin, const GURL& requesting_origin,
bool user_gesture, bool user_gesture,
const base::DictionaryValue* details, base::Value::Dict details,
StatusCallback callback); StatusCallback callback);
void RequestPermissions(const std::vector<blink::PermissionType>& permissions, void RequestPermissions(const std::vector<blink::PermissionType>& permissions,
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
@ -78,7 +77,7 @@ class ElectronPermissionManager : public content::PermissionControllerDelegate {
const std::vector<blink::PermissionType>& permissions, const std::vector<blink::PermissionType>& permissions,
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
bool user_gesture, bool user_gesture,
const base::DictionaryValue* details, base::Value::Dict details,
StatusesCallback callback); StatusesCallback callback);
blink::mojom::PermissionStatus GetPermissionStatusForCurrentDocument( blink::mojom::PermissionStatus GetPermissionStatusForCurrentDocument(
@ -88,7 +87,7 @@ class ElectronPermissionManager : public content::PermissionControllerDelegate {
bool CheckPermissionWithDetails(blink::PermissionType permission, bool CheckPermissionWithDetails(blink::PermissionType permission,
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin, const GURL& requesting_origin,
const base::DictionaryValue* details) const; base::Value::Dict details) const;
bool CheckDevicePermission(blink::PermissionType permission, bool CheckDevicePermission(blink::PermissionType permission,
const url::Origin& origin, const url::Origin& origin,

View file

@ -61,14 +61,14 @@ std::unique_ptr<content::HidChooser> ElectronHidDelegate::RunChooser(
bool ElectronHidDelegate::CanRequestDevicePermission( bool ElectronHidDelegate::CanRequestDevicePermission(
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
const url::Origin& origin) { const url::Origin& origin) {
base::DictionaryValue details; base::Value::Dict details;
details.SetString("securityOrigin", origin.GetURL().spec()); details.Set("securityOrigin", origin.GetURL().spec());
auto* permission_manager = static_cast<ElectronPermissionManager*>( auto* permission_manager = static_cast<ElectronPermissionManager*>(
browser_context->GetPermissionControllerDelegate()); browser_context->GetPermissionControllerDelegate());
return permission_manager->CheckPermissionWithDetails( return permission_manager->CheckPermissionWithDetails(
static_cast<blink::PermissionType>( static_cast<blink::PermissionType>(
WebContentsPermissionHelper::PermissionType::HID), WebContentsPermissionHelper::PermissionType::HID),
nullptr, origin.GetURL(), &details); nullptr, origin.GetURL(), std::move(details));
} }
bool ElectronHidDelegate::HasDevicePermission( bool ElectronHidDelegate::HasDevicePermission(

View file

@ -64,25 +64,25 @@ void WebContentsPermissionHelper::RequestPermission(
blink::PermissionType permission, blink::PermissionType permission,
base::OnceCallback<void(bool)> callback, base::OnceCallback<void(bool)> callback,
bool user_gesture, bool user_gesture,
const base::DictionaryValue* details) { base::Value::Dict details) {
auto* rfh = web_contents_->GetPrimaryMainFrame(); auto* rfh = web_contents_->GetPrimaryMainFrame();
auto* permission_manager = static_cast<ElectronPermissionManager*>( auto* permission_manager = static_cast<ElectronPermissionManager*>(
web_contents_->GetBrowserContext()->GetPermissionControllerDelegate()); web_contents_->GetBrowserContext()->GetPermissionControllerDelegate());
auto origin = web_contents_->GetLastCommittedURL(); auto origin = web_contents_->GetLastCommittedURL();
permission_manager->RequestPermissionWithDetails( permission_manager->RequestPermissionWithDetails(
permission, rfh, origin, false, details, permission, rfh, origin, false, std::move(details),
base::BindOnce(&OnPermissionResponse, std::move(callback))); base::BindOnce(&OnPermissionResponse, std::move(callback)));
} }
bool WebContentsPermissionHelper::CheckPermission( bool WebContentsPermissionHelper::CheckPermission(
blink::PermissionType permission, blink::PermissionType permission,
const base::DictionaryValue* details) const { base::Value::Dict details) const {
auto* rfh = web_contents_->GetPrimaryMainFrame(); auto* rfh = web_contents_->GetPrimaryMainFrame();
auto* permission_manager = static_cast<ElectronPermissionManager*>( auto* permission_manager = static_cast<ElectronPermissionManager*>(
web_contents_->GetBrowserContext()->GetPermissionControllerDelegate()); web_contents_->GetBrowserContext()->GetPermissionControllerDelegate());
auto origin = web_contents_->GetLastCommittedURL(); auto origin = web_contents_->GetLastCommittedURL();
return permission_manager->CheckPermissionWithDetails(permission, rfh, origin, return permission_manager->CheckPermissionWithDetails(permission, rfh, origin,
details); std::move(details));
} }
void WebContentsPermissionHelper::RequestFullscreenPermission( void WebContentsPermissionHelper::RequestFullscreenPermission(
@ -98,23 +98,23 @@ void WebContentsPermissionHelper::RequestMediaAccessPermission(
auto callback = base::BindOnce(&MediaAccessAllowed, request, auto callback = base::BindOnce(&MediaAccessAllowed, request,
std::move(response_callback)); std::move(response_callback));
base::DictionaryValue details; base::Value::Dict details;
auto media_types = std::make_unique<base::ListValue>(); base::Value::List media_types;
if (request.audio_type == if (request.audio_type ==
blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE) { blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE) {
media_types->Append("audio"); media_types.Append("audio");
} }
if (request.video_type == if (request.video_type ==
blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE) { blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE) {
media_types->Append("video"); media_types.Append("video");
} }
details.SetList("mediaTypes", std::move(media_types)); details.Set("mediaTypes", std::move(media_types));
details.SetString("securityOrigin", request.security_origin.spec()); details.Set("securityOrigin", request.security_origin.spec());
// The permission type doesn't matter here, AUDIO_CAPTURE/VIDEO_CAPTURE // The permission type doesn't matter here, AUDIO_CAPTURE/VIDEO_CAPTURE
// are presented as same type in content_converter.h. // are presented as same type in content_converter.h.
RequestPermission(blink::PermissionType::AUDIO_CAPTURE, std::move(callback), RequestPermission(blink::PermissionType::AUDIO_CAPTURE, std::move(callback),
false, &details); false, std::move(details));
} }
void WebContentsPermissionHelper::RequestWebNotificationPermission( void WebContentsPermissionHelper::RequestWebNotificationPermission(
@ -138,30 +138,32 @@ void WebContentsPermissionHelper::RequestOpenExternalPermission(
base::OnceCallback<void(bool)> callback, base::OnceCallback<void(bool)> callback,
bool user_gesture, bool user_gesture,
const GURL& url) { const GURL& url) {
base::DictionaryValue details; base::Value::Dict details;
details.SetString("externalURL", url.spec()); details.Set("externalURL", url.spec());
RequestPermission( RequestPermission(
static_cast<blink::PermissionType>(PermissionType::OPEN_EXTERNAL), static_cast<blink::PermissionType>(PermissionType::OPEN_EXTERNAL),
std::move(callback), user_gesture, &details); std::move(callback), user_gesture, std::move(details));
} }
bool WebContentsPermissionHelper::CheckMediaAccessPermission( bool WebContentsPermissionHelper::CheckMediaAccessPermission(
const GURL& security_origin, const GURL& security_origin,
blink::mojom::MediaStreamType type) const { blink::mojom::MediaStreamType type) const {
base::DictionaryValue details; base::Value::Dict details;
details.SetString("securityOrigin", security_origin.spec()); details.Set("securityOrigin", security_origin.spec());
details.SetString("mediaType", MediaStreamTypeToString(type)); details.Set("mediaType", MediaStreamTypeToString(type));
// The permission type doesn't matter here, AUDIO_CAPTURE/VIDEO_CAPTURE // The permission type doesn't matter here, AUDIO_CAPTURE/VIDEO_CAPTURE
// are presented as same type in content_converter.h. // are presented as same type in content_converter.h.
return CheckPermission(blink::PermissionType::AUDIO_CAPTURE, &details); return CheckPermission(blink::PermissionType::AUDIO_CAPTURE,
std::move(details));
} }
bool WebContentsPermissionHelper::CheckSerialAccessPermission( bool WebContentsPermissionHelper::CheckSerialAccessPermission(
const url::Origin& embedding_origin) const { const url::Origin& embedding_origin) const {
base::DictionaryValue details; base::Value::Dict details;
details.SetString("securityOrigin", embedding_origin.GetURL().spec()); details.Set("securityOrigin", embedding_origin.GetURL().spec());
return CheckPermission( return CheckPermission(
static_cast<blink::PermissionType>(PermissionType::SERIAL), &details); static_cast<blink::PermissionType>(PermissionType::SERIAL),
std::move(details));
} }
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsPermissionHelper); WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsPermissionHelper);

View file

@ -59,10 +59,10 @@ class WebContentsPermissionHelper
void RequestPermission(blink::PermissionType permission, void RequestPermission(blink::PermissionType permission,
base::OnceCallback<void(bool)> callback, base::OnceCallback<void(bool)> callback,
bool user_gesture = false, bool user_gesture = false,
const base::DictionaryValue* details = nullptr); base::Value::Dict details = {});
bool CheckPermission(blink::PermissionType permission, bool CheckPermission(blink::PermissionType permission,
const base::DictionaryValue* details) const; base::Value::Dict details) const;
// TODO(clavin): refactor to use the WebContents provided by the // TODO(clavin): refactor to use the WebContents provided by the
// WebContentsUserData base class instead of storing a duplicate ref // WebContentsUserData base class instead of storing a duplicate ref