diff options
Diffstat (limited to 'chrome/browser/extensions/extension_message_service.h')
-rwxr-xr-x | chrome/browser/extensions/extension_message_service.h | 65 |
1 files changed, 40 insertions, 25 deletions
diff --git a/chrome/browser/extensions/extension_message_service.h b/chrome/browser/extensions/extension_message_service.h index 1b4040d..0020a30 100755 --- a/chrome/browser/extensions/extension_message_service.h +++ b/chrome/browser/extensions/extension_message_service.h @@ -8,13 +8,14 @@ #include <map> #include <string> +#include "base/lock.h" + class ExtensionView; class ResourceMessageFilter; -// This class manages message passing to and from extension processes. It -// maintains a list of available extensions, as well as a set of open channels. -// It should only be accessed on the IO thread, with the exception of -// RegisterExtensionView(). +// This class manages message passing between renderer processes. It maintains +// a list of available extensions and which renderers each lives in, as well as +// a set of open channels. // // Terminology: // channel: connection between two ports (one of which belongs to an extension) @@ -27,39 +28,53 @@ class ExtensionMessageService { ExtensionMessageService(); - // Registers an extension so that it can be referenced by its ID. This method - // should only be used by the UI thread. - void RegisterExtensionView(ExtensionView* view); + // --- UI thread only: + + // Register an extension and its corresponding renderer process. + void RegisterExtension(const std::string& extension_id, + int render_process_id); + + // --- IO thread only: // Given an extension's ID, opens a channel between the given renderer "port" // and that extension. Returns a channel ID to be used for posting messages // between the processes, or -1 if the extension doesn't exist. int OpenChannelToExtension(const std::string& extension_id, - ResourceMessageFilter* renderer_port); + ResourceMessageFilter* source); + + // Sends a message from a renderer to the given port. + void PostMessageFromRenderer(int port_id, const std::string& message, + ResourceMessageFilter* source); - // Sends a message to the extension via the given channel. - void PostMessageToExtension(int channel_id, const std::string& message); + // --- UI or IO thread: + + // Called to let us know that a renderer has been started. + void RendererReady(ResourceMessageFilter* port); // Called to let us know that a renderer is going away. - void RendererShutdown(ResourceMessageFilter* renderer_port); + void RendererShutdown(ResourceMessageFilter* port); + private: - class ExtensionFilter; - friend class ExtensionFilter; + // A map of extension ID to the render_process_id that the extension lives in. + typedef std::map<std::string, int> ProcessIDMap; + ProcessIDMap process_ids_; + + // A map of render_process_id to its corresponding message filter, which we + // use for sending messages. + typedef std::map<int, ResourceMessageFilter*> RendererMap; + RendererMap renderers_; - // Called when our ExtensionFilter is ready/going away. - void OnExtensionRegistered(ExtensionFilter* extension); - void OnExtensionUnregistered(ExtensionFilter* extension); + // Protects the two maps above, since each can be accessed on the IO thread + // or UI thread. Be careful not to hold this lock when calling external + // code (especially sending messages) to avoid deadlock. + Lock renderers_lock_; - // A map of extension ID to the extension port to communicate through. - // TODO(mpcomplete): Handle the case where there's multiple ExtensionViews - // in a given extension. - typedef std::map<std::string, ExtensionFilter*> ExtensionMap; - ExtensionMap extensions_; + // --- IO thread only: - // The connection between the renderer and extension. + // The connection between two renderers. struct MessageChannel { - ExtensionFilter* extension_port; - ResourceMessageFilter* renderer_port; + ResourceMessageFilter* port1; + ResourceMessageFilter* port2; }; // A map of channel ID to its channel object. @@ -67,7 +82,7 @@ class ExtensionMessageService { MessageChannelMap channels_; // For generating unique channel IDs. - int next_channel_id_; + int next_port_id_; }; #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ |