OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/system/statistics_provider.h" | 5 #include "chrome/browser/chromeos/system/statistics_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
8 #include "base/chromeos/chromeos_version.h" | 9 #include "base/chromeos/chromeos_version.h" |
9 #include "base/file_path.h" | 10 #include "base/file_path.h" |
10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 14 #include "base/message_loop.h" |
| 15 #include "base/metrics/histogram.h" |
13 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
14 #include "base/time.h" | 17 #include "base/time.h" |
15 #include "base/chromeos/chromeos_version.h" | 18 #include "base/chromeos/chromeos_version.h" |
16 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h" | 19 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h" |
17 #include "chrome/common/child_process_logging.h" | 20 #include "chrome/common/child_process_logging.h" |
18 #include "chrome/common/chrome_version_info.h" | 21 #include "chrome/common/chrome_version_info.h" |
19 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
20 | 23 |
21 using content::BrowserThread; | 24 using content::BrowserThread; |
22 | 25 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 const char kMachineOSInfoDelim[] = "\n"; | 58 const char kMachineOSInfoDelim[] = "\n"; |
56 | 59 |
57 // File to get VPD info from, and key/value delimiters of the file. | 60 // File to get VPD info from, and key/value delimiters of the file. |
58 const char kVpdFile[] = "/var/log/vpd_2.0.txt"; | 61 const char kVpdFile[] = "/var/log/vpd_2.0.txt"; |
59 const char kVpdEq[] = "="; | 62 const char kVpdEq[] = "="; |
60 const char kVpdDelim[] = "\n"; | 63 const char kVpdDelim[] = "\n"; |
61 | 64 |
62 // Timeout that we should wait for statistics to get loaded | 65 // Timeout that we should wait for statistics to get loaded |
63 const int kTimeoutSecs = 3; | 66 const int kTimeoutSecs = 3; |
64 | 67 |
| 68 // Helper to invoke |callbacks| on the UI thread. |
| 69 void NotifyOnUI(const std::vector<base::Closure>& callbacks) { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 71 for (std::vector<base::Closure>::const_iterator it = callbacks.begin(); |
| 72 it != callbacks.end(); ++it) { |
| 73 it->Run(); |
| 74 } |
| 75 } |
| 76 |
65 } // namespace | 77 } // namespace |
66 | 78 |
67 // The StatisticsProvider implementation used in production. | 79 // The StatisticsProvider implementation used in production. |
68 class StatisticsProviderImpl : public StatisticsProvider { | 80 class StatisticsProviderImpl : public StatisticsProvider { |
69 public: | 81 public: |
70 // StatisticsProvider implementation: | 82 // StatisticsProvider implementation: |
71 virtual bool GetMachineStatistic(const std::string& name, | 83 virtual bool GetMachineStatistic(const std::string& name, |
72 std::string* result) OVERRIDE; | 84 std::string* result) OVERRIDE; |
73 | 85 |
| 86 virtual void WhenReady(const base::Closure& callback) OVERRIDE; |
| 87 |
74 static StatisticsProviderImpl* GetInstance(); | 88 static StatisticsProviderImpl* GetInstance(); |
75 | 89 |
76 private: | 90 private: |
77 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; | 91 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; |
78 | 92 |
79 StatisticsProviderImpl(); | 93 StatisticsProviderImpl(); |
80 | 94 |
81 // Starts loading the machine statistcs. | 95 // Starts loading the machine statistcs. |
82 void StartLoadingMachineStatistics(); | 96 void StartLoadingMachineStatistics(); |
83 | 97 |
84 // Loads the machine statistcs by examining the system. | 98 // Loads the machine statistcs by examining the system. |
85 void LoadMachineStatistics(); | 99 void LoadMachineStatistics(); |
86 | 100 |
87 NameValuePairsParser::NameValueMap machine_info_; | 101 NameValuePairsParser::NameValueMap machine_info_; |
88 base::WaitableEvent on_statistics_loaded_; | 102 base::WaitableEvent on_statistics_loaded_; |
| 103 std::vector<base::Closure> callbacks_; |
89 | 104 |
90 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); | 105 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); |
91 }; | 106 }; |
92 | 107 |
93 bool StatisticsProviderImpl::GetMachineStatistic( | 108 bool StatisticsProviderImpl::GetMachineStatistic( |
94 const std::string& name, std::string* result) { | 109 const std::string& name, |
| 110 std::string* result) { |
95 VLOG(1) << "Statistic is requested for " << name; | 111 VLOG(1) << "Statistic is requested for " << name; |
96 // Block if the statistics are not loaded yet. Per LOG(WARNING) below, | 112 // Block if the statistics are not loaded yet. Per DLOG(WARNING) below, |
97 // the statistics are loaded before requested as of now. For regular | 113 // the statistics are loaded before requested as of now. For regular |
98 // sessions (i.e. not OOBE), statistics are first requested when the | 114 // sessions (i.e. not OOBE), statistics are first requested when the |
99 // user is logging in so we have plenty of time to load the data | 115 // user is logging in so we have plenty of time to load the data |
100 // beforehand. | 116 // beforehand. |
101 // | 117 // |
102 // If you see the warning appeared for regular sessions, it probably | 118 // If you see the warning appeared for regular sessions, it probably |
103 // means that there is new client code that uses the statistics in the | 119 // means that there is new client code that uses the statistics in the |
104 // very early stage of the browser startup. The statistic name should be | 120 // very early stage of the browser startup. The statistic name should be |
105 // helpful to identify the caller. | 121 // helpful to identify the caller. |
106 if (!on_statistics_loaded_.IsSignaled()) { | 122 if (!on_statistics_loaded_.IsSignaled()) { |
107 LOG(WARNING) << "Waiting to load statistics. Requested statistic: " | 123 DLOG(WARNING) << "Waiting to load statistics. Requested statistic: " |
108 << name; | 124 << name; |
109 on_statistics_loaded_.TimedWait(base::TimeDelta::FromSeconds(kTimeoutSecs)); | 125 on_statistics_loaded_.TimedWait(base::TimeDelta::FromSeconds(kTimeoutSecs)); |
110 | 126 |
111 if (!on_statistics_loaded_.IsSignaled()) { | 127 if (!on_statistics_loaded_.IsSignaled()) { |
112 LOG(ERROR) << "Statistics weren't loaded after waiting! " | 128 DLOG(ERROR) << "Statistics weren't loaded after waiting! " |
113 << "Requested statistic: " << name; | 129 << "Requested statistic: " << name; |
114 return false; | 130 return false; |
115 } | 131 } |
116 } | 132 } |
117 | 133 |
118 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name); | 134 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name); |
119 if (iter != machine_info_.end()) { | 135 if (iter != machine_info_.end()) { |
120 *result = iter->second; | 136 *result = iter->second; |
121 return true; | 137 return true; |
122 } | 138 } |
123 return false; | 139 return false; |
124 } | 140 } |
125 | 141 |
| 142 void StatisticsProviderImpl::WhenReady(const base::Closure& callback) { |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 144 if (on_statistics_loaded_.IsSignaled()) { |
| 145 // Don't assume the caller is re-entrant. |
| 146 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
| 147 } else { |
| 148 callbacks_.push_back(callback); |
| 149 } |
| 150 } |
| 151 |
126 // manual_reset needs to be true, as we want to keep the signaled state. | 152 // manual_reset needs to be true, as we want to keep the signaled state. |
127 StatisticsProviderImpl::StatisticsProviderImpl() | 153 StatisticsProviderImpl::StatisticsProviderImpl() |
128 : on_statistics_loaded_(true /* manual_reset */, | 154 : on_statistics_loaded_(true /* manual_reset */, |
129 false /* initially_signaled */) { | 155 false /* initially_signaled */) { |
130 StartLoadingMachineStatistics(); | 156 StartLoadingMachineStatistics(); |
131 } | 157 } |
132 | 158 |
133 void StatisticsProviderImpl::StartLoadingMachineStatistics() { | 159 void StatisticsProviderImpl::StartLoadingMachineStatistics() { |
134 VLOG(1) << "Started loading statistics"; | 160 VLOG(1) << "Started loading statistics"; |
135 BrowserThread::PostBlockingPoolTask( | 161 BrowserThread::PostBlockingPoolTask( |
136 FROM_HERE, | 162 FROM_HERE, |
137 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics, | 163 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics, |
138 base::Unretained(this))); | 164 base::Unretained(this))); |
139 } | 165 } |
140 | 166 |
141 void StatisticsProviderImpl::LoadMachineStatistics() { | 167 void StatisticsProviderImpl::LoadMachineStatistics() { |
142 NameValuePairsParser parser(&machine_info_); | 168 NameValuePairsParser parser(&machine_info_); |
143 | 169 |
144 // Parse all of the key/value pairs from the crossystem tool. | 170 // Parse all of the key/value pairs from the crossystem tool. |
145 if (!parser.ParseNameValuePairsFromTool( | 171 if (!parser.ParseNameValuePairsFromTool( |
146 arraysize(kCrosSystemTool), kCrosSystemTool, kCrosSystemEq, | 172 arraysize(kCrosSystemTool), kCrosSystemTool, kCrosSystemEq, |
147 kCrosSystemDelim, kCrosSystemCommentDelim)) { | 173 kCrosSystemDelim, kCrosSystemCommentDelim)) { |
148 LOG(WARNING) << "There were errors parsing the output of " | 174 DLOG(WARNING) << "There were errors parsing the output of " |
149 << kCrosSystemTool << "."; | 175 << kCrosSystemTool << "."; |
| 176 UMA_HISTOGRAM_BOOLEAN("Cros.CrosSystemParsingFailed", true); |
150 } | 177 } |
151 | 178 |
152 // Ensure that the hardware class key is present with the expected | 179 // Ensure that the hardware class key is present with the expected |
153 // key name, and if it couldn't be retrieved, that the value is "unknown". | 180 // key name, and if it couldn't be retrieved, that the value is "unknown". |
154 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; | 181 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; |
155 if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue) | 182 if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue) |
156 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; | 183 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; |
157 else | 184 else |
158 machine_info_[kHardwareClassKey] = hardware_class; | 185 machine_info_[kHardwareClassKey] = hardware_class; |
159 | 186 |
160 parser.GetNameValuePairsFromFile(FilePath(kMachineHardwareInfoFile), | 187 parser.GetNameValuePairsFromFile(FilePath(kMachineHardwareInfoFile), |
161 kMachineHardwareInfoEq, | 188 kMachineHardwareInfoEq, |
162 kMachineHardwareInfoDelim); | 189 kMachineHardwareInfoDelim); |
163 parser.GetNameValuePairsFromFile(FilePath(kEchoCouponFile), | 190 parser.GetNameValuePairsFromFile(FilePath(kEchoCouponFile), |
164 kEchoCouponEq, | 191 kEchoCouponEq, |
165 kEchoCouponDelim); | 192 kEchoCouponDelim); |
166 parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile), | 193 parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile), |
167 kMachineOSInfoEq, | 194 kMachineOSInfoEq, |
168 kMachineOSInfoDelim); | 195 kMachineOSInfoDelim); |
169 parser.GetNameValuePairsFromFile(FilePath(kVpdFile), kVpdEq, kVpdDelim); | 196 parser.GetNameValuePairsFromFile(FilePath(kVpdFile), kVpdEq, kVpdDelim); |
170 | 197 |
171 // Finished loading the statistics. | 198 // Finished loading the statistics. |
172 on_statistics_loaded_.Signal(); | 199 on_statistics_loaded_.Signal(); |
173 VLOG(1) << "Finished loading statistics"; | 200 VLOG(1) << "Finished loading statistics"; |
174 | 201 |
| 202 // Any callbacks registed before Signal() are in |callbacks_|. Any registed |
| 203 // after Signal() will be immediately posted. NotifyOnUI() posts the callbacks |
| 204 // in |callbacks_| from the UI loop. |
| 205 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 206 base::Bind(NotifyOnUI, callbacks_)); |
| 207 callbacks_.clear(); |
| 208 |
175 #if defined(GOOGLE_CHROME_BUILD) | 209 #if defined(GOOGLE_CHROME_BUILD) |
176 // TODO(kochi): This is for providing a channel information to | 210 // TODO(kochi): This is for providing a channel information to |
177 // chrome::VersionInfo::GetChannel()/GetVersionStringModifier(), | 211 // chrome::VersionInfo::GetChannel()/GetVersionStringModifier(), |
178 // but this is still late for some early customers such as | 212 // but this is still late for some early customers such as |
179 // prerender::ConfigurePrefetchAndPrerender() and | 213 // prerender::ConfigurePrefetchAndPrerender() and |
180 // ThreadWatcherList::ParseCommandLine(). | 214 // ThreadWatcherList::ParseCommandLine(). |
181 // See http://crbug.com/107333 . | 215 // See http://crbug.com/107333 . |
182 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK"; | 216 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK"; |
183 std::string channel; | 217 std::string channel; |
184 if (GetMachineStatistic(kChromeOSReleaseTrack, &channel)) { | 218 if (GetMachineStatistic(kChromeOSReleaseTrack, &channel)) { |
(...skipping 18 matching lines...) Expand all Loading... |
203 | 237 |
204 // The stub StatisticsProvider implementation used on Linux desktop. | 238 // The stub StatisticsProvider implementation used on Linux desktop. |
205 class StatisticsProviderStubImpl : public StatisticsProvider { | 239 class StatisticsProviderStubImpl : public StatisticsProvider { |
206 public: | 240 public: |
207 // StatisticsProvider implementation: | 241 // StatisticsProvider implementation: |
208 virtual bool GetMachineStatistic(const std::string& name, | 242 virtual bool GetMachineStatistic(const std::string& name, |
209 std::string* result) OVERRIDE { | 243 std::string* result) OVERRIDE { |
210 return false; | 244 return false; |
211 } | 245 } |
212 | 246 |
| 247 virtual void WhenReady(const base::Closure& callback) OVERRIDE { |
| 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 249 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
| 250 } |
| 251 |
213 static StatisticsProviderStubImpl* GetInstance() { | 252 static StatisticsProviderStubImpl* GetInstance() { |
214 return Singleton<StatisticsProviderStubImpl, | 253 return Singleton<StatisticsProviderStubImpl, |
215 DefaultSingletonTraits<StatisticsProviderStubImpl> >::get(); | 254 DefaultSingletonTraits<StatisticsProviderStubImpl> >::get(); |
216 } | 255 } |
217 | 256 |
218 private: | 257 private: |
219 friend struct DefaultSingletonTraits<StatisticsProviderStubImpl>; | 258 friend struct DefaultSingletonTraits<StatisticsProviderStubImpl>; |
220 | 259 |
221 StatisticsProviderStubImpl() { | 260 StatisticsProviderStubImpl() { |
222 } | 261 } |
223 | 262 |
224 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderStubImpl); | 263 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderStubImpl); |
225 }; | 264 }; |
226 | 265 |
227 StatisticsProvider* StatisticsProvider::GetInstance() { | 266 StatisticsProvider* StatisticsProvider::GetInstance() { |
228 if (base::chromeos::IsRunningOnChromeOS()) { | 267 if (base::chromeos::IsRunningOnChromeOS()) { |
229 return StatisticsProviderImpl::GetInstance(); | 268 return StatisticsProviderImpl::GetInstance(); |
230 } else { | 269 } else { |
231 return StatisticsProviderStubImpl::GetInstance(); | 270 return StatisticsProviderStubImpl::GetInstance(); |
232 } | 271 } |
233 } | 272 } |
234 | 273 |
235 } // namespace system | 274 } // namespace system |
236 } // namespace chromeos | 275 } // namespace chromeos |
OLD | NEW |