blob: c0eaa8b95656da90c50e2038fc60810f7b49cf08 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
[email protected]bfac5ac2008-10-09 17:55:385#ifndef CHROME_BROWSER_TASK_MANAGER_H_
6#define CHROME_BROWSER_TASK_MANAGER_H_
initial.commit09911bf2008-07-26 23:55:297
[email protected]b7937d5b2008-09-10 22:12:198#include <map>
[email protected]bfac5ac2008-10-09 17:55:389#include <string>
[email protected]0860b9b92009-05-11 18:49:1810#include <utility>
[email protected]b7937d5b2008-09-10 22:12:1911#include <vector>
12
[email protected]5d438dbad2009-04-30 08:59:3913#include "base/basictypes.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/lock.h"
[email protected]5d438dbad2009-04-30 08:59:3915#include "base/process_util.h"
initial.commit09911bf2008-07-26 23:55:2916#include "base/ref_counted.h"
[email protected]5d438dbad2009-04-30 08:59:3917#include "base/singleton.h"
[email protected]2d316662008-09-03 18:18:1418#include "base/timer.h"
[email protected]2c434b32009-03-19 06:27:4719#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]f3ec7742009-01-15 00:59:1620#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:2921#include "net/url_request/url_request_job_tracker.h"
[email protected]1222cce2009-04-27 06:58:2722#include "testing/gtest/include/gtest/gtest_prod.h"
initial.commit09911bf2008-07-26 23:55:2923
24class MessageLoop;
initial.commit09911bf2008-07-26 23:55:2925class SkBitmap;
initial.commit09911bf2008-07-26 23:55:2926class TaskManager;
[email protected]0da192212009-05-14 14:01:5827class TaskManagerView;
[email protected]0860b9b92009-05-11 18:49:1828class TaskManagerModel;
initial.commit09911bf2008-07-26 23:55:2929
30struct BytesReadParam;
31
[email protected]176aa482008-11-14 03:25:1532namespace base {
initial.commit09911bf2008-07-26 23:55:2933class ProcessMetrics;
34}
35
36// This class is a singleton.
[email protected]f9fdb072009-05-06 07:27:4037class TaskManager {
initial.commit09911bf2008-07-26 23:55:2938 public:
39 // A resource represents one row in the task manager.
40 // Resources from similar processes are grouped together by the task manager.
41 class Resource {
42 public:
[email protected]b7937d5b2008-09-10 22:12:1943 virtual ~Resource() {}
44
initial.commit09911bf2008-07-26 23:55:2945 virtual std::wstring GetTitle() const = 0;
46 virtual SkBitmap GetIcon() const = 0;
[email protected]5d438dbad2009-04-30 08:59:3947 virtual base::ProcessHandle GetProcess() const = 0;
initial.commit09911bf2008-07-26 23:55:2948
[email protected]b7937d5b2008-09-10 22:12:1949 // A helper function for ActivateFocusedTab. Returns NULL by default
50 // because not all resources have an assoiciated tab.
51 virtual TabContents* GetTabContents() const {return NULL;}
52
initial.commit09911bf2008-07-26 23:55:2953 // Whether this resource does report the network usage accurately.
54 // This controls whether 0 or N/A is displayed when no bytes have been
55 // reported as being read. This is because some plugins do not report the
56 // bytes read and we don't want to display a misleading 0 value in that
57 // case.
58 virtual bool SupportNetworkUsage() const = 0;
59
60 // Called when some bytes have been read and support_network_usage returns
[email protected]bfac5ac2008-10-09 17:55:3861 // false (meaning we do have network usage support).
initial.commit09911bf2008-07-26 23:55:2962 virtual void SetSupportNetworkUsage() = 0;
63 };
64
65 // ResourceProviders are responsible for adding/removing resources to the task
66 // manager. The task manager notifies the ResourceProvider that it is ready
67 // to receive resource creation/termination notifications with a call to
68 // StartUpdating(). At that point, the resource provider should call
69 // AddResource with all the existing resources, and after that it should call
70 // AddResource/RemoveResource as resources are created/terminated.
71 // The provider remains the owner of the resource objects and is responsible
72 // for deleting them (when StopUpdating() is called).
73 // After StopUpdating() is called the provider should also stop reporting
74 // notifications to the task manager.
75 // Note: ResourceProviders have to be ref counted as they are used in
76 // MessageLoop::InvokeLater().
77 class ResourceProvider : public base::RefCounted<ResourceProvider> {
78 public:
[email protected]a3725772009-04-27 12:55:4079 virtual ~ResourceProvider() {}
80
initial.commit09911bf2008-07-26 23:55:2981 // Should return the resource associated to the specified ids, or NULL if
82 // the resource does not belong to this provider.
83 virtual TaskManager::Resource* GetResource(int process_id,
84 int render_process_host_id,
85 int routing_id) = 0;
86 virtual void StartUpdating() = 0;
87 virtual void StopUpdating() = 0;
88 };
89
90 static void RegisterPrefs(PrefService* prefs);
91
initial.commit09911bf2008-07-26 23:55:2992 // Call this method to show the Task Manager.
93 // Only one instance of Task Manager is created, so if the Task Manager has
94 // already be opened, it is reopened. If it is currently opened, then it is
95 // moved to the front.
96 static void Open();
97
[email protected]9c5e4172009-05-27 08:46:2898 // Close the task manager if it's currently opened.
99 static void Close();
initial.commit09911bf2008-07-26 23:55:29100
101 // Returns true if the current selection includes the browser process.
102 bool BrowserProcessIsSelected();
103
104 // Terminates the selected tab(s) in the list.
105 void KillSelectedProcesses();
106
[email protected]b7937d5b2008-09-10 22:12:19107 // Activates the browser tab associated with the focused row in the task
108 // manager table. This happens when the user double clicks or hits return.
109 void ActivateFocusedTab();
110
initial.commit09911bf2008-07-26 23:55:29111 void AddResourceProvider(ResourceProvider* provider);
112 void RemoveResourceProvider(ResourceProvider* provider);
113
114 // These methods are invoked by the resource providers to add/remove resources
115 // to the Task Manager. Note that the resources are owned by the
116 // ResourceProviders and are not valid after StopUpdating() has been called
117 // on the ResourceProviders.
118 void AddResource(Resource* resource);
119 void RemoveResource(Resource* resource);
120
[email protected]9c5e4172009-05-27 08:46:28121 void OnWindowClosed();
122
initial.commit09911bf2008-07-26 23:55:29123 private:
[email protected]1222cce2009-04-27 06:58:27124 FRIEND_TEST(TaskManagerTest, Basic);
125 FRIEND_TEST(TaskManagerTest, Resources);
126
initial.commit09911bf2008-07-26 23:55:29127 // Obtain an instance via GetInstance().
128 TaskManager();
[email protected]5d438dbad2009-04-30 08:59:39129 friend struct DefaultSingletonTraits<TaskManager>;
initial.commit09911bf2008-07-26 23:55:29130
131 ~TaskManager();
132
133 void Init();
134
135 // Returns the singleton instance (and initializes it if necessary).
136 static TaskManager* GetInstance();
137
[email protected]0860b9b92009-05-11 18:49:18138 // The model used for gathering and processing task data. It is ref counted
139 // because it is passed as a parameter to MessageLoop::InvokeLater().
140 scoped_refptr<TaskManagerModel> model_;
initial.commit09911bf2008-07-26 23:55:29141
142 // A container containing the buttons and table.
[email protected]0da192212009-05-14 14:01:58143 scoped_ptr<TaskManagerView> view_;
initial.commit09911bf2008-07-26 23:55:29144
[email protected]bfac5ac2008-10-09 17:55:38145 DISALLOW_COPY_AND_ASSIGN(TaskManager);
initial.commit09911bf2008-07-26 23:55:29146};
147
[email protected]0860b9b92009-05-11 18:49:18148class TaskManagerModelObserver {
initial.commit09911bf2008-07-26 23:55:29149 public:
[email protected]0860b9b92009-05-11 18:49:18150 virtual ~TaskManagerModelObserver() {}
initial.commit09911bf2008-07-26 23:55:29151
[email protected]0860b9b92009-05-11 18:49:18152 // Invoked when the model has been completely changed.
153 virtual void OnModelChanged() = 0;
initial.commit09911bf2008-07-26 23:55:29154
[email protected]0860b9b92009-05-11 18:49:18155 // Invoked when a range of items has changed.
156 virtual void OnItemsChanged(int start, int length) = 0;
157
158 // Invoked when new items are added.
159 virtual void OnItemsAdded(int start, int length) = 0;
160
161 // Invoked when a range of items has been removed.
162 virtual void OnItemsRemoved(int start, int length) = 0;
163};
164
165// The model that the TaskManager is using.
166class TaskManagerModel : public URLRequestJobTracker::JobObserver,
167 public base::RefCounted<TaskManagerModel> {
168 public:
169 explicit TaskManagerModel(TaskManager* task_manager);
170 ~TaskManagerModel();
171
172 // Set object to be notified on model changes.
173 void SetObserver(TaskManagerModelObserver* observer);
174
175 // Returns number of registered resources.
176 int ResourceCount() const;
177
178 // Methods to return formatted resource information.
179 std::wstring GetResourceTitle(int index) const;
180 std::wstring GetResourceNetworkUsage(int index) const;
181 std::wstring GetResourceCPUUsage(int index) const;
182 std::wstring GetResourcePrivateMemory(int index) const;
183 std::wstring GetResourceSharedMemory(int index) const;
184 std::wstring GetResourcePhysicalMemory(int index) const;
185 std::wstring GetResourceProcessId(int index) const;
186 std::wstring GetResourceStatsValue(int index, int col_id) const;
187 std::wstring GetResourceGoatsTeleported(int index) const;
188
189 // Returns true if the resource is first in its group (resources
190 // rendered by the same process are groupped together).
191 bool IsResourceFirstInGroup(int index) const;
192
193 // Returns icon to be used for resource (for example a favicon).
194 SkBitmap GetResourceIcon(int index) const;
195
196 // Returns a pair (start, length) of the group range of resource.
197 std::pair<int, int> GetGroupRangeForResource(int index) const;
198
199 // Compares values in column |col_id| and rows |row1|, |row2|.
200 // Returns -1 if value in |row1| is less than value in |row2|,
201 // 0 if they are equal, and 1 otherwise.
202 int CompareValues(int row1, int row2, int col_id) const;
203
204 // Returns process handle for given resource.
205 base::ProcessHandle GetResourceProcessHandle(int index) const;
206
207 // Returns TabContents of given resource or NULL if not applicable.
208 TabContents* GetResourceTabContents(int index) const;
initial.commit09911bf2008-07-26 23:55:29209
210 // JobObserver methods:
211 void OnJobAdded(URLRequestJob* job);
212 void OnJobRemoved(URLRequestJob* job);
213 void OnJobDone(URLRequestJob* job, const URLRequestStatus& status);
214 void OnJobRedirect(URLRequestJob* job, const GURL& location, int status_code);
215 void OnBytesRead(URLRequestJob* job, int byte_count);
216
initial.commit09911bf2008-07-26 23:55:29217 void AddResourceProvider(TaskManager::ResourceProvider* provider);
218 void RemoveResourceProvider(TaskManager::ResourceProvider* provider);
219
220 void AddResource(TaskManager::Resource* resource);
221 void RemoveResource(TaskManager::Resource* resource);
222
[email protected]0860b9b92009-05-11 18:49:18223 void StartUpdating();
224 void StopUpdating();
initial.commit09911bf2008-07-26 23:55:29225
[email protected]0860b9b92009-05-11 18:49:18226 void Clear(); // Removes all items.
227
228 private:
[email protected]bfac5ac2008-10-09 17:55:38229 enum UpdateState {
230 IDLE = 0, // Currently not updating.
231 TASK_PENDING, // An update task is pending.
232 STOPPING // A update task is pending and it should stop the update.
233 };
234
initial.commit09911bf2008-07-26 23:55:29235 // This struct is used to exchange information between the io and ui threads.
236 struct BytesReadParam {
237 BytesReadParam(int origin_pid, int render_process_host_id,
238 int routing_id, int byte_count)
[email protected]bfac5ac2008-10-09 17:55:38239 : origin_pid(origin_pid),
240 render_process_host_id(render_process_host_id),
241 routing_id(routing_id),
242 byte_count(byte_count) { }
initial.commit09911bf2008-07-26 23:55:29243
244 int origin_pid;
245 int render_process_host_id;
246 int routing_id;
247 int byte_count;
248 };
249
initial.commit09911bf2008-07-26 23:55:29250 typedef std::vector<TaskManager::Resource*> ResourceList;
251 typedef std::vector<TaskManager::ResourceProvider*> ResourceProviderList;
[email protected]0860b9b92009-05-11 18:49:18252 typedef std::map<base::ProcessHandle, ResourceList*> GroupMap;
253 typedef std::map<base::ProcessHandle, base::ProcessMetrics*> MetricsMap;
254 typedef std::map<base::ProcessHandle, int> CPUUsageMap;
255 typedef std::map<TaskManager::Resource*, int64> ResourceValueMap;
initial.commit09911bf2008-07-26 23:55:29256
[email protected]bfac5ac2008-10-09 17:55:38257 // Updates the values for all rows.
258 void Refresh();
259
initial.commit09911bf2008-07-26 23:55:29260 void AddItem(TaskManager::Resource* resource, bool notify_table);
261 void RemoveItem(TaskManager::Resource* resource);
262
263 // Register for network usage updates
264 void RegisterForJobDoneNotifications();
265 void UnregisterForJobDoneNotifications();
266
267 // Returns the network usage (in bytes per seconds) for the specified
268 // resource. That's the value retrieved at the last timer's tick.
[email protected]d043c2cc2009-03-25 18:30:45269 int64 GetNetworkUsageForResource(TaskManager::Resource* resource) const;
initial.commit09911bf2008-07-26 23:55:29270
initial.commit09911bf2008-07-26 23:55:29271 // Called on the UI thread when some bytes are read.
272 void BytesRead(BytesReadParam param);
273
[email protected]bfac5ac2008-10-09 17:55:38274 // Returns the network usage (in byte per second) that should be displayed for
275 // the passed |resource|. -1 means the information is not available for that
276 // resource.
[email protected]d043c2cc2009-03-25 18:30:45277 int64 GetNetworkUsage(TaskManager::Resource* resource) const;
[email protected]bfac5ac2008-10-09 17:55:38278
279 // Returns the CPU usage (in %) that should be displayed for the passed
280 // |resource|.
[email protected]d043c2cc2009-03-25 18:30:45281 int GetCPUUsage(TaskManager::Resource* resource) const;
[email protected]bfac5ac2008-10-09 17:55:38282
283 // Retrieves the private memory (in KB) that should be displayed from the
284 // passed |process_metrics|.
[email protected]d043c2cc2009-03-25 18:30:45285 size_t GetPrivateMemory(const base::ProcessMetrics* process_metrics) const;
[email protected]bfac5ac2008-10-09 17:55:38286
287 // Returns the shared memory (in KB) that should be displayed from the passed
288 // |process_metrics|.
[email protected]d043c2cc2009-03-25 18:30:45289 size_t GetSharedMemory(const base::ProcessMetrics* process_metrics) const;
[email protected]bfac5ac2008-10-09 17:55:38290
291 // Returns the pysical memory (in KB) that should be displayed from the passed
292 // |process_metrics|.
[email protected]d043c2cc2009-03-25 18:30:45293 size_t GetPhysicalMemory(const base::ProcessMetrics* process_metrics) const;
[email protected]bfac5ac2008-10-09 17:55:38294
295 // Returns the stat value at the column |col_id| that should be displayed from
296 // the passed |process_metrics|.
[email protected]d043c2cc2009-03-25 18:30:45297 int GetStatsValue(const TaskManager::Resource* resource, int col_id) const;
[email protected]2a5af8a2009-01-23 23:13:05298
[email protected]bfac5ac2008-10-09 17:55:38299 // Retrieves the ProcessMetrics for the resources at the specified rows.
300 // Returns true if there was a ProcessMetrics available for both rows.
301 bool GetProcessMetricsForRows(int row1,
302 int row2,
[email protected]176aa482008-11-14 03:25:15303 base::ProcessMetrics** proc_metrics1,
[email protected]0860b9b92009-05-11 18:49:18304 base::ProcessMetrics** proc_metrics2) const;
[email protected]bfac5ac2008-10-09 17:55:38305
[email protected]2a5af8a2009-01-23 23:13:05306 // Given a string containing a number, this function returns the formatted
307 // string that should be displayed in the task manager's memory cell.
308 std::wstring GetMemCellText(std::wstring* number) const;
309
initial.commit09911bf2008-07-26 23:55:29310 // The list of providers to the task manager. They are ref counted.
311 ResourceProviderList providers_;
312
313 // The list of all the resources displayed in the task manager. They are owned
314 // by the ResourceProviders.
315 ResourceList resources_;
316
317 // A map to keep tracks of the grouped resources (they are grouped if they
318 // share the same process). The groups (the Resources vectors) are owned by
319 // the model (but the actual Resources are owned by the ResourceProviders).
320 GroupMap group_map_;
321
322 // A map to retrieve the process metrics for a process. The ProcessMetrics are
323 // owned by the model.
324 MetricsMap metrics_map_;
325
326 // A map that keeps track of the number of bytes read per process since last
327 // tick. The Resources are owned by the ResourceProviders.
328 ResourceValueMap current_byte_count_map_;
329
330 // A map that contains the network usage is displayed in the table, in bytes
331 // per second. It is computed every time the timer ticks. The Resources are
332 // owned by the ResourceProviders.
333 ResourceValueMap displayed_network_usage_map_;
334
[email protected]bfac5ac2008-10-09 17:55:38335 // A map that contains the CPU usage (in %) for a process since last refresh.
336 CPUUsageMap cpu_usage_map_;
initial.commit09911bf2008-07-26 23:55:29337
[email protected]0860b9b92009-05-11 18:49:18338 TaskManagerModelObserver* observer_;
initial.commit09911bf2008-07-26 23:55:29339
initial.commit09911bf2008-07-26 23:55:29340 MessageLoop* ui_loop_;
341
[email protected]bfac5ac2008-10-09 17:55:38342 // Whether we are currently in the process of updating.
343 UpdateState update_state_;
344
initial.commit09911bf2008-07-26 23:55:29345 // See design doc at http://go/at-teleporter for more information.
346 static int goats_teleported_;
347
[email protected]0860b9b92009-05-11 18:49:18348 DISALLOW_COPY_AND_ASSIGN(TaskManagerModel);
initial.commit09911bf2008-07-26 23:55:29349};
initial.commit09911bf2008-07-26 23:55:29350
[email protected]0da192212009-05-14 14:01:58351class TaskManagerView {
352 public:
353 virtual ~TaskManagerView() {}
354
355 virtual void GetSelection(std::vector<int>* selection) = 0;
356 virtual void GetFocused(std::vector<int>* focused) = 0;
357
358 virtual void OpenWindow() = 0;
[email protected]9c5e4172009-05-27 08:46:28359 virtual void CloseWindow() = 0;
[email protected]0da192212009-05-14 14:01:58360};
361
[email protected]bfac5ac2008-10-09 17:55:38362#endif // CHROME_BROWSER_TASK_MANAGER_H_