| 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 const char ExtensionMessageService::kDispatchOnConnect[] = | 105 const char ExtensionMessageService::kDispatchOnConnect[] = |
| 106 "Port.dispatchOnConnect"; | 106 "Port.dispatchOnConnect"; |
| 107 const char ExtensionMessageService::kDispatchOnDisconnect[] = | 107 const char ExtensionMessageService::kDispatchOnDisconnect[] = |
| 108 "Port.dispatchOnDisconnect"; | 108 "Port.dispatchOnDisconnect"; |
| 109 const char ExtensionMessageService::kDispatchOnMessage[] = | 109 const char ExtensionMessageService::kDispatchOnMessage[] = |
| 110 "Port.dispatchOnMessage"; | 110 "Port.dispatchOnMessage"; |
| 111 const char ExtensionMessageService::kDispatchEvent[] = | 111 const char ExtensionMessageService::kDispatchEvent[] = |
| 112 "Event.dispatchJSON"; | 112 "Event.dispatchJSON"; |
| 113 | 113 |
| 114 ExtensionMessageService::ExtensionMessageService(Profile* profile) | 114 ExtensionMessageService::ExtensionMessageService(Profile* profile) |
| 115 : ui_loop_(MessageLoop::current()), | 115 : profile_(profile), |
| 116 profile_(profile), | |
| 117 extension_devtools_manager_(NULL), | 116 extension_devtools_manager_(NULL), |
| 118 next_port_id_(0) { | 117 next_port_id_(0) { |
| 119 DCHECK_EQ(ui_loop_->type(), MessageLoop::TYPE_UI); | |
| 120 | |
| 121 registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, | 118 registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, |
| 122 NotificationService::AllSources()); | 119 NotificationService::AllSources()); |
| 123 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, | 120 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, |
| 124 NotificationService::AllSources()); | 121 NotificationService::AllSources()); |
| 125 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED, | 122 registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED, |
| 126 NotificationService::AllSources()); | 123 NotificationService::AllSources()); |
| 127 | 124 |
| 128 extension_devtools_manager_ = profile_->GetExtensionDevToolsManager(); | 125 extension_devtools_manager_ = profile_->GetExtensionDevToolsManager(); |
| 129 } | 126 } |
| 130 | 127 |
| 131 ExtensionMessageService::~ExtensionMessageService() { | 128 ExtensionMessageService::~ExtensionMessageService() { |
| 132 } | 129 } |
| 133 | 130 |
| 134 void ExtensionMessageService::ProfileDestroyed() { | 131 void ExtensionMessageService::ProfileDestroyed() { |
| 135 DCHECK_EQ(ui_loop_->type(), MessageLoop::TYPE_UI); | |
| 136 | |
| 137 profile_ = NULL; | 132 profile_ = NULL; |
| 138 | 133 |
| 139 // We remove notifications here because our destructor might be called on | 134 // We remove notifications here because our destructor might be called on |
| 140 // a non-UI thread. | 135 // a non-UI thread. |
| 141 registrar_.RemoveAll(); | 136 registrar_.RemoveAll(); |
| 142 } | 137 } |
| 143 | 138 |
| 144 void ExtensionMessageService::AddEventListener(const std::string& event_name, | 139 void ExtensionMessageService::AddEventListener(const std::string& event_name, |
| 145 int render_process_id) { | 140 int render_process_id) { |
| 146 DCHECK(RenderProcessHost::FromID(render_process_id)) << | 141 DCHECK(RenderProcessHost::FromID(render_process_id)) << |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 const std::string& channel_name, ResourceMessageFilter* source) { | 195 const std::string& channel_name, ResourceMessageFilter* source) { |
| 201 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 196 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 202 | 197 |
| 203 // Create a channel ID for both sides of the channel. | 198 // Create a channel ID for both sides of the channel. |
| 204 int port1_id = -1; | 199 int port1_id = -1; |
| 205 int port2_id = -1; | 200 int port2_id = -1; |
| 206 AllocatePortIdPair(&port1_id, &port2_id); | 201 AllocatePortIdPair(&port1_id, &port2_id); |
| 207 | 202 |
| 208 // Each side of the port is given his own port ID. When they send messages, | 203 // Each side of the port is given his own port ID. When they send messages, |
| 209 // we convert to the opposite port ID. See PostMessageFromRenderer. | 204 // we convert to the opposite port ID. See PostMessageFromRenderer. |
| 210 ui_loop_->PostTask(FROM_HERE, | 205 ChromeThread::PostTask( |
| 211 NewRunnableMethod(this, | 206 ChromeThread::UI, FROM_HERE, |
| 212 &ExtensionMessageService::OpenChannelToExtensionOnUIThread, | 207 NewRunnableMethod( |
| 208 this, &ExtensionMessageService::OpenChannelToExtensionOnUIThread, |
| 213 source->id(), routing_id, port2_id, source_extension_id, | 209 source->id(), routing_id, port2_id, source_extension_id, |
| 214 target_extension_id, channel_name)); | 210 target_extension_id, channel_name)); |
| 215 | 211 |
| 216 return port1_id; | 212 return port1_id; |
| 217 } | 213 } |
| 218 | 214 |
| 219 int ExtensionMessageService::OpenChannelToTab(int routing_id, | 215 int ExtensionMessageService::OpenChannelToTab(int routing_id, |
| 220 int tab_id, | 216 int tab_id, |
| 221 const std::string& extension_id, | 217 const std::string& extension_id, |
| 222 const std::string& channel_name, | 218 const std::string& channel_name, |
| 223 ResourceMessageFilter* source) { | 219 ResourceMessageFilter* source) { |
| 224 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 220 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 225 | 221 |
| 226 // Create a channel ID for both sides of the channel. | 222 // Create a channel ID for both sides of the channel. |
| 227 int port1_id = -1; | 223 int port1_id = -1; |
| 228 int port2_id = -1; | 224 int port2_id = -1; |
| 229 AllocatePortIdPair(&port1_id, &port2_id); | 225 AllocatePortIdPair(&port1_id, &port2_id); |
| 230 | 226 |
| 231 // Each side of the port is given his own port ID. When they send messages, | 227 // Each side of the port is given his own port ID. When they send messages, |
| 232 // we convert to the opposite port ID. See PostMessageFromRenderer. | 228 // we convert to the opposite port ID. See PostMessageFromRenderer. |
| 233 ui_loop_->PostTask(FROM_HERE, | 229 ChromeThread::PostTask( |
| 234 NewRunnableMethod(this, | 230 ChromeThread::UI, FROM_HERE, |
| 235 &ExtensionMessageService::OpenChannelToTabOnUIThread, | 231 NewRunnableMethod( |
| 232 this, &ExtensionMessageService::OpenChannelToTabOnUIThread, |
| 236 source->id(), routing_id, port2_id, tab_id, extension_id, | 233 source->id(), routing_id, port2_id, tab_id, extension_id, |
| 237 channel_name)); | 234 channel_name)); |
| 238 | 235 |
| 239 return port1_id; | 236 return port1_id; |
| 240 } | 237 } |
| 241 | 238 |
| 242 void ExtensionMessageService::OpenChannelToExtensionOnUIThread( | 239 void ExtensionMessageService::OpenChannelToExtensionOnUIThread( |
| 243 int source_process_id, int source_routing_id, int receiver_port_id, | 240 int source_process_id, int source_routing_id, int receiver_port_id, |
| 244 const std::string& source_extension_id, | 241 const std::string& source_extension_id, |
| 245 const std::string& target_extension_id, | 242 const std::string& target_extension_id, |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 | 503 |
| 507 if (current->second->opener.sender == sender) { | 504 if (current->second->opener.sender == sender) { |
| 508 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), | 505 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), |
| 509 notify_other_port); | 506 notify_other_port); |
| 510 } else if (current->second->receiver.sender == sender) { | 507 } else if (current->second->receiver.sender == sender) { |
| 511 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), | 508 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), |
| 512 notify_other_port); | 509 notify_other_port); |
| 513 } | 510 } |
| 514 } | 511 } |
| 515 } | 512 } |
| OLD | NEW |