1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_TASK_MANAGEMENT_TASK_MANAGER_INTERFACE_H_
6 #define CHROME_BROWSER_TASK_MANAGEMENT_TASK_MANAGER_INTERFACE_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/observer_list.h"
10 #include "base/process/process_handle.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "chrome/browser/task_management/providers/task.h"
14 #include "chrome/browser/task_management/task_manager_observer.h"
15 #include "third_party/WebKit/public/web/WebCache.h"
16 #include "ui/gfx/image/image_skia.h"
18 namespace task_management
{
20 // Defines the interface for any implementation of the task manager.
21 // Concrete implementations have no control over the refresh rate nor the
22 // enabled calculations of the usage of the various resources.
23 class TaskManagerInterface
{
25 void AddObserver(TaskManagerObserver
* observer
);
26 void RemoveObserver(TaskManagerObserver
* observer
);
28 // Activates the task with |task_id| by bringing its container to the front if
30 virtual void ActivateTask(TaskId task_id
) = 0;
32 // returns the CPU usage in percent for the process on which the task with
33 // |task_id| is running during the current refresh cycle.
34 virtual double GetCpuUsage(TaskId task_id
) const = 0;
36 // Returns the current physical/private/shared memory usage of the task with
37 // |task_id| in bytes. A value of -1 means no valid value is currently
39 virtual int64
GetPhysicalMemoryUsage(TaskId task_id
) const = 0;
40 virtual int64
GetPrivateMemoryUsage(TaskId task_id
) const = 0;
41 virtual int64
GetSharedMemoryUsage(TaskId task_id
) const = 0;
43 // Returns the GPU memory usage of the task with |task_id| in bytes. A value
44 // of -1 means no valid value is currently available.
45 // |has_duplicates| will be set to true if this process' GPU resource count is
46 // inflated because it is counting other processes' resources.
47 virtual int64
GetGpuMemoryUsage(TaskId task_id
,
48 bool* has_duplicates
) const = 0;
50 // Returns the number of average idle CPU wakeups per second since the last
51 // refresh cycle. A value of -1 means no valid value is currently available.
52 virtual int GetIdleWakeupsPerSecond(TaskId task_id
) const = 0;
54 // Returns the NaCl GDB debug stub port. A value of -1 means no valid value is
55 // currently available.
56 virtual int GetNaClDebugStubPort(TaskId task_id
) const = 0;
58 // On Windows, gets the current and peak number of GDI and USER handles in
59 // use. A value of -1 means no valid value is currently available.
60 virtual void GetGDIHandles(TaskId task_id
,
62 int64
* peak
) const = 0;
63 virtual void GetUSERHandles(TaskId task_id
,
65 int64
* peak
) const = 0;
67 // Returns the title of the task with |task_id|.
68 virtual const base::string16
& GetTitle(TaskId task_id
) const = 0;
70 // Returns the name of the profile associated with the browser context of the
71 // render view host that the task with |task_id| represents (if that task
72 // represents a renderer).
73 virtual base::string16
GetProfileName(TaskId task_id
) const = 0;
75 // Returns the favicon of the task with |task_id|.
76 virtual const gfx::ImageSkia
& GetIcon(TaskId task_id
) const = 0;
78 // Returns the ID and handle of the process on which the task with |task_id|
80 virtual const base::ProcessHandle
& GetProcessHandle(TaskId task_id
) const = 0;
81 virtual const base::ProcessId
& GetProcessId(TaskId task_id
) const = 0;
83 // Returns the type of the task with |task_id|.
84 virtual Task::Type
GetType(TaskId task_id
) const = 0;
86 // Returns the network usage (in bytes per second) during the current refresh
87 // cycle for the task with |task_id|. A value of -1 means no valid value is
88 // currently available or that task has never been notified of any network
90 virtual int64
GetNetworkUsage(TaskId task_id
) const = 0;
92 // Returns the Sqlite used memory (in bytes) for the task with |task_id|.
93 // A value of -1 means no valid value is currently available.
94 virtual int64
GetSqliteMemoryUsed(TaskId task_id
) const = 0;
96 // Returns the allocated and used V8 memory (in bytes) for the task with
97 // |task_id|. A return value of false means no valid value is currently
99 virtual bool GetV8Memory(TaskId task_id
,
101 int64
* used
) const = 0;
103 // Gets the Webkit resource cache stats for the task with |task_id|.
104 // A return value of false means that task does NOT report WebCache stats.
105 virtual bool GetWebCacheStats(
107 blink::WebCache::ResourceTypeStats
* stats
) const = 0;
109 // Returns true if the resource |type| usage calculation is enabled and
110 // the implementation should refresh its value (this means that at least one
111 // of the observers require this value). False otherwise.
112 bool IsResourceRefreshEnabled(RefreshType type
);
115 TaskManagerInterface();
116 virtual ~TaskManagerInterface();
118 // Notifying observers of various events.
119 void NotifyObserversOnTaskAdded(TaskId id
);
120 void NotifyObserversOnTaskToBeRemoved(TaskId id
);
121 void NotifyObserversOnRefresh(const TaskIdList
& task_ids
);
123 // Refresh all the enabled resources usage of all the available tasks.
124 virtual void Refresh() = 0;
125 // TODO(afakhry): Add more virtual methods here as needed.
127 // Returns the current refresh time that this task manager is running at. It
128 // will return base::TimeDelta::Max() if the task manager is not running.
129 base::TimeDelta
GetCurrentRefreshTime() const;
131 int64
enabled_resources_flags() const { return enabled_resources_flags_
; }
133 void set_timer_for_testing(scoped_ptr
<base::Timer
> timer
) {
134 refresh_timer_
= timer
.Pass();
138 // Appends |flags| to the |enabled_resources_flags_|.
139 void ResourceFlagsAdded(int64 flags
);
141 // Sets |enabled_resources_flags_| to |flags|.
142 void SetEnabledResourceFlags(int64 flags
);
144 // Schedules the task manager refresh cycles using the given |refresh_time|.
145 // It stops any existing refresh schedule.
146 void ScheduleRefresh(base::TimeDelta refresh_time
);
148 // The list of observers.
149 base::ObserverList
<TaskManagerObserver
> observers_
;
151 // The timer that will be used to schedule the successive refreshes.
152 scoped_ptr
<base::Timer
> refresh_timer_
;
154 // The flags containing the enabled resources types calculations.
155 int64 enabled_resources_flags_
;
157 DISALLOW_COPY_AND_ASSIGN(TaskManagerInterface
);
160 } // namespace task_management
162 #endif // CHROME_BROWSER_TASK_MANAGEMENT_TASK_MANAGER_INTERFACE_H_