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

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 comments Created 8 years, 8 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/message_loop.h"
13 #include "base/synchronization/waitable_event.h" 14 #include "base/synchronization/waitable_event.h"
14 #include "base/time.h" 15 #include "base/time.h"
15 #include "base/chromeos/chromeos_version.h" 16 #include "base/chromeos/chromeos_version.h"
16 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h" 17 #include "chrome/browser/chromeos/system/name_value_pairs_parser.h"
17 #include "chrome/common/child_process_logging.h" 18 #include "chrome/common/child_process_logging.h"
18 #include "chrome/common/chrome_version_info.h" 19 #include "chrome/common/chrome_version_info.h"
19 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
20 21
21 using content::BrowserThread; 22 using content::BrowserThread;
22 23
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 65
65 } // namespace 66 } // namespace
66 67
67 // The StatisticsProvider implementation used in production. 68 // The StatisticsProvider implementation used in production.
68 class StatisticsProviderImpl : public StatisticsProvider { 69 class StatisticsProviderImpl : public StatisticsProvider {
69 public: 70 public:
70 // StatisticsProvider implementation: 71 // StatisticsProvider implementation:
71 virtual bool GetMachineStatistic(const std::string& name, 72 virtual bool GetMachineStatistic(const std::string& name,
72 std::string* result) OVERRIDE; 73 std::string* result) OVERRIDE;
73 74
75 virtual void WhenReady(const base::Closure& callback) OVERRIDE;
76
74 static StatisticsProviderImpl* GetInstance(); 77 static StatisticsProviderImpl* GetInstance();
75 78
76 private: 79 private:
77 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; 80 friend struct DefaultSingletonTraits<StatisticsProviderImpl>;
78 81
79 StatisticsProviderImpl(); 82 StatisticsProviderImpl();
80 83
81 // Starts loading the machine statistcs. 84 // Starts loading the machine statistcs.
82 void StartLoadingMachineStatistics(); 85 void StartLoadingMachineStatistics();
83 86
84 // Loads the machine statistcs by examining the system. 87 // Loads the machine statistcs by examining the system.
85 void LoadMachineStatistics(); 88 void LoadMachineStatistics();
86 89
90 // Invokes all the callbacks.
91 void NotifyOnUI();
92
87 NameValuePairsParser::NameValueMap machine_info_; 93 NameValuePairsParser::NameValueMap machine_info_;
88 base::WaitableEvent on_statistics_loaded_; 94 base::WaitableEvent on_statistics_loaded_;
95 std::vector<base::Closure> callbacks_;
89 96
90 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); 97 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl);
91 }; 98 };
92 99
93 bool StatisticsProviderImpl::GetMachineStatistic( 100 bool StatisticsProviderImpl::GetMachineStatistic(
94 const std::string& name, std::string* result) { 101 const std::string& name, std::string* result) {
95 VLOG(1) << "Statistic is requested for " << name; 102 VLOG(1) << "Statistic is requested for " << name;
96 // Block if the statistics are not loaded yet. Per LOG(WARNING) below, 103 // Block if the statistics are not loaded yet. Per LOG(WARNING) below,
97 // the statistics are loaded before requested as of now. For regular 104 // the statistics are loaded before requested as of now. For regular
98 // sessions (i.e. not OOBE), statistics are first requested when the 105 // sessions (i.e. not OOBE), statistics are first requested when the
(...skipping 11 matching lines...) Expand all
110 } 117 }
111 118
112 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name); 119 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name);
113 if (iter != machine_info_.end()) { 120 if (iter != machine_info_.end()) {
114 *result = iter->second; 121 *result = iter->second;
115 return true; 122 return true;
116 } 123 }
117 return false; 124 return false;
118 } 125 }
119 126
127 void StatisticsProviderImpl::WhenReady(const base::Closure& callback) {
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
129 if (on_statistics_loaded_.IsSignaled()) {
130 // Don't assume the caller is re-entrant.
131 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
132 } else {
133 callbacks_.push_back(callback);
134 }
135 }
136
120 // manual_reset needs to be true, as we want to keep the signaled state. 137 // manual_reset needs to be true, as we want to keep the signaled state.
121 StatisticsProviderImpl::StatisticsProviderImpl() 138 StatisticsProviderImpl::StatisticsProviderImpl()
122 : on_statistics_loaded_(true /* manual_reset */, 139 : on_statistics_loaded_(true /* manual_reset */,
123 false /* initially_signaled */) { 140 false /* initially_signaled */) {
124 StartLoadingMachineStatistics(); 141 StartLoadingMachineStatistics();
125 } 142 }
126 143
127 void StatisticsProviderImpl::StartLoadingMachineStatistics() { 144 void StatisticsProviderImpl::StartLoadingMachineStatistics() {
128 VLOG(1) << "Started loading statistics"; 145 VLOG(1) << "Started loading statistics";
129 CHECK(BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) 146 CHECK(BrowserThread::IsMessageLoopValid(BrowserThread::FILE))
(...skipping 29 matching lines...) Expand all
159 kEchoCouponDelim); 176 kEchoCouponDelim);
160 parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile), 177 parser.GetNameValuePairsFromFile(FilePath(kMachineOSInfoFile),
161 kMachineOSInfoEq, 178 kMachineOSInfoEq,
162 kMachineOSInfoDelim); 179 kMachineOSInfoDelim);
163 parser.GetNameValuePairsFromFile(FilePath(kVpdFile), kVpdEq, kVpdDelim); 180 parser.GetNameValuePairsFromFile(FilePath(kVpdFile), kVpdEq, kVpdDelim);
164 181
165 // Finished loading the statistics. 182 // Finished loading the statistics.
166 on_statistics_loaded_.Signal(); 183 on_statistics_loaded_.Signal();
167 VLOG(1) << "Finished loading statistics"; 184 VLOG(1) << "Finished loading statistics";
168 185
186 // Any callbacks registed before Signal() are in |callbacks_|. Any registed
187 // after Signal() will be immediately posted. NotifyOnUI() posts the callbacks
188 // in |callbacks_| from the UI loop.
189 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
190 base::Bind(&StatisticsProviderImpl::NotifyOnUI,
191 base::Unretained(this)));
192
169 #if defined(GOOGLE_CHROME_BUILD) 193 #if defined(GOOGLE_CHROME_BUILD)
170 // TODO(kochi): This is for providing a channel information to 194 // TODO(kochi): This is for providing a channel information to
171 // chrome::VersionInfo::GetChannel()/GetVersionStringModifier(), 195 // chrome::VersionInfo::GetChannel()/GetVersionStringModifier(),
172 // but this is still late for some early customers such as 196 // but this is still late for some early customers such as
173 // prerender::ConfigurePrefetchAndPrerender() and 197 // prerender::ConfigurePrefetchAndPrerender() and
174 // ThreadWatcherList::ParseCommandLine(). 198 // ThreadWatcherList::ParseCommandLine().
175 // See http://crbug.com/107333 . 199 // See http://crbug.com/107333 .
176 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK"; 200 const char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK";
177 std::string channel; 201 std::string channel;
178 if (GetMachineStatistic(kChromeOSReleaseTrack, &channel)) { 202 if (GetMachineStatistic(kChromeOSReleaseTrack, &channel)) {
179 chrome::VersionInfo::SetChannel(channel); 203 chrome::VersionInfo::SetChannel(channel);
180 // Set the product channel for crash reports. We can't just do this in 204 // Set the product channel for crash reports. We can't just do this in
181 // ChromeBrowserMainParts::PreCreateThreads like we do for Linux because 205 // ChromeBrowserMainParts::PreCreateThreads like we do for Linux because
182 // the FILE thread hasn't been created yet there so we can't possibly 206 // the FILE thread hasn't been created yet there so we can't possibly
183 // have read this yet. Note that this string isn't exactly the same as 207 // have read this yet. Note that this string isn't exactly the same as
184 // 'channel', it's been parsed to be consistent with other platforms 208 // 'channel', it's been parsed to be consistent with other platforms
185 // (eg. "canary-channel" becomes "canary", "testimage-channel" becomes 209 // (eg. "canary-channel" becomes "canary", "testimage-channel" becomes
186 // "unknown"). 210 // "unknown").
187 child_process_logging::SetChannel( 211 child_process_logging::SetChannel(
188 chrome::VersionInfo::GetVersionStringModifier()); 212 chrome::VersionInfo::GetVersionStringModifier());
189 } 213 }
190 #endif 214 #endif
191 } 215 }
192 216
217 void StatisticsProviderImpl::NotifyOnUI() {
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
219 for (std::vector<base::Closure>::iterator it = callbacks_.begin();
220 it != callbacks_.end(); ++it) {
221 it->Run();
222 }
223 callbacks_.clear();
224 }
225
193 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() { 226 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() {
194 return Singleton<StatisticsProviderImpl, 227 return Singleton<StatisticsProviderImpl,
195 DefaultSingletonTraits<StatisticsProviderImpl> >::get(); 228 DefaultSingletonTraits<StatisticsProviderImpl> >::get();
196 } 229 }
197 230
198 // The stub StatisticsProvider implementation used on Linux desktop. 231 // The stub StatisticsProvider implementation used on Linux desktop.
199 class StatisticsProviderStubImpl : public StatisticsProvider { 232 class StatisticsProviderStubImpl : public StatisticsProvider {
200 public: 233 public:
201 // StatisticsProvider implementation: 234 // StatisticsProvider implementation:
202 virtual bool GetMachineStatistic(const std::string& name, 235 virtual bool GetMachineStatistic(const std::string& name,
203 std::string* result) OVERRIDE { 236 std::string* result) OVERRIDE {
204 return false; 237 return false;
205 } 238 }
206 239
240 virtual void WhenReady(const base::Closure& callback) OVERRIDE {
241 callback.Run();
242 }
243
207 static StatisticsProviderStubImpl* GetInstance() { 244 static StatisticsProviderStubImpl* GetInstance() {
208 return Singleton<StatisticsProviderStubImpl, 245 return Singleton<StatisticsProviderStubImpl,
209 DefaultSingletonTraits<StatisticsProviderStubImpl> >::get(); 246 DefaultSingletonTraits<StatisticsProviderStubImpl> >::get();
210 } 247 }
211 248
212 private: 249 private:
213 friend struct DefaultSingletonTraits<StatisticsProviderStubImpl>; 250 friend struct DefaultSingletonTraits<StatisticsProviderStubImpl>;
214 251
215 StatisticsProviderStubImpl() { 252 StatisticsProviderStubImpl() {
216 } 253 }
217 254
218 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderStubImpl); 255 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderStubImpl);
219 }; 256 };
220 257
221 StatisticsProvider* StatisticsProvider::GetInstance() { 258 StatisticsProvider* StatisticsProvider::GetInstance() {
222 if (base::chromeos::IsRunningOnChromeOS()) { 259 if (base::chromeos::IsRunningOnChromeOS()) {
223 return StatisticsProviderImpl::GetInstance(); 260 return StatisticsProviderImpl::GetInstance();
224 } else { 261 } else {
225 return StatisticsProviderStubImpl::GetInstance(); 262 return StatisticsProviderStubImpl::GetInstance();
226 } 263 }
227 } 264 }
228 265
229 } // namespace system 266 } // namespace system
230 } // namespace chromeos 267 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698