OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "chrome/browser/extensions/extension_function.h" | 12 #include "chrome/browser/extensions/extension_function.h" |
13 #include "chrome/browser/task_manager/task_manager.h" | 13 #include "chrome/browser/task_manager/task_manager.h" |
14 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
| 15 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/browser/render_widget_host.h" |
15 | 17 |
16 // Observes the Task Manager and routes the notifications as events to the | 18 // Observes the Task Manager and routes the notifications as events to the |
17 // extension system. | 19 // extension system. |
18 class ExtensionProcessesEventRouter : public TaskManagerModelObserver { | 20 class ExtensionProcessesEventRouter : public TaskManagerModelObserver, |
| 21 public content::NotificationObserver { |
19 public: | 22 public: |
20 // Single instance of the event router. | 23 // Single instance of the event router. |
21 static ExtensionProcessesEventRouter* GetInstance(); | 24 static ExtensionProcessesEventRouter* GetInstance(); |
22 | 25 |
23 // Safe to call multiple times. | 26 // Safe to call multiple times. |
24 void ObserveProfile(Profile* profile); | 27 void ObserveProfile(Profile* profile); |
25 | 28 |
26 // Called when an extension process wants to listen to process events. | 29 // Called when an extension process wants to listen to process events. |
27 void ListenerAdded(); | 30 void ListenerAdded(); |
28 | 31 |
29 // Called when an extension process with a listener exits or removes it. | 32 // Called when an extension process with a listener exits or removes it. |
30 void ListenerRemoved(); | 33 void ListenerRemoved(); |
31 | 34 |
| 35 // Called on the first invocation of extension API function. This will call |
| 36 // out to the Task Manager to start listening for notifications. Returns |
| 37 // true if this was the first call and false if this has already been called. |
| 38 bool DidStartTaskManagerListening(); |
| 39 |
| 40 bool is_task_manager_listening() { return task_manager_listening_; } |
| 41 int num_listeners() { return listeners_; } |
| 42 |
32 private: | 43 private: |
33 friend struct DefaultSingletonTraits<ExtensionProcessesEventRouter>; | 44 friend struct DefaultSingletonTraits<ExtensionProcessesEventRouter>; |
34 | 45 |
35 ExtensionProcessesEventRouter(); | 46 ExtensionProcessesEventRouter(); |
36 virtual ~ExtensionProcessesEventRouter(); | 47 virtual ~ExtensionProcessesEventRouter(); |
37 | 48 |
| 49 // content::NotificationObserver implementation. |
| 50 virtual void Observe(int type, |
| 51 const content::NotificationSource& source, |
| 52 const content::NotificationDetails& details) OVERRIDE; |
| 53 |
38 // TaskManagerModelObserver methods. | 54 // TaskManagerModelObserver methods. |
| 55 virtual void OnItemsAdded(int start, int length) OVERRIDE; |
39 virtual void OnModelChanged() OVERRIDE {} | 56 virtual void OnModelChanged() OVERRIDE {} |
40 virtual void OnItemsChanged(int start, int length) OVERRIDE; | 57 virtual void OnItemsChanged(int start, int length) OVERRIDE; |
41 virtual void OnItemsAdded(int start, int length) OVERRIDE {} | |
42 virtual void OnItemsRemoved(int start, int length) OVERRIDE {} | 58 virtual void OnItemsRemoved(int start, int length) OVERRIDE {} |
| 59 virtual void OnItemsToBeRemoved(int start, int length) OVERRIDE; |
| 60 |
| 61 // Internal helpers for processing notifications. |
| 62 void ProcessHangEvent(content::RenderWidgetHost* widget); |
| 63 void ProcessClosedEvent( |
| 64 content::RenderProcessHost* rph, |
| 65 content::RenderProcessHost::RendererClosedDetails* details); |
| 66 |
| 67 void NotifyProfiles(const char* event_name, std::string json_args); |
43 | 68 |
44 void DispatchEvent(Profile* profile, | 69 void DispatchEvent(Profile* profile, |
45 const char* event_name, | 70 const char* event_name, |
46 const std::string& json_args); | 71 const std::string& json_args); |
47 | 72 |
| 73 // Determines whether there is a registered listener for the specified event. |
| 74 // It helps to avoid collecing data if no one is interested in it. |
| 75 bool HasEventListeners(std::string& event_name); |
| 76 |
48 // Used for tracking registrations to process related notifications. | 77 // Used for tracking registrations to process related notifications. |
49 content::NotificationRegistrar registrar_; | 78 content::NotificationRegistrar registrar_; |
50 | 79 |
51 // Registered profiles. | 80 // Registered profiles. |
52 typedef std::set<Profile*> ProfileSet; | 81 typedef std::set<Profile*> ProfileSet; |
53 ProfileSet profiles_; | 82 ProfileSet profiles_; |
54 | 83 |
55 // TaskManager to observe for updates. | 84 // TaskManager to observe for updates. |
56 TaskManagerModel* model_; | 85 TaskManagerModel* model_; |
57 | 86 |
| 87 // Count of listeners, so we avoid sending updates if no one is interested. |
| 88 int listeners_; |
| 89 |
| 90 // Indicator whether we've initialized the Task Manager listeners. This is |
| 91 // done once for the lifetime of this object. |
| 92 bool task_manager_listening_; |
| 93 |
58 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessesEventRouter); | 94 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessesEventRouter); |
59 }; | 95 }; |
60 | 96 |
61 | 97 |
62 // This extension function returns the Process object for the renderer process | 98 // This extension function returns the Process object for the renderer process |
63 // currently in use by the specified Tab. | 99 // currently in use by the specified Tab. |
64 class GetProcessIdForTabFunction : public SyncExtensionFunction { | 100 class GetProcessIdForTabFunction : public AsyncExtensionFunction, |
| 101 public content::NotificationObserver { |
| 102 private: |
65 virtual ~GetProcessIdForTabFunction() {} | 103 virtual ~GetProcessIdForTabFunction() {} |
66 virtual bool RunImpl() OVERRIDE; | 104 virtual bool RunImpl() OVERRIDE; |
| 105 |
| 106 // content::NotificationObserver implementation. |
| 107 virtual void Observe(int type, |
| 108 const content::NotificationSource& source, |
| 109 const content::NotificationDetails& details) OVERRIDE; |
| 110 |
| 111 void GetProcessIdForTab(); |
| 112 |
| 113 content::NotificationRegistrar registrar_; |
| 114 |
| 115 // Storage for the tab ID parameter. |
| 116 int tab_id_; |
| 117 |
67 DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.getProcessIdForTab") | 118 DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.getProcessIdForTab") |
68 }; | 119 }; |
69 | 120 |
| 121 // Extension function that allows terminating Chrome subprocesses, by supplying |
| 122 // the unique ID for the process coming from the ChildProcess ID pool. |
| 123 // Using unique IDs instead of OS process IDs allows two advantages: |
| 124 // * guaranteed uniqueness, since OS process IDs can be reused |
| 125 // * guards against killing non-Chrome processes |
| 126 class TerminateFunction : public AsyncExtensionFunction, |
| 127 public content::NotificationObserver { |
| 128 private: |
| 129 virtual ~TerminateFunction() {} |
| 130 virtual bool RunImpl() OVERRIDE; |
| 131 |
| 132 // content::NotificationObserver implementation. |
| 133 virtual void Observe(int type, |
| 134 const content::NotificationSource& source, |
| 135 const content::NotificationDetails& details) OVERRIDE; |
| 136 |
| 137 void TerminateProcess(); |
| 138 |
| 139 content::NotificationRegistrar registrar_; |
| 140 |
| 141 // Storage for the process ID parameter. |
| 142 int process_id_; |
| 143 |
| 144 DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.terminate") |
| 145 }; |
| 146 |
| 147 // Extension function which returns a set of Process objects, containing the |
| 148 // details corresponding to the process IDs supplied as input. |
| 149 class GetProcessInfoFunction : public AsyncExtensionFunction, |
| 150 public content::NotificationObserver { |
| 151 public: |
| 152 GetProcessInfoFunction(); |
| 153 |
| 154 private: |
| 155 virtual ~GetProcessInfoFunction(); |
| 156 virtual bool RunImpl() OVERRIDE; |
| 157 |
| 158 // content::NotificationObserver implementation. |
| 159 virtual void Observe(int type, |
| 160 const content::NotificationSource& source, |
| 161 const content::NotificationDetails& details) OVERRIDE; |
| 162 |
| 163 void GatherProcessInfo(); |
| 164 |
| 165 content::NotificationRegistrar registrar_; |
| 166 |
| 167 // Member variables to store the function parameters |
| 168 std::vector<int> process_ids_; |
| 169 bool memory_; |
| 170 |
| 171 DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.getProcessInfo") |
| 172 }; |
| 173 |
70 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__ | 174 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__ |
OLD | NEW |