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 #include "chrome/browser/extensions/extension_message_service.h" | 5 #include "chrome/browser/extensions/extension_message_service.h" |
6 | 6 |
7 #include "base/json_writer.h" | 7 #include "base/json_writer.h" |
8 #include "base/singleton.h" | 8 #include "base/singleton.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 | 360 |
361 return port1_id; | 361 return port1_id; |
362 } | 362 } |
363 | 363 |
364 void ExtensionMessageService::CloseChannel(int port_id) { | 364 void ExtensionMessageService::CloseChannel(int port_id) { |
365 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 365 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
366 | 366 |
367 // Note: The channel might be gone already, if the other side closed first. | 367 // Note: The channel might be gone already, if the other side closed first. |
368 MessageChannelMap::iterator it = channels_.find(GET_CHANNEL_ID(port_id)); | 368 MessageChannelMap::iterator it = channels_.find(GET_CHANNEL_ID(port_id)); |
369 if (it != channels_.end()) | 369 if (it != channels_.end()) |
370 CloseChannelImpl(it, port_id); | 370 CloseChannelImpl(it, port_id, true); |
371 } | 371 } |
372 | 372 |
373 void ExtensionMessageService::CloseChannelImpl( | 373 void ExtensionMessageService::CloseChannelImpl( |
374 MessageChannelMap::iterator channel_iter, int closing_port_id) { | 374 MessageChannelMap::iterator channel_iter, int closing_port_id, |
| 375 bool notify_other_port) { |
375 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 376 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
376 | 377 |
377 // Notify the other side. | 378 // Notify the other side. |
378 const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ? | 379 const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ? |
379 channel_iter->second->receiver : channel_iter->second->opener; | 380 channel_iter->second->receiver : channel_iter->second->opener; |
380 | 381 |
381 DispatchOnDisconnect(port, GET_OPPOSITE_PORT_ID(closing_port_id)); | 382 if (notify_other_port) |
| 383 DispatchOnDisconnect(port, GET_OPPOSITE_PORT_ID(closing_port_id)); |
382 channels_.erase(channel_iter); | 384 channels_.erase(channel_iter); |
383 } | 385 } |
384 | 386 |
385 | |
386 void ExtensionMessageService::PostMessageFromRenderer( | 387 void ExtensionMessageService::PostMessageFromRenderer( |
387 int source_port_id, const std::string& message) { | 388 int source_port_id, const std::string& message) { |
388 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 389 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
389 | 390 |
390 MessageChannelMap::iterator iter = | 391 MessageChannelMap::iterator iter = |
391 channels_.find(GET_CHANNEL_ID(source_port_id)); | 392 channels_.find(GET_CHANNEL_ID(source_port_id)); |
392 if (iter == channels_.end()) | 393 if (iter == channels_.end()) |
393 return; | 394 return; |
394 | 395 |
395 // Figure out which port the ID corresponds to. | 396 // Figure out which port the ID corresponds to. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 return; | 450 return; |
450 } | 451 } |
451 } | 452 } |
452 | 453 |
453 void ExtensionMessageService::OnSenderClosed(IPC::Message::Sender* sender) { | 454 void ExtensionMessageService::OnSenderClosed(IPC::Message::Sender* sender) { |
454 // Close any channels that share this renderer. We notify the opposite | 455 // Close any channels that share this renderer. We notify the opposite |
455 // port that his pair has closed. | 456 // port that his pair has closed. |
456 for (MessageChannelMap::iterator it = channels_.begin(); | 457 for (MessageChannelMap::iterator it = channels_.begin(); |
457 it != channels_.end(); ) { | 458 it != channels_.end(); ) { |
458 MessageChannelMap::iterator current = it++; | 459 MessageChannelMap::iterator current = it++; |
| 460 // If both sides are the same renderer, and it is closing, there is no |
| 461 // "other" port, so there's no need to notify it. |
| 462 bool notify_other_port = |
| 463 current->second->opener.sender != current->second->receiver.sender; |
459 if (current->second->opener.sender == sender) { | 464 if (current->second->opener.sender == sender) { |
460 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first)); | 465 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), |
| 466 notify_other_port); |
461 } else if (current->second->receiver.sender == sender) { | 467 } else if (current->second->receiver.sender == sender) { |
462 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first)); | 468 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), |
| 469 notify_other_port); |
463 } | 470 } |
464 } | 471 } |
465 } | 472 } |
OLD | NEW |