refactor: in StopTracing(), use string literals instead of optional<string> (#45291)

refactor: simplify StopTracing() a little by using a string_view instead of an optional<string>

We have compile-time string literals that we're passing to a method
that takes a string_view argument, so we don't need all this extra
optional<string> scaffolding

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-01-22 09:43:19 -06:00 committed by GitHub
parent a1816f3587
commit 4a1c029f94
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,6 +5,7 @@
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include "base/files/file_util.h"
@ -20,6 +21,7 @@
#include "shell/common/node_includes.h"
using content::TracingController;
using namespace std::literals;
namespace gin {
@ -69,9 +71,9 @@ void StopTracing(gin_helper::Promise<base::FilePath> promise,
std::optional<base::FilePath> file_path) {
auto resolve_or_reject = base::BindOnce(
[](gin_helper::Promise<base::FilePath> promise,
const base::FilePath& path, std::optional<std::string> error) {
if (error) {
promise.RejectWithErrorMessage(error.value());
const base::FilePath& path, const std::string_view error) {
if (!std::empty(error)) {
promise.RejectWithErrorMessage(error);
} else {
promise.Resolve(path);
}
@ -81,21 +83,17 @@ void StopTracing(gin_helper::Promise<base::FilePath> promise,
auto* instance = TracingController::GetInstance();
if (!instance->IsTracing()) {
std::move(resolve_or_reject)
.Run(std::make_optional(
"Failed to stop tracing - no trace in progress"));
.Run("Failed to stop tracing - no trace in progress"sv);
} else if (file_path) {
auto split_callback = base::SplitOnceCallback(std::move(resolve_or_reject));
auto endpoint = TracingController::CreateFileEndpoint(
*file_path,
base::BindOnce(std::move(split_callback.first), std::nullopt));
*file_path, base::BindOnce(std::move(split_callback.first), ""sv));
if (!instance->StopTracing(endpoint)) {
std::move(split_callback.second)
.Run(std::make_optional("Failed to stop tracing"));
std::move(split_callback.second).Run("Failed to stop tracing"sv);
}
} else {
std::move(resolve_or_reject)
.Run(std::make_optional(
"Failed to create temporary file for trace data"));
.Run("Failed to create temporary file for trace data"sv);
}
}