Commit 9f420bec authored by Jiewei Qian's avatar Jiewei Qian Committed by Commit Bot

webui: add a method to register permission lists to WebUIAllowlist

This CL adds a RegisterAutoGrantedPermissions method WebUIAllowlist.
This method allows registering a list of permissions, instead of doing
a for-loop.

Also adds a comment about permissions (and content settings) commonly
wanted by WebUIs.

Bug: 1109773
Change-Id: I50c05d3b2b7672978dbf53c267ddcaa0352c0545
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2336425Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Reviewed-by: default avatarcalamity <calamity@chromium.org>
Commit-Queue: Jiewei Qian  <qjw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796297}
parent ca8ae31f
......@@ -253,3 +253,27 @@ TEST_F(WebUIAllowlistProviderTest, RegisterDevtools) {
url_no_permission_webui, url_no_permission_webui,
ContentSettingsType::BLUETOOTH_GUARD, std::string()));
}
TEST_F(WebUIAllowlistProviderTest, RegisterWithPermissionList) {
auto* map = GetHostContentSettingsMap(profile());
map->SetDefaultContentSetting(ContentSettingsType::BLUETOOTH_GUARD,
CONTENT_SETTING_BLOCK);
map->SetDefaultContentSetting(ContentSettingsType::NOTIFICATIONS,
CONTENT_SETTING_BLOCK);
const GURL url_chrome = GURL("chrome://test");
auto* allowlist = WebUIAllowlist::GetOrCreate(profile());
allowlist->RegisterAutoGrantedPermissions(
url::Origin::Create(url_chrome), {ContentSettingsType::BLUETOOTH_GUARD,
ContentSettingsType::NOTIFICATIONS});
EXPECT_EQ(CONTENT_SETTING_ALLOW,
map->GetContentSetting(url_chrome, url_chrome,
ContentSettingsType::BLUETOOTH_GUARD,
std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
map->GetContentSetting(url_chrome, url_chrome,
ContentSettingsType::NOTIFICATIONS,
std::string()));
}
......@@ -89,6 +89,13 @@ void WebUIAllowlist::RegisterAutoGrantedPermission(const url::Origin& origin,
}
}
void WebUIAllowlist::RegisterAutoGrantedPermissions(
const url::Origin& origin,
std::initializer_list<ContentSettingsType> types) {
for (const ContentSettingsType& type : types)
RegisterAutoGrantedPermission(origin, type);
}
void WebUIAllowlist::SetWebUIAllowlistProvider(
WebUIAllowlistProvider* provider) {
provider_ = provider;
......
......@@ -5,6 +5,7 @@
#ifndef UI_WEBUI_WEBUI_ALLOWLIST_H_
#define UI_WEBUI_WEBUI_ALLOWLIST_H_
#include <initializer_list>
#include <map>
#include "base/supports_user_data.h"
......@@ -32,11 +33,26 @@ class WebUIAllowlist : public base::SupportsUserData::Data {
~WebUIAllowlist() override;
// Register auto-granted |type| permission for |origin|.
//
// WebUIAllowlist comes with no permission by default. Users can deny
// permissions (e.g. Settings > Site Settings) unless they are registered
// here.
//
// Most WebUIs would want to declare these:
// COOKIES: use persistent storage (e.g. localStorage)
// JAVASCRIPT: run JavaScript
// IMAGES: show images
// SOUND: play sounds
void RegisterAutoGrantedPermission(
const url::Origin& origin,
ContentSettingsType type,
ContentSetting setting = CONTENT_SETTING_ALLOW);
// Register auto-granted |types| permissions for |origin|.
void RegisterAutoGrantedPermissions(
const url::Origin& origin,
std::initializer_list<ContentSettingsType> types);
std::unique_ptr<content_settings::RuleIterator> GetRuleIterator(
ContentSettingsType content_type) const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment