| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/system/statistics_provider.h" |
| 6 |
| 7 #include "base/memory/singleton.h" |
| 8 #include "base/task.h" |
| 9 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h" |
| 10 |
| 11 namespace chromeos { |
| 12 namespace system { |
| 13 namespace { |
| 14 |
| 15 // The system command that returns the hardware class. |
| 16 const char kHardwareClassKey[] = "hardware_class"; |
| 17 const char* kHardwareClassTool[] = { "crossystem", "hwid" }; |
| 18 const char kUnknownHardwareClass[] = "unknown"; |
| 19 |
| 20 // Command to get machine hardware info and key/value delimiters. |
| 21 // /tmp/machine-info is generated by platform/init/chromeos_startup. |
| 22 const char* kMachineHardwareInfoTool[] = { "cat", "/tmp/machine-info" }; |
| 23 const char kMachineHardwareInfoEq[] = "="; |
| 24 const char kMachineHardwareInfoDelim[] = " \n"; |
| 25 |
| 26 // Command to get machine OS info and key/value delimiters. |
| 27 const char* kMachineOSInfoTool[] = { "cat", "/etc/lsb-release" }; |
| 28 const char kMachineOSInfoEq[] = "="; |
| 29 const char kMachineOSInfoDelim[] = "\n"; |
| 30 |
| 31 // Command to get VPD info and key/value delimiters. |
| 32 const char* kVpdTool[] = { "cat", "/var/log/vpd_2.0.txt" }; |
| 33 const char kVpdEq[] = "="; |
| 34 const char kVpdDelim[] = "\n"; |
| 35 |
| 36 } // namespace |
| 37 |
| 38 class StatisticsProviderImpl : public StatisticsProvider { |
| 39 public: |
| 40 // StatisticsProvider implementation: |
| 41 virtual bool GetMachineStatistic(const std::string& name, |
| 42 std::string* result); |
| 43 |
| 44 static StatisticsProviderImpl* GetInstance(); |
| 45 |
| 46 private: |
| 47 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; |
| 48 |
| 49 StatisticsProviderImpl(); |
| 50 |
| 51 // Updates the machine statistcs by examining the system. |
| 52 void UpdateMachineStatistics(); |
| 53 |
| 54 NameValuePairsParser::NameValueMap machine_info_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); |
| 57 }; |
| 58 |
| 59 bool StatisticsProviderImpl::GetMachineStatistic( |
| 60 const std::string& name, std::string* result) { |
| 61 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name); |
| 62 if (iter != machine_info_.end()) { |
| 63 *result = iter->second; |
| 64 return true; |
| 65 } |
| 66 return false; |
| 67 } |
| 68 |
| 69 StatisticsProviderImpl::StatisticsProviderImpl() { |
| 70 // Get Statistics |
| 71 UpdateMachineStatistics(); |
| 72 } |
| 73 |
| 74 void StatisticsProviderImpl::UpdateMachineStatistics() { |
| 75 NameValuePairsParser parser(&machine_info_); |
| 76 if (!parser.GetSingleValueFromTool(arraysize(kHardwareClassTool), |
| 77 kHardwareClassTool, |
| 78 kHardwareClassKey)) { |
| 79 // Use kUnknownHardwareClass if the hardware class command fails. |
| 80 parser.AddNameValuePair(kHardwareClassKey, kUnknownHardwareClass); |
| 81 } |
| 82 parser.ParseNameValuePairsFromTool(arraysize(kMachineHardwareInfoTool), |
| 83 kMachineHardwareInfoTool, |
| 84 kMachineHardwareInfoEq, |
| 85 kMachineHardwareInfoDelim); |
| 86 parser.ParseNameValuePairsFromTool(arraysize(kMachineOSInfoTool), |
| 87 kMachineOSInfoTool, |
| 88 kMachineOSInfoEq, |
| 89 kMachineOSInfoDelim); |
| 90 parser.ParseNameValuePairsFromTool( |
| 91 arraysize(kVpdTool), kVpdTool, kVpdEq, kVpdDelim); |
| 92 } |
| 93 |
| 94 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() { |
| 95 return Singleton<StatisticsProviderImpl, |
| 96 DefaultSingletonTraits<StatisticsProviderImpl> >::get(); |
| 97 } |
| 98 |
| 99 StatisticsProvider* StatisticsProvider::GetInstance() { |
| 100 return StatisticsProviderImpl::GetInstance(); |
| 101 } |
| 102 |
| 103 } // namespace system |
| 104 } // namespace chromeos |
| 105 |
| 106 // Allows InvokeLater without adding refcounting. StatisticsProviderImpl is a |
| 107 // Singleton and won't be deleted until it's last InvokeLater is run. |
| 108 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::system::StatisticsProviderImpl); |
| OLD | NEW |