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

Side by Side Diff: chrome/browser/extensions/extension_message_service.h

Issue 10787002: Moved ExtensionMessage* into extensions namespace (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Latest master for cq Created 8 years, 5 months 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
OLDNEW
(Empty)
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/compiler_specific.h"
13 #include "base/memory/linked_ptr.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16
17 class ExtensionHost;
18 class Profile;
19
20 namespace content {
21 class RenderProcessHost;
22 class WebContents;
23 }
24
25 namespace extensions {
26 class LazyBackgroundTaskQueue;
27 }
28
29 // This class manages message and event passing between renderer processes.
30 // It maintains a list of processes that are listening to events and a set of
31 // open channels.
32 //
33 // Messaging works this way:
34 // - An extension-owned script context (like a background page or a content
35 // script) adds an event listener to the "onConnect" event.
36 // - Another context calls "extension.connect()" to open a channel to the
37 // extension process, or an extension context calls "tabs.connect(tabId)" to
38 // open a channel to the content scripts for the given tab. The EMS notifies
39 // the target process/tab, which then calls the onConnect event in every
40 // context owned by the connecting extension in that process/tab.
41 // - Once the channel is established, either side can call postMessage to send
42 // a message to the opposite side of the channel, which may have multiple
43 // listeners.
44 //
45 // Terminology:
46 // channel: connection between two ports
47 // port: an IPC::Message::Process interface and an optional routing_id (in the
48 // case that the port is a tab). The Process is usually either a
49 // RenderProcessHost or a RenderViewHost.
50 class ExtensionMessageService : public content::NotificationObserver {
51 public:
52 // A messaging channel. Note that the opening port can be the same as the
53 // receiver, if an extension background page wants to talk to its tab (for
54 // example).
55 struct MessageChannel;
56 struct MessagePort;
57
58 // Allocates a pair of port ids.
59 // NOTE: this can be called from any thread.
60 static void AllocatePortIdPair(int* port1, int* port2);
61
62 explicit ExtensionMessageService(extensions::LazyBackgroundTaskQueue* queue);
63 virtual ~ExtensionMessageService();
64
65 // Given an extension's ID, opens a channel between the given renderer "port"
66 // and every listening context owned by that extension. |channel_name| is
67 // an optional identifier for use by extension developers.
68 void OpenChannelToExtension(
69 int source_process_id, int source_routing_id, int receiver_port_id,
70 const std::string& source_extension_id,
71 const std::string& target_extension_id,
72 const std::string& channel_name);
73
74 // Same as above, but opens a channel to the tab with the given ID. Messages
75 // are restricted to that tab, so if there are multiple tabs in that process,
76 // only the targeted tab will receive messages.
77 void OpenChannelToTab(
78 int source_process_id, int source_routing_id, int receiver_port_id,
79 int tab_id, const std::string& extension_id,
80 const std::string& channel_name);
81
82 // Closes the message channel associated with the given port, and notifies
83 // the other side.
84 void CloseChannel(int port_id, bool connection_error);
85
86 // Sends a message from a renderer to the given port.
87 void PostMessageFromRenderer(int port_id, const std::string& message);
88
89 private:
90 friend class MockExtensionMessageService;
91 struct OpenChannelParams;
92
93 // A map of channel ID to its channel object.
94 typedef std::map<int, MessageChannel*> MessageChannelMap;
95
96 // A map of channel ID to information about the extension that is waiting
97 // for that channel to open. Used for lazy background pages.
98 typedef std::string ExtensionID;
99 typedef std::pair<Profile*, ExtensionID> PendingChannel;
100 typedef std::map<int, PendingChannel> PendingChannelMap;
101
102 // Common among OpenChannel* variants.
103 bool OpenChannelImpl(const OpenChannelParams& params);
104
105 void CloseChannelImpl(MessageChannelMap::iterator channel_iter,
106 int port_id, bool connection_error,
107 bool notify_other_port);
108
109 // content::NotificationObserver interface.
110 virtual void Observe(int type,
111 const content::NotificationSource& source,
112 const content::NotificationDetails& details) OVERRIDE;
113
114 // A process that might be in our list of channels has closed.
115 void OnProcessClosed(content::RenderProcessHost* process);
116
117 // Potentially registers a pending task with the LazyBackgroundTaskQueue
118 // to open a channel. Returns true if a task was queued.
119 bool MaybeAddPendingOpenChannelTask(Profile* profile,
120 const OpenChannelParams& params);
121
122 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an
123 // ExtensionHost to its task callbacks, though some of our callbacks don't
124 // use that argument.
125 void PendingOpenChannel(const OpenChannelParams& params,
126 int source_process_id,
127 ExtensionHost* host);
128 void PendingCloseChannel(int port_id,
129 bool connection_error,
130 ExtensionHost* host) {
131 if (host)
132 CloseChannel(port_id, connection_error);
133 }
134 void PendingPostMessage(int port_id,
135 const std::string& message,
136 ExtensionHost* host) {
137 if (host)
138 PostMessageFromRenderer(port_id, message);
139 }
140
141 content::NotificationRegistrar registrar_;
142 MessageChannelMap channels_;
143 PendingChannelMap pending_channels_;
144
145 // Weak pointer. Guaranteed to outlive this class.
146 extensions::LazyBackgroundTaskQueue* lazy_background_task_queue_;
147
148 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService);
149 };
150
151 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_message_handler.cc ('k') | chrome/browser/extensions/extension_message_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698