Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(682)

Side by Side Diff: chrome/browser/extensions/api/processes/processes_api.h

Issue 1584473004: Migrate ProcessesEventRouter to the new task manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase ... Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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_API_PROCESSES_PROCESSES_API_H__ 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__ 6 #define CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__
7 7
8 #include <set> 8 #include <vector>
9 #include <string>
10 9
11 #include "base/macros.h" 10 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h" 11 #include "chrome/browser/task_management/task_manager_observer.h"
13 #include "chrome/browser/extensions/chrome_extension_function.h"
14 #include "chrome/browser/task_manager/task_manager.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/render_widget_host.h"
19 #include "extensions/browser/browser_context_keyed_api_factory.h" 12 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 #include "extensions/browser/event_router.h" 13 #include "extensions/browser/event_router.h"
21 #include "extensions/browser/extension_event_histogram_value.h" 14 #include "extensions/browser/extension_event_histogram_value.h"
15 #include "extensions/browser/extension_function.h"
22 16
23 namespace base { 17 class ProcessesApiTest;
24 class ListValue;
25 }
26
27 namespace content {
28 class BrowserContext;
29 }
30 18
31 namespace extensions { 19 namespace extensions {
32 20
33 // Observes the Task Manager and routes the notifications as events to the 21 // Observes the Task Manager and routes the notifications as events to the
34 // extension system. 22 // extension system.
35 class ProcessesEventRouter : public TaskManagerModelObserver, 23 class ProcessesEventRouter : public task_management::TaskManagerObserver {
36 public content::NotificationObserver {
37 public: 24 public:
38 explicit ProcessesEventRouter(content::BrowserContext* context); 25 explicit ProcessesEventRouter(content::BrowserContext* context);
39 ~ProcessesEventRouter() override; 26 ~ProcessesEventRouter() override;
40 27
41 // Called when an extension process wants to listen to process events. 28 // Called when an extension process wants to listen to process events.
42 void ListenerAdded(); 29 void ListenerAdded();
43 30
44 // Called when an extension process with a listener exits or removes it. 31 // Called when an extension process with a listener exits or removes it.
45 void ListenerRemoved(); 32 void ListenerRemoved();
46 33
47 // Called on the first invocation of extension API function. This will call 34 // task_management::TaskManagerObserver:
48 // out to the Task Manager to start listening for notifications. Returns 35 void OnTaskAdded(task_management::TaskId id) override;
49 // true if this was the first call and false if this has already been called. 36 void OnTaskToBeRemoved(task_management::TaskId id) override;
50 void StartTaskManagerListening(); 37 void OnTasksRefreshed(const task_management::TaskIdList& task_ids) override {}
51 38 void OnTasksRefreshedWithBackgroundCalculations(
52 bool is_task_manager_listening() { return task_manager_listening_; } 39 const task_management::TaskIdList& task_ids) override;
40 void OnTaskUnresponsive(task_management::TaskId id) override;
53 41
54 private: 42 private:
55 // content::NotificationObserver implementation. 43 friend class ::ProcessesApiTest;
56 void Observe(int type,
57 const content::NotificationSource& source,
58 const content::NotificationDetails& details) override;
59
60 // TaskManagerModelObserver methods.
61 void OnItemsAdded(int start, int length) override;
62 void OnModelChanged() override {}
63 void OnItemsChanged(int start, int length) override;
64 void OnItemsRemoved(int start, int length) override {}
65 void OnItemsToBeRemoved(int start, int length) override;
66
67 // Internal helpers for processing notifications.
68 void ProcessHangEvent(content::RenderWidgetHost* widget);
69 void ProcessClosedEvent(
70 content::RenderProcessHost* rph,
71 content::RenderProcessHost::RendererClosedDetails* details);
72 44
73 void DispatchEvent(events::HistogramValue histogram_value, 45 void DispatchEvent(events::HistogramValue histogram_value,
74 const std::string& event_name, 46 const std::string& event_name,
75 scoped_ptr<base::ListValue> event_args); 47 scoped_ptr<base::ListValue> event_args) const;
76 48
77 // Determines whether there is a registered listener for the specified event. 49 // 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. 50 // It helps to avoid collecting data if no one is interested in it.
79 bool HasEventListeners(const std::string& event_name); 51 bool HasEventListeners(const std::string& event_name) const;
80
81 // Used for tracking registrations to process related notifications.
82 content::NotificationRegistrar registrar_;
83 52
84 content::BrowserContext* browser_context_; 53 content::BrowserContext* browser_context_;
85 54
86 // TaskManager to observe for updates.
87 TaskManagerModel* model_;
88
89 // Count of listeners, so we avoid sending updates if no one is interested. 55 // Count of listeners, so we avoid sending updates if no one is interested.
90 int listeners_; 56 int listeners_;
91 57
92 // Indicator whether we've initialized the Task Manager listeners. This is
93 // done once for the lifetime of this object.
94 bool task_manager_listening_;
95
96 DISALLOW_COPY_AND_ASSIGN(ProcessesEventRouter); 58 DISALLOW_COPY_AND_ASSIGN(ProcessesEventRouter);
97 }; 59 };
98 60
61 ////////////////////////////////////////////////////////////////////////////////
99 // The profile-keyed service that manages the processes extension API. 62 // The profile-keyed service that manages the processes extension API.
100 class ProcessesAPI : public BrowserContextKeyedAPI, 63 class ProcessesAPI : public BrowserContextKeyedAPI,
101 public EventRouter::Observer { 64 public EventRouter::Observer {
102 public: 65 public:
103 explicit ProcessesAPI(content::BrowserContext* context); 66 explicit ProcessesAPI(content::BrowserContext* context);
104 ~ProcessesAPI() override; 67 ~ProcessesAPI() override;
105 68
106 // KeyedService implementation. 69 // BrowserContextKeyedAPI:
107 void Shutdown() override;
108
109 // BrowserContextKeyedAPI implementation.
110 static BrowserContextKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance(); 70 static BrowserContextKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance();
111 71
112 // Convenience method to get the ProcessesAPI for a profile. 72 // Convenience method to get the ProcessesAPI for a profile.
113 static ProcessesAPI* Get(content::BrowserContext* context); 73 static ProcessesAPI* Get(content::BrowserContext* context);
114 74
115 ProcessesEventRouter* processes_event_router(); 75 // KeyedService:
76 void Shutdown() override;
116 77
117 // EventRouter::Observer implementation. 78 // EventRouter::Observer:
118 void OnListenerAdded(const EventListenerInfo& details) override; 79 void OnListenerAdded(const EventListenerInfo& details) override;
119 void OnListenerRemoved(const EventListenerInfo& details) override; 80 void OnListenerRemoved(const EventListenerInfo& details) override;
120 81
82 ProcessesEventRouter* processes_event_router();
83
121 private: 84 private:
122 friend class BrowserContextKeyedAPIFactory<ProcessesAPI>; 85 friend class BrowserContextKeyedAPIFactory<ProcessesAPI>;
123 86
124 content::BrowserContext* browser_context_; 87 // BrowserContextKeyedAPI:
125 88 static const char* service_name() { return "ProcessesAPI"; }
126 // BrowserContextKeyedAPI implementation.
127 static const char* service_name() {
128 return "ProcessesAPI";
129 }
130 static const bool kServiceRedirectedInIncognito = true; 89 static const bool kServiceRedirectedInIncognito = true;
131 static const bool kServiceIsNULLWhileTesting = true; 90 static const bool kServiceIsNULLWhileTesting = true;
132 91
92 content::BrowserContext* browser_context_;
93
133 // Created lazily on first access. 94 // Created lazily on first access.
134 scoped_ptr<ProcessesEventRouter> processes_event_router_; 95 scoped_ptr<ProcessesEventRouter> processes_event_router_;
135 }; 96 };
136 97
137 //////////////////////////////////////////////////////////////////////////////// 98 ////////////////////////////////////////////////////////////////////////////////
138 // This extension function returns the Process object for the renderer process 99 // This extension function returns the Process object for the renderer process
139 // currently in use by the specified Tab. 100 // currently in use by the specified Tab.
140 class ProcessesGetProcessIdForTabFunction : public UIThreadExtensionFunction { 101 class ProcessesGetProcessIdForTabFunction : public UIThreadExtensionFunction {
141 public: 102 public:
142 ProcessesGetProcessIdForTabFunction();
143
144 // UIThreadExtensionFunction: 103 // UIThreadExtensionFunction:
145 ExtensionFunction::ResponseAction Run() override; 104 ExtensionFunction::ResponseAction Run() override;
146 105
106 DECLARE_EXTENSION_FUNCTION("processes.getProcessIdForTab",
107 PROCESSES_GETPROCESSIDFORTAB);
108
147 private: 109 private:
148 ~ProcessesGetProcessIdForTabFunction() override {} 110 ~ProcessesGetProcessIdForTabFunction() override {}
149
150 void GetProcessIdForTab();
151
152 // Storage for the tab ID parameter.
153 int tab_id_;
154
155 DECLARE_EXTENSION_FUNCTION("processes.getProcessIdForTab",
156 PROCESSES_GETPROCESSIDFORTAB)
157 }; 111 };
158 112
159 //////////////////////////////////////////////////////////////////////////////// 113 ////////////////////////////////////////////////////////////////////////////////
160 // Extension function that allows terminating Chrome subprocesses, by supplying 114 // Extension function that allows terminating Chrome subprocesses, by supplying
161 // the unique ID for the process coming from the ChildProcess ID pool. 115 // the unique ID for the process coming from the ChildProcess ID pool.
162 // Using unique IDs instead of OS process IDs allows two advantages: 116 // Using unique IDs instead of OS process IDs allows two advantages:
163 // * guaranteed uniqueness, since OS process IDs can be reused. 117 // * guaranteed uniqueness, since OS process IDs can be reused.
164 // * guards against killing non-Chrome processes. 118 // * guards against killing non-Chrome processes.
165 class ProcessesTerminateFunction : public UIThreadExtensionFunction { 119 class ProcessesTerminateFunction : public UIThreadExtensionFunction {
166 public: 120 public:
167 ProcessesTerminateFunction();
168
169 // UIThreadExtensionFunction: 121 // UIThreadExtensionFunction:
170 ExtensionFunction::ResponseAction Run() override; 122 ExtensionFunction::ResponseAction Run() override;
171 123
124 DECLARE_EXTENSION_FUNCTION("processes.terminate", PROCESSES_TERMINATE);
125
172 private: 126 private:
173 ~ProcessesTerminateFunction() override {} 127 ~ProcessesTerminateFunction() override {}
174 128
175 void TerminateProcess(); 129 // Functions to get the process handle on the IO thread and post it back to
130 // the UI thread from processing.
131 base::ProcessHandle GetProcessHandleIO(int child_process_host_id) const;
Devlin 2016/03/03 23:12:43 nit: OnIO
afakhry 2016/03/08 01:37:29 Done.
132 void OnProcessHandleUI(base::ProcessHandle handle);
Devlin 2016/03/03 23:12:43 nit: ReceivedProcessHandleOnUI
afakhry 2016/03/08 01:37:29 Done.
Devlin 2016/03/08 21:27:22 I think this one was only half-done - OnProcessHan
176 133
177 // Storage for the process ID parameter. 134 // Caches the parameter of this function. To be accessed only on the UI
178 int process_id_; 135 // thread.
179 136 int child_process_host_id_ = 0;
180 DECLARE_EXTENSION_FUNCTION("processes.terminate",
181 PROCESSES_TERMINATE)
182 }; 137 };
183 138
184 //////////////////////////////////////////////////////////////////////////////// 139 ////////////////////////////////////////////////////////////////////////////////
185 // Extension function which returns a set of Process objects, containing the 140 // Extension function which returns a set of Process objects, containing the
186 // details corresponding to the process IDs supplied as input. 141 // details corresponding to the process IDs supplied as input.
187 class ProcessesGetProcessInfoFunction : public UIThreadExtensionFunction { 142 class ProcessesGetProcessInfoFunction :
143 public UIThreadExtensionFunction,
144 public task_management::TaskManagerObserver {
188 public: 145 public:
189 ProcessesGetProcessInfoFunction(); 146 ProcessesGetProcessInfoFunction();
190 147
191 // UIThreadExtensionFunction: 148 // UIThreadExtensionFunction:
192 ExtensionFunction::ResponseAction Run() override; 149 ExtensionFunction::ResponseAction Run() override;
193 150
151 // task_management::TaskManagerObserver:
152 void OnTaskAdded(task_management::TaskId id) override {}
153 void OnTaskToBeRemoved(task_management::TaskId id) override {}
154 void OnTasksRefreshed(const task_management::TaskIdList& task_ids) override;
155 void OnTasksRefreshedWithBackgroundCalculations(
156 const task_management::TaskIdList& task_ids) override;
157
158 DECLARE_EXTENSION_FUNCTION("processes.getProcessInfo",
159 PROCESSES_GETPROCESSINFO);
160
194 private: 161 private:
195 ~ProcessesGetProcessInfoFunction() override; 162 ~ProcessesGetProcessInfoFunction() override;
196 163
197 void GatherProcessInfo(); 164 // Since we don't report optional process data like CPU usage in the results
165 // of this function. The only background calculations we want to watch is
Devlin 2016/03/03 23:12:43 Reword this sentence (is the "function." supposed
afakhry 2016/03/08 01:37:29 Yes, done.
166 // memory usage (which will be requested only when |include_memory_| is true).
167 // This function will be called by either OnTasksRefreshed() or
168 // OnTasksRefreshedWithBackgroundCalculations() depending on whether memory is
169 // requested.
170 void GatherDataAndRespond(const task_management::TaskIdList& task_ids);
198 171
199 // Member variables to store the function parameters 172 std::vector<int> process_host_ids_;
200 std::vector<int> process_ids_; 173 bool include_memory_ = false;
201 #if defined(ENABLE_TASK_MANAGER)
202 bool memory_;
203 #endif
204
205 DECLARE_EXTENSION_FUNCTION("processes.getProcessInfo",
206 PROCESSES_GETPROCESSINFO)
207 }; 174 };
208 175
209 } // namespace extensions 176 } // namespace extensions
210 177
211 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__ 178 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698