Browse Source

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

Jeremy Rose 2 years ago
parent
commit
07294cbf15

+ 1 - 1
shell/browser/api/electron_api_app.cc

@@ -1443,7 +1443,7 @@ v8::Local<v8::Value> App::GetGPUFeatureStatus(v8::Isolate* isolate) {
 v8::Local<v8::Promise> App::GetGPUInfo(v8::Isolate* isolate,
                                        const std::string& info_type) {
   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();
   if (info_type != "basic" && info_type != "complete") {
     promise.RejectWithErrorMessage(

+ 55 - 57
shell/browser/api/gpu_info_enumerator.cc

@@ -8,127 +8,125 @@
 
 namespace electron {
 
-GPUInfoEnumerator::GPUInfoEnumerator()
-    : value_stack(), current(std::make_unique<base::DictionaryValue>()) {}
+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<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) {
   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<const uint8_t>& value) {
-  current->Set(name, std::make_unique<base::Value>(value));
+  current_.Set(name, base::Value(value));
 }
 
 void GPUInfoEnumerator::BeginGPUDevice() {
-  value_stack.push(std::move(current));
-  current = std::make_unique<base::DictionaryValue>();
+  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<base::ListValue> 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<base::DictionaryValue>();
+  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<base::DictionaryValue>();
+  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<base::DictionaryValue>();
+  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<base::DictionaryValue>();
+  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<base::DictionaryValue>();
+  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<base::DictionaryValue> GPUInfoEnumerator::GetDictionary() {
-  return std::move(current);
+base::Value::Dict GPUInfoEnumerator::GetDictionary() {
+  return std::move(current_);
 }
 
 }  // namespace electron

+ 3 - 3
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<base::DictionaryValue> GetDictionary();
+  base::Value::Dict GetDictionary();
 
  private:
   // The stack is used to manage nested values
-  std::stack<std::unique_ptr<base::DictionaryValue>> value_stack;
-  std::unique_ptr<base::DictionaryValue> current;
+  std::stack<base::Value::Dict> value_stack_;
+  base::Value::Dict current_;
 };
 
 }  // namespace electron

+ 7 - 8
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<base::DictionaryValue> promise) {
+    gin_helper::Promise<base::Value> 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<base::DictionaryValue> promise) {
+    gin_helper::Promise<base::Value> 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<base::DictionaryValue> promise) {
+void GPUInfoManager::FetchBasicInfo(gin_helper::Promise<base::Value> promise) {
   gpu::GPUInfo 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 {
   GPUInfoEnumerator enumerator;
   gpu_info.EnumerateFields(&enumerator);

+ 5 - 7
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<base::DictionaryValue> promise);
-  void FetchBasicInfo(gin_helper::Promise<base::DictionaryValue> promise);
+  void FetchCompleteInfo(gin_helper::Promise<base::Value> promise);
+  void FetchBasicInfo(gin_helper::Promise<base::Value> promise);
   void OnGpuInfoUpdate() override;
 
  private:
-  std::unique_ptr<base::DictionaryValue> 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<base::DictionaryValue> promise);
+  void CompleteInfoFetcher(gin_helper::Promise<base::Value> promise);
   void ProcessCompleteInfo();
 
   // This set maintains all the promises that should be fulfilled
   // once we have the complete information data
-  std::vector<gin_helper::Promise<base::DictionaryValue>>
-      complete_info_promise_set_;
+  std::vector<gin_helper::Promise<base::Value>> complete_info_promise_set_;
   content::GpuDataManagerImpl* gpu_data_manager_;
 };