| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 void Init() OVERRIDE; |
| 74 virtual void StartLoadingMachineStatistics() OVERRIDE; |
| 74 virtual bool GetMachineStatistic(const std::string& name, | 75 virtual bool GetMachineStatistic(const std::string& name, |
| 75 std::string* result) OVERRIDE; | 76 std::string* result) OVERRIDE; |
| 76 | 77 |
| 77 static StatisticsProviderImpl* GetInstance(); | 78 static StatisticsProviderImpl* GetInstance(); |
| 78 | 79 |
| 79 private: | 80 private: |
| 80 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; | 81 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; |
| 81 | 82 |
| 82 StatisticsProviderImpl(); | 83 StatisticsProviderImpl(); |
| 83 | 84 |
| 84 // Loads the machine info file, which is necessary to get the Chrome channel. | 85 // Loads the machine info file, which is necessary to get the Chrome channel. |
| 85 // Treat MachineOSInfoFile specially, as distribution channel information | 86 // Treat MachineOSInfoFile specially, as distribution channel information |
| 86 // (stable, beta, dev, canary) is required at earlier stage than everything | 87 // (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 // else. Rather than posting a delayed task, read and parse the machine OS |
| 88 // info file immediately. | 89 // info file immediately. |
| 89 void LoadMachineOSInfoFile(); | 90 void LoadMachineOSInfoFile(); |
| 90 | 91 |
| 91 // Starts loading the machine statistcs. | |
| 92 void StartLoadingMachineStatistics(); | |
| 93 | |
| 94 // Loads the machine statistcs by examining the system. | 92 // Loads the machine statistcs by examining the system. |
| 95 void LoadMachineStatistics(); | 93 void LoadMachineStatistics(); |
| 96 | 94 |
| 97 bool initialized_; | 95 bool initialized_; |
| 96 bool load_statistics_started_; |
| 98 NameValuePairsParser::NameValueMap machine_info_; | 97 NameValuePairsParser::NameValueMap machine_info_; |
| 99 base::WaitableEvent on_statistics_loaded_; | 98 base::WaitableEvent on_statistics_loaded_; |
| 100 | 99 |
| 101 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); | 100 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); |
| 102 }; | 101 }; |
| 103 | 102 |
| 104 void StatisticsProviderImpl::Init() { | 103 void StatisticsProviderImpl::Init() { |
| 105 DCHECK(!initialized_); | 104 DCHECK(!initialized_); |
| 106 initialized_ = true; | 105 initialized_ = true; |
| 107 | 106 |
| 108 // Load the machine info file immediately to get the channel info and delay | 107 // Load the machine info file immediately to get the channel info. |
| 109 // loading the remaining statistics. | |
| 110 LoadMachineOSInfoFile(); | 108 LoadMachineOSInfoFile(); |
| 111 StartLoadingMachineStatistics(); | |
| 112 } | 109 } |
| 113 | 110 |
| 114 bool StatisticsProviderImpl::GetMachineStatistic( | 111 bool StatisticsProviderImpl::GetMachineStatistic( |
| 115 const std::string& name, std::string* result) { | 112 const std::string& name, std::string* result) { |
| 116 DCHECK(initialized_); | 113 DCHECK(initialized_); |
| 114 DCHECK(load_statistics_started_); |
| 117 | 115 |
| 118 VLOG(1) << "Statistic is requested for " << name; | 116 VLOG(1) << "Statistic is requested for " << name; |
| 119 // Block if the statistics are not loaded yet. Per LOG(WARNING) below, | 117 // Block if the statistics are not loaded yet. Per LOG(WARNING) below, |
| 120 // the statistics are loaded before requested as of now. For regular | 118 // the statistics are loaded before requested as of now. For regular |
| 121 // sessions (i.e. not OOBE), statistics are first requested when the | 119 // sessions (i.e. not OOBE), statistics are first requested when the |
| 122 // user is logging in so we have plenty of time to load the data | 120 // user is logging in so we have plenty of time to load the data |
| 123 // beforehand. | 121 // beforehand. |
| 124 // | 122 // |
| 125 // If you see the warning appeared for regular sessions, it probably | 123 // If you see the warning appeared for regular sessions, it probably |
| 126 // means that there is new client code that uses the statistics in the | 124 // means that there is new client code that uses the statistics in the |
| (...skipping 17 matching lines...) Expand all Loading... |
| 144 if (iter != machine_info_.end()) { | 142 if (iter != machine_info_.end()) { |
| 145 *result = iter->second; | 143 *result = iter->second; |
| 146 return true; | 144 return true; |
| 147 } | 145 } |
| 148 return false; | 146 return false; |
| 149 } | 147 } |
| 150 | 148 |
| 151 // manual_reset needs to be true, as we want to keep the signaled state. | 149 // manual_reset needs to be true, as we want to keep the signaled state. |
| 152 StatisticsProviderImpl::StatisticsProviderImpl() | 150 StatisticsProviderImpl::StatisticsProviderImpl() |
| 153 : initialized_(false), | 151 : initialized_(false), |
| 152 load_statistics_started_(false), |
| 154 on_statistics_loaded_(true /* manual_reset */, | 153 on_statistics_loaded_(true /* manual_reset */, |
| 155 false /* initially_signaled */) { | 154 false /* initially_signaled */) { |
| 156 } | 155 } |
| 157 | 156 |
| 158 void StatisticsProviderImpl::LoadMachineOSInfoFile() { | 157 void StatisticsProviderImpl::LoadMachineOSInfoFile() { |
| 159 NameValuePairsParser parser(&machine_info_); | 158 NameValuePairsParser parser(&machine_info_); |
| 160 if (parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile), | 159 if (parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile), |
| 161 kMachineOSInfoEq, | 160 kMachineOSInfoEq, |
| 162 kMachineOSInfoDelim)) { | 161 kMachineOSInfoDelim)) { |
| 163 #if defined(GOOGLE_CHROME_BUILD) | 162 #if defined(GOOGLE_CHROME_BUILD) |
| 164 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK"; | 163 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK"; |
| 165 NameValuePairsParser::NameValueMap::iterator iter = | 164 NameValuePairsParser::NameValueMap::iterator iter = |
| 166 machine_info_.find(kChromeOSReleaseTrack); | 165 machine_info_.find(kChromeOSReleaseTrack); |
| 167 if (iter != machine_info_.end()) | 166 if (iter != machine_info_.end()) |
| 168 chrome::VersionInfo::SetChannel(iter->second); | 167 chrome::VersionInfo::SetChannel(iter->second); |
| 169 #endif | 168 #endif |
| 170 } | 169 } |
| 171 } | 170 } |
| 172 | 171 |
| 173 void StatisticsProviderImpl::StartLoadingMachineStatistics() { | 172 void StatisticsProviderImpl::StartLoadingMachineStatistics() { |
| 173 DCHECK(initialized_); |
| 174 DCHECK(!load_statistics_started_); |
| 175 load_statistics_started_ = true; |
| 176 |
| 174 VLOG(1) << "Started loading statistics"; | 177 VLOG(1) << "Started loading statistics"; |
| 175 BrowserThread::PostBlockingPoolTask( | 178 BrowserThread::PostBlockingPoolTask( |
| 176 FROM_HERE, | 179 FROM_HERE, |
| 177 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics, | 180 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics, |
| 178 base::Unretained(this))); | 181 base::Unretained(this))); |
| 179 } | 182 } |
| 180 | 183 |
| 181 void StatisticsProviderImpl::LoadMachineStatistics() { | 184 void StatisticsProviderImpl::LoadMachineStatistics() { |
| 182 NameValuePairsParser parser(&machine_info_); | 185 NameValuePairsParser parser(&machine_info_); |
| 183 | 186 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 212 | 215 |
| 213 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() { | 216 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() { |
| 214 return Singleton<StatisticsProviderImpl, | 217 return Singleton<StatisticsProviderImpl, |
| 215 DefaultSingletonTraits<StatisticsProviderImpl> >::get(); | 218 DefaultSingletonTraits<StatisticsProviderImpl> >::get(); |
| 216 } | 219 } |
| 217 | 220 |
| 218 // The stub StatisticsProvider implementation used on Linux desktop. | 221 // The stub StatisticsProvider implementation used on Linux desktop. |
| 219 class StatisticsProviderStubImpl : public StatisticsProvider { | 222 class StatisticsProviderStubImpl : public StatisticsProvider { |
| 220 public: | 223 public: |
| 221 // StatisticsProvider implementation: | 224 // StatisticsProvider implementation: |
| 222 virtual void Init() OVERRIDE { | 225 virtual void Init() OVERRIDE {} |
| 223 } | 226 |
| 227 virtual void StartLoadingMachineStatistics() OVERRIDE {} |
| 224 | 228 |
| 225 virtual bool GetMachineStatistic(const std::string& name, | 229 virtual bool GetMachineStatistic(const std::string& name, |
| 226 std::string* result) OVERRIDE { | 230 std::string* result) OVERRIDE { |
| 227 if (name == "CHROMEOS_RELEASE_BOARD") { | 231 if (name == "CHROMEOS_RELEASE_BOARD") { |
| 228 // Note: syncer::GetSessionNameSynchronously() also uses the mechanism | 232 // Note: syncer::GetSessionNameSynchronously() also uses the mechanism |
| 229 // below to determine the CrOs release board. However, it cannot include | 233 // below to determine the CrOs release board. However, it cannot include |
| 230 // statistics_provider.h and use this method because of the mutual | 234 // statistics_provider.h and use this method because of the mutual |
| 231 // dependency that creates between sync.gyp:sync and chrome.gyp:browser. | 235 // dependency that creates between sync.gyp:sync and chrome.gyp:browser. |
| 232 // TODO(rsimha): Update syncer::GetSessionNameSynchronously() if this code | 236 // TODO(rsimha): Update syncer::GetSessionNameSynchronously() if this code |
| 233 // is ever moved into base/. See http://crbug.com/126732. | 237 // is ever moved into base/. See http://crbug.com/126732. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 258 StatisticsProvider* StatisticsProvider::GetInstance() { | 262 StatisticsProvider* StatisticsProvider::GetInstance() { |
| 259 if (base::chromeos::IsRunningOnChromeOS()) { | 263 if (base::chromeos::IsRunningOnChromeOS()) { |
| 260 return StatisticsProviderImpl::GetInstance(); | 264 return StatisticsProviderImpl::GetInstance(); |
| 261 } else { | 265 } else { |
| 262 return StatisticsProviderStubImpl::GetInstance(); | 266 return StatisticsProviderStubImpl::GetInstance(); |
| 263 } | 267 } |
| 264 } | 268 } |
| 265 | 269 |
| 266 } // namespace system | 270 } // namespace system |
| 267 } // namespace chromeos | 271 } // namespace chromeos |
| OLD | NEW |