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

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

Issue 10114015: Fix bug where transient pages would miss events dispatched while it was (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 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 | 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 #include "chrome/browser/extensions/extension_message_service.h" 5 #include "chrome/browser/extensions/extension_message_service.h"
6 6
7 #include "base/atomic_sequence_num.h" 7 #include "base/atomic_sequence_num.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/extensions/extension_host.h" 13 #include "chrome/browser/extensions/extension_host.h"
14 #include "chrome/browser/extensions/extension_process_manager.h" 14 #include "chrome/browser/extensions/extension_process_manager.h"
15 #include "chrome/browser/extensions/extension_service.h" 15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_tab_util.h" 17 #include "chrome/browser/extensions/extension_tab_util.h"
17 #include "chrome/browser/extensions/lazy_background_task_queue.h" 18 #include "chrome/browser/extensions/lazy_background_task_queue.h"
18 #include "chrome/browser/extensions/process_map.h" 19 #include "chrome/browser/extensions/process_map.h"
19 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/tab_contents/tab_util.h" 21 #include "chrome/browser/tab_contents/tab_util.h"
21 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 22 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
22 #include "chrome/common/chrome_notification_types.h" 23 #include "chrome/common/chrome_notification_types.h"
23 #include "chrome/common/chrome_view_type.h" 24 #include "chrome/common/chrome_view_type.h"
24 #include "chrome/common/extensions/extension.h" 25 #include "chrome/common/extensions/extension.h"
25 #include "chrome/common/extensions/extension_messages.h" 26 #include "chrome/common/extensions/extension_messages.h"
(...skipping 14 matching lines...) Expand all
40 41
41 // Port1 is always even, port2 is always odd. 42 // Port1 is always even, port2 is always odd.
42 #define IS_OPENER_PORT_ID(port_id) (((port_id) & 1) == 0) 43 #define IS_OPENER_PORT_ID(port_id) (((port_id) & 1) == 0)
43 44
44 // Change even to odd and vice versa, to get the other side of a given channel. 45 // Change even to odd and vice versa, to get the other side of a given channel.
45 #define GET_OPPOSITE_PORT_ID(source_port_id) ((source_port_id) ^ 1) 46 #define GET_OPPOSITE_PORT_ID(source_port_id) ((source_port_id) ^ 1)
46 47
47 struct ExtensionMessageService::MessagePort { 48 struct ExtensionMessageService::MessagePort {
48 content::RenderProcessHost* process; 49 content::RenderProcessHost* process;
49 int routing_id; 50 int routing_id;
50 explicit MessagePort(content::RenderProcessHost* process = NULL, 51 std::string extension_id;
51 int routing_id = MSG_ROUTING_CONTROL) 52 void* background_host_ptr; // used in IncrementLazyKeepaliveCount
52 : process(process), routing_id(routing_id) {} 53
54 MessagePort()
55 : process(NULL),
56 routing_id(MSG_ROUTING_CONTROL),
57 background_host_ptr(NULL) {}
58 MessagePort(content::RenderProcessHost* process,
59 int routing_id,
60 const std::string& extension_id)
61 : process(process),
62 routing_id(routing_id),
63 extension_id(extension_id),
64 background_host_ptr(NULL) {}
53 }; 65 };
54 66
55 struct ExtensionMessageService::MessageChannel { 67 struct ExtensionMessageService::MessageChannel {
56 ExtensionMessageService::MessagePort opener; 68 ExtensionMessageService::MessagePort opener;
57 ExtensionMessageService::MessagePort receiver; 69 ExtensionMessageService::MessagePort receiver;
58 std::string source_extension_id;
59 std::string target_extension_id;
60 }; 70 };
61 71
62 struct ExtensionMessageService::OpenChannelParams { 72 struct ExtensionMessageService::OpenChannelParams {
63 content::RenderProcessHost* source; 73 content::RenderProcessHost* source;
64 std::string tab_json; 74 std::string tab_json;
65 MessagePort receiver; 75 MessagePort receiver;
66 int receiver_port_id; 76 int receiver_port_id;
67 std::string source_extension_id; 77 std::string source_extension_id;
68 std::string target_extension_id; 78 std::string target_extension_id;
69 std::string channel_name; 79 std::string channel_name;
(...skipping 12 matching lines...) Expand all
82 source_extension_id(source_extension_id), 92 source_extension_id(source_extension_id),
83 target_extension_id(target_extension_id), 93 target_extension_id(target_extension_id),
84 channel_name(channel_name) {} 94 channel_name(channel_name) {}
85 }; 95 };
86 96
87 namespace { 97 namespace {
88 98
89 static base::StaticAtomicSequenceNumber g_next_channel_id; 99 static base::StaticAtomicSequenceNumber g_next_channel_id;
90 100
91 static void DispatchOnConnect(const ExtensionMessageService::MessagePort& port, 101 static void DispatchOnConnect(const ExtensionMessageService::MessagePort& port,
92 int dest_port_id, 102 int dest_port_id, const std::string& channel_name,
Yoyo Zhou 2012/04/19 21:47:13 nit: why this change?
Matt Perry 2012/04/19 22:21:53 Just bringing it up to style code: http://dev.chro
Yoyo Zhou 2012/04/19 22:32:55 It doesn't look particularly recommended for funct
Matt Perry 2012/04/19 22:35:00 See bullet point that starts "For function declara
93 const std::string& channel_name,
94 const std::string& tab_json, 103 const std::string& tab_json,
95 const std::string& source_extension_id, 104 const std::string& source_extension_id,
96 const std::string& target_extension_id) { 105 const std::string& target_extension_id) {
97 port.process->Send(new ExtensionMsg_DispatchOnConnect( 106 port.process->Send(new ExtensionMsg_DispatchOnConnect(
98 port.routing_id, dest_port_id, channel_name, 107 port.routing_id, dest_port_id, channel_name,
99 tab_json, source_extension_id, target_extension_id)); 108 tab_json, source_extension_id, target_extension_id));
100 } 109 }
101 110
102 static void DispatchOnDisconnect( 111 static void DispatchOnDisconnect(
103 const ExtensionMessageService::MessagePort& port, int source_port_id, 112 const ExtensionMessageService::MessagePort& port, int source_port_id,
(...skipping 13 matching lines...) Expand all
117 SiteInstance* site_instance = 126 SiteInstance* site_instance =
118 profile->GetExtensionProcessManager()->GetSiteInstanceForURL( 127 profile->GetExtensionProcessManager()->GetSiteInstanceForURL(
119 Extension::GetBaseURLFromExtensionId(extension_id)); 128 Extension::GetBaseURLFromExtensionId(extension_id));
120 129
121 if (!site_instance->HasProcess()) 130 if (!site_instance->HasProcess())
122 return NULL; 131 return NULL;
123 132
124 return site_instance->GetProcess(); 133 return site_instance->GetProcess();
125 } 134 }
126 135
127 static void IncrementLazyKeepaliveCount(content::RenderProcessHost* process, 136 static void IncrementLazyKeepaliveCount(
128 const std::string& extension_id) { 137 ExtensionMessageService::MessagePort* port) {
129 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext()); 138 Profile* profile =
130 const Extension* extension = profile->GetExtensionService()->extensions()-> 139 Profile::FromBrowserContext(port->process->GetBrowserContext());
131 GetByID(extension_id); 140 ExtensionProcessManager* pm =
132 if (extension) 141 ExtensionSystem::Get(profile)->process_manager();
133 profile->GetExtensionProcessManager()->IncrementLazyKeepaliveCount( 142 ExtensionHost* host = pm->GetBackgroundHostForExtension(port->extension_id);
134 extension); 143 if (host && host->extension()->has_lazy_background_page())
144 pm->IncrementLazyKeepaliveCount(host->extension());
145
146 // Keep track of the background host, so when we decrement, we only do so if
147 // the host hasn't reloaded.
148 port->background_host_ptr = host;
135 } 149 }
136 150
137 static void DecrementLazyKeepaliveCount(content::RenderProcessHost* process, 151 static void DecrementLazyKeepaliveCount(
138 const std::string& extension_id) { 152 ExtensionMessageService::MessagePort* port) {
139 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext()); 153 Profile* profile =
140 const Extension* extension = profile->GetExtensionService()->extensions()-> 154 Profile::FromBrowserContext(port->process->GetBrowserContext());
141 GetByID(extension_id); 155 ExtensionProcessManager* pm =
142 if (extension) 156 ExtensionSystem::Get(profile)->process_manager();
143 profile->GetExtensionProcessManager()->DecrementLazyKeepaliveCount( 157 ExtensionHost* host = pm->GetBackgroundHostForExtension(port->extension_id);
144 extension); 158 if (host && host == port->background_host_ptr)
159 pm->DecrementLazyKeepaliveCount(host->extension());
145 } 160 }
146 161
147 } // namespace 162 } // namespace
148 163
149 // static 164 // static
150 void ExtensionMessageService::AllocatePortIdPair(int* port1, int* port2) { 165 void ExtensionMessageService::AllocatePortIdPair(int* port1, int* port2) {
151 int channel_id = g_next_channel_id.GetNext(); 166 int channel_id = g_next_channel_id.GetNext();
152 int port1_id = channel_id * 2; 167 int port1_id = channel_id * 2;
153 int port2_id = channel_id * 2 + 1; 168 int port2_id = channel_id * 2 + 1;
154 169
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 content::RenderProcessHost::FromID(source_process_id); 203 content::RenderProcessHost::FromID(source_process_id);
189 if (!source) 204 if (!source)
190 return; 205 return;
191 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); 206 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext());
192 207
193 // Note: we use the source's profile here. If the source is an incognito 208 // Note: we use the source's profile here. If the source is an incognito
194 // process, we will use the incognito EPM to find the right extension process, 209 // process, we will use the incognito EPM to find the right extension process,
195 // which depends on whether the extension uses spanning or split mode. 210 // which depends on whether the extension uses spanning or split mode.
196 MessagePort receiver( 211 MessagePort receiver(
197 GetExtensionProcess(profile, target_extension_id), 212 GetExtensionProcess(profile, target_extension_id),
198 MSG_ROUTING_CONTROL); 213 MSG_ROUTING_CONTROL,
214 target_extension_id);
199 WebContents* source_contents = tab_util::GetWebContentsByID( 215 WebContents* source_contents = tab_util::GetWebContentsByID(
200 source_process_id, source_routing_id); 216 source_process_id, source_routing_id);
201 217
202 // Include info about the opener's tab (if it was a tab). 218 // Include info about the opener's tab (if it was a tab).
203 std::string tab_json = "null"; 219 std::string tab_json = "null";
204 if (source_contents) { 220 if (source_contents) {
205 scoped_ptr<DictionaryValue> tab_value( 221 scoped_ptr<DictionaryValue> tab_value(
206 ExtensionTabUtil::CreateTabValue(source_contents)); 222 ExtensionTabUtil::CreateTabValue(source_contents));
207 base::JSONWriter::Write(tab_value.get(), &tab_json); 223 base::JSONWriter::Write(tab_value.get(), &tab_json);
208 } 224 }
(...skipping 21 matching lines...) Expand all
230 return; 246 return;
231 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); 247 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext());
232 248
233 TabContentsWrapper* contents = NULL; 249 TabContentsWrapper* contents = NULL;
234 MessagePort receiver; 250 MessagePort receiver;
235 if (ExtensionTabUtil::GetTabById(tab_id, profile, true, 251 if (ExtensionTabUtil::GetTabById(tab_id, profile, true,
236 NULL, NULL, &contents, NULL)) { 252 NULL, NULL, &contents, NULL)) {
237 receiver.process = contents->web_contents()->GetRenderProcessHost(); 253 receiver.process = contents->web_contents()->GetRenderProcessHost();
238 receiver.routing_id = 254 receiver.routing_id =
239 contents->web_contents()->GetRenderViewHost()->GetRoutingID(); 255 contents->web_contents()->GetRenderViewHost()->GetRoutingID();
256 receiver.extension_id = extension_id;
240 } 257 }
241 258
242 if (contents && contents->web_contents()->GetController().NeedsReload()) { 259 if (contents && contents->web_contents()->GetController().NeedsReload()) {
243 // The tab isn't loaded yet. Don't attempt to connect. Treat this as a 260 // The tab isn't loaded yet. Don't attempt to connect. Treat this as a
244 // disconnect. 261 // disconnect.
245 DispatchOnDisconnect(MessagePort(source, MSG_ROUTING_CONTROL), 262 DispatchOnDisconnect(MessagePort(source, MSG_ROUTING_CONTROL, extension_id),
246 GET_OPPOSITE_PORT_ID(receiver_port_id), true); 263 GET_OPPOSITE_PORT_ID(receiver_port_id), true);
247 return; 264 return;
248 } 265 }
249 266
250 WebContents* source_contents = tab_util::GetWebContentsByID( 267 WebContents* source_contents = tab_util::GetWebContentsByID(
251 source_process_id, source_routing_id); 268 source_process_id, source_routing_id);
252 269
253 // Include info about the opener's tab (if it was a tab). 270 // Include info about the opener's tab (if it was a tab).
254 std::string tab_json = "null"; 271 std::string tab_json = "null";
255 if (source_contents) { 272 if (source_contents) {
256 scoped_ptr<DictionaryValue> tab_value( 273 scoped_ptr<DictionaryValue> tab_value(
257 ExtensionTabUtil::CreateTabValue(source_contents)); 274 ExtensionTabUtil::CreateTabValue(source_contents));
258 base::JSONWriter::Write(tab_value.get(), &tab_json); 275 base::JSONWriter::Write(tab_value.get(), &tab_json);
259 } 276 }
260 277
261 OpenChannelParams params(source, tab_json, receiver, receiver_port_id, 278 OpenChannelParams params(source, tab_json, receiver, receiver_port_id,
262 extension_id, extension_id, channel_name); 279 extension_id, extension_id, channel_name);
263 OpenChannelImpl(params); 280 OpenChannelImpl(params);
264 } 281 }
265 282
266 bool ExtensionMessageService::OpenChannelImpl(const OpenChannelParams& params) { 283 bool ExtensionMessageService::OpenChannelImpl(const OpenChannelParams& params) {
267 if (!params.source) 284 if (!params.source)
268 return false; // Closed while in flight. 285 return false; // Closed while in flight.
269 286
270 if (!params.receiver.process) { 287 if (!params.receiver.process) {
271 // Treat it as a disconnect. 288 // Treat it as a disconnect.
272 DispatchOnDisconnect(MessagePort(params.source, MSG_ROUTING_CONTROL), 289 DispatchOnDisconnect(MessagePort(params.source, MSG_ROUTING_CONTROL, ""),
273 GET_OPPOSITE_PORT_ID(params.receiver_port_id), true); 290 GET_OPPOSITE_PORT_ID(params.receiver_port_id), true);
274 return false; 291 return false;
275 } 292 }
276 293
277 // Add extra paranoid CHECKs, since we have crash reports of this being NULL. 294 // Add extra paranoid CHECKs, since we have crash reports of this being NULL.
278 // http://code.google.com/p/chromium/issues/detail?id=19067 295 // http://code.google.com/p/chromium/issues/detail?id=19067
279 CHECK(params.receiver.process); 296 CHECK(params.receiver.process);
280 297
281 MessageChannel* channel(new MessageChannel); 298 MessageChannel* channel(new MessageChannel);
282 channel->opener = MessagePort(params.source, MSG_ROUTING_CONTROL); 299 channel->opener = MessagePort(params.source, MSG_ROUTING_CONTROL,
300 params.source_extension_id);
283 channel->receiver = params.receiver; 301 channel->receiver = params.receiver;
284 channel->source_extension_id = params.source_extension_id;
285 channel->target_extension_id = params.target_extension_id;
286 302
287 CHECK(params.receiver.process); 303 CHECK(params.receiver.process);
288 304
289 int channel_id = GET_CHANNEL_ID(params.receiver_port_id); 305 int channel_id = GET_CHANNEL_ID(params.receiver_port_id);
290 CHECK(channels_.find(channel_id) == channels_.end()); 306 CHECK(channels_.find(channel_id) == channels_.end());
291 channels_[channel_id] = channel; 307 channels_[channel_id] = channel;
292 pending_channels_.erase(channel_id); 308 pending_channels_.erase(channel_id);
293 309
294 CHECK(params.receiver.process); 310 CHECK(params.receiver.process);
295 311
296 // Send the connect event to the receiver. Give it the opener's port ID (the 312 // Send the connect event to the receiver. Give it the opener's port ID (the
297 // opener has the opposite port ID). 313 // opener has the opposite port ID).
298 DispatchOnConnect(params.receiver, params.receiver_port_id, 314 DispatchOnConnect(params.receiver, params.receiver_port_id,
299 params.channel_name, params.tab_json, 315 params.channel_name, params.tab_json,
300 params.source_extension_id, params.target_extension_id); 316 params.source_extension_id, params.target_extension_id);
301 317
302 // Keep both ends of the channel alive until the channel is closed. 318 // Keep both ends of the channel alive until the channel is closed.
303 IncrementLazyKeepaliveCount(channel->opener.process, 319 IncrementLazyKeepaliveCount(&channel->opener);
304 channel->source_extension_id); 320 IncrementLazyKeepaliveCount(&channel->receiver);
305 IncrementLazyKeepaliveCount(channel->receiver.process,
306 channel->target_extension_id);
307 return true; 321 return true;
308 } 322 }
309 323
310 void ExtensionMessageService::CloseChannel(int port_id, bool connection_error) { 324 void ExtensionMessageService::CloseChannel(int port_id, bool connection_error) {
311 // Note: The channel might be gone already, if the other side closed first. 325 // Note: The channel might be gone already, if the other side closed first.
312 int channel_id = GET_CHANNEL_ID(port_id); 326 int channel_id = GET_CHANNEL_ID(port_id);
313 MessageChannelMap::iterator it = channels_.find(channel_id); 327 MessageChannelMap::iterator it = channels_.find(channel_id);
314 if (it == channels_.end()) { 328 if (it == channels_.end()) {
315 PendingChannelMap::iterator pending = pending_channels_.find(channel_id); 329 PendingChannelMap::iterator pending = pending_channels_.find(channel_id);
316 if (pending != pending_channels_.end()) { 330 if (pending != pending_channels_.end()) {
(...skipping 14 matching lines...) Expand all
331 345
332 // Notify the other side. 346 // Notify the other side.
333 if (notify_other_port) { 347 if (notify_other_port) {
334 const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ? 348 const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ?
335 channel->receiver : channel->opener; 349 channel->receiver : channel->opener;
336 DispatchOnDisconnect(port, GET_OPPOSITE_PORT_ID(closing_port_id), 350 DispatchOnDisconnect(port, GET_OPPOSITE_PORT_ID(closing_port_id),
337 connection_error); 351 connection_error);
338 } 352 }
339 353
340 // Balance the addrefs in OpenChannelImpl. 354 // Balance the addrefs in OpenChannelImpl.
341 DecrementLazyKeepaliveCount(channel->opener.process, 355 DecrementLazyKeepaliveCount(&channel->opener);
342 channel->source_extension_id); 356 DecrementLazyKeepaliveCount(&channel->receiver);
343 DecrementLazyKeepaliveCount(channel->receiver.process,
344 channel->target_extension_id);
345 357
346 delete channel_iter->second; 358 delete channel_iter->second;
347 channels_.erase(channel_iter); 359 channels_.erase(channel_iter);
348 } 360 }
349 361
350 void ExtensionMessageService::PostMessageFromRenderer( 362 void ExtensionMessageService::PostMessageFromRenderer(
351 int source_port_id, const std::string& message) { 363 int source_port_id, const std::string& message) {
352 int channel_id = GET_CHANNEL_ID(source_port_id); 364 int channel_id = GET_CHANNEL_ID(source_port_id);
353 MessageChannelMap::iterator iter = channels_.find(channel_id); 365 MessageChannelMap::iterator iter = channels_.find(channel_id);
354 if (iter == channels_.end()) { 366 if (iter == channels_.end()) {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 if (!host) 457 if (!host)
446 return; // TODO(mpcomplete): notify source of disconnect? 458 return; // TODO(mpcomplete): notify source of disconnect?
447 459
448 // Re-lookup the source process since it may no longer be valid. 460 // Re-lookup the source process since it may no longer be valid.
449 OpenChannelParams params = params_in; 461 OpenChannelParams params = params_in;
450 params.source = content::RenderProcessHost::FromID(source_process_id); 462 params.source = content::RenderProcessHost::FromID(source_process_id);
451 if (!params.source) 463 if (!params.source)
452 return; 464 return;
453 465
454 params.receiver = MessagePort(host->render_process_host(), 466 params.receiver = MessagePort(host->render_process_host(),
455 MSG_ROUTING_CONTROL); 467 MSG_ROUTING_CONTROL,
468 params.target_extension_id);
456 OpenChannelImpl(params); 469 OpenChannelImpl(params);
457 } 470 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698