1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/system/statistics_provider.h"
#include "base/memory/singleton.h"
#include "base/task.h"
#include "chrome/browser/chromeos/system/name_value_pairs_parser.h"
#include "chrome/browser/chromeos/system/runtime_environment.h"
namespace chromeos {
namespace system {
namespace {
// The tools used here should be specified as absolute paths. The
// existence of the tools is checked in GetSingleValueFromTool().
// The system command that returns the hardware class.
const char kHardwareClassKey[] = "hardware_class";
const char* kHardwareClassTool[] = { "/usr/bin/crossystem", "hwid" };
const char kUnknownHardwareClass[] = "unknown";
// Command to get machine hardware info and key/value delimiters.
// /tmp/machine-info is generated by platform/init/chromeos_startup.
const char* kMachineHardwareInfoTool[] = { "/bin/cat", "/tmp/machine-info" };
const char kMachineHardwareInfoEq[] = "=";
const char kMachineHardwareInfoDelim[] = " \n";
// Command to get machine OS info and key/value delimiters.
const char* kMachineOSInfoTool[] = { "/bin/cat", "/etc/lsb-release" };
const char kMachineOSInfoEq[] = "=";
const char kMachineOSInfoDelim[] = "\n";
// Command to get VPD info and key/value delimiters.
const char* kVpdTool[] = { "/bin/cat", "/var/log/vpd_2.0.txt" };
const char kVpdEq[] = "=";
const char kVpdDelim[] = "\n";
} // namespace
// The StatisticsProvider implementation used in production.
class StatisticsProviderImpl : public StatisticsProvider {
public:
// StatisticsProvider implementation:
virtual bool GetMachineStatistic(const std::string& name,
std::string* result);
static StatisticsProviderImpl* GetInstance();
private:
friend struct DefaultSingletonTraits<StatisticsProviderImpl>;
StatisticsProviderImpl();
// Updates the machine statistcs by examining the system.
void UpdateMachineStatistics();
NameValuePairsParser::NameValueMap machine_info_;
DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl);
};
bool StatisticsProviderImpl::GetMachineStatistic(
const std::string& name, std::string* result) {
NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name);
if (iter != machine_info_.end()) {
*result = iter->second;
return true;
}
return false;
}
StatisticsProviderImpl::StatisticsProviderImpl() {
// Get Statistics
UpdateMachineStatistics();
}
void StatisticsProviderImpl::UpdateMachineStatistics() {
NameValuePairsParser parser(&machine_info_);
if (!parser.GetSingleValueFromTool(arraysize(kHardwareClassTool),
kHardwareClassTool,
kHardwareClassKey)) {
// Use kUnknownHardwareClass if the hardware class command fails.
parser.AddNameValuePair(kHardwareClassKey, kUnknownHardwareClass);
}
parser.ParseNameValuePairsFromTool(arraysize(kMachineHardwareInfoTool),
kMachineHardwareInfoTool,
kMachineHardwareInfoEq,
kMachineHardwareInfoDelim);
parser.ParseNameValuePairsFromTool(arraysize(kMachineOSInfoTool),
kMachineOSInfoTool,
kMachineOSInfoEq,
kMachineOSInfoDelim);
parser.ParseNameValuePairsFromTool(
arraysize(kVpdTool), kVpdTool, kVpdEq, kVpdDelim);
}
StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() {
return Singleton<StatisticsProviderImpl,
DefaultSingletonTraits<StatisticsProviderImpl> >::get();
}
// The stub StatisticsProvider implementation used on Linux desktop.
class StatisticsProviderStubImpl : public StatisticsProvider {
public:
// StatisticsProvider implementation:
virtual bool GetMachineStatistic(const std::string& name,
std::string* result) {
*result = "stub statistic for " + name;
return true;
}
static StatisticsProviderStubImpl* GetInstance() {
return Singleton<StatisticsProviderStubImpl,
DefaultSingletonTraits<StatisticsProviderStubImpl> >::get();
}
private:
friend struct DefaultSingletonTraits<StatisticsProviderStubImpl>;
StatisticsProviderStubImpl() {
}
DISALLOW_COPY_AND_ASSIGN(StatisticsProviderStubImpl);
};
StatisticsProvider* StatisticsProvider::GetInstance() {
if (system::runtime_environment::IsRunningOnChromeOS()) {
return StatisticsProviderImpl::GetInstance();
} else {
return StatisticsProviderStubImpl::GetInstance();
}
}
} // namespace system
} // namespace chromeos
|