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/chromeos/chromeos_version.h" | 8 #include "base/chromeos/chromeos_version.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 63 |
64 // Timeout that we should wait for statistics to get loaded | 64 // Timeout that we should wait for statistics to get loaded |
65 const int kTimeoutSecs = 3; | 65 const int kTimeoutSecs = 3; |
66 | 66 |
67 } // namespace | 67 } // namespace |
68 | 68 |
69 // The StatisticsProvider implementation used in production. | 69 // The StatisticsProvider implementation used in production. |
70 class StatisticsProviderImpl : public StatisticsProvider { | 70 class StatisticsProviderImpl : public StatisticsProvider { |
71 public: | 71 public: |
72 // StatisticsProvider implementation: | 72 // StatisticsProvider implementation: |
| 73 virtual void Init() OVERRIDE; |
73 virtual bool GetMachineStatistic(const std::string& name, | 74 virtual bool GetMachineStatistic(const std::string& name, |
74 std::string* result) OVERRIDE; | 75 std::string* result) OVERRIDE; |
75 | 76 |
76 static StatisticsProviderImpl* GetInstance(); | 77 static StatisticsProviderImpl* GetInstance(); |
77 | 78 |
78 private: | 79 private: |
79 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; | 80 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; |
80 | 81 |
81 StatisticsProviderImpl(); | 82 StatisticsProviderImpl(); |
82 | 83 |
| 84 // Loads the machine info file, which is necessary to get the Chrome channel. |
| 85 // Treat MachineOSInfoFile specially, as distribution channel information |
| 86 // (stable, beta, dev, canary) is required at earlier stage than everything |
| 87 // else. Rather than posting a delayed task, read and parse the machine OS |
| 88 // info file immediately. |
| 89 void LoadMachineOSInfoFile(); |
| 90 |
83 // Starts loading the machine statistcs. | 91 // Starts loading the machine statistcs. |
84 void StartLoadingMachineStatistics(); | 92 void StartLoadingMachineStatistics(); |
85 | 93 |
86 // Loads the machine statistcs by examining the system. | 94 // Loads the machine statistcs by examining the system. |
87 void LoadMachineStatistics(); | 95 void LoadMachineStatistics(); |
88 | 96 |
| 97 bool initialized_; |
89 NameValuePairsParser::NameValueMap machine_info_; | 98 NameValuePairsParser::NameValueMap machine_info_; |
90 base::WaitableEvent on_statistics_loaded_; | 99 base::WaitableEvent on_statistics_loaded_; |
91 | 100 |
92 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); | 101 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); |
93 }; | 102 }; |
94 | 103 |
| 104 void StatisticsProviderImpl::Init() { |
| 105 DCHECK(!initialized_); |
| 106 initialized_ = true; |
| 107 |
| 108 // Load the machine info file immediately to get the channel info and delay |
| 109 // loading the remaining statistics. |
| 110 LoadMachineOSInfoFile(); |
| 111 StartLoadingMachineStatistics(); |
| 112 } |
| 113 |
95 bool StatisticsProviderImpl::GetMachineStatistic( | 114 bool StatisticsProviderImpl::GetMachineStatistic( |
96 const std::string& name, std::string* result) { | 115 const std::string& name, std::string* result) { |
| 116 DCHECK(initialized_); |
| 117 |
97 VLOG(1) << "Statistic is requested for " << name; | 118 VLOG(1) << "Statistic is requested for " << name; |
98 // Block if the statistics are not loaded yet. Per LOG(WARNING) below, | 119 // Block if the statistics are not loaded yet. Per LOG(WARNING) below, |
99 // the statistics are loaded before requested as of now. For regular | 120 // the statistics are loaded before requested as of now. For regular |
100 // sessions (i.e. not OOBE), statistics are first requested when the | 121 // sessions (i.e. not OOBE), statistics are first requested when the |
101 // user is logging in so we have plenty of time to load the data | 122 // user is logging in so we have plenty of time to load the data |
102 // beforehand. | 123 // beforehand. |
103 // | 124 // |
104 // If you see the warning appeared for regular sessions, it probably | 125 // If you see the warning appeared for regular sessions, it probably |
105 // means that there is new client code that uses the statistics in the | 126 // means that there is new client code that uses the statistics in the |
106 // very early stage of the browser startup. The statistic name should be | 127 // very early stage of the browser startup. The statistic name should be |
(...skipping 15 matching lines...) Expand all Loading... |
122 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name); | 143 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name); |
123 if (iter != machine_info_.end()) { | 144 if (iter != machine_info_.end()) { |
124 *result = iter->second; | 145 *result = iter->second; |
125 return true; | 146 return true; |
126 } | 147 } |
127 return false; | 148 return false; |
128 } | 149 } |
129 | 150 |
130 // manual_reset needs to be true, as we want to keep the signaled state. | 151 // manual_reset needs to be true, as we want to keep the signaled state. |
131 StatisticsProviderImpl::StatisticsProviderImpl() | 152 StatisticsProviderImpl::StatisticsProviderImpl() |
132 : on_statistics_loaded_(true /* manual_reset */, | 153 : initialized_(false), |
| 154 on_statistics_loaded_(true /* manual_reset */, |
133 false /* initially_signaled */) { | 155 false /* initially_signaled */) { |
134 StartLoadingMachineStatistics(); | 156 } |
| 157 |
| 158 void StatisticsProviderImpl::LoadMachineOSInfoFile() { |
| 159 NameValuePairsParser parser(&machine_info_); |
| 160 if (parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile), |
| 161 kMachineOSInfoEq, |
| 162 kMachineOSInfoDelim)) { |
| 163 #if defined(GOOGLE_CHROME_BUILD) |
| 164 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK"; |
| 165 NameValuePairsParser::NameValueMap::iterator iter = |
| 166 machine_info_.find(kChromeOSReleaseTrack); |
| 167 if (iter != machine_info_.end()) |
| 168 chrome::VersionInfo::SetChannel(iter->second); |
| 169 #endif |
| 170 } |
135 } | 171 } |
136 | 172 |
137 void StatisticsProviderImpl::StartLoadingMachineStatistics() { | 173 void StatisticsProviderImpl::StartLoadingMachineStatistics() { |
138 VLOG(1) << "Started loading statistics"; | 174 VLOG(1) << "Started loading statistics"; |
139 BrowserThread::PostBlockingPoolTask( | 175 BrowserThread::PostBlockingPoolTask( |
140 FROM_HERE, | 176 FROM_HERE, |
141 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics, | 177 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics, |
142 base::Unretained(this))); | 178 base::Unretained(this))); |
143 } | 179 } |
144 | 180 |
(...skipping 15 matching lines...) Expand all Loading... |
160 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; | 196 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; |
161 else | 197 else |
162 machine_info_[kHardwareClassKey] = hardware_class; | 198 machine_info_[kHardwareClassKey] = hardware_class; |
163 | 199 |
164 parser.GetNameValuePairsFromFile(FilePath(kMachineHardwareInfoFile), | 200 parser.GetNameValuePairsFromFile(FilePath(kMachineHardwareInfoFile), |
165 kMachineHardwareInfoEq, | 201 kMachineHardwareInfoEq, |
166 kMachineHardwareInfoDelim); | 202 kMachineHardwareInfoDelim); |
167 parser.GetNameValuePairsFromFile(FilePath(kEchoCouponFile), | 203 parser.GetNameValuePairsFromFile(FilePath(kEchoCouponFile), |
168 kEchoCouponEq, | 204 kEchoCouponEq, |
169 kEchoCouponDelim); | 205 kEchoCouponDelim); |
170 parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile), | |
171 kMachineOSInfoEq, | |
172 kMachineOSInfoDelim); | |
173 parser.GetNameValuePairsFromFile(FilePath(kVpdFile), kVpdEq, kVpdDelim); | 206 parser.GetNameValuePairsFromFile(FilePath(kVpdFile), kVpdEq, kVpdDelim); |
174 | 207 |
175 // Finished loading the statistics. | 208 // Finished loading the statistics. |
176 on_statistics_loaded_.Signal(); | 209 on_statistics_loaded_.Signal(); |
177 VLOG(1) << "Finished loading statistics"; | 210 VLOG(1) << "Finished loading statistics"; |
178 | |
179 #if defined(GOOGLE_CHROME_BUILD) | |
180 // TODO(kochi): This is for providing a channel information to | |
181 // chrome::VersionInfo::GetChannel()/GetVersionStringModifier(), | |
182 // but this is still late for some early customers such as | |
183 // prerender::ConfigurePrefetchAndPrerender() and | |
184 // ThreadWatcherList::ParseCommandLine(). | |
185 // See http://crbug.com/107333 . | |
186 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK"; | |
187 std::string channel; | |
188 if (GetMachineStatistic(kChromeOSReleaseTrack, &channel)) { | |
189 chrome::VersionInfo::SetChannel(channel); | |
190 // Set the product channel for crash reports. We can't just do this in | |
191 // ChromeBrowserMainParts::PreCreateThreads like we do for Linux because | |
192 // the FILE thread hasn't been created yet there so we can't possibly | |
193 // have read this yet. Note that this string isn't exactly the same as | |
194 // 'channel', it's been parsed to be consistent with other platforms | |
195 // (eg. "canary-channel" becomes "canary", "testimage-channel" becomes | |
196 // "unknown"). | |
197 child_process_logging::SetChannel( | |
198 chrome::VersionInfo::GetVersionStringModifier()); | |
199 } | |
200 #endif | |
201 } | 211 } |
202 | 212 |
203 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() { | 213 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() { |
204 return Singleton<StatisticsProviderImpl, | 214 return Singleton<StatisticsProviderImpl, |
205 DefaultSingletonTraits<StatisticsProviderImpl> >::get(); | 215 DefaultSingletonTraits<StatisticsProviderImpl> >::get(); |
206 } | 216 } |
207 | 217 |
208 // The stub StatisticsProvider implementation used on Linux desktop. | 218 // The stub StatisticsProvider implementation used on Linux desktop. |
209 class StatisticsProviderStubImpl : public StatisticsProvider { | 219 class StatisticsProviderStubImpl : public StatisticsProvider { |
210 public: | 220 public: |
211 // StatisticsProvider implementation: | 221 // StatisticsProvider implementation: |
| 222 virtual void Init() OVERRIDE { |
| 223 } |
| 224 |
212 virtual bool GetMachineStatistic(const std::string& name, | 225 virtual bool GetMachineStatistic(const std::string& name, |
213 std::string* result) OVERRIDE { | 226 std::string* result) OVERRIDE { |
214 if (name == "CHROMEOS_RELEASE_BOARD") { | 227 if (name == "CHROMEOS_RELEASE_BOARD") { |
215 // Note: syncer::GetSessionNameSynchronously() also uses the mechanism | 228 // Note: syncer::GetSessionNameSynchronously() also uses the mechanism |
216 // below to determine the CrOs release board. However, it cannot include | 229 // below to determine the CrOs release board. However, it cannot include |
217 // statistics_provider.h and use this method because of the mutual | 230 // statistics_provider.h and use this method because of the mutual |
218 // dependency that creates between sync.gyp:sync and chrome.gyp:browser. | 231 // dependency that creates between sync.gyp:sync and chrome.gyp:browser. |
219 // TODO(rsimha): Update syncer::GetSessionNameSynchronously() if this code | 232 // TODO(rsimha): Update syncer::GetSessionNameSynchronously() if this code |
220 // is ever moved into base/. See http://crbug.com/126732. | 233 // is ever moved into base/. See http://crbug.com/126732. |
221 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 234 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
(...skipping 23 matching lines...) Expand all Loading... |
245 StatisticsProvider* StatisticsProvider::GetInstance() { | 258 StatisticsProvider* StatisticsProvider::GetInstance() { |
246 if (base::chromeos::IsRunningOnChromeOS()) { | 259 if (base::chromeos::IsRunningOnChromeOS()) { |
247 return StatisticsProviderImpl::GetInstance(); | 260 return StatisticsProviderImpl::GetInstance(); |
248 } else { | 261 } else { |
249 return StatisticsProviderStubImpl::GetInstance(); | 262 return StatisticsProviderStubImpl::GetInstance(); |
250 } | 263 } |
251 } | 264 } |
252 | 265 |
253 } // namespace system | 266 } // namespace system |
254 } // namespace chromeos | 267 } // namespace chromeos |
OLD | NEW |