0

Revert "Use base::cstring_view in base::Environment"

This reverts commit 197604a7e5.

Reason for revert: Causes compile failure on linux64-rel-ready builder:
See https://ci.chromium.org/ui/p/chrome/builders/ci/linux64-rel-ready/46753/ and subsequent builds.

Original change's description:
> Use base::cstring_view in base::Environment
>
> Before this change, it was possible to pass a string that didn't
> have a terminating null character. To account for this, some of the
> functions created a temporary std::string, nullifying the point of
> the string_view. In addition, this temporary std::string was not
> created in every function.
>
> This is now fixed by using base::cstring_view.
>
> This change required modifying a couple existing constants to be
> fully defined at the call site.
>
> Change-Id: I5e28a4df4623d7ce9a13cfcf4b54a0194daf89d8
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6418733
> Reviewed-by: Kyle Charbonneau <kylechar@chromium.org>
> Reviewed-by: Charlie Harrison <csharrison@chromium.org>
> Reviewed-by: Amelie Schneider <amelies@google.com>
> Reviewed-by: Dale Curtis <dalecurtis@chromium.org>
> Reviewed-by: Boris Sazonov <bsazonov@chromium.org>
> Reviewed-by: Joe Downing <joedow@chromium.org>
> Reviewed-by: Aaron Leventhal <aleventhal@chromium.org>
> Reviewed-by: Ryan Hamilton <rch@chromium.org>
> Reviewed-by: Yaron Friedman <yfriedman@chromium.org>
> Commit-Queue: Patrick Monette <pmonette@chromium.org>
> Reviewed-by: Tom Sepez <tsepez@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1448397}

No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Change-Id: I8ca8ca0786c0d834ffc91d62028aaccb8cc5424c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6470383
Auto-Submit: Ian Clelland <iclelland@chromium.org>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Commit-Queue: Ian Clelland <iclelland@chromium.org>
Owners-Override: Ian Clelland <iclelland@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1448488}
This commit is contained in:
Ian Clelland
2025-04-17 12:10:41 -07:00
committed by Chromium LUCI CQ
parent 4cecdffdf9
commit c2de899cda
27 changed files with 203 additions and 164 deletions

@ -26,7 +26,7 @@ namespace {
class EnvironmentImpl : public Environment {
public:
std::optional<std::string> GetVar(cstring_view variable_name) override {
std::optional<std::string> GetVar(std::string_view variable_name) override {
auto result = GetVarImpl(variable_name);
if (result.has_value()) {
return result;
@ -48,17 +48,17 @@ class EnvironmentImpl : public Environment {
return GetVarImpl(alternate_case_var);
}
bool SetVar(cstring_view variable_name,
bool SetVar(std::string_view variable_name,
const std::string& new_value) override {
return SetVarImpl(variable_name, new_value);
}
bool UnSetVar(cstring_view variable_name) override {
bool UnSetVar(std::string_view variable_name) override {
return UnSetVarImpl(variable_name);
}
private:
std::optional<std::string> GetVarImpl(cstring_view variable_name) {
std::optional<std::string> GetVarImpl(std::string_view variable_name) {
#if BUILDFLAG(IS_WIN)
std::wstring wide_name = UTF8ToWide(variable_name);
// Documented to be the maximum environment variable size.
@ -73,7 +73,7 @@ class EnvironmentImpl : public Environment {
<< "value should fit in the buffer (including the null terminator)";
return WideToUTF8(std::wstring_view(value.data(), value_length));
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
const char* env_value = getenv(variable_name.c_str());
const char* env_value = getenv(std::string(variable_name).c_str());
if (!env_value) {
return std::nullopt;
}
@ -81,30 +81,41 @@ class EnvironmentImpl : public Environment {
#endif
}
bool SetVarImpl(cstring_view variable_name, const std::string& new_value) {
bool SetVarImpl(std::string_view variable_name,
const std::string& new_value) {
#if BUILDFLAG(IS_WIN)
// On success, a nonzero value is returned.
return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
UTF8ToWide(new_value).c_str());
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
// On success, zero is returned.
return !setenv(variable_name.c_str(), new_value.c_str(), 1);
return !setenv(variable_name.data(), new_value.c_str(), 1);
#endif
}
bool UnSetVarImpl(cstring_view variable_name) {
bool UnSetVarImpl(std::string_view variable_name) {
#if BUILDFLAG(IS_WIN)
// On success, a nonzero value is returned.
return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
// On success, zero is returned.
return !unsetenv(variable_name.c_str());
return !unsetenv(variable_name.data());
#endif
}
};
} // namespace
namespace env_vars {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
// On Posix systems, this variable contains the location of the user's home
// directory. (e.g, /home/username/).
const char kHome[] = "HOME";
#endif
} // namespace env_vars
Environment::~Environment() = default;
// static
@ -112,7 +123,7 @@ std::unique_ptr<Environment> Environment::Create() {
return std::make_unique<EnvironmentImpl>();
}
bool Environment::GetVar(cstring_view variable_name, std::string* result) {
bool Environment::GetVar(std::string_view variable_name, std::string* result) {
std::optional<std::string> actual_result = GetVar(variable_name);
if (!actual_result.has_value()) {
return false;
@ -122,7 +133,7 @@ bool Environment::GetVar(cstring_view variable_name, std::string* result) {
return true;
}
bool Environment::HasVar(cstring_view variable_name) {
bool Environment::HasVar(std::string_view variable_name) {
return GetVar(variable_name).has_value();
}

@ -9,9 +9,9 @@
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include "base/base_export.h"
#include "base/strings/cstring_view.h"
#include "build/build_config.h"
namespace base {
@ -19,9 +19,7 @@ namespace base {
namespace env_vars {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
// On Posix systems, this variable contains the location of the user's home
// directory. (e.g, /home/username/).
inline constexpr char kHome[] = "HOME";
BASE_EXPORT extern const char kHome[];
#endif
} // namespace env_vars
@ -36,24 +34,24 @@ class BASE_EXPORT Environment {
// Returns an environment variable's value.
// Returns std::nullopt if the key is unset.
// Note that the variable may be set to an empty string.
virtual std::optional<std::string> GetVar(cstring_view variable_name) = 0;
virtual std::optional<std::string> GetVar(std::string_view variable_name) = 0;
// DEPRECATED. Prefer GetVar() overload above.
// Gets an environment variable's value and stores it in |result|.
// Returns false if the key is unset.
bool GetVar(cstring_view variable_name, std::string* result);
bool GetVar(std::string_view variable_name, std::string* result);
// Syntactic sugar for GetVar(variable_name).has_value();
bool HasVar(cstring_view variable_name);
bool HasVar(std::string_view variable_name);
// Returns true on success, otherwise returns false. This method should not
// be called in a multi-threaded process.
virtual bool SetVar(cstring_view variable_name,
virtual bool SetVar(std::string_view variable_name,
const std::string& new_value) = 0;
// Returns true on success, otherwise returns false. This method should not
// be called in a multi-threaded process.
virtual bool UnSetVar(cstring_view variable_name) = 0;
virtual bool UnSetVar(std::string_view variable_name) = 0;
};
#if BUILDFLAG(IS_WIN)

@ -42,8 +42,15 @@ std::optional<std::string>& GetXdgActivationToken() {
namespace base::nix {
const char kDotConfigDir[] = ".config";
const char kXdgConfigHomeEnvVar[] = "XDG_CONFIG_HOME";
const char kXdgCurrentDesktopEnvVar[] = "XDG_CURRENT_DESKTOP";
const char kXdgSessionTypeEnvVar[] = "XDG_SESSION_TYPE";
const char kXdgActivationTokenEnvVar[] = "XDG_ACTIVATION_TOKEN";
const char kXdgActivationTokenSwitch[] = "xdg-activation-token";
FilePath GetXDGDirectory(Environment* env,
cstring_view env_name,
const char* env_name,
const char* fallback_dir) {
FilePath path;
if (auto env_value = env->GetVar(env_name).value_or(""); !env_value.empty()) {

@ -14,7 +14,6 @@
#include "base/base_export.h"
#include "base/functional/callback.h"
#include "base/strings/cstring_view.h"
namespace base {
@ -65,23 +64,23 @@ using XdgActivationLaunchOptionsCallback =
base::OnceCallback<void(LaunchOptions)>;
// The default XDG config directory name.
inline constexpr char kDotConfigDir[] = ".config";
BASE_EXPORT extern const char kDotConfigDir[];
// The XDG config directory environment variable.
inline constexpr char kXdgConfigHomeEnvVar[] = "XDG_CONFIG_HOME";
BASE_EXPORT extern const char kXdgConfigHomeEnvVar[];
// The XDG current desktop environment variable.
inline constexpr char kXdgCurrentDesktopEnvVar[] = "XDG_CURRENT_DESKTOP";
BASE_EXPORT extern const char kXdgCurrentDesktopEnvVar[];
// The XDG session type environment variable.
inline constexpr char kXdgSessionTypeEnvVar[] = "XDG_SESSION_TYPE";
BASE_EXPORT extern const char kXdgSessionTypeEnvVar[];
// The XDG activation token environment variable.
inline constexpr char kXdgActivationTokenEnvVar[] = "XDG_ACTIVATION_TOKEN";
BASE_EXPORT extern const char kXdgActivationTokenEnvVar[];
// Internally used to communicate the activation token between a newly launched
// process and an existing browser process.
inline constexpr char kXdgActivationTokenSwitch[] = "xdg-activation-token";
BASE_EXPORT extern const char kXdgActivationTokenSwitch[];
// Utility function for getting XDG directories.
// |env_name| is the name of an environment variable that we want to use to get
@ -89,7 +88,7 @@ inline constexpr char kXdgActivationTokenSwitch[] = "xdg-activation-token";
// use if |env_name| cannot be found or is empty. |fallback_dir| may be NULL.
// Examples of |env_name| are XDG_CONFIG_HOME and XDG_DATA_HOME.
BASE_EXPORT FilePath GetXDGDirectory(Environment* env,
cstring_view env_name,
const char* env_name,
const char* fallback_dir);
// Wrapper around xdg_user_dir_lookup() from src/base/third_party/xdg-user-dirs

@ -12,7 +12,6 @@
#include "base/files/file_path.h"
#include "base/nix/scoped_xdg_activation_token_injector.h"
#include "base/process/launch.h"
#include "base/strings/cstring_view.h"
#include "base/test/bind.h"
#include "base/test/scoped_path_override.h"
#include "testing/gmock/include/gmock/gmock.h"
@ -22,7 +21,6 @@
using ::testing::_;
using ::testing::Eq;
using ::testing::Return;
using ::testing::StrEq;
namespace base::nix {
@ -30,12 +28,15 @@ namespace {
class MockEnvironment : public Environment {
public:
MOCK_METHOD(std::optional<std::string>, GetVar, (cstring_view), (override));
MOCK_METHOD(std::optional<std::string>,
GetVar,
(std::string_view),
(override));
MOCK_METHOD(bool,
SetVar,
(cstring_view, const std::string& new_value),
(std::string_view, const std::string& new_value),
(override));
MOCK_METHOD(bool, UnSetVar, (cstring_view), (override));
MOCK_METHOD(bool, UnSetVar, (std::string_view), (override));
};
// Needs to be const char* to make gmock happy.
@ -89,7 +90,7 @@ TEST(XDGUtilTest, GetXDGDataWriteLocation) {
// Test that it returns $XDG_DATA_HOME.
{
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(StrEq("XDG_DATA_HOME")))
EXPECT_CALL(getter, GetVar(Eq("XDG_DATA_HOME")))
.WillOnce(Return("/user/path"));
ScopedPathOverride home_override(DIR_HOME, FilePath("/home/user"),
@ -113,9 +114,9 @@ TEST(XDGUtilTest, GetXDGDataSearchLocations) {
// Test that it returns $XDG_DATA_HOME + $XDG_DATA_DIRS.
{
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(StrEq("XDG_DATA_HOME")))
EXPECT_CALL(getter, GetVar(Eq("XDG_DATA_HOME")))
.WillOnce(Return("/user/path"));
EXPECT_CALL(getter, GetVar(StrEq("XDG_DATA_DIRS")))
EXPECT_CALL(getter, GetVar(Eq("XDG_DATA_DIRS")))
.WillOnce(Return("/system/path/1:/system/path/2"));
ScopedPathOverride home_override(DIR_HOME, FilePath("/home/user"),
/*is_absolute=*/true, /*create=*/false);
@ -128,7 +129,7 @@ TEST(XDGUtilTest, GetXDGDataSearchLocations) {
{
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq("XDG_DATA_DIRS")))
EXPECT_CALL(getter, GetVar(Eq("XDG_DATA_DIRS")))
.WillOnce(Return("/system/path/1:/system/path/2"));
ScopedPathOverride home_override(DIR_HOME, FilePath("/home/user"),
@ -143,7 +144,7 @@ TEST(XDGUtilTest, GetXDGDataSearchLocations) {
{
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq("XDG_DATA_DIRS")))
EXPECT_CALL(getter, GetVar(Eq("XDG_DATA_DIRS")))
.WillOnce(Return("/system/path/1:/system/path/2"));
std::vector<std::string> results =
FilePathsToStrings(GetXDGDataSearchLocations(&getter));
@ -157,7 +158,7 @@ TEST(XDGUtilTest, GetXDGDataSearchLocations) {
{
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq("XDG_DATA_HOME")))
EXPECT_CALL(getter, GetVar(Eq("XDG_DATA_HOME")))
.WillOnce(Return("/user/path"));
ScopedPathOverride home_override(DIR_HOME, FilePath("/home/user"),
/*is_absolute=*/true, /*create=*/false);
@ -170,7 +171,7 @@ TEST(XDGUtilTest, GetXDGDataSearchLocations) {
TEST(XDGUtilTest, GetDesktopEnvironmentGnome) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kDesktopSession)))
EXPECT_CALL(getter, GetVar(Eq(kDesktopSession)))
.WillOnce(Return(kDesktopGnome));
EXPECT_EQ(DESKTOP_ENVIRONMENT_GNOME, GetDesktopEnvironment(&getter));
@ -179,7 +180,7 @@ TEST(XDGUtilTest, GetDesktopEnvironmentGnome) {
TEST(XDGUtilTest, GetDesktopEnvironmentMATE) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kDesktopSession)))
EXPECT_CALL(getter, GetVar(Eq(kDesktopSession)))
.WillOnce(Return(kDesktopMATE));
EXPECT_EQ(DESKTOP_ENVIRONMENT_GNOME, GetDesktopEnvironment(&getter));
@ -188,7 +189,7 @@ TEST(XDGUtilTest, GetDesktopEnvironmentMATE) {
TEST(XDGUtilTest, GetDesktopEnvironmentKDE4) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kDesktopSession)))
EXPECT_CALL(getter, GetVar(Eq(kDesktopSession)))
.WillOnce(Return(kDesktopKDE4));
EXPECT_EQ(DESKTOP_ENVIRONMENT_KDE4, GetDesktopEnvironment(&getter));
@ -197,7 +198,7 @@ TEST(XDGUtilTest, GetDesktopEnvironmentKDE4) {
TEST(XDGUtilTest, GetDesktopEnvironmentKDE3) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kDesktopSession)))
EXPECT_CALL(getter, GetVar(Eq(kDesktopSession)))
.WillOnce(Return(kDesktopKDE));
EXPECT_EQ(DESKTOP_ENVIRONMENT_KDE3, GetDesktopEnvironment(&getter));
@ -206,7 +207,7 @@ TEST(XDGUtilTest, GetDesktopEnvironmentKDE3) {
TEST(XDGUtilTest, GetDesktopEnvironmentXFCE) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kDesktopSession)))
EXPECT_CALL(getter, GetVar(Eq(kDesktopSession)))
.WillOnce(Return(kDesktopXFCE));
EXPECT_EQ(DESKTOP_ENVIRONMENT_XFCE, GetDesktopEnvironment(&getter));
@ -215,7 +216,7 @@ TEST(XDGUtilTest, GetDesktopEnvironmentXFCE) {
TEST(XDGUtilTest, GetXdgDesktopCinnamon) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopCinnamon));
EXPECT_EQ(DESKTOP_ENVIRONMENT_CINNAMON, GetDesktopEnvironment(&getter));
@ -224,7 +225,7 @@ TEST(XDGUtilTest, GetXdgDesktopCinnamon) {
TEST(XDGUtilTest, GetXdgDesktopDeepin) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopDeepin));
EXPECT_EQ(DESKTOP_ENVIRONMENT_DEEPIN, GetDesktopEnvironment(&getter));
@ -233,7 +234,7 @@ TEST(XDGUtilTest, GetXdgDesktopDeepin) {
TEST(XDGUtilTest, GetXdgDesktopGnome) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopGNOME));
EXPECT_EQ(DESKTOP_ENVIRONMENT_GNOME, GetDesktopEnvironment(&getter));
@ -242,7 +243,7 @@ TEST(XDGUtilTest, GetXdgDesktopGnome) {
TEST(XDGUtilTest, GetXdgDesktopGnomeClassic) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopGNOMEClassic));
EXPECT_EQ(DESKTOP_ENVIRONMENT_GNOME, GetDesktopEnvironment(&getter));
@ -251,9 +252,9 @@ TEST(XDGUtilTest, GetXdgDesktopGnomeClassic) {
TEST(XDGUtilTest, GetXdgDesktopGnomeFallback) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopUnity));
EXPECT_CALL(getter, GetVar(StrEq(kDesktopSession)))
EXPECT_CALL(getter, GetVar(Eq(kDesktopSession)))
.WillOnce(Return(kDesktopGnomeFallback));
EXPECT_EQ(DESKTOP_ENVIRONMENT_GNOME, GetDesktopEnvironment(&getter));
@ -262,9 +263,9 @@ TEST(XDGUtilTest, GetXdgDesktopGnomeFallback) {
TEST(XDGUtilTest, GetXdgDesktopKDE5) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopKDE));
EXPECT_CALL(getter, GetVar(StrEq(kKDESession)))
EXPECT_CALL(getter, GetVar(Eq(kKDESession)))
.WillOnce(Return(kKDESessionKDE5));
EXPECT_EQ(DESKTOP_ENVIRONMENT_KDE5, GetDesktopEnvironment(&getter));
@ -273,9 +274,9 @@ TEST(XDGUtilTest, GetXdgDesktopKDE5) {
TEST(XDGUtilTest, GetXdgDesktopKDE6) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopKDE));
EXPECT_CALL(getter, GetVar(StrEq(kKDESession)))
EXPECT_CALL(getter, GetVar(Eq(kKDESession)))
.WillOnce(Return(kKDESessionKDE6));
EXPECT_EQ(DESKTOP_ENVIRONMENT_KDE6, GetDesktopEnvironment(&getter));
@ -284,7 +285,7 @@ TEST(XDGUtilTest, GetXdgDesktopKDE6) {
TEST(XDGUtilTest, GetXdgDesktopKDE4) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopKDE));
EXPECT_EQ(DESKTOP_ENVIRONMENT_KDE4, GetDesktopEnvironment(&getter));
@ -293,7 +294,7 @@ TEST(XDGUtilTest, GetXdgDesktopKDE4) {
TEST(XDGUtilTest, GetXdgDesktopPantheon) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopPantheon));
EXPECT_EQ(DESKTOP_ENVIRONMENT_PANTHEON, GetDesktopEnvironment(&getter));
@ -302,7 +303,7 @@ TEST(XDGUtilTest, GetXdgDesktopPantheon) {
TEST(XDGUtilTest, GetXdgDesktopUKUI) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopUKUI));
EXPECT_EQ(DESKTOP_ENVIRONMENT_UKUI, GetDesktopEnvironment(&getter));
@ -311,7 +312,7 @@ TEST(XDGUtilTest, GetXdgDesktopUKUI) {
TEST(XDGUtilTest, GetXdgDesktopUnity) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopUnity));
EXPECT_EQ(DESKTOP_ENVIRONMENT_UNITY, GetDesktopEnvironment(&getter));
@ -320,7 +321,7 @@ TEST(XDGUtilTest, GetXdgDesktopUnity) {
TEST(XDGUtilTest, GetXdgDesktopUnity7) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopUnity7));
EXPECT_EQ(DESKTOP_ENVIRONMENT_UNITY, GetDesktopEnvironment(&getter));
@ -329,7 +330,7 @@ TEST(XDGUtilTest, GetXdgDesktopUnity7) {
TEST(XDGUtilTest, GetXdgDesktopUnity8) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgCurrentDesktopEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgCurrentDesktopEnvVar)))
.WillOnce(Return(kXdgDesktopUnity8));
EXPECT_EQ(DESKTOP_ENVIRONMENT_UNITY, GetDesktopEnvironment(&getter));
@ -345,7 +346,7 @@ TEST(XDGUtilTest, GetXdgSessiontypeUnset) {
TEST(XDGUtilTest, GetXdgSessionTypeOther) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgSessionTypeEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgSessionTypeEnvVar)))
.WillOnce(Return(kSessionUnknown));
EXPECT_EQ(SessionType::kOther, GetSessionType(getter));
@ -354,7 +355,7 @@ TEST(XDGUtilTest, GetXdgSessionTypeOther) {
TEST(XDGUtilTest, GetXdgSessionTypeUnspecified) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgSessionTypeEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgSessionTypeEnvVar)))
.WillOnce(Return(kSessionUnspecified));
EXPECT_EQ(SessionType::kUnspecified, GetSessionType(getter));
@ -363,7 +364,7 @@ TEST(XDGUtilTest, GetXdgSessionTypeUnspecified) {
TEST(XDGUtilTest, GetXdgSessionTypeTty) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgSessionTypeEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgSessionTypeEnvVar)))
.WillOnce(Return(kSessionTty));
EXPECT_EQ(SessionType::kTty, GetSessionType(getter));
@ -372,7 +373,7 @@ TEST(XDGUtilTest, GetXdgSessionTypeTty) {
TEST(XDGUtilTest, GetXdgSessionTypeMir) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgSessionTypeEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgSessionTypeEnvVar)))
.WillOnce(Return(kSessionMir));
EXPECT_EQ(SessionType::kMir, GetSessionType(getter));
@ -381,7 +382,7 @@ TEST(XDGUtilTest, GetXdgSessionTypeMir) {
TEST(XDGUtilTest, GetXdgSessionTypeX11) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgSessionTypeEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgSessionTypeEnvVar)))
.WillOnce(Return(kSessionX11));
EXPECT_EQ(SessionType::kX11, GetSessionType(getter));
@ -390,7 +391,7 @@ TEST(XDGUtilTest, GetXdgSessionTypeX11) {
TEST(XDGUtilTest, GetXdgSessionTypeWayland) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgSessionTypeEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgSessionTypeEnvVar)))
.WillOnce(Return(kSessionWayland));
EXPECT_EQ(SessionType::kWayland, GetSessionType(getter));
@ -399,7 +400,7 @@ TEST(XDGUtilTest, GetXdgSessionTypeWayland) {
TEST(XDGUtilTest, GetXdgSessionTypeWaylandCapital) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgSessionTypeEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgSessionTypeEnvVar)))
.WillOnce(Return(kSessionWaylandCapital));
EXPECT_EQ(SessionType::kWayland, GetSessionType(getter));
@ -408,7 +409,7 @@ TEST(XDGUtilTest, GetXdgSessionTypeWaylandCapital) {
TEST(XDGUtilTest, GetXdgSessionTypeWaylandWhitespace) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(_)).WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(getter, GetVar(StrEq(kXdgSessionTypeEnvVar)))
EXPECT_CALL(getter, GetVar(Eq(kXdgSessionTypeEnvVar)))
.WillOnce(Return(kSessionWaylandWhitespace));
EXPECT_EQ(SessionType::kWayland, GetSessionType(getter));
@ -423,9 +424,9 @@ TEST(XDGUtilTest, ExtractXdgActivationTokenFromEnvNotSet) {
TEST(XDGUtilTest, ExtractXdgActivationTokenFromEnv) {
MockEnvironment getter;
EXPECT_CALL(getter, GetVar(StrEq("XDG_ACTIVATION_TOKEN")))
EXPECT_CALL(getter, GetVar(Eq("XDG_ACTIVATION_TOKEN")))
.WillOnce(Return(kXdgActivationTokenFromEnv));
EXPECT_CALL(getter, UnSetVar(StrEq("XDG_ACTIVATION_TOKEN")));
EXPECT_CALL(getter, UnSetVar(Eq("XDG_ACTIVATION_TOKEN")));
EXPECT_EQ(kXdgActivationTokenFromEnv,
ExtractXdgActivationTokenFromEnv(getter));
EXPECT_EQ(kXdgActivationTokenFromEnv, TakeXdgActivationToken());
@ -443,9 +444,9 @@ TEST(XDGUtilTest, ExtractXdgActivationTokenFromCmdLine) {
CommandLine command_line(CommandLine::NO_PROGRAM);
MockEnvironment getter;
// Extract activation token initially from env.
EXPECT_CALL(getter, GetVar(StrEq("XDG_ACTIVATION_TOKEN")))
EXPECT_CALL(getter, GetVar(Eq("XDG_ACTIVATION_TOKEN")))
.WillOnce(Return(kXdgActivationTokenFromEnv));
EXPECT_CALL(getter, UnSetVar(StrEq("XDG_ACTIVATION_TOKEN")));
EXPECT_CALL(getter, UnSetVar(Eq("XDG_ACTIVATION_TOKEN")));
EXPECT_EQ(kXdgActivationTokenFromEnv,
ExtractXdgActivationTokenFromEnv(getter));
// Now extract token from command line.
@ -466,9 +467,9 @@ TEST(XDGUtilTest, ScopedXdgActivationTokenInjector) {
cmd_line.AppendSwitch("z");
CommandLine::SwitchMap initial_switches = cmd_line.GetSwitches();
// Set token value in env
EXPECT_CALL(getter, GetVar(StrEq("XDG_ACTIVATION_TOKEN")))
EXPECT_CALL(getter, GetVar(Eq("XDG_ACTIVATION_TOKEN")))
.WillOnce(Return(kXdgActivationTokenFromEnv));
EXPECT_CALL(getter, UnSetVar(StrEq("XDG_ACTIVATION_TOKEN")));
EXPECT_CALL(getter, UnSetVar(Eq("XDG_ACTIVATION_TOKEN")));
{
ScopedXdgActivationTokenInjector scoped_injector(cmd_line, getter);
for (const auto& pair : initial_switches) {

@ -326,7 +326,7 @@ void KillSpawnedTestProcesses() {
// Parses the environment variable var as an Int32. If it is unset, returns
// true. If it is set, unsets it then converts it to Int32 before
// returning it in |result|. Returns true on success.
bool TakeInt32FromEnvironment(cstring_view var, int32_t* result) {
bool TakeInt32FromEnvironment(const char* const var, int32_t* result) {
std::unique_ptr<Environment> env(Environment::Create());
std::optional<std::string> str_val = env->GetVar(var);

@ -11,6 +11,7 @@
#include <cstdlib>
#include <map>
#include <optional>
#include <string_view>
#include <vector>
#include "base/base_paths.h"
@ -21,7 +22,6 @@
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/strings/cstring_view.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
@ -52,24 +52,24 @@ class MockEnvironment : public base::Environment {
MockEnvironment(const MockEnvironment&) = delete;
MockEnvironment& operator=(const MockEnvironment&) = delete;
void Set(base::cstring_view name, const std::string& value) {
void Set(std::string_view name, const std::string& value) {
variables_[std::string(name)] = value;
}
std::optional<std::string> GetVar(base::cstring_view variable_name) override {
std::optional<std::string> GetVar(std::string_view variable_name) override {
if (!base::Contains(variables_, std::string(variable_name))) {
return std::nullopt;
}
return variables_[std::string(variable_name)];
}
bool SetVar(base::cstring_view variable_name,
bool SetVar(std::string_view variable_name,
const std::string& new_value) override {
ADD_FAILURE();
return false;
}
bool UnSetVar(base::cstring_view variable_name) override {
bool UnSetVar(std::string_view variable_name) override {
ADD_FAILURE();
return false;
}

@ -12,13 +12,13 @@ namespace web_app {
FakeEnvironment::FakeEnvironment() = default;
FakeEnvironment::~FakeEnvironment() = default;
void FakeEnvironment::Set(base::cstring_view name, const std::string& value) {
void FakeEnvironment::Set(std::string_view name, const std::string& value) {
const std::string key(name);
variables_[key] = value;
}
std::optional<std::string> FakeEnvironment::GetVar(
base::cstring_view variable_name) {
std::string_view variable_name) {
const std::string key(variable_name);
if (!base::Contains(variables_, key)) {
return std::nullopt;
@ -26,13 +26,13 @@ std::optional<std::string> FakeEnvironment::GetVar(
return variables_[key];
}
bool FakeEnvironment::SetVar(base::cstring_view variable_name,
bool FakeEnvironment::SetVar(std::string_view variable_name,
const std::string& new_value) {
ADD_FAILURE();
return false;
}
bool FakeEnvironment::UnSetVar(base::cstring_view variable_name) {
bool FakeEnvironment::UnSetVar(std::string_view variable_name) {
ADD_FAILURE();
return false;
}

@ -7,9 +7,9 @@
#include <map>
#include <string>
#include <string_view>
#include "base/environment.h"
#include "base/strings/cstring_view.h"
namespace web_app {
@ -21,13 +21,13 @@ class FakeEnvironment : public base::Environment {
FakeEnvironment& operator=(const FakeEnvironment&) = delete;
~FakeEnvironment() override;
void Set(base::cstring_view name, const std::string& value);
void Set(std::string_view name, const std::string& value);
// base::Environment:
std::optional<std::string> GetVar(base::cstring_view variable_name) override;
bool SetVar(base::cstring_view variable_name,
std::optional<std::string> GetVar(std::string_view variable_name) override;
bool SetVar(std::string_view variable_name,
const std::string& new_value) override;
bool UnSetVar(base::cstring_view variable_name) override;
bool UnSetVar(std::string_view variable_name) override;
private:
std::map<std::string, std::string> variables_;

@ -251,7 +251,7 @@ StringMapping GetEnvironmentVariablesMapping(
StringMapping string_mapping;
for (const std::wstring& variable : environment_variables) {
std::optional<std::string> value =
environment->GetVar(base::WideToASCII(variable));
environment->GetVar(base::WideToASCII(variable).c_str());
if (value.has_value()) {
std::string_view trimmed_value =
base::TrimString(value.value(), "\\", base::TRIM_TRAILING);

@ -467,6 +467,7 @@ static_library("non_code_constants") {
"chrome_constants.h",
"chrome_switches.cc",
"chrome_switches.h",
"env_vars.cc",
"env_vars.h",
]
configs += [ "//build/config/compiler:wexit_time_destructors" ]

24
chrome/common/env_vars.cc Normal file

@ -0,0 +1,24 @@
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/env_vars.h"
namespace env_vars {
// We call running in unattended mode (for automated testing) "headless".
// This mode can be enabled using this variable or by the kNoErrorDialogs
// switch.
const char kHeadless[] = "CHROME_HEADLESS";
// The name of the log file.
const char kLogFileName[] = "CHROME_LOG_FILE";
// Flag indicating if metro viewer is connected to browser instance.
// As of now there is only one metro viewer instance per browser.
const char kMetroConnected[] = "CHROME_METRO_CONNECTED";
// The name of the session log directory when logged in to ChromeOS.
const char kSessionLogDir[] = "CHROMEOS_SESSION_LOG_DIR";
} // namespace env_vars

@ -9,20 +9,10 @@
namespace env_vars {
// We call running in unattended mode (for automated testing) "headless".
// This mode can be enabled using this variable or by the kNoErrorDialogs
// switch.
inline constexpr char kHeadless[] = "CHROME_HEADLESS";
// The name of the log file.
inline constexpr char kLogFileName[] = "CHROME_LOG_FILE";
// Flag indicating if metro viewer is connected to browser instance.
// As of now there is only one metro viewer instance per browser.
inline constexpr char kMetroConnected[] = "CHROME_METRO_CONNECTED";
// The name of the session log directory when logged in to ChromeOS.
inline constexpr char kSessionLogDir[] = "CHROMEOS_SESSION_LOG_DIR";
extern const char kHeadless[];
extern const char kLogFileName[];
extern const char kMetroConnected[];
extern const char kSessionLogDir[];
} // namespace env_vars

@ -185,6 +185,15 @@ const char kVerboseLogging[] = "verbose-logging";
} // namespace switches
namespace env_vars {
// The presence of this environment variable with a value of 1 implies that
// setup.exe should run as a system installation regardless of what is on the
// command line.
const char kGoogleUpdateIsMachineEnvVar[] = "GoogleUpdateIsMachine";
} // namespace env_vars
// The Active Setup executable will be an identical copy of setup.exe; this is
// necessary because Windows' installer detection heuristics (which include
// things like process name being "setup.exe") will otherwise force elevation

@ -218,10 +218,7 @@ extern const char kVerboseLogging[];
namespace env_vars {
// The presence of this environment variable with a value of 1 implies that
// setup.exe should run as a system installation regardless of what is on the
// command line.
inline constexpr char kGoogleUpdateIsMachineEnvVar[] = "GoogleUpdateIsMachine";
extern const char kGoogleUpdateIsMachineEnvVar[];
} // namespace env_vars

@ -524,13 +524,16 @@ class SubresourceFilteringRulesetServiceDeathTest
}
private:
static constexpr char kInheritedTempDirKey[] =
"SUBRESOURCE_FILTERING_RULESET_SERVICE_DEATH_TEST_TEMP_DIR";
static const char kInheritedTempDirKey[];
std::unique_ptr<base::Environment> environment_;
base::FilePath inherited_temp_dir_;
};
// static
const char SubresourceFilteringRulesetServiceDeathTest::kInheritedTempDirKey[] =
"SUBRESOURCE_FILTERING_RULESET_SERVICE_DEATH_TEST_TEMP_DIR";
TEST_F(SubresourceFilteringRulesetServiceTest, PathsAreSane) {
IndexedRulesetVersion indexed_version(
kTestContentVersion1, IndexedRulesetVersion::CurrentFormatVersion(),

@ -42,16 +42,15 @@ namespace {
// 5. Baked into the build.
// |command_line_switch| may be NULL. Official Google Chrome builds will not
// use the value provided by an environment variable.
static std::string CalculateKeyValue(
const char* baked_in_value,
base::cstring_view environment_variable_name,
const char* command_line_switch,
const std::string& default_if_unset,
base::Environment* environment,
base::CommandLine* command_line,
GaiaConfig* gaia_config,
bool allow_override_via_environment,
bool allow_unset_values) {
static std::string CalculateKeyValue(const char* baked_in_value,
const char* environment_variable_name,
const char* command_line_switch,
const std::string& default_if_unset,
base::Environment* environment,
base::CommandLine* command_line,
GaiaConfig* gaia_config,
bool allow_override_via_environment,
bool allow_unset_values) {
std::string key_value = baked_in_value;
std::string temp;
#if BUILDFLAG(IS_APPLE)

@ -5,13 +5,14 @@
#ifndef GOOGLE_APIS_GOOGLE_API_KEYS_MAC_H_
#define GOOGLE_APIS_GOOGLE_API_KEYS_MAC_H_
#include <string>
#include "base/component_export.h"
#include "base/strings/cstring_view.h"
namespace google_apis {
COMPONENT_EXPORT(GOOGLE_APIS)
std::string GetAPIKeyFromInfoPlist(base::cstring_view key_name);
std::string GetAPIKeyFromInfoPlist(const std::string& key_name);
} // namespace google_apis

@ -12,7 +12,7 @@
namespace google_apis {
std::string GetAPIKeyFromInfoPlist(base::cstring_view key_name) {
std::string GetAPIKeyFromInfoPlist(const std::string& key_name) {
NSString* keyName = base::SysUTF8ToNSString(key_name);
NSString* keyValue = base::apple::ObjCCast<NSString>(
[base::apple::FrameworkBundle() objectForInfoDictionaryKey:keyName]);

@ -128,7 +128,7 @@ ProxyConfigWithAnnotation GetConfigOrDirect(
ProxyConfigServiceLinux::Delegate::~Delegate() = default;
bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
base::cstring_view variable,
std::string_view variable,
ProxyServer::Scheme scheme,
ProxyChain* result_chain) {
std::optional<std::string> env_value = env_var_getter_->GetVar(variable);
@ -150,7 +150,7 @@ bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
}
bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
base::cstring_view variable,
std::string_view variable,
ProxyChain* result_chain) {
return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
result_chain);
@ -816,7 +816,8 @@ class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter {
void ResolveIndirect(StringSetting key) {
auto it = string_table_.find(key);
if (it != string_table_.end()) {
std::optional<std::string> value = env_var_getter_->GetVar(it->second);
std::optional<std::string> value =
env_var_getter_->GetVar(it->second.c_str());
if (value.has_value() && !value->empty()) {
it->second = value.value();
} else {
@ -830,7 +831,7 @@ class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter {
if (it != strings_table_.end()) {
if (!it->second.empty()) {
std::optional<std::string> value =
env_var_getter_->GetVar(it->second[0]);
env_var_getter_->GetVar(it->second[0].c_str());
if (value.has_value() && !value->empty()) {
AddHostList(key, value.value());
} else {

@ -18,7 +18,6 @@
#include "base/memory/ref_counted.h"
#include "base/notreached.h"
#include "base/observer_list.h"
#include "base/strings/cstring_view.h"
#include "net/base/net_export.h"
#include "net/base/proxy_server.h"
#include "net/proxy_resolution/proxy_config_service.h"
@ -221,11 +220,11 @@ class NET_EXPORT_PRIVATE ProxyConfigServiceLinux : public ProxyConfigService {
// Obtains an environment variable's value. Parses a proxy chain
// specification from it and puts it in result. Returns true if the
// requested variable is defined and the value valid.
bool GetProxyFromEnvVarForScheme(base::cstring_view variable,
bool GetProxyFromEnvVarForScheme(std::string_view variable,
ProxyServer::Scheme scheme,
ProxyChain* result_chain);
// As above but with scheme set to HTTP, for convenience.
bool GetProxyFromEnvVar(base::cstring_view variable,
bool GetProxyFromEnvVar(std::string_view variable,
ProxyChain* result_chain);
// Returns a proxy config based on the environment variables, or empty value
// on failure.

@ -7,6 +7,7 @@
#include <array>
#include <map>
#include <string>
#include <string_view>
#include <vector>
#include "base/check.h"
@ -19,7 +20,6 @@
#include "base/memory/raw_ptr.h"
#include "base/message_loop/message_pump_type.h"
#include "base/run_loop.h"
#include "base/strings/cstring_view.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
@ -139,7 +139,7 @@ class MockEnvironment : public base::Environment {
}
// Begin base::Environment implementation.
std::optional<std::string> GetVar(base::cstring_view variable_name) override {
std::optional<std::string> GetVar(std::string_view variable_name) override {
auto it = table_.find(variable_name);
if (it == table_.end() || !*it->second) {
return std::nullopt;
@ -149,13 +149,13 @@ class MockEnvironment : public base::Environment {
return *(it->second);
}
bool SetVar(base::cstring_view variable_name,
bool SetVar(std::string_view variable_name,
const std::string& new_value) override {
ADD_FAILURE();
return false;
}
bool UnSetVar(base::cstring_view variable_name) override {
bool UnSetVar(std::string_view variable_name) override {
ADD_FAILURE();
return false;
}
@ -165,7 +165,7 @@ class MockEnvironment : public base::Environment {
EnvVarValues values;
private:
std::map<base::cstring_view, const char**> table_;
std::map<std::string_view, const char**> table_;
};
class MockSettingGetter : public ProxyConfigServiceLinux::SettingGetter {

@ -5,12 +5,12 @@
#include "remoting/host/webauthn/remote_webauthn_caller_security_utils.h"
#include <optional>
#include <string_view>
#include "base/environment.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/process/process_handle.h"
#include "base/strings/cstring_view.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
@ -63,7 +63,7 @@ constexpr auto kAllowedCallerPrograms =
// Names of environment variables that store the path to directories where apps
// are installed.
constexpr auto kAppsDirectoryEnvVars =
base::MakeFixedFlatSet<base::cstring_view>({
base::MakeFixedFlatSet<std::string_view>({
"PROGRAMFILES",
// May happen if Chrome is upgraded from a 32-bit version.
@ -144,7 +144,7 @@ bool IsLaunchedByTrustedProcess() {
}
// Check if the caller's image path is allowlisted.
for (base::cstring_view apps_dir_env_var : kAppsDirectoryEnvVars) {
for (std::string_view apps_dir_env_var : kAppsDirectoryEnvVars) {
std::optional<std::string> apps_dir_path_utf8 =
environment->GetVar(apps_dir_env_var);
if (!apps_dir_path_utf8.has_value()) {

@ -18,7 +18,6 @@
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/cstring_view.h"
#include "base/strings/string_number_conversions.h"
#include "sandbox/linux/suid/common/sandbox.h"
@ -46,7 +45,7 @@ int GetHelperApi(base::Environment* env) {
// Convert |var_name| from the environment |env| to an int.
// Return -1 if the variable does not exist or the value cannot be converted.
int EnvToInt(base::Environment* env, base::cstring_view var_name) {
int EnvToInt(base::Environment* env, const char* var_name) {
std::string var_string = env->GetVar(var_name).value_or(std::string());
int var_value = -1;
if (!var_string.empty() && !base::StringToInt(var_string, &var_value)) {

@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
@ -32,7 +33,6 @@
#include "base/posix/eintr_wrapper.h"
#include "base/process/launch.h"
#include "base/process/process_metrics.h"
#include "base/strings/cstring_view.h"
#include "base/strings/string_number_conversions.h"
#include "sandbox/linux/suid/common/sandbox.h"
#include "sandbox/linux/suid/common/suid_unsafe_environment_variables.h"
@ -71,8 +71,8 @@ void UnsetExpectedEnvironmentVariables(base::EnvironmentMap* env_map) {
// Wrapper around a shared C function.
// Returns the "saved" environment variable name corresponding to |envvar|
// in a new string or NULL.
std::string* CreateSavedVariableName(base::cstring_view env_var) {
char* const saved_env_var = SandboxSavedEnvironmentVariable(env_var.c_str());
std::string* CreateSavedVariableName(const char* env_var) {
char* const saved_env_var = SandboxSavedEnvironmentVariable(env_var);
if (!saved_env_var)
return nullptr;
std::string* saved_env_var_copy = new std::string(saved_env_var);
@ -87,7 +87,7 @@ std::string* CreateSavedVariableName(base::cstring_view env_var) {
// renderer.
void SaveSUIDUnsafeEnvironmentVariables(base::Environment* env) {
for (unsigned i = 0; kSUIDUnsafeEnvironmentVariables[i]; ++i) {
const base::cstring_view env_var(kSUIDUnsafeEnvironmentVariables[i]);
const char* env_var = kSUIDUnsafeEnvironmentVariables[i];
// Get the saved environment variable corresponding to envvar.
std::unique_ptr<std::string> saved_env_var(
CreateSavedVariableName(env_var));

@ -9,6 +9,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "base/environment.h"
@ -16,7 +17,6 @@
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/cstring_view.h"
#include "base/strings/stringprintf.h"
#include "sandbox/linux/syscall_broker/broker_command.h"
#include "sandbox/linux/syscall_broker/broker_file_permission.h"
@ -59,7 +59,7 @@ void AddAlsaFilePermissions(std::vector<BrokerFilePermission>* permissions) {
// are specified through environment variables. |recursive_only| is used to
// determine if the path itself should be allowed access or only its content.
void AllowAccessToEnvSpecifiedPath(
base::cstring_view variable_name,
std::string_view variable_name,
std::vector<BrokerFilePermission>* permissions,
bool recursive_only) {
std::unique_ptr<base::Environment> env(base::Environment::Create());
@ -119,17 +119,16 @@ void AddPulseAudioFilePermissions(
// "/tmp/pulse-<random string>".
permissions->push_back(
BrokerFilePermission::ReadWriteCreateRecursive("/tmp/"));
static constexpr base::cstring_view env_tmp_paths[] = {"TMPDIR", "TMP",
"TEMP", "TEMPDIR"};
for (base::cstring_view env_tmp_path : env_tmp_paths) {
const char* env_tmp_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
for (const char* env_tmp_path : env_tmp_paths) {
AllowAccessToEnvSpecifiedPath(env_tmp_path, permissions,
/*recursive_only=*/true);
}
// Read up the Pulse paths specified via environment variable and allow for
// read/write/create recursively on the directory.
static constexpr base::cstring_view env_pulse_paths[] = {
"PULSE_CONFIG_PATH", "PULSE_RUNTIME_PATH", "PULSE_STATE_PATH"};
for (base::cstring_view env_pulse_path : env_pulse_paths) {
const char* env_pulse_paths[] = {"PULSE_CONFIG_PATH", "PULSE_RUNTIME_PATH",
"PULSE_STATE_PATH"};
for (const char* env_pulse_path : env_pulse_paths) {
AllowAccessToEnvSpecifiedPath(env_pulse_path, permissions,
/*recursive_only=*/false);
}

@ -2,6 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <atk/atk.h>
#include <array>
@ -27,12 +32,11 @@
namespace {
constexpr auto kAccessibilityEnabledVariables =
std::to_array<base::cstring_view>({
"ACCESSIBILITY_ENABLED",
"GNOME_ACCESSIBILITY",
"QT_ACCESSIBILITY",
});
auto kAccessibilityEnabledVariables = std::to_array<const char*>({
"ACCESSIBILITY_ENABLED",
"GNOME_ACCESSIBILITY",
"QT_ACCESSIBILITY",
});
//
// AtkUtilAuraLinux definition and implementation.
@ -45,10 +49,7 @@ struct AtkUtilAuraLinuxClass {
AtkUtilClass parent_class;
};
// SAFETY: Usage of third-party library macro is outside our control.
UNSAFE_BUFFERS(G_DEFINE_TYPE(AtkUtilAuraLinux,
atk_util_auralinux,
ATK_TYPE_UTIL))
G_DEFINE_TYPE(AtkUtilAuraLinux, atk_util_auralinux, ATK_TYPE_UTIL)
static void atk_util_auralinux_init(AtkUtilAuraLinux *ax_util) {
}
@ -125,7 +126,7 @@ AtkUtilAuraLinux* AtkUtilAuraLinux::GetInstance() {
bool AtkUtilAuraLinux::ShouldEnableAccessibility() {
// Check enabled/disabled accessibility based on env variable
std::unique_ptr<base::Environment> env(base::Environment::Create());
for (base::cstring_view variable : kAccessibilityEnabledVariables) {
for (const auto* variable : kAccessibilityEnabledVariables) {
std::string enable_accessibility =
env->GetVar(variable).value_or(std::string());
if (enable_accessibility == "1")