Chromium Code Reviews| Index: chrome/browser/extensions/api/systeminfo_cpu/systeminfo_cpu_api.cc |
| diff --git a/chrome/browser/extensions/api/systeminfo_cpu/systeminfo_cpu_api.cc b/chrome/browser/extensions/api/systeminfo_cpu/systeminfo_cpu_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3fd9e8b97af418e5888a8b1d77b1b3f187b562ad |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/systeminfo_cpu/systeminfo_cpu_api.cc |
| @@ -0,0 +1,84 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +#include "chrome/browser/extensions/api/systeminfo_cpu/systeminfo_cpu_api.h" |
| + |
| +#include "base/logging.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "chrome/browser/extensions/api/systeminfo_cpu/cpu_info_provider.h" |
| + |
| +namespace extensions { |
| +using api::experimental_systeminfo_cpu::CpuInfo; |
| +using api::experimental_systeminfo_cpu::CpuCoreInfo; |
| +using content::BrowserThread; |
| + |
| +SysteminfoCpuGetFunction::SysteminfoCpuGetFunction() { |
| +} |
| + |
| +SysteminfoCpuGetFunction::~SysteminfoCpuGetFunction() { |
| + // Delete the provider that was created on FILE thread. |
| + BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, provider_); |
| +} |
| + |
| +bool SysteminfoCpuGetFunction::RunImpl() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&SysteminfoCpuGetFunction::WorkOnFileThread, this)); |
| + return true; |
| +} |
| + |
| +void SysteminfoCpuGetFunction::GetCpuInfoOnFileThread() { |
|
Mihai Parparita -not on Chrome
2012/08/10 20:24:56
Can you move this to be below WorkOnFileThread, so
Hongbo Min
2012/08/12 14:22:44
Done.
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + bool success = false; |
| + |
| + CpuInfo info; |
| + if (provider_ && provider_->GetCpuInfo(&info)) { |
| + SetResult(info.ToValue().release()); |
| + success = true; |
| + } else { |
| + SetError("Error in querying cpu information!"); |
| + } |
| + // Respond on UI thread. |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&SysteminfoCpuGetFunction::RespondOnUIThread, this, success)); |
| +} |
| + |
| +void SysteminfoCpuGetFunction::WorkOnFileThread() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + provider_ = CpuInfoProvider::Create(); |
|
Mihai Parparita -not on Chrome
2012/08/10 20:24:56
It seems expensive to create a provider per functi
Hongbo Min
2012/08/12 14:22:44
Yes, it makes lots of senses to make it as a singl
|
| + // Delay 500ms to give a chance for CPU information sampling. |
|
Mihai Parparita -not on Chrome
2012/08/10 20:24:56
The 500ms delay seems arbitrary and prone to flaki
Hongbo Min
2012/08/12 14:22:44
The 500ms is a tricky for getting cpu usage per co
|
| + BrowserThread::PostDelayedTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&SysteminfoCpuGetFunction::GetCpuInfoOnFileThread, this), |
| + base::TimeDelta::FromMilliseconds(500)); |
| +} |
| + |
| +void SysteminfoCpuGetFunction::RespondOnUIThread(bool success) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + SendResponse(success); |
| +} |
| + |
| +// A mock implementation of CpuInfoProver for temporary usage. |
| +// Will be moved out when the platform specific implementation are done. |
| +class MockCpuInfoProvider : public CpuInfoProvider { |
| + public: |
| + MockCpuInfoProvider() {} |
| + virtual ~MockCpuInfoProvider() {} |
| + virtual bool GetCpuInfo(CpuInfo* info) OVERRIDE; |
| +}; |
| + |
| +bool MockCpuInfoProvider::GetCpuInfo(CpuInfo* info) { |
| + if (!info) return false; |
| + linked_ptr<CpuCoreInfo> core(new CpuCoreInfo()); |
| + core->load = 53; |
| + info->cores.push_back(core); |
| + return true; |
| +} |
| + |
| +// static |
| +CpuInfoProvider* CpuInfoProvider::Create() { |
| + return new MockCpuInfoProvider(); |
| +} |
| + |
| +} // namespace extensions |