license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 5 | #ifndef CHROME_BROWSER_TASK_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_TASK_MANAGER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
[email protected] | b7937d5b | 2008-09-10 22:12:19 | [diff] [blame] | 8 | #include <map> |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 9 | #include <string> |
[email protected] | b7937d5b | 2008-09-10 22:12:19 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/lock.h" |
| 13 | #include "base/singleton.h" |
| 14 | #include "base/ref_counted.h" |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 15 | #include "base/timer.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | #include "chrome/views/dialog_delegate.h" |
| 17 | #include "chrome/views/group_table_view.h" |
| 18 | #include "chrome/browser/cache_manager_host.h" |
[email protected] | b7937d5b | 2008-09-10 22:12:19 | [diff] [blame] | 19 | #include "chrome/browser/tab_contents.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include "net/url_request/url_request_job_tracker.h" |
| 21 | |
| 22 | class MessageLoop; |
| 23 | class ModelEntry; |
| 24 | class PrefService; |
| 25 | class SkBitmap; |
| 26 | class Task; |
| 27 | class TaskManager; |
| 28 | class TaskManagerContents; |
| 29 | class TaskManagerTableModel; |
| 30 | class TaskManagerWindow; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | |
| 32 | struct BytesReadParam; |
| 33 | |
| 34 | namespace ChromeViews { |
| 35 | class View; |
| 36 | class Window; |
| 37 | } |
| 38 | |
| 39 | namespace process_util { |
| 40 | class ProcessMetrics; |
| 41 | } |
| 42 | |
| 43 | // This class is a singleton. |
| 44 | class TaskManager : public ChromeViews::DialogDelegate { |
| 45 | public: |
| 46 | // A resource represents one row in the task manager. |
| 47 | // Resources from similar processes are grouped together by the task manager. |
| 48 | class Resource { |
| 49 | public: |
[email protected] | b7937d5b | 2008-09-10 22:12:19 | [diff] [blame] | 50 | virtual ~Resource() {} |
| 51 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | virtual std::wstring GetTitle() const = 0; |
| 53 | virtual SkBitmap GetIcon() const = 0; |
| 54 | virtual HANDLE GetProcess() const = 0; |
| 55 | |
[email protected] | b7937d5b | 2008-09-10 22:12:19 | [diff] [blame] | 56 | // A helper function for ActivateFocusedTab. Returns NULL by default |
| 57 | // because not all resources have an assoiciated tab. |
| 58 | virtual TabContents* GetTabContents() const {return NULL;} |
| 59 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | // Whether this resource does report the network usage accurately. |
| 61 | // This controls whether 0 or N/A is displayed when no bytes have been |
| 62 | // reported as being read. This is because some plugins do not report the |
| 63 | // bytes read and we don't want to display a misleading 0 value in that |
| 64 | // case. |
| 65 | virtual bool SupportNetworkUsage() const = 0; |
| 66 | |
| 67 | // Called when some bytes have been read and support_network_usage returns |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 68 | // false (meaning we do have network usage support). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | virtual void SetSupportNetworkUsage() = 0; |
| 70 | }; |
| 71 | |
| 72 | // ResourceProviders are responsible for adding/removing resources to the task |
| 73 | // manager. The task manager notifies the ResourceProvider that it is ready |
| 74 | // to receive resource creation/termination notifications with a call to |
| 75 | // StartUpdating(). At that point, the resource provider should call |
| 76 | // AddResource with all the existing resources, and after that it should call |
| 77 | // AddResource/RemoveResource as resources are created/terminated. |
| 78 | // The provider remains the owner of the resource objects and is responsible |
| 79 | // for deleting them (when StopUpdating() is called). |
| 80 | // After StopUpdating() is called the provider should also stop reporting |
| 81 | // notifications to the task manager. |
| 82 | // Note: ResourceProviders have to be ref counted as they are used in |
| 83 | // MessageLoop::InvokeLater(). |
| 84 | class ResourceProvider : public base::RefCounted<ResourceProvider> { |
| 85 | public: |
| 86 | // Should return the resource associated to the specified ids, or NULL if |
| 87 | // the resource does not belong to this provider. |
| 88 | virtual TaskManager::Resource* GetResource(int process_id, |
| 89 | int render_process_host_id, |
| 90 | int routing_id) = 0; |
| 91 | virtual void StartUpdating() = 0; |
| 92 | virtual void StopUpdating() = 0; |
| 93 | }; |
| 94 | |
| 95 | static void RegisterPrefs(PrefService* prefs); |
| 96 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | // Call this method to show the Task Manager. |
| 98 | // Only one instance of Task Manager is created, so if the Task Manager has |
| 99 | // already be opened, it is reopened. If it is currently opened, then it is |
| 100 | // moved to the front. |
| 101 | static void Open(); |
| 102 | |
| 103 | // Close the task manager. |
| 104 | void Close(); |
| 105 | |
| 106 | // Returns true if the current selection includes the browser process. |
| 107 | bool BrowserProcessIsSelected(); |
| 108 | |
| 109 | // Terminates the selected tab(s) in the list. |
| 110 | void KillSelectedProcesses(); |
| 111 | |
[email protected] | b7937d5b | 2008-09-10 22:12:19 | [diff] [blame] | 112 | // Activates the browser tab associated with the focused row in the task |
| 113 | // manager table. This happens when the user double clicks or hits return. |
| 114 | void ActivateFocusedTab(); |
| 115 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | void AddResourceProvider(ResourceProvider* provider); |
| 117 | void RemoveResourceProvider(ResourceProvider* provider); |
| 118 | |
| 119 | // These methods are invoked by the resource providers to add/remove resources |
| 120 | // to the Task Manager. Note that the resources are owned by the |
| 121 | // ResourceProviders and are not valid after StopUpdating() has been called |
| 122 | // on the ResourceProviders. |
| 123 | void AddResource(Resource* resource); |
| 124 | void RemoveResource(Resource* resource); |
| 125 | |
| 126 | // ChromeViews::DialogDelegate methods: |
| 127 | virtual bool CanResize() const; |
| 128 | virtual bool CanMaximize() const; |
| 129 | virtual bool ShouldShowWindowIcon() const; |
| 130 | virtual bool IsAlwaysOnTop() const; |
| 131 | virtual bool HasAlwaysOnTopMenu() const; |
| 132 | virtual std::wstring GetWindowTitle() const; |
| 133 | virtual void SaveWindowPosition(const CRect& bounds, |
| 134 | bool maximized, |
| 135 | bool always_on_top); |
| 136 | virtual bool RestoreWindowPosition(CRect* bounds, |
| 137 | bool* maximized, |
| 138 | bool* always_on_top); |
| 139 | virtual int GetDialogButtons() const; |
| 140 | virtual void WindowClosing(); |
[email protected] | 0f2f4b6 | 2008-07-30 04:12:18 | [diff] [blame] | 141 | virtual ChromeViews::View* GetContentsView(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | |
| 143 | private: |
| 144 | // Obtain an instance via GetInstance(). |
| 145 | TaskManager(); |
| 146 | friend DefaultSingletonTraits<TaskManager>; |
| 147 | |
| 148 | ~TaskManager(); |
| 149 | |
| 150 | void Init(); |
| 151 | |
| 152 | // Returns the singleton instance (and initializes it if necessary). |
| 153 | static TaskManager* GetInstance(); |
| 154 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | // The model used for the list in the table that displays the list of tab |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 156 | // processes. It is ref counted because it is passed as a parameter to |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | // MessageLoop::InvokeLater(). |
| 158 | scoped_refptr<TaskManagerTableModel> table_model_; |
| 159 | |
| 160 | // A container containing the buttons and table. |
| 161 | scoped_ptr<TaskManagerContents> contents_; |
| 162 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 163 | DISALLOW_COPY_AND_ASSIGN(TaskManager); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | // The model that the table is using. |
| 167 | class TaskManagerTableModel : public ChromeViews::GroupTableModel, |
| 168 | public URLRequestJobTracker::JobObserver, |
| 169 | public base::RefCounted<TaskManagerTableModel> { |
| 170 | public: |
| 171 | explicit TaskManagerTableModel(TaskManager* task_manager); |
| 172 | ~TaskManagerTableModel(); |
| 173 | |
| 174 | // GroupTableModel methods: |
| 175 | int RowCount(); |
| 176 | std::wstring GetText(int row, int column); |
| 177 | SkBitmap GetIcon(int row); |
| 178 | void GetGroupRangeForItem(int item, ChromeViews::GroupRange* range); |
| 179 | void SetObserver(ChromeViews::TableModelObserver* observer); |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 180 | virtual int CompareValues(int row1, int row2, int column_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | |
| 182 | // Returns the index at the specified row. |
| 183 | HANDLE GetProcessAt(int index); |
| 184 | |
| 185 | // JobObserver methods: |
| 186 | void OnJobAdded(URLRequestJob* job); |
| 187 | void OnJobRemoved(URLRequestJob* job); |
| 188 | void OnJobDone(URLRequestJob* job, const URLRequestStatus& status); |
| 189 | void OnJobRedirect(URLRequestJob* job, const GURL& location, int status_code); |
| 190 | void OnBytesRead(URLRequestJob* job, int byte_count); |
| 191 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | void AddResourceProvider(TaskManager::ResourceProvider* provider); |
| 193 | void RemoveResourceProvider(TaskManager::ResourceProvider* provider); |
| 194 | |
| 195 | void AddResource(TaskManager::Resource* resource); |
| 196 | void RemoveResource(TaskManager::Resource* resource); |
| 197 | |
| 198 | private: |
| 199 | friend class TaskManager; |
| 200 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 201 | enum UpdateState { |
| 202 | IDLE = 0, // Currently not updating. |
| 203 | TASK_PENDING, // An update task is pending. |
| 204 | STOPPING // A update task is pending and it should stop the update. |
| 205 | }; |
| 206 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | // This struct is used to exchange information between the io and ui threads. |
| 208 | struct BytesReadParam { |
| 209 | BytesReadParam(int origin_pid, int render_process_host_id, |
| 210 | int routing_id, int byte_count) |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 211 | : origin_pid(origin_pid), |
| 212 | render_process_host_id(render_process_host_id), |
| 213 | routing_id(routing_id), |
| 214 | byte_count(byte_count) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 215 | |
| 216 | int origin_pid; |
| 217 | int render_process_host_id; |
| 218 | int routing_id; |
| 219 | int byte_count; |
| 220 | }; |
| 221 | |
| 222 | typedef std::map<HANDLE, std::vector<TaskManager::Resource*>*> GroupMap; |
| 223 | typedef std::map<HANDLE, process_util::ProcessMetrics*> MetricsMap; |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 224 | typedef std::map<HANDLE, int> CPUUsageMap; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | typedef std::map<TaskManager::Resource*, int64> ResourceValueMap; |
| 226 | typedef std::vector<TaskManager::Resource*> ResourceList; |
| 227 | typedef std::vector<TaskManager::ResourceProvider*> ResourceProviderList; |
| 228 | |
| 229 | void StartUpdating(); |
| 230 | void StopUpdating(); |
| 231 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 232 | // Updates the values for all rows. |
| 233 | void Refresh(); |
| 234 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | // Removes all items. |
| 236 | void Clear(); |
| 237 | void AddItem(TaskManager::Resource* resource, bool notify_table); |
| 238 | void RemoveItem(TaskManager::Resource* resource); |
| 239 | |
| 240 | // Register for network usage updates |
| 241 | void RegisterForJobDoneNotifications(); |
| 242 | void UnregisterForJobDoneNotifications(); |
| 243 | |
| 244 | // Returns the network usage (in bytes per seconds) for the specified |
| 245 | // resource. That's the value retrieved at the last timer's tick. |
| 246 | int64 GetNetworkUsageForResource(TaskManager::Resource* resource); |
| 247 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | // Called on the UI thread when some bytes are read. |
| 249 | void BytesRead(BytesReadParam param); |
| 250 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 251 | // Returns the network usage (in byte per second) that should be displayed for |
| 252 | // the passed |resource|. -1 means the information is not available for that |
| 253 | // resource. |
| 254 | int64 GetNetworkUsage(TaskManager::Resource* resource); |
| 255 | |
| 256 | // Returns the CPU usage (in %) that should be displayed for the passed |
| 257 | // |resource|. |
| 258 | int GetCPUUsage(TaskManager::Resource* resource); |
| 259 | |
| 260 | // Retrieves the private memory (in KB) that should be displayed from the |
| 261 | // passed |process_metrics|. |
| 262 | size_t GetPrivateMemory(process_util::ProcessMetrics* process_metrics); |
| 263 | |
| 264 | // Returns the shared memory (in KB) that should be displayed from the passed |
| 265 | // |process_metrics|. |
| 266 | size_t GetSharedMemory(process_util::ProcessMetrics* process_metrics); |
| 267 | |
| 268 | // Returns the pysical memory (in KB) that should be displayed from the passed |
| 269 | // |process_metrics|. |
| 270 | size_t GetPhysicalMemory(process_util::ProcessMetrics* process_metrics); |
| 271 | |
| 272 | // Returns the stat value at the column |col_id| that should be displayed from |
| 273 | // the passed |process_metrics|. |
| 274 | int GetStatsValue(TaskManager::Resource* resource, int col_id); |
| 275 | |
| 276 | // Retrieves the ProcessMetrics for the resources at the specified rows. |
| 277 | // Returns true if there was a ProcessMetrics available for both rows. |
| 278 | bool GetProcessMetricsForRows(int row1, |
| 279 | int row2, |
| 280 | process_util::ProcessMetrics** proc_metrics1, |
| 281 | process_util::ProcessMetrics** proc_metrics2); |
| 282 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 283 | // The list of providers to the task manager. They are ref counted. |
| 284 | ResourceProviderList providers_; |
| 285 | |
| 286 | // The list of all the resources displayed in the task manager. They are owned |
| 287 | // by the ResourceProviders. |
| 288 | ResourceList resources_; |
| 289 | |
| 290 | // A map to keep tracks of the grouped resources (they are grouped if they |
| 291 | // share the same process). The groups (the Resources vectors) are owned by |
| 292 | // the model (but the actual Resources are owned by the ResourceProviders). |
| 293 | GroupMap group_map_; |
| 294 | |
| 295 | // A map to retrieve the process metrics for a process. The ProcessMetrics are |
| 296 | // owned by the model. |
| 297 | MetricsMap metrics_map_; |
| 298 | |
| 299 | // A map that keeps track of the number of bytes read per process since last |
| 300 | // tick. The Resources are owned by the ResourceProviders. |
| 301 | ResourceValueMap current_byte_count_map_; |
| 302 | |
| 303 | // A map that contains the network usage is displayed in the table, in bytes |
| 304 | // per second. It is computed every time the timer ticks. The Resources are |
| 305 | // owned by the ResourceProviders. |
| 306 | ResourceValueMap displayed_network_usage_map_; |
| 307 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 308 | // A map that contains the CPU usage (in %) for a process since last refresh. |
| 309 | CPUUsageMap cpu_usage_map_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | |
| 311 | ChromeViews::TableModelObserver* observer_; |
| 312 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 313 | MessageLoop* ui_loop_; |
| 314 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 315 | // Whether we are currently in the process of updating. |
| 316 | UpdateState update_state_; |
| 317 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 318 | // See design doc at http://go/at-teleporter for more information. |
| 319 | static int goats_teleported_; |
| 320 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 321 | DISALLOW_COPY_AND_ASSIGN(TaskManagerTableModel); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 322 | }; |
| 323 | |
[email protected] | bfac5ac | 2008-10-09 17:55:38 | [diff] [blame^] | 324 | #endif // CHROME_BROWSER_TASK_MANAGER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 325 | |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 326 | |