Skip to content

Commit

Permalink
Convert various const strings to constexprs: chrome/browser/extensions/
Browse files Browse the repository at this point in the history
This makes it possible to supply them to absl::StrFormat() as format
strings, and may enable better codegen.

Bug: 1371963
Change-Id: Ic16e4cf4daed61b7731a6f40bfdb60d87e33b66e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4944680
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Giovanni Ortuno Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1211891}
  • Loading branch information
pkasting authored and Chromium LUCI CQ committed Oct 19, 2023
1 parent 00d9fb1 commit 2110cbc
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 89 deletions.
45 changes: 0 additions & 45 deletions chrome/browser/extensions/api/browsing_data/browsing_data_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,51 +37,6 @@ using browsing_data::BrowsingDataType;
using browsing_data::ClearBrowsingDataTab;
using content::BrowserThread;

namespace extension_browsing_data_api_constants {
// Parameter name keys.
const char kDataRemovalPermittedKey[] = "dataRemovalPermitted";
const char kDataToRemoveKey[] = "dataToRemove";
const char kOptionsKey[] = "options";

// Type keys.
const char kCacheKey[] = "cache";
const char kCookiesKey[] = "cookies";
const char kDownloadsKey[] = "downloads";
const char kFileSystemsKey[] = "fileSystems";
const char kFormDataKey[] = "formData";
const char kHistoryKey[] = "history";
const char kIndexedDBKey[] = "indexedDB";
const char kLocalStorageKey[] = "localStorage";
const char kPasswordsKey[] = "passwords";
const char kPluginDataKeyDeprecated[] = "pluginData";
const char kServiceWorkersKey[] = "serviceWorkers";
const char kCacheStorageKey[] = "cacheStorage";
const char kWebSQLKey[] = "webSQL";

// Option keys.
const char kExtensionsKey[] = "extension";
const char kOriginTypesKey[] = "originTypes";
const char kProtectedWebKey[] = "protectedWeb";
const char kSinceKey[] = "since";
const char kOriginsKey[] = "origins";
const char kExcludeOriginsKey[] = "excludeOrigins";
const char kUnprotectedWebKey[] = "unprotectedWeb";

// Errors!
// The placeholder will be filled by the name of the affected data type (e.g.,
// "history").
const char kBadDataTypeDetails[] = "Invalid value for data type '%s'.";
const char kDeleteProhibitedError[] =
"Browsing history and downloads are not "
"permitted to be removed.";
const char kNonFilterableError[] =
"At least one data type doesn't support filtering by origin.";
const char kIncompatibleFilterError[] =
"Don't set both 'origins' and 'excludeOrigins' at the same time.";
const char kInvalidOriginError[] = "'%s' is not a valid origin.";

} // namespace extension_browsing_data_api_constants

namespace {

const int64_t kFilterableDataTypes =
Expand Down
61 changes: 35 additions & 26 deletions chrome/browser/extensions/api/browsing_data/browsing_data_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,47 @@ class PrefService;
namespace extension_browsing_data_api_constants {

// Parameter name keys.
extern const char kDataRemovalPermittedKey[];
extern const char kDataToRemoveKey[];
extern const char kOptionsKey[];
inline constexpr char kDataRemovalPermittedKey[] = "dataRemovalPermitted";
inline constexpr char kDataToRemoveKey[] = "dataToRemove";
inline constexpr char kOptionsKey[] = "options";

// Type keys.
extern const char kCacheKey[];
extern const char kCookiesKey[];
extern const char kDownloadsKey[];
extern const char kFileSystemsKey[];
extern const char kFormDataKey[];
extern const char kHistoryKey[];
extern const char kIndexedDBKey[];
extern const char kPluginDataKey[];
extern const char kLocalStorageKey[];
extern const char kPasswordsKey[];
extern const char kServiceWorkersKey[];
extern const char kCacheStorageKey[];
extern const char kWebSQLKey[];
inline constexpr char kCacheKey[] = "cache";
inline constexpr char kCookiesKey[] = "cookies";
inline constexpr char kDownloadsKey[] = "downloads";
inline constexpr char kFileSystemsKey[] = "fileSystems";
inline constexpr char kFormDataKey[] = "formData";
inline constexpr char kHistoryKey[] = "history";
inline constexpr char kIndexedDBKey[] = "indexedDB";
inline constexpr char kLocalStorageKey[] = "localStorage";
inline constexpr char kPasswordsKey[] = "passwords";
inline constexpr char kPluginDataKeyDeprecated[] = "pluginData";
inline constexpr char kServiceWorkersKey[] = "serviceWorkers";
inline constexpr char kCacheStorageKey[] = "cacheStorage";
inline constexpr char kWebSQLKey[] = "webSQL";

// Option keys.
extern const char kExtensionsKey[];
extern const char kOriginTypesKey[];
extern const char kProtectedWebKey[];
extern const char kSinceKey[];
extern const char kUnprotectedWebKey[];
inline constexpr char kExtensionsKey[] = "extension";
inline constexpr char kOriginTypesKey[] = "originTypes";
inline constexpr char kProtectedWebKey[] = "protectedWeb";
inline constexpr char kSinceKey[] = "since";
inline constexpr char kOriginsKey[] = "origins";
inline constexpr char kExcludeOriginsKey[] = "excludeOrigins";
inline constexpr char kUnprotectedWebKey[] = "unprotectedWeb";

// Errors!
extern const char kBadDataTypeDetails[];
extern const char kDeleteProhibitedError[];
extern const char kNonFilterableError[];
extern const char kIncompatibleFilterError[];
extern const char kInvalidOriginError[];
// The placeholder will be filled by the name of the affected data type (e.g.,
// "history").
inline constexpr char kBadDataTypeDetails[] =
"Invalid value for data type '%s'.";
inline constexpr char kDeleteProhibitedError[] =
"Browsing history and downloads are not "
"permitted to be removed.";
inline constexpr char kNonFilterableError[] =
"At least one data type doesn't support filtering by origin.";
inline constexpr char kIncompatibleFilterError[] =
"Don't set both 'origins' and 'excludeOrigins' at the same time.";
inline constexpr char kInvalidOriginError[] = "'%s' is not a valid origin.";

} // namespace extension_browsing_data_api_constants

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ IN_PROC_BROWSER_TEST_F(WebAccessibleResourcesApiTest,
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), gurl));
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
const int tab_id = ExtensionTabUtil::GetTabId(web_contents);
std::string script =
static constexpr char kScript[] =
R"((async () => {
await chrome.scripting.executeScript(
{target: {tabId: %d}, files: ['test.js']})
})();)";
BackgroundScriptExecutor::ExecuteScriptAsync(
profile(), extension->id(), base::StringPrintf(script.c_str(), tab_id));
profile(), extension->id(), base::StringPrintf(kScript, tab_id));
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}

Expand All @@ -215,7 +215,7 @@ class WebAccessibleResourcesDynamicUrlApiTest : public ExtensionApiTest {
protected:
const Extension* GetExtension(const char* manifest_piece) {
// manifest.json.
const char* kManifestStub = R"({
static constexpr char kManifestStub[] = R"({
"name": "Test",
"version": "1.0",
"manifest_version": 3,
Expand All @@ -237,7 +237,7 @@ class WebAccessibleResourcesDynamicUrlApiTest : public ExtensionApiTest {
test_dir_.WriteManifest(kManifest);

// content.js
const char* kTestScript = R"(
static constexpr char kTestScript[] = R"(
// Verify that web accessible resource can be fetched.
async function run(expectOk, filename, identifier) {
return new Promise(async resolve => {
Expand Down Expand Up @@ -300,7 +300,7 @@ class WebAccessibleResourcesDynamicUrlApiTest : public ExtensionApiTest {

// Load dynamic web accessible resource from a content script.
IN_PROC_BROWSER_TEST_F(WebAccessibleResourcesDynamicUrlApiTest, ContentScript) {
const char* kManifest = R"(
static constexpr char kManifest[] = R"(
"content_scripts": [
{
"matches": ["<all_urls>"],
Expand All @@ -322,7 +322,7 @@ IN_PROC_BROWSER_TEST_F(WebAccessibleResourcesDynamicUrlApiTest, ContentScript) {
IN_PROC_BROWSER_TEST_F(WebAccessibleResourcesDynamicUrlApiTest, ExecuteScript) {
// Load extension.
WriteFile(FILE_PATH_LITERAL("worker.js"), "// Intentionally blank.");
const char* kManifest = R"(
static constexpr char kManifest[] = R"(
"permissions": ["scripting"],
"background": {"service_worker": "worker.js"}
)";
Expand All @@ -335,13 +335,13 @@ IN_PROC_BROWSER_TEST_F(WebAccessibleResourcesDynamicUrlApiTest, ExecuteScript) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), gurl));
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
const int tab_id = ExtensionTabUtil::GetTabId(web_contents);
std::string script =
static constexpr char kScript[] =
R"((async () => {
await chrome.scripting.executeScript(
{target: {tabId: %d}, files: ['content.js']})
})();)";
BackgroundScriptExecutor::ExecuteScriptAsync(
profile(), extension->id(), base::StringPrintf(script.c_str(), tab_id));
profile(), extension->id(), base::StringPrintf(kScript, tab_id));
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
}

Expand Down
7 changes: 3 additions & 4 deletions chrome/browser/extensions/back_forward_cache_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,13 @@ IN_PROC_BROWSER_TEST_P(ExtensionBackForwardCacheBrowserTest,
content::TitleWatcher title_watcher(
browser()->tab_strip_model()->GetActiveWebContents(), expected_title);

std::string action =
static constexpr char kAction[] =
R"HTML(
chrome.runtime.sendMessage('%s', 'some message',
() => { document.title = 'sent'});
)HTML";
EXPECT_TRUE(
ExecJs(render_frame_host_a.get(),
base::StringPrintf(action.c_str(), extension->id().c_str())));
EXPECT_TRUE(ExecJs(render_frame_host_a.get(),
base::StringPrintf(kAction, extension->id().c_str())));

// 2) Wait until the sendMessage has completed.
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ IN_PROC_BROWSER_TEST_F(CrossOriginIsolationTest, WebAccessibleFrame) {
// contexts inherit extension's cross-origin privileges.
{
auto execute_fetch = [](content::RenderFrameHost* host, const GURL& url) {
const char* kScript = R"(
static constexpr char kScript[] = R"(
fetch('%s')
.then(response => response.text())
.catch(err => "Fetch error: " + err);
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/extensions/fetch_apitest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ std::unique_ptr<net::test_server::HttpResponse> HandleEchoOrigin(
// JavaScript snippet which performs a fetch given a URL expression to be
// substituted as %s, then sends back the fetched content using
// chrome.test.sendScriptResult.
const char* kFetchScript = R"(
constexpr char kFetchScript[] = R"(
fetch(%s).then(function(result) {
return result.text();
}).then(function(text) {
Expand All @@ -59,7 +59,7 @@ const char* kFetchScript = R"(

// JavaScript snippet which performs a fetch given a URL expression to be
// substituted as %s.
const char* kDOMFetchScript = R"(
constexpr char kDOMFetchScript[] = R"(
fetch(%s).then(function(result) {
return result.text();
}).catch(function(err) {
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/extensions/policy_handlers_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ const char kSanitizedTestManagementPolicy[] =
" },"
"}";

const char kTestManagementPolicy3[] =
constexpr char kTestManagementPolicy3[] =
"{"
" \"*\": {"
" \"runtime_blocked_hosts\": [\"%s\"]"
" }"
"}";

const char kTestManagementPolicy4[] =
constexpr char kTestManagementPolicy4[] =
"{"
" \"*\": {"
" \"runtime_allowed_hosts\": [\"%s\"]"
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/extensions/wake_event_page_apitest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace {
//
// Expects a single string replacement of the "background" property, including
// trailing comma, or nothing if there is no background page.
const char* kManifestJson =
constexpr char kManifestJson[] =
"{\n"
" %s\n"
" 'content_scripts': [{\n"
Expand Down

0 comments on commit 2110cbc

Please sign in to comment.