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

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: rearrange some code to make it easier to review. 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> 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 ProccessesApiTest;
ncarter (slow) 2016/02/11 00:33:55 Proccesses -> Processes
afakhry 2016/02/17 02:27:14 Done.
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 OnTaskUnresponsive(task_management::TaskId id) override;
52 bool is_task_manager_listening() { return task_manager_listening_; }
53 39
54 private: 40 private:
55 // content::NotificationObserver implementation. 41 friend class ::ProccessesApiTest;
ncarter (slow) 2016/02/11 00:33:55 Proccesses -> Processes
afakhry 2016/02/17 02:27:14 Done.
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 42
73 void DispatchEvent(events::HistogramValue histogram_value, 43 void DispatchEvent(events::HistogramValue histogram_value,
74 const std::string& event_name, 44 const std::string& event_name,
75 scoped_ptr<base::ListValue> event_args); 45 scoped_ptr<base::ListValue> event_args) const;
76 46
77 // Determines whether there is a registered listener for the specified event. 47 // 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. 48 // It helps to avoid collecting data if no one is interested in it.
79 bool HasEventListeners(const std::string& event_name); 49 bool HasEventListeners(const std::string& event_name) const;
80
81 // Used for tracking registrations to process related notifications.
82 content::NotificationRegistrar registrar_;
83 50
84 content::BrowserContext* browser_context_; 51 content::BrowserContext* browser_context_;
85 52
86 // TaskManager to observe for updates.
87 TaskManagerModel* model_;
88
89 // Count of listeners, so we avoid sending updates if no one is interested. 53 // Count of listeners, so we avoid sending updates if no one is interested.
90 int listeners_; 54 int listeners_;
91 55
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); 56 DISALLOW_COPY_AND_ASSIGN(ProcessesEventRouter);
97 }; 57 };
98 58
59 ////////////////////////////////////////////////////////////////////////////////
99 // The profile-keyed service that manages the processes extension API. 60 // The profile-keyed service that manages the processes extension API.
100 class ProcessesAPI : public BrowserContextKeyedAPI, 61 class ProcessesAPI : public BrowserContextKeyedAPI,
101 public EventRouter::Observer { 62 public EventRouter::Observer {
102 public: 63 public:
103 explicit ProcessesAPI(content::BrowserContext* context); 64 explicit ProcessesAPI(content::BrowserContext* context);
104 ~ProcessesAPI() override; 65 ~ProcessesAPI() override;
105 66
106 // KeyedService implementation. 67 // BrowserContextKeyedAPI:
107 void Shutdown() override;
108
109 // BrowserContextKeyedAPI implementation.
110 static BrowserContextKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance(); 68 static BrowserContextKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance();
111 69
112 // Convenience method to get the ProcessesAPI for a profile. 70 // Convenience method to get the ProcessesAPI for a profile.
113 static ProcessesAPI* Get(content::BrowserContext* context); 71 static ProcessesAPI* Get(content::BrowserContext* context);
114 72
115 ProcessesEventRouter* processes_event_router(); 73 // KeyedService:
74 void Shutdown() override;
116 75
117 // EventRouter::Observer implementation. 76 // EventRouter::Observer:
118 void OnListenerAdded(const EventListenerInfo& details) override; 77 void OnListenerAdded(const EventListenerInfo& details) override;
119 void OnListenerRemoved(const EventListenerInfo& details) override; 78 void OnListenerRemoved(const EventListenerInfo& details) override;
120 79
80 ProcessesEventRouter* processes_event_router();
81
121 private: 82 private:
122 friend class BrowserContextKeyedAPIFactory<ProcessesAPI>; 83 friend class BrowserContextKeyedAPIFactory<ProcessesAPI>;
123 84
124 content::BrowserContext* browser_context_; 85 // BrowserContextKeyedAPI:
125 86 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; 87 static const bool kServiceRedirectedInIncognito = true;
131 static const bool kServiceIsNULLWhileTesting = true; 88 static const bool kServiceIsNULLWhileTesting = true;
132 89
90 content::BrowserContext* browser_context_;
91
133 // Created lazily on first access. 92 // Created lazily on first access.
134 scoped_ptr<ProcessesEventRouter> processes_event_router_; 93 scoped_ptr<ProcessesEventRouter> processes_event_router_;
135 }; 94 };
136 95
96 ////////////////////////////////////////////////////////////////////////////////
137 // This extension function returns the Process object for the renderer process 97 // This extension function returns the Process object for the renderer process
138 // currently in use by the specified Tab. 98 // currently in use by the specified Tab.
139 class GetProcessIdForTabFunction : public ChromeAsyncExtensionFunction { 99 class ProcessesGetProcessIdForTabFunction : public UIThreadExtensionFunction {
140 public: 100 public:
141 GetProcessIdForTabFunction(); 101 // UIThreadExtensionFunction:
102 ExtensionFunction::ResponseAction Run() override;
103
104 DECLARE_EXTENSION_FUNCTION("processes.getProcessIdForTab",
105 PROCESSES_GETPROCESSIDFORTAB);
142 106
143 private: 107 private:
144 ~GetProcessIdForTabFunction() override {} 108 ~ProcessesGetProcessIdForTabFunction() 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 }; 109 };
155 110
111 ////////////////////////////////////////////////////////////////////////////////
156 // Extension function that allows terminating Chrome subprocesses, by supplying 112 // Extension function that allows terminating Chrome subprocesses, by supplying
157 // the unique ID for the process coming from the ChildProcess ID pool. 113 // the unique ID for the process coming from the ChildProcess ID pool.
158 // Using unique IDs instead of OS process IDs allows two advantages: 114 // Using unique IDs instead of OS process IDs allows two advantages:
159 // * guaranteed uniqueness, since OS process IDs can be reused 115 // * guaranteed uniqueness, since OS process IDs can be reused.
160 // * guards against killing non-Chrome processes 116 // * guards against killing non-Chrome processes.
161 class TerminateFunction : public ChromeAsyncExtensionFunction { 117 class ProcessesTerminateFunction : public UIThreadExtensionFunction {
162 public: 118 public:
163 TerminateFunction(); 119 // UIThreadExtensionFunction:
120 ExtensionFunction::ResponseAction Run() override;
121
122 DECLARE_EXTENSION_FUNCTION("processes.terminate", PROCESSES_TERMINATE);
164 123
165 private: 124 private:
166 ~TerminateFunction() override {} 125 ~ProcessesTerminateFunction() override {}
167 bool RunAsync() override;
168 126
169 void TerminateProcess(); 127 // Functions to get the process handle on the IO thread and post it back to
128 // the UI thread from processing.
129 base::ProcessHandle GetProcessHandleIO(int child_process_host_id) const;
130 void OnProcessHandleUI(base::ProcessHandle handle);
170 131
171 // Storage for the process ID parameter. 132 // Caches the parameter of this function. To be accessed only on the UI
172 int process_id_; 133 // thread.
173 134 int child_process_host_id_ = 0;
174 DECLARE_EXTENSION_FUNCTION("processes.terminate",
175 PROCESSES_TERMINATE)
176 }; 135 };
177 136
137 ////////////////////////////////////////////////////////////////////////////////
178 // Extension function which returns a set of Process objects, containing the 138 // Extension function which returns a set of Process objects, containing the
179 // details corresponding to the process IDs supplied as input. 139 // details corresponding to the process IDs supplied as input.
180 class GetProcessInfoFunction : public ChromeAsyncExtensionFunction { 140 class ProcessesGetProcessInfoFunction :
141 public UIThreadExtensionFunction,
142 public task_management::TaskManagerObserver {
181 public: 143 public:
182 GetProcessInfoFunction(); 144 ProcessesGetProcessInfoFunction();
145
146 // UIThreadExtensionFunction:
147 ExtensionFunction::ResponseAction Run() override;
148
149 // task_management::TaskManagerObserver:
150 void OnTaskAdded(task_management::TaskId id) override {}
151 void OnTaskToBeRemoved(task_management::TaskId id) override {}
152 void OnTasksRefreshed(const task_management::TaskIdList& task_ids) override;
153
154 DECLARE_EXTENSION_FUNCTION("processes.getProcessInfo",
155 PROCESSES_GETPROCESSINFO);
183 156
184 private: 157 private:
185 ~GetProcessInfoFunction() override; 158 ~ProcessesGetProcessInfoFunction() override;
186 bool RunAsync() override;
187 159
188 void GatherProcessInfo(); 160 std::vector<int> process_host_ids_;
189 161 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 }; 162 };
199 163
200 } // namespace extensions 164 } // namespace extensions
201 165
202 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__ 166 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698