| 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 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 void ExtensionMessageService::OpenChannelToExtensionOnUIThread( | 218 void ExtensionMessageService::OpenChannelToExtensionOnUIThread( |
| 219 int source_process_id, int source_routing_id, int receiver_port_id, | 219 int source_process_id, int source_routing_id, int receiver_port_id, |
| 220 const std::string& extension_id, const std::string& channel_name) { | 220 const std::string& extension_id, const std::string& channel_name) { |
| 221 if (!profile_) | 221 if (!profile_) |
| 222 return; | 222 return; |
| 223 | 223 |
| 224 RenderProcessHost* source = RenderProcessHost::FromID(source_process_id); | 224 RenderProcessHost* source = RenderProcessHost::FromID(source_process_id); |
| 225 MessagePort receiver( | 225 MessagePort receiver( |
| 226 profile_->GetExtensionProcessManager()->GetExtensionProcess(extension_id), | 226 profile_->GetExtensionProcessManager()->GetExtensionProcess(extension_id), |
| 227 MSG_ROUTING_CONTROL); | 227 MSG_ROUTING_CONTROL); |
| 228 OpenChannelOnUIThreadImpl(source, source_process_id, source_routing_id, | 228 TabContents* source_contents = tab_util::GetTabContentsByID( |
| 229 receiver, receiver_port_id, extension_id, | 229 source_process_id, source_routing_id); |
| 230 channel_name); | 230 OpenChannelOnUIThreadImpl(source, source_contents, |
| 231 receiver, receiver_port_id, |
| 232 extension_id, channel_name); |
| 231 } | 233 } |
| 232 | 234 |
| 233 void ExtensionMessageService::OpenChannelToTabOnUIThread( | 235 void ExtensionMessageService::OpenChannelToTabOnUIThread( |
| 234 int source_process_id, int source_routing_id, int receiver_port_id, | 236 int source_process_id, int source_routing_id, int receiver_port_id, |
| 235 int tab_id, const std::string& extension_id, | 237 int tab_id, const std::string& extension_id, |
| 236 const std::string& channel_name) { | 238 const std::string& channel_name) { |
| 237 RenderProcessHost* source = RenderProcessHost::FromID(source_process_id); | 239 RenderProcessHost* source = RenderProcessHost::FromID(source_process_id); |
| 238 TabContents* contents; | 240 TabContents* contents; |
| 239 MessagePort receiver; | 241 MessagePort receiver; |
| 240 if (ExtensionTabUtil::GetTabById(tab_id, source->profile(), | 242 if (ExtensionTabUtil::GetTabById(tab_id, source->profile(), |
| 241 NULL, NULL, &contents, NULL)) { | 243 NULL, NULL, &contents, NULL)) { |
| 242 receiver.sender = contents->render_view_host(); | 244 receiver.sender = contents->render_view_host(); |
| 243 receiver.routing_id = contents->render_view_host()->routing_id(); | 245 receiver.routing_id = contents->render_view_host()->routing_id(); |
| 244 } | 246 } |
| 245 OpenChannelOnUIThreadImpl(source, source_process_id, source_routing_id, | 247 TabContents* source_contents = tab_util::GetTabContentsByID( |
| 246 receiver, receiver_port_id, extension_id, | 248 source_process_id, source_routing_id); |
| 247 channel_name); | 249 OpenChannelOnUIThreadImpl(source, source_contents, |
| 250 receiver, receiver_port_id, |
| 251 extension_id, channel_name); |
| 248 } | 252 } |
| 249 | 253 |
| 250 bool ExtensionMessageService::OpenChannelOnUIThreadImpl( | 254 bool ExtensionMessageService::OpenChannelOnUIThreadImpl( |
| 251 IPC::Message::Sender* source, int source_process_id, int source_routing_id, | 255 IPC::Message::Sender* source, TabContents* source_contents, |
| 252 const MessagePort& receiver, int receiver_port_id, | 256 const MessagePort& receiver, int receiver_port_id, |
| 253 const std::string& extension_id, const std::string& channel_name) { | 257 const std::string& extension_id, const std::string& channel_name) { |
| 254 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 258 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
| 255 | 259 |
| 256 // TODO(mpcomplete): notify source if reciever doesn't exist | 260 // TODO(mpcomplete): notify source if reciever doesn't exist |
| 257 if (!source) | 261 if (!source) |
| 258 return false; // Closed while in flight. | 262 return false; // Closed while in flight. |
| 259 | 263 |
| 260 if (!receiver.sender) { | 264 if (!receiver.sender) { |
| 261 // Treat it as a disconnect. | 265 // Treat it as a disconnect. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 273 channel->receiver = receiver; | 277 channel->receiver = receiver; |
| 274 | 278 |
| 275 CHECK(receiver.sender); | 279 CHECK(receiver.sender); |
| 276 | 280 |
| 277 channels_[GET_CHANNEL_ID(receiver_port_id)] = channel; | 281 channels_[GET_CHANNEL_ID(receiver_port_id)] = channel; |
| 278 | 282 |
| 279 CHECK(receiver.sender); | 283 CHECK(receiver.sender); |
| 280 | 284 |
| 281 // Include info about the opener's tab (if it was a tab). | 285 // Include info about the opener's tab (if it was a tab). |
| 282 std::string tab_json = "null"; | 286 std::string tab_json = "null"; |
| 283 TabContents* contents = tab_util::GetTabContentsByID(source_process_id, | 287 if (source_contents) { |
| 284 source_routing_id); | 288 DictionaryValue* tab_value = |
| 285 if (contents) { | 289 ExtensionTabUtil::CreateTabValue(source_contents); |
| 286 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue(contents); | |
| 287 JSONWriter::Write(tab_value, false, &tab_json); | 290 JSONWriter::Write(tab_value, false, &tab_json); |
| 288 } | 291 } |
| 289 | 292 |
| 290 CHECK(receiver.sender); | 293 CHECK(receiver.sender); |
| 291 | 294 |
| 292 // Send the connect event to the receiver. Give it the opener's port ID (the | 295 // Send the connect event to the receiver. Give it the opener's port ID (the |
| 293 // opener has the opposite port ID). | 296 // opener has the opposite port ID). |
| 294 DispatchOnConnect(receiver, receiver_port_id, channel_name, tab_json, | 297 DispatchOnConnect(receiver, receiver_port_id, channel_name, tab_json, |
| 295 extension_id); | 298 extension_id); |
| 296 | 299 |
| 297 return true; | 300 return true; |
| 298 } | 301 } |
| 299 | 302 |
| 300 int ExtensionMessageService::OpenAutomationChannelToExtension( | 303 int ExtensionMessageService::OpenSpecialChannelToExtension( |
| 301 int source_process_id, int routing_id, const std::string& extension_id, | 304 const std::string& extension_id, const std::string& channel_name, |
| 302 const std::string& channel_name, IPC::Message::Sender* source) { | 305 IPC::Message::Sender* source) { |
| 303 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 306 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
| 304 DCHECK(profile_); | 307 DCHECK(profile_); |
| 305 | 308 |
| 306 int port1_id = -1; | 309 int port1_id = -1; |
| 307 int port2_id = -1; | 310 int port2_id = -1; |
| 308 // Create a channel ID for both sides of the channel. | 311 // Create a channel ID for both sides of the channel. |
| 309 AllocatePortIdPair(&port1_id, &port2_id); | 312 AllocatePortIdPair(&port1_id, &port2_id); |
| 310 | 313 |
| 311 // TODO(siggi): The source process- and routing ids are used to | |
| 312 // describe the originating tab to the target extension. | |
| 313 // This isn't really appropriate here, the originating tab | |
| 314 // information should be supplied by the caller for | |
| 315 // automation-initiated ports. | |
| 316 MessagePort receiver( | 314 MessagePort receiver( |
| 317 profile_->GetExtensionProcessManager()->GetExtensionProcess(extension_id), | 315 profile_->GetExtensionProcessManager()-> |
| 316 GetExtensionProcess(extension_id), |
| 318 MSG_ROUTING_CONTROL); | 317 MSG_ROUTING_CONTROL); |
| 319 if (!OpenChannelOnUIThreadImpl(source, source_process_id, routing_id, | 318 if (!OpenChannelOnUIThreadImpl( |
| 320 receiver, port2_id, extension_id, | 319 source, NULL, receiver, port2_id, extension_id, channel_name)) |
| 321 channel_name)) | 320 return -1; |
| 321 |
| 322 return port1_id; |
| 323 } |
| 324 |
| 325 int ExtensionMessageService::OpenSpecialChannelToTab( |
| 326 const std::string& extension_id, const std::string& channel_name, |
| 327 TabContents* target_tab_contents, IPC::Message::Sender* source) { |
| 328 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
| 329 DCHECK(target_tab_contents); |
| 330 |
| 331 int port1_id = -1; |
| 332 int port2_id = -1; |
| 333 // Create a channel ID for both sides of the channel. |
| 334 AllocatePortIdPair(&port1_id, &port2_id); |
| 335 |
| 336 MessagePort receiver( |
| 337 target_tab_contents->render_view_host(), |
| 338 target_tab_contents->render_view_host()->routing_id()); |
| 339 if (!OpenChannelOnUIThreadImpl(source, NULL, |
| 340 receiver, port2_id, |
| 341 extension_id, channel_name)) |
| 322 return -1; | 342 return -1; |
| 323 | 343 |
| 324 return port1_id; | 344 return port1_id; |
| 325 } | 345 } |
| 326 | 346 |
| 327 void ExtensionMessageService::CloseChannel(int port_id) { | 347 void ExtensionMessageService::CloseChannel(int port_id) { |
| 328 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); | 348 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
| 329 | 349 |
| 330 // Note: The channel might be gone already, if the other side closed first. | 350 // Note: The channel might be gone already, if the other side closed first. |
| 331 MessageChannelMap::iterator it = channels_.find(GET_CHANNEL_ID(port_id)); | 351 MessageChannelMap::iterator it = channels_.find(GET_CHANNEL_ID(port_id)); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 for (MessageChannelMap::iterator it = channels_.begin(); | 439 for (MessageChannelMap::iterator it = channels_.begin(); |
| 420 it != channels_.end(); ) { | 440 it != channels_.end(); ) { |
| 421 MessageChannelMap::iterator current = it++; | 441 MessageChannelMap::iterator current = it++; |
| 422 if (current->second->opener.sender == sender) { | 442 if (current->second->opener.sender == sender) { |
| 423 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first)); | 443 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first)); |
| 424 } else if (current->second->receiver.sender == sender) { | 444 } else if (current->second->receiver.sender == sender) { |
| 425 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first)); | 445 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first)); |
| 426 } | 446 } |
| 427 } | 447 } |
| 428 } | 448 } |
| OLD | NEW |