Chromium Code Reviews| Index: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h |
| diff --git a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h |
| index a3c8b29b6ad74d4497a09776df291b7781fb84d7..763be099be16e1459dfc7d0c2db469e140ee91a4 100644 |
| --- a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h |
| +++ b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h |
| @@ -8,16 +8,22 @@ |
| #include <vector> |
| +#include "base/timer.h" |
| #include "chrome/common/extensions/api/experimental_system_info_cpu.h" |
| +#include "content/public/browser/notification_observer.h" |
| +#include "content/public/browser/notification_registrar.h" |
| namespace extensions { |
| class CpuInfoProvider |
| - : public SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo> { |
| + : public content::NotificationObserver, |
| + public SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo> { |
| public: |
| - typedef base::Callback<api::experimental_system_info_cpu::CpuUpdateInfo> |
| - QueryCpuTimeCallback; |
| - virtual ~CpuInfoProvider() {} |
| + typedef base::Callback< |
| + void(scoped_ptr<api::experimental_system_info_cpu::CpuUpdateInfo>)> |
| + SamplingCallback; |
| + |
| + virtual ~CpuInfoProvider(); |
| // Overriden from SystemInfoProvider<CpuInfo>. |
| virtual bool QueryInfo( |
| @@ -25,28 +31,65 @@ class CpuInfoProvider |
| // Start sampling the CPU usage. The callback gets called when one sampling |
| // cycle is completed periodically with the CPU updated usage info for each |
| - // processors. Return true if it succeeds to start, otherwise, false is |
| - // returned. |
| - bool StartSampling(const QueryCpuTimeCallback& callback); |
| + // processors. It gets called on UI thread, the |callback| gets called on the |
| + // FILE thread. |
| + void StartSampling(const SamplingCallback& callback); |
| - // Stop the sampling cycle. Return true if it succeeds to stop. |
| - bool StopSampling(); |
| + // Stop the sampling cycle. Called on the FILE thread. |
| + void StopSampling(); |
| // Return the single shared instance of CpuInfoProvider. |
| static CpuInfoProvider* Get(); |
| private: |
| + friend class SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo>; |
| + friend class MockCpuInfoProviderImpl; |
| + |
| // The amount of time that CPU spent on performing different kinds of work. |
| // It is used to calculate the usage percent for processors. |
| - struct CpuTime { |
| + class CpuTime { |
|
benwells
2012/10/04 06:58:59
Nit: leave this as a struct.
Hongbo Min
2012/10/05 02:40:56
Done.
|
| + public: |
| + CpuTime() : user(0), kernel(0), idle(0) {} |
| int64 user; // user mode. |
| int64 kernel; // kernel mode. |
| int64 idle; // twiddling thumbs. |
| }; |
| + CpuInfoProvider(); |
| + |
| + // content::NotificationObserver implementation. |
| + virtual void Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details); |
| + |
| // Platform specific implementation for querying the CPU time information |
| // for each processor. |
| - bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times); |
| + virtual bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times); |
| + |
| + // Start and stop sampling on the FILE thread. |
| + void StartSamplingOnFileThread(const SamplingCallback& callback); |
| + void StopSamplingOnFileThread(); |
| + |
| + // Called when the sampling timer is triggered. |
| + void DoSample(); |
| + |
| + content::NotificationRegistrar registrar_; |
| + |
| + // Indicates whether the CPU sampling is started. |
| + bool is_sampling_started_; |
| + |
| + // The sampling value returned from the most recent QueryCpuTimePerProcessor |
| + // call. |
| + std::vector<CpuTime> baseline_cpu_time_; |
| + |
| + // The callback which will be called when one sampling cycle is completed. |
| + SamplingCallback callback_; |
| + |
| + // The timer used for polling CPU time periodically. Lives on FILE thread. |
| + base::RepeatingTimer<CpuInfoProvider>* sampling_timer_; |
| + |
| + // The time interval for sampling, in milliseconds. |
| + int sampling_interval_; |
| }; |
| } // namespace extensions |