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

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

Issue 11359081: Lazy initialization for ProcessesEventRouter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixor Created 8 years, 1 month 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 | Annotate | Revision Log
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 <set>
9 #include <string> 9 #include <string>
10 10
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/extensions/event_router.h"
11 #include "chrome/browser/extensions/extension_function.h" 13 #include "chrome/browser/extensions/extension_function.h"
14 #include "chrome/browser/profiles/profile_keyed_service.h"
12 #include "chrome/browser/task_manager/task_manager.h" 15 #include "chrome/browser/task_manager/task_manager.h"
13 #include "content/public/browser/notification_registrar.h" 16 #include "content/public/browser/notification_registrar.h"
14 #include "content/public/browser/render_process_host.h" 17 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_widget_host.h" 18 #include "content/public/browser/render_widget_host.h"
16 19
20 class Profile;
21
17 namespace base { 22 namespace base {
18 class ListValue; 23 class ListValue;
19 } 24 }
20 25
21 namespace extensions { 26 namespace extensions {
22 27
23 // Observes the Task Manager and routes the notifications as events to the 28 // Observes the Task Manager and routes the notifications as events to the
24 // extension system. 29 // extension system.
25 class ProcessesEventRouter : public TaskManagerModelObserver, 30 class ProcessesEventRouter : public TaskManagerModelObserver,
26 public content::NotificationObserver { 31 public content::NotificationObserver {
27 public: 32 public:
28 // Single instance of the event router. 33 explicit ProcessesEventRouter(Profile* profile);
29 static ProcessesEventRouter* GetInstance(); 34 virtual ~ProcessesEventRouter();
30
31 // Safe to call multiple times.
32 void ObserveProfile(Profile* profile);
33 35
34 // Called when an extension process wants to listen to process events. 36 // Called when an extension process wants to listen to process events.
35 void ListenerAdded(); 37 void ListenerAdded();
36 38
37 // Called when an extension process with a listener exits or removes it. 39 // Called when an extension process with a listener exits or removes it.
38 void ListenerRemoved(); 40 void ListenerRemoved();
39 41
40 // Called on the first invocation of extension API function. This will call 42 // Called on the first invocation of extension API function. This will call
41 // out to the Task Manager to start listening for notifications. Returns 43 // 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. 44 // true if this was the first call and false if this has already been called.
43 void StartTaskManagerListening(); 45 void StartTaskManagerListening();
44 46
45 bool is_task_manager_listening() { return task_manager_listening_; } 47 bool is_task_manager_listening() { return task_manager_listening_; }
46 int num_listeners() { return listeners_; } 48 int num_listeners() { return listeners_; }
47 49
48 private: 50 private:
49 friend struct DefaultSingletonTraits<ProcessesEventRouter>;
50
51 ProcessesEventRouter();
52 virtual ~ProcessesEventRouter();
53
54 // content::NotificationObserver implementation. 51 // content::NotificationObserver implementation.
55 virtual void Observe(int type, 52 virtual void Observe(int type,
56 const content::NotificationSource& source, 53 const content::NotificationSource& source,
57 const content::NotificationDetails& details) OVERRIDE; 54 const content::NotificationDetails& details) OVERRIDE;
58 55
59 // TaskManagerModelObserver methods. 56 // TaskManagerModelObserver methods.
60 virtual void OnItemsAdded(int start, int length) OVERRIDE; 57 virtual void OnItemsAdded(int start, int length) OVERRIDE;
61 virtual void OnModelChanged() OVERRIDE {} 58 virtual void OnModelChanged() OVERRIDE {}
62 virtual void OnItemsChanged(int start, int length) OVERRIDE; 59 virtual void OnItemsChanged(int start, int length) OVERRIDE;
63 virtual void OnItemsRemoved(int start, int length) OVERRIDE {} 60 virtual void OnItemsRemoved(int start, int length) OVERRIDE {}
64 virtual void OnItemsToBeRemoved(int start, int length) OVERRIDE; 61 virtual void OnItemsToBeRemoved(int start, int length) OVERRIDE;
65 62
66 // Internal helpers for processing notifications. 63 // Internal helpers for processing notifications.
67 void ProcessHangEvent(content::RenderWidgetHost* widget); 64 void ProcessHangEvent(content::RenderWidgetHost* widget);
68 void ProcessClosedEvent( 65 void ProcessClosedEvent(
69 content::RenderProcessHost* rph, 66 content::RenderProcessHost* rph,
70 content::RenderProcessHost::RendererClosedDetails* details); 67 content::RenderProcessHost::RendererClosedDetails* details);
71 68
72 void NotifyProfiles(const char* event_name, 69 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); 70 scoped_ptr<base::ListValue> event_args);
78 71
79 // Determines whether there is a registered listener for the specified event. 72 // 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. 73 // It helps to avoid collecing data if no one is interested in it.
81 bool HasEventListeners(const std::string& event_name); 74 bool HasEventListeners(const std::string& event_name);
82 75
83 // Used for tracking registrations to process related notifications. 76 // Used for tracking registrations to process related notifications.
84 content::NotificationRegistrar registrar_; 77 content::NotificationRegistrar registrar_;
85 78
86 // Registered profiles. 79 Profile* profile_;
87 typedef std::set<Profile*> ProfileSet;
88 ProfileSet profiles_;
89 80
90 // TaskManager to observe for updates. 81 // TaskManager to observe for updates.
91 TaskManagerModel* model_; 82 TaskManagerModel* model_;
92 83
93 // Count of listeners, so we avoid sending updates if no one is interested. 84 // Count of listeners, so we avoid sending updates if no one is interested.
94 int listeners_; 85 int listeners_;
95 86
96 // Indicator whether we've initialized the Task Manager listeners. This is 87 // Indicator whether we've initialized the Task Manager listeners. This is
97 // done once for the lifetime of this object. 88 // done once for the lifetime of this object.
98 bool task_manager_listening_; 89 bool task_manager_listening_;
99 90
100 DISALLOW_COPY_AND_ASSIGN(ProcessesEventRouter); 91 DISALLOW_COPY_AND_ASSIGN(ProcessesEventRouter);
101 }; 92 };
102 93
94 // The profile-keyed service that manages the processes extension API.
95 class ProcessesAPI : public ProfileKeyedService,
96 public EventRouter::Observer {
97 public:
98 explicit ProcessesAPI(Profile* profile);
99 virtual ~ProcessesAPI();
100
101 // Convenience method to get the ProcessesAPI for a profile.
102 static ProcessesAPI* Get(Profile* profile);
103
104 ProcessesEventRouter* processes_event_router() {
105 return processes_event_router_.get();
106 }
107
108 // EventRouter::Observer implementation.
109 virtual void OnListenerAdded(const std::string& event_name) OVERRIDE;
110 virtual void OnListenerRemoved(const std::string& event_name) OVERRIDE;
111
112 private:
113 Profile* profile_;
114
115 // TODO(yoz): Currently, the event router cannot be started lazily
116 // because some API functions use it. It ought to be possible to fix
117 // by moving some state to ProcessesAPI from the event router.
Matt Perry 2012/11/09 00:23:34 Couldn't you have the accessor lazily create the e
Yoyo Zhou 2012/11/09 00:35:55 Duh. Thanks.
118 scoped_ptr<extensions::ProcessesEventRouter> processes_event_router_;
119 };
103 120
104 // This extension function returns the Process object for the renderer process 121 // This extension function returns the Process object for the renderer process
105 // currently in use by the specified Tab. 122 // currently in use by the specified Tab.
106 class GetProcessIdForTabFunction : public AsyncExtensionFunction, 123 class GetProcessIdForTabFunction : public AsyncExtensionFunction,
107 public content::NotificationObserver { 124 public content::NotificationObserver {
108 public: 125 public:
109 GetProcessIdForTabFunction(); 126 GetProcessIdForTabFunction();
110 127
111 private: 128 private:
112 virtual ~GetProcessIdForTabFunction() {} 129 virtual ~GetProcessIdForTabFunction() {}
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 #if defined(ENABLE_TASK_MANAGER) 198 #if defined(ENABLE_TASK_MANAGER)
182 bool memory_; 199 bool memory_;
183 #endif 200 #endif
184 201
185 DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.getProcessInfo") 202 DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.getProcessInfo")
186 }; 203 };
187 204
188 } // namespace extensions 205 } // namespace extensions
189 206
190 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__ 207 #endif // CHROME_BROWSER_EXTENSIONS_API_PROCESSES_PROCESSES_API_H__
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/processes/processes_api.cc » ('j') | chrome/browser/extensions/extension_system.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698