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

Side by Side Diff: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h

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

Powered by Google App Engine
This is Rietveld 408576698