OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_EXTENSION_MESSAGE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 22 matching lines...) Expand all Loading... |
33 static ExtensionMessageService* GetInstance(URLRequestContext* context); | 33 static ExtensionMessageService* GetInstance(URLRequestContext* context); |
34 | 34 |
35 ExtensionMessageService(); | 35 ExtensionMessageService(); |
36 | 36 |
37 // --- UI thread only: | 37 // --- UI thread only: |
38 | 38 |
39 // Register an extension and its corresponding renderer process. | 39 // Register an extension and its corresponding renderer process. |
40 void RegisterExtension(const std::string& extension_id, | 40 void RegisterExtension(const std::string& extension_id, |
41 int render_process_id); | 41 int render_process_id); |
42 | 42 |
| 43 // Add or remove |render_process_pid| as a listener for |event_name|. |
| 44 void AddEventListener(std::string event_name, int render_process_id); |
| 45 void RemoveEventListener(std::string event_name, int render_process_id); |
| 46 |
43 // --- IO thread only: | 47 // --- IO thread only: |
44 | 48 |
45 // Given an extension's ID, opens a channel between the given renderer "port" | 49 // Given an extension's ID, opens a channel between the given renderer "port" |
46 // and that extension. Returns a channel ID to be used for posting messages | 50 // and that extension. Returns a channel ID to be used for posting messages |
47 // between the processes, or -1 if the extension doesn't exist. | 51 // between the processes, or -1 if the extension doesn't exist. |
48 int OpenChannelToExtension(const std::string& extension_id, | 52 int OpenChannelToExtension(const std::string& extension_id, |
49 ResourceMessageFilter* source); | 53 ResourceMessageFilter* source); |
50 | 54 |
51 // Sends a message from a renderer to the given port. | 55 // Sends a message from a renderer to the given port. |
52 void PostMessageFromRenderer(int port_id, const std::string& message, | 56 void PostMessageFromRenderer(int port_id, const std::string& message, |
(...skipping 16 matching lines...) Expand all Loading... |
69 private: | 73 private: |
70 // A map of extension ID to the render_process_id that the extension lives in. | 74 // A map of extension ID to the render_process_id that the extension lives in. |
71 typedef std::map<std::string, int> ProcessIDMap; | 75 typedef std::map<std::string, int> ProcessIDMap; |
72 ProcessIDMap process_ids_; | 76 ProcessIDMap process_ids_; |
73 | 77 |
74 // Protects the process_ids map, since it can be accessed on the IO thread | 78 // Protects the process_ids map, since it can be accessed on the IO thread |
75 // or UI thread. Be careful not to hold this lock when calling external | 79 // or UI thread. Be careful not to hold this lock when calling external |
76 // code (especially sending messages) to avoid deadlock. | 80 // code (especially sending messages) to avoid deadlock. |
77 Lock process_ids_lock_; | 81 Lock process_ids_lock_; |
78 | 82 |
| 83 // A map between an event name and a set of process id's that are listening |
| 84 // to that event. |
| 85 typedef std::map<std::string, std::set<int> > ListenerMap; |
| 86 ListenerMap listeners_; |
| 87 |
| 88 // Protects listeners_ map, since it can be accessed from either the IO or |
| 89 // UI thread. Be careful not to hold this lock when calling external code |
| 90 // (especially sending messages) to avoid deadlock. |
| 91 Lock listener_lock_; |
| 92 |
79 // --- IO thread only: | 93 // --- IO thread only: |
80 | 94 |
81 // The connection between two renderers. | 95 // The connection between two renderers. |
82 struct MessageChannel { | 96 struct MessageChannel { |
83 ResourceMessageFilter* port1; | 97 ResourceMessageFilter* port1; |
84 ResourceMessageFilter* port2; | 98 ResourceMessageFilter* port2; |
85 }; | 99 }; |
86 | 100 |
87 // A map of channel ID to its channel object. | 101 // A map of channel ID to its channel object. |
88 typedef std::map<int, MessageChannel> MessageChannelMap; | 102 typedef std::map<int, MessageChannel> MessageChannelMap; |
89 MessageChannelMap channels_; | 103 MessageChannelMap channels_; |
90 | 104 |
91 // For generating unique channel IDs. | 105 // For generating unique channel IDs. |
92 int next_port_id_; | 106 int next_port_id_; |
93 | 107 |
94 // A map of render_process_id to its corresponding message filter, which we | 108 // A map of render_process_id to its corresponding message filter, which we |
95 // use for sending messages. | 109 // use for sending messages. |
96 typedef std::map<int, ResourceMessageFilter*> RendererMap; | 110 typedef std::map<int, ResourceMessageFilter*> RendererMap; |
97 RendererMap renderers_; | 111 RendererMap renderers_; |
98 | 112 |
99 // A unique list of renderers that we are aware of. | 113 // A unique list of renderers that we are aware of. |
100 std::set<ResourceMessageFilter*> renderers_unique_; | 114 std::set<ResourceMessageFilter*> renderers_unique_; |
101 | 115 |
102 // Set to true when we start observing this notification. | 116 // Set to true when we start observing this notification. |
103 bool observing_renderer_shutdown_; | 117 bool observing_renderer_shutdown_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService); |
104 }; | 120 }; |
105 | 121 |
106 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ | 122 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ |
OLD | NEW |