From 07294cbf15a94a6150877b67178911273cb022cf Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Tue, 28 Jun 2022 09:52:59 -0700 Subject: [PATCH] chore: modernize ListValue usage in gpu info (#34663) --- shell/browser/api/electron_api_app.cc | 2 +- shell/browser/api/gpu_info_enumerator.cc | 112 +++++++++++------------ shell/browser/api/gpu_info_enumerator.h | 6 +- shell/browser/api/gpuinfo_manager.cc | 15 ++- shell/browser/api/gpuinfo_manager.h | 12 +-- 5 files changed, 71 insertions(+), 76 deletions(-) diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index 77281437ef32..e4fc3989f73f 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -1443,7 +1443,7 @@ v8::Local App::GetGPUFeatureStatus(v8::Isolate* isolate) { v8::Local App::GetGPUInfo(v8::Isolate* isolate, const std::string& info_type) { auto* const gpu_data_manager = content::GpuDataManagerImpl::GetInstance(); - gin_helper::Promise promise(isolate); + gin_helper::Promise promise(isolate); v8::Local handle = promise.GetHandle(); if (info_type != "basic" && info_type != "complete") { promise.RejectWithErrorMessage( diff --git a/shell/browser/api/gpu_info_enumerator.cc b/shell/browser/api/gpu_info_enumerator.cc index 23b2ccd345a4..57c3448068aa 100644 --- a/shell/browser/api/gpu_info_enumerator.cc +++ b/shell/browser/api/gpu_info_enumerator.cc @@ -8,127 +8,125 @@ namespace electron { -GPUInfoEnumerator::GPUInfoEnumerator() - : value_stack(), current(std::make_unique()) {} +GPUInfoEnumerator::GPUInfoEnumerator() : value_stack_(), current_{} {} GPUInfoEnumerator::~GPUInfoEnumerator() = default; 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(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) { if (!value.empty()) - current->SetString(name, value); + current_.Set(name, value); } void GPUInfoEnumerator::AddBool(const char* name, bool value) { - current->SetBoolean(name, value); + current_.Set(name, value); } void GPUInfoEnumerator::AddTimeDeltaInSecondsF(const char* name, const base::TimeDelta& value) { - current->SetInteger(name, value.InMilliseconds()); + current_.Set(name, value.InMillisecondsF()); } void GPUInfoEnumerator::AddBinary(const char* name, const base::span& value) { - current->Set(name, std::make_unique(value)); + current_.Set(name, base::Value(value)); } void GPUInfoEnumerator::BeginGPUDevice() { - value_stack.push(std::move(current)); - current = std::make_unique(); + value_stack_.push(std::move(current_)); + current_ = {}; } 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. // The first one is the active GPU device. - if (top_value->FindKey(kGPUDeviceKey)) { - base::ListValue* list; - top_value->GetList(kGPUDeviceKey, &list); - list->Append(base::Value::FromUniquePtrValue(std::move(current))); + if (base::Value* list_value = top_value.Find(kGPUDeviceKey)) { + DCHECK(list_value->is_list()); + base::Value::List& list = list_value->GetList(); + list.Append(std::move(current_)); } else { - std::unique_ptr gpus(new base::ListValue()); - gpus->Append(base::Value::FromUniquePtrValue(std::move(current))); - top_value->SetList(kGPUDeviceKey, std::move(gpus)); + base::Value::List gpus; + gpus.Append(std::move(current_)); + top_value.Set(kGPUDeviceKey, std::move(gpus)); } - current = std::move(top_value); - value_stack.pop(); + current_ = std::move(top_value); + value_stack_.pop(); } void GPUInfoEnumerator::BeginVideoDecodeAcceleratorSupportedProfile() { - value_stack.push(std::move(current)); - current = std::make_unique(); + value_stack_.push(std::move(current_)); + current_ = {}; } void GPUInfoEnumerator::EndVideoDecodeAcceleratorSupportedProfile() { - auto& top_value = value_stack.top(); - top_value->SetKey(kVideoDecodeAcceleratorSupportedProfileKey, - base::Value::FromUniquePtrValue(std::move(current))); - current = std::move(top_value); - value_stack.pop(); + auto& top_value = value_stack_.top(); + top_value.Set(kVideoDecodeAcceleratorSupportedProfileKey, + std::move(current_)); + current_ = std::move(top_value); + value_stack_.pop(); } void GPUInfoEnumerator::BeginVideoEncodeAcceleratorSupportedProfile() { - value_stack.push(std::move(current)); - current = std::make_unique(); + value_stack_.push(std::move(current_)); + current_ = {}; } void GPUInfoEnumerator::EndVideoEncodeAcceleratorSupportedProfile() { - auto& top_value = value_stack.top(); - top_value->SetKey(kVideoEncodeAcceleratorSupportedProfileKey, - base::Value::FromUniquePtrValue(std::move(current))); - current = std::move(top_value); - value_stack.pop(); + auto& top_value = value_stack_.top(); + top_value.Set(kVideoEncodeAcceleratorSupportedProfileKey, + std::move(current_)); + current_ = std::move(top_value); + value_stack_.pop(); } void GPUInfoEnumerator::BeginImageDecodeAcceleratorSupportedProfile() { - value_stack.push(std::move(current)); - current = std::make_unique(); + value_stack_.push(std::move(current_)); + current_ = {}; } void GPUInfoEnumerator::EndImageDecodeAcceleratorSupportedProfile() { - auto& top_value = value_stack.top(); - top_value->SetKey(kImageDecodeAcceleratorSupportedProfileKey, - base::Value::FromUniquePtrValue(std::move(current))); - current = std::move(top_value); - value_stack.pop(); + auto& top_value = value_stack_.top(); + top_value.Set(kImageDecodeAcceleratorSupportedProfileKey, + std::move(current_)); + current_ = std::move(top_value); + value_stack_.pop(); } void GPUInfoEnumerator::BeginAuxAttributes() { - value_stack.push(std::move(current)); - current = std::make_unique(); + value_stack_.push(std::move(current_)); + current_ = {}; } void GPUInfoEnumerator::EndAuxAttributes() { - auto& top_value = value_stack.top(); - top_value->SetKey(kAuxAttributesKey, - base::Value::FromUniquePtrValue(std::move(current))); - current = std::move(top_value); - value_stack.pop(); + auto& top_value = value_stack_.top(); + top_value.Set(kAuxAttributesKey, std::move(current_)); + current_ = std::move(top_value); + value_stack_.pop(); } void GPUInfoEnumerator::BeginOverlayInfo() { - value_stack.push(std::move(current)); - current = std::make_unique(); + value_stack_.push(std::move(current_)); + current_ = {}; } void GPUInfoEnumerator::EndOverlayInfo() { - auto& top_value = value_stack.top(); - top_value->SetKey(kOverlayInfo, - base::Value::FromUniquePtrValue(std::move(current))); - current = std::move(top_value); - value_stack.pop(); + auto& top_value = value_stack_.top(); + top_value.Set(kOverlayInfo, std::move(current_)); + current_ = std::move(top_value); + value_stack_.pop(); } -std::unique_ptr GPUInfoEnumerator::GetDictionary() { - return std::move(current); +base::Value::Dict GPUInfoEnumerator::GetDictionary() { + return std::move(current_); } } // namespace electron diff --git a/shell/browser/api/gpu_info_enumerator.h b/shell/browser/api/gpu_info_enumerator.h index 389aa9d604d4..bd1af0799cec 100644 --- a/shell/browser/api/gpu_info_enumerator.h +++ b/shell/browser/api/gpu_info_enumerator.h @@ -50,12 +50,12 @@ class GPUInfoEnumerator final : public gpu::GPUInfo::Enumerator { void EndAuxAttributes() override; void BeginOverlayInfo() override; void EndOverlayInfo() override; - std::unique_ptr GetDictionary(); + base::Value::Dict GetDictionary(); private: // The stack is used to manage nested values - std::stack> value_stack; - std::unique_ptr current; + std::stack value_stack_; + base::Value::Dict current_; }; } // namespace electron diff --git a/shell/browser/api/gpuinfo_manager.cc b/shell/browser/api/gpuinfo_manager.cc index d797d0ec4a6d..e4bcf0dcad0c 100644 --- a/shell/browser/api/gpuinfo_manager.cc +++ b/shell/browser/api/gpuinfo_manager.cc @@ -41,11 +41,11 @@ bool GPUInfoManager::NeedsCompleteGpuInfoCollection() const { // Should be posted to the task runner 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 // were waiting for this info. for (auto& promise : complete_info_promise_set_) { - promise.Resolve(*result); + promise.Resolve(base::Value(result.Clone())); } complete_info_promise_set_.clear(); } @@ -61,7 +61,7 @@ void GPUInfoManager::OnGpuInfoUpdate() { // Should be posted to the task runner void GPUInfoManager::CompleteInfoFetcher( - gin_helper::Promise promise) { + gin_helper::Promise promise) { complete_info_promise_set_.emplace_back(std::move(promise)); if (NeedsCompleteGpuInfoCollection()) { @@ -73,7 +73,7 @@ void GPUInfoManager::CompleteInfoFetcher( } void GPUInfoManager::FetchCompleteInfo( - gin_helper::Promise promise) { + gin_helper::Promise promise) { base::ThreadTaskRunnerHandle::Get()->PostTask( FROM_HERE, base::BindOnce(&GPUInfoManager::CompleteInfoFetcher, 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. // There cannot be multiple promises as they are resolved synchronously. -void GPUInfoManager::FetchBasicInfo( - gin_helper::Promise promise) { +void GPUInfoManager::FetchBasicInfo(gin_helper::Promise promise) { gpu::GPUInfo gpu_info; CollectBasicGraphicsInfo(&gpu_info); - promise.Resolve(*EnumerateGPUInfo(gpu_info)); + promise.Resolve(base::Value(EnumerateGPUInfo(gpu_info))); } -std::unique_ptr GPUInfoManager::EnumerateGPUInfo( +base::Value::Dict GPUInfoManager::EnumerateGPUInfo( gpu::GPUInfo gpu_info) const { GPUInfoEnumerator enumerator; gpu_info.EnumerateFields(&enumerator); diff --git a/shell/browser/api/gpuinfo_manager.h b/shell/browser/api/gpuinfo_manager.h index 43357ececc22..8b134e50bf97 100644 --- a/shell/browser/api/gpuinfo_manager.h +++ b/shell/browser/api/gpuinfo_manager.h @@ -28,22 +28,20 @@ class GPUInfoManager : public content::GpuDataManagerObserver { GPUInfoManager& operator=(const GPUInfoManager&) = delete; bool NeedsCompleteGpuInfoCollection() const; - void FetchCompleteInfo(gin_helper::Promise promise); - void FetchBasicInfo(gin_helper::Promise promise); + void FetchCompleteInfo(gin_helper::Promise promise); + void FetchBasicInfo(gin_helper::Promise promise); void OnGpuInfoUpdate() override; private: - std::unique_ptr EnumerateGPUInfo( - gpu::GPUInfo gpu_info) const; + base::Value::Dict EnumerateGPUInfo(gpu::GPUInfo gpu_info) const; // These should be posted to the task queue - void CompleteInfoFetcher(gin_helper::Promise promise); + void CompleteInfoFetcher(gin_helper::Promise promise); void ProcessCompleteInfo(); // This set maintains all the promises that should be fulfilled // once we have the complete information data - std::vector> - complete_info_promise_set_; + std::vector> complete_info_promise_set_; content::GpuDataManagerImpl* gpu_data_manager_; };