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 14 class PrefRegistrySimple; 15 class TaskManagerModel; 16 17 namespace content { 18 class WebContents; 19 } 20 21 namespace extensions { 22 class Extension; 23 } 24 25 namespace gfx { 26 class ImageSkia; 27 } 28 29 namespace 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. 50 class 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 ReportsFPS() const; 71 virtual float GetFPS() const; 72 73 virtual bool ReportsSqliteMemoryUsed() const; 74 virtual size_t SqliteMemoryUsedBytes() const; 75 76 // Return extension associated with the resource, or NULL if not applicable. 77 virtual const extensions::Extension* GetExtension() const; 78 79 virtual bool ReportsV8MemoryStats() const; 80 virtual size_t GetV8MemoryAllocated() const; 81 virtual size_t GetV8MemoryUsed() const; 82 83 // Returns true if this resource can be inspected using developer tools. 84 virtual bool CanInspect() const; 85 86 // Invokes or reveals developer tools window for this resource. 87 virtual void Inspect() const {} 88 89 // A helper function for ActivateProcess when selected resource refers 90 // to a Tab or other window containing web contents. Returns NULL by 91 // default because not all resources have an associated web contents. 92 virtual content::WebContents* GetWebContents() const; 93 94 // Whether this resource does report the network usage accurately. 95 // This controls whether 0 or N/A is displayed when no bytes have been 96 // reported as being read. This is because some plugins do not report the 97 // bytes read and we don't want to display a misleading 0 value in that 98 // case. 99 virtual bool SupportNetworkUsage() const = 0; 100 101 // Called when some bytes have been read and support_network_usage returns 102 // false (meaning we do have network usage support). 103 virtual void SetSupportNetworkUsage() = 0; 104 105 // The TaskManagerModel periodically refreshes its data and call this 106 // on all live resources. 107 virtual void Refresh() {} 108 109 virtual void NotifyResourceTypeStats( 110 const blink::WebCache::ResourceTypeStats& stats) {} 111 virtual void NotifyFPS(float fps) {} 112 virtual void NotifyV8HeapStats(size_t v8_memory_allocated, 113 size_t v8_memory_used) {} 114 115 // Returns true if this resource is not visible to the user because it lives 116 // in the background (e.g. extension background page, background contents). 117 virtual bool IsBackground() const; 118 119 static const char* GetResourceTypeAsString(const Type type) { 120 switch (type) { 121 TASKMANAGER_RESOURCE_TYPE_LIST(TASKMANAGER_RESOURCE_TYPE_LIST_AS_STRING) 122 default: return "UNKNOWN"; 123 } 124 } 125 126 // Returns resource identifier that is unique within single task manager 127 // session (between StartUpdating and StopUpdating). 128 int get_unique_id() { return unique_id_; } 129 130 protected: 131 Resource() : unique_id_(0) {} 132 133 private: 134 friend class ::TaskManagerModel; 135 int unique_id_; 136 137 DISALLOW_COPY_AND_ASSIGN(Resource); 138 }; 139 140 #undef TASKMANAGER_RESOURCE_TYPE_LIST 141 #undef TASKMANAGER_RESOURCE_TYPE_LIST_ENUM 142 #undef TASKMANAGER_RESOURCE_TYPE_LIST_AS_STRING 143 144 // ResourceProviders are responsible for adding/removing resources to the task 145 // manager. The task manager notifies the ResourceProvider that it is ready 146 // to receive resource creation/termination notifications with a call to 147 // StartUpdating(). At that point, the resource provider should call 148 // AddResource with all the existing resources, and after that it should call 149 // AddResource/RemoveResource as resources are created/terminated. 150 // The provider remains the owner of the resource objects and is responsible 151 // for deleting them (when StopUpdating() is called). 152 // After StopUpdating() is called the provider should also stop reporting 153 // notifications to the task manager. 154 // Note: ResourceProviders have to be ref counted as they are used in 155 // MessageLoop::InvokeLater(). 156 class ResourceProvider : public base::RefCountedThreadSafe<ResourceProvider> { 157 public: 158 // Should return the resource associated to the specified ids, or NULL if 159 // the resource does not belong to this provider. 160 virtual Resource* GetResource(int process_id, 161 int render_process_host_id, 162 int routing_id) = 0; 163 virtual void StartUpdating() = 0; 164 virtual void StopUpdating() = 0; 165 166 protected: 167 friend class base::RefCountedThreadSafe<ResourceProvider>; 168 169 virtual ~ResourceProvider() {} 170 }; 171 172 } // namespace task_manager 173 174 #endif // CHROME_BROWSER_TASK_MANAGER_RESOURCE_PROVIDER_H_ 175