Commit 357a4a6e authored by Jeremy Roman's avatar Jeremy Roman Committed by Chromium LUCI CQ

Use base::StrCat and base::MakeFixedFlatMap in chrome_pages.cc.

They're cool and efficient. This should be more efficient at runtime but
approximately as readable in the source text.

Change-Id: Id8b8a2b1c8b869c4a3f22326bfa381fb67b3de13
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2562819
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Auto-Submit: Jeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#831951}
parent 2b9eacf0
......@@ -8,11 +8,11 @@
#include <memory>
#include "base/containers/fixed_flat_map.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/metrics/user_metrics.h"
#include "base/no_destructor.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
......@@ -189,8 +189,9 @@ std::string GenerateContentSettingsExceptionsSubPage(ContentSettingsType type) {
// TODO(crbug.com/728353): Update the group names defined in
// site_settings_helper once Options is removed from Chrome. Then this list
// will no longer be needed.
static base::NoDestructor<std::map<ContentSettingsType, std::string>>
kSettingsPathOverrides(
static constexpr auto kSettingsPathOverrides =
base::MakeFixedFlatMap<ContentSettingsType, base::StringPiece>(
{{ContentSettingsType::AUTOMATIC_DOWNLOADS, "automaticDownloads"},
{ContentSettingsType::BACKGROUND_SYNC, "backgroundSync"},
{ContentSettingsType::MEDIASTREAM_MIC, "microphone"},
......@@ -198,13 +199,12 @@ std::string GenerateContentSettingsExceptionsSubPage(ContentSettingsType type) {
{ContentSettingsType::MIDI_SYSEX, "midiDevices"},
{ContentSettingsType::ADS, "ads"},
{ContentSettingsType::PPAPI_BROKER, "unsandboxedPlugins"}});
const auto it = kSettingsPathOverrides->find(type);
const std::string content_type_path =
(it == kSettingsPathOverrides->end())
? site_settings::ContentSettingsTypeToGroupName(type)
: it->second;
const auto* it = kSettingsPathOverrides.find(type);
return std::string(kContentSettingsSubPage) + "/" + content_type_path;
return base::StrCat({kContentSettingsSubPage, "/",
(it == kSettingsPathOverrides.end())
? site_settings::ContentSettingsTypeToGroupName(type)
: it->second});
}
void ShowSiteSettingsImpl(Browser* browser, Profile* profile, const GURL& url) {
......@@ -379,9 +379,9 @@ void ShowSiteSettings(Profile* profile, const GURL& url) {
void ShowContentSettings(Browser* browser,
ContentSettingsType content_settings_type) {
ShowSettingsSubPage(
browser,
kContentSettingsSubPage + std::string(kHashMark) +
site_settings::ContentSettingsTypeToGroupName(content_settings_type));
browser, base::StrCat({kContentSettingsSubPage, kHashMark,
site_settings::ContentSettingsTypeToGroupName(
content_settings_type)}));
}
void ShowClearBrowsingDataDialog(Browser* browser) {
......
......@@ -2148,7 +2148,8 @@ TEST_F(SiteSettingsHandlerChooserExceptionTest,
HandleGetChooserExceptionListForUsb) {
const std::string kUsbChooserGroupName =
site_settings::ContentSettingsTypeToGroupName(
ContentSettingsType::USB_CHOOSER_DATA);
ContentSettingsType::USB_CHOOSER_DATA)
.as_string();
const base::Value& exceptions = GetChooserExceptionListFromWebUiCallData(
kUsbChooserGroupName, /*expected_total_calls=*/1u);
......@@ -2164,7 +2165,8 @@ TEST_F(SiteSettingsHandlerChooserExceptionTest,
HandleGetChooserExceptionListForUsbOffTheRecord) {
const std::string kUsbChooserGroupName =
site_settings::ContentSettingsTypeToGroupName(
ContentSettingsType::USB_CHOOSER_DATA);
ContentSettingsType::USB_CHOOSER_DATA)
.as_string();
SetUpOffTheRecordUsbChooserContext();
web_ui()->ClearTrackedCalls();
......@@ -2200,7 +2202,8 @@ TEST_F(SiteSettingsHandlerChooserExceptionTest,
HandleResetChooserExceptionForSiteForUsb) {
const std::string kUsbChooserGroupName =
site_settings::ContentSettingsTypeToGroupName(
ContentSettingsType::USB_CHOOSER_DATA);
ContentSettingsType::USB_CHOOSER_DATA)
.as_string();
const auto kAndroidOrigin = url::Origin::Create(AndroidUrl());
const auto kChromiumOrigin = url::Origin::Create(ChromiumUrl());
const std::string kAndroidOriginStr = AndroidUrl().GetOrigin().spec();
......
......@@ -376,7 +376,7 @@ bool HasRegisteredGroupName(ContentSettingsType type) {
return false;
}
ContentSettingsType ContentSettingsTypeFromGroupName(const std::string& name) {
ContentSettingsType ContentSettingsTypeFromGroupName(base::StringPiece name) {
for (size_t i = 0; i < base::size(kContentSettingsTypeGroupNames); ++i) {
if (name == kContentSettingsTypeGroupNames[i].name)
return kContentSettingsTypeGroupNames[i].type;
......@@ -386,7 +386,7 @@ ContentSettingsType ContentSettingsTypeFromGroupName(const std::string& name) {
return ContentSettingsType::DEFAULT;
}
std::string ContentSettingsTypeToGroupName(ContentSettingsType type) {
base::StringPiece ContentSettingsTypeToGroupName(ContentSettingsType type) {
for (size_t i = 0; i < base::size(kContentSettingsTypeGroupNames); ++i) {
if (type == kContentSettingsTypeGroupNames[i].type) {
const char* name = kContentSettingsTypeGroupNames[i].name;
......
......@@ -13,6 +13,7 @@
#include <vector>
#include "base/strings/string16.h"
#include "base/strings/string_piece.h"
#include "base/values.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
......@@ -109,8 +110,8 @@ struct ManagedState {
bool HasRegisteredGroupName(ContentSettingsType type);
// Converts a ContentSettingsType to/from its group name identifier.
ContentSettingsType ContentSettingsTypeFromGroupName(const std::string& name);
std::string ContentSettingsTypeToGroupName(ContentSettingsType type);
ContentSettingsType ContentSettingsTypeFromGroupName(base::StringPiece name);
base::StringPiece ContentSettingsTypeToGroupName(ContentSettingsType type);
// Converts a ListValue of group names to a list of ContentSettingsTypes
std::vector<ContentSettingsType> ContentSettingsTypesFromGroupNames(
......
......@@ -560,7 +560,8 @@ void ExpectValidSiteExceptionObject(const base::Value& actual_site_object,
TEST_F(SiteSettingsHelperTest, CreateChooserExceptionObject) {
const std::string kUsbChooserGroupName =
ContentSettingsTypeToGroupName(ContentSettingsType::USB_CHOOSER_DATA);
ContentSettingsTypeToGroupName(ContentSettingsType::USB_CHOOSER_DATA)
.as_string();
const std::string& kPolicySource =
SiteSettingSourceToString(SiteSettingSource::kPolicy);
const std::string& kPreferenceSource =
......@@ -752,7 +753,8 @@ void ExpectDisplayNameEq(const base::Value& actual_exception_object,
TEST_F(SiteSettingsHelperChooserExceptionTest,
GetChooserExceptionListFromProfile) {
const std::string kUsbChooserGroupName =
ContentSettingsTypeToGroupName(ContentSettingsType::USB_CHOOSER_DATA);
ContentSettingsTypeToGroupName(ContentSettingsType::USB_CHOOSER_DATA)
.as_string();
const ChooserTypeNameEntry* chooser_type =
ChooserTypeFromGroupName(kUsbChooserGroupName);
const std::string& kPolicySource =
......
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