Skip to content

Commit

Permalink
Convert various const strings to constexprs: chrome/browser/ash/
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.

Also marks constexpr globals in headers as inline, since that's not
implied.

Bug: 1371963
Change-Id: I08298b394cb64ecb009a81183894013d7e7cfc53
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4944854
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1210683}
  • Loading branch information
pkasting authored and Chromium LUCI CQ committed Oct 17, 2023
1 parent 3cd1827 commit a9a61fd
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 150 deletions.
37 changes: 0 additions & 37 deletions chrome/browser/ash/arc/fileapi/arc_select_files_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,6 @@
#include "url/gurl.h"

namespace arc {

// Script for clicking OK button on the selector.
const char kScriptClickOk[] =
"(function() { document.querySelector('#ok-button').click(); })();";

// Script for clicking Cancel button on the selector.
const char kScriptClickCancel[] =
"(function() { document.querySelector('#cancel-button').click(); })();";

// Script for clicking a directory element in the left pane of the selector.
// %s should be replaced by the target directory name wrapped by double-quotes.
const char kScriptClickDirectory[] =
"(function() {"
" var dirs = document.querySelectorAll('#directory-tree .entry-name');"
" Array.from(dirs).filter(a => a.innerText === %s)[0].click();"
"})();";

// Script for clicking a file element in the right pane of the selector.
// %s should be replaced by the target file name wrapped by double-quotes.
const char kScriptClickFile[] =
"(function() {"
" var evt = document.createEvent('MouseEvents');"
" evt.initMouseEvent('mousedown', true, false);"
" var files = document.querySelectorAll('#file-list .file');"
" Array.from(files).filter(a => a.getAttribute('file-name') === %s)[0]"
" .dispatchEvent(evt);"
"})();";

// Script for querying UI elements (directories and files) shown on the selector.
const char kScriptGetElements[] =
"(function() {"
" var dirs = document.querySelectorAll('#directory-tree .entry-name');"
" var files = document.querySelectorAll('#file-list .file');"
" return {dirNames: Array.from(dirs, a => a.innerText),"
" fileNames: Array.from(files, a => a.getAttribute('file-name'))};"
"})();";

namespace {

constexpr char kRecentAllFakePath[] = "/.fake-entry/recent/all";
Expand Down
41 changes: 36 additions & 5 deletions chrome/browser/ash/arc/fileapi/arc_select_files_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,42 @@ class ArcSelectFilesHandler;
class SelectFileDialogHolder;

// Exposed for testing.
extern const char kScriptClickOk[];
extern const char kScriptClickCancel[];
extern const char kScriptClickDirectory[];
extern const char kScriptClickFile[];
extern const char kScriptGetElements[];
// Script for clicking OK button on the selector.
inline constexpr char kScriptClickOk[] =
"(function() { document.querySelector('#ok-button').click(); })();";

// Script for clicking Cancel button on the selector.
inline constexpr char kScriptClickCancel[] =
"(function() { document.querySelector('#cancel-button').click(); })();";

// Script for clicking a directory element in the left pane of the selector.
// %s should be replaced by the target directory name wrapped by double-quotes.
inline constexpr char kScriptClickDirectory[] =
"(function() {"
" var dirs = document.querySelectorAll('#directory-tree .entry-name');"
" Array.from(dirs).filter(a => a.innerText === %s)[0].click();"
"})();";

// Script for clicking a file element in the right pane of the selector.
// %s should be replaced by the target file name wrapped by double-quotes.
inline constexpr char kScriptClickFile[] =
"(function() {"
" var evt = document.createEvent('MouseEvents');"
" evt.initMouseEvent('mousedown', true, false);"
" var files = document.querySelectorAll('#file-list .file');"
" Array.from(files).filter(a => a.getAttribute('file-name') === %s)[0]"
" .dispatchEvent(evt);"
"})();";

// Script for querying UI elements (directories and files) shown on the
// selector.
inline constexpr char kScriptGetElements[] =
"(function() {"
" var dirs = document.querySelectorAll('#directory-tree .entry-name');"
" var files = document.querySelectorAll('#file-list .file');"
" return {dirNames: Array.from(dirs, a => a.innerText),"
" fileNames: Array.from(files, a => a.getAttribute('file-name'))};"
"})();";

// Manages multiple ArcSelectFilesHandler instances.
class ArcSelectFilesHandlersManager {
Expand Down
8 changes: 4 additions & 4 deletions chrome/browser/ash/authpolicy/kerberos_files_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ absl::optional<std::string> MaybeAdjustConfig(
bool is_dns_cname_enabled) {
if (!config.has_value())
return absl::nullopt;
static constexpr char kKrb5CnameSettings[] =
"[libdefaults]\n"
"\tdns_canonicalize_hostname = %s\n"
"\trdns = false\n";
std::string adjusted_config = base::StringPrintf(
kKrb5CnameSettings, is_dns_cname_enabled ? "true" : "false");
adjusted_config.append(config.value());
Expand All @@ -87,10 +91,6 @@ absl::optional<std::string> MaybeAdjustConfig(

} // namespace

const char kKrb5CnameSettings[] =
"[libdefaults]\n"
"\tdns_canonicalize_hostname = %s\n"
"\trdns = false\n";
const char kKrb5CCEnvName[] = "KRB5CCNAME";
const char kKrb5ConfEnvName[] = "KRB5_CONFIG";
const char kKrb5CCFilePrefix[] = "FILE:";
Expand Down
2 changes: 0 additions & 2 deletions chrome/browser/ash/authpolicy/kerberos_files_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace ash {
// Kerberos defaults for canonicalization SPN. (see
// https://web.mit.edu/kerberos/krb5-1.12/doc/admin/conf_files/krb5_conf.html)
// Exported for browsertests.
extern const char kKrb5CnameSettings[];

// Environment variable pointing to credential cache file.
extern const char kKrb5CCEnvName[];
// Environment variable pointing to Kerberos config file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace {
constexpr char kFcmCertProvisioningPublicTopicPrefix[] = "cert-";

// Shall be expanded to cert.[scope].[topic]
const char* kOwnerNameFormat = "cert.%s.%s";
constexpr char kOwnerNameFormat[] = "cert.%s.%s";

const char* CertScopeToString(CertScope scope) {
switch (scope) {
Expand Down
16 changes: 0 additions & 16 deletions chrome/browser/ash/crostini/crostini_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,6 @@
#include "ui/base/l10n/time_format.h"

namespace crostini {

const char kCrostiniImageAliasPattern[] = "debian/%s";
const char kCrostiniContainerDefaultVersion[] = "bullseye";
const char kCrostiniContainerFlag[] = "crostini-container-install-version";

const guest_os::VmType kCrostiniDefaultVmType = guest_os::VmType::TERMINA;
const char kCrostiniDefaultVmName[] = "termina";
const char kCrostiniDefaultContainerName[] = "penguin";
const char kCrostiniDefaultUsername[] = "emperor";
const char kCrostiniDefaultImageServerUrl[] =
"https://storage.googleapis.com/cros-containers/%d";
const char kCrostiniDlcName[] = "termina-dlc";

const base::FilePath::CharType kHomeDirectory[] =
FILE_PATH_LITERAL("/home/chronos/user");

namespace {

constexpr char kCrostiniAppLaunchHistogram[] = "Crostini.AppLaunch";
Expand Down
28 changes: 16 additions & 12 deletions chrome/browser/ash/crostini/crostini_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ class Profile;

namespace crostini {

extern const char kCrostiniImageAliasPattern[];
extern const char kCrostiniContainerDefaultVersion[];
extern const char kCrostiniContainerFlag[];

extern const guest_os::VmType kCrostiniDefaultVmType;
extern const char kCrostiniDefaultVmName[];
extern const char kCrostiniDefaultContainerName[];
extern const char kCrostiniDefaultUsername[];
extern const char kCrostiniDefaultImageServerUrl[];
extern const char kCrostiniDlcName[];

extern const base::FilePath::CharType kHomeDirectory[];
inline constexpr char kCrostiniImageAliasPattern[] = "debian/%s";
inline constexpr char kCrostiniContainerDefaultVersion[] = "bullseye";
inline constexpr char kCrostiniContainerFlag[] =
"crostini-container-install-version";

inline constexpr guest_os::VmType kCrostiniDefaultVmType =
guest_os::VmType::TERMINA;
inline constexpr char kCrostiniDefaultVmName[] = "termina";
inline constexpr char kCrostiniDefaultContainerName[] = "penguin";
inline constexpr char kCrostiniDefaultUsername[] = "emperor";
inline constexpr char kCrostiniDefaultImageServerUrl[] =
"https://storage.googleapis.com/cros-containers/%d";
inline constexpr char kCrostiniDlcName[] = "termina-dlc";

inline constexpr base::FilePath::CharType kHomeDirectory[] =
FILE_PATH_LITERAL("/home/chronos/user");

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
Expand Down
4 changes: 0 additions & 4 deletions chrome/browser/ash/customization/customization_document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@ std::string ReadFileInBackground(const base::FilePath& file) {

} // anonymous namespace

// Template URL where to fetch OEM services customization manifest from.
const char ServicesCustomizationDocument::kManifestUrl[] =
"https://ssl.gstatic.com/chrome/chromeos-customization/%s.json";

// A custom extensions::ExternalLoader that the ServicesCustomizationDocument
// creates and uses to publish OEM default apps to the extensions system.
class ServicesCustomizationExternalLoader
Expand Down
4 changes: 3 additions & 1 deletion chrome/browser/ash/customization/customization_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ class ServicesCustomizationDocument : public CustomizationDocument {
static void RegisterPrefs(PrefRegistrySimple* registry);
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

static const char kManifestUrl[];
// Template URL where to fetch OEM services customization manifest from.
static constexpr char kManifestUrl[] =
"https://ssl.gstatic.com/chrome/chromeos-customization/%s.json";

// Return true if the customization was applied. Customization is applied only
// once per machine.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,6 @@ bool HasOneDecimalDigit(absl::optional<double> rate) {

} // namespace

const char kFullRequestTemplate[] =
R"({
"advanced_options": {
"audio_generation_options": {"speed_factor": %.1f},
"force_language": "%s"
},
"text": {
"text_parts": ["%s"]
},
"voice_settings": {
"voice_criteria_and_selections": [{
"criteria": {"language": "%s"},
"selection": {"default_voice": "%s"}
}]
}
})";

extern const char kSimpleRequestTemplate[] =
R"({"advanced_options": {
"audio_generation_options": {"speed_factor": %.1f}
},
"text": {"text_parts": ["%s"]}})";

extern const char kTemplateResponse[] =
R"([
{"metadata": {}},
{"text": {
"timingInfo": [
{
"text": "test1",
"location": {
"textLocation": {"length": 5},
"timeLocation": {
"timeOffset": "0.01s",
"duration": "0.14s"
}
}
},
{
"text": "test2",
"location": {
"textLocation": {"length": 5, "offset": 6},
"timeLocation": {
"timeOffset": "0.16s",
"duration": "0.17s"
}
}
}
]}
},
{"audio": {"bytes": "%s"}}
])";

std::string CreateCorrectRequest(const std::string& input_text,
float rate,
const std::string& voice_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,66 @@ namespace ash {
namespace enhanced_network_tts {

// The accuracy used to compare two doubles.
constexpr double kDoubleCompareAccuracy = 0.000001;
inline constexpr double kDoubleCompareAccuracy = 0.000001;

// The template for a full request that contains utterance, rate, voice name,
// and language. See https://goto.google.com/readaloud-proto for more
// information.
extern const char kFullRequestTemplate[];
inline constexpr char kFullRequestTemplate[] =
R"({
"advanced_options": {
"audio_generation_options": {"speed_factor": %.1f},
"force_language": "%s"
},
"text": {
"text_parts": ["%s"]
},
"voice_settings": {
"voice_criteria_and_selections": [{
"criteria": {"language": "%s"},
"selection": {"default_voice": "%s"}
}]
}
})";

// The template for a simple request that only contains utterance and rate.
// See https://goto.google.com/readaloud-proto for more information.
extern const char kSimpleRequestTemplate[];
inline constexpr char kSimpleRequestTemplate[] =
R"({"advanced_options": {
"audio_generation_options": {"speed_factor": %.1f}
},
"text": {"text_parts": ["%s"]}})";

// Template for a server response.
extern const char kTemplateResponse[];
inline constexpr char kTemplateResponse[] =
R"([
{"metadata": {}},
{"text": {
"timingInfo": [
{
"text": "test1",
"location": {
"textLocation": {"length": 5},
"timeLocation": {
"timeOffset": "0.01s",
"duration": "0.14s"
}
}
},
{
"text": "test2",
"location": {
"textLocation": {"length": 5, "offset": 6},
"timeLocation": {
"timeOffset": "0.16s",
"duration": "0.17s"
}
}
}
]}
},
{"audio": {"bytes": "%s"}}
])";

// Create a correct request based on the |kFullRequestTemplate|.
std::string CreateCorrectRequest(const std::string& input_text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ void NetworkPortalDetectorMixin::SetShillDefaultNetwork(
}

std::string state = StatusToState(status);
const std::string json =
static constexpr char kJson[] =
R"({"GUID": "%s", "Type": "%s", "SSID": "wifi_ssid",
"State": "%s", "Strength": 100, "AutoConnect": true})";
std::string json_str = base::StringPrintf(
json.c_str(), network_guid.c_str(), network_type.c_str(), state.c_str());
kJson, network_guid.c_str(), network_type.c_str(), state.c_str());
absl::optional<base::Value::Dict> json_dict =
chromeos::onc::ReadDictionaryFromJson(json_str);
CHECK(json_dict.has_value());
Expand Down

0 comments on commit a9a61fd

Please sign in to comment.