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

Unified Diff: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.cc

Issue 15817008: Move systemInfo.cpu API out out experimental namespace. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.cc
diff --git a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.cc b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.cc
index fbe2e2e152c2eb8a02696abae9035762665c95dc..edc56fe2ff52f729d138d57ff5738797bc63963a 100644
--- a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.cc
+++ b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.cc
@@ -5,34 +5,14 @@
#include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h"
#include "base/sys_info.h"
-#include "chrome/common/chrome_notification_types.h"
-#include "content/public/browser/browser_thread.h"
-#include "content/public/browser/notification_details.h"
-#include "content/public/browser/notification_service.h"
-#include "content/public/browser/notification_source.h"
-#include "content/public/browser/notification_types.h"
namespace extensions {
using api::system_info_cpu::CpuInfo;
-using api::system_info_cpu::CpuUpdateInfo;
-using content::BrowserThread;
-// Default sampling interval is 1000ms.
-const unsigned int kDefaultSamplingIntervalMs = 1000;
+CpuInfoProvider::CpuInfoProvider() {}
-CpuInfoProvider::CpuInfoProvider()
- : is_sampling_started_(false),
- sampling_timer_(NULL),
- sampling_interval_(kDefaultSamplingIntervalMs) {
- registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
- content::NotificationService::AllSources());
-}
-
-CpuInfoProvider::~CpuInfoProvider() {
- DCHECK(sampling_timer_ == NULL);
- registrar_.RemoveAll();
-}
+CpuInfoProvider::~CpuInfoProvider() {}
bool CpuInfoProvider::QueryInfo(CpuInfo* info) {
if (info == NULL)
@@ -44,100 +24,9 @@ bool CpuInfoProvider::QueryInfo(CpuInfo* info) {
return true;
}
-void CpuInfoProvider::StartSampling(const SamplingCallback& callback) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- BrowserThread::PostTask(
- BrowserThread::FILE,
- FROM_HERE,
- base::Bind(&CpuInfoProvider::StartSamplingOnFileThread, this, callback));
-}
-
-void CpuInfoProvider::StopSampling() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- BrowserThread::PostTask(
- BrowserThread::FILE,
- FROM_HERE,
- base::Bind(&CpuInfoProvider::StopSamplingOnFileThread, this));
-}
-
// static
CpuInfoProvider* CpuInfoProvider::Get() {
return CpuInfoProvider::GetInstance<CpuInfoProvider>();
}
-void CpuInfoProvider::Observe(
- int type,
- const content::NotificationSource& source,
- const content::NotificationDetails& details) {
- switch (type) {
- case chrome::NOTIFICATION_APP_TERMINATING:
- StopSampling();
- break;
- default:
- NOTREACHED();
- break;
- }
-}
-
-void CpuInfoProvider::StartSamplingOnFileThread(
- const SamplingCallback& callback) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
- if (is_sampling_started_)
- return;
-
- if (!QueryCpuTimePerProcessor(&baseline_cpu_time_))
- return;
-
- is_sampling_started_ = true;
- callback_ = callback;
- DCHECK(!sampling_timer_) << "The sampling timer has leaked. Did you forgot "
- "to call StopSampling?";
- // Should be deleted on FILE thread.
- sampling_timer_ = new base::RepeatingTimer<CpuInfoProvider>();
- sampling_timer_->Start(FROM_HERE,
- base::TimeDelta::FromMilliseconds(sampling_interval_),
- this, &CpuInfoProvider::DoSample);
-}
-
-void CpuInfoProvider::StopSamplingOnFileThread() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
- if (!is_sampling_started_) return;
- delete sampling_timer_;
- sampling_timer_ = NULL;
- baseline_cpu_time_.clear();
- is_sampling_started_ = false;
-}
-
-void CpuInfoProvider::DoSample() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
-
- std::vector<CpuTime> next_cpu_time;
- if (!QueryCpuTimePerProcessor(&next_cpu_time))
- return;
-
- // Calculate the CPU usage information from the next_cpu_time and
- // |baseline_cpu_time_|.
- double total_usage = 0;
- scoped_ptr<CpuUpdateInfo> info(new CpuUpdateInfo());
- for (size_t i = 0; i < next_cpu_time.size(); ++i) {
- double total_time =
- (next_cpu_time[i].user - baseline_cpu_time_[i].user) +
- (next_cpu_time[i].kernel - baseline_cpu_time_[i].kernel) +
- (next_cpu_time[i].idle - baseline_cpu_time_[i].idle);
- double idle_time = next_cpu_time[i].idle - baseline_cpu_time_[i].idle;
-
- double usage = 0;
- if (total_time != 0)
- usage = (100 - idle_time * 100 / total_time);
- info->usage_per_processor.push_back(usage);
- total_usage += usage;
- }
-
- info->average_usage = total_usage / next_cpu_time.size();
- if (!callback_.is_null())
- callback_.Run(info.Pass());
- // Use next_cpu_time as baseline_cpu_time_ for the next sampling cycle.
- baseline_cpu_time_.swap(next_cpu_time);
-}
-
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698