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/json_writer.h" | 7 #include "base/json/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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 NotificationService::AllSources()); | 119 NotificationService::AllSources()); |
120 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, | 120 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, |
121 NotificationService::AllSources()); | 121 NotificationService::AllSources()); |
122 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED, | 122 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED, |
123 NotificationService::AllSources()); | 123 NotificationService::AllSources()); |
124 | 124 |
125 extension_devtools_manager_ = profile_->GetExtensionDevToolsManager(); | 125 extension_devtools_manager_ = profile_->GetExtensionDevToolsManager(); |
126 } | 126 } |
127 | 127 |
128 ExtensionMessageService::~ExtensionMessageService() { | 128 ExtensionMessageService::~ExtensionMessageService() { |
129 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); | |
130 channels_.clear(); | |
129 } | 131 } |
130 | 132 |
131 void ExtensionMessageService::ProfileDestroyed() { | 133 void ExtensionMessageService::ProfileDestroyed() { |
132 profile_ = NULL; | 134 profile_ = NULL; |
133 | 135 |
134 // We remove notifications here because our destructor might be called on | 136 // We remove notifications here because our destructor might be called on |
135 // a non-UI thread. | 137 // a non-UI thread. |
136 registrar_.RemoveAll(); | 138 registrar_.RemoveAll(); |
137 } | 139 } |
138 | 140 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 // Treat it as a disconnect. | 297 // Treat it as a disconnect. |
296 DispatchOnDisconnect(MessagePort(source, MSG_ROUTING_CONTROL), | 298 DispatchOnDisconnect(MessagePort(source, MSG_ROUTING_CONTROL), |
297 GET_OPPOSITE_PORT_ID(receiver_port_id)); | 299 GET_OPPOSITE_PORT_ID(receiver_port_id)); |
298 return false; | 300 return false; |
299 } | 301 } |
300 | 302 |
301 // Add extra paranoid CHECKs, since we have crash reports of this being NULL. | 303 // Add extra paranoid CHECKs, since we have crash reports of this being NULL. |
302 // http://code.google.com/p/chromium/issues/detail?id=19067 | 304 // http://code.google.com/p/chromium/issues/detail?id=19067 |
303 CHECK(receiver.sender); | 305 CHECK(receiver.sender); |
304 | 306 |
305 linked_ptr<MessageChannel> channel(new MessageChannel); | 307 MessageChannel* channel(new MessageChannel); |
306 channel->opener = MessagePort(source, MSG_ROUTING_CONTROL); | 308 channel->opener = MessagePort(source, MSG_ROUTING_CONTROL); |
307 channel->receiver = receiver; | 309 channel->receiver = receiver; |
308 | 310 |
309 CHECK(receiver.sender); | 311 CHECK(receiver.sender); |
310 | 312 |
313 CHECK(channels_.find(GET_CHANNEL_ID(receiver_port_id)) == channels_.end()); | |
311 channels_[GET_CHANNEL_ID(receiver_port_id)] = channel; | 314 channels_[GET_CHANNEL_ID(receiver_port_id)] = channel; |
312 | 315 |
313 CHECK(receiver.sender); | 316 CHECK(receiver.sender); |
Erik does not do reviews
2009/11/04 17:00:33
remove redundant CHECK
| |
314 | 317 |
315 // Include info about the opener's tab (if it was a tab). | 318 // Include info about the opener's tab (if it was a tab). |
316 std::string tab_json = "null"; | 319 std::string tab_json = "null"; |
317 if (source_contents) { | 320 if (source_contents) { |
318 DictionaryValue* tab_value = | 321 scoped_ptr<DictionaryValue> tab_value( |
319 ExtensionTabUtil::CreateTabValue(source_contents); | 322 ExtensionTabUtil::CreateTabValue(source_contents)); |
320 base::JSONWriter::Write(tab_value, false, &tab_json); | 323 base::JSONWriter::Write(tab_value.get(), false, &tab_json); |
321 } | 324 } |
322 | 325 |
323 CHECK(receiver.sender); | 326 CHECK(receiver.sender); |
324 | 327 |
325 // Send the connect event to the receiver. Give it the opener's port ID (the | 328 // Send the connect event to the receiver. Give it the opener's port ID (the |
326 // opener has the opposite port ID). | 329 // opener has the opposite port ID). |
327 DispatchOnConnect(receiver, receiver_port_id, channel_name, tab_json, | 330 DispatchOnConnect(receiver, receiver_port_id, channel_name, tab_json, |
328 source_extension_id, target_extension_id); | 331 source_extension_id, target_extension_id); |
329 | 332 |
330 return true; | 333 return true; |
331 } | 334 } |
332 | 335 |
333 int ExtensionMessageService::OpenSpecialChannelToExtension( | 336 int ExtensionMessageService::OpenSpecialChannelToExtension( |
334 const std::string& extension_id, const std::string& channel_name, | 337 const std::string& extension_id, const std::string& channel_name, |
335 IPC::Message::Sender* source) { | 338 IPC::Message::Sender* source) { |
336 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 339 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
337 DCHECK(profile_); | 340 DCHECK(profile_); |
338 | 341 |
339 int port1_id = -1; | 342 int port1_id = -1; |
340 int port2_id = -1; | 343 int port2_id = -1; |
341 // Create a channel ID for both sides of the channel. | 344 // Create a channel ID for both sides of the channel. |
342 AllocatePortIdPair(&port1_id, &port2_id); | 345 AllocatePortIdPair(&port1_id, &port2_id); |
343 | 346 |
344 MessagePort receiver( | 347 MessagePort receiver( |
345 profile_->GetExtensionProcessManager()-> | 348 profile_->GetExtensionProcessManager()-> |
346 GetExtensionProcess(extension_id), | 349 GetExtensionProcess(extension_id), |
347 MSG_ROUTING_CONTROL); | 350 MSG_ROUTING_CONTROL); |
348 receiver.debug_info = 4; | 351 receiver.debug_info = 4; |
349 if (!OpenChannelOnUIThreadImpl( | 352 if (!OpenChannelOnUIThreadImpl( |
350 source, NULL, receiver, port2_id, extension_id, extension_id, | 353 source, NULL, receiver, port2_id, extension_id, extension_id, |
351 channel_name)) | 354 channel_name)) |
352 return -1; | 355 return -1; |
353 | 356 |
354 return port1_id; | 357 return port1_id; |
355 } | 358 } |
356 | 359 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 MessageChannelMap::iterator channel_iter, int closing_port_id, | 393 MessageChannelMap::iterator channel_iter, int closing_port_id, |
391 bool notify_other_port) { | 394 bool notify_other_port) { |
392 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 395 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
393 | 396 |
394 // Notify the other side. | 397 // Notify the other side. |
395 const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ? | 398 const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ? |
396 channel_iter->second->receiver : channel_iter->second->opener; | 399 channel_iter->second->receiver : channel_iter->second->opener; |
397 | 400 |
398 if (notify_other_port) | 401 if (notify_other_port) |
399 DispatchOnDisconnect(port, GET_OPPOSITE_PORT_ID(closing_port_id)); | 402 DispatchOnDisconnect(port, GET_OPPOSITE_PORT_ID(closing_port_id)); |
403 delete channel_iter->second; | |
400 channels_.erase(channel_iter); | 404 channels_.erase(channel_iter); |
401 } | 405 } |
402 | 406 |
403 void ExtensionMessageService::PostMessageFromRenderer( | 407 void ExtensionMessageService::PostMessageFromRenderer( |
404 int source_port_id, const std::string& message) { | 408 int source_port_id, const std::string& message) { |
405 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 409 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
406 | 410 |
407 MessageChannelMap::iterator iter = | 411 MessageChannelMap::iterator iter = |
408 channels_.find(GET_CHANNEL_ID(source_port_id)); | 412 channels_.find(GET_CHANNEL_ID(source_port_id)); |
409 if (iter == channels_.end()) | 413 if (iter == channels_.end()) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
503 | 507 |
504 if (current->second->opener.sender == sender) { | 508 if (current->second->opener.sender == sender) { |
505 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), | 509 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), |
506 notify_other_port); | 510 notify_other_port); |
507 } else if (current->second->receiver.sender == sender) { | 511 } else if (current->second->receiver.sender == sender) { |
508 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), | 512 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), |
509 notify_other_port); | 513 notify_other_port); |
510 } | 514 } |
511 } | 515 } |
512 } | 516 } |
OLD | NEW |