Chromium Code Reviews| 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 "chromeos/system/statistics_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/location.h" | |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/singleton.h" | |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
| 15 #include "base/task_runner.h" | |
| 16 #include "base/task_runner_util.h" | |
| 15 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 17 #include "chromeos/app_mode/kiosk_oem_manifest_parser.h" | 19 #include "chromeos/app_mode/kiosk_oem_manifest_parser.h" |
| 18 #include "chromeos/chromeos_constants.h" | 20 #include "chromeos/chromeos_constants.h" |
| 19 #include "chromeos/chromeos_switches.h" | 21 #include "chromeos/chromeos_switches.h" |
| 20 #include "chromeos/system/name_value_pairs_parser.h" | 22 #include "chromeos/system/name_value_pairs_parser.h" |
| 21 #include "content/public/browser/browser_thread.h" | |
| 22 | |
| 23 using content::BrowserThread; | |
| 24 | 23 |
| 25 namespace chromeos { | 24 namespace chromeos { |
| 26 namespace system { | 25 namespace system { |
| 26 | |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // Path to the tool used to get system info, and delimiters for the output | 29 // Path to the tool used to get system info, and delimiters for the output |
| 30 // format of the tool. | 30 // format of the tool. |
| 31 const char* kCrosSystemTool[] = { "/usr/bin/crossystem" }; | 31 const char* kCrosSystemTool[] = { "/usr/bin/crossystem" }; |
| 32 const char kCrosSystemEq[] = "="; | 32 const char kCrosSystemEq[] = "="; |
| 33 const char kCrosSystemDelim[] = "\n"; | 33 const char kCrosSystemDelim[] = "\n"; |
| 34 const char kCrosSystemCommentDelim[] = "#"; | 34 const char kCrosSystemCommentDelim[] = "#"; |
| 35 const char kCrosSystemUnknownValue[] = "(error)"; | 35 const char kCrosSystemUnknownValue[] = "(error)"; |
| 36 | 36 |
| 37 const char kHardwareClassCrosSystemKey[] = "hwid"; | 37 const char kHardwareClassCrosSystemKey[] = "hwid"; |
| 38 const char kHardwareClassKey[] = "hardware_class"; | |
| 39 const char kUnknownHardwareClass[] = "unknown"; | 38 const char kUnknownHardwareClass[] = "unknown"; |
| 40 | 39 |
| 41 // File to get machine hardware info from, and key/value delimiters of | 40 // File to get machine hardware info from, and key/value delimiters of |
| 42 // the file. | 41 // the file. |
| 43 // /tmp/machine-info is generated by platform/init/chromeos_startup. | 42 // /tmp/machine-info is generated by platform/init/chromeos_startup. |
| 44 const char kMachineHardwareInfoFile[] = "/tmp/machine-info"; | 43 const char kMachineHardwareInfoFile[] = "/tmp/machine-info"; |
| 45 const char kMachineHardwareInfoEq[] = "="; | 44 const char kMachineHardwareInfoEq[] = "="; |
| 46 const char kMachineHardwareInfoDelim[] = " \n"; | 45 const char kMachineHardwareInfoDelim[] = " \n"; |
| 47 | 46 |
| 48 // File to get ECHO coupon info from, and key/value delimiters of | 47 // File to get ECHO coupon info from, and key/value delimiters of |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 60 const int kTimeoutSecs = 3; | 59 const int kTimeoutSecs = 3; |
| 61 | 60 |
| 62 // The location of OEM manifest file used to trigger OOBE flow for kiosk mode. | 61 // The location of OEM manifest file used to trigger OOBE flow for kiosk mode. |
| 63 const CommandLine::CharType kOemManifestFilePath[] = | 62 const CommandLine::CharType kOemManifestFilePath[] = |
| 64 FILE_PATH_LITERAL("/usr/share/oem/oobe/manifest.json"); | 63 FILE_PATH_LITERAL("/usr/share/oem/oobe/manifest.json"); |
| 65 | 64 |
| 66 } // namespace | 65 } // namespace |
| 67 | 66 |
| 68 // Key values for GetMachineStatistic()/GetMachineFlag() calls. | 67 // Key values for GetMachineStatistic()/GetMachineFlag() calls. |
| 69 const char kDevSwitchBootMode[] = "devsw_boot"; | 68 const char kDevSwitchBootMode[] = "devsw_boot"; |
| 70 const char kHardwareClass[] = "hardware_class"; | 69 const char kHardwareClassKey[] = "hardware_class"; |
| 71 const char kOffersCouponCodeKey[] = "ubind_attribute"; | 70 const char kOffersCouponCodeKey[] = "ubind_attribute"; |
| 72 const char kOffersGroupCodeKey[] = "gbind_attribute"; | 71 const char kOffersGroupCodeKey[] = "gbind_attribute"; |
| 73 const char kOemCanExitEnterpriseEnrollmentKey[] = | 72 const char kOemCanExitEnterpriseEnrollmentKey[] = "oem_can_exit_enrollment"; |
| 74 "oem_can_exit_enrollment"; | 73 const char kOemDeviceRequisitionKey[] = "oem_device_requisition"; |
| 75 const char kOemDeviceRequisitionKey[] = | 74 const char kOemIsEnterpriseManagedKey[] = "oem_enterprise_managed"; |
| 76 "oem_device_requisition"; | 75 const char kOemKeyboardDrivenOobeKey[] = "oem_keyboard_driven_oobe"; |
| 77 const char kOemIsEnterpriseManagedKey[] = | |
| 78 "oem_enterprise_managed"; | |
| 79 const char kOemKeyboardDrivenOobeKey[] = | |
| 80 "oem_keyboard_driven_oobe"; | |
| 81 | 76 |
| 82 // The StatisticsProvider implementation used in production. | 77 // The StatisticsProvider implementation used in production. |
| 83 class StatisticsProviderImpl : public StatisticsProvider { | 78 class StatisticsProviderImpl : public StatisticsProvider { |
| 84 public: | 79 public: |
| 80 explicit StatisticsProviderImpl( | |
| 81 const scoped_refptr<base::TaskRunner>& file_task_runner); | |
| 82 virtual ~StatisticsProviderImpl(); | |
| 83 | |
| 85 // StatisticsProvider implementation: | 84 // StatisticsProvider implementation: |
| 86 virtual void Init() OVERRIDE; | 85 virtual void StartLoadingMachineStatistics(bool load_oem_manifest) OVERRIDE; |
| 87 virtual void StartLoadingMachineStatistics() OVERRIDE; | |
| 88 virtual bool GetMachineStatistic(const std::string& name, | 86 virtual bool GetMachineStatistic(const std::string& name, |
| 89 std::string* result) OVERRIDE; | 87 std::string* result) OVERRIDE; |
| 90 virtual bool GetMachineFlag(const std::string& name, | 88 virtual bool GetMachineFlag(const std::string& name, bool* result) OVERRIDE; |
| 91 bool* result) OVERRIDE; | |
| 92 virtual void LoadOemManifest() OVERRIDE; | |
| 93 | |
| 94 static StatisticsProviderImpl* GetInstance(); | |
| 95 | 89 |
| 96 protected: | 90 protected: |
| 97 StatisticsProviderImpl(); | 91 typedef std::map<std::string, bool> MachineFlags; |
| 92 | |
| 93 // Waits up to |kTimeoutSecs| for statistics to be loaded. Returns true if | |
| 94 // they were loaded successfully. | |
| 95 bool WaitForStatisticsLoaded(); | |
| 96 | |
| 97 // Loads the machine statistics off of disk. Runs on the file thread. | |
| 98 void LoadMachineStatistics(bool load_oem_manifest); | |
| 99 | |
| 100 // Loads the OEM statistics off of disk. Runs on the file thread. | |
| 98 void LoadOemManifestFromFile(const base::FilePath& file); | 101 void LoadOemManifestFromFile(const base::FilePath& file); |
| 99 | 102 |
| 100 private: | |
| 101 typedef std::map<std::string, bool> MachineFlags; | |
| 102 friend struct DefaultSingletonTraits<StatisticsProviderImpl>; | |
| 103 | |
| 104 // Loads the machine statistcs by examining the system. | |
| 105 void LoadMachineStatistics(); | |
| 106 | |
| 107 bool initialized_; | |
| 108 bool load_statistics_started_; | 103 bool load_statistics_started_; |
| 104 scoped_refptr<base::TaskRunner> file_task_runner_; | |
| 105 // |on_statistics_loaded_| protects |machine_info_| and |machine_flags_|. | |
| 106 base::WaitableEvent on_statistics_loaded_; | |
| 109 NameValuePairsParser::NameValueMap machine_info_; | 107 NameValuePairsParser::NameValueMap machine_info_; |
| 110 MachineFlags machine_flags_; | 108 MachineFlags machine_flags_; |
| 111 base::WaitableEvent on_statistics_loaded_; | 109 |
| 110 private: | |
| 111 base::WeakPtrFactory<StatisticsProviderImpl> weak_ptr_factory_; | |
| 112 | 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); | 113 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl); |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 void StatisticsProviderImpl::Init() { | 116 bool StatisticsProviderImpl::WaitForStatisticsLoaded() { |
| 117 DCHECK(!initialized_); | 117 CHECK(load_statistics_started_); |
| 118 initialized_ = true; | 118 if (on_statistics_loaded_.IsSignaled()) |
| 119 return true; | |
| 120 | |
| 121 // Block if the statistics are not loaded yet. Normally this shouldn't | |
| 122 // happen excpet during OOBE. | |
| 123 base::Time start_time = base::Time::Now(); | |
| 124 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 125 on_statistics_loaded_.TimedWait(base::TimeDelta::FromSeconds(kTimeoutSecs)); | |
| 126 | |
| 127 base::TimeDelta dtime = base::Time::Now() - start_time; | |
| 128 if (on_statistics_loaded_.IsSignaled()) { | |
| 129 LOG(ERROR) << "Statistics loaded after waiting " | |
| 130 << dtime.InMilliseconds() << "ms. "; | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 134 LOG(ERROR) << "Statistics not loaded after waiting " | |
| 135 << dtime.InMilliseconds() << "ms. "; | |
| 136 return false; | |
| 119 } | 137 } |
| 120 | 138 |
| 121 bool StatisticsProviderImpl::GetMachineStatistic( | 139 bool StatisticsProviderImpl::GetMachineStatistic( |
| 122 const std::string& name, std::string* result) { | 140 const std::string& name, std::string* result) { |
| 123 CHECK(initialized_); | 141 VLOG(1) << "Machine Statistic requested: " << name; |
| 124 CHECK(load_statistics_started_) | 142 if (!WaitForStatisticsLoaded()) { |
| 125 << "GetMachineStatistic called before load started: " << name; | 143 LOG(ERROR) << "GetMachineStatistic called before load started: " << name; |
| 126 | 144 return false; |
| 127 VLOG(1) << "Statistic is requested for " << name; | |
| 128 // Block if the statistics are not loaded yet. Per LOG(WARNING) below, | |
| 129 // the statistics are loaded before requested as of now. For regular | |
| 130 // sessions (i.e. not OOBE), statistics are first requested when the | |
| 131 // user is logging in so we have plenty of time to load the data | |
| 132 // beforehand. | |
| 133 // | |
| 134 // If you see the warning appeared for regular sessions, it probably | |
| 135 // means that there is new client code that uses the statistics in the | |
| 136 // very early stage of the browser startup. The statistic name should be | |
| 137 // helpful to identify the caller. | |
| 138 if (!on_statistics_loaded_.IsSignaled()) { | |
| 139 // http://crbug.com/125385 | |
| 140 base::Time start_time = base::Time::Now(); | |
| 141 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 142 on_statistics_loaded_.TimedWait(base::TimeDelta::FromSeconds(kTimeoutSecs)); | |
| 143 base::TimeDelta dtime = base::Time::Now() - start_time; | |
| 144 if (!on_statistics_loaded_.IsSignaled()) { | |
| 145 LOG(ERROR) << "Statistics weren't loaded after waiting " | |
| 146 << dtime.InMilliseconds() << "ms. " | |
| 147 << "Requested statistic: " << name; | |
| 148 return false; | |
| 149 } else { | |
| 150 LOG(ERROR) << "Statistic loaded after waiting " | |
| 151 << dtime.InMilliseconds() << "ms. " | |
| 152 << "Requested statistic: " << name; | |
| 153 } | |
| 154 } | 145 } |
| 155 | 146 |
| 156 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name); | 147 NameValuePairsParser::NameValueMap::iterator iter = machine_info_.find(name); |
| 157 if (iter != machine_info_.end()) { | 148 if (iter == machine_info_.end()) { |
| 158 *result = iter->second; | 149 if (base::SysInfo::IsRunningOnChromeOS()) |
| 159 return true; | 150 LOG(ERROR) << "Requested statistic not found: " << name; |
| 151 return false; | |
| 160 } | 152 } |
| 161 return false; | 153 *result = iter->second; |
| 154 return true; | |
| 162 } | 155 } |
| 163 | 156 |
| 164 bool StatisticsProviderImpl::GetMachineFlag( | 157 bool StatisticsProviderImpl::GetMachineFlag(const std::string& name, |
| 165 const std::string& name, bool* result) { | 158 bool* result) { |
| 166 MachineFlags::const_iterator iter = machine_flags_.find(name); | 159 VLOG(1) << "Machine Flag requested: " << name; |
| 167 if (iter != machine_flags_.end()) { | 160 if (!WaitForStatisticsLoaded()) { |
| 168 *result = iter->second; | 161 LOG(ERROR) << "GetMachineFlag called before load started: " << name; |
| 169 return true; | 162 return false; |
| 170 } | 163 } |
| 171 | 164 |
| 172 return false; | 165 MachineFlags::const_iterator iter = machine_flags_.find(name); |
| 166 if (iter == machine_flags_.end()) { | |
| 167 if (base::SysInfo::IsRunningOnChromeOS()) | |
| 168 LOG(ERROR) << "Requested machine flag not found: " << name; | |
| 169 return false; | |
| 170 } | |
| 171 *result = iter->second; | |
| 172 return true; | |
| 173 } | 173 } |
| 174 | 174 |
| 175 void StatisticsProviderImpl::LoadOemManifest() { | 175 StatisticsProviderImpl::StatisticsProviderImpl( |
| 176 LoadOemManifestFromFile(base::FilePath(kOemManifestFilePath)); | 176 const scoped_refptr<base::TaskRunner>& file_task_runner) |
| 177 : load_statistics_started_(false), | |
| 178 file_task_runner_(file_task_runner), | |
| 179 on_statistics_loaded_(true /* manual_reset */, | |
| 180 false /* initially_signaled */), | |
| 181 weak_ptr_factory_(this) { | |
| 177 } | 182 } |
| 178 | 183 |
| 179 // manual_reset needs to be true, as we want to keep the signaled state. | 184 StatisticsProviderImpl::~StatisticsProviderImpl() { |
| 180 StatisticsProviderImpl::StatisticsProviderImpl() | |
| 181 : initialized_(false), | |
| 182 load_statistics_started_(false), | |
| 183 on_statistics_loaded_(true /* manual_reset */, | |
| 184 false /* initially_signaled */) { | |
| 185 } | 185 } |
| 186 | 186 |
| 187 void StatisticsProviderImpl::StartLoadingMachineStatistics() { | 187 void StatisticsProviderImpl::StartLoadingMachineStatistics( |
| 188 DCHECK(initialized_); | 188 bool load_oem_manifest) { |
| 189 DCHECK(!load_statistics_started_); | 189 CHECK(!load_statistics_started_); |
|
satorux1
2013/10/04 08:07:56
DCHECK?
stevenjb
2013/10/04 18:48:19
We don't run DEBUG builds on devices often enough.
| |
| 190 load_statistics_started_ = true; | 190 load_statistics_started_ = true; |
| 191 | 191 |
| 192 VLOG(1) << "Started loading statistics"; | 192 VLOG(1) << "Started loading statistics. Load OEM Manifest: " |
| 193 BrowserThread::PostBlockingPoolTask( | 193 << load_oem_manifest; |
| 194 | |
| 195 file_task_runner_->PostTask( | |
| 194 FROM_HERE, | 196 FROM_HERE, |
| 195 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics, | 197 base::Bind(&StatisticsProviderImpl::LoadMachineStatistics, |
| 196 base::Unretained(this))); | 198 weak_ptr_factory_.GetWeakPtr(), |
|
satorux1
2013/10/04 08:07:56
you cannot use a weak ptr to post a task to anothe
satorux1
2013/10/04 08:13:00
I think it will DCHECK-fail with a Debug build.
stevenjb
2013/10/04 18:48:19
Ugh, right. I had it that way, but on_statistics_l
| |
| 199 load_oem_manifest)); | |
| 197 } | 200 } |
| 198 | 201 |
| 199 void StatisticsProviderImpl::LoadMachineStatistics() { | 202 void StatisticsProviderImpl::LoadMachineStatistics(bool load_oem_manifest) { |
| 203 // Parse all of the key/value pairs from the crossystem tool. | |
| 200 NameValuePairsParser parser(&machine_info_); | 204 NameValuePairsParser parser(&machine_info_); |
| 201 | 205 if (!parser.ParseNameValuePairsFromTool(arraysize(kCrosSystemTool), |
| 202 // Parse all of the key/value pairs from the crossystem tool. | 206 kCrosSystemTool, |
| 203 if (!parser.ParseNameValuePairsFromTool( | 207 kCrosSystemEq, |
| 204 arraysize(kCrosSystemTool), kCrosSystemTool, kCrosSystemEq, | 208 kCrosSystemDelim, |
| 205 kCrosSystemDelim, kCrosSystemCommentDelim)) { | 209 kCrosSystemCommentDelim)) { |
| 206 LOG(WARNING) << "There were errors parsing the output of " | 210 LOG(ERROR) << "Errors parsing output from: " << kCrosSystemTool; |
| 207 << kCrosSystemTool << "."; | |
| 208 } | 211 } |
| 209 | 212 |
| 210 // Ensure that the hardware class key is present with the expected | 213 // Ensure that the hardware class key is present with the expected |
| 211 // key name, and if it couldn't be retrieved, that the value is "unknown". | 214 // key name, and if it couldn't be retrieved, that the value is "unknown". |
| 212 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; | 215 std::string hardware_class = machine_info_[kHardwareClassCrosSystemKey]; |
| 213 if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue) | 216 if (hardware_class.empty() || hardware_class == kCrosSystemUnknownValue) |
| 214 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; | 217 machine_info_[kHardwareClassKey] = kUnknownHardwareClass; |
| 215 else | 218 else |
| 216 machine_info_[kHardwareClassKey] = hardware_class; | 219 machine_info_[kHardwareClassKey] = hardware_class; |
| 217 | 220 |
| 218 parser.GetNameValuePairsFromFile(base::FilePath(kMachineHardwareInfoFile), | 221 parser.GetNameValuePairsFromFile(base::FilePath(kMachineHardwareInfoFile), |
| 219 kMachineHardwareInfoEq, | 222 kMachineHardwareInfoEq, |
| 220 kMachineHardwareInfoDelim); | 223 kMachineHardwareInfoDelim); |
| 221 parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile), | 224 parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile), |
| 222 kEchoCouponEq, | 225 kEchoCouponEq, |
| 223 kEchoCouponDelim); | 226 kEchoCouponDelim); |
| 224 parser.GetNameValuePairsFromFile(base::FilePath(kVpdFile), kVpdEq, kVpdDelim); | 227 parser.GetNameValuePairsFromFile(base::FilePath(kVpdFile), kVpdEq, kVpdDelim); |
| 225 | 228 |
| 229 if (load_oem_manifest) | |
| 230 LoadOemManifestFromFile(base::FilePath(kOemManifestFilePath)); | |
| 231 | |
| 226 // Finished loading the statistics. | 232 // Finished loading the statistics. |
| 227 on_statistics_loaded_.Signal(); | 233 on_statistics_loaded_.Signal(); |
| 228 VLOG(1) << "Finished loading statistics"; | 234 VLOG(1) << "Finished loading statistics."; |
| 229 } | 235 } |
| 230 | 236 |
| 231 void StatisticsProviderImpl::LoadOemManifestFromFile( | 237 void StatisticsProviderImpl::LoadOemManifestFromFile( |
| 232 const base::FilePath& file) { | 238 const base::FilePath& file) { |
| 233 KioskOemManifestParser::Manifest oem_manifest; | 239 KioskOemManifestParser::Manifest oem_manifest; |
| 234 if (!KioskOemManifestParser::Load(file, &oem_manifest)) | 240 if (!KioskOemManifestParser::Load(file, &oem_manifest)) { |
| 241 LOG(WARNING) << "Unable to load OEM Manifest file: " << file.value(); | |
| 235 return; | 242 return; |
| 236 | 243 } |
| 237 machine_info_[kOemDeviceRequisitionKey] = | 244 machine_info_[kOemDeviceRequisitionKey] = |
| 238 oem_manifest.device_requisition; | 245 oem_manifest.device_requisition; |
| 239 machine_flags_[kOemIsEnterpriseManagedKey] = | 246 machine_flags_[kOemIsEnterpriseManagedKey] = |
| 240 oem_manifest.enterprise_managed; | 247 oem_manifest.enterprise_managed; |
| 241 machine_flags_[kOemCanExitEnterpriseEnrollmentKey] = | 248 machine_flags_[kOemCanExitEnterpriseEnrollmentKey] = |
| 242 oem_manifest.can_exit_enrollment; | 249 oem_manifest.can_exit_enrollment; |
| 243 machine_flags_[kOemKeyboardDrivenOobeKey] = | 250 machine_flags_[kOemKeyboardDrivenOobeKey] = |
| 244 oem_manifest.keyboard_driven_oobe; | 251 oem_manifest.keyboard_driven_oobe; |
| 245 } | |
| 246 | 252 |
| 247 StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() { | 253 VLOG(1) << "Loaded OEM Manifest statistics from " << file.value(); |
| 248 return Singleton<StatisticsProviderImpl, | |
| 249 DefaultSingletonTraits<StatisticsProviderImpl> >::get(); | |
| 250 } | 254 } |
| 251 | 255 |
| 252 // The stub StatisticsProvider implementation used on Linux desktop. | 256 // The stub StatisticsProvider implementation used on Linux desktop. |
| 253 class StatisticsProviderStubImpl : public StatisticsProviderImpl { | 257 class StatisticsProviderStubImpl : public StatisticsProviderImpl { |
| 254 public: | 258 public: |
| 255 // StatisticsProvider implementation: | 259 explicit StatisticsProviderStubImpl( |
| 256 virtual void Init() OVERRIDE {} | 260 const scoped_refptr<base::TaskRunner>& file_task_runner) |
| 257 | 261 : StatisticsProviderImpl(file_task_runner), |
| 258 virtual void StartLoadingMachineStatistics() OVERRIDE {} | 262 weak_ptr_factory_stub_(this) { |
| 259 | 263 } |
| 260 virtual bool GetMachineStatistic(const std::string& name, | 264 virtual ~StatisticsProviderStubImpl() { |
| 261 std::string* result) OVERRIDE { | |
| 262 if (name == "CHROMEOS_RELEASE_BOARD") { | |
| 263 // Note: syncer::GetSessionNameSynchronously() also uses the mechanism | |
| 264 // below to determine the CrOs release board. However, it cannot include | |
| 265 // statistics_provider.h and use this method because of the mutual | |
| 266 // dependency that creates between sync.gyp:sync and chrome.gyp:browser. | |
| 267 // TODO(rsimha): Update syncer::GetSessionNameSynchronously() if this code | |
| 268 // is ever moved into base/. See http://crbug.com/126732. | |
| 269 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 270 if (command_line->HasSwitch(chromeos::switches::kChromeOSReleaseBoard)) { | |
| 271 *result = command_line-> | |
| 272 GetSwitchValueASCII(chromeos::switches::kChromeOSReleaseBoard); | |
| 273 return true; | |
| 274 } | |
| 275 } | |
| 276 return false; | |
| 277 } | 265 } |
| 278 | 266 |
| 279 virtual void LoadOemManifest() OVERRIDE { | 267 // StatisticsProvider implementation: |
| 280 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 268 virtual void StartLoadingMachineStatistics(bool load_oem_manifest) OVERRIDE { |
| 281 if (!command_line->HasSwitch(switches::kAppOemManifestFile)) | 269 CHECK(!load_statistics_started_); |
|
satorux1
2013/10/04 08:07:56
ditto
| |
| 282 return; | 270 load_statistics_started_ = true; |
| 283 | 271 |
| 284 LoadOemManifestFromFile( | 272 VLOG(1) << "Started loading STUB statistics. Load OEM Manifest: " |
| 285 command_line->GetSwitchValuePath(switches::kAppOemManifestFile)); | 273 << load_oem_manifest; |
| 286 } | |
| 287 | 274 |
| 288 static StatisticsProviderStubImpl* GetInstance() { | 275 file_task_runner_->PostTask( |
| 289 return Singleton<StatisticsProviderStubImpl, | 276 FROM_HERE, |
| 290 DefaultSingletonTraits<StatisticsProviderStubImpl> >::get(); | 277 base::Bind(&StatisticsProviderStubImpl::LoadStubMachineStatistics, |
| 278 weak_ptr_factory_stub_.GetWeakPtr(), | |
|
satorux1
2013/10/04 08:07:56
likewise, you cannot use weak ptr for posting task
| |
| 279 load_oem_manifest)); | |
| 291 } | 280 } |
| 292 | 281 |
| 293 private: | 282 private: |
| 294 friend struct DefaultSingletonTraits<StatisticsProviderStubImpl>; | 283 void LoadStubMachineStatistics(bool load_oem_manifest) { |
| 284 if (load_oem_manifest) { | |
| 285 // If kAppOemManifestFile switch is specified, load OEM Manifest file. | |
| 286 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 287 if (command_line->HasSwitch(switches::kAppOemManifestFile)) { | |
| 288 LoadOemManifestFromFile( | |
| 289 command_line->GetSwitchValuePath(switches::kAppOemManifestFile)); | |
| 290 } | |
| 291 } | |
| 292 // Finished loading the statistics. | |
| 293 on_statistics_loaded_.Signal(); | |
| 294 VLOG(1) << "Finished loading STUB statistics"; | |
| 295 } | |
| 295 | 296 |
| 296 StatisticsProviderStubImpl() { | 297 base::WeakPtrFactory<StatisticsProviderStubImpl> weak_ptr_factory_stub_; |
| 297 } | |
| 298 | 298 |
| 299 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderStubImpl); | 299 DISALLOW_COPY_AND_ASSIGN(StatisticsProviderStubImpl); |
| 300 }; | 300 }; |
| 301 | 301 |
| 302 static StatisticsProvider* g_statistics_provider = NULL; | |
| 303 | |
| 304 // static | |
| 305 void StatisticsProvider::Initialize( | |
| 306 const scoped_refptr<base::TaskRunner>& file_task_runner) { | |
| 307 CHECK(!g_statistics_provider); | |
| 308 if (base::SysInfo::IsRunningOnChromeOS()) { | |
| 309 g_statistics_provider = new StatisticsProviderImpl(file_task_runner); | |
| 310 } else { | |
| 311 g_statistics_provider = new StatisticsProviderStubImpl(file_task_runner); | |
| 312 } | |
| 313 } | |
| 314 | |
| 315 // static | |
| 316 void StatisticsProvider::Shutdown() { | |
| 317 delete g_statistics_provider; | |
| 318 g_statistics_provider = NULL; | |
| 319 } | |
| 320 | |
| 302 StatisticsProvider* StatisticsProvider::GetInstance() { | 321 StatisticsProvider* StatisticsProvider::GetInstance() { |
| 303 if (base::SysInfo::IsRunningOnChromeOS()) { | 322 CHECK(g_statistics_provider); |
| 304 return StatisticsProviderImpl::GetInstance(); | 323 return g_statistics_provider; |
| 305 } else { | |
| 306 return StatisticsProviderStubImpl::GetInstance(); | |
| 307 } | |
| 308 } | 324 } |
| 309 | 325 |
| 310 } // namespace system | 326 } // namespace system |
| 311 } // namespace chromeos | 327 } // namespace chromeos |
| OLD | NEW |