| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_process_manager.h" | 11 #include "chrome/browser/extensions/extension_process_manager.h" |
| 12 #include "chrome/browser/extensions/extension_tab_util.h" | 12 #include "chrome/browser/extensions/extension_tab_util.h" |
| 13 #include "chrome/browser/extensions/process_map.h" | 13 #include "chrome/browser/extensions/process_map.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/tab_contents/tab_util.h" | 15 #include "chrome/browser/tab_contents/tab_util.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 17 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_messages.h" | 18 #include "chrome/common/extensions/extension_messages.h" |
| 19 #include "content/browser/child_process_security_policy.h" | 19 #include "content/browser/child_process_security_policy.h" |
| 20 #include "content/browser/renderer_host/render_view_host.h" | 20 #include "content/browser/renderer_host/render_view_host.h" |
| 21 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/site_instance.h" |
| 22 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 23 #include "content/public/browser/notification_types.h" | 23 #include "content/public/browser/notification_types.h" |
| 24 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
| 25 #include "content/public/browser/web_contents.h" |
| 26 |
| 27 using content::WebContents; |
| 25 | 28 |
| 26 // Since we have 2 ports for every channel, we just index channels by half the | 29 // Since we have 2 ports for every channel, we just index channels by half the |
| 27 // port ID. | 30 // port ID. |
| 28 #define GET_CHANNEL_ID(port_id) ((port_id) / 2) | 31 #define GET_CHANNEL_ID(port_id) ((port_id) / 2) |
| 29 #define GET_CHANNEL_OPENER_ID(channel_id) ((channel_id) * 2) | 32 #define GET_CHANNEL_OPENER_ID(channel_id) ((channel_id) * 2) |
| 30 #define GET_CHANNEL_RECEIVERS_ID(channel_id) ((channel_id) * 2 + 1) | 33 #define GET_CHANNEL_RECEIVERS_ID(channel_id) ((channel_id) * 2 + 1) |
| 31 | 34 |
| 32 // Port1 is always even, port2 is always odd. | 35 // Port1 is always even, port2 is always odd. |
| 33 #define IS_OPENER_PORT_ID(port_id) (((port_id) & 1) == 0) | 36 #define IS_OPENER_PORT_ID(port_id) (((port_id) & 1) == 0) |
| 34 | 37 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 if (!source) | 161 if (!source) |
| 159 return; | 162 return; |
| 160 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); | 163 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); |
| 161 | 164 |
| 162 // Note: we use the source's profile here. If the source is an incognito | 165 // Note: we use the source's profile here. If the source is an incognito |
| 163 // process, we will use the incognito EPM to find the right extension process, | 166 // process, we will use the incognito EPM to find the right extension process, |
| 164 // which depends on whether the extension uses spanning or split mode. | 167 // which depends on whether the extension uses spanning or split mode. |
| 165 MessagePort receiver( | 168 MessagePort receiver( |
| 166 GetExtensionProcess(profile, target_extension_id), | 169 GetExtensionProcess(profile, target_extension_id), |
| 167 MSG_ROUTING_CONTROL); | 170 MSG_ROUTING_CONTROL); |
| 168 TabContents* source_contents = tab_util::GetTabContentsByID( | 171 WebContents* source_contents = tab_util::GetWebContentsByID( |
| 169 source_process_id, source_routing_id); | 172 source_process_id, source_routing_id); |
| 170 | 173 |
| 171 // Include info about the opener's tab (if it was a tab). | 174 // Include info about the opener's tab (if it was a tab). |
| 172 std::string tab_json = "null"; | 175 std::string tab_json = "null"; |
| 173 if (source_contents) { | 176 if (source_contents) { |
| 174 scoped_ptr<DictionaryValue> tab_value( | 177 scoped_ptr<DictionaryValue> tab_value( |
| 175 ExtensionTabUtil::CreateTabValue(source_contents)); | 178 ExtensionTabUtil::CreateTabValue(source_contents)); |
| 176 base::JSONWriter::Write(tab_value.get(), false, &tab_json); | 179 base::JSONWriter::Write(tab_value.get(), false, &tab_json); |
| 177 } | 180 } |
| 178 | 181 |
| 179 OpenChannelImpl(source, tab_json, receiver, receiver_port_id, | 182 OpenChannelImpl(source, tab_json, receiver, receiver_port_id, |
| 180 source_extension_id, target_extension_id, channel_name); | 183 source_extension_id, target_extension_id, channel_name); |
| 181 } | 184 } |
| 182 | 185 |
| 183 void ExtensionMessageService::OpenChannelToTab( | 186 void ExtensionMessageService::OpenChannelToTab( |
| 184 int source_process_id, int source_routing_id, int receiver_port_id, | 187 int source_process_id, int source_routing_id, int receiver_port_id, |
| 185 int tab_id, const std::string& extension_id, | 188 int tab_id, const std::string& extension_id, |
| 186 const std::string& channel_name) { | 189 const std::string& channel_name) { |
| 187 content::RenderProcessHost* source = | 190 content::RenderProcessHost* source = |
| 188 content::RenderProcessHost::FromID(source_process_id); | 191 content::RenderProcessHost::FromID(source_process_id); |
| 189 if (!source) | 192 if (!source) |
| 190 return; | 193 return; |
| 191 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); | 194 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); |
| 192 | 195 |
| 193 TabContentsWrapper* contents = NULL; | 196 TabContentsWrapper* contents = NULL; |
| 194 MessagePort receiver; | 197 MessagePort receiver; |
| 195 if (ExtensionTabUtil::GetTabById(tab_id, profile, true, | 198 if (ExtensionTabUtil::GetTabById(tab_id, profile, true, |
| 196 NULL, NULL, &contents, NULL)) { | 199 NULL, NULL, &contents, NULL)) { |
| 197 receiver.sender = contents->tab_contents()->GetRenderViewHost(); | 200 receiver.sender = contents->web_contents()->GetRenderViewHost(); |
| 198 receiver.routing_id = | 201 receiver.routing_id = |
| 199 contents->tab_contents()->GetRenderViewHost()->routing_id(); | 202 contents->web_contents()->GetRenderViewHost()->routing_id(); |
| 200 } | 203 } |
| 201 | 204 |
| 202 if (contents && contents->tab_contents()->GetController().NeedsReload()) { | 205 if (contents && contents->web_contents()->GetController().NeedsReload()) { |
| 203 // The tab isn't loaded yet. Don't attempt to connect. Treat this as a | 206 // The tab isn't loaded yet. Don't attempt to connect. Treat this as a |
| 204 // disconnect. | 207 // disconnect. |
| 205 DispatchOnDisconnect(MessagePort(source, MSG_ROUTING_CONTROL), | 208 DispatchOnDisconnect(MessagePort(source, MSG_ROUTING_CONTROL), |
| 206 GET_OPPOSITE_PORT_ID(receiver_port_id), true); | 209 GET_OPPOSITE_PORT_ID(receiver_port_id), true); |
| 207 return; | 210 return; |
| 208 } | 211 } |
| 209 | 212 |
| 210 TabContents* source_contents = tab_util::GetTabContentsByID( | 213 WebContents* source_contents = tab_util::GetWebContentsByID( |
| 211 source_process_id, source_routing_id); | 214 source_process_id, source_routing_id); |
| 212 | 215 |
| 213 // Include info about the opener's tab (if it was a tab). | 216 // Include info about the opener's tab (if it was a tab). |
| 214 std::string tab_json = "null"; | 217 std::string tab_json = "null"; |
| 215 if (source_contents) { | 218 if (source_contents) { |
| 216 scoped_ptr<DictionaryValue> tab_value( | 219 scoped_ptr<DictionaryValue> tab_value( |
| 217 ExtensionTabUtil::CreateTabValue(source_contents)); | 220 ExtensionTabUtil::CreateTabValue(source_contents)); |
| 218 base::JSONWriter::Write(tab_value.get(), false, &tab_json); | 221 base::JSONWriter::Write(tab_value.get(), false, &tab_json); |
| 219 } | 222 } |
| 220 | 223 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 MSG_ROUTING_CONTROL); | 280 MSG_ROUTING_CONTROL); |
| 278 if (!OpenChannelImpl(source, tab_json, receiver, port2_id, | 281 if (!OpenChannelImpl(source, tab_json, receiver, port2_id, |
| 279 extension_id, extension_id, channel_name)) | 282 extension_id, extension_id, channel_name)) |
| 280 return -1; | 283 return -1; |
| 281 | 284 |
| 282 return port1_id; | 285 return port1_id; |
| 283 } | 286 } |
| 284 | 287 |
| 285 int ExtensionMessageService::OpenSpecialChannelToTab( | 288 int ExtensionMessageService::OpenSpecialChannelToTab( |
| 286 const std::string& extension_id, const std::string& channel_name, | 289 const std::string& extension_id, const std::string& channel_name, |
| 287 TabContents* target_tab_contents, IPC::Message::Sender* source) { | 290 WebContents* target_web_contents, IPC::Message::Sender* source) { |
| 288 DCHECK(target_tab_contents); | 291 DCHECK(target_web_contents); |
| 289 | 292 |
| 290 if (target_tab_contents->GetController().NeedsReload()) { | 293 if (target_web_contents->GetController().NeedsReload()) { |
| 291 // The tab isn't loaded yet. Don't attempt to connect. | 294 // The tab isn't loaded yet. Don't attempt to connect. |
| 292 return -1; | 295 return -1; |
| 293 } | 296 } |
| 294 | 297 |
| 295 int port1_id = -1; | 298 int port1_id = -1; |
| 296 int port2_id = -1; | 299 int port2_id = -1; |
| 297 // Create a channel ID for both sides of the channel. | 300 // Create a channel ID for both sides of the channel. |
| 298 AllocatePortIdPair(&port1_id, &port2_id); | 301 AllocatePortIdPair(&port1_id, &port2_id); |
| 299 | 302 |
| 300 MessagePort receiver( | 303 MessagePort receiver( |
| 301 target_tab_contents->GetRenderViewHost(), | 304 target_web_contents->GetRenderViewHost(), |
| 302 target_tab_contents->GetRenderViewHost()->routing_id()); | 305 target_web_contents->GetRenderViewHost()->routing_id()); |
| 303 if (!OpenChannelImpl(source, "null", receiver, port2_id, | 306 if (!OpenChannelImpl(source, "null", receiver, port2_id, |
| 304 extension_id, extension_id, channel_name)) | 307 extension_id, extension_id, channel_name)) |
| 305 return -1; | 308 return -1; |
| 306 | 309 |
| 307 return port1_id; | 310 return port1_id; |
| 308 } | 311 } |
| 309 | 312 |
| 310 void ExtensionMessageService::CloseChannel(int port_id) { | 313 void ExtensionMessageService::CloseChannel(int port_id) { |
| 311 // Note: The channel might be gone already, if the other side closed first. | 314 // Note: The channel might be gone already, if the other side closed first. |
| 312 MessageChannelMap::iterator it = channels_.find(GET_CHANNEL_ID(port_id)); | 315 MessageChannelMap::iterator it = channels_.find(GET_CHANNEL_ID(port_id)); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 | 378 |
| 376 if (current->second->opener.sender == sender) { | 379 if (current->second->opener.sender == sender) { |
| 377 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), | 380 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), |
| 378 notify_other_port); | 381 notify_other_port); |
| 379 } else if (current->second->receiver.sender == sender) { | 382 } else if (current->second->receiver.sender == sender) { |
| 380 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), | 383 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), |
| 381 notify_other_port); | 384 notify_other_port); |
| 382 } | 385 } |
| 383 } | 386 } |
| 384 } | 387 } |
| OLD | NEW |