Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: chrome/browser/chromeos/system/statistics_provider.cc

Issue 10078017: Added asynchronous notification of readiness to the StatisticsProvider, and (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed Ilya's comments Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/singleton.h" 12 #include "base/memory/singleton.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/metrics/histogram.h"
15 #include "base/observer_list.h"
13 #include "base/synchronization/waitable_event.h" 16 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/thread_restrictions.h" 17 #include "base/threading/thread_restrictions.h"
15 #include "base/time.h" 18 #include "base/time.h"
16 #include "base/chromeos/chromeos_version.h" 19 #include "base/chromeos/chromeos_version.h"
17 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h" 20 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h"
18 #include "chrome/common/child_process_logging.h" 21 #include "chrome/common/child_process_logging.h"
19 #include "chrome/common/chrome_version_info.h" 22 #include "chrome/common/chrome_version_info.h"
20 #include "content/public/browser/browser_thread.h" 23 #include "content/public/browser/browser_thread.h"
21 24
22 using content::BrowserThread; 25 using content::BrowserThread;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 68
66 } // namespace 69 } // namespace
67 70
68 // The StatisticsProvider implementation used in production. 71 // The StatisticsProvider implementation used in production.
69 class StatisticsProviderImpl : public StatisticsProvider { 72 class StatisticsProviderImpl : public StatisticsProvider {
70 public: 73 public:
71 // StatisticsProvider implementation: 74 // StatisticsProvider implementation:
72 virtual bool GetMachineStatistic(const std::string& name, 75 virtual bool GetMachineStatistic(const std::string& name,
73 std::string* result) OVERRIDE; 76 std::string* result) OVERRIDE;
74 77
78 virtual void AddObserver(Observer* observer) OVERRIDE;
79 virtual void RemoveObserver(Observer* observer) OVERRIDE;
80
75 static StatisticsProviderImpl* GetInstance(); 81 static StatisticsProviderImpl* GetInstance();
76 82
77 private: 83 private:
78 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; 84 friend struct DefaultSingletonTraits<StatisticsProviderImpl>;
79 85
80 StatisticsProviderImpl(); 86 StatisticsProviderImpl();
81 87
82 // Starts loading the machine statistcs. 88 // Starts loading the machine statistcs.
83 void StartLoadingMachineStatistics(); 89 void StartLoadingMachineStatistics();
84 90
85 // Loads the machine statistcs by examining the system. 91 // Loads the machine statistics by examining the system.
86 void LoadMachineStatistics(); 92 // This is executed on the blocking pool, and replies by posting a task to
93 // the UI thread using |weak_this|.
94 void LoadMachineStatistics(base::WeakPtr<StatisticsProviderImpl> weak_this,
95 base::Time start_time);
96
97 // Posted to UI from LoadMachineStatistics() once the info has been read.
98 void OnStatisticsLoaded(base::Time start_time);
87 99
88 NameValuePairsParser::NameValueMap machine_info_; 100 NameValuePairsParser::NameValueMap machine_info_;
89 base::WaitableEvent on_statistics_loaded_; 101 base::WaitableEvent on_statistics_loaded_;
102 base::WeakPtrFactory<StatisticsProviderImpl> weak_factory_;
103 ObserverList<Observer, true> observer_list_;
90 104
91 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); 105 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl);
92 }; 106 };
93 107
94 bool StatisticsProviderImpl::GetMachineStatistic( 108 bool StatisticsProviderImpl::GetMachineStatistic(const std::string& name,
95 const std::string& name, std::string* result) { 109 std::string* result) {
96 VLOG(1) << "Statistic is requested for " << name; 110 VLOG(1) << "Statistic is requested for " << name;
97 // Block if the statistics are not loaded yet. Per LOG(WARNING) below, 111 // Block if the statistics are not loaded yet. Per DLOG(WARNING) below,
98 // the statistics are loaded before requested as of now. For regular 112 // the statistics are loaded before requested as of now. For regular
99 // sessions (i.e. not OOBE), statistics are first requested when the 113 // sessions (i.e. not OOBE), statistics are first requested when the
100 // user is logging in so we have plenty of time to load the data 114 // user is logging in so we have plenty of time to load the data
101 // beforehand. 115 // beforehand.
102 // 116 //
103 // If you see the warning appeared for regular sessions, it probably 117 // If you see the warning appeared for regular sessions, it probably
104 // means that there is new client code that uses the statistics in the 118 // means that there is new client code that uses the statistics in the
105 // very early stage of the browser startup. The statistic name should be 119 // very early stage of the browser startup. The statistic name should be
106 // helpful to identify the caller. 120 // helpful to identify the caller.
107 if (!on_statistics_loaded_.IsSignaled()) { 121 if (!on_statistics_loaded_.IsSignaled()) {
108 LOG(WARNING) << "Waiting to load statistics. Requested statistic: " 122 DLOG(WARNING) << "Waiting to load statistics. Requested statistic: "
109 << name; 123 << name;
110 // http://crbug.com/125385 124 // http://crbug.com/125385
111 base::ThreadRestrictions::ScopedAllowWait allow_wait; 125 base::ThreadRestrictions::ScopedAllowWait allow_wait;
112 on_statistics_loaded_.TimedWait(base::TimeDelta::FromSeconds(kTimeoutSecs)); 126 on_statistics_loaded_.TimedWait(base::TimeDelta::FromSeconds(kTimeoutSecs));
113 127
114 if (!on_statistics_loaded_.IsSignaled()) { 128 if (!on_statistics_loaded_.IsSignaled()) {
115 LOG(ERROR) << "Statistics weren't loaded after waiting! " 129 DLOG(ERROR) << "Statistics weren't loaded after waiting! "
116 << "Requested statistic: " << name; 130 << "Requested statistic: " << name;
131 UMA_HISTOGRAM_BOOLEAN("Cros.StatisticsProviderTimedOut", true);
117 return false; 132 return false;
118 } 133 }
119 } 134 }
120 135
121 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name); 136 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name);
122 if (iter != machine_info_.end()) { 137 if (iter != machine_info_.end()) {
123 *result = iter->second; 138 *result = iter->second;
124 return true; 139 return true;
125 } 140 }
126 return false; 141 return false;
127 } 142 }
128 143
144 void StatisticsProviderImpl::AddObserver(Observer* observer) {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
146 if (on_statistics_loaded_.IsSignaled()) {
jar (doing other things) 2012/05/21 18:10:02 This confuses me a bit, as we're doing signalling,
Joao da Silva 2012/05/22 14:55:25 Removing the signal would require refactoring all
147 observer->OnMachineStatisticsReady();
148 } else {
149 observer_list_.AddObserver(observer);
jar (doing other things) 2012/05/21 18:10:02 I see an observer optionally being added... but I
Joao da Silva 2012/05/22 14:55:25 Right, this is a bit confusing. Removing an observ
Joao da Silva 2012/05/22 19:10:09 I'd like your take on this matter before proceedin
jar (doing other things) 2012/05/22 20:09:30 The "callback" version was re-inventing the wheel.
150 }
151 }
152
153 void StatisticsProviderImpl::RemoveObserver(Observer* observer) {
154 observer_list_.RemoveObserver(observer);
155 }
156
129 // manual_reset needs to be true, as we want to keep the signaled state. 157 // manual_reset needs to be true, as we want to keep the signaled state.
130 StatisticsProviderImpl::StatisticsProviderImpl() 158 StatisticsProviderImpl::StatisticsProviderImpl()
131 : on_statistics_loaded_(true /* manual_reset */, 159 : on_statistics_loaded_(true /* manual_reset */,
132 false /* initially_signaled */) { 160 false /* initially_signaled */),
161 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
133 StartLoadingMachineStatistics(); 162 StartLoadingMachineStatistics();
134 } 163 }
135 164
136 void StatisticsProviderImpl::StartLoadingMachineStatistics() { 165 void StatisticsProviderImpl::StartLoadingMachineStatistics() {
137 VLOG(1) << "Started loading statistics"; 166 VLOG(1) << "Started loading statistics";
138 BrowserThread::PostBlockingPoolTask( 167 BrowserThread::PostBlockingPoolTask(
139 FROM_HERE, 168 FROM_HERE,
140 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics, 169 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics,
141 base::Unretained(this))); 170 base::Unretained(this),
171 weak_factory_.GetWeakPtr(),
172 base::Time::Now()));
142 } 173 }
143 174
144 void StatisticsProviderImpl::LoadMachineStatistics() { 175 void StatisticsProviderImpl::LoadMachineStatistics(
176 base::WeakPtr<StatisticsProviderImpl> weak_this,
177 base::Time start_time) {
145 NameValuePairsParser parser(&machine_info_); 178 NameValuePairsParser parser(&machine_info_);
146 179
147 // Parse all of the key/value pairs from the crossystem tool. 180 // Parse all of the key/value pairs from the crossystem tool.
148 if (!parser.ParseNameValuePairsFromTool( 181 if (!parser.ParseNameValuePairsFromTool(
149 arraysize(kCrosSystemTool), kCrosSystemTool, kCrosSystemEq, 182 arraysize(kCrosSystemTool), kCrosSystemTool, kCrosSystemEq,
150 kCrosSystemDelim, kCrosSystemCommentDelim)) { 183 kCrosSystemDelim, kCrosSystemCommentDelim)) {
151 LOG(WARNING) << "There were errors parsing the output of " 184 DLOG(WARNING) << "There were errors parsing the output of "
152 << kCrosSystemTool << "."; 185 << kCrosSystemTool << ".";
186 UMA_HISTOGRAM_BOOLEAN("Cros.CrosSystemParsingFailed", true);
153 } 187 }
154 188
155 // Ensure that the hardware class key is present with the expected 189 // Ensure that the hardware class key is present with the expected
156 // key name, and if it couldn't be retrieved, that the value is "unknown". 190 // key name, and if it couldn't be retrieved, that the value is "unknown".
157 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; 191 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey];
158 if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue) 192 if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue)
159 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; 193 machine_info_[kHardwareClassKey] = kUnknownHardwareClass;
160 else 194 else
161 machine_info_[kHardwareClassKey] = hardware_class; 195 machine_info_[kHardwareClassKey] = hardware_class;
162 196
163 parser.GetNameValuePairsFromFile(FilePath(kMachineHardwareInfoFile), 197 parser.GetNameValuePairsFromFile(FilePath(kMachineHardwareInfoFile),
164 kMachineHardwareInfoEq, 198 kMachineHardwareInfoEq,
165 kMachineHardwareInfoDelim); 199 kMachineHardwareInfoDelim);
166 parser.GetNameValuePairsFromFile(FilePath(kEchoCouponFile), 200 parser.GetNameValuePairsFromFile(FilePath(kEchoCouponFile),
167 kEchoCouponEq, 201 kEchoCouponEq,
168 kEchoCouponDelim); 202 kEchoCouponDelim);
169 parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile), 203 parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile),
170 kMachineOSInfoEq, 204 kMachineOSInfoEq,
171 kMachineOSInfoDelim); 205 kMachineOSInfoDelim);
172 parser.GetNameValuePairsFromFile(FilePath(kVpdFile), kVpdEq, kVpdDelim); 206 parser.GetNameValuePairsFromFile(FilePath(kVpdFile), kVpdEq, kVpdDelim);
173 207
174 // Finished loading the statistics. 208 // Finished loading the statistics.
175 on_statistics_loaded_.Signal(); 209 on_statistics_loaded_.Signal();
176 VLOG(1) << "Finished loading statistics"; 210 VLOG(1) << "Finished loading statistics";
177 211
212 BrowserThread::PostTask(
213 BrowserThread::UI, FROM_HERE,
214 base::Bind(&StatisticsProviderImpl::OnStatisticsLoaded,
215 weak_this,
216 start_time));
217
178 #if defined(GOOGLE_CHROME_BUILD) 218 #if defined(GOOGLE_CHROME_BUILD)
179 // TODO(kochi): This is for providing a channel information to 219 // TODO(kochi): This is for providing a channel information to
180 // chrome::VersionInfo::GetChannel()/GetVersionStringModifier(), 220 // chrome::VersionInfo::GetChannel()/GetVersionStringModifier(),
181 // but this is still late for some early customers such as 221 // but this is still late for some early customers such as
182 // prerender::ConfigurePrefetchAndPrerender() and 222 // prerender::ConfigurePrefetchAndPrerender() and
183 // ThreadWatcherList::ParseCommandLine(). 223 // ThreadWatcherList::ParseCommandLine().
184 // See http://crbug.com/107333 . 224 // See http://crbug.com/107333 .
185 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK"; 225 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK";
186 std::string channel; 226 std::string channel;
187 if (GetMachineStatistic(kChromeOSReleaseTrack, &channel)) { 227 if (GetMachineStatistic(kChromeOSReleaseTrack, &channel)) {
188 chrome::VersionInfo::SetChannel(channel); 228 chrome::VersionInfo::SetChannel(channel);
189 // Set the product channel for crash reports. We can't just do this in 229 // Set the product channel for crash reports. We can't just do this in
190 // ChromeBrowserMainParts::PreCreateThreads like we do for Linux because 230 // ChromeBrowserMainParts::PreCreateThreads like we do for Linux because
191 // the FILE thread hasn't been created yet there so we can't possibly 231 // the FILE thread hasn't been created yet there so we can't possibly
192 // have read this yet. Note that this string isn't exactly the same as 232 // have read this yet. Note that this string isn't exactly the same as
193 // 'channel', it's been parsed to be consistent with other platforms 233 // 'channel', it's been parsed to be consistent with other platforms
194 // (eg. "canary-channel" becomes "canary", "testimage-channel" becomes 234 // (eg. "canary-channel" becomes "canary", "testimage-channel" becomes
195 // "unknown"). 235 // "unknown").
196 child_process_logging::SetChannel( 236 child_process_logging::SetChannel(
197 chrome::VersionInfo::GetVersionStringModifier()); 237 chrome::VersionInfo::GetVersionStringModifier());
198 } 238 }
199 #endif 239 #endif
200 } 240 }
201 241
242 void StatisticsProviderImpl::OnStatisticsLoaded(base::Time start_time) {
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
244 base::TimeDelta elapsed = base::Time::Now() - start_time;
245 UMA_HISTOGRAM_LONG_TIMES("Cros.StatisticsProviderReadyDelay", elapsed);
246 FOR_EACH_OBSERVER(Observer, observer_list_, OnMachineStatisticsReady());
jar (doing other things) 2012/05/21 18:10:02 This, for instance, would be a great place to remo
jar (doing other things) 2012/05/22 20:09:30 Note that it would get messy if you tried to use a
247 }
248
202 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() { 249 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() {
203 return Singleton<StatisticsProviderImpl, 250 return Singleton<StatisticsProviderImpl,
204 DefaultSingletonTraits<StatisticsProviderImpl> >::get(); 251 DefaultSingletonTraits<StatisticsProviderImpl> >::get();
205 } 252 }
206 253
207 // The stub StatisticsProvider implementation used on Linux desktop. 254 // The stub StatisticsProvider implementation used on Linux desktop.
208 class StatisticsProviderStubImpl : public StatisticsProvider { 255 class StatisticsProviderStubImpl : public StatisticsProvider {
209 public: 256 public:
210 // StatisticsProvider implementation: 257 // StatisticsProvider implementation:
211 virtual bool GetMachineStatistic(const std::string& name, 258 virtual bool GetMachineStatistic(const std::string& name,
212 std::string* result) OVERRIDE { 259 std::string* result) OVERRIDE {
213 return false; 260 return false;
214 } 261 }
215 262
263 virtual void AddObserver(Observer* observer) OVERRIDE {
264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
265 observer->OnMachineStatisticsReady();
266 }
267
268 virtual void RemoveObserver(Observer* observer) OVERRIDE {}
269
216 static StatisticsProviderStubImpl* GetInstance() { 270 static StatisticsProviderStubImpl* GetInstance() {
217 return Singleton<StatisticsProviderStubImpl, 271 return Singleton<StatisticsProviderStubImpl,
218 DefaultSingletonTraits<StatisticsProviderStubImpl> >::get(); 272 DefaultSingletonTraits<StatisticsProviderStubImpl> >::get();
219 } 273 }
220 274
221 private: 275 private:
222 friend struct DefaultSingletonTraits<StatisticsProviderStubImpl>; 276 friend struct DefaultSingletonTraits<StatisticsProviderStubImpl>;
223 277
224 StatisticsProviderStubImpl() { 278 StatisticsProviderStubImpl() {
225 } 279 }
226 280
227 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderStubImpl); 281 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderStubImpl);
228 }; 282 };
229 283
230 StatisticsProvider* StatisticsProvider::GetInstance() { 284 StatisticsProvider* StatisticsProvider::GetInstance() {
231 if (base::chromeos::IsRunningOnChromeOS()) { 285 if (base::chromeos::IsRunningOnChromeOS()) {
232 return StatisticsProviderImpl::GetInstance(); 286 return StatisticsProviderImpl::GetInstance();
233 } else { 287 } else {
234 return StatisticsProviderStubImpl::GetInstance(); 288 return StatisticsProviderStubImpl::GetInstance();
235 } 289 }
236 } 290 }
237 291
238 } // namespace system 292 } // namespace system
239 } // namespace chromeos 293 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698