Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/message_service.h" | 5 #include "chrome/browser/extensions/message_service.h" |
| 6 | 6 |
| 7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/extension_host.h" | 13 #include "chrome/browser/extensions/extension_host.h" |
| 14 #include "chrome/browser/extensions/extension_process_manager.h" | 14 #include "chrome/browser/extensions/extension_process_manager.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/extension_system.h" | 16 #include "chrome/browser/extensions/extension_system.h" |
| 17 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
| 18 #include "chrome/browser/extensions/lazy_background_task_queue.h" | 18 #include "chrome/browser/extensions/lazy_background_task_queue.h" |
| 19 #include "chrome/browser/extensions/message_port.h" | |
| 20 #include "chrome/browser/extensions/native_message_process.h" | |
| 19 #include "chrome/browser/extensions/process_map.h" | 21 #include "chrome/browser/extensions/process_map.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/tab_contents/tab_util.h" | 23 #include "chrome/browser/tab_contents/tab_util.h" |
| 22 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 24 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 23 #include "chrome/common/chrome_notification_types.h" | 25 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/extensions/extension.h" | 26 #include "chrome/common/extensions/extension.h" |
| 25 #include "chrome/common/extensions/extension_messages.h" | 27 #include "chrome/common/extensions/extension_messages.h" |
| 26 #include "chrome/common/view_type.h" | 28 #include "chrome/common/view_type.h" |
| 29 #include "content/public/browser/browser_thread.h" | |
| 27 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
| 28 #include "content/public/browser/render_process_host.h" | 31 #include "content/public/browser/render_process_host.h" |
| 29 #include "content/public/browser/render_view_host.h" | 32 #include "content/public/browser/render_view_host.h" |
| 30 #include "content/public/browser/site_instance.h" | 33 #include "content/public/browser/site_instance.h" |
| 31 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
| 32 | 35 |
| 33 using content::SiteInstance; | 36 using content::SiteInstance; |
| 34 using content::WebContents; | 37 using content::WebContents; |
| 35 | 38 |
| 36 // Since we have 2 ports for every channel, we just index channels by half the | 39 // Since we have 2 ports for every channel, we just index channels by half the |
| 37 // port ID. | 40 // port ID. |
| 38 #define GET_CHANNEL_ID(port_id) ((port_id) / 2) | 41 #define GET_CHANNEL_ID(port_id) ((port_id) / 2) |
| 39 #define GET_CHANNEL_OPENER_ID(channel_id) ((channel_id) * 2) | 42 #define GET_CHANNEL_OPENER_ID(channel_id) ((channel_id) * 2) |
| 40 #define GET_CHANNEL_RECEIVERS_ID(channel_id) ((channel_id) * 2 + 1) | 43 #define GET_CHANNEL_RECEIVERS_ID(channel_id) ((channel_id) * 2 + 1) |
| 41 | 44 |
| 42 // Port1 is always even, port2 is always odd. | 45 // Port1 is always even, port2 is always odd. |
| 43 #define IS_OPENER_PORT_ID(port_id) (((port_id) & 1) == 0) | 46 #define IS_OPENER_PORT_ID(port_id) (((port_id) & 1) == 0) |
| 44 | 47 |
| 45 // Change even to odd and vice versa, to get the other side of a given channel. | 48 // Change even to odd and vice versa, to get the other side of a given channel. |
| 46 #define GET_OPPOSITE_PORT_ID(source_port_id) ((source_port_id) ^ 1) | 49 #define GET_OPPOSITE_PORT_ID(source_port_id) ((source_port_id) ^ 1) |
| 47 | 50 |
| 48 namespace extensions { | 51 namespace extensions { |
| 49 | 52 |
| 50 struct MessageService::MessagePort { | |
| 51 content::RenderProcessHost* process; | |
| 52 int routing_id; | |
| 53 std::string extension_id; | |
| 54 void* background_host_ptr; // used in IncrementLazyKeepaliveCount | |
| 55 | |
| 56 MessagePort() | |
| 57 : process(NULL), | |
| 58 routing_id(MSG_ROUTING_CONTROL), | |
| 59 background_host_ptr(NULL) {} | |
| 60 MessagePort(content::RenderProcessHost* process, | |
| 61 int routing_id, | |
| 62 const std::string& extension_id) | |
| 63 : process(process), | |
| 64 routing_id(routing_id), | |
| 65 extension_id(extension_id), | |
| 66 background_host_ptr(NULL) {} | |
| 67 }; | |
| 68 | |
| 69 struct MessageService::MessageChannel { | 53 struct MessageService::MessageChannel { |
| 70 MessageService::MessagePort opener; | 54 scoped_ptr<MessagePort> opener; |
| 71 MessageService::MessagePort receiver; | 55 scoped_ptr<MessagePort> receiver; |
| 72 }; | 56 }; |
| 73 | 57 |
| 74 struct MessageService::OpenChannelParams { | 58 struct MessageService::OpenChannelParams { |
| 75 content::RenderProcessHost* source; | 59 content::RenderProcessHost* source; |
| 76 std::string tab_json; | 60 std::string tab_json; |
| 77 MessagePort receiver; | 61 scoped_ptr<MessagePort> receiver; |
| 78 int receiver_port_id; | 62 int receiver_port_id; |
| 79 std::string source_extension_id; | 63 std::string source_extension_id; |
| 80 std::string target_extension_id; | 64 std::string target_extension_id; |
| 81 std::string channel_name; | 65 std::string channel_name; |
| 82 | 66 |
| 67 // Takes ownership of receiver. | |
| 83 OpenChannelParams(content::RenderProcessHost* source, | 68 OpenChannelParams(content::RenderProcessHost* source, |
| 84 const std::string& tab_json, | 69 const std::string& tab_json, |
| 85 const MessagePort& receiver, | 70 MessagePort* receiver, |
| 86 int receiver_port_id, | 71 int receiver_port_id, |
| 87 const std::string& source_extension_id, | 72 const std::string& source_extension_id, |
| 88 const std::string& target_extension_id, | 73 const std::string& target_extension_id, |
| 89 const std::string& channel_name) | 74 const std::string& channel_name) |
| 90 : source(source), | 75 : source(source), |
| 91 tab_json(tab_json), | 76 tab_json(tab_json), |
| 92 receiver(receiver), | 77 receiver(receiver), |
| 93 receiver_port_id(receiver_port_id), | 78 receiver_port_id(receiver_port_id), |
| 94 source_extension_id(source_extension_id), | 79 source_extension_id(source_extension_id), |
| 95 target_extension_id(target_extension_id), | 80 target_extension_id(target_extension_id), |
| 96 channel_name(channel_name) {} | 81 channel_name(channel_name) {} |
| 97 }; | 82 }; |
| 98 | 83 |
| 99 namespace { | 84 namespace { |
| 100 | 85 |
| 101 static base::StaticAtomicSequenceNumber g_next_channel_id; | 86 static base::StaticAtomicSequenceNumber g_next_channel_id; |
| 102 | 87 |
| 103 static void DispatchOnConnect(const MessageService::MessagePort& port, | |
| 104 int dest_port_id, | |
| 105 const std::string& channel_name, | |
| 106 const std::string& tab_json, | |
| 107 const std::string& source_extension_id, | |
| 108 const std::string& target_extension_id) { | |
| 109 port.process->Send(new ExtensionMsg_DispatchOnConnect( | |
| 110 port.routing_id, dest_port_id, channel_name, | |
| 111 tab_json, source_extension_id, target_extension_id)); | |
| 112 } | |
| 113 | |
| 114 static void DispatchOnDisconnect(const MessageService::MessagePort& port, | |
| 115 int source_port_id, | |
| 116 bool connection_error) { | |
| 117 port.process->Send(new ExtensionMsg_DispatchOnDisconnect( | |
| 118 port.routing_id, source_port_id, connection_error)); | |
| 119 } | |
| 120 | |
| 121 static void DispatchOnMessage(const MessageService::MessagePort& port, | |
| 122 const std::string& message, | |
| 123 int target_port_id) { | |
| 124 port.process->Send(new ExtensionMsg_DeliverMessage( | |
| 125 port.routing_id, target_port_id, message)); | |
| 126 } | |
| 127 | |
| 128 static content::RenderProcessHost* GetExtensionProcess( | 88 static content::RenderProcessHost* GetExtensionProcess( |
| 129 Profile* profile, const std::string& extension_id) { | 89 Profile* profile, const std::string& extension_id) { |
| 130 SiteInstance* site_instance = | 90 SiteInstance* site_instance = |
| 131 profile->GetExtensionProcessManager()->GetSiteInstanceForURL( | 91 profile->GetExtensionProcessManager()->GetSiteInstanceForURL( |
| 132 Extension::GetBaseURLFromExtensionId(extension_id)); | 92 Extension::GetBaseURLFromExtensionId(extension_id)); |
| 133 | 93 |
| 134 if (!site_instance->HasProcess()) | 94 if (!site_instance->HasProcess()) |
| 135 return NULL; | 95 return NULL; |
| 136 | 96 |
| 137 return site_instance->GetProcess(); | 97 return site_instance->GetProcess(); |
| 138 } | 98 } |
| 139 | 99 |
| 140 static void IncrementLazyKeepaliveCount(MessageService::MessagePort* port) { | |
| 141 Profile* profile = | |
| 142 Profile::FromBrowserContext(port->process->GetBrowserContext()); | |
| 143 ExtensionProcessManager* pm = | |
| 144 ExtensionSystem::Get(profile)->process_manager(); | |
| 145 ExtensionHost* host = pm->GetBackgroundHostForExtension(port->extension_id); | |
| 146 if (host && host->extension()->has_lazy_background_page()) | |
| 147 pm->IncrementLazyKeepaliveCount(host->extension()); | |
| 148 | |
| 149 // Keep track of the background host, so when we decrement, we only do so if | |
| 150 // the host hasn't reloaded. | |
| 151 port->background_host_ptr = host; | |
| 152 } | |
| 153 | |
| 154 static void DecrementLazyKeepaliveCount(MessageService::MessagePort* port) { | |
| 155 Profile* profile = | |
| 156 Profile::FromBrowserContext(port->process->GetBrowserContext()); | |
| 157 ExtensionProcessManager* pm = | |
| 158 ExtensionSystem::Get(profile)->process_manager(); | |
| 159 ExtensionHost* host = pm->GetBackgroundHostForExtension(port->extension_id); | |
| 160 if (host && host == port->background_host_ptr) | |
| 161 pm->DecrementLazyKeepaliveCount(host->extension()); | |
| 162 } | |
| 163 | |
| 164 } // namespace | 100 } // namespace |
| 165 | 101 |
| 166 // static | 102 // static |
| 167 void MessageService::AllocatePortIdPair(int* port1, int* port2) { | 103 void MessageService::AllocatePortIdPair(int* port1, int* port2) { |
| 168 int channel_id = g_next_channel_id.GetNext(); | 104 int channel_id = g_next_channel_id.GetNext(); |
| 169 int port1_id = channel_id * 2; | 105 int port1_id = channel_id * 2; |
| 170 int port2_id = channel_id * 2 + 1; | 106 int port2_id = channel_id * 2 + 1; |
| 171 | 107 |
| 172 // Sanity checks to make sure our channel<->port converters are correct. | 108 // Sanity checks to make sure our channel<->port converters are correct. |
| 173 DCHECK(IS_OPENER_PORT_ID(port1_id)); | 109 DCHECK(IS_OPENER_PORT_ID(port1_id)); |
| 174 DCHECK(GET_OPPOSITE_PORT_ID(port1_id) == port2_id); | 110 DCHECK(GET_OPPOSITE_PORT_ID(port1_id) == port2_id); |
| 175 DCHECK(GET_OPPOSITE_PORT_ID(port2_id) == port1_id); | 111 DCHECK(GET_OPPOSITE_PORT_ID(port2_id) == port1_id); |
| 176 DCHECK(GET_CHANNEL_ID(port1_id) == GET_CHANNEL_ID(port2_id)); | 112 DCHECK(GET_CHANNEL_ID(port1_id) == GET_CHANNEL_ID(port2_id)); |
| 177 DCHECK(GET_CHANNEL_ID(port1_id) == channel_id); | 113 DCHECK(GET_CHANNEL_ID(port1_id) == channel_id); |
| 178 DCHECK(GET_CHANNEL_OPENER_ID(channel_id) == port1_id); | 114 DCHECK(GET_CHANNEL_OPENER_ID(channel_id) == port1_id); |
| 179 DCHECK(GET_CHANNEL_RECEIVERS_ID(channel_id) == port2_id); | 115 DCHECK(GET_CHANNEL_RECEIVERS_ID(channel_id) == port2_id); |
| 180 | 116 |
| 181 *port1 = port1_id; | 117 *port1 = port1_id; |
| 182 *port2 = port2_id; | 118 *port2 = port2_id; |
| 183 } | 119 } |
| 184 | 120 |
| 185 MessageService::MessageService( | 121 MessageService::MessageService( |
| 186 LazyBackgroundTaskQueue* queue) | 122 LazyBackgroundTaskQueue* queue) |
| 187 : lazy_background_task_queue_(queue) { | 123 : lazy_background_task_queue_(queue), |
| 124 weak_factory_(this) { | |
| 188 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 125 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 189 content::NotificationService::AllBrowserContextsAndSources()); | 126 content::NotificationService::AllBrowserContextsAndSources()); |
| 190 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 127 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 191 content::NotificationService::AllBrowserContextsAndSources()); | 128 content::NotificationService::AllBrowserContextsAndSources()); |
| 192 } | 129 } |
| 193 | 130 |
| 194 MessageService::~MessageService() { | 131 MessageService::~MessageService() { |
| 195 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); | 132 STLDeleteContainerPairSecondPointers(channels_.begin(), channels_.end()); |
| 196 channels_.clear(); | 133 channels_.clear(); |
| 134 weak_factory_.InvalidateWeakPtrs(); | |
|
Aaron Boodman
2012/08/15 03:39:21
This happens automatically, no need to call explic
eaugusti
2012/08/21 23:08:34
Done.
| |
| 197 } | 135 } |
| 198 | 136 |
| 199 void MessageService::OpenChannelToExtension( | 137 void MessageService::OpenChannelToExtension( |
| 200 int source_process_id, int source_routing_id, int receiver_port_id, | 138 int source_process_id, int source_routing_id, int receiver_port_id, |
| 201 const std::string& source_extension_id, | 139 const std::string& source_extension_id, |
| 202 const std::string& target_extension_id, | 140 const std::string& target_extension_id, |
| 203 const std::string& channel_name) { | 141 const std::string& channel_name) { |
| 204 content::RenderProcessHost* source = | 142 content::RenderProcessHost* source = |
| 205 content::RenderProcessHost::FromID(source_process_id); | 143 content::RenderProcessHost::FromID(source_process_id); |
| 206 if (!source) | 144 if (!source) |
| 207 return; | 145 return; |
| 208 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); | 146 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); |
| 209 | 147 |
| 210 // Note: we use the source's profile here. If the source is an incognito | 148 // Note: we use the source's profile here. If the source is an incognito |
| 211 // process, we will use the incognito EPM to find the right extension process, | 149 // process, we will use the incognito EPM to find the right extension process, |
| 212 // which depends on whether the extension uses spanning or split mode. | 150 // which depends on whether the extension uses spanning or split mode. |
| 213 MessagePort receiver(GetExtensionProcess(profile, target_extension_id), | 151 ExtensionMessagePort* receiver = new ExtensionMessagePort( |
| 214 MSG_ROUTING_CONTROL, | 152 GetExtensionProcess(profile, target_extension_id), MSG_ROUTING_CONTROL, |
| 215 target_extension_id); | 153 target_extension_id); |
| 216 WebContents* source_contents = tab_util::GetWebContentsByID( | 154 WebContents* source_contents = tab_util::GetWebContentsByID( |
| 217 source_process_id, source_routing_id); | 155 source_process_id, source_routing_id); |
| 218 | 156 |
| 219 // Include info about the opener's tab (if it was a tab). | 157 // Include info about the opener's tab (if it was a tab). |
| 220 std::string tab_json = "null"; | 158 std::string tab_json = "null"; |
| 221 if (source_contents) { | 159 if (source_contents) { |
| 222 scoped_ptr<DictionaryValue> tab_value( | 160 scoped_ptr<DictionaryValue> tab_value( |
| 223 ExtensionTabUtil::CreateTabValue(source_contents)); | 161 ExtensionTabUtil::CreateTabValue(source_contents)); |
| 224 base::JSONWriter::Write(tab_value.get(), &tab_json); | 162 base::JSONWriter::Write(tab_value.get(), &tab_json); |
| 225 } | 163 } |
| 226 | 164 |
| 227 OpenChannelParams params(source, tab_json, receiver, receiver_port_id, | 165 OpenChannelParams* params = new OpenChannelParams(source, tab_json, receiver, |
| 228 source_extension_id, target_extension_id, | 166 receiver_port_id, |
| 229 channel_name); | 167 source_extension_id, |
| 168 target_extension_id, | |
| 169 channel_name); | |
| 230 | 170 |
| 231 // The target might be a lazy background page. In that case, we have to check | 171 // The target might be a lazy background page. In that case, we have to check |
| 232 // if it is loaded and ready, and if not, queue up the task and load the | 172 // if it is loaded and ready, and if not, queue up the task and load the |
| 233 // page. | 173 // page. |
| 234 if (MaybeAddPendingOpenChannelTask(profile, params)) | 174 if (MaybeAddPendingOpenChannelTask(profile, params)) { |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 scoped_ptr<OpenChannelParams> scoped_params(params); | |
|
Aaron Boodman
2012/08/15 03:39:21
Either define params as scoped_ptr from the start,
eaugusti
2012/08/21 23:08:34
Done.
| |
| 179 OpenChannelImpl(scoped_params.Pass()); | |
| 180 } | |
| 181 | |
| 182 void MessageService::OpenChannelToNativeApp( | |
| 183 int source_process_id, | |
| 184 int source_routing_id, | |
| 185 int receiver_port_id, | |
| 186 const std::string& source_extension_id, | |
| 187 const std::string& native_app_name, | |
| 188 const std::string& channel_name, | |
| 189 const std::string& connect_message) { | |
| 190 content::RenderProcessHost* source = | |
| 191 content::RenderProcessHost::FromID(source_process_id); | |
| 192 if (!source) | |
| 235 return; | 193 return; |
| 236 | 194 |
| 237 OpenChannelImpl(params); | 195 WebContents* source_contents = tab_util::GetWebContentsByID( |
| 196 source_process_id, source_routing_id); | |
| 197 | |
| 198 // Include info about the opener's tab (if it was a tab). | |
| 199 std::string tab_json = "null"; | |
| 200 if (source_contents) { | |
| 201 scoped_ptr<DictionaryValue> tab_value( | |
| 202 ExtensionTabUtil::CreateTabValue(source_contents)); | |
| 203 base::JSONWriter::Write(tab_value.get(), &tab_json); | |
| 204 } | |
| 205 | |
| 206 scoped_ptr<MessageChannel> channel(new MessageChannel()); | |
| 207 channel->opener.reset(new ExtensionMessagePort(source, MSG_ROUTING_CONTROL, | |
| 208 source_extension_id)); | |
| 209 | |
| 210 NativeMessageProcess::MessageType type = | |
| 211 channel_name == "chrome.extension.sendNativeMessage" ? | |
| 212 NativeMessageProcess::TYPE_SEND_MESSAGE_REQUEST : | |
| 213 NativeMessageProcess::TYPE_CONNECT; | |
| 214 content::BrowserThread::PostTask( | |
| 215 content::BrowserThread::FILE, | |
| 216 FROM_HERE, | |
| 217 base::Bind(&NativeMessageProcess::Create, weak_factory_.GetWeakPtr(), | |
| 218 native_app_name, connect_message, receiver_port_id, | |
| 219 type, | |
| 220 base::Bind(&MessageService::FinalizeOpenChannelToNativeApp, | |
| 221 weak_factory_.GetWeakPtr(), | |
| 222 receiver_port_id, | |
| 223 channel_name, | |
| 224 base::Passed(&channel), | |
| 225 tab_json))); | |
| 226 } | |
| 227 | |
| 228 void MessageService::FinalizeOpenChannelToNativeApp( | |
| 229 int receiver_port_id, | |
| 230 const std::string& channel_name, | |
| 231 scoped_ptr<MessageChannel> channel, | |
| 232 const std::string& tab_json, | |
| 233 NativeMessageProcess* native_process) { | |
| 234 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 235 | |
| 236 // Abandon the channel | |
| 237 if (!native_process) { | |
| 238 LOG(ERROR) << "Failed to create native process."; | |
| 239 return; | |
| 240 } | |
| 241 channel->receiver.reset(new NativeMessagePort(native_process)); | |
| 242 | |
| 243 // Keep the opener alive until the channel is closed. | |
| 244 channel->opener->IncrementLazyKeepaliveCount(); | |
| 245 | |
| 246 AddChannel(channel.release(), receiver_port_id); | |
| 238 } | 247 } |
| 239 | 248 |
| 240 void MessageService::OpenChannelToTab( | 249 void MessageService::OpenChannelToTab( |
| 241 int source_process_id, int source_routing_id, int receiver_port_id, | 250 int source_process_id, int source_routing_id, int receiver_port_id, |
| 242 int tab_id, const std::string& extension_id, | 251 int tab_id, const std::string& extension_id, |
| 243 const std::string& channel_name) { | 252 const std::string& channel_name) { |
| 244 content::RenderProcessHost* source = | 253 content::RenderProcessHost* source = |
| 245 content::RenderProcessHost::FromID(source_process_id); | 254 content::RenderProcessHost::FromID(source_process_id); |
| 246 if (!source) | 255 if (!source) |
| 247 return; | 256 return; |
| 248 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); | 257 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); |
| 249 | 258 |
| 250 TabContents* contents = NULL; | 259 TabContents* contents = NULL; |
| 251 MessagePort receiver; | 260 scoped_ptr<MessagePort> receiver; |
| 252 if (ExtensionTabUtil::GetTabById(tab_id, profile, true, | 261 if (ExtensionTabUtil::GetTabById(tab_id, profile, true, |
| 253 NULL, NULL, &contents, NULL)) { | 262 NULL, NULL, &contents, NULL)) { |
| 254 receiver.process = contents->web_contents()->GetRenderProcessHost(); | 263 receiver.reset(new ExtensionMessagePort( |
| 255 receiver.routing_id = | 264 contents->web_contents()->GetRenderProcessHost(), |
| 256 contents->web_contents()->GetRenderViewHost()->GetRoutingID(); | 265 contents->web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 257 receiver.extension_id = extension_id; | 266 extension_id)); |
| 258 } | 267 } |
| 259 | 268 |
| 260 if (contents && contents->web_contents()->GetController().NeedsReload()) { | 269 if (contents && contents->web_contents()->GetController().NeedsReload()) { |
| 261 // The tab isn't loaded yet. Don't attempt to connect. Treat this as a | 270 // The tab isn't loaded yet. Don't attempt to connect. Treat this as a |
| 262 // disconnect. | 271 // disconnect. |
| 263 DispatchOnDisconnect(MessagePort(source, MSG_ROUTING_CONTROL, extension_id), | 272 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, extension_id); |
| 264 GET_OPPOSITE_PORT_ID(receiver_port_id), true); | 273 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(receiver_port_id), true); |
| 265 return; | 274 return; |
| 266 } | 275 } |
| 267 | 276 |
| 268 WebContents* source_contents = tab_util::GetWebContentsByID( | 277 WebContents* source_contents = tab_util::GetWebContentsByID( |
| 269 source_process_id, source_routing_id); | 278 source_process_id, source_routing_id); |
| 270 | 279 |
| 271 // Include info about the opener's tab (if it was a tab). | 280 // Include info about the opener's tab (if it was a tab). |
| 272 std::string tab_json = "null"; | 281 std::string tab_json = "null"; |
| 273 if (source_contents) { | 282 if (source_contents) { |
| 274 scoped_ptr<DictionaryValue> tab_value( | 283 scoped_ptr<DictionaryValue> tab_value( |
| 275 ExtensionTabUtil::CreateTabValue(source_contents)); | 284 ExtensionTabUtil::CreateTabValue(source_contents)); |
| 276 base::JSONWriter::Write(tab_value.get(), &tab_json); | 285 base::JSONWriter::Write(tab_value.get(), &tab_json); |
| 277 } | 286 } |
| 278 | 287 |
| 279 OpenChannelParams params(source, tab_json, receiver, receiver_port_id, | 288 OpenChannelParams* params = new OpenChannelParams(source, tab_json, |
|
Matt Perry
2012/08/16 00:01:36
nit: I prefer making this a scoped_ptr and using p
eaugusti
2012/08/21 23:08:34
Done.
| |
| 280 extension_id, extension_id, channel_name); | 289 receiver.release(), |
| 281 OpenChannelImpl(params); | 290 receiver_port_id, |
| 291 extension_id, extension_id, | |
| 292 channel_name); | |
| 293 OpenChannelImpl(scoped_ptr<OpenChannelParams>(params)); | |
| 282 } | 294 } |
| 283 | 295 |
| 284 bool MessageService::OpenChannelImpl(const OpenChannelParams& params) { | 296 bool MessageService::OpenChannelImpl(scoped_ptr<OpenChannelParams> params) { |
| 285 if (!params.source) | 297 if (!params->source) |
| 286 return false; // Closed while in flight. | 298 return false; // Closed while in flight. |
| 287 | 299 |
| 288 if (!params.receiver.process) { | 300 if (!params->receiver.get() || |
| 301 !params->receiver->AsExtensionMessagePort()->process()) { | |
| 289 // Treat it as a disconnect. | 302 // Treat it as a disconnect. |
| 290 DispatchOnDisconnect(MessagePort(params.source, MSG_ROUTING_CONTROL, ""), | 303 ExtensionMessagePort port(params->source, MSG_ROUTING_CONTROL, ""); |
| 291 GET_OPPOSITE_PORT_ID(params.receiver_port_id), true); | 304 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(params->receiver_port_id), |
| 305 true); | |
| 292 return false; | 306 return false; |
| 293 } | 307 } |
| 294 | 308 |
| 295 // Add extra paranoid CHECKs, since we have crash reports of this being NULL. | 309 // Add extra paranoid CHECKs, since we have crash reports of this being NULL. |
| 296 // http://code.google.com/p/chromium/issues/detail?id=19067 | 310 // http://code.google.com/p/chromium/issues/detail?id=19067 |
| 297 CHECK(params.receiver.process); | 311 CHECK(params->receiver->AsExtensionMessagePort()->process()); |
| 298 | 312 |
| 299 MessageChannel* channel(new MessageChannel); | 313 MessageChannel* channel(new MessageChannel); |
| 300 channel->opener = MessagePort(params.source, MSG_ROUTING_CONTROL, | 314 channel->opener.reset(new ExtensionMessagePort(params->source, |
| 301 params.source_extension_id); | 315 MSG_ROUTING_CONTROL, |
| 302 channel->receiver = params.receiver; | 316 params->source_extension_id)); |
| 317 channel->receiver.reset(params->receiver.release()); | |
| 303 | 318 |
| 304 CHECK(params.receiver.process); | 319 CHECK(channel->receiver->AsExtensionMessagePort()->process()); |
| 305 | 320 |
| 306 int channel_id = GET_CHANNEL_ID(params.receiver_port_id); | 321 AddChannel(channel, params->receiver_port_id); |
| 322 | |
| 323 CHECK(channel->receiver->AsExtensionMessagePort()->process()); | |
| 324 | |
| 325 // Send the connect event to the receiver. Give it the opener's port ID (the | |
| 326 // opener has the opposite port ID). | |
| 327 channel->receiver->DispatchOnConnect(params->receiver_port_id, | |
| 328 params->channel_name, params->tab_json, | |
| 329 params->source_extension_id, | |
| 330 params->target_extension_id); | |
| 331 | |
| 332 // Keep both ends of the channel alive until the channel is closed. | |
| 333 channel->opener->IncrementLazyKeepaliveCount(); | |
| 334 channel->receiver->IncrementLazyKeepaliveCount(); | |
| 335 return true; | |
| 336 } | |
| 337 | |
| 338 void MessageService::AddChannel(MessageChannel* channel, int receiver_port_id) { | |
| 339 int channel_id = GET_CHANNEL_ID(receiver_port_id); | |
| 307 CHECK(channels_.find(channel_id) == channels_.end()); | 340 CHECK(channels_.find(channel_id) == channels_.end()); |
| 308 channels_[channel_id] = channel; | 341 channels_[channel_id] = channel; |
| 309 pending_channels_.erase(channel_id); | 342 pending_channels_.erase(channel_id); |
| 310 | |
| 311 CHECK(params.receiver.process); | |
| 312 | |
| 313 // Send the connect event to the receiver. Give it the opener's port ID (the | |
| 314 // opener has the opposite port ID). | |
| 315 DispatchOnConnect(params.receiver, params.receiver_port_id, | |
| 316 params.channel_name, params.tab_json, | |
| 317 params.source_extension_id, params.target_extension_id); | |
| 318 | |
| 319 // Keep both ends of the channel alive until the channel is closed. | |
| 320 IncrementLazyKeepaliveCount(&channel->opener); | |
| 321 IncrementLazyKeepaliveCount(&channel->receiver); | |
| 322 return true; | |
| 323 } | 343 } |
| 324 | 344 |
| 325 void MessageService::CloseChannel(int port_id, bool connection_error) { | 345 void MessageService::CloseChannel(int port_id, bool connection_error) { |
| 326 // Note: The channel might be gone already, if the other side closed first. | 346 // Note: The channel might be gone already, if the other side closed first. |
| 327 int channel_id = GET_CHANNEL_ID(port_id); | 347 int channel_id = GET_CHANNEL_ID(port_id); |
| 328 MessageChannelMap::iterator it = channels_.find(channel_id); | 348 MessageChannelMap::iterator it = channels_.find(channel_id); |
| 329 if (it == channels_.end()) { | 349 if (it == channels_.end()) { |
| 330 PendingChannelMap::iterator pending = pending_channels_.find(channel_id); | 350 PendingChannelMap::iterator pending = pending_channels_.find(channel_id); |
| 331 if (pending != pending_channels_.end()) { | 351 if (pending != pending_channels_.end()) { |
| 332 lazy_background_task_queue_->AddPendingTask( | 352 lazy_background_task_queue_->AddPendingTask( |
| 333 pending->second.first, pending->second.second, | 353 pending->second.first, pending->second.second, |
| 334 base::Bind(&MessageService::PendingCloseChannel, | 354 base::Bind(&MessageService::PendingCloseChannel, |
| 335 base::Unretained(this), port_id, connection_error)); | 355 base::Unretained(this), port_id, connection_error)); |
| 336 } | 356 } |
| 337 return; | 357 return; |
| 338 } | 358 } |
| 339 CloseChannelImpl(it, port_id, connection_error, true); | 359 CloseChannelImpl(it, port_id, connection_error, true); |
| 340 } | 360 } |
| 341 | 361 |
| 342 void MessageService::CloseChannelImpl( | 362 void MessageService::CloseChannelImpl( |
| 343 MessageChannelMap::iterator channel_iter, int closing_port_id, | 363 MessageChannelMap::iterator channel_iter, int closing_port_id, |
| 344 bool connection_error, bool notify_other_port) { | 364 bool connection_error, bool notify_other_port) { |
| 345 MessageChannel* channel = channel_iter->second; | 365 MessageChannel* channel = channel_iter->second; |
| 346 | 366 |
| 347 // Notify the other side. | 367 // Notify the other side. |
| 348 if (notify_other_port) { | 368 if (notify_other_port) { |
| 349 const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ? | 369 MessagePort* port = IS_OPENER_PORT_ID(closing_port_id) ? |
| 350 channel->receiver : channel->opener; | 370 channel->receiver.get() : channel->opener.get(); |
| 351 DispatchOnDisconnect(port, GET_OPPOSITE_PORT_ID(closing_port_id), | 371 port->DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(closing_port_id), |
| 352 connection_error); | 372 connection_error); |
| 353 } | 373 } |
| 354 | 374 |
| 355 // Balance the addrefs in OpenChannelImpl. | 375 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. |
| 356 DecrementLazyKeepaliveCount(&channel->opener); | 376 channel->opener->DecrementLazyKeepaliveCount(); |
| 357 DecrementLazyKeepaliveCount(&channel->receiver); | 377 channel->receiver->DecrementLazyKeepaliveCount(); |
| 358 | 378 |
| 359 delete channel_iter->second; | 379 delete channel_iter->second; |
| 360 channels_.erase(channel_iter); | 380 channels_.erase(channel_iter); |
| 361 } | 381 } |
| 362 | 382 |
| 363 void MessageService::PostMessageFromRenderer( | 383 void MessageService::PostMessageFromRenderer( |
| 364 int source_port_id, const std::string& message) { | 384 int source_port_id, const std::string& message) { |
| 365 int channel_id = GET_CHANNEL_ID(source_port_id); | 385 int channel_id = GET_CHANNEL_ID(source_port_id); |
| 366 MessageChannelMap::iterator iter = channels_.find(channel_id); | 386 MessageChannelMap::iterator iter = channels_.find(channel_id); |
| 367 if (iter == channels_.end()) { | 387 if (iter == channels_.end()) { |
| 368 // If this channel is pending, queue up the PostMessage to run once | 388 // If this channel is pending, queue up the PostMessage to run once |
| 369 // the channel opens. | 389 // the channel opens. |
| 370 PendingChannelMap::iterator pending = pending_channels_.find(channel_id); | 390 PendingChannelMap::iterator pending = pending_channels_.find(channel_id); |
| 371 if (pending != pending_channels_.end()) { | 391 if (pending != pending_channels_.end()) { |
| 372 lazy_background_task_queue_->AddPendingTask( | 392 lazy_background_task_queue_->AddPendingTask( |
| 373 pending->second.first, pending->second.second, | 393 pending->second.first, pending->second.second, |
| 374 base::Bind(&MessageService::PendingPostMessage, | 394 base::Bind(&MessageService::PendingPostMessage, |
| 375 base::Unretained(this), source_port_id, message)); | 395 base::Unretained(this), source_port_id, message)); |
| 376 } | 396 } |
| 377 return; | 397 return; |
| 378 } | 398 } |
| 379 | 399 |
| 380 // Figure out which port the ID corresponds to. | 400 // Figure out which port the ID corresponds to. |
| 381 int dest_port_id = GET_OPPOSITE_PORT_ID(source_port_id); | 401 int dest_port_id = GET_OPPOSITE_PORT_ID(source_port_id); |
| 382 const MessagePort& port = IS_OPENER_PORT_ID(dest_port_id) ? | 402 MessagePort* port = IS_OPENER_PORT_ID(dest_port_id) ? |
| 383 iter->second->opener : iter->second->receiver; | 403 iter->second->opener.get() : iter->second->receiver.get(); |
| 384 | 404 |
| 385 DispatchOnMessage(port, message, dest_port_id); | 405 port->DispatchOnMessage(message, dest_port_id); |
| 386 } | 406 } |
| 387 | 407 |
| 388 void MessageService::Observe(int type, | 408 void MessageService::Observe(int type, |
| 389 const content::NotificationSource& source, | 409 const content::NotificationSource& source, |
| 390 const content::NotificationDetails& details) { | 410 const content::NotificationDetails& details) { |
| 391 switch (type) { | 411 switch (type) { |
| 392 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | 412 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
| 393 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | 413 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 394 content::RenderProcessHost* renderer = | 414 content::RenderProcessHost* renderer = |
| 395 content::Source<content::RenderProcessHost>(source).ptr(); | 415 content::Source<content::RenderProcessHost>(source).ptr(); |
| 396 OnProcessClosed(renderer); | 416 OnProcessClosed(renderer); |
| 397 break; | 417 break; |
| 398 } | 418 } |
| 399 default: | 419 default: |
| 400 NOTREACHED(); | 420 NOTREACHED(); |
| 401 return; | 421 return; |
| 402 } | 422 } |
| 403 } | 423 } |
| 404 | 424 |
| 405 void MessageService::OnProcessClosed(content::RenderProcessHost* process) { | 425 void MessageService::OnProcessClosed(content::RenderProcessHost* process) { |
| 406 // Close any channels that share this renderer. We notify the opposite | 426 // Close any channels that share this renderer. We notify the opposite |
| 407 // port that his pair has closed. | 427 // port that his pair has closed. |
| 408 for (MessageChannelMap::iterator it = channels_.begin(); | 428 for (MessageChannelMap::iterator it = channels_.begin(); |
| 409 it != channels_.end(); ) { | 429 it != channels_.end(); ) { |
| 410 MessageChannelMap::iterator current = it++; | 430 MessageChannelMap::iterator current = it++; |
| 411 // If both sides are the same renderer, and it is closing, there is no | |
| 412 // "other" port, so there's no need to notify it. | |
| 413 bool notify_other_port = | |
| 414 current->second->opener.process != current->second->receiver.process; | |
| 415 | 431 |
| 416 if (current->second->opener.process == process) { | 432 content::RenderProcessHost* opener_process = |
| 433 current->second->opener->IsNative() ? | |
| 434 NULL : current->second->opener->AsExtensionMessagePort()->process(); | |
| 435 content::RenderProcessHost* receiver_process = | |
| 436 current->second->receiver->IsNative() ? | |
| 437 NULL : current->second->receiver->AsExtensionMessagePort()->process(); | |
|
Matt Perry
2012/08/16 00:01:36
Could replace this check and the IsNative method w
eaugusti
2012/08/21 23:08:34
Removed IsNative().
| |
| 438 | |
| 439 bool notify_other_port = opener_process && | |
| 440 opener_process == receiver_process; | |
| 441 | |
| 442 if (opener_process == process) { | |
| 417 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), | 443 CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), |
| 418 false, notify_other_port); | 444 false, notify_other_port); |
| 419 } else if (current->second->receiver.process == process) { | 445 } else if (receiver_process == process) { |
| 420 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), | 446 CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first), |
| 421 false, notify_other_port); | 447 false, notify_other_port); |
| 422 } | 448 } |
| 423 } | 449 } |
| 424 } | 450 } |
| 425 | 451 |
| 426 bool MessageService::MaybeAddPendingOpenChannelTask( | 452 bool MessageService::MaybeAddPendingOpenChannelTask( |
| 427 Profile* profile, | 453 Profile* profile, |
| 428 const OpenChannelParams& params) { | 454 OpenChannelParams* params) { |
| 429 ExtensionService* service = profile->GetExtensionService(); | 455 ExtensionService* service = profile->GetExtensionService(); |
| 430 const std::string& extension_id = params.target_extension_id; | 456 const std::string& extension_id = params->target_extension_id; |
| 431 const Extension* extension = service->extensions()->GetByID( | 457 const Extension* extension = service->extensions()->GetByID(extension_id); |
| 432 extension_id); | |
| 433 if (extension && extension->has_lazy_background_page()) { | 458 if (extension && extension->has_lazy_background_page()) { |
| 434 // If the extension uses spanning incognito mode, make sure we're always | 459 // If the extension uses spanning incognito mode, make sure we're always |
| 435 // using the original profile since that is what the extension process | 460 // using the original profile since that is what the extension process |
| 436 // will use. | 461 // will use. |
| 437 if (!extension->incognito_split_mode()) | 462 if (!extension->incognito_split_mode()) |
| 438 profile = profile->GetOriginalProfile(); | 463 profile = profile->GetOriginalProfile(); |
| 439 | 464 |
| 440 if (lazy_background_task_queue_->ShouldEnqueueTask(profile, extension)) { | 465 if (lazy_background_task_queue_->ShouldEnqueueTask(profile, extension)) { |
| 466 pending_channels_[GET_CHANNEL_ID(params->receiver_port_id)] = | |
| 467 PendingChannel(profile, extension_id); | |
| 468 scoped_ptr<OpenChannelParams> scoped_params(params); | |
| 441 lazy_background_task_queue_->AddPendingTask(profile, extension_id, | 469 lazy_background_task_queue_->AddPendingTask(profile, extension_id, |
| 442 base::Bind(&MessageService::PendingOpenChannel, | 470 base::Bind(&MessageService::PendingOpenChannel, |
| 443 base::Unretained(this), params, params.source->GetID())); | 471 base::Unretained(this), base::Passed(&scoped_params), |
|
Matt Perry
2012/08/16 00:01:36
can you update the uses of base::Unretained to use
eaugusti
2012/08/21 23:08:34
You got it!
| |
| 444 pending_channels_[GET_CHANNEL_ID(params.receiver_port_id)] = | 472 params->source->GetID())); |
| 445 PendingChannel(profile, extension_id); | |
| 446 return true; | 473 return true; |
| 447 } | 474 } |
| 448 } | 475 } |
| 449 | 476 |
| 450 return false; | 477 return false; |
| 451 } | 478 } |
| 452 | 479 |
| 453 void MessageService::PendingOpenChannel(const OpenChannelParams& params_in, | 480 void MessageService::PendingOpenChannel(scoped_ptr<OpenChannelParams> params, |
| 454 int source_process_id, | 481 int source_process_id, |
| 455 ExtensionHost* host) { | 482 ExtensionHost* host) { |
| 456 if (!host) | 483 if (!host) |
| 457 return; // TODO(mpcomplete): notify source of disconnect? | 484 return; // TODO(mpcomplete): notify source of disconnect? |
| 458 | 485 |
| 459 // Re-lookup the source process since it may no longer be valid. | 486 // Re-lookup the source process since it may no longer be valid. |
| 460 OpenChannelParams params = params_in; | 487 content::RenderProcessHost* source = |
| 461 params.source = content::RenderProcessHost::FromID(source_process_id); | 488 content::RenderProcessHost::FromID(source_process_id); |
| 462 if (!params.source) | 489 if (!source) |
| 463 return; | 490 return; |
| 464 | 491 |
| 465 params.receiver = MessagePort(host->render_process_host(), | 492 params->source = source; |
| 466 MSG_ROUTING_CONTROL, | 493 params->receiver.reset(new ExtensionMessagePort(host->render_process_host(), |
| 467 params.target_extension_id); | 494 MSG_ROUTING_CONTROL, |
| 468 OpenChannelImpl(params); | 495 params->target_extension_id)); |
| 496 OpenChannelImpl(params.Pass()); | |
| 469 } | 497 } |
| 470 | 498 |
| 471 } // namespace extensions | 499 } // namespace extensions |
| OLD | NEW |