1// Copyright 2013 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.
4
5#ifndef CHROME_BROWSER_TASK_MANAGER_RESOURCE_PROVIDER_H_
6#define CHROME_BROWSER_TASK_MANAGER_RESOURCE_PROVIDER_H_
7
8#include "base/basictypes.h"
9#include "base/memory/ref_counted.h"
10#include "base/process/process_handle.h"
11#include "base/strings/string16.h"
12#include "third_party/WebKit/public/web/WebCache.h"
13
14class PrefRegistrySimple;
15class TaskManagerModel;
16
17namespace content {
18class WebContents;
19}
20
21namespace extensions {
22class Extension;
23}
24
25namespace gfx {
26class ImageSkia;
27}
28
29namespace task_manager {
30
31#define TASKMANAGER_RESOURCE_TYPE_LIST(def) \
32    def(BROWSER)         /* The main browser process. */ \
33    def(RENDERER)        /* A normal WebContents renderer process. */ \
34    def(EXTENSION)       /* An extension or app process. */ \
35    def(NOTIFICATION)    /* A notification process. */ \
36    def(GUEST)           /* A browser plugin guest process. */ \
37    def(PLUGIN)          /* A plugin process. */ \
38    def(WORKER)          /* A web worker process. */ \
39    def(NACL)            /* A NativeClient loader or broker process. */ \
40    def(UTILITY)         /* A browser utility process. */ \
41    def(ZYGOTE)          /* A Linux zygote process. */ \
42    def(SANDBOX_HELPER)  /* A sandbox helper process. */ \
43    def(GPU)             /* A graphics process. */
44
45#define TASKMANAGER_RESOURCE_TYPE_LIST_ENUM(a)   a,
46#define TASKMANAGER_RESOURCE_TYPE_LIST_AS_STRING(a)   case a: return #a;
47
48// A resource represents one row in the task manager.
49// Resources from similar processes are grouped together by the task manager.
50class Resource {
51 public:
52  virtual ~Resource() {}
53
54  enum Type {
55    UNKNOWN = 0,
56    TASKMANAGER_RESOURCE_TYPE_LIST(TASKMANAGER_RESOURCE_TYPE_LIST_ENUM)
57  };
58
59  virtual base::string16 GetTitle() const = 0;
60  virtual base::string16 GetProfileName() const = 0;
61  virtual gfx::ImageSkia GetIcon() const = 0;
62  virtual base::ProcessHandle GetProcess() const = 0;
63  virtual int GetUniqueChildProcessId() const = 0;
64  virtual Type GetType() const = 0;
65  virtual int GetRoutingID() const;
66
67  virtual bool ReportsCacheStats() const;
68  virtual blink::WebCache::ResourceTypeStats GetWebCoreCacheStats() const;
69
70  virtual bool ReportsSqliteMemoryUsed() const;
71  virtual size_t SqliteMemoryUsedBytes() const;
72
73  virtual bool ReportsV8MemoryStats() const;
74  virtual size_t GetV8MemoryAllocated() const;
75  virtual size_t GetV8MemoryUsed() const;
76
77  // A helper function for ActivateProcess when selected resource refers
78  // to a Tab or other window containing web contents.  Returns NULL by
79  // default because not all resources have an associated web contents.
80  virtual content::WebContents* GetWebContents() const;
81
82  // Whether this resource does report the network usage accurately.
83  // This controls whether 0 or N/A is displayed when no bytes have been
84  // reported as being read. This is because some plugins do not report the
85  // bytes read and we don't want to display a misleading 0 value in that
86  // case.
87  virtual bool SupportNetworkUsage() const = 0;
88
89  // Called when some bytes have been read and support_network_usage returns
90  // false (meaning we do have network usage support).
91  virtual void SetSupportNetworkUsage() = 0;
92
93  // The TaskManagerModel periodically refreshes its data and call this
94  // on all live resources.
95  virtual void Refresh() {}
96
97  virtual void NotifyResourceTypeStats(
98      const blink::WebCache::ResourceTypeStats& stats) {}
99  virtual void NotifyV8HeapStats(size_t v8_memory_allocated,
100                                 size_t v8_memory_used) {}
101
102  static const char* GetResourceTypeAsString(const Type type) {
103    switch (type) {
104      TASKMANAGER_RESOURCE_TYPE_LIST(TASKMANAGER_RESOURCE_TYPE_LIST_AS_STRING)
105      default: return "UNKNOWN";
106    }
107  }
108
109 protected:
110  Resource() {}
111
112 private:
113  DISALLOW_COPY_AND_ASSIGN(Resource);
114};
115
116#undef TASKMANAGER_RESOURCE_TYPE_LIST
117#undef TASKMANAGER_RESOURCE_TYPE_LIST_ENUM
118#undef TASKMANAGER_RESOURCE_TYPE_LIST_AS_STRING
119
120// ResourceProviders are responsible for adding/removing resources to the task
121// manager. The task manager notifies the ResourceProvider that it is ready
122// to receive resource creation/termination notifications with a call to
123// StartUpdating(). At that point, the resource provider should call
124// AddResource with all the existing resources, and after that it should call
125// AddResource/RemoveResource as resources are created/terminated.
126// The provider remains the owner of the resource objects and is responsible
127// for deleting them (when StopUpdating() is called).
128// After StopUpdating() is called the provider should also stop reporting
129// notifications to the task manager.
130// Note: ResourceProviders have to be ref counted as they are used in
131// MessageLoop::InvokeLater().
132class ResourceProvider : public base::RefCountedThreadSafe<ResourceProvider> {
133 public:
134  // Should return the resource associated to the specified ids, or NULL if
135  // the resource does not belong to this provider.
136  virtual Resource* GetResource(int origin_pid,
137                                int child_id,
138                                int route_id) = 0;
139  virtual void StartUpdating() = 0;
140  virtual void StopUpdating() = 0;
141
142 protected:
143  friend class base::RefCountedThreadSafe<ResourceProvider>;
144
145  virtual ~ResourceProvider() {}
146};
147
148}  // namespace task_manager
149
150#endif  // CHROME_BROWSER_TASK_MANAGER_RESOURCE_PROVIDER_H_
151