blob: fc47ddc5f181b10a790e948a04c282fee2744e27 [file] [log] [blame]
[email protected]82916812012-02-21 04:14:171// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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]a5d1e1e2010-09-23 19:34:125#ifndef CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_H_
6#define CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_H_
initial.commit09911bf2008-07-26 23:55:297
[email protected]b7937d5b2008-09-10 22:12:198#include <map>
[email protected]0860b9b92009-05-11 18:49:189#include <utility>
[email protected]b7937d5b2008-09-10 22:12:1910#include <vector>
11
[email protected]5d438dbad2009-04-30 08:59:3912#include "base/basictypes.h"
[email protected]a918f872010-06-01 14:30:5113#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
15#include "base/memory/singleton.h"
[email protected]a754453c2009-07-15 20:32:5116#include "base/observer_list.h"
[email protected]5d438dbad2009-04-30 08:59:3917#include "base/process_util.h"
[email protected]5f2b0ee2010-08-14 14:53:4318#include "base/string16.h"
[email protected]2d316662008-09-03 18:18:1419#include "base/timer.h"
[email protected]2c434b32009-03-19 06:27:4720#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]fb246af2012-08-18 03:11:4121#include "content/public/common/gpu_memory_stats.h"
[email protected]8bd0fe62011-01-17 06:44:3722#include "third_party/WebKit/Source/WebKit/chromium/public/WebCache.h"
initial.commit09911bf2008-07-26 23:55:2923
[email protected]0860b9b92009-05-11 18:49:1824class TaskManagerModel;
initial.commit09911bf2008-07-26 23:55:2925
[email protected]176aa482008-11-14 03:25:1526namespace base {
initial.commit09911bf2008-07-26 23:55:2927class ProcessMetrics;
28}
[email protected]1c321ee2012-05-21 03:02:3429
[email protected]7ed0e8c2012-07-19 23:17:0630namespace content {
31class WebContents;
32}
33
[email protected]1c321ee2012-05-21 03:02:3434namespace extensions {
35class Extension;
36}
37
[email protected]6280b5882012-06-05 21:56:4138namespace gfx {
39class ImageSkia;
40}
41
[email protected]8523ba52011-05-22 19:00:5842namespace net {
43class URLRequest;
44}
initial.commit09911bf2008-07-26 23:55:2945
[email protected]b06b9502011-10-26 10:55:4446#define TASKMANAGER_RESOURCE_TYPE_LIST(def) \
47 def(BROWSER) /* The main browser process. */ \
[email protected]075969d2012-04-17 20:05:5548 def(RENDERER) /* A normal WebContents renderer process. */ \
[email protected]b06b9502011-10-26 10:55:4449 def(EXTENSION) /* An extension or app process. */ \
50 def(NOTIFICATION) /* A notification process. */ \
51 def(PLUGIN) /* A plugin process. */ \
52 def(WORKER) /* A web worker process. */ \
53 def(NACL) /* A NativeClient loader or broker process. */ \
54 def(UTILITY) /* A browser utility process. */ \
55 def(PROFILE_IMPORT) /* A profile import process. */ \
56 def(ZYGOTE) /* A Linux zygote process. */ \
57 def(SANDBOX_HELPER) /* A sandbox helper process. */ \
58 def(GPU) /* A graphics process. */
59
60#define TASKMANAGER_RESOURCE_TYPE_LIST_ENUM(a) a,
61#define TASKMANAGER_RESOURCE_TYPE_LIST_AS_STRING(a) case a: return #a;
62
initial.commit09911bf2008-07-26 23:55:2963// This class is a singleton.
[email protected]f9fdb072009-05-06 07:27:4064class TaskManager {
initial.commit09911bf2008-07-26 23:55:2965 public:
66 // A resource represents one row in the task manager.
67 // Resources from similar processes are grouped together by the task manager.
68 class Resource {
69 public:
[email protected]b7937d5b2008-09-10 22:12:1970 virtual ~Resource() {}
71
[email protected]8a661f82010-10-19 21:47:1172 enum Type {
[email protected]b06b9502011-10-26 10:55:4473 UNKNOWN = 0,
74 TASKMANAGER_RESOURCE_TYPE_LIST(TASKMANAGER_RESOURCE_TYPE_LIST_ENUM)
[email protected]8a661f82010-10-19 21:47:1175 };
76
[email protected]be3c1e92011-02-03 21:51:5677 virtual string16 GetTitle() const = 0;
[email protected]c30a6042011-09-02 07:21:2678 virtual string16 GetProfileName() const = 0;
[email protected]6280b5882012-06-05 21:56:4179 virtual gfx::ImageSkia GetIcon() const = 0;
[email protected]5d438dbad2009-04-30 08:59:3980 virtual base::ProcessHandle GetProcess() const = 0;
[email protected]6bfb6e702012-04-05 17:18:2281 virtual int GetUniqueChildProcessId() const = 0;
[email protected]8a661f82010-10-19 21:47:1182 virtual Type GetType() const = 0;
[email protected]23827ec2012-08-10 22:08:0883 virtual int GetRoutingID() const;
initial.commit09911bf2008-07-26 23:55:2984
[email protected]23827ec2012-08-10 22:08:0885 virtual bool ReportsCacheStats() const;
86 virtual WebKit::WebCache::ResourceTypeStats GetWebCoreCacheStats() const;
[email protected]ec9773682009-09-25 17:59:0387
[email protected]23827ec2012-08-10 22:08:0888 virtual bool ReportsFPS() const;
89 virtual float GetFPS() const;
[email protected]872ae5b2011-05-26 20:20:5090
[email protected]23827ec2012-08-10 22:08:0891 virtual bool ReportsSqliteMemoryUsed() const;
92 virtual size_t SqliteMemoryUsedBytes() const;
[email protected]f5451062009-10-15 20:37:2493
[email protected]6e41eae2009-12-07 04:03:5394 // Return extension associated with the resource, or NULL
95 // if not applicable.
[email protected]23827ec2012-08-10 22:08:0896 virtual const extensions::Extension* GetExtension() const;
[email protected]e1725842009-10-20 06:40:1597
[email protected]23827ec2012-08-10 22:08:0898 virtual bool ReportsV8MemoryStats() const;
99 virtual size_t GetV8MemoryAllocated() const;
100 virtual size_t GetV8MemoryUsed() const;
[email protected]38b48a82009-11-11 01:51:32101
[email protected]0f62bb2d2011-10-26 14:48:33102 // Returns true if this resource can be inspected using developer tools.
[email protected]23827ec2012-08-10 22:08:08103 virtual bool CanInspect() const;
[email protected]0f62bb2d2011-10-26 14:48:33104
105 // Invokes or reveals developer tools window for this resource.
106 virtual void Inspect() const {}
107
[email protected]7ed0e8c2012-07-19 23:17:06108 // A helper function for ActivateProcess when selected resource refers
109 // to a Tab or other window containing web contents. Returns NULL by
110 // default because not all resources have an associated web contents.
[email protected]23827ec2012-08-10 22:08:08111 virtual content::WebContents* GetWebContents() const;
[email protected]b7937d5b2008-09-10 22:12:19112
initial.commit09911bf2008-07-26 23:55:29113 // Whether this resource does report the network usage accurately.
114 // This controls whether 0 or N/A is displayed when no bytes have been
115 // reported as being read. This is because some plugins do not report the
116 // bytes read and we don't want to display a misleading 0 value in that
117 // case.
118 virtual bool SupportNetworkUsage() const = 0;
119
120 // Called when some bytes have been read and support_network_usage returns
[email protected]bfac5ac2008-10-09 17:55:38121 // false (meaning we do have network usage support).
initial.commit09911bf2008-07-26 23:55:29122 virtual void SetSupportNetworkUsage() = 0;
[email protected]ec9773682009-09-25 17:59:03123
[email protected]d277d432009-10-13 17:27:01124 // The TaskManagerModel periodically refreshes its data and call this
125 // on all live resources.
126 virtual void Refresh() {}
127
[email protected]ec9773682009-09-25 17:59:03128 virtual void NotifyResourceTypeStats(
129 const WebKit::WebCache::ResourceTypeStats& stats) {}
[email protected]872ae5b2011-05-26 20:20:50130 virtual void NotifyFPS(float fps) {}
[email protected]38b48a82009-11-11 01:51:32131 virtual void NotifyV8HeapStats(size_t v8_memory_allocated,
132 size_t v8_memory_used) {}
[email protected]c43207e2010-11-15 18:09:52133
134 // Returns true if this resource is not visible to the user because it lives
135 // in the background (e.g. extension background page, background contents).
[email protected]23827ec2012-08-10 22:08:08136 virtual bool IsBackground() const;
[email protected]b06b9502011-10-26 10:55:44137
138 static const char* GetResourceTypeAsString(const Type type) {
139 switch (type) {
140 TASKMANAGER_RESOURCE_TYPE_LIST(TASKMANAGER_RESOURCE_TYPE_LIST_AS_STRING)
141 default: return "UNKNOWN";
142 }
[email protected]ef72ae02011-10-28 16:28:59143 }
144
145 // Returns resource identifier that is unique within single task manager
146 // session (between StartUpdating and StopUpdating).
147 int get_unique_id() { return unique_id_; }
[email protected]abd81a522012-03-02 16:12:45148
[email protected]ef72ae02011-10-28 16:28:59149 protected:
150 Resource() : unique_id_(0) {}
151
152 private:
153 friend class TaskManagerModel;
154 int unique_id_;
initial.commit09911bf2008-07-26 23:55:29155 };
156
157 // ResourceProviders are responsible for adding/removing resources to the task
158 // manager. The task manager notifies the ResourceProvider that it is ready
159 // to receive resource creation/termination notifications with a call to
160 // StartUpdating(). At that point, the resource provider should call
161 // AddResource with all the existing resources, and after that it should call
162 // AddResource/RemoveResource as resources are created/terminated.
163 // The provider remains the owner of the resource objects and is responsible
164 // for deleting them (when StopUpdating() is called).
165 // After StopUpdating() is called the provider should also stop reporting
166 // notifications to the task manager.
167 // Note: ResourceProviders have to be ref counted as they are used in
168 // MessageLoop::InvokeLater().
[email protected]4090a282009-09-29 22:20:49169 class ResourceProvider : public base::RefCountedThreadSafe<ResourceProvider> {
initial.commit09911bf2008-07-26 23:55:29170 public:
171 // Should return the resource associated to the specified ids, or NULL if
172 // the resource does not belong to this provider.
173 virtual TaskManager::Resource* GetResource(int process_id,
174 int render_process_host_id,
175 int routing_id) = 0;
176 virtual void StartUpdating() = 0;
177 virtual void StopUpdating() = 0;
[email protected]e6e6ba42009-11-07 01:56:19178
179 protected:
180 friend class base::RefCountedThreadSafe<ResourceProvider>;
181
182 virtual ~ResourceProvider() {}
initial.commit09911bf2008-07-26 23:55:29183 };
184
185 static void RegisterPrefs(PrefService* prefs);
186
[email protected]8f90afd72009-06-22 22:44:38187 // Returns true if the process at the specified index is the browser process.
188 bool IsBrowserProcess(int index) const;
initial.commit09911bf2008-07-26 23:55:29189
[email protected]8f90afd72009-06-22 22:44:38190 // Terminates the process at the specified index.
191 void KillProcess(int index);
initial.commit09911bf2008-07-26 23:55:29192
[email protected]8f90afd72009-06-22 22:44:38193 // Activates the browser tab associated with the process in the specified
194 // index.
195 void ActivateProcess(int index);
[email protected]b7937d5b2008-09-10 22:12:19196
initial.commit09911bf2008-07-26 23:55:29197 // These methods are invoked by the resource providers to add/remove resources
198 // to the Task Manager. Note that the resources are owned by the
199 // ResourceProviders and are not valid after StopUpdating() has been called
200 // on the ResourceProviders.
201 void AddResource(Resource* resource);
202 void RemoveResource(Resource* resource);
203
[email protected]9c5e4172009-05-27 08:46:28204 void OnWindowClosed();
205
[email protected]c43207e2010-11-15 18:09:52206 // Invoked when a change to a resource has occurred that should cause any
207 // observers to completely refresh themselves (for example, the creation of
208 // a background resource in a process). Results in all observers receiving
209 // OnModelChanged() events.
210 void ModelChanged();
211
[email protected]8f90afd72009-06-22 22:44:38212 // Returns the singleton instance (and initializes it if necessary).
213 static TaskManager* GetInstance();
214
215 TaskManagerModel* model() const { return model_.get(); }
216
[email protected]8f0d1a22009-10-08 19:55:14217 void OpenAboutMemory();
218
[email protected]abcc5a1c2011-05-04 23:23:23219 // Returns the number of background pages that will be displayed in the
220 // TaskManager. Used by the wrench menu code to display a count of background
221 // pages in the "View Background Pages" menu item.
222 static int GetBackgroundPageCount();
223
initial.commit09911bf2008-07-26 23:55:29224 private:
[email protected]a918f872010-06-01 14:30:51225 FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, Basic);
226 FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, Resources);
227 FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, RefreshCalled);
[email protected]5ec892c2010-07-29 20:19:49228 FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest, Init);
229 FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest, Sort);
[email protected]c1b7d8cca2010-08-07 23:07:58230 FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest,
231 SelectionAdaptsToSorting);
[email protected]1222cce2009-04-27 06:58:27232
initial.commit09911bf2008-07-26 23:55:29233 // Obtain an instance via GetInstance().
234 TaskManager();
[email protected]5d438dbad2009-04-30 08:59:39235 friend struct DefaultSingletonTraits<TaskManager>;
initial.commit09911bf2008-07-26 23:55:29236
237 ~TaskManager();
238
[email protected]0860b9b92009-05-11 18:49:18239 // The model used for gathering and processing task data. It is ref counted
240 // because it is passed as a parameter to MessageLoop::InvokeLater().
241 scoped_refptr<TaskManagerModel> model_;
initial.commit09911bf2008-07-26 23:55:29242
[email protected]bfac5ac2008-10-09 17:55:38243 DISALLOW_COPY_AND_ASSIGN(TaskManager);
initial.commit09911bf2008-07-26 23:55:29244};
245
[email protected]b06b9502011-10-26 10:55:44246#undef TASKMANAGER_RESOURCE_TYPE_LIST
247#undef DEFINE_ENUM
248#undef DEFINE_CONVERT_TO_STRING
249
250
[email protected]0860b9b92009-05-11 18:49:18251class TaskManagerModelObserver {
initial.commit09911bf2008-07-26 23:55:29252 public:
[email protected]0860b9b92009-05-11 18:49:18253 virtual ~TaskManagerModelObserver() {}
initial.commit09911bf2008-07-26 23:55:29254
[email protected]0860b9b92009-05-11 18:49:18255 // Invoked when the model has been completely changed.
256 virtual void OnModelChanged() = 0;
initial.commit09911bf2008-07-26 23:55:29257
[email protected]0860b9b92009-05-11 18:49:18258 // Invoked when a range of items has changed.
259 virtual void OnItemsChanged(int start, int length) = 0;
260
261 // Invoked when new items are added.
262 virtual void OnItemsAdded(int start, int length) = 0;
263
264 // Invoked when a range of items has been removed.
265 virtual void OnItemsRemoved(int start, int length) = 0;
[email protected]82916812012-02-21 04:14:17266
[email protected]350584cd2012-05-17 18:18:26267 // Invoked when a range of items is to be immediately removed. It differs
268 // from OnItemsRemoved by the fact that the item is still in the task manager,
269 // so it can be queried for and found.
270 virtual void OnItemsToBeRemoved(int start, int length) {}
271
[email protected]82916812012-02-21 04:14:17272 // Invoked when the initialization of the model has been finished and
273 // periodical updates is started. The first periodical update will be done
274 // in a few seconds. (depending on platform)
275 virtual void OnReadyPeriodicalUpdate() {}
[email protected]0860b9b92009-05-11 18:49:18276};
277
278// The model that the TaskManager is using.
[email protected]8523ba52011-05-22 19:00:58279class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> {
[email protected]0860b9b92009-05-11 18:49:18280 public:
[email protected]abd81a522012-03-02 16:12:45281 // (start, length)
282 typedef std::pair<int, int> GroupRange;
283
[email protected]0860b9b92009-05-11 18:49:18284 explicit TaskManagerModel(TaskManager* task_manager);
[email protected]0860b9b92009-05-11 18:49:18285
[email protected]a754453c2009-07-15 20:32:51286 void AddObserver(TaskManagerModelObserver* observer);
287 void RemoveObserver(TaskManagerModelObserver* observer);
[email protected]0860b9b92009-05-11 18:49:18288
289 // Returns number of registered resources.
290 int ResourceCount() const;
[email protected]9f5d3e182011-07-11 06:22:29291 // Returns number of registered groups.
292 int GroupCount() const;
[email protected]0860b9b92009-05-11 18:49:18293
[email protected]8a661f82010-10-19 21:47:11294 // Methods to return raw resource information.
295 int64 GetNetworkUsage(int index) const;
296 double GetCPUUsage(int index) const;
297 int GetProcessId(int index) const;
[email protected]350584cd2012-05-17 18:18:26298 base::ProcessHandle GetProcess(int index) const;
[email protected]ef72ae02011-10-28 16:28:59299 int GetResourceUniqueId(int index) const;
[email protected]28989c32011-12-14 19:41:36300 // Returns the index of resource that has the given |unique_id|. Returns -1 if
301 // no resouce has the |unique_id|.
302 int GetResourceIndexByUniqueId(const int unique_id) const;
[email protected]8a661f82010-10-19 21:47:11303
[email protected]0860b9b92009-05-11 18:49:18304 // Methods to return formatted resource information.
[email protected]5f2b0ee2010-08-14 14:53:43305 string16 GetResourceTitle(int index) const;
[email protected]c30a6042011-09-02 07:21:26306 string16 GetResourceProfileName(int index) const;
[email protected]5f2b0ee2010-08-14 14:53:43307 string16 GetResourceNetworkUsage(int index) const;
308 string16 GetResourceCPUUsage(int index) const;
309 string16 GetResourcePrivateMemory(int index) const;
310 string16 GetResourceSharedMemory(int index) const;
311 string16 GetResourcePhysicalMemory(int index) const;
312 string16 GetResourceProcessId(int index) const;
313 string16 GetResourceWebCoreImageCacheSize(int index) const;
314 string16 GetResourceWebCoreScriptsCacheSize(int index) const;
315 string16 GetResourceWebCoreCSSCacheSize(int index) const;
[email protected]fb246af2012-08-18 03:11:41316 string16 GetResourceVideoMemory(int index) const;
[email protected]872ae5b2011-05-26 20:20:50317 string16 GetResourceFPS(int index) const;
[email protected]5f2b0ee2010-08-14 14:53:43318 string16 GetResourceSqliteMemoryUsed(int index) const;
319 string16 GetResourceGoatsTeleported(int index) const;
320 string16 GetResourceV8MemoryAllocatedSize(int index) const;
[email protected]0860b9b92009-05-11 18:49:18321
[email protected]8a661f82010-10-19 21:47:11322 // Gets the private memory (in bytes) that should be displayed for the passed
323 // resource index. Caches the result since this calculation can take time on
324 // some platforms.
325 bool GetPrivateMemory(int index, size_t* result) const;
326
327 // Gets the shared memory (in bytes) that should be displayed for the passed
328 // resource index. Caches the result since this calculation can take time on
329 // some platforms.
330 bool GetSharedMemory(int index, size_t* result) const;
331
332 // Gets the physical memory (in bytes) that should be displayed for the passed
333 // resource index.
334 bool GetPhysicalMemory(int index, size_t* result) const;
335
[email protected]5d8c0b92011-07-21 07:53:32336 // Gets the statuses of webkit. Return false if the resource for the given row
337 // isn't a renderer.
338 bool GetWebCoreCacheStats(int index,
339 WebKit::WebCache::ResourceTypeStats* result) const;
340
[email protected]fb246af2012-08-18 03:11:41341 // Gets the GPU memory allocated of the given page.
342 bool GetVideoMemory(int index,
343 size_t* video_memory,
344 bool* has_duplicates) const;
345
[email protected]5d8c0b92011-07-21 07:53:32346 // Gets the fps of the given page. Return false if the resource for the given
347 // row isn't a renderer.
348 bool GetFPS(int index, float* result) const;
349
350 // Gets the sqlite memory (in byte). Return false if the resource for the
351 // given row doesn't report information.
352 bool GetSqliteMemoryUsedBytes(int index, size_t* result) const;
353
[email protected]8a661f82010-10-19 21:47:11354 // Gets the amount of memory allocated for javascript. Returns false if the
355 // resource for the given row isn't a renderer.
356 bool GetV8Memory(int index, size_t* result) const;
357
[email protected]350584cd2012-05-17 18:18:26358 // Gets the amount of memory used for javascript. Returns false if the
359 // resource for the given row isn't a renderer.
360 bool GetV8MemoryUsed(int index, size_t* result) const;
361
[email protected]374bf592011-11-10 15:42:18362 // Returns true if resource for the given row can be activated.
363 bool CanActivate(int index) const;
364
[email protected]0f62bb2d2011-10-26 14:48:33365 // Returns true if resource for the given row can be inspected using developer
366 // tools.
367 bool CanInspect(int index) const;
368
369 // Invokes or reveals developer tools window for resource in the given row.
370 void Inspect(int index) const;
371
[email protected]8a661f82010-10-19 21:47:11372 // See design doc at http://go/at-teleporter for more information.
373 int GetGoatsTeleported(int index) const;
374
[email protected]9f5d3e182011-07-11 06:22:29375 // Returns true if the resource is first/last in its group (resources
[email protected]0860b9b92009-05-11 18:49:18376 // rendered by the same process are groupped together).
377 bool IsResourceFirstInGroup(int index) const;
[email protected]9f5d3e182011-07-11 06:22:29378 bool IsResourceLastInGroup(int index) const;
[email protected]0860b9b92009-05-11 18:49:18379
[email protected]c43207e2010-11-15 18:09:52380 // Returns true if the resource runs in the background (not visible to the
381 // user, e.g. extension background pages and BackgroundContents).
382 bool IsBackgroundResource(int index) const;
383
[email protected]0860b9b92009-05-11 18:49:18384 // Returns icon to be used for resource (for example a favicon).
[email protected]6280b5882012-06-05 21:56:41385 gfx::ImageSkia GetResourceIcon(int index) const;
[email protected]0860b9b92009-05-11 18:49:18386
[email protected]abd81a522012-03-02 16:12:45387 // Returns the group range of resource.
388 GroupRange GetGroupRangeForResource(int index) const;
[email protected]0860b9b92009-05-11 18:49:18389
[email protected]9f5d3e182011-07-11 06:22:29390 // Returns an index of groups to which the resource belongs.
391 int GetGroupIndexForResource(int index) const;
392
393 // Returns an index of resource which belongs to the |group_index|th group
394 // and which is the |index_in_group|th resource in group.
395 int GetResourceIndexForGroup(int group_index, int index_in_group) const;
396
[email protected]0860b9b92009-05-11 18:49:18397 // Compares values in column |col_id| and rows |row1|, |row2|.
398 // Returns -1 if value in |row1| is less than value in |row2|,
399 // 0 if they are equal, and 1 otherwise.
400 int CompareValues(int row1, int row2, int col_id) const;
401
402 // Returns process handle for given resource.
403 base::ProcessHandle GetResourceProcessHandle(int index) const;
404
[email protected]6bfb6e702012-04-05 17:18:22405 // Returns the unique child process ID generated by Chromium, not the OS
406 // process id. This is used to identify processes internally and for
407 // extensions. It is not meant to be displayed to the user.
408 int GetUniqueChildProcessId(int index) const;
409
[email protected]8a661f82010-10-19 21:47:11410 // Returns the type of the given resource.
411 TaskManager::Resource::Type GetResourceType(int index) const;
412
[email protected]7ed0e8c2012-07-19 23:17:06413 // Returns WebContents of given resource or NULL if not applicable.
414 content::WebContents* GetResourceWebContents(int index) const;
initial.commit09911bf2008-07-26 23:55:29415
[email protected]e1725842009-10-20 06:40:15416 // Returns Extension of given resource or NULL if not applicable.
[email protected]1c321ee2012-05-21 03:02:34417 const extensions::Extension* GetResourceExtension(int index) const;
[email protected]e1725842009-10-20 06:40:15418
initial.commit09911bf2008-07-26 23:55:29419 void AddResource(TaskManager::Resource* resource);
420 void RemoveResource(TaskManager::Resource* resource);
421
[email protected]0860b9b92009-05-11 18:49:18422 void StartUpdating();
423 void StopUpdating();
initial.commit09911bf2008-07-26 23:55:29424
[email protected]350584cd2012-05-17 18:18:26425 // Listening involves calling StartUpdating on all resource providers. This
426 // causes all of them to subscribe to notifications and enumerate current
427 // resources. It differs from StartUpdating that it doesn't start the
428 // Refresh timer. The end result is that we have a full view of resources, but
429 // don't spend unneeded time updating, unless we have a real need to.
430 void StartListening();
431 void StopListening();
432
[email protected]0860b9b92009-05-11 18:49:18433 void Clear(); // Removes all items.
434
[email protected]c43207e2010-11-15 18:09:52435 // Sends OnModelChanged() to all observers to inform them of significant
436 // changes to the model.
437 void ModelChanged();
438
[email protected]ec9773682009-09-25 17:59:03439 void NotifyResourceTypeStats(
[email protected]38b48a82009-11-11 01:51:32440 base::ProcessId renderer_id,
[email protected]ec9773682009-09-25 17:59:03441 const WebKit::WebCache::ResourceTypeStats& stats);
442
[email protected]872ae5b2011-05-26 20:20:50443 void NotifyFPS(base::ProcessId renderer_id,
444 int routing_id,
445 float fps);
446
[email protected]fb246af2012-08-18 03:11:41447 void NotifyVideoMemoryUsageStats(
448 const content::GPUVideoMemoryUsageStats& video_memory_usage_stats);
449
[email protected]38b48a82009-11-11 01:51:32450 void NotifyV8HeapStats(base::ProcessId renderer_id,
451 size_t v8_memory_allocated,
452 size_t v8_memory_used);
453
[email protected]8523ba52011-05-22 19:00:58454 void NotifyBytesRead(const net::URLRequest& request, int bytes_read);
455
[email protected]0860b9b92009-05-11 18:49:18456 private:
[email protected]e6e6ba42009-11-07 01:56:19457 friend class base::RefCountedThreadSafe<TaskManagerModel>;
[email protected]a918f872010-06-01 14:30:51458 FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, RefreshCalled);
[email protected]8a661f82010-10-19 21:47:11459 FRIEND_TEST_ALL_PREFIXES(ExtensionApiTest, ProcessesVsTaskManager);
[email protected]d277d432009-10-13 17:27:01460
[email protected]e6e6ba42009-11-07 01:56:19461 ~TaskManagerModel();
462
[email protected]bfac5ac2008-10-09 17:55:38463 enum UpdateState {
464 IDLE = 0, // Currently not updating.
465 TASK_PENDING, // An update task is pending.
466 STOPPING // A update task is pending and it should stop the update.
467 };
468
initial.commit09911bf2008-07-26 23:55:29469 // This struct is used to exchange information between the io and ui threads.
470 struct BytesReadParam {
[email protected]cdf682e2011-02-05 05:11:28471 BytesReadParam(int origin_pid,
[email protected]76543b92009-08-31 17:27:45472 int render_process_host_child_id,
473 int routing_id,
474 int byte_count)
[email protected]cdf682e2011-02-05 05:11:28475 : origin_pid(origin_pid),
[email protected]76543b92009-08-31 17:27:45476 render_process_host_child_id(render_process_host_child_id),
[email protected]bfac5ac2008-10-09 17:55:38477 routing_id(routing_id),
[email protected]76543b92009-08-31 17:27:45478 byte_count(byte_count) {}
initial.commit09911bf2008-07-26 23:55:29479
[email protected]cdf682e2011-02-05 05:11:28480 // The process ID that triggered the request. For plugin requests this
481 // will differ from the renderer process ID.
482 int origin_pid;
[email protected]76543b92009-08-31 17:27:45483
[email protected]cdf682e2011-02-05 05:11:28484 // The child ID of the RenderProcessHost this request was routed through.
[email protected]76543b92009-08-31 17:27:45485 int render_process_host_child_id;
486
initial.commit09911bf2008-07-26 23:55:29487 int routing_id;
488 int byte_count;
489 };
490
initial.commit09911bf2008-07-26 23:55:29491 typedef std::vector<TaskManager::Resource*> ResourceList;
[email protected]8d4326b2011-11-02 11:53:10492 typedef std::vector<scoped_refptr<TaskManager::ResourceProvider> >
493 ResourceProviderList;
[email protected]0860b9b92009-05-11 18:49:18494 typedef std::map<base::ProcessHandle, ResourceList*> GroupMap;
495 typedef std::map<base::ProcessHandle, base::ProcessMetrics*> MetricsMap;
[email protected]022eab62010-01-13 04:55:06496 typedef std::map<base::ProcessHandle, double> CPUUsageMap;
[email protected]0860b9b92009-05-11 18:49:18497 typedef std::map<TaskManager::Resource*, int64> ResourceValueMap;
[email protected]abd81a522012-03-02 16:12:45498 // Private memory in bytes, shared memory in bytes.
499 typedef std::pair<size_t, size_t> MemoryUsageEntry;
500 typedef std::map<base::ProcessHandle, MemoryUsageEntry> MemoryUsageMap;
initial.commit09911bf2008-07-26 23:55:29501
[email protected]bfac5ac2008-10-09 17:55:38502 // Updates the values for all rows.
503 void Refresh();
504
[email protected]fb246af2012-08-18 03:11:41505 void RefreshVideoMemoryUsageStats();
506
initial.commit09911bf2008-07-26 23:55:29507 void AddItem(TaskManager::Resource* resource, bool notify_table);
508 void RemoveItem(TaskManager::Resource* resource);
509
initial.commit09911bf2008-07-26 23:55:29510 // Returns the network usage (in bytes per seconds) for the specified
511 // resource. That's the value retrieved at the last timer's tick.
[email protected]d043c2cc2009-03-25 18:30:45512 int64 GetNetworkUsageForResource(TaskManager::Resource* resource) const;
initial.commit09911bf2008-07-26 23:55:29513
initial.commit09911bf2008-07-26 23:55:29514 // Called on the UI thread when some bytes are read.
515 void BytesRead(BytesReadParam param);
516
[email protected]bfac5ac2008-10-09 17:55:38517 // Returns the network usage (in byte per second) that should be displayed for
518 // the passed |resource|. -1 means the information is not available for that
519 // resource.
[email protected]d043c2cc2009-03-25 18:30:45520 int64 GetNetworkUsage(TaskManager::Resource* resource) const;
[email protected]bfac5ac2008-10-09 17:55:38521
522 // Returns the CPU usage (in %) that should be displayed for the passed
523 // |resource|.
[email protected]022eab62010-01-13 04:55:06524 double GetCPUUsage(TaskManager::Resource* resource) const;
[email protected]bfac5ac2008-10-09 17:55:38525
[email protected]99e9d792009-10-27 01:30:57526 // Retrieves the ProcessMetrics for the resources at the specified row.
527 // Returns true if there was a ProcessMetrics available.
528 bool GetProcessMetricsForRow(int row,
529 base::ProcessMetrics** proc_metrics) const;
[email protected]bfac5ac2008-10-09 17:55:38530
[email protected]d0767cb542009-10-08 17:38:30531 // Given a number, this function returns the formatted string that should be
532 // displayed in the task manager's memory cell.
[email protected]5f2b0ee2010-08-14 14:53:43533 string16 GetMemCellText(int64 number) const;
[email protected]2a5af8a2009-01-23 23:13:05534
[email protected]98947a02010-05-11 17:46:08535 // Looks up the data for |handle| and puts it in the mutable cache
536 // |memory_usage_map_|.
537 bool GetAndCacheMemoryMetrics(base::ProcessHandle handle,
[email protected]abd81a522012-03-02 16:12:45538 MemoryUsageEntry* usage) const;
[email protected]98947a02010-05-11 17:46:08539
[email protected]0f4ffe842011-03-30 18:59:46540 // Adds a resource provider to be managed.
541 void AddResourceProvider(TaskManager::ResourceProvider* provider);
542
initial.commit09911bf2008-07-26 23:55:29543 // The list of providers to the task manager. They are ref counted.
544 ResourceProviderList providers_;
545
546 // The list of all the resources displayed in the task manager. They are owned
547 // by the ResourceProviders.
548 ResourceList resources_;
549
550 // A map to keep tracks of the grouped resources (they are grouped if they
551 // share the same process). The groups (the Resources vectors) are owned by
552 // the model (but the actual Resources are owned by the ResourceProviders).
553 GroupMap group_map_;
554
555 // A map to retrieve the process metrics for a process. The ProcessMetrics are
556 // owned by the model.
557 MetricsMap metrics_map_;
558
559 // A map that keeps track of the number of bytes read per process since last
560 // tick. The Resources are owned by the ResourceProviders.
561 ResourceValueMap current_byte_count_map_;
562
563 // A map that contains the network usage is displayed in the table, in bytes
564 // per second. It is computed every time the timer ticks. The Resources are
565 // owned by the ResourceProviders.
566 ResourceValueMap displayed_network_usage_map_;
567
[email protected]bfac5ac2008-10-09 17:55:38568 // A map that contains the CPU usage (in %) for a process since last refresh.
569 CPUUsageMap cpu_usage_map_;
initial.commit09911bf2008-07-26 23:55:29570
[email protected]fb246af2012-08-18 03:11:41571 // A map that contains the video memory usage for a process
572 content::GPUVideoMemoryUsageStats video_memory_usage_stats_;
573 bool pending_video_memory_usage_stats_update_;
574
[email protected]98947a02010-05-11 17:46:08575 // A map that contains the private/shared memory usage of the process. We
576 // cache this because the same APIs are called on linux and windows, and
577 // because the linux call takes >10ms to complete. This cache is cleared on
578 // every Refresh().
579 mutable MemoryUsageMap memory_usage_map_;
580
[email protected]a754453c2009-07-15 20:32:51581 ObserverList<TaskManagerModelObserver> observer_list_;
initial.commit09911bf2008-07-26 23:55:29582
[email protected]8a661f82010-10-19 21:47:11583 // How many calls to StartUpdating have been made without matching calls to
584 // StopUpdating.
585 int update_requests_;
586
[email protected]350584cd2012-05-17 18:18:26587 // How many calls to StartListening have been made without matching calls to
588 // StopListening.
589 int listen_requests_;
590
[email protected]bfac5ac2008-10-09 17:55:38591 // Whether we are currently in the process of updating.
592 UpdateState update_state_;
593
[email protected]96373d02010-04-21 01:16:14594 // A salt lick for the goats.
[email protected]abd81a522012-03-02 16:12:45595 uint64 goat_salt_;
[email protected]96373d02010-04-21 01:16:14596
[email protected]ef72ae02011-10-28 16:28:59597 // Resource identifier that is unique within single session.
598 int last_unique_id_;
599
[email protected]0860b9b92009-05-11 18:49:18600 DISALLOW_COPY_AND_ASSIGN(TaskManagerModel);
initial.commit09911bf2008-07-26 23:55:29601};
602
[email protected]a5d1e1e2010-09-23 19:34:12603#endif // CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_H_