Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / processes / processes_api.h
blob289f161614ab410d5c13af16ef1b9cc62e11b213
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__
8 #include <set>
9 #include <string>
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/extensions/chrome_extension_function.h"
13 #include "chrome/browser/task_manager/task_manager.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/render_widget_host.h"
18 #include "extensions/browser/browser_context_keyed_api_factory.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/browser/extension_event_histogram_value.h"
22 namespace base {
23 class ListValue;
26 namespace content {
27 class BrowserContext;
30 namespace extensions {
32 // Observes the Task Manager and routes the notifications as events to the
33 // extension system.
34 class ProcessesEventRouter : public TaskManagerModelObserver,
35 public content::NotificationObserver {
36 public:
37 explicit ProcessesEventRouter(content::BrowserContext* context);
38 ~ProcessesEventRouter() override;
40 // Called when an extension process wants to listen to process events.
41 void ListenerAdded();
43 // Called when an extension process with a listener exits or removes it.
44 void ListenerRemoved();
46 // Called on the first invocation of extension API function. This will call
47 // out to the Task Manager to start listening for notifications. Returns
48 // true if this was the first call and false if this has already been called.
49 void StartTaskManagerListening();
51 bool is_task_manager_listening() { return task_manager_listening_; }
53 private:
54 // content::NotificationObserver implementation.
55 void Observe(int type,
56 const content::NotificationSource& source,
57 const content::NotificationDetails& details) override;
59 // TaskManagerModelObserver methods.
60 void OnItemsAdded(int start, int length) override;
61 void OnModelChanged() override {}
62 void OnItemsChanged(int start, int length) override;
63 void OnItemsRemoved(int start, int length) override {}
64 void OnItemsToBeRemoved(int start, int length) override;
66 // Internal helpers for processing notifications.
67 void ProcessHangEvent(content::RenderWidgetHost* widget);
68 void ProcessClosedEvent(
69 content::RenderProcessHost* rph,
70 content::RenderProcessHost::RendererClosedDetails* details);
72 void DispatchEvent(events::HistogramValue histogram_value,
73 const std::string& event_name,
74 scoped_ptr<base::ListValue> event_args);
76 // Determines whether there is a registered listener for the specified event.
77 // It helps to avoid collecing data if no one is interested in it.
78 bool HasEventListeners(const std::string& event_name);
80 // Used for tracking registrations to process related notifications.
81 content::NotificationRegistrar registrar_;
83 content::BrowserContext* browser_context_;
85 // TaskManager to observe for updates.
86 TaskManagerModel* model_;
88 // Count of listeners, so we avoid sending updates if no one is interested.
89 int listeners_;
91 // Indicator whether we've initialized the Task Manager listeners. This is
92 // done once for the lifetime of this object.
93 bool task_manager_listening_;
95 DISALLOW_COPY_AND_ASSIGN(ProcessesEventRouter);
98 // The profile-keyed service that manages the processes extension API.
99 class ProcessesAPI : public BrowserContextKeyedAPI,
100 public EventRouter::Observer {
101 public:
102 explicit ProcessesAPI(content::BrowserContext* context);
103 ~ProcessesAPI() override;
105 // KeyedService implementation.
106 void Shutdown() override;
108 // BrowserContextKeyedAPI implementation.
109 static BrowserContextKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance();
111 // Convenience method to get the ProcessesAPI for a profile.
112 static ProcessesAPI* Get(content::BrowserContext* context);
114 ProcessesEventRouter* processes_event_router();
116 // EventRouter::Observer implementation.
117 void OnListenerAdded(const EventListenerInfo& details) override;
118 void OnListenerRemoved(const EventListenerInfo& details) override;
120 private:
121 friend class BrowserContextKeyedAPIFactory<ProcessesAPI>;
123 content::BrowserContext* browser_context_;
125 // BrowserContextKeyedAPI implementation.
126 static const char* service_name() {
127 return "ProcessesAPI";
129 static const bool kServiceRedirectedInIncognito = true;
130 static const bool kServiceIsNULLWhileTesting = true;
132 // Created lazily on first access.
133 scoped_ptr<ProcessesEventRouter> processes_event_router_;
136 // This extension function returns the Process object for the renderer process
137 // currently in use by the specified Tab.
138 class GetProcessIdForTabFunction : public ChromeAsyncExtensionFunction {
139 public:
140 GetProcessIdForTabFunction();
142 private:
143 ~GetProcessIdForTabFunction() override {}
144 bool RunAsync() override;
146 void GetProcessIdForTab();
148 // Storage for the tab ID parameter.
149 int tab_id_;
151 DECLARE_EXTENSION_FUNCTION("processes.getProcessIdForTab",
152 PROCESSES_GETPROCESSIDFORTAB)
155 // Extension function that allows terminating Chrome subprocesses, by supplying
156 // the unique ID for the process coming from the ChildProcess ID pool.
157 // Using unique IDs instead of OS process IDs allows two advantages:
158 // * guaranteed uniqueness, since OS process IDs can be reused
159 // * guards against killing non-Chrome processes
160 class TerminateFunction : public ChromeAsyncExtensionFunction {
161 public:
162 TerminateFunction();
164 private:
165 ~TerminateFunction() override {}
166 bool RunAsync() override;
168 void TerminateProcess();
170 // Storage for the process ID parameter.
171 int process_id_;
173 DECLARE_EXTENSION_FUNCTION("processes.terminate",
174 PROCESSES_TERMINATE)
177 // Extension function which returns a set of Process objects, containing the
178 // details corresponding to the process IDs supplied as input.
179 class GetProcessInfoFunction : public ChromeAsyncExtensionFunction {
180 public:
181 GetProcessInfoFunction();
183 private:
184 ~GetProcessInfoFunction() override;
185 bool RunAsync() override;
187 void GatherProcessInfo();
189 // Member variables to store the function parameters
190 std::vector<int> process_ids_;
191 #if defined(ENABLE_TASK_MANAGER)
192 bool memory_;
193 #endif
195 DECLARE_EXTENSION_FUNCTION("processes.getProcessInfo",
196 PROCESSES_GETPROCESSINFO)
199 } // namespace extensions
201 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__