| 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_process_host.h" | |
| 21 #include "content/browser/renderer_host/render_view_host.h" | 20 #include "content/browser/renderer_host/render_view_host.h" |
| 22 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/tab_contents/tab_contents.h" |
| 23 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/browser/notification_types.h" | 23 #include "content/public/browser/notification_types.h" |
| 24 #include "content/public/browser/render_process_host.h" |
| 25 | 25 |
| 26 // Since we have 2 ports for every channel, we just index channels by half the | 26 // Since we have 2 ports for every channel, we just index channels by half the |
| 27 // port ID. | 27 // port ID. |
| 28 #define GET_CHANNEL_ID(port_id) ((port_id) / 2) | 28 #define GET_CHANNEL_ID(port_id) ((port_id) / 2) |
| 29 #define GET_CHANNEL_OPENER_ID(channel_id) ((channel_id) * 2) | 29 #define GET_CHANNEL_OPENER_ID(channel_id) ((channel_id) * 2) |
| 30 #define GET_CHANNEL_RECEIVERS_ID(channel_id) ((channel_id) * 2 + 1) | 30 #define GET_CHANNEL_RECEIVERS_ID(channel_id) ((channel_id) * 2 + 1) |
| 31 | 31 |
| 32 // Port1 is always even, port2 is always odd. | 32 // Port1 is always even, port2 is always odd. |
| 33 #define IS_OPENER_PORT_ID(port_id) (((port_id) & 1) == 0) | 33 #define IS_OPENER_PORT_ID(port_id) (((port_id) & 1) == 0) |
| 34 | 34 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 "", ExtensionMessageService::kDispatchOnDisconnect, args, GURL())); | 87 "", ExtensionMessageService::kDispatchOnDisconnect, args, GURL())); |
| 88 } | 88 } |
| 89 | 89 |
| 90 static void DispatchOnMessage(const ExtensionMessageService::MessagePort& port, | 90 static void DispatchOnMessage(const ExtensionMessageService::MessagePort& port, |
| 91 const std::string& message, int target_port_id) { | 91 const std::string& message, int target_port_id) { |
| 92 port.sender->Send( | 92 port.sender->Send( |
| 93 new ExtensionMsg_DeliverMessage( | 93 new ExtensionMsg_DeliverMessage( |
| 94 port.routing_id, target_port_id, message)); | 94 port.routing_id, target_port_id, message)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 static RenderProcessHost* GetExtensionProcess(Profile* profile, | 97 static content::RenderProcessHost* GetExtensionProcess(Profile* profile, |
| 98 const std::string& extension_id) { | 98 const std::string& extension_id) { |
| 99 SiteInstance* site_instance = | 99 SiteInstance* site_instance = |
| 100 profile->GetExtensionProcessManager()->GetSiteInstanceForURL( | 100 profile->GetExtensionProcessManager()->GetSiteInstanceForURL( |
| 101 Extension::GetBaseURLFromExtensionId(extension_id)); | 101 Extension::GetBaseURLFromExtensionId(extension_id)); |
| 102 | 102 |
| 103 if (!site_instance->HasProcess()) | 103 if (!site_instance->HasProcess()) |
| 104 return NULL; | 104 return NULL; |
| 105 | 105 |
| 106 return site_instance->GetProcess(); | 106 return site_instance->GetProcess(); |
| 107 } | 107 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 profile_ = NULL; | 146 profile_ = NULL; |
| 147 if (!registrar_.IsEmpty()) | 147 if (!registrar_.IsEmpty()) |
| 148 registrar_.RemoveAll(); | 148 registrar_.RemoveAll(); |
| 149 } | 149 } |
| 150 | 150 |
| 151 void ExtensionMessageService::OpenChannelToExtension( | 151 void ExtensionMessageService::OpenChannelToExtension( |
| 152 int source_process_id, int source_routing_id, int receiver_port_id, | 152 int source_process_id, int source_routing_id, int receiver_port_id, |
| 153 const std::string& source_extension_id, | 153 const std::string& source_extension_id, |
| 154 const std::string& target_extension_id, | 154 const std::string& target_extension_id, |
| 155 const std::string& channel_name) { | 155 const std::string& channel_name) { |
| 156 RenderProcessHost* source = RenderProcessHost::FromID(source_process_id); | 156 content::RenderProcessHost* source = |
| 157 content::RenderProcessHost::FromID(source_process_id); |
| 157 if (!source) | 158 if (!source) |
| 158 return; | 159 return; |
| 159 Profile* profile = Profile::FromBrowserContext(source->browser_context()); | 160 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); |
| 160 | 161 |
| 161 // Note: we use the source's profile here. If the source is an incognito | 162 // Note: we use the source's profile here. If the source is an incognito |
| 162 // process, we will use the incognito EPM to find the right extension process, | 163 // process, we will use the incognito EPM to find the right extension process, |
| 163 // which depends on whether the extension uses spanning or split mode. | 164 // which depends on whether the extension uses spanning or split mode. |
| 164 MessagePort receiver( | 165 MessagePort receiver( |
| 165 GetExtensionProcess(profile, target_extension_id), | 166 GetExtensionProcess(profile, target_extension_id), |
| 166 MSG_ROUTING_CONTROL); | 167 MSG_ROUTING_CONTROL); |
| 167 TabContents* source_contents = tab_util::GetTabContentsByID( | 168 TabContents* source_contents = tab_util::GetTabContentsByID( |
| 168 source_process_id, source_routing_id); | 169 source_process_id, source_routing_id); |
| 169 | 170 |
| 170 // Include info about the opener's tab (if it was a tab). | 171 // Include info about the opener's tab (if it was a tab). |
| 171 std::string tab_json = "null"; | 172 std::string tab_json = "null"; |
| 172 if (source_contents) { | 173 if (source_contents) { |
| 173 scoped_ptr<DictionaryValue> tab_value( | 174 scoped_ptr<DictionaryValue> tab_value( |
| 174 ExtensionTabUtil::CreateTabValue(source_contents)); | 175 ExtensionTabUtil::CreateTabValue(source_contents)); |
| 175 base::JSONWriter::Write(tab_value.get(), false, &tab_json); | 176 base::JSONWriter::Write(tab_value.get(), false, &tab_json); |
| 176 } | 177 } |
| 177 | 178 |
| 178 OpenChannelImpl(source, tab_json, receiver, receiver_port_id, | 179 OpenChannelImpl(source, tab_json, receiver, receiver_port_id, |
| 179 source_extension_id, target_extension_id, channel_name); | 180 source_extension_id, target_extension_id, channel_name); |
| 180 } | 181 } |
| 181 | 182 |
| 182 void ExtensionMessageService::OpenChannelToTab( | 183 void ExtensionMessageService::OpenChannelToTab( |
| 183 int source_process_id, int source_routing_id, int receiver_port_id, | 184 int source_process_id, int source_routing_id, int receiver_port_id, |
| 184 int tab_id, const std::string& extension_id, | 185 int tab_id, const std::string& extension_id, |
| 185 const std::string& channel_name) { | 186 const std::string& channel_name) { |
| 186 RenderProcessHost* source = RenderProcessHost::FromID(source_process_id); | 187 content::RenderProcessHost* source = |
| 188 content::RenderProcessHost::FromID(source_process_id); |
| 187 if (!source) | 189 if (!source) |
| 188 return; | 190 return; |
| 189 Profile* profile = Profile::FromBrowserContext(source->browser_context()); | 191 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); |
| 190 | 192 |
| 191 TabContentsWrapper* contents = NULL; | 193 TabContentsWrapper* contents = NULL; |
| 192 MessagePort receiver; | 194 MessagePort receiver; |
| 193 if (ExtensionTabUtil::GetTabById(tab_id, profile, true, | 195 if (ExtensionTabUtil::GetTabById(tab_id, profile, true, |
| 194 NULL, NULL, &contents, NULL)) { | 196 NULL, NULL, &contents, NULL)) { |
| 195 receiver.sender = contents->render_view_host(); | 197 receiver.sender = contents->render_view_host(); |
| 196 receiver.routing_id = contents->render_view_host()->routing_id(); | 198 receiver.routing_id = contents->render_view_host()->routing_id(); |
| 197 } | 199 } |
| 198 | 200 |
| 199 if (contents && contents->controller().needs_reload()) { | 201 if (contents && contents->controller().needs_reload()) { |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 | 340 |
| 339 DispatchOnMessage(port, message, dest_port_id); | 341 DispatchOnMessage(port, message, dest_port_id); |
| 340 } | 342 } |
| 341 void ExtensionMessageService::Observe( | 343 void ExtensionMessageService::Observe( |
| 342 int type, | 344 int type, |
| 343 const content::NotificationSource& source, | 345 const content::NotificationSource& source, |
| 344 const content::NotificationDetails& details) { | 346 const content::NotificationDetails& details) { |
| 345 switch (type) { | 347 switch (type) { |
| 346 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | 348 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
| 347 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | 349 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 348 RenderProcessHost* renderer = | 350 content::RenderProcessHost* renderer = |
| 349 content::Source<RenderProcessHost>(source).ptr(); | 351 content::Source<content::RenderProcessHost>(source).ptr(); |
| 350 OnSenderClosed(renderer); | 352 OnSenderClosed(renderer); |
| 351 break; | 353 break; |
| 352 } | 354 } |
| 353 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED: | 355 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED: |
| 354 OnSenderClosed(content::Source<RenderViewHost>(source).ptr()); | 356 OnSenderClosed(content::Source<RenderViewHost>(source).ptr()); |
| 355 break; | 357 break; |
| 356 default: | 358 default: |
| 357 NOTREACHED(); | 359 NOTREACHED(); |
| 358 return; | 360 return; |
| 359 } | 361 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 372 | 374 |
| 373 if (current->second->opener.sender == sender) { | 375 if (current->second->opener.sender == sender) { |
| 374 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), | 376 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), |
| 375 notify_other_port); | 377 notify_other_port); |
| 376 } else if (current->second->receiver.sender == sender) { | 378 } else if (current->second->receiver.sender == sender) { |
| 377 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), | 379 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), |
| 378 notify_other_port); | 380 notify_other_port); |
| 379 } | 381 } |
| 380 } | 382 } |
| 381 } | 383 } |
| OLD | NEW |