initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #ifndef CHROME_BROWSER_TASK_MANAGER_RESOURCE_PROVIDERS_H__ |
| 31 | #define CHROME_BROWSER_TASK_MANAGER_RESOURCE_PROVIDERS_H__ |
| 32 | |
[email protected] | b5d2066 | 2008-08-01 12:33:21 | [diff] [blame^] | 33 | #include "base/basictypes.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | #include "chrome/browser/plugin_process_info.h" |
| 35 | #include "chrome/browser/task_manager.h" |
[email protected] | b5d2066 | 2008-08-01 12:33:21 | [diff] [blame^] | 36 | #include "chrome/common/notification_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | |
| 38 | class PluginProcessHost; |
| 39 | class WebContents; |
| 40 | |
| 41 | // These file contains the resource providers used in the task manager. |
| 42 | |
| 43 | class TaskManagerWebContentsResource : public TaskManager::Resource { |
| 44 | public: |
| 45 | explicit TaskManagerWebContentsResource(WebContents* web_contents); |
| 46 | ~TaskManagerWebContentsResource(); |
| 47 | |
| 48 | // TaskManagerResource methods: |
| 49 | std::wstring GetTitle() const; |
| 50 | SkBitmap GetIcon() const; |
| 51 | HANDLE GetProcess() const; |
| 52 | // WebContents always provide the network usage. |
| 53 | bool SupportNetworkUsage() const { return true; } |
| 54 | void SetSupportNetworkUsage() { }; |
| 55 | |
| 56 | private: |
| 57 | WebContents* web_contents_; |
| 58 | HANDLE process_; |
| 59 | int pid_; |
| 60 | |
| 61 | DISALLOW_EVIL_CONSTRUCTORS(TaskManagerWebContentsResource); |
| 62 | }; |
| 63 | |
| 64 | class TaskManagerWebContentsResourceProvider |
| 65 | : public TaskManager::ResourceProvider, |
| 66 | public NotificationObserver { |
| 67 | public: |
| 68 | explicit TaskManagerWebContentsResourceProvider(TaskManager* task_manager); |
| 69 | virtual ~TaskManagerWebContentsResourceProvider(); |
| 70 | |
| 71 | virtual TaskManager::Resource* GetResource(int origin_pid, |
| 72 | int render_process_host_id, |
| 73 | int routing_id); |
| 74 | virtual void StartUpdating(); |
| 75 | virtual void StopUpdating(); |
| 76 | |
| 77 | // NotificationObserver method: |
| 78 | virtual void Observe(NotificationType type, |
| 79 | const NotificationSource& source, |
| 80 | const NotificationDetails& details); |
| 81 | |
| 82 | private: |
| 83 | void Add(WebContents* web_contents); |
| 84 | void Remove(WebContents* web_contents); |
| 85 | |
| 86 | void AddToTaskManager(WebContents* web_contents); |
| 87 | |
| 88 | // Whether we are currently reporting to the task manager. Used to ignore |
| 89 | // notifications sent after StopUpdating(). |
| 90 | bool updating_; |
| 91 | |
| 92 | TaskManager* task_manager_; |
| 93 | |
| 94 | // Maps the actual resources (the WebContents) to the Task Manager |
| 95 | // resources. |
| 96 | std::map<WebContents*, TaskManagerWebContentsResource*> resources_; |
| 97 | |
| 98 | DISALLOW_EVIL_CONSTRUCTORS(TaskManagerWebContentsResourceProvider); |
| 99 | }; |
| 100 | |
| 101 | class TaskManagerPluginProcessResource : public TaskManager::Resource { |
| 102 | public: |
| 103 | explicit TaskManagerPluginProcessResource(PluginProcessInfo plugin_proc); |
| 104 | ~TaskManagerPluginProcessResource(); |
| 105 | |
| 106 | // TaskManagerResource methods: |
| 107 | std::wstring GetTitle() const; |
| 108 | SkBitmap GetIcon() const; |
| 109 | HANDLE GetProcess() const; |
| 110 | |
| 111 | bool SupportNetworkUsage() const { |
| 112 | return network_usage_support_; |
| 113 | } |
| 114 | |
| 115 | void SetSupportNetworkUsage() { |
| 116 | network_usage_support_ = true; |
| 117 | } |
| 118 | |
| 119 | // Returns the pid of the plugin process. |
| 120 | int process_id() const { return pid_; } |
| 121 | |
| 122 | private: |
| 123 | PluginProcessInfo plugin_process_; |
| 124 | int pid_; |
| 125 | mutable std::wstring title_; |
| 126 | bool network_usage_support_; |
| 127 | |
| 128 | // The icon painted for plugins. |
| 129 | // TODO (jcampan): we should have plugin specific icons for well-known |
| 130 | // plugins. |
| 131 | static SkBitmap* default_icon_; |
| 132 | |
| 133 | DISALLOW_EVIL_CONSTRUCTORS(TaskManagerPluginProcessResource); |
| 134 | }; |
| 135 | |
| 136 | class TaskManagerPluginProcessResourceProvider |
| 137 | : public TaskManager::ResourceProvider, |
| 138 | public NotificationObserver { |
| 139 | public: |
| 140 | explicit TaskManagerPluginProcessResourceProvider(TaskManager* task_manager); |
| 141 | virtual ~TaskManagerPluginProcessResourceProvider(); |
| 142 | |
| 143 | virtual TaskManager::Resource* GetResource(int origin_pid, |
| 144 | int render_process_host_id, |
| 145 | int routing_id); |
| 146 | virtual void StartUpdating(); |
| 147 | virtual void StopUpdating(); |
| 148 | |
| 149 | // NotificationObserver method: |
| 150 | virtual void Observe(NotificationType type, |
| 151 | const NotificationSource& source, |
| 152 | const NotificationDetails& details); |
| 153 | |
| 154 | // Retrieves the current PluginProcessInfo (performed in the IO thread). |
| 155 | virtual void RetrievePluginProcessInfo(); |
| 156 | |
| 157 | // Notifies the UI thread that the PluginProcessInfo have been retrieved. |
| 158 | virtual void PluginProcessInfoRetreived(); |
| 159 | |
| 160 | // Whether we are currently reporting to the task manager. Used to ignore |
| 161 | // notifications sent after StopUpdating(). |
| 162 | bool updating_; |
| 163 | |
| 164 | // The list of PluginProcessInfo retrieved when starting the update. |
| 165 | std::vector<PluginProcessInfo> existing_plugin_process_info; |
| 166 | |
| 167 | private: |
| 168 | void Add(PluginProcessInfo plugin_process_info); |
| 169 | void Remove(PluginProcessInfo plugin_process_info); |
| 170 | |
| 171 | void AddToTaskManager(PluginProcessInfo plugin_process_info); |
| 172 | |
| 173 | TaskManager* task_manager_; |
| 174 | |
| 175 | MessageLoop* ui_loop_; |
| 176 | |
| 177 | // Maps the actual resources (the PluginProcessInfo) to the Task Manager |
| 178 | // resources. |
| 179 | std::map<PluginProcessInfo, TaskManagerPluginProcessResource*> resources_; |
| 180 | |
| 181 | // Maps the pids to the resources (used for quick access to the resource on |
| 182 | // byte read notifications). |
| 183 | std::map<int, TaskManagerPluginProcessResource*> pid_to_resources_; |
| 184 | |
| 185 | DISALLOW_EVIL_CONSTRUCTORS(TaskManagerPluginProcessResourceProvider); |
| 186 | }; |
| 187 | |
| 188 | class TaskManagerBrowserProcessResource : public TaskManager::Resource { |
| 189 | public: |
| 190 | TaskManagerBrowserProcessResource(); |
| 191 | ~TaskManagerBrowserProcessResource(); |
| 192 | |
| 193 | // TaskManagerResource methods: |
| 194 | std::wstring GetTitle() const; |
| 195 | SkBitmap GetIcon() const; |
| 196 | HANDLE GetProcess() const; |
| 197 | |
| 198 | bool SupportNetworkUsage() const { |
| 199 | return network_usage_support_; |
| 200 | } |
| 201 | |
| 202 | void SetSupportNetworkUsage() { |
| 203 | network_usage_support_ = true; |
| 204 | } |
| 205 | |
| 206 | // Returns the pid of the browser process. |
| 207 | int process_id() const { return pid_; } |
| 208 | |
| 209 | private: |
| 210 | HANDLE process_; |
| 211 | int pid_; |
| 212 | bool network_usage_support_; |
| 213 | mutable std::wstring title_; |
| 214 | |
| 215 | static SkBitmap* default_icon_; |
| 216 | |
| 217 | DISALLOW_EVIL_CONSTRUCTORS(TaskManagerBrowserProcessResource); |
| 218 | }; |
| 219 | |
| 220 | class TaskManagerBrowserProcessResourceProvider |
| 221 | : public TaskManager::ResourceProvider { |
| 222 | public: |
| 223 | explicit TaskManagerBrowserProcessResourceProvider( |
| 224 | TaskManager* task_manager); |
| 225 | virtual ~TaskManagerBrowserProcessResourceProvider(); |
| 226 | |
| 227 | virtual TaskManager::Resource* GetResource(int origin_pid, |
| 228 | int render_process_host_id, |
| 229 | int routing_id); |
| 230 | virtual void StartUpdating(); |
| 231 | virtual void StopUpdating(); |
| 232 | |
| 233 | // Whether we are currently reporting to the task manager. Used to ignore |
| 234 | // notifications sent after StopUpdating(). |
| 235 | bool updating_; |
| 236 | |
| 237 | private: |
| 238 | void AddToTaskManager(PluginProcessInfo plugin_process_info); |
| 239 | |
| 240 | TaskManager* task_manager_; |
| 241 | TaskManagerBrowserProcessResource resource_; |
| 242 | |
| 243 | DISALLOW_EVIL_CONSTRUCTORS(TaskManagerBrowserProcessResourceProvider); |
| 244 | }; |
| 245 | |
| 246 | #endif // CHROME_BROWSER_TASK_MANAGER_RESOURCE_PROVIDERS_H__ |