Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / task_management / task_manager_interface.h
bloba58b7e72ff988e23f9c4465410ce9dd72f7fb753
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 <stdint.h>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/observer_list.h"
12 #include "base/process/process_handle.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
15 #include "chrome/browser/task_management/providers/task.h"
16 #include "chrome/browser/task_management/task_manager_observer.h"
17 #include "third_party/WebKit/public/web/WebCache.h"
18 #include "ui/gfx/image/image_skia.h"
20 namespace net {
21 class URLRequest;
22 } // namespace net
24 namespace task_management {
26 // Defines the interface for any implementation of the task manager.
27 // Concrete implementations have no control over the refresh rate nor the
28 // enabled calculations of the usage of the various resources.
29 class TaskManagerInterface {
30 public:
31 // Gets the existing instance of the task manager if any, otherwise it will
32 // create it first. Must be called on the UI thread.
33 static TaskManagerInterface* GetTaskManager();
35 // This notification will be received on the IO thread from
36 // ChromeNetworkDelegate to update the task manager with network usage.
37 static void OnRawBytesRead(const net::URLRequest& request,
38 int64_t bytes_read);
40 void AddObserver(TaskManagerObserver* observer);
41 void RemoveObserver(TaskManagerObserver* observer);
43 // Activates the task with |task_id| by bringing its container to the front if
44 // possible.
45 virtual void ActivateTask(TaskId task_id) = 0;
47 // returns the CPU usage in percent for the process on which the task with
48 // |task_id| is running during the current refresh cycle.
49 virtual double GetCpuUsage(TaskId task_id) const = 0;
51 // Returns the current physical/private/shared memory usage of the task with
52 // |task_id| in bytes. A value of -1 means no valid value is currently
53 // available.
54 virtual int64 GetPhysicalMemoryUsage(TaskId task_id) const = 0;
55 virtual int64 GetPrivateMemoryUsage(TaskId task_id) const = 0;
56 virtual int64 GetSharedMemoryUsage(TaskId task_id) const = 0;
58 // Returns the GPU memory usage of the task with |task_id| in bytes. A value
59 // of -1 means no valid value is currently available.
60 // |has_duplicates| will be set to true if this process' GPU resource count is
61 // inflated because it is counting other processes' resources.
62 virtual int64 GetGpuMemoryUsage(TaskId task_id,
63 bool* has_duplicates) const = 0;
65 // Returns the number of average idle CPU wakeups per second since the last
66 // refresh cycle. A value of -1 means no valid value is currently available.
67 virtual int GetIdleWakeupsPerSecond(TaskId task_id) const = 0;
69 // Returns the NaCl GDB debug stub port. A value of -1 means no valid value is
70 // currently available.
71 virtual int GetNaClDebugStubPort(TaskId task_id) const = 0;
73 // On Windows, gets the current and peak number of GDI and USER handles in
74 // use. A value of -1 means no valid value is currently available.
75 virtual void GetGDIHandles(TaskId task_id,
76 int64* current,
77 int64* peak) const = 0;
78 virtual void GetUSERHandles(TaskId task_id,
79 int64* current,
80 int64* peak) const = 0;
82 // Returns the title of the task with |task_id|.
83 virtual const base::string16& GetTitle(TaskId task_id) const = 0;
85 // Returns the name of the profile associated with the browser context of the
86 // render view host that the task with |task_id| represents (if that task
87 // represents a renderer).
88 virtual base::string16 GetProfileName(TaskId task_id) const = 0;
90 // Returns the favicon of the task with |task_id|.
91 virtual const gfx::ImageSkia& GetIcon(TaskId task_id) const = 0;
93 // Returns the ID and handle of the process on which the task with |task_id|
94 // is running.
95 virtual const base::ProcessHandle& GetProcessHandle(TaskId task_id) const = 0;
96 virtual const base::ProcessId& GetProcessId(TaskId task_id) const = 0;
98 // Returns the type of the task with |task_id|.
99 virtual Task::Type GetType(TaskId task_id) const = 0;
101 // Returns the network usage (in bytes per second) during the current refresh
102 // cycle for the task with |task_id|. A value of -1 means no valid value is
103 // currently available or that task has never been notified of any network
104 // usage.
105 virtual int64 GetNetworkUsage(TaskId task_id) const = 0;
107 // Returns the Sqlite used memory (in bytes) for the task with |task_id|.
108 // A value of -1 means no valid value is currently available.
109 virtual int64 GetSqliteMemoryUsed(TaskId task_id) const = 0;
111 // Returns the allocated and used V8 memory (in bytes) for the task with
112 // |task_id|. A return value of false means no valid value is currently
113 // available.
114 virtual bool GetV8Memory(TaskId task_id,
115 int64* allocated,
116 int64* used) const = 0;
118 // Gets the Webkit resource cache stats for the task with |task_id|.
119 // A return value of false means that task does NOT report WebCache stats.
120 virtual bool GetWebCacheStats(
121 TaskId task_id,
122 blink::WebCache::ResourceTypeStats* stats) const = 0;
124 // Gets the list of task IDs currently tracked by the task manager. The list
125 // will be sorted such that the task representing the browser process is at
126 // the top of the list and the rest of the IDs will be sorted by the process
127 // IDs on which the tasks are running, then by the task IDs themselves.
128 virtual const TaskIdList& GetTaskIdsList() const = 0;
130 // Gets the number of task-manager tasks running on the same process on which
131 // the Task with |task_id| is running.
132 virtual size_t GetNumberOfTasksOnSameProcess(TaskId task_id) const = 0;
134 // Returns true if the resource |type| usage calculation is enabled and
135 // the implementation should refresh its value (this means that at least one
136 // of the observers require this value). False otherwise.
137 bool IsResourceRefreshEnabled(RefreshType type);
139 protected:
140 TaskManagerInterface();
141 virtual ~TaskManagerInterface();
143 // Notifying observers of various events.
144 void NotifyObserversOnTaskAdded(TaskId id);
145 void NotifyObserversOnTaskToBeRemoved(TaskId id);
146 void NotifyObserversOnRefresh(const TaskIdList& task_ids);
148 // Refresh all the enabled resources usage of all the available tasks.
149 virtual void Refresh() = 0;
151 // StartUpdating will be called once an observer is added, and StopUpdating
152 // will be called when the last observer is removed.
153 virtual void StartUpdating() = 0;
154 virtual void StopUpdating() = 0;
156 // Returns the current refresh time that this task manager is running at. It
157 // will return base::TimeDelta::Max() if the task manager is not running.
158 base::TimeDelta GetCurrentRefreshTime() const;
160 int64 enabled_resources_flags() const { return enabled_resources_flags_; }
162 void set_timer_for_testing(scoped_ptr<base::Timer> timer) {
163 refresh_timer_ = timer.Pass();
166 private:
167 friend class TaskManagerObserver;
169 // This should be called after each time an observer changes its own desired
170 // resources refresh flags.
171 void RecalculateRefreshFlags();
173 // Appends |flags| to the |enabled_resources_flags_|.
174 void ResourceFlagsAdded(int64 flags);
176 // Sets |enabled_resources_flags_| to |flags|.
177 void SetEnabledResourceFlags(int64 flags);
179 // Schedules the task manager refresh cycles using the given |refresh_time|.
180 // It stops any existing refresh schedule.
181 void ScheduleRefresh(base::TimeDelta refresh_time);
183 // The list of observers.
184 base::ObserverList<TaskManagerObserver> observers_;
186 // The timer that will be used to schedule the successive refreshes.
187 scoped_ptr<base::Timer> refresh_timer_;
189 // The flags containing the enabled resources types calculations.
190 int64 enabled_resources_flags_;
192 DISALLOW_COPY_AND_ASSIGN(TaskManagerInterface);
195 } // namespace task_management
197 #endif // CHROME_BROWSER_TASK_MANAGEMENT_TASK_MANAGER_INTERFACE_H_