[cr136] Use modernised Environment::GetVar

This variant of this function makes use of an optional return type,
which makes code easier to read around it.

Chromium change:
https://chromium.googlesource.com/chromium/src/+/29d1bd26d171ea8d187ef62fb45519bad9551e08

commit 29d1bd26d171ea8d187ef62fb45519bad9551e08
Author: Lei Zhang <thestig@chromium.org>
Date:   Mon Mar 10 15:43:32 2025 -0700

    Add modern version of base::Environment::GetVar() that returns optional

    Add a version of GetVar() that returns an optional, instead of writing
    the result to an out-parameter and returning a bool. Keep the existing
    GetVar() but add a comment to mention it is deprecated.

    Convert base/nix/xdg_util.cc and its unit test to use the new GetVar().

    Bug: 400758498
This commit is contained in:
Claudio DeSouza
2025-04-16 09:30:24 +01:00
parent 1db83a89b6
commit e700fed8b9
8 changed files with 30 additions and 47 deletions
@@ -96,7 +96,7 @@ class BraveStatsUpdaterBrowserTest : public PlatformBrowserTest {
}
void SetBaseUpdateURLForTest() {
std::unique_ptr<base::Environment> env(base::Environment::Create());
auto env = base::Environment::Create();
env->SetVar("BRAVE_REFERRALS_SERVER",
embedded_test_server()->base_url().spec());
}
+1 -1
View File
@@ -48,7 +48,7 @@ IN_PROC_BROWSER_TEST_F(BraveWalletExtensionApiTest,
}
IN_PROC_BROWSER_TEST_F(BraveWalletExtensionApiTest, BraveWalletAPIAvailable) {
std::unique_ptr<base::Environment> env(base::Environment::Create());
auto env = base::Environment::Create();
env->SetVar("BRAVE_INFURA_PROJECT_ID", "test-project-id");
env->SetVar("BRAVE_SERVICES_KEY", "test-brave-key");
ResultCatcher catcher;
@@ -32,32 +32,20 @@ bool HasInfuraProjectID() {
return true;
}
std::unique_ptr<base::Environment> env(base::Environment::Create());
bool has_project_id = env->HasVar("BRAVE_INFURA_PROJECT_ID");
return has_project_id;
auto env = base::Environment::Create();
return env->HasVar("BRAVE_INFURA_PROJECT_ID");
}
std::string GetInfuraProjectID() {
std::string project_id(BUILDFLAG(BRAVE_INFURA_PROJECT_ID));
std::unique_ptr<base::Environment> env(base::Environment::Create());
if (env->HasVar("BRAVE_INFURA_PROJECT_ID")) {
env->GetVar("BRAVE_INFURA_PROJECT_ID", &project_id);
}
return project_id;
auto env = base::Environment::Create();
return env->GetVar("BRAVE_INFURA_PROJECT_ID")
.value_or(BUILDFLAG(BRAVE_INFURA_PROJECT_ID));
}
std::string GetBraveKey() {
std::string brave_key(BUILDFLAG(BRAVE_SERVICES_KEY));
std::unique_ptr<base::Environment> env(base::Environment::Create());
if (env->HasVar("BRAVE_SERVICES_KEY")) {
env->GetVar("BRAVE_SERVICES_KEY", &brave_key);
}
return brave_key;
auto env = base::Environment::Create();
return env->GetVar("BRAVE_SERVICES_KEY")
.value_or(BUILDFLAG(BRAVE_SERVICES_KEY));
}
} // namespace extensions
@@ -54,8 +54,8 @@ std::string GetChannelSuffixForExtraFlagsEnvVarName() {
#if BUILDFLAG(IS_LINUX)
std::string GetDesktopName(base::Environment* env) {
std::string brave_snap;
if (env->GetVar("BRAVE_SNAP", &brave_snap) && brave_snap == "1") {
if (auto brave_snap = env->GetVar("BRAVE_SNAP");
brave_snap && *brave_snap == "1") {
return "brave.desktop";
}
#if defined(OFFICIAL_BUILD)
@@ -74,9 +74,10 @@ std::string GetDesktopName(base::Environment* env) {
// Allow $CHROME_DESKTOP to override the built-in value, so that development
// versions can set themselves as the default without interfering with
// non-official, packaged versions using the built-in value.
std::string name;
if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty())
if (std::string name = env->GetVar("CHROME_DESKTOP").value_or(std::string());
!name.empty()) {
return name;
}
return "brave-browser.desktop";
#endif
}
@@ -14,7 +14,7 @@
namespace chrome {
bool GetDefaultUserDataDirectory(base::FilePath* result) {
std::unique_ptr<base::Environment> env(base::Environment::Create());
auto env = base::Environment::Create();
base::FilePath config_dir;
std::string chrome_config_home_str;
config_dir =
@@ -128,9 +128,9 @@ std::string ReadPromoCode(const base::FilePath& promo_code_file) {
}
std::string BuildReferralEndpoint(const std::string& path) {
std::unique_ptr<base::Environment> env(base::Environment::Create());
std::string referral_server;
env->GetVar("BRAVE_REFERRALS_SERVER", &referral_server);
auto env = base::Environment::Create();
std::string referral_server =
env->GetVar("BRAVE_REFERRALS_SERVER").value_or(std::string());
if (referral_server.empty()) {
auto referral_domain = brave_domains::GetServicesDomain("usage-ping");
referral_server = base::StrCat(
@@ -401,9 +401,9 @@ void BraveReferralsService::MaybeCheckForReferralFinalization() {
// Only check for referral finalization after 30 days have elapsed
// since first run.
uint64_t check_time = 30 * 24 * 60 * 60;
std::unique_ptr<base::Environment> env(base::Environment::Create());
std::string check_time_str;
env->GetVar("BRAVE_REFERRALS_CHECK_TIME", &check_time_str);
auto env = base::Environment::Create();
std::string check_time_str =
env->GetVar("BRAVE_REFERRALS_CHECK_TIME").value_or(std::string());
if (!check_time_str.empty())
base::StringToUint64(check_time_str, &check_time);
@@ -435,9 +435,9 @@ void BraveReferralsService::MaybeDeletePromoCodePref() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
uint64_t delete_time = 90 * 24 * 60 * 60;
std::unique_ptr<base::Environment> env(base::Environment::Create());
std::string delete_time_str;
env->GetVar("BRAVE_REFERRALS_DELETE_TIME", &delete_time_str);
auto env = base::Environment::Create();
std::string delete_time_str =
env->GetVar("BRAVE_REFERRALS_DELETE_TIME").value_or(std::string());
if (!delete_time_str.empty())
base::StringToUint64(delete_time_str, &delete_time);
@@ -121,12 +121,9 @@ base::Time GetYMDAsDate(std::string_view ymd) {
}
std::string GetAPIKey() {
std::string api_key = BUILDFLAG(BRAVE_STATS_API_KEY);
std::unique_ptr<base::Environment> env(base::Environment::Create());
if (env->HasVar("BRAVE_STATS_API_KEY"))
env->GetVar("BRAVE_STATS_API_KEY", &api_key);
return api_key;
auto env = base::Environment::Create();
return env->GetVar("BRAVE_STATS_API_KEY")
.value_or(BUILDFLAG(BRAVE_STATS_API_KEY));
}
// This is a helper method for dealing with timestamps set by other services in
+2 -5
View File
@@ -56,11 +56,8 @@ class RedirectCC {
.Append(UTF8ToFilePathString(BUILDFLAG(REAL_REWRAPPER)))
.value();
#else // defined(REDIRECT_CC_AS_REWRAPPER)
std::string cc_wrapper;
std::unique_ptr<base::Environment> env(base::Environment::Create());
if (env->HasVar("CC_WRAPPER")) {
CHECK(env->GetVar("CC_WRAPPER", &cc_wrapper));
}
auto env = base::Environment::Create();
std::string cc_wrapper = env->GetVar("CC_WRAPPER").value_or(std::string());
if (!cc_wrapper.empty()) {
*first_compiler_arg_idx = 1;