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 #ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ | 4 #ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ |
5 #define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ | 5 #define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/timer.h" | 9 #include "base/timer.h" |
10 #include "chrome/browser/extensions/api/system_info/system_info_provider.h" | 10 #include "chrome/browser/extensions/api/system_info/system_info_provider.h" |
11 #include "chrome/common/extensions/api/system_info_cpu.h" | 11 #include "chrome/common/extensions/api/system_info_cpu.h" |
12 #include "content/public/browser/notification_observer.h" | |
13 #include "content/public/browser/notification_registrar.h" | |
14 | 12 |
15 namespace extensions { | 13 namespace extensions { |
16 | 14 |
17 class CpuInfoProvider | 15 class CpuInfoProvider |
18 : public content::NotificationObserver, | 16 : public SystemInfoProvider<api::system_info_cpu::CpuInfo> { |
19 public SystemInfoProvider<api::system_info_cpu::CpuInfo> { | |
20 public: | 17 public: |
21 typedef base::Callback< | |
22 void(scoped_ptr<api::system_info_cpu::CpuUpdateInfo>)> | |
23 SamplingCallback; | |
24 | |
25 // Overriden from SystemInfoProvider<CpuInfo>. | 18 // Overriden from SystemInfoProvider<CpuInfo>. |
26 virtual bool QueryInfo( | 19 virtual bool QueryInfo( |
27 api::system_info_cpu::CpuInfo* info) OVERRIDE; | 20 api::system_info_cpu::CpuInfo* info) OVERRIDE; |
28 | 21 |
29 // Start sampling the CPU usage. The callback gets called when one sampling | |
30 // cycle is completed periodically with the CPU updated usage info for each | |
31 // processors. It gets called on UI thread, the |callback| gets called on the | |
32 // FILE thread. | |
33 void StartSampling(const SamplingCallback& callback); | |
34 | |
35 // Stop the sampling cycle. Called on the FILE thread. | |
36 void StopSampling(); | |
37 | |
38 // Return the single shared instance of CpuInfoProvider. | 22 // Return the single shared instance of CpuInfoProvider. |
39 static CpuInfoProvider* Get(); | 23 static CpuInfoProvider* Get(); |
40 | 24 |
41 private: | 25 private: |
42 friend class SystemInfoProvider<api::system_info_cpu::CpuInfo>; | 26 friend class SystemInfoProvider<api::system_info_cpu::CpuInfo>; |
43 friend class MockCpuInfoProviderImpl; | 27 friend class MockCpuInfoProviderImpl; |
44 friend class TestCpuInfoProvider; | 28 friend class TestCpuInfoProvider; |
45 | 29 |
46 // The amount of time that CPU spent on performing different kinds of work. | |
47 // It is used to calculate the usage percent for processors. | |
48 struct CpuTime { | |
49 CpuTime() : user(0), kernel(0), idle(0) {} | |
50 int64 user; // user mode. | |
51 int64 kernel; // kernel mode. | |
52 int64 idle; // twiddling thumbs. | |
53 }; | |
54 | |
55 CpuInfoProvider(); | 30 CpuInfoProvider(); |
56 | 31 |
57 virtual ~CpuInfoProvider(); | 32 virtual ~CpuInfoProvider(); |
58 | |
59 // content::NotificationObserver implementation. | |
60 virtual void Observe(int type, | |
61 const content::NotificationSource& source, | |
62 const content::NotificationDetails& details) OVERRIDE; | |
63 | |
64 // Platform specific implementation for querying the CPU time information | |
65 // for each processor. | |
66 virtual bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times); | |
67 | |
68 // Start and stop sampling on the FILE thread. | |
69 void StartSamplingOnFileThread(const SamplingCallback& callback); | |
70 void StopSamplingOnFileThread(); | |
71 | |
72 // Called when the sampling timer is triggered. | |
73 void DoSample(); | |
74 | |
75 content::NotificationRegistrar registrar_; | |
76 | |
77 // Indicates whether the CPU sampling is started. | |
78 bool is_sampling_started_; | |
79 | |
80 // The sampling value returned from the most recent QueryCpuTimePerProcessor | |
81 // call. | |
82 std::vector<CpuTime> baseline_cpu_time_; | |
83 | |
84 // The callback which will be called when one sampling cycle is completed. | |
85 SamplingCallback callback_; | |
86 | |
87 // The timer used for polling CPU time periodically. Lives on FILE thread. | |
88 base::RepeatingTimer<CpuInfoProvider>* sampling_timer_; | |
89 | |
90 // The time interval for sampling, in milliseconds. | |
91 int sampling_interval_; | |
92 }; | 33 }; |
93 | 34 |
94 } // namespace extensions | 35 } // namespace extensions |
95 | 36 |
96 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ | 37 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ |
97 | 38 |
OLD | NEW |