OLD | NEW |
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 <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "chrome/browser/extensions/extension_function.h" | 11 #include "chrome/browser/extensions/extension_function.h" |
12 #include "chrome/browser/task_manager/task_manager.h" | 12 #include "chrome/browser/task_manager/task_manager.h" |
13 #include "content/public/browser/notification_registrar.h" | 13 #include "content/public/browser/notification_registrar.h" |
14 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/render_widget_host.h" | 15 #include "content/public/browser/render_widget_host.h" |
16 | 16 |
| 17 class Profile; |
| 18 |
17 namespace base { | 19 namespace base { |
18 class ListValue; | 20 class ListValue; |
19 } | 21 } |
20 | 22 |
21 namespace extensions { | 23 namespace extensions { |
22 | 24 |
23 // Observes the Task Manager and routes the notifications as events to the | 25 // Observes the Task Manager and routes the notifications as events to the |
24 // extension system. | 26 // extension system. |
25 class ProcessesEventRouter : public TaskManagerModelObserver, | 27 class ProcessesEventRouter : public TaskManagerModelObserver, |
26 public content::NotificationObserver { | 28 public content::NotificationObserver { |
27 public: | 29 public: |
28 // Single instance of the event router. | 30 explicit ProcessesEventRouter(Profile* profile); |
29 static ProcessesEventRouter* GetInstance(); | 31 virtual ~ProcessesEventRouter(); |
30 | |
31 // Safe to call multiple times. | |
32 void ObserveProfile(Profile* profile); | |
33 | 32 |
34 // Called when an extension process wants to listen to process events. | 33 // Called when an extension process wants to listen to process events. |
35 void ListenerAdded(); | 34 void ListenerAdded(); |
36 | 35 |
37 // Called when an extension process with a listener exits or removes it. | 36 // Called when an extension process with a listener exits or removes it. |
38 void ListenerRemoved(); | 37 void ListenerRemoved(); |
39 | 38 |
40 // Called on the first invocation of extension API function. This will call | 39 // Called on the first invocation of extension API function. This will call |
41 // out to the Task Manager to start listening for notifications. Returns | 40 // out to the Task Manager to start listening for notifications. Returns |
42 // true if this was the first call and false if this has already been called. | 41 // true if this was the first call and false if this has already been called. |
43 void StartTaskManagerListening(); | 42 void StartTaskManagerListening(); |
44 | 43 |
45 bool is_task_manager_listening() { return task_manager_listening_; } | 44 bool is_task_manager_listening() { return task_manager_listening_; } |
46 int num_listeners() { return listeners_; } | 45 int num_listeners() { return listeners_; } |
47 | 46 |
48 private: | 47 private: |
49 friend struct DefaultSingletonTraits<ProcessesEventRouter>; | |
50 | |
51 ProcessesEventRouter(); | |
52 virtual ~ProcessesEventRouter(); | |
53 | |
54 // content::NotificationObserver implementation. | 48 // content::NotificationObserver implementation. |
55 virtual void Observe(int type, | 49 virtual void Observe(int type, |
56 const content::NotificationSource& source, | 50 const content::NotificationSource& source, |
57 const content::NotificationDetails& details) OVERRIDE; | 51 const content::NotificationDetails& details) OVERRIDE; |
58 | 52 |
59 // TaskManagerModelObserver methods. | 53 // TaskManagerModelObserver methods. |
60 virtual void OnItemsAdded(int start, int length) OVERRIDE; | 54 virtual void OnItemsAdded(int start, int length) OVERRIDE; |
61 virtual void OnModelChanged() OVERRIDE {} | 55 virtual void OnModelChanged() OVERRIDE {} |
62 virtual void OnItemsChanged(int start, int length) OVERRIDE; | 56 virtual void OnItemsChanged(int start, int length) OVERRIDE; |
63 virtual void OnItemsRemoved(int start, int length) OVERRIDE {} | 57 virtual void OnItemsRemoved(int start, int length) OVERRIDE {} |
64 virtual void OnItemsToBeRemoved(int start, int length) OVERRIDE; | 58 virtual void OnItemsToBeRemoved(int start, int length) OVERRIDE; |
65 | 59 |
66 // Internal helpers for processing notifications. | 60 // Internal helpers for processing notifications. |
67 void ProcessHangEvent(content::RenderWidgetHost* widget); | 61 void ProcessHangEvent(content::RenderWidgetHost* widget); |
68 void ProcessClosedEvent( | 62 void ProcessClosedEvent( |
69 content::RenderProcessHost* rph, | 63 content::RenderProcessHost* rph, |
70 content::RenderProcessHost::RendererClosedDetails* details); | 64 content::RenderProcessHost::RendererClosedDetails* details); |
71 | 65 |
72 void NotifyProfiles(const char* event_name, | 66 void DispatchEvent(const char* event_name, |
73 scoped_ptr<base::ListValue> event_args); | |
74 | |
75 void DispatchEvent(Profile* profile, | |
76 const char* event_name, | |
77 scoped_ptr<base::ListValue> event_args); | 67 scoped_ptr<base::ListValue> event_args); |
78 | 68 |
79 // Determines whether there is a registered listener for the specified event. | 69 // Determines whether there is a registered listener for the specified event. |
80 // It helps to avoid collecing data if no one is interested in it. | 70 // It helps to avoid collecing data if no one is interested in it. |
81 bool HasEventListeners(const std::string& event_name); | 71 bool HasEventListeners(const std::string& event_name); |
82 | 72 |
83 // Used for tracking registrations to process related notifications. | 73 // Used for tracking registrations to process related notifications. |
84 content::NotificationRegistrar registrar_; | 74 content::NotificationRegistrar registrar_; |
85 | 75 |
86 // Registered profiles. | 76 Profile* profile_; |
87 typedef std::set<Profile*> ProfileSet; | |
88 ProfileSet profiles_; | |
89 | 77 |
90 // TaskManager to observe for updates. | 78 // TaskManager to observe for updates. |
91 TaskManagerModel* model_; | 79 TaskManagerModel* model_; |
92 | 80 |
93 // Count of listeners, so we avoid sending updates if no one is interested. | 81 // Count of listeners, so we avoid sending updates if no one is interested. |
94 int listeners_; | 82 int listeners_; |
95 | 83 |
96 // Indicator whether we've initialized the Task Manager listeners. This is | 84 // Indicator whether we've initialized the Task Manager listeners. This is |
97 // done once for the lifetime of this object. | 85 // done once for the lifetime of this object. |
98 bool task_manager_listening_; | 86 bool task_manager_listening_; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 #if defined(ENABLE_TASK_MANAGER) | 169 #if defined(ENABLE_TASK_MANAGER) |
182 bool memory_; | 170 bool memory_; |
183 #endif | 171 #endif |
184 | 172 |
185 DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.getProcessInfo") | 173 DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.getProcessInfo") |
186 }; | 174 }; |
187 | 175 |
188 } // namespace extensions | 176 } // namespace extensions |
189 | 177 |
190 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__ | 178 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__ |
OLD | NEW |