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