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

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: dispatch onCreated and onExited only for processes with valid child process host IDs Created 4 years, 10 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>
9 #include <string>
10
11 #include "base/macros.h" 8 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h" 9 #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" 10 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 #include "extensions/browser/event_router.h" 11 #include "extensions/browser/event_router.h"
21 #include "extensions/browser/extension_event_histogram_value.h" 12 #include "extensions/browser/extension_event_histogram_value.h"
13 #include "extensions/browser/extension_function.h"
22 14
23 namespace base { 15 class ProccessesApiTest;
24 class ListValue;
25 }
26
27 namespace content {
28 class BrowserContext;
29 }
30 16
31 namespace extensions { 17 namespace extensions {
32 18
33 // Observes the Task Manager and routes the notifications as events to the 19 // Observes the Task Manager and routes the notifications as events to the
34 // extension system. 20 // extension system.
35 class ProcessesEventRouter : public TaskManagerModelObserver, 21 class ProcessesEventRouter : public task_management::TaskManagerObserver {
36 public content::NotificationObserver {
37 public: 22 public:
38 explicit ProcessesEventRouter(content::BrowserContext* context); 23 explicit ProcessesEventRouter(content::BrowserContext* context);
39 ~ProcessesEventRouter() override; 24 ~ProcessesEventRouter() override;
40 25
41 // Called when an extension process wants to listen to process events. 26 // Called when an extension process wants to listen to process events.
42 void ListenerAdded(); 27 void ListenerAdded();
43 28
44 // Called when an extension process with a listener exits or removes it. 29 // Called when an extension process with a listener exits or removes it.
45 void ListenerRemoved(); 30 void ListenerRemoved();
46 31
47 // Called on the first invocation of extension API function. This will call 32 // task_management::TaskManagerObserver:
48 // out to the Task Manager to start listening for notifications. Returns 33 void OnTaskAdded(task_management::TaskId id) override;
49 // true if this was the first call and false if this has already been called. 34 void OnTaskToBeRemoved(task_management::TaskId id) override;
50 void StartTaskManagerListening(); 35 void OnTasksRefreshed(const task_management::TaskIdList& task_ids) override;
51 36 void OnTaskUnresponsive(task_management::TaskId id) override;
52 bool is_task_manager_listening() { return task_manager_listening_; }
53 37
54 private: 38 private:
55 // content::NotificationObserver implementation. 39 friend class ::ProccessesApiTest;
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 40
73 void DispatchEvent(events::HistogramValue histogram_value, 41 void DispatchEvent(events::HistogramValue histogram_value,
74 const std::string& event_name, 42 const std::string& event_name,
75 scoped_ptr<base::ListValue> event_args); 43 scoped_ptr<base::ListValue> event_args) const;
76 44
77 // Determines whether there is a registered listener for the specified event. 45 // 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. 46 // It helps to avoid collecting data if no one is interested in it.
79 bool HasEventListeners(const std::string& event_name); 47 bool HasEventListeners(const std::string& event_name) const;
80
81 // Used for tracking registrations to process related notifications.
82 content::NotificationRegistrar registrar_;
83 48
84 content::BrowserContext* browser_context_; 49 content::BrowserContext* browser_context_;
85 50
86 // TaskManager to observe for updates.
87 TaskManagerModel* model_;
88
89 // Count of listeners, so we avoid sending updates if no one is interested. 51 // Count of listeners, so we avoid sending updates if no one is interested.
90 int listeners_; 52 int listeners_;
91 53
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); 54 DISALLOW_COPY_AND_ASSIGN(ProcessesEventRouter);
97 }; 55 };
98 56
57 ////////////////////////////////////////////////////////////////////////////////
99 // The profile-keyed service that manages the processes extension API. 58 // The profile-keyed service that manages the processes extension API.
100 class ProcessesAPI : public BrowserContextKeyedAPI, 59 class ProcessesAPI : public BrowserContextKeyedAPI,
101 public EventRouter::Observer { 60 public EventRouter::Observer {
102 public: 61 public:
103 explicit ProcessesAPI(content::BrowserContext* context); 62 explicit ProcessesAPI(content::BrowserContext* context);
104 ~ProcessesAPI() override; 63 ~ProcessesAPI() override;
105 64
106 // KeyedService implementation.
107 void Shutdown() override;
108
109 // BrowserContextKeyedAPI implementation.
110 static BrowserContextKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance();
111
112 // Convenience method to get the ProcessesAPI for a profile. 65 // Convenience method to get the ProcessesAPI for a profile.
113 static ProcessesAPI* Get(content::BrowserContext* context); 66 static ProcessesAPI* Get(content::BrowserContext* context);
114 67
115 ProcessesEventRouter* processes_event_router(); 68 // BrowserContextKeyedAPI:
69 static BrowserContextKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance();
116 70
117 // EventRouter::Observer implementation. 71 // KeyedService:
72 void Shutdown() override;
73
74 // EventRouter::Observer:
118 void OnListenerAdded(const EventListenerInfo& details) override; 75 void OnListenerAdded(const EventListenerInfo& details) override;
119 void OnListenerRemoved(const EventListenerInfo& details) override; 76 void OnListenerRemoved(const EventListenerInfo& details) override;
120 77
78 ProcessesEventRouter* processes_event_router();
79
121 private: 80 private:
122 friend class BrowserContextKeyedAPIFactory<ProcessesAPI>; 81 friend class BrowserContextKeyedAPIFactory<ProcessesAPI>;
123 82
124 content::BrowserContext* browser_context_; 83 // BrowserContextKeyedAPI:
125 84 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; 85 static const bool kServiceRedirectedInIncognito = true;
131 static const bool kServiceIsNULLWhileTesting = true; 86 static const bool kServiceIsNULLWhileTesting = true;
132 87
88 content::BrowserContext* browser_context_;
89
133 // Created lazily on first access. 90 // Created lazily on first access.
134 scoped_ptr<ProcessesEventRouter> processes_event_router_; 91 scoped_ptr<ProcessesEventRouter> processes_event_router_;
135 }; 92 };
136 93
137 // This extension function returns the Process object for the renderer process 94 ////////////////////////////////////////////////////////////////////////////////
138 // currently in use by the specified Tab.
139 class GetProcessIdForTabFunction : public ChromeAsyncExtensionFunction {
140 public:
141 GetProcessIdForTabFunction();
142
143 private:
144 ~GetProcessIdForTabFunction() override {}
145 bool RunAsync() override;
146
147 void GetProcessIdForTab();
148
149 // Storage for the tab ID parameter.
150 int tab_id_;
151
152 DECLARE_EXTENSION_FUNCTION("processes.getProcessIdForTab",
153 PROCESSES_GETPROCESSIDFORTAB)
154 };
155
156 // Extension function that allows terminating Chrome subprocesses, by supplying 95 // Extension function that allows terminating Chrome subprocesses, by supplying
157 // the unique ID for the process coming from the ChildProcess ID pool. 96 // the unique ID for the process coming from the ChildProcess ID pool.
158 // Using unique IDs instead of OS process IDs allows two advantages: 97 // Using unique IDs instead of OS process IDs allows two advantages:
159 // * guaranteed uniqueness, since OS process IDs can be reused 98 // * guaranteed uniqueness, since OS process IDs can be reused.
160 // * guards against killing non-Chrome processes 99 // * guards against killing non-Chrome processes.
161 class TerminateFunction : public ChromeAsyncExtensionFunction { 100 class ProcessesTerminateFunction : public UIThreadExtensionFunction {
162 public: 101 public:
163 TerminateFunction(); 102 // UIThreadExtensionFunction:
103 ExtensionFunction::ResponseAction Run() override;
104
105 DECLARE_EXTENSION_FUNCTION("processes.terminate", PROCESSES_TERMINATE);
164 106
165 private: 107 private:
166 ~TerminateFunction() override {} 108 ~ProcessesTerminateFunction() override {}
167 bool RunAsync() override;
168 109
169 void TerminateProcess(); 110 // Functions to get the process handle on the IO thread and post it back to
111 // the UI thread from processing.
112 base::ProcessHandle GetProcessHandleIO(int child_process_host_id) const;
113 void OnProcessHandleUI(base::ProcessHandle handle);
170 114
171 // Storage for the process ID parameter. 115 // Caches the parameter of this function. To be accessed only on the UI
172 int process_id_; 116 // thread.
173 117 int child_process_host_id_;
ncarter (slow) 2016/02/08 22:37:09 You don't have a ctor, so it looks like there's no
afakhry 2016/02/09 04:01:23 Done.
174 DECLARE_EXTENSION_FUNCTION("processes.terminate",
175 PROCESSES_TERMINATE)
176 }; 118 };
177 119
120 ////////////////////////////////////////////////////////////////////////////////
121 // This extension function returns the Process object for the renderer process
122 // currently in use by the specified Tab.
123 class ProcessesGetProcessIdForTabFunction : public UIThreadExtensionFunction {
124 public:
125 // UIThreadExtensionFunction:
126 ExtensionFunction::ResponseAction Run() override;
127
128 DECLARE_EXTENSION_FUNCTION("processes.getProcessIdForTab",
129 PROCESSES_GETPROCESSIDFORTAB);
130
131 private:
132 ~ProcessesGetProcessIdForTabFunction() override {}
133 };
134
135 ////////////////////////////////////////////////////////////////////////////////
178 // Extension function which returns a set of Process objects, containing the 136 // Extension function which returns a set of Process objects, containing the
179 // details corresponding to the process IDs supplied as input. 137 // details corresponding to the process IDs supplied as input.
180 class GetProcessInfoFunction : public ChromeAsyncExtensionFunction { 138 class ProcessesGetProcessInfoFunction :
139 public UIThreadExtensionFunction,
140 public task_management::TaskManagerObserver {
181 public: 141 public:
182 GetProcessInfoFunction(); 142 ProcessesGetProcessInfoFunction();
143
144 // UIThreadExtensionFunction:
145 ExtensionFunction::ResponseAction Run() override;
146
147 // task_management::TaskManagerObserver:
148 void OnTaskAdded(task_management::TaskId id) override {}
149 void OnTaskToBeRemoved(task_management::TaskId id) override {}
150 void OnTasksRefreshed(const task_management::TaskIdList& task_ids) override;
151
152 DECLARE_EXTENSION_FUNCTION("processes.getProcessInfo",
153 PROCESSES_GETPROCESSINFO);
183 154
184 private: 155 private:
185 ~GetProcessInfoFunction() override; 156 ~ProcessesGetProcessInfoFunction() override;
186 bool RunAsync() override;
187 157
188 void GatherProcessInfo(); 158 std::vector<int> process_host_ids_;
ncarter (slow) 2016/02/08 22:37:09 #include <vector>
afakhry 2016/02/09 04:01:23 Done.
189 159 bool include_memory_ = false;
190 // Member variables to store the function parameters
191 std::vector<int> process_ids_;
192 #if defined(ENABLE_TASK_MANAGER)
193 bool memory_;
194 #endif
195
196 DECLARE_EXTENSION_FUNCTION("processes.getProcessInfo",
197 PROCESSES_GETPROCESSINFO)
198 }; 160 };
199 161
200 } // namespace extensions 162 } // namespace extensions
201 163
202 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__ 164 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698