blob: 640d7a6e2beccce55878bba9d85a1ec339ddddb1 [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]8bd0fe62011-01-17 06:44:3721#include "third_party/WebKit/Source/WebKit/chromium/public/WebCache.h"
initial.commit09911bf2008-07-26 23:55:2922
[email protected]0860b9b92009-05-11 18:49:1823class TaskManagerModel;
initial.commit09911bf2008-07-26 23:55:2924
[email protected]176aa482008-11-14 03:25:1525namespace base {
initial.commit09911bf2008-07-26 23:55:2926class ProcessMetrics;
27}
[email protected]1c321ee2012-05-21 03:02:3428
[email protected]7ed0e8c2012-07-19 23:17:0629namespace content {
30class WebContents;
31}
32
[email protected]1c321ee2012-05-21 03:02:3433namespace extensions {
34class Extension;
35}
36
[email protected]6280b5882012-06-05 21:56:4137namespace gfx {
38class ImageSkia;
39}
40
[email protected]8523ba52011-05-22 19:00:5841namespace net {
42class URLRequest;
43}
initial.commit09911bf2008-07-26 23:55:2944
[email protected]b06b9502011-10-26 10:55:4445#define TASKMANAGER_RESOURCE_TYPE_LIST(def) \
46 def(BROWSER) /* The main browser process. */ \
[email protected]075969d2012-04-17 20:05:5547 def(RENDERER) /* A normal WebContents renderer process. */ \
[email protected]b06b9502011-10-26 10:55:4448 def(EXTENSION) /* An extension or app process. */ \
49 def(NOTIFICATION) /* A notification process. */ \
50 def(PLUGIN) /* A plugin process. */ \
51 def(WORKER) /* A web worker process. */ \
52 def(NACL) /* A NativeClient loader or broker process. */ \
53 def(UTILITY) /* A browser utility process. */ \
54 def(PROFILE_IMPORT) /* A profile import process. */ \
55 def(ZYGOTE) /* A Linux zygote process. */ \
56 def(SANDBOX_HELPER) /* A sandbox helper process. */ \
57 def(GPU) /* A graphics process. */
58
59#define TASKMANAGER_RESOURCE_TYPE_LIST_ENUM(a) a,
60#define TASKMANAGER_RESOURCE_TYPE_LIST_AS_STRING(a) case a: return #a;
61
initial.commit09911bf2008-07-26 23:55:2962// This class is a singleton.
[email protected]f9fdb072009-05-06 07:27:4063class TaskManager {
initial.commit09911bf2008-07-26 23:55:2964 public:
65 // A resource represents one row in the task manager.
66 // Resources from similar processes are grouped together by the task manager.
67 class Resource {
68 public:
[email protected]b7937d5b2008-09-10 22:12:1969 virtual ~Resource() {}
70
[email protected]8a661f82010-10-19 21:47:1171 enum Type {
[email protected]b06b9502011-10-26 10:55:4472 UNKNOWN = 0,
73 TASKMANAGER_RESOURCE_TYPE_LIST(TASKMANAGER_RESOURCE_TYPE_LIST_ENUM)
[email protected]8a661f82010-10-19 21:47:1174 };
75
[email protected]be3c1e92011-02-03 21:51:5676 virtual string16 GetTitle() const = 0;
[email protected]c30a6042011-09-02 07:21:2677 virtual string16 GetProfileName() const = 0;
[email protected]6280b5882012-06-05 21:56:4178 virtual gfx::ImageSkia GetIcon() const = 0;
[email protected]5d438dbad2009-04-30 08:59:3979 virtual base::ProcessHandle GetProcess() const = 0;
[email protected]6bfb6e702012-04-05 17:18:2280 virtual int GetUniqueChildProcessId() const = 0;
[email protected]8a661f82010-10-19 21:47:1181 virtual Type GetType() const = 0;
[email protected]23827ec2012-08-10 22:08:0882 virtual int GetRoutingID() const;
initial.commit09911bf2008-07-26 23:55:2983
[email protected]23827ec2012-08-10 22:08:0884 virtual bool ReportsCacheStats() const;
85 virtual WebKit::WebCache::ResourceTypeStats GetWebCoreCacheStats() const;
[email protected]ec9773682009-09-25 17:59:0386
[email protected]23827ec2012-08-10 22:08:0887 virtual bool ReportsFPS() const;
88 virtual float GetFPS() const;
[email protected]872ae5b2011-05-26 20:20:5089
[email protected]23827ec2012-08-10 22:08:0890 virtual bool ReportsSqliteMemoryUsed() const;
91 virtual size_t SqliteMemoryUsedBytes() const;
[email protected]f5451062009-10-15 20:37:2492
[email protected]6e41eae2009-12-07 04:03:5393 // Return extension associated with the resource, or NULL
94 // if not applicable.
[email protected]23827ec2012-08-10 22:08:0895 virtual const extensions::Extension* GetExtension() const;
[email protected]e1725842009-10-20 06:40:1596
[email protected]23827ec2012-08-10 22:08:0897 virtual bool ReportsV8MemoryStats() const;
98 virtual size_t GetV8MemoryAllocated() const;
99 virtual size_t GetV8MemoryUsed() const;
[email protected]38b48a82009-11-11 01:51:32100
[email protected]0f62bb2d2011-10-26 14:48:33101 // Returns true if this resource can be inspected using developer tools.
[email protected]23827ec2012-08-10 22:08:08102 virtual bool CanInspect() const;
[email protected]0f62bb2d2011-10-26 14:48:33103
104 // Invokes or reveals developer tools window for this resource.
105 virtual void Inspect() const {}
106
[email protected]7ed0e8c2012-07-19 23:17:06107 // A helper function for ActivateProcess when selected resource refers
108 // to a Tab or other window containing web contents. Returns NULL by
109 // default because not all resources have an associated web contents.
[email protected]23827ec2012-08-10 22:08:08110 virtual content::WebContents* GetWebContents() const;
[email protected]b7937d5b2008-09-10 22:12:19111
initial.commit09911bf2008-07-26 23:55:29112 // Whether this resource does report the network usage accurately.
113 // This controls whether 0 or N/A is displayed when no bytes have been
114 // reported as being read. This is because some plugins do not report the
115 // bytes read and we don't want to display a misleading 0 value in that
116 // case.
117 virtual bool SupportNetworkUsage() const = 0;
118
119 // Called when some bytes have been read and support_network_usage returns
[email protected]bfac5ac2008-10-09 17:55:38120 // false (meaning we do have network usage support).
initial.commit09911bf2008-07-26 23:55:29121 virtual void SetSupportNetworkUsage() = 0;
[email protected]ec9773682009-09-25 17:59:03122
[email protected]d277d432009-10-13 17:27:01123 // The TaskManagerModel periodically refreshes its data and call this
124 // on all live resources.
125 virtual void Refresh() {}
126
[email protected]ec9773682009-09-25 17:59:03127 virtual void NotifyResourceTypeStats(
128 const WebKit::WebCache::ResourceTypeStats& stats) {}
[email protected]872ae5b2011-05-26 20:20:50129 virtual void NotifyFPS(float fps) {}
[email protected]38b48a82009-11-11 01:51:32130 virtual void NotifyV8HeapStats(size_t v8_memory_allocated,
131 size_t v8_memory_used) {}
[email protected]c43207e2010-11-15 18:09:52132
133 // Returns true if this resource is not visible to the user because it lives
134 // in the background (e.g. extension background page, background contents).
[email protected]23827ec2012-08-10 22:08:08135 virtual bool IsBackground() const;
[email protected]b06b9502011-10-26 10:55:44136
137 static const char* GetResourceTypeAsString(const Type type) {
138 switch (type) {
139 TASKMANAGER_RESOURCE_TYPE_LIST(TASKMANAGER_RESOURCE_TYPE_LIST_AS_STRING)
140 default: return "UNKNOWN";
141 }
[email protected]ef72ae02011-10-28 16:28:59142 }
143
144 // Returns resource identifier that is unique within single task manager
145 // session (between StartUpdating and StopUpdating).
146 int get_unique_id() { return unique_id_; }
[email protected]abd81a522012-03-02 16:12:45147
[email protected]ef72ae02011-10-28 16:28:59148 protected:
149 Resource() : unique_id_(0) {}
150
151 private:
152 friend class TaskManagerModel;
153 int unique_id_;
initial.commit09911bf2008-07-26 23:55:29154 };
155
156 // ResourceProviders are responsible for adding/removing resources to the task
157 // manager. The task manager notifies the ResourceProvider that it is ready
158 // to receive resource creation/termination notifications with a call to
159 // StartUpdating(). At that point, the resource provider should call
160 // AddResource with all the existing resources, and after that it should call
161 // AddResource/RemoveResource as resources are created/terminated.
162 // The provider remains the owner of the resource objects and is responsible
163 // for deleting them (when StopUpdating() is called).
164 // After StopUpdating() is called the provider should also stop reporting
165 // notifications to the task manager.
166 // Note: ResourceProviders have to be ref counted as they are used in
167 // MessageLoop::InvokeLater().
[email protected]4090a282009-09-29 22:20:49168 class ResourceProvider : public base::RefCountedThreadSafe<ResourceProvider> {
initial.commit09911bf2008-07-26 23:55:29169 public:
170 // Should return the resource associated to the specified ids, or NULL if
171 // the resource does not belong to this provider.
172 virtual TaskManager::Resource* GetResource(int process_id,
173 int render_process_host_id,
174 int routing_id) = 0;
175 virtual void StartUpdating() = 0;
176 virtual void StopUpdating() = 0;
[email protected]e6e6ba42009-11-07 01:56:19177
178 protected:
179 friend class base::RefCountedThreadSafe<ResourceProvider>;
180
181 virtual ~ResourceProvider() {}
initial.commit09911bf2008-07-26 23:55:29182 };
183
184 static void RegisterPrefs(PrefService* prefs);
185
[email protected]8f90afd72009-06-22 22:44:38186 // Returns true if the process at the specified index is the browser process.
187 bool IsBrowserProcess(int index) const;
initial.commit09911bf2008-07-26 23:55:29188
[email protected]8f90afd72009-06-22 22:44:38189 // Terminates the process at the specified index.
190 void KillProcess(int index);
initial.commit09911bf2008-07-26 23:55:29191
[email protected]8f90afd72009-06-22 22:44:38192 // Activates the browser tab associated with the process in the specified
193 // index.
194 void ActivateProcess(int index);
[email protected]b7937d5b2008-09-10 22:12:19195
initial.commit09911bf2008-07-26 23:55:29196 // These methods are invoked by the resource providers to add/remove resources
197 // to the Task Manager. Note that the resources are owned by the
198 // ResourceProviders and are not valid after StopUpdating() has been called
199 // on the ResourceProviders.
200 void AddResource(Resource* resource);
201 void RemoveResource(Resource* resource);
202
[email protected]9c5e4172009-05-27 08:46:28203 void OnWindowClosed();
204
[email protected]c43207e2010-11-15 18:09:52205 // Invoked when a change to a resource has occurred that should cause any
206 // observers to completely refresh themselves (for example, the creation of
207 // a background resource in a process). Results in all observers receiving
208 // OnModelChanged() events.
209 void ModelChanged();
210
[email protected]8f90afd72009-06-22 22:44:38211 // Returns the singleton instance (and initializes it if necessary).
212 static TaskManager* GetInstance();
213
214 TaskManagerModel* model() const { return model_.get(); }
215
[email protected]8f0d1a22009-10-08 19:55:14216 void OpenAboutMemory();
217
[email protected]abcc5a1c2011-05-04 23:23:23218 // Returns the number of background pages that will be displayed in the
219 // TaskManager. Used by the wrench menu code to display a count of background
220 // pages in the "View Background Pages" menu item.
221 static int GetBackgroundPageCount();
222
initial.commit09911bf2008-07-26 23:55:29223 private:
[email protected]a918f872010-06-01 14:30:51224 FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, Basic);
225 FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, Resources);
226 FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, RefreshCalled);
[email protected]5ec892c2010-07-29 20:19:49227 FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest, Init);
228 FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest, Sort);
[email protected]c1b7d8cca2010-08-07 23:07:58229 FRIEND_TEST_ALL_PREFIXES(TaskManagerWindowControllerTest,
230 SelectionAdaptsToSorting);
[email protected]1222cce2009-04-27 06:58:27231
initial.commit09911bf2008-07-26 23:55:29232 // Obtain an instance via GetInstance().
233 TaskManager();
[email protected]5d438dbad2009-04-30 08:59:39234 friend struct DefaultSingletonTraits<TaskManager>;
initial.commit09911bf2008-07-26 23:55:29235
236 ~TaskManager();
237
[email protected]0860b9b92009-05-11 18:49:18238 // The model used for gathering and processing task data. It is ref counted
239 // because it is passed as a parameter to MessageLoop::InvokeLater().
240 scoped_refptr<TaskManagerModel> model_;
initial.commit09911bf2008-07-26 23:55:29241
[email protected]bfac5ac2008-10-09 17:55:38242 DISALLOW_COPY_AND_ASSIGN(TaskManager);
initial.commit09911bf2008-07-26 23:55:29243};
244
[email protected]b06b9502011-10-26 10:55:44245#undef TASKMANAGER_RESOURCE_TYPE_LIST
246#undef DEFINE_ENUM
247#undef DEFINE_CONVERT_TO_STRING
248
249
[email protected]0860b9b92009-05-11 18:49:18250class TaskManagerModelObserver {
initial.commit09911bf2008-07-26 23:55:29251 public:
[email protected]0860b9b92009-05-11 18:49:18252 virtual ~TaskManagerModelObserver() {}
initial.commit09911bf2008-07-26 23:55:29253
[email protected]0860b9b92009-05-11 18:49:18254 // Invoked when the model has been completely changed.
255 virtual void OnModelChanged() = 0;
initial.commit09911bf2008-07-26 23:55:29256
[email protected]0860b9b92009-05-11 18:49:18257 // Invoked when a range of items has changed.
258 virtual void OnItemsChanged(int start, int length) = 0;
259
260 // Invoked when new items are added.
261 virtual void OnItemsAdded(int start, int length) = 0;
262
263 // Invoked when a range of items has been removed.
264 virtual void OnItemsRemoved(int start, int length) = 0;
[email protected]82916812012-02-21 04:14:17265
[email protected]350584cd2012-05-17 18:18:26266 // Invoked when a range of items is to be immediately removed. It differs
267 // from OnItemsRemoved by the fact that the item is still in the task manager,
268 // so it can be queried for and found.
269 virtual void OnItemsToBeRemoved(int start, int length) {}
270
[email protected]82916812012-02-21 04:14:17271 // Invoked when the initialization of the model has been finished and
272 // periodical updates is started. The first periodical update will be done
273 // in a few seconds. (depending on platform)
274 virtual void OnReadyPeriodicalUpdate() {}
[email protected]0860b9b92009-05-11 18:49:18275};
276
277// The model that the TaskManager is using.
[email protected]8523ba52011-05-22 19:00:58278class TaskManagerModel : public base::RefCountedThreadSafe<TaskManagerModel> {
[email protected]0860b9b92009-05-11 18:49:18279 public:
[email protected]abd81a522012-03-02 16:12:45280 // (start, length)
281 typedef std::pair<int, int> GroupRange;
282
[email protected]0860b9b92009-05-11 18:49:18283 explicit TaskManagerModel(TaskManager* task_manager);
[email protected]0860b9b92009-05-11 18:49:18284
[email protected]a754453c2009-07-15 20:32:51285 void AddObserver(TaskManagerModelObserver* observer);
286 void RemoveObserver(TaskManagerModelObserver* observer);
[email protected]0860b9b92009-05-11 18:49:18287
288 // Returns number of registered resources.
289 int ResourceCount() const;
[email protected]9f5d3e182011-07-11 06:22:29290 // Returns number of registered groups.
291 int GroupCount() const;
[email protected]0860b9b92009-05-11 18:49:18292
[email protected]8a661f82010-10-19 21:47:11293 // Methods to return raw resource information.
294 int64 GetNetworkUsage(int index) const;
295 double GetCPUUsage(int index) const;
296 int GetProcessId(int index) const;
[email protected]350584cd2012-05-17 18:18:26297 base::ProcessHandle GetProcess(int index) const;
[email protected]ef72ae02011-10-28 16:28:59298 int GetResourceUniqueId(int index) const;
[email protected]28989c32011-12-14 19:41:36299 // Returns the index of resource that has the given |unique_id|. Returns -1 if
300 // no resouce has the |unique_id|.
301 int GetResourceIndexByUniqueId(const int unique_id) const;
[email protected]8a661f82010-10-19 21:47:11302
[email protected]0860b9b92009-05-11 18:49:18303 // Methods to return formatted resource information.
[email protected]5f2b0ee2010-08-14 14:53:43304 string16 GetResourceTitle(int index) const;
[email protected]c30a6042011-09-02 07:21:26305 string16 GetResourceProfileName(int index) const;
[email protected]5f2b0ee2010-08-14 14:53:43306 string16 GetResourceNetworkUsage(int index) const;
307 string16 GetResourceCPUUsage(int index) const;
308 string16 GetResourcePrivateMemory(int index) const;
309 string16 GetResourceSharedMemory(int index) const;
310 string16 GetResourcePhysicalMemory(int index) const;
311 string16 GetResourceProcessId(int index) const;
312 string16 GetResourceWebCoreImageCacheSize(int index) const;
313 string16 GetResourceWebCoreScriptsCacheSize(int index) const;
314 string16 GetResourceWebCoreCSSCacheSize(int index) const;
[email protected]872ae5b2011-05-26 20:20:50315 string16 GetResourceFPS(int index) const;
[email protected]5f2b0ee2010-08-14 14:53:43316 string16 GetResourceSqliteMemoryUsed(int index) const;
317 string16 GetResourceGoatsTeleported(int index) const;
318 string16 GetResourceV8MemoryAllocatedSize(int index) const;
[email protected]0860b9b92009-05-11 18:49:18319
[email protected]8a661f82010-10-19 21:47:11320 // Gets the private memory (in bytes) that should be displayed for the passed
321 // resource index. Caches the result since this calculation can take time on
322 // some platforms.
323 bool GetPrivateMemory(int index, size_t* result) const;
324
325 // Gets the shared memory (in bytes) that should be displayed for the passed
326 // resource index. Caches the result since this calculation can take time on
327 // some platforms.
328 bool GetSharedMemory(int index, size_t* result) const;
329
330 // Gets the physical memory (in bytes) that should be displayed for the passed
331 // resource index.
332 bool GetPhysicalMemory(int index, size_t* result) const;
333
[email protected]5d8c0b92011-07-21 07:53:32334 // Gets the statuses of webkit. Return false if the resource for the given row
335 // isn't a renderer.
336 bool GetWebCoreCacheStats(int index,
337 WebKit::WebCache::ResourceTypeStats* result) const;
338
339 // Gets the fps of the given page. Return false if the resource for the given
340 // row isn't a renderer.
341 bool GetFPS(int index, float* result) const;
342
343 // Gets the sqlite memory (in byte). Return false if the resource for the
344 // given row doesn't report information.
345 bool GetSqliteMemoryUsedBytes(int index, size_t* result) const;
346
[email protected]8a661f82010-10-19 21:47:11347 // Gets the amount of memory allocated for javascript. Returns false if the
348 // resource for the given row isn't a renderer.
349 bool GetV8Memory(int index, size_t* result) const;
350
[email protected]350584cd2012-05-17 18:18:26351 // Gets the amount of memory used for javascript. Returns false if the
352 // resource for the given row isn't a renderer.
353 bool GetV8MemoryUsed(int index, size_t* result) const;
354
[email protected]374bf592011-11-10 15:42:18355 // Returns true if resource for the given row can be activated.
356 bool CanActivate(int index) const;
357
[email protected]0f62bb2d2011-10-26 14:48:33358 // Returns true if resource for the given row can be inspected using developer
359 // tools.
360 bool CanInspect(int index) const;
361
362 // Invokes or reveals developer tools window for resource in the given row.
363 void Inspect(int index) const;
364
[email protected]8a661f82010-10-19 21:47:11365 // See design doc at http://go/at-teleporter for more information.
366 int GetGoatsTeleported(int index) const;
367
[email protected]9f5d3e182011-07-11 06:22:29368 // Returns true if the resource is first/last in its group (resources
[email protected]0860b9b92009-05-11 18:49:18369 // rendered by the same process are groupped together).
370 bool IsResourceFirstInGroup(int index) const;
[email protected]9f5d3e182011-07-11 06:22:29371 bool IsResourceLastInGroup(int index) const;
[email protected]0860b9b92009-05-11 18:49:18372
[email protected]c43207e2010-11-15 18:09:52373 // Returns true if the resource runs in the background (not visible to the
374 // user, e.g. extension background pages and BackgroundContents).
375 bool IsBackgroundResource(int index) const;
376
[email protected]0860b9b92009-05-11 18:49:18377 // Returns icon to be used for resource (for example a favicon).
[email protected]6280b5882012-06-05 21:56:41378 gfx::ImageSkia GetResourceIcon(int index) const;
[email protected]0860b9b92009-05-11 18:49:18379
[email protected]abd81a522012-03-02 16:12:45380 // Returns the group range of resource.
381 GroupRange GetGroupRangeForResource(int index) const;
[email protected]0860b9b92009-05-11 18:49:18382
[email protected]9f5d3e182011-07-11 06:22:29383 // Returns an index of groups to which the resource belongs.
384 int GetGroupIndexForResource(int index) const;
385
386 // Returns an index of resource which belongs to the |group_index|th group
387 // and which is the |index_in_group|th resource in group.
388 int GetResourceIndexForGroup(int group_index, int index_in_group) const;
389
[email protected]0860b9b92009-05-11 18:49:18390 // Compares values in column |col_id| and rows |row1|, |row2|.
391 // Returns -1 if value in |row1| is less than value in |row2|,
392 // 0 if they are equal, and 1 otherwise.
393 int CompareValues(int row1, int row2, int col_id) const;
394
395 // Returns process handle for given resource.
396 base::ProcessHandle GetResourceProcessHandle(int index) const;
397
[email protected]6bfb6e702012-04-05 17:18:22398 // Returns the unique child process ID generated by Chromium, not the OS
399 // process id. This is used to identify processes internally and for
400 // extensions. It is not meant to be displayed to the user.
401 int GetUniqueChildProcessId(int index) const;
402
[email protected]8a661f82010-10-19 21:47:11403 // Returns the type of the given resource.
404 TaskManager::Resource::Type GetResourceType(int index) const;
405
[email protected]7ed0e8c2012-07-19 23:17:06406 // Returns WebContents of given resource or NULL if not applicable.
407 content::WebContents* GetResourceWebContents(int index) const;
initial.commit09911bf2008-07-26 23:55:29408
[email protected]e1725842009-10-20 06:40:15409 // Returns Extension of given resource or NULL if not applicable.
[email protected]1c321ee2012-05-21 03:02:34410 const extensions::Extension* GetResourceExtension(int index) const;
[email protected]e1725842009-10-20 06:40:15411
initial.commit09911bf2008-07-26 23:55:29412 void AddResource(TaskManager::Resource* resource);
413 void RemoveResource(TaskManager::Resource* resource);
414
[email protected]0860b9b92009-05-11 18:49:18415 void StartUpdating();
416 void StopUpdating();
initial.commit09911bf2008-07-26 23:55:29417
[email protected]350584cd2012-05-17 18:18:26418 // Listening involves calling StartUpdating on all resource providers. This
419 // causes all of them to subscribe to notifications and enumerate current
420 // resources. It differs from StartUpdating that it doesn't start the
421 // Refresh timer. The end result is that we have a full view of resources, but
422 // don't spend unneeded time updating, unless we have a real need to.
423 void StartListening();
424 void StopListening();
425
[email protected]0860b9b92009-05-11 18:49:18426 void Clear(); // Removes all items.
427
[email protected]c43207e2010-11-15 18:09:52428 // Sends OnModelChanged() to all observers to inform them of significant
429 // changes to the model.
430 void ModelChanged();
431
[email protected]ec9773682009-09-25 17:59:03432 void NotifyResourceTypeStats(
[email protected]38b48a82009-11-11 01:51:32433 base::ProcessId renderer_id,
[email protected]ec9773682009-09-25 17:59:03434 const WebKit::WebCache::ResourceTypeStats& stats);
435
[email protected]872ae5b2011-05-26 20:20:50436 void NotifyFPS(base::ProcessId renderer_id,
437 int routing_id,
438 float fps);
439
[email protected]38b48a82009-11-11 01:51:32440 void NotifyV8HeapStats(base::ProcessId renderer_id,
441 size_t v8_memory_allocated,
442 size_t v8_memory_used);
443
[email protected]8523ba52011-05-22 19:00:58444 void NotifyBytesRead(const net::URLRequest& request, int bytes_read);
445
[email protected]0860b9b92009-05-11 18:49:18446 private:
[email protected]e6e6ba42009-11-07 01:56:19447 friend class base::RefCountedThreadSafe<TaskManagerModel>;
[email protected]a918f872010-06-01 14:30:51448 FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, RefreshCalled);
[email protected]8a661f82010-10-19 21:47:11449 FRIEND_TEST_ALL_PREFIXES(ExtensionApiTest, ProcessesVsTaskManager);
[email protected]d277d432009-10-13 17:27:01450
[email protected]e6e6ba42009-11-07 01:56:19451 ~TaskManagerModel();
452
[email protected]bfac5ac2008-10-09 17:55:38453 enum UpdateState {
454 IDLE = 0, // Currently not updating.
455 TASK_PENDING, // An update task is pending.
456 STOPPING // A update task is pending and it should stop the update.
457 };
458
initial.commit09911bf2008-07-26 23:55:29459 // This struct is used to exchange information between the io and ui threads.
460 struct BytesReadParam {
[email protected]cdf682e2011-02-05 05:11:28461 BytesReadParam(int origin_pid,
[email protected]76543b92009-08-31 17:27:45462 int render_process_host_child_id,
463 int routing_id,
464 int byte_count)
[email protected]cdf682e2011-02-05 05:11:28465 : origin_pid(origin_pid),
[email protected]76543b92009-08-31 17:27:45466 render_process_host_child_id(render_process_host_child_id),
[email protected]bfac5ac2008-10-09 17:55:38467 routing_id(routing_id),
[email protected]76543b92009-08-31 17:27:45468 byte_count(byte_count) {}
initial.commit09911bf2008-07-26 23:55:29469
[email protected]cdf682e2011-02-05 05:11:28470 // The process ID that triggered the request. For plugin requests this
471 // will differ from the renderer process ID.
472 int origin_pid;
[email protected]76543b92009-08-31 17:27:45473
[email protected]cdf682e2011-02-05 05:11:28474 // The child ID of the RenderProcessHost this request was routed through.
[email protected]76543b92009-08-31 17:27:45475 int render_process_host_child_id;
476
initial.commit09911bf2008-07-26 23:55:29477 int routing_id;
478 int byte_count;
479 };
480
initial.commit09911bf2008-07-26 23:55:29481 typedef std::vector<TaskManager::Resource*> ResourceList;
[email protected]8d4326b2011-11-02 11:53:10482 typedef std::vector<scoped_refptr<TaskManager::ResourceProvider> >
483 ResourceProviderList;
[email protected]0860b9b92009-05-11 18:49:18484 typedef std::map<base::ProcessHandle, ResourceList*> GroupMap;
485 typedef std::map<base::ProcessHandle, base::ProcessMetrics*> MetricsMap;
[email protected]022eab62010-01-13 04:55:06486 typedef std::map<base::ProcessHandle, double> CPUUsageMap;
[email protected]0860b9b92009-05-11 18:49:18487 typedef std::map<TaskManager::Resource*, int64> ResourceValueMap;
[email protected]abd81a522012-03-02 16:12:45488 // Private memory in bytes, shared memory in bytes.
489 typedef std::pair<size_t, size_t> MemoryUsageEntry;
490 typedef std::map<base::ProcessHandle, MemoryUsageEntry> MemoryUsageMap;
initial.commit09911bf2008-07-26 23:55:29491
[email protected]bfac5ac2008-10-09 17:55:38492 // Updates the values for all rows.
493 void Refresh();
494
initial.commit09911bf2008-07-26 23:55:29495 void AddItem(TaskManager::Resource* resource, bool notify_table);
496 void RemoveItem(TaskManager::Resource* resource);
497
initial.commit09911bf2008-07-26 23:55:29498 // Returns the network usage (in bytes per seconds) for the specified
499 // resource. That's the value retrieved at the last timer's tick.
[email protected]d043c2cc2009-03-25 18:30:45500 int64 GetNetworkUsageForResource(TaskManager::Resource* resource) const;
initial.commit09911bf2008-07-26 23:55:29501
initial.commit09911bf2008-07-26 23:55:29502 // Called on the UI thread when some bytes are read.
503 void BytesRead(BytesReadParam param);
504
[email protected]bfac5ac2008-10-09 17:55:38505 // Returns the network usage (in byte per second) that should be displayed for
506 // the passed |resource|. -1 means the information is not available for that
507 // resource.
[email protected]d043c2cc2009-03-25 18:30:45508 int64 GetNetworkUsage(TaskManager::Resource* resource) const;
[email protected]bfac5ac2008-10-09 17:55:38509
510 // Returns the CPU usage (in %) that should be displayed for the passed
511 // |resource|.
[email protected]022eab62010-01-13 04:55:06512 double GetCPUUsage(TaskManager::Resource* resource) const;
[email protected]bfac5ac2008-10-09 17:55:38513
[email protected]99e9d792009-10-27 01:30:57514 // Retrieves the ProcessMetrics for the resources at the specified row.
515 // Returns true if there was a ProcessMetrics available.
516 bool GetProcessMetricsForRow(int row,
517 base::ProcessMetrics** proc_metrics) const;
[email protected]bfac5ac2008-10-09 17:55:38518
[email protected]d0767cb542009-10-08 17:38:30519 // Given a number, this function returns the formatted string that should be
520 // displayed in the task manager's memory cell.
[email protected]5f2b0ee2010-08-14 14:53:43521 string16 GetMemCellText(int64 number) const;
[email protected]2a5af8a2009-01-23 23:13:05522
[email protected]98947a02010-05-11 17:46:08523 // Looks up the data for |handle| and puts it in the mutable cache
524 // |memory_usage_map_|.
525 bool GetAndCacheMemoryMetrics(base::ProcessHandle handle,
[email protected]abd81a522012-03-02 16:12:45526 MemoryUsageEntry* usage) const;
[email protected]98947a02010-05-11 17:46:08527
[email protected]0f4ffe842011-03-30 18:59:46528 // Adds a resource provider to be managed.
529 void AddResourceProvider(TaskManager::ResourceProvider* provider);
530
initial.commit09911bf2008-07-26 23:55:29531 // The list of providers to the task manager. They are ref counted.
532 ResourceProviderList providers_;
533
534 // The list of all the resources displayed in the task manager. They are owned
535 // by the ResourceProviders.
536 ResourceList resources_;
537
538 // A map to keep tracks of the grouped resources (they are grouped if they
539 // share the same process). The groups (the Resources vectors) are owned by
540 // the model (but the actual Resources are owned by the ResourceProviders).
541 GroupMap group_map_;
542
543 // A map to retrieve the process metrics for a process. The ProcessMetrics are
544 // owned by the model.
545 MetricsMap metrics_map_;
546
547 // A map that keeps track of the number of bytes read per process since last
548 // tick. The Resources are owned by the ResourceProviders.
549 ResourceValueMap current_byte_count_map_;
550
551 // A map that contains the network usage is displayed in the table, in bytes
552 // per second. It is computed every time the timer ticks. The Resources are
553 // owned by the ResourceProviders.
554 ResourceValueMap displayed_network_usage_map_;
555
[email protected]bfac5ac2008-10-09 17:55:38556 // A map that contains the CPU usage (in %) for a process since last refresh.
557 CPUUsageMap cpu_usage_map_;
initial.commit09911bf2008-07-26 23:55:29558
[email protected]98947a02010-05-11 17:46:08559 // A map that contains the private/shared memory usage of the process. We
560 // cache this because the same APIs are called on linux and windows, and
561 // because the linux call takes >10ms to complete. This cache is cleared on
562 // every Refresh().
563 mutable MemoryUsageMap memory_usage_map_;
564
[email protected]a754453c2009-07-15 20:32:51565 ObserverList<TaskManagerModelObserver> observer_list_;
initial.commit09911bf2008-07-26 23:55:29566
[email protected]8a661f82010-10-19 21:47:11567 // How many calls to StartUpdating have been made without matching calls to
568 // StopUpdating.
569 int update_requests_;
570
[email protected]350584cd2012-05-17 18:18:26571 // How many calls to StartListening have been made without matching calls to
572 // StopListening.
573 int listen_requests_;
574
[email protected]bfac5ac2008-10-09 17:55:38575 // Whether we are currently in the process of updating.
576 UpdateState update_state_;
577
[email protected]96373d02010-04-21 01:16:14578 // A salt lick for the goats.
[email protected]abd81a522012-03-02 16:12:45579 uint64 goat_salt_;
[email protected]96373d02010-04-21 01:16:14580
[email protected]ef72ae02011-10-28 16:28:59581 // Resource identifier that is unique within single session.
582 int last_unique_id_;
583
[email protected]0860b9b92009-05-11 18:49:18584 DISALLOW_COPY_AND_ASSIGN(TaskManagerModel);
initial.commit09911bf2008-07-26 23:55:29585};
586
[email protected]a5d1e1e2010-09-23 19:34:12587#endif // CHROME_BROWSER_TASK_MANAGER_TASK_MANAGER_H_