Home | History | Annotate | Download | only in devtools
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/browser/devtools/devtools_system_info_handler.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/callback.h"
      9 #include "base/values.h"
     10 #include "content/browser/devtools/devtools_protocol_constants.h"
     11 #include "content/browser/gpu/compositor_util.h"
     12 #include "content/browser/gpu/gpu_data_manager_impl.h"
     13 #include "gpu/config/gpu_info.h"
     14 
     15 namespace content {
     16 
     17 namespace {
     18 
     19 const char kAuxAttributes[] = "auxAttributes";
     20 const char kDeviceId[] = "deviceId";
     21 const char kDeviceString[] = "deviceString";
     22 const char kDevices[] = "devices";
     23 const char kFeatureStatus[] = "featureStatus";
     24 const char kGPU[] = "gpu";
     25 const char kModelName[] = "modelName";
     26 const char kVendorId[] = "vendorId";
     27 const char kVendorString[] = "vendorString";
     28 
     29 class AuxGPUInfoEnumerator : public gpu::GPUInfo::Enumerator {
     30  public:
     31   AuxGPUInfoEnumerator(base::DictionaryValue* dictionary)
     32       : dictionary_(dictionary),
     33         in_aux_attributes_(false) { }
     34 
     35   virtual void AddInt64(const char* name, int64 value) OVERRIDE {
     36     if (in_aux_attributes_)
     37       dictionary_->SetDouble(name, value);
     38   }
     39 
     40   virtual void AddInt(const char* name, int value) OVERRIDE {
     41     if (in_aux_attributes_)
     42       dictionary_->SetInteger(name, value);
     43   }
     44 
     45   virtual void AddString(const char* name, const std::string& value) OVERRIDE {
     46     if (in_aux_attributes_)
     47       dictionary_->SetString(name, value);
     48   }
     49 
     50   virtual void AddBool(const char* name, bool value) OVERRIDE {
     51     if (in_aux_attributes_)
     52       dictionary_->SetBoolean(name, value);
     53   }
     54 
     55   virtual void AddTimeDeltaInSecondsF(const char* name,
     56                                       const base::TimeDelta& value) OVERRIDE {
     57     if (in_aux_attributes_)
     58       dictionary_->SetDouble(name, value.InSecondsF());
     59   }
     60 
     61   virtual void BeginGPUDevice() OVERRIDE {
     62   }
     63 
     64   virtual void EndGPUDevice() OVERRIDE {
     65   }
     66 
     67   virtual void BeginAuxAttributes() OVERRIDE {
     68     in_aux_attributes_ = true;
     69   }
     70 
     71   virtual void EndAuxAttributes() OVERRIDE {
     72     in_aux_attributes_ = false;
     73   }
     74 
     75  private:
     76   base::DictionaryValue* dictionary_;
     77   bool in_aux_attributes_;
     78 };
     79 
     80 base::DictionaryValue* GPUDeviceToDictionary(
     81     const gpu::GPUInfo::GPUDevice& device) {
     82   base::DictionaryValue* result = new base::DictionaryValue;
     83   result->SetInteger(kVendorId, device.vendor_id);
     84   result->SetInteger(kDeviceId, device.device_id);
     85   result->SetString(kVendorString, device.vendor_string);
     86   result->SetString(kDeviceString, device.device_string);
     87   return result;
     88 }
     89 
     90 }  // namespace
     91 
     92 DevToolsSystemInfoHandler::DevToolsSystemInfoHandler() {
     93   RegisterCommandHandler(devtools::SystemInfo::getInfo::kName,
     94                          base::Bind(&DevToolsSystemInfoHandler::OnGetInfo,
     95                                     base::Unretained(this)));
     96 }
     97 
     98 DevToolsSystemInfoHandler::~DevToolsSystemInfoHandler() {
     99 }
    100 
    101 scoped_refptr<DevToolsProtocol::Response>
    102 DevToolsSystemInfoHandler::OnGetInfo(
    103     scoped_refptr<DevToolsProtocol::Command> command) {
    104   gpu::GPUInfo gpu_info = GpuDataManagerImpl::GetInstance()->GetGPUInfo();
    105   base::DictionaryValue* gpu_dict = new base::DictionaryValue;
    106 
    107   base::ListValue* devices = new base::ListValue;
    108   devices->Append(GPUDeviceToDictionary(gpu_info.gpu));
    109   for (size_t ii = 0; ii < gpu_info.secondary_gpus.size(); ++ii) {
    110     devices->Append(GPUDeviceToDictionary(gpu_info.secondary_gpus[ii]));
    111   }
    112   gpu_dict->Set(kDevices, devices);
    113 
    114   base::DictionaryValue* aux_attributes = new base::DictionaryValue;
    115   AuxGPUInfoEnumerator enumerator(aux_attributes);
    116   gpu_info.EnumerateFields(&enumerator);
    117   gpu_dict->Set(kAuxAttributes, aux_attributes);
    118 
    119   gpu_dict->Set(kFeatureStatus,  GetFeatureStatus());
    120 
    121   base::DictionaryValue* system_dict = new base::DictionaryValue;
    122   system_dict->SetString(kModelName, gpu_info.machine_model);
    123   system_dict->Set(kGPU, gpu_dict);
    124   return command->SuccessResponse(system_dict);
    125 }
    126 
    127 }  // namespace content
    128