chore: modernize ListValue usage in gpu info (#34663)

This commit is contained in:
Jeremy Rose 2022-06-28 09:52:59 -07:00 committed by GitHub
parent a4043237da
commit 07294cbf15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 76 deletions

View file

@ -1443,7 +1443,7 @@ v8::Local<v8::Value> App::GetGPUFeatureStatus(v8::Isolate* isolate) {
v8::Local<v8::Promise> App::GetGPUInfo(v8::Isolate* isolate, v8::Local<v8::Promise> App::GetGPUInfo(v8::Isolate* isolate,
const std::string& info_type) { const std::string& info_type) {
auto* const gpu_data_manager = content::GpuDataManagerImpl::GetInstance(); auto* const gpu_data_manager = content::GpuDataManagerImpl::GetInstance();
gin_helper::Promise<base::DictionaryValue> promise(isolate); gin_helper::Promise<base::Value> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle(); v8::Local<v8::Promise> handle = promise.GetHandle();
if (info_type != "basic" && info_type != "complete") { if (info_type != "basic" && info_type != "complete") {
promise.RejectWithErrorMessage( promise.RejectWithErrorMessage(

View file

@ -8,127 +8,125 @@
namespace electron { namespace electron {
GPUInfoEnumerator::GPUInfoEnumerator() GPUInfoEnumerator::GPUInfoEnumerator() : value_stack_(), current_{} {}
: value_stack(), current(std::make_unique<base::DictionaryValue>()) {}
GPUInfoEnumerator::~GPUInfoEnumerator() = default; GPUInfoEnumerator::~GPUInfoEnumerator() = default;
void GPUInfoEnumerator::AddInt64(const char* name, int64_t value) { void GPUInfoEnumerator::AddInt64(const char* name, int64_t value) {
current->SetInteger(name, value); // NOTE(nornagon): this loses precision. base::Value can't store int64_t.
current_.Set(name, static_cast<int>(value));
} }
void GPUInfoEnumerator::AddInt(const char* name, int value) { void GPUInfoEnumerator::AddInt(const char* name, int value) {
current->SetInteger(name, value); current_.Set(name, value);
} }
void GPUInfoEnumerator::AddString(const char* name, const std::string& value) { void GPUInfoEnumerator::AddString(const char* name, const std::string& value) {
if (!value.empty()) if (!value.empty())
current->SetString(name, value); current_.Set(name, value);
} }
void GPUInfoEnumerator::AddBool(const char* name, bool value) { void GPUInfoEnumerator::AddBool(const char* name, bool value) {
current->SetBoolean(name, value); current_.Set(name, value);
} }
void GPUInfoEnumerator::AddTimeDeltaInSecondsF(const char* name, void GPUInfoEnumerator::AddTimeDeltaInSecondsF(const char* name,
const base::TimeDelta& value) { const base::TimeDelta& value) {
current->SetInteger(name, value.InMilliseconds()); current_.Set(name, value.InMillisecondsF());
} }
void GPUInfoEnumerator::AddBinary(const char* name, void GPUInfoEnumerator::AddBinary(const char* name,
const base::span<const uint8_t>& value) { const base::span<const uint8_t>& value) {
current->Set(name, std::make_unique<base::Value>(value)); current_.Set(name, base::Value(value));
} }
void GPUInfoEnumerator::BeginGPUDevice() { void GPUInfoEnumerator::BeginGPUDevice() {
value_stack.push(std::move(current)); value_stack_.push(std::move(current_));
current = std::make_unique<base::DictionaryValue>(); current_ = {};
} }
void GPUInfoEnumerator::EndGPUDevice() { void GPUInfoEnumerator::EndGPUDevice() {
auto& top_value = value_stack.top(); auto& top_value = value_stack_.top();
// GPUDevice can be more than one. So create a list of all. // GPUDevice can be more than one. So create a list of all.
// The first one is the active GPU device. // The first one is the active GPU device.
if (top_value->FindKey(kGPUDeviceKey)) { if (base::Value* list_value = top_value.Find(kGPUDeviceKey)) {
base::ListValue* list; DCHECK(list_value->is_list());
top_value->GetList(kGPUDeviceKey, &list); base::Value::List& list = list_value->GetList();
list->Append(base::Value::FromUniquePtrValue(std::move(current))); list.Append(std::move(current_));
} else { } else {
std::unique_ptr<base::ListValue> gpus(new base::ListValue()); base::Value::List gpus;
gpus->Append(base::Value::FromUniquePtrValue(std::move(current))); gpus.Append(std::move(current_));
top_value->SetList(kGPUDeviceKey, std::move(gpus)); top_value.Set(kGPUDeviceKey, std::move(gpus));
} }
current = std::move(top_value); current_ = std::move(top_value);
value_stack.pop(); value_stack_.pop();
} }
void GPUInfoEnumerator::BeginVideoDecodeAcceleratorSupportedProfile() { void GPUInfoEnumerator::BeginVideoDecodeAcceleratorSupportedProfile() {
value_stack.push(std::move(current)); value_stack_.push(std::move(current_));
current = std::make_unique<base::DictionaryValue>(); current_ = {};
} }
void GPUInfoEnumerator::EndVideoDecodeAcceleratorSupportedProfile() { void GPUInfoEnumerator::EndVideoDecodeAcceleratorSupportedProfile() {
auto& top_value = value_stack.top(); auto& top_value = value_stack_.top();
top_value->SetKey(kVideoDecodeAcceleratorSupportedProfileKey, top_value.Set(kVideoDecodeAcceleratorSupportedProfileKey,
base::Value::FromUniquePtrValue(std::move(current))); std::move(current_));
current = std::move(top_value); current_ = std::move(top_value);
value_stack.pop(); value_stack_.pop();
} }
void GPUInfoEnumerator::BeginVideoEncodeAcceleratorSupportedProfile() { void GPUInfoEnumerator::BeginVideoEncodeAcceleratorSupportedProfile() {
value_stack.push(std::move(current)); value_stack_.push(std::move(current_));
current = std::make_unique<base::DictionaryValue>(); current_ = {};
} }
void GPUInfoEnumerator::EndVideoEncodeAcceleratorSupportedProfile() { void GPUInfoEnumerator::EndVideoEncodeAcceleratorSupportedProfile() {
auto& top_value = value_stack.top(); auto& top_value = value_stack_.top();
top_value->SetKey(kVideoEncodeAcceleratorSupportedProfileKey, top_value.Set(kVideoEncodeAcceleratorSupportedProfileKey,
base::Value::FromUniquePtrValue(std::move(current))); std::move(current_));
current = std::move(top_value); current_ = std::move(top_value);
value_stack.pop(); value_stack_.pop();
} }
void GPUInfoEnumerator::BeginImageDecodeAcceleratorSupportedProfile() { void GPUInfoEnumerator::BeginImageDecodeAcceleratorSupportedProfile() {
value_stack.push(std::move(current)); value_stack_.push(std::move(current_));
current = std::make_unique<base::DictionaryValue>(); current_ = {};
} }
void GPUInfoEnumerator::EndImageDecodeAcceleratorSupportedProfile() { void GPUInfoEnumerator::EndImageDecodeAcceleratorSupportedProfile() {
auto& top_value = value_stack.top(); auto& top_value = value_stack_.top();
top_value->SetKey(kImageDecodeAcceleratorSupportedProfileKey, top_value.Set(kImageDecodeAcceleratorSupportedProfileKey,
base::Value::FromUniquePtrValue(std::move(current))); std::move(current_));
current = std::move(top_value); current_ = std::move(top_value);
value_stack.pop(); value_stack_.pop();
} }
void GPUInfoEnumerator::BeginAuxAttributes() { void GPUInfoEnumerator::BeginAuxAttributes() {
value_stack.push(std::move(current)); value_stack_.push(std::move(current_));
current = std::make_unique<base::DictionaryValue>(); current_ = {};
} }
void GPUInfoEnumerator::EndAuxAttributes() { void GPUInfoEnumerator::EndAuxAttributes() {
auto& top_value = value_stack.top(); auto& top_value = value_stack_.top();
top_value->SetKey(kAuxAttributesKey, top_value.Set(kAuxAttributesKey, std::move(current_));
base::Value::FromUniquePtrValue(std::move(current))); current_ = std::move(top_value);
current = std::move(top_value); value_stack_.pop();
value_stack.pop();
} }
void GPUInfoEnumerator::BeginOverlayInfo() { void GPUInfoEnumerator::BeginOverlayInfo() {
value_stack.push(std::move(current)); value_stack_.push(std::move(current_));
current = std::make_unique<base::DictionaryValue>(); current_ = {};
} }
void GPUInfoEnumerator::EndOverlayInfo() { void GPUInfoEnumerator::EndOverlayInfo() {
auto& top_value = value_stack.top(); auto& top_value = value_stack_.top();
top_value->SetKey(kOverlayInfo, top_value.Set(kOverlayInfo, std::move(current_));
base::Value::FromUniquePtrValue(std::move(current))); current_ = std::move(top_value);
current = std::move(top_value); value_stack_.pop();
value_stack.pop();
} }
std::unique_ptr<base::DictionaryValue> GPUInfoEnumerator::GetDictionary() { base::Value::Dict GPUInfoEnumerator::GetDictionary() {
return std::move(current); return std::move(current_);
} }
} // namespace electron } // namespace electron

View file

@ -50,12 +50,12 @@ class GPUInfoEnumerator final : public gpu::GPUInfo::Enumerator {
void EndAuxAttributes() override; void EndAuxAttributes() override;
void BeginOverlayInfo() override; void BeginOverlayInfo() override;
void EndOverlayInfo() override; void EndOverlayInfo() override;
std::unique_ptr<base::DictionaryValue> GetDictionary(); base::Value::Dict GetDictionary();
private: private:
// The stack is used to manage nested values // The stack is used to manage nested values
std::stack<std::unique_ptr<base::DictionaryValue>> value_stack; std::stack<base::Value::Dict> value_stack_;
std::unique_ptr<base::DictionaryValue> current; base::Value::Dict current_;
}; };
} // namespace electron } // namespace electron

View file

@ -41,11 +41,11 @@ bool GPUInfoManager::NeedsCompleteGpuInfoCollection() const {
// Should be posted to the task runner // Should be posted to the task runner
void GPUInfoManager::ProcessCompleteInfo() { void GPUInfoManager::ProcessCompleteInfo() {
const auto result = EnumerateGPUInfo(gpu_data_manager_->GetGPUInfo()); base::Value::Dict result = EnumerateGPUInfo(gpu_data_manager_->GetGPUInfo());
// We have received the complete information, resolve all promises that // We have received the complete information, resolve all promises that
// were waiting for this info. // were waiting for this info.
for (auto& promise : complete_info_promise_set_) { for (auto& promise : complete_info_promise_set_) {
promise.Resolve(*result); promise.Resolve(base::Value(result.Clone()));
} }
complete_info_promise_set_.clear(); complete_info_promise_set_.clear();
} }
@ -61,7 +61,7 @@ void GPUInfoManager::OnGpuInfoUpdate() {
// Should be posted to the task runner // Should be posted to the task runner
void GPUInfoManager::CompleteInfoFetcher( void GPUInfoManager::CompleteInfoFetcher(
gin_helper::Promise<base::DictionaryValue> promise) { gin_helper::Promise<base::Value> promise) {
complete_info_promise_set_.emplace_back(std::move(promise)); complete_info_promise_set_.emplace_back(std::move(promise));
if (NeedsCompleteGpuInfoCollection()) { if (NeedsCompleteGpuInfoCollection()) {
@ -73,7 +73,7 @@ void GPUInfoManager::CompleteInfoFetcher(
} }
void GPUInfoManager::FetchCompleteInfo( void GPUInfoManager::FetchCompleteInfo(
gin_helper::Promise<base::DictionaryValue> promise) { gin_helper::Promise<base::Value> promise) {
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&GPUInfoManager::CompleteInfoFetcher, FROM_HERE, base::BindOnce(&GPUInfoManager::CompleteInfoFetcher,
base::Unretained(this), std::move(promise))); base::Unretained(this), std::move(promise)));
@ -81,14 +81,13 @@ void GPUInfoManager::FetchCompleteInfo(
// This fetches the info synchronously, so no need to post to the task queue. // This fetches the info synchronously, so no need to post to the task queue.
// There cannot be multiple promises as they are resolved synchronously. // There cannot be multiple promises as they are resolved synchronously.
void GPUInfoManager::FetchBasicInfo( void GPUInfoManager::FetchBasicInfo(gin_helper::Promise<base::Value> promise) {
gin_helper::Promise<base::DictionaryValue> promise) {
gpu::GPUInfo gpu_info; gpu::GPUInfo gpu_info;
CollectBasicGraphicsInfo(&gpu_info); CollectBasicGraphicsInfo(&gpu_info);
promise.Resolve(*EnumerateGPUInfo(gpu_info)); promise.Resolve(base::Value(EnumerateGPUInfo(gpu_info)));
} }
std::unique_ptr<base::DictionaryValue> GPUInfoManager::EnumerateGPUInfo( base::Value::Dict GPUInfoManager::EnumerateGPUInfo(
gpu::GPUInfo gpu_info) const { gpu::GPUInfo gpu_info) const {
GPUInfoEnumerator enumerator; GPUInfoEnumerator enumerator;
gpu_info.EnumerateFields(&enumerator); gpu_info.EnumerateFields(&enumerator);

View file

@ -28,22 +28,20 @@ class GPUInfoManager : public content::GpuDataManagerObserver {
GPUInfoManager& operator=(const GPUInfoManager&) = delete; GPUInfoManager& operator=(const GPUInfoManager&) = delete;
bool NeedsCompleteGpuInfoCollection() const; bool NeedsCompleteGpuInfoCollection() const;
void FetchCompleteInfo(gin_helper::Promise<base::DictionaryValue> promise); void FetchCompleteInfo(gin_helper::Promise<base::Value> promise);
void FetchBasicInfo(gin_helper::Promise<base::DictionaryValue> promise); void FetchBasicInfo(gin_helper::Promise<base::Value> promise);
void OnGpuInfoUpdate() override; void OnGpuInfoUpdate() override;
private: private:
std::unique_ptr<base::DictionaryValue> EnumerateGPUInfo( base::Value::Dict EnumerateGPUInfo(gpu::GPUInfo gpu_info) const;
gpu::GPUInfo gpu_info) const;
// These should be posted to the task queue // These should be posted to the task queue
void CompleteInfoFetcher(gin_helper::Promise<base::DictionaryValue> promise); void CompleteInfoFetcher(gin_helper::Promise<base::Value> promise);
void ProcessCompleteInfo(); void ProcessCompleteInfo();
// This set maintains all the promises that should be fulfilled // This set maintains all the promises that should be fulfilled
// once we have the complete information data // once we have the complete information data
std::vector<gin_helper::Promise<base::DictionaryValue>> std::vector<gin_helper::Promise<base::Value>> complete_info_promise_set_;
complete_info_promise_set_;
content::GpuDataManagerImpl* gpu_data_manager_; content::GpuDataManagerImpl* gpu_data_manager_;
}; };