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