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/api/messaging/message_service.h" | 5 #include "chrome/browser/extensions/api/messaging/message_service.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <limits> | 8 #include <limits> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/atomic_sequence_num.h" | |
12 #include "base/bind.h" | 11 #include "base/bind.h" |
13 #include "base/callback.h" | 12 #include "base/callback.h" |
14 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
15 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
16 #include "base/macros.h" | 15 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
18 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
19 #include "build/build_config.h" | 18 #include "build/build_config.h" |
20 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" | 19 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" |
21 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" | 20 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 #include "extensions/common/manifest_handlers/incognito_info.h" | 55 #include "extensions/common/manifest_handlers/incognito_info.h" |
57 #include "extensions/common/permissions/permissions_data.h" | 56 #include "extensions/common/permissions/permissions_data.h" |
58 #include "net/base/completion_callback.h" | 57 #include "net/base/completion_callback.h" |
59 #include "url/gurl.h" | 58 #include "url/gurl.h" |
60 | 59 |
61 using content::BrowserContext; | 60 using content::BrowserContext; |
62 using content::BrowserThread; | 61 using content::BrowserThread; |
63 using content::SiteInstance; | 62 using content::SiteInstance; |
64 using content::WebContents; | 63 using content::WebContents; |
65 | 64 |
66 // Since we have 2 ports for every channel, we just index channels by half the | |
67 // port ID. | |
68 #define GET_CHANNEL_ID(port_id) ((port_id) / 2) | |
69 #define GET_CHANNEL_OPENER_ID(channel_id) ((channel_id) * 2) | |
70 #define GET_CHANNEL_RECEIVERS_ID(channel_id) ((channel_id) * 2 + 1) | |
71 | |
72 // Port1 is always even, port2 is always odd. | |
73 #define IS_OPENER_PORT_ID(port_id) (((port_id) & 1) == 0) | |
74 | |
75 // Change even to odd and vice versa, to get the other side of a given channel. | |
76 #define GET_OPPOSITE_PORT_ID(source_port_id) ((source_port_id) ^ 1) | |
77 | |
78 namespace extensions { | 65 namespace extensions { |
79 | 66 |
80 MessageService::PolicyPermission MessageService::IsNativeMessagingHostAllowed( | 67 MessageService::PolicyPermission MessageService::IsNativeMessagingHostAllowed( |
81 const PrefService* pref_service, | 68 const PrefService* pref_service, |
82 const std::string& native_host_name) { | 69 const std::string& native_host_name) { |
83 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
84 | 71 |
85 PolicyPermission allow_result = ALLOW_ALL; | 72 PolicyPermission allow_result = ALLOW_ALL; |
86 if (pref_service->IsManagedPreference( | 73 if (pref_service->IsManagedPreference( |
87 pref_names::kNativeMessagingUserLevelHosts)) { | 74 pref_names::kNativeMessagingUserLevelHosts)) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 std::unique_ptr<MessagePort> opener; | 118 std::unique_ptr<MessagePort> opener; |
132 std::unique_ptr<MessagePort> receiver; | 119 std::unique_ptr<MessagePort> receiver; |
133 }; | 120 }; |
134 | 121 |
135 struct MessageService::OpenChannelParams { | 122 struct MessageService::OpenChannelParams { |
136 int source_process_id; | 123 int source_process_id; |
137 int source_routing_id; | 124 int source_routing_id; |
138 std::unique_ptr<base::DictionaryValue> source_tab; | 125 std::unique_ptr<base::DictionaryValue> source_tab; |
139 int source_frame_id; | 126 int source_frame_id; |
140 std::unique_ptr<MessagePort> receiver; | 127 std::unique_ptr<MessagePort> receiver; |
141 int receiver_port_id; | 128 PortId receiver_port_id; |
142 std::string source_extension_id; | 129 std::string source_extension_id; |
143 std::string target_extension_id; | 130 std::string target_extension_id; |
144 GURL source_url; | 131 GURL source_url; |
145 std::string channel_name; | 132 std::string channel_name; |
146 bool include_tls_channel_id; | 133 bool include_tls_channel_id; |
147 std::string tls_channel_id; | 134 std::string tls_channel_id; |
148 bool include_guest_process_info; | 135 bool include_guest_process_info; |
149 | 136 |
150 // Takes ownership of receiver. | 137 // Takes ownership of receiver. |
151 OpenChannelParams(int source_process_id, | 138 OpenChannelParams(int source_process_id, |
152 int source_routing_id, | 139 int source_routing_id, |
153 std::unique_ptr<base::DictionaryValue> source_tab, | 140 std::unique_ptr<base::DictionaryValue> source_tab, |
154 int source_frame_id, | 141 int source_frame_id, |
155 MessagePort* receiver, | 142 MessagePort* receiver, |
156 int receiver_port_id, | 143 const PortId& receiver_port_id, |
157 const std::string& source_extension_id, | 144 const std::string& source_extension_id, |
158 const std::string& target_extension_id, | 145 const std::string& target_extension_id, |
159 const GURL& source_url, | 146 const GURL& source_url, |
160 const std::string& channel_name, | 147 const std::string& channel_name, |
161 bool include_tls_channel_id, | 148 bool include_tls_channel_id, |
162 bool include_guest_process_info) | 149 bool include_guest_process_info) |
163 : source_process_id(source_process_id), | 150 : source_process_id(source_process_id), |
164 source_routing_id(source_routing_id), | 151 source_routing_id(source_routing_id), |
165 source_frame_id(source_frame_id), | 152 source_frame_id(source_frame_id), |
166 receiver(receiver), | 153 receiver(receiver), |
167 receiver_port_id(receiver_port_id), | 154 receiver_port_id(receiver_port_id), |
168 source_extension_id(source_extension_id), | 155 source_extension_id(source_extension_id), |
169 target_extension_id(target_extension_id), | 156 target_extension_id(target_extension_id), |
170 source_url(source_url), | 157 source_url(source_url), |
171 channel_name(channel_name), | 158 channel_name(channel_name), |
172 include_tls_channel_id(include_tls_channel_id), | 159 include_tls_channel_id(include_tls_channel_id), |
173 include_guest_process_info(include_guest_process_info) { | 160 include_guest_process_info(include_guest_process_info) { |
174 if (source_tab) | 161 if (source_tab) |
175 this->source_tab = std::move(source_tab); | 162 this->source_tab = std::move(source_tab); |
176 } | 163 } |
177 | 164 |
178 private: | 165 private: |
179 DISALLOW_COPY_AND_ASSIGN(OpenChannelParams); | 166 DISALLOW_COPY_AND_ASSIGN(OpenChannelParams); |
180 }; | 167 }; |
181 | 168 |
182 namespace { | 169 namespace { |
183 | 170 |
184 static base::StaticAtomicSequenceNumber g_next_channel_id; | |
185 | |
186 static content::RenderProcessHost* GetExtensionProcess( | 171 static content::RenderProcessHost* GetExtensionProcess( |
187 BrowserContext* context, | 172 BrowserContext* context, |
188 const std::string& extension_id) { | 173 const std::string& extension_id) { |
189 scoped_refptr<SiteInstance> site_instance = | 174 scoped_refptr<SiteInstance> site_instance = |
190 ProcessManager::Get(context)->GetSiteInstanceForURL( | 175 ProcessManager::Get(context)->GetSiteInstanceForURL( |
191 Extension::GetBaseURLFromExtensionId(extension_id)); | 176 Extension::GetBaseURLFromExtensionId(extension_id)); |
192 return site_instance->HasProcess() ? site_instance->GetProcess() : NULL; | 177 return site_instance->HasProcess() ? site_instance->GetProcess() : NULL; |
193 } | 178 } |
194 | 179 |
195 } // namespace | 180 } // namespace |
196 | 181 |
197 void MessageService::MessagePort::RemoveCommonFrames(const MessagePort& port) {} | 182 void MessageService::MessagePort::RemoveCommonFrames(const MessagePort& port) {} |
198 | 183 |
199 bool MessageService::MessagePort::HasFrame( | 184 bool MessageService::MessagePort::HasFrame( |
200 content::RenderFrameHost* rfh) const { | 185 content::RenderFrameHost* rfh) const { |
201 return false; | 186 return false; |
202 } | 187 } |
203 | 188 |
204 // static | |
205 void MessageService::AllocatePortIdPair(int* port1, int* port2) { | |
206 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
207 | |
208 unsigned channel_id = static_cast<unsigned>(g_next_channel_id.GetNext()) % | |
209 (std::numeric_limits<int32_t>::max() / 2); | |
210 unsigned port1_id = channel_id * 2; | |
211 unsigned port2_id = channel_id * 2 + 1; | |
212 | |
213 // Sanity checks to make sure our channel<->port converters are correct. | |
214 DCHECK(IS_OPENER_PORT_ID(port1_id)); | |
215 DCHECK_EQ(GET_OPPOSITE_PORT_ID(port1_id), port2_id); | |
216 DCHECK_EQ(GET_OPPOSITE_PORT_ID(port2_id), port1_id); | |
217 DCHECK_EQ(GET_CHANNEL_ID(port1_id), GET_CHANNEL_ID(port2_id)); | |
218 DCHECK_EQ(GET_CHANNEL_ID(port1_id), channel_id); | |
219 DCHECK_EQ(GET_CHANNEL_OPENER_ID(channel_id), port1_id); | |
220 DCHECK_EQ(GET_CHANNEL_RECEIVERS_ID(channel_id), port2_id); | |
221 | |
222 *port1 = port1_id; | |
223 *port2 = port2_id; | |
224 } | |
225 | |
226 MessageService::MessageService(BrowserContext* context) | 189 MessageService::MessageService(BrowserContext* context) |
227 : lazy_background_task_queue_( | 190 : lazy_background_task_queue_( |
228 LazyBackgroundTaskQueue::Get(context)), | 191 LazyBackgroundTaskQueue::Get(context)), |
229 weak_factory_(this) { | 192 weak_factory_(this) { |
230 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 193 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
231 } | 194 } |
232 | 195 |
233 MessageService::~MessageService() { | 196 MessageService::~MessageService() { |
234 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 197 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
235 | 198 |
236 channels_.clear(); | 199 channels_.clear(); |
237 } | 200 } |
238 | 201 |
239 static base::LazyInstance<BrowserContextKeyedAPIFactory<MessageService> > | 202 static base::LazyInstance<BrowserContextKeyedAPIFactory<MessageService> > |
240 g_factory = LAZY_INSTANCE_INITIALIZER; | 203 g_factory = LAZY_INSTANCE_INITIALIZER; |
241 | 204 |
242 // static | 205 // static |
243 BrowserContextKeyedAPIFactory<MessageService>* | 206 BrowserContextKeyedAPIFactory<MessageService>* |
244 MessageService::GetFactoryInstance() { | 207 MessageService::GetFactoryInstance() { |
245 return g_factory.Pointer(); | 208 return g_factory.Pointer(); |
246 } | 209 } |
247 | 210 |
248 // static | 211 // static |
249 MessageService* MessageService::Get(BrowserContext* context) { | 212 MessageService* MessageService::Get(BrowserContext* context) { |
250 return BrowserContextKeyedAPIFactory<MessageService>::Get(context); | 213 return BrowserContextKeyedAPIFactory<MessageService>::Get(context); |
251 } | 214 } |
252 | 215 |
253 void MessageService::OpenChannelToExtension( | 216 void MessageService::OpenChannelToExtension( |
254 int source_process_id, int source_routing_id, int receiver_port_id, | 217 int source_process_id, |
| 218 int source_routing_id, |
| 219 const PortId& source_port_id, |
255 const std::string& source_extension_id, | 220 const std::string& source_extension_id, |
256 const std::string& target_extension_id, | 221 const std::string& target_extension_id, |
257 const GURL& source_url, | 222 const GURL& source_url, |
258 const std::string& channel_name, | 223 const std::string& channel_name, |
259 bool include_tls_channel_id) { | 224 bool include_tls_channel_id) { |
260 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 225 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 226 DCHECK(source_port_id.is_opener); |
261 | 227 |
262 content::RenderFrameHost* source = | 228 content::RenderFrameHost* source = |
263 content::RenderFrameHost::FromID(source_process_id, source_routing_id); | 229 content::RenderFrameHost::FromID(source_process_id, source_routing_id); |
264 if (!source) | 230 if (!source) |
265 return; | 231 return; |
266 BrowserContext* context = source->GetProcess()->GetBrowserContext(); | 232 BrowserContext* context = source->GetProcess()->GetBrowserContext(); |
267 | 233 |
268 ExtensionRegistry* registry = ExtensionRegistry::Get(context); | 234 ExtensionRegistry* registry = ExtensionRegistry::Get(context); |
269 const Extension* target_extension = | 235 const Extension* target_extension = |
270 registry->enabled_extensions().GetByID(target_extension_id); | 236 registry->enabled_extensions().GetByID(target_extension_id); |
| 237 PortId receiver_port_id(source_port_id.context_id, source_port_id.port_number, |
| 238 false); |
271 if (!target_extension) { | 239 if (!target_extension) { |
272 DispatchOnDisconnect( | 240 DispatchOnDisconnect( |
273 source, receiver_port_id, kReceivingEndDoesntExistError); | 241 source, receiver_port_id, kReceivingEndDoesntExistError); |
274 return; | 242 return; |
275 } | 243 } |
276 | 244 |
277 bool is_web_connection = false; | 245 bool is_web_connection = false; |
278 | 246 |
279 if (source_extension_id != target_extension_id) { | 247 if (source_extension_id != target_extension_id) { |
280 // It's an external connection. Check the externally_connectable manifest | 248 // It's an external connection. Check the externally_connectable manifest |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 include_guest_process_info = true; | 316 include_guest_process_info = true; |
349 } | 317 } |
350 } | 318 } |
351 | 319 |
352 std::unique_ptr<OpenChannelParams> params(new OpenChannelParams( | 320 std::unique_ptr<OpenChannelParams> params(new OpenChannelParams( |
353 source_process_id, source_routing_id, std::move(source_tab), | 321 source_process_id, source_routing_id, std::move(source_tab), |
354 source_frame_id, nullptr, receiver_port_id, source_extension_id, | 322 source_frame_id, nullptr, receiver_port_id, source_extension_id, |
355 target_extension_id, source_url, channel_name, include_tls_channel_id, | 323 target_extension_id, source_url, channel_name, include_tls_channel_id, |
356 include_guest_process_info)); | 324 include_guest_process_info)); |
357 | 325 |
358 pending_incognito_channels_[GET_CHANNEL_ID(params->receiver_port_id)] = | 326 pending_incognito_channels_[params->receiver_port_id.GetChannelId()] = |
359 PendingMessagesQueue(); | 327 PendingMessagesQueue(); |
360 if (context->IsOffTheRecord() && | 328 if (context->IsOffTheRecord() && |
361 !util::IsIncognitoEnabled(target_extension_id, context)) { | 329 !util::IsIncognitoEnabled(target_extension_id, context)) { |
362 // Give the user a chance to accept an incognito connection from the web if | 330 // Give the user a chance to accept an incognito connection from the web if |
363 // they haven't already, with the conditions: | 331 // they haven't already, with the conditions: |
364 // - Only for spanning-mode incognito. We don't want the complication of | 332 // - Only for spanning-mode incognito. We don't want the complication of |
365 // spinning up an additional process here which might need to do some | 333 // spinning up an additional process here which might need to do some |
366 // setup that we're not expecting. | 334 // setup that we're not expecting. |
367 // - Only for extensions that can't normally be enabled in incognito, since | 335 // - Only for extensions that can't normally be enabled in incognito, since |
368 // that surface (e.g. chrome://extensions) should be the only one for | 336 // that surface (e.g. chrome://extensions) should be the only one for |
(...skipping 29 matching lines...) Expand all Loading... |
398 weak_factory_.GetWeakPtr(), base::Passed(¶ms))); | 366 weak_factory_.GetWeakPtr(), base::Passed(¶ms))); |
399 return; | 367 return; |
400 } | 368 } |
401 | 369 |
402 OnOpenChannelAllowed(std::move(params), true); | 370 OnOpenChannelAllowed(std::move(params), true); |
403 } | 371 } |
404 | 372 |
405 void MessageService::OpenChannelToNativeApp( | 373 void MessageService::OpenChannelToNativeApp( |
406 int source_process_id, | 374 int source_process_id, |
407 int source_routing_id, | 375 int source_routing_id, |
408 int receiver_port_id, | 376 const PortId& source_port_id, |
409 const std::string& native_app_name) { | 377 const std::string& native_app_name) { |
410 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 378 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 379 DCHECK(source_port_id.is_opener); |
411 | 380 |
412 content::RenderFrameHost* source = | 381 content::RenderFrameHost* source = |
413 content::RenderFrameHost::FromID(source_process_id, source_routing_id); | 382 content::RenderFrameHost::FromID(source_process_id, source_routing_id); |
414 if (!source) | 383 if (!source) |
415 return; | 384 return; |
416 | 385 |
417 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) | 386 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
418 content::WebContents* web_contents = | 387 content::WebContents* web_contents = |
419 content::WebContents::FromRenderFrameHost(source); | 388 content::WebContents::FromRenderFrameHost(source); |
420 ExtensionWebContentsObserver* extension_web_contents_observer = | 389 ExtensionWebContentsObserver* extension_web_contents_observer = |
421 web_contents ? | 390 web_contents ? |
422 ExtensionWebContentsObserver::GetForWebContents(web_contents) : | 391 ExtensionWebContentsObserver::GetForWebContents(web_contents) : |
423 nullptr; | 392 nullptr; |
424 const Extension* extension = | 393 const Extension* extension = |
425 extension_web_contents_observer ? | 394 extension_web_contents_observer ? |
426 extension_web_contents_observer->GetExtensionFromFrame(source, true) : | 395 extension_web_contents_observer->GetExtensionFromFrame(source, true) : |
427 nullptr; | 396 nullptr; |
428 | 397 |
429 bool has_permission = extension && | 398 bool has_permission = extension && |
430 extension->permissions_data()->HasAPIPermission( | 399 extension->permissions_data()->HasAPIPermission( |
431 APIPermission::kNativeMessaging); | 400 APIPermission::kNativeMessaging); |
432 | 401 |
| 402 PortId receiver_port_id(source_port_id.context_id, source_port_id.port_number, |
| 403 false); |
433 if (!has_permission) { | 404 if (!has_permission) { |
434 DispatchOnDisconnect(source, receiver_port_id, kMissingPermissionError); | 405 DispatchOnDisconnect(source, receiver_port_id, kMissingPermissionError); |
435 return; | 406 return; |
436 } | 407 } |
437 | 408 |
438 PrefService* pref_service = | 409 PrefService* pref_service = |
439 Profile::FromBrowserContext(source->GetProcess()->GetBrowserContext())-> | 410 Profile::FromBrowserContext(source->GetProcess()->GetBrowserContext())-> |
440 GetPrefs(); | 411 GetPrefs(); |
441 | 412 |
442 // Verify that the host is not blocked by policies. | 413 // Verify that the host is not blocked by policies. |
443 PolicyPermission policy_permission = | 414 PolicyPermission policy_permission = |
444 IsNativeMessagingHostAllowed(pref_service, native_app_name); | 415 IsNativeMessagingHostAllowed(pref_service, native_app_name); |
445 if (policy_permission == DISALLOW) { | 416 if (policy_permission == DISALLOW) { |
446 DispatchOnDisconnect(source, receiver_port_id, kProhibitedByPoliciesError); | 417 DispatchOnDisconnect(source, receiver_port_id, kProhibitedByPoliciesError); |
447 return; | 418 return; |
448 } | 419 } |
449 | 420 |
450 std::unique_ptr<MessageChannel> channel(new MessageChannel()); | 421 std::unique_ptr<MessageChannel> channel(new MessageChannel()); |
451 channel->opener.reset( | 422 channel->opener.reset( |
452 new ExtensionMessagePort(weak_factory_.GetWeakPtr(), | 423 new ExtensionMessagePort(weak_factory_.GetWeakPtr(), source_port_id, |
453 GET_OPPOSITE_PORT_ID(receiver_port_id), | |
454 extension->id(), source, false)); | 424 extension->id(), source, false)); |
455 if (!channel->opener->IsValidPort()) | 425 if (!channel->opener->IsValidPort()) |
456 return; | 426 return; |
457 channel->opener->OpenPort(source_process_id, source_routing_id); | 427 channel->opener->OpenPort(source_process_id, source_routing_id); |
458 | 428 |
459 // Get handle of the native view and pass it to the native messaging host. | 429 // Get handle of the native view and pass it to the native messaging host. |
460 gfx::NativeView native_view = source ? source->GetNativeView() : nullptr; | 430 gfx::NativeView native_view = source ? source->GetNativeView() : nullptr; |
461 | 431 |
462 std::string error = kReceivingEndDoesntExistError; | 432 std::string error = kReceivingEndDoesntExistError; |
463 std::unique_ptr<NativeMessageHost> native_host = NativeMessageHost::Create( | 433 std::unique_ptr<NativeMessageHost> native_host = NativeMessageHost::Create( |
(...skipping 16 matching lines...) Expand all Loading... |
480 #else // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) | 450 #else // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) |
481 const char kNativeMessagingNotSupportedError[] = | 451 const char kNativeMessagingNotSupportedError[] = |
482 "Native Messaging is not supported on this platform."; | 452 "Native Messaging is not supported on this platform."; |
483 DispatchOnDisconnect( | 453 DispatchOnDisconnect( |
484 source, receiver_port_id, kNativeMessagingNotSupportedError); | 454 source, receiver_port_id, kNativeMessagingNotSupportedError); |
485 #endif // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) | 455 #endif // !(defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)) |
486 } | 456 } |
487 | 457 |
488 void MessageService::OpenChannelToTab(int source_process_id, | 458 void MessageService::OpenChannelToTab(int source_process_id, |
489 int source_routing_id, | 459 int source_routing_id, |
490 int receiver_port_id, | 460 const PortId& source_port_id, |
491 int tab_id, | 461 int tab_id, |
492 int frame_id, | 462 int frame_id, |
493 const std::string& extension_id, | 463 const std::string& extension_id, |
494 const std::string& channel_name) { | 464 const std::string& channel_name) { |
495 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 465 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
496 DCHECK_GE(frame_id, -1); | 466 DCHECK_GE(frame_id, -1); |
| 467 DCHECK(source_port_id.is_opener); |
497 | 468 |
498 content::RenderFrameHost* source = | 469 content::RenderFrameHost* source = |
499 content::RenderFrameHost::FromID(source_process_id, source_routing_id); | 470 content::RenderFrameHost::FromID(source_process_id, source_routing_id); |
500 if (!source) | 471 if (!source) |
501 return; | 472 return; |
502 Profile* profile = | 473 Profile* profile = |
503 Profile::FromBrowserContext(source->GetProcess()->GetBrowserContext()); | 474 Profile::FromBrowserContext(source->GetProcess()->GetBrowserContext()); |
504 | 475 |
505 WebContents* contents = NULL; | 476 WebContents* contents = NULL; |
506 std::unique_ptr<MessagePort> receiver; | 477 std::unique_ptr<MessagePort> receiver; |
| 478 PortId receiver_port_id(source_port_id.context_id, source_port_id.port_number, |
| 479 false); |
507 if (!ExtensionTabUtil::GetTabById(tab_id, profile, true, NULL, NULL, | 480 if (!ExtensionTabUtil::GetTabById(tab_id, profile, true, NULL, NULL, |
508 &contents, NULL) || | 481 &contents, NULL) || |
509 contents->GetController().NeedsReload()) { | 482 contents->GetController().NeedsReload()) { |
510 // The tab isn't loaded yet. Don't attempt to connect. | 483 // The tab isn't loaded yet. Don't attempt to connect. |
511 DispatchOnDisconnect( | 484 DispatchOnDisconnect( |
512 source, receiver_port_id, kReceivingEndDoesntExistError); | 485 source, receiver_port_id, kReceivingEndDoesntExistError); |
513 return; | 486 return; |
514 } | 487 } |
515 | 488 |
516 // Frame ID -1 is every frame in the tab. | 489 // Frame ID -1 is every frame in the tab. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
567 return; // Closed while in flight. | 540 return; // Closed while in flight. |
568 | 541 |
569 if (!params->receiver || !params->receiver->IsValidPort()) { | 542 if (!params->receiver || !params->receiver->IsValidPort()) { |
570 DispatchOnDisconnect(source, params->receiver_port_id, | 543 DispatchOnDisconnect(source, params->receiver_port_id, |
571 kReceivingEndDoesntExistError); | 544 kReceivingEndDoesntExistError); |
572 return; | 545 return; |
573 } | 546 } |
574 | 547 |
575 std::unique_ptr<ExtensionMessagePort> opener( | 548 std::unique_ptr<ExtensionMessagePort> opener( |
576 new ExtensionMessagePort(weak_factory_.GetWeakPtr(), | 549 new ExtensionMessagePort(weak_factory_.GetWeakPtr(), |
577 GET_OPPOSITE_PORT_ID(params->receiver_port_id), | 550 params->receiver_port_id.GetOppositePortId(), |
578 params->source_extension_id, source, false)); | 551 params->source_extension_id, source, false)); |
579 if (!opener->IsValidPort()) | 552 if (!opener->IsValidPort()) |
580 return; | 553 return; |
581 opener->OpenPort(params->source_process_id, params->source_routing_id); | 554 opener->OpenPort(params->source_process_id, params->source_routing_id); |
582 opener->RevalidatePort(); | 555 opener->RevalidatePort(); |
583 | 556 |
584 params->receiver->RemoveCommonFrames(*opener); | 557 params->receiver->RemoveCommonFrames(*opener); |
585 if (!params->receiver->IsValidPort()) { | 558 if (!params->receiver->IsValidPort()) { |
586 opener->DispatchOnDisconnect(kReceivingEndDoesntExistError); | 559 opener->DispatchOnDisconnect(kReceivingEndDoesntExistError); |
587 return; | 560 return; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 EventRouter::Get(browser_context) | 620 EventRouter::Get(browser_context) |
648 ->ReportEvent(histogram_value, target_extension, did_enqueue); | 621 ->ReportEvent(histogram_value, target_extension, did_enqueue); |
649 } | 622 } |
650 | 623 |
651 // Keep both ends of the channel alive until the channel is closed. | 624 // Keep both ends of the channel alive until the channel is closed. |
652 channel->opener->IncrementLazyKeepaliveCount(); | 625 channel->opener->IncrementLazyKeepaliveCount(); |
653 channel->receiver->IncrementLazyKeepaliveCount(); | 626 channel->receiver->IncrementLazyKeepaliveCount(); |
654 } | 627 } |
655 | 628 |
656 void MessageService::AddChannel(std::unique_ptr<MessageChannel> channel, | 629 void MessageService::AddChannel(std::unique_ptr<MessageChannel> channel, |
657 int receiver_port_id) { | 630 const PortId& receiver_port_id) { |
658 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 631 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
659 | 632 |
660 int channel_id = GET_CHANNEL_ID(receiver_port_id); | 633 ChannelId channel_id = receiver_port_id.GetChannelId(); |
661 CHECK(channels_.find(channel_id) == channels_.end()); | 634 CHECK(channels_.find(channel_id) == channels_.end()); |
662 channels_[channel_id] = std::move(channel); | 635 channels_[channel_id] = std::move(channel); |
663 pending_lazy_background_page_channels_.erase(channel_id); | 636 pending_lazy_background_page_channels_.erase(channel_id); |
664 } | 637 } |
665 | 638 |
666 void MessageService::OpenPort(int port_id, int process_id, int routing_id) { | 639 void MessageService::OpenPort(const PortId& port_id, |
| 640 int process_id, |
| 641 int routing_id) { |
667 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 642 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
668 DCHECK(!IS_OPENER_PORT_ID(port_id)); | 643 DCHECK(!port_id.is_opener); |
669 | 644 |
670 int channel_id = GET_CHANNEL_ID(port_id); | 645 ChannelId channel_id = port_id.GetChannelId(); |
671 MessageChannelMap::iterator it = channels_.find(channel_id); | 646 MessageChannelMap::iterator it = channels_.find(channel_id); |
672 if (it == channels_.end()) | 647 if (it == channels_.end()) |
673 return; | 648 return; |
674 | 649 |
675 it->second->receiver->OpenPort(process_id, routing_id); | 650 it->second->receiver->OpenPort(process_id, routing_id); |
676 } | 651 } |
677 | 652 |
678 void MessageService::ClosePort( | 653 void MessageService::ClosePort(const PortId& port_id, |
679 int port_id, int process_id, int routing_id, bool force_close) { | 654 int process_id, |
| 655 int routing_id, |
| 656 bool force_close) { |
680 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 657 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
681 ClosePortImpl(port_id, process_id, routing_id, force_close, std::string()); | 658 ClosePortImpl(port_id, process_id, routing_id, force_close, std::string()); |
682 } | 659 } |
683 | 660 |
684 void MessageService::CloseChannel(int port_id, | 661 void MessageService::CloseChannel(const PortId& port_id, |
685 const std::string& error_message) { | 662 const std::string& error_message) { |
686 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 663 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
687 ClosePortImpl(port_id, content::ChildProcessHost::kInvalidUniqueID, | 664 ClosePortImpl(port_id, content::ChildProcessHost::kInvalidUniqueID, |
688 MSG_ROUTING_NONE, true, error_message); | 665 MSG_ROUTING_NONE, true, error_message); |
689 } | 666 } |
690 | 667 |
691 void MessageService::ClosePortImpl(int port_id, | 668 void MessageService::ClosePortImpl(const PortId& port_id, |
692 int process_id, | 669 int process_id, |
693 int routing_id, | 670 int routing_id, |
694 bool force_close, | 671 bool force_close, |
695 const std::string& error_message) { | 672 const std::string& error_message) { |
696 // Note: The channel might be gone already, if the other side closed first. | 673 // Note: The channel might be gone already, if the other side closed first. |
697 int channel_id = GET_CHANNEL_ID(port_id); | 674 ChannelId channel_id = port_id.GetChannelId(); |
698 MessageChannelMap::iterator it = channels_.find(channel_id); | 675 MessageChannelMap::iterator it = channels_.find(channel_id); |
699 if (it == channels_.end()) { | 676 if (it == channels_.end()) { |
700 PendingLazyBackgroundPageChannelMap::iterator pending = | 677 PendingLazyBackgroundPageChannelMap::iterator pending = |
701 pending_lazy_background_page_channels_.find(channel_id); | 678 pending_lazy_background_page_channels_.find(channel_id); |
702 if (pending != pending_lazy_background_page_channels_.end()) { | 679 if (pending != pending_lazy_background_page_channels_.end()) { |
703 lazy_background_task_queue_->AddPendingTask( | 680 lazy_background_task_queue_->AddPendingTask( |
704 pending->second.first, pending->second.second, | 681 pending->second.first, pending->second.second, |
705 base::Bind(&MessageService::PendingLazyBackgroundPageClosePort, | 682 base::Bind(&MessageService::PendingLazyBackgroundPageClosePort, |
706 weak_factory_.GetWeakPtr(), port_id, process_id, | 683 weak_factory_.GetWeakPtr(), port_id, process_id, |
707 routing_id, force_close, error_message)); | 684 routing_id, force_close, error_message)); |
708 } | 685 } |
709 return; | 686 return; |
710 } | 687 } |
711 | 688 |
712 // The difference between closing a channel and port is that closing a port | 689 // The difference between closing a channel and port is that closing a port |
713 // does not necessarily have to destroy the channel if there are multiple | 690 // does not necessarily have to destroy the channel if there are multiple |
714 // receivers, whereas closing a channel always forces all ports to be closed. | 691 // receivers, whereas closing a channel always forces all ports to be closed. |
715 if (force_close) { | 692 if (force_close) { |
716 CloseChannelImpl(it, port_id, error_message, true); | 693 CloseChannelImpl(it, port_id, error_message, true); |
717 } else if (IS_OPENER_PORT_ID(port_id)) { | 694 } else if (port_id.is_opener) { |
718 it->second->opener->ClosePort(process_id, routing_id); | 695 it->second->opener->ClosePort(process_id, routing_id); |
719 } else { | 696 } else { |
720 it->second->receiver->ClosePort(process_id, routing_id); | 697 it->second->receiver->ClosePort(process_id, routing_id); |
721 } | 698 } |
722 } | 699 } |
723 | 700 |
724 void MessageService::CloseChannelImpl( | 701 void MessageService::CloseChannelImpl(MessageChannelMap::iterator channel_iter, |
725 MessageChannelMap::iterator channel_iter, | 702 const PortId& closing_port_id, |
726 int closing_port_id, | 703 const std::string& error_message, |
727 const std::string& error_message, | 704 bool notify_other_port) { |
728 bool notify_other_port) { | |
729 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 705 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
730 | 706 |
731 std::unique_ptr<MessageChannel> channel = std::move(channel_iter->second); | 707 std::unique_ptr<MessageChannel> channel = std::move(channel_iter->second); |
732 // Remove from map to make sure that it is impossible for CloseChannelImpl to | 708 // Remove from map to make sure that it is impossible for CloseChannelImpl to |
733 // run twice for the same channel. | 709 // run twice for the same channel. |
734 channels_.erase(channel_iter); | 710 channels_.erase(channel_iter); |
735 | 711 |
736 // Notify the other side. | 712 // Notify the other side. |
737 if (notify_other_port) { | 713 if (notify_other_port) { |
738 MessagePort* port = IS_OPENER_PORT_ID(closing_port_id) ? | 714 MessagePort* port = closing_port_id.is_opener ? channel->receiver.get() |
739 channel->receiver.get() : channel->opener.get(); | 715 : channel->opener.get(); |
740 port->DispatchOnDisconnect(error_message); | 716 port->DispatchOnDisconnect(error_message); |
741 } | 717 } |
742 | 718 |
743 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. | 719 // Balance the IncrementLazyKeepaliveCount() in OpenChannelImpl. |
744 channel->opener->DecrementLazyKeepaliveCount(); | 720 channel->opener->DecrementLazyKeepaliveCount(); |
745 channel->receiver->DecrementLazyKeepaliveCount(); | 721 channel->receiver->DecrementLazyKeepaliveCount(); |
746 } | 722 } |
747 | 723 |
748 void MessageService::PostMessage(int source_port_id, const Message& message) { | 724 void MessageService::PostMessage(const PortId& source_port_id, |
| 725 const Message& message) { |
749 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 726 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
750 | 727 |
751 int channel_id = GET_CHANNEL_ID(source_port_id); | 728 ChannelId channel_id = source_port_id.GetChannelId(); |
752 MessageChannelMap::iterator iter = channels_.find(channel_id); | 729 MessageChannelMap::iterator iter = channels_.find(channel_id); |
753 if (iter == channels_.end()) { | 730 if (iter == channels_.end()) { |
754 // If this channel is pending, queue up the PostMessage to run once | 731 // If this channel is pending, queue up the PostMessage to run once |
755 // the channel opens. | 732 // the channel opens. |
756 EnqueuePendingMessage(source_port_id, channel_id, message); | 733 EnqueuePendingMessage(source_port_id, channel_id, message); |
757 return; | 734 return; |
758 } | 735 } |
759 | 736 |
760 DispatchMessage(source_port_id, iter->second.get(), message); | 737 DispatchMessage(source_port_id, iter->second.get(), message); |
761 } | 738 } |
762 | 739 |
763 void MessageService::EnqueuePendingMessage(int source_port_id, | 740 void MessageService::EnqueuePendingMessage(const PortId& source_port_id, |
764 int channel_id, | 741 const ChannelId& channel_id, |
765 const Message& message) { | 742 const Message& message) { |
766 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 743 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
767 | 744 |
768 PendingChannelMap::iterator pending_for_incognito = | 745 PendingChannelMap::iterator pending_for_incognito = |
769 pending_incognito_channels_.find(channel_id); | 746 pending_incognito_channels_.find(channel_id); |
770 if (pending_for_incognito != pending_incognito_channels_.end()) { | 747 if (pending_for_incognito != pending_incognito_channels_.end()) { |
771 pending_for_incognito->second.push_back( | 748 pending_for_incognito->second.push_back( |
772 PendingMessage(source_port_id, message)); | 749 PendingMessage(source_port_id, message)); |
773 // A channel should only be holding pending messages because it is in one | 750 // A channel should only be holding pending messages because it is in one |
774 // of these states. | 751 // of these states. |
(...skipping 12 matching lines...) Expand all Loading... |
787 DCHECK( | 764 DCHECK( |
788 !base::ContainsKey(pending_lazy_background_page_channels_, channel_id)); | 765 !base::ContainsKey(pending_lazy_background_page_channels_, channel_id)); |
789 return; | 766 return; |
790 } | 767 } |
791 EnqueuePendingMessageForLazyBackgroundLoad(source_port_id, | 768 EnqueuePendingMessageForLazyBackgroundLoad(source_port_id, |
792 channel_id, | 769 channel_id, |
793 message); | 770 message); |
794 } | 771 } |
795 | 772 |
796 void MessageService::EnqueuePendingMessageForLazyBackgroundLoad( | 773 void MessageService::EnqueuePendingMessageForLazyBackgroundLoad( |
797 int source_port_id, | 774 const PortId& source_port_id, |
798 int channel_id, | 775 const ChannelId& channel_id, |
799 const Message& message) { | 776 const Message& message) { |
800 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 777 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
801 | 778 |
802 PendingLazyBackgroundPageChannelMap::iterator pending = | 779 PendingLazyBackgroundPageChannelMap::iterator pending = |
803 pending_lazy_background_page_channels_.find(channel_id); | 780 pending_lazy_background_page_channels_.find(channel_id); |
804 if (pending != pending_lazy_background_page_channels_.end()) { | 781 if (pending != pending_lazy_background_page_channels_.end()) { |
805 lazy_background_task_queue_->AddPendingTask( | 782 lazy_background_task_queue_->AddPendingTask( |
806 pending->second.first, pending->second.second, | 783 pending->second.first, pending->second.second, |
807 base::Bind(&MessageService::PendingLazyBackgroundPagePostMessage, | 784 base::Bind(&MessageService::PendingLazyBackgroundPagePostMessage, |
808 weak_factory_.GetWeakPtr(), source_port_id, message)); | 785 weak_factory_.GetWeakPtr(), source_port_id, message)); |
809 } | 786 } |
810 } | 787 } |
811 | 788 |
812 void MessageService::DispatchMessage(int source_port_id, | 789 void MessageService::DispatchMessage(const PortId& source_port_id, |
813 MessageChannel* channel, | 790 MessageChannel* channel, |
814 const Message& message) { | 791 const Message& message) { |
815 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 792 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
816 | 793 |
817 // Figure out which port the ID corresponds to. | 794 // Figure out which port the ID corresponds to. |
818 int dest_port_id = GET_OPPOSITE_PORT_ID(source_port_id); | 795 MessagePort* dest_port = source_port_id.is_opener ? channel->receiver.get() |
819 MessagePort* port = IS_OPENER_PORT_ID(dest_port_id) ? | 796 : channel->opener.get(); |
820 channel->opener.get() : channel->receiver.get(); | |
821 | 797 |
822 port->DispatchOnMessage(message); | 798 dest_port->DispatchOnMessage(message); |
823 } | 799 } |
824 | 800 |
825 bool MessageService::MaybeAddPendingLazyBackgroundPageOpenChannelTask( | 801 bool MessageService::MaybeAddPendingLazyBackgroundPageOpenChannelTask( |
826 BrowserContext* context, | 802 BrowserContext* context, |
827 const Extension* extension, | 803 const Extension* extension, |
828 std::unique_ptr<OpenChannelParams>* params, | 804 std::unique_ptr<OpenChannelParams>* params, |
829 const PendingMessagesQueue& pending_messages) { | 805 const PendingMessagesQueue& pending_messages) { |
830 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 806 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
831 | 807 |
832 if (!BackgroundInfo::HasLazyBackgroundPage(extension)) | 808 if (!BackgroundInfo::HasLazyBackgroundPage(extension)) |
833 return false; | 809 return false; |
834 | 810 |
835 // If the extension uses spanning incognito mode, make sure we're always | 811 // If the extension uses spanning incognito mode, make sure we're always |
836 // using the original profile since that is what the extension process | 812 // using the original profile since that is what the extension process |
837 // will use. | 813 // will use. |
838 if (!IncognitoInfo::IsSplitMode(extension)) | 814 if (!IncognitoInfo::IsSplitMode(extension)) |
839 context = ExtensionsBrowserClient::Get()->GetOriginalContext(context); | 815 context = ExtensionsBrowserClient::Get()->GetOriginalContext(context); |
840 | 816 |
841 if (!lazy_background_task_queue_->ShouldEnqueueTask(context, extension)) | 817 if (!lazy_background_task_queue_->ShouldEnqueueTask(context, extension)) |
842 return false; | 818 return false; |
843 | 819 |
844 int channel_id = GET_CHANNEL_ID((*params)->receiver_port_id); | 820 ChannelId channel_id = (*params)->receiver_port_id.GetChannelId(); |
845 pending_lazy_background_page_channels_[channel_id] = | 821 pending_lazy_background_page_channels_[channel_id] = |
846 PendingLazyBackgroundPageChannel(context, extension->id()); | 822 PendingLazyBackgroundPageChannel(context, extension->id()); |
847 int source_id = (*params)->source_process_id; | 823 int source_id = (*params)->source_process_id; |
848 lazy_background_task_queue_->AddPendingTask( | 824 lazy_background_task_queue_->AddPendingTask( |
849 context, extension->id(), | 825 context, extension->id(), |
850 base::Bind(&MessageService::PendingLazyBackgroundPageOpenChannel, | 826 base::Bind(&MessageService::PendingLazyBackgroundPageOpenChannel, |
851 weak_factory_.GetWeakPtr(), base::Passed(params), source_id)); | 827 weak_factory_.GetWeakPtr(), base::Passed(params), source_id)); |
852 | 828 |
853 for (const PendingMessage& message : pending_messages) { | 829 for (const PendingMessage& message : pending_messages) { |
854 EnqueuePendingMessageForLazyBackgroundLoad(message.first, channel_id, | 830 EnqueuePendingMessageForLazyBackgroundLoad(message.first, channel_id, |
855 message.second); | 831 message.second); |
856 } | 832 } |
857 return true; | 833 return true; |
858 } | 834 } |
859 | 835 |
860 void MessageService::OnOpenChannelAllowed( | 836 void MessageService::OnOpenChannelAllowed( |
861 std::unique_ptr<OpenChannelParams> params, | 837 std::unique_ptr<OpenChannelParams> params, |
862 bool allowed) { | 838 bool allowed) { |
863 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 839 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
864 | 840 |
865 int channel_id = GET_CHANNEL_ID(params->receiver_port_id); | 841 ChannelId channel_id = params->receiver_port_id.GetChannelId(); |
866 | 842 |
867 PendingChannelMap::iterator pending_for_incognito = | 843 PendingChannelMap::iterator pending_for_incognito = |
868 pending_incognito_channels_.find(channel_id); | 844 pending_incognito_channels_.find(channel_id); |
869 if (pending_for_incognito == pending_incognito_channels_.end()) { | 845 if (pending_for_incognito == pending_incognito_channels_.end()) { |
870 NOTREACHED(); | 846 NOTREACHED(); |
871 return; | 847 return; |
872 } | 848 } |
873 PendingMessagesQueue pending_messages; | 849 PendingMessagesQueue pending_messages; |
874 pending_messages.swap(pending_for_incognito->second); | 850 pending_messages.swap(pending_for_incognito->second); |
875 pending_incognito_channels_.erase(pending_for_incognito); | 851 pending_incognito_channels_.erase(pending_for_incognito); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
937 false /* did_enqueue */); | 913 false /* did_enqueue */); |
938 DispatchPendingMessages(pending_messages, channel_id); | 914 DispatchPendingMessages(pending_messages, channel_id); |
939 } | 915 } |
940 } | 916 } |
941 | 917 |
942 void MessageService::GotChannelID(std::unique_ptr<OpenChannelParams> params, | 918 void MessageService::GotChannelID(std::unique_ptr<OpenChannelParams> params, |
943 const std::string& tls_channel_id) { | 919 const std::string& tls_channel_id) { |
944 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 920 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
945 | 921 |
946 params->tls_channel_id.assign(tls_channel_id); | 922 params->tls_channel_id.assign(tls_channel_id); |
947 int channel_id = GET_CHANNEL_ID(params->receiver_port_id); | 923 ChannelId channel_id = params->receiver_port_id.GetChannelId(); |
948 | 924 |
949 PendingChannelMap::iterator pending_for_tls_channel_id = | 925 PendingChannelMap::iterator pending_for_tls_channel_id = |
950 pending_tls_channel_id_channels_.find(channel_id); | 926 pending_tls_channel_id_channels_.find(channel_id); |
951 if (pending_for_tls_channel_id == pending_tls_channel_id_channels_.end()) { | 927 if (pending_for_tls_channel_id == pending_tls_channel_id_channels_.end()) { |
952 NOTREACHED(); | 928 NOTREACHED(); |
953 return; | 929 return; |
954 } | 930 } |
955 PendingMessagesQueue pending_messages; | 931 PendingMessagesQueue pending_messages; |
956 pending_messages.swap(pending_for_tls_channel_id->second); | 932 pending_messages.swap(pending_for_tls_channel_id->second); |
957 pending_tls_channel_id_channels_.erase(pending_for_tls_channel_id); | 933 pending_tls_channel_id_channels_.erase(pending_for_tls_channel_id); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 | 969 |
994 params->receiver.reset( | 970 params->receiver.reset( |
995 new ExtensionMessagePort( | 971 new ExtensionMessagePort( |
996 weak_factory_.GetWeakPtr(), params->receiver_port_id, | 972 weak_factory_.GetWeakPtr(), params->receiver_port_id, |
997 params->target_extension_id, host->render_process_host())); | 973 params->target_extension_id, host->render_process_host())); |
998 OpenChannelImpl(host->browser_context(), std::move(params), host->extension(), | 974 OpenChannelImpl(host->browser_context(), std::move(params), host->extension(), |
999 true /* did_enqueue */); | 975 true /* did_enqueue */); |
1000 } | 976 } |
1001 | 977 |
1002 void MessageService::DispatchOnDisconnect(content::RenderFrameHost* source, | 978 void MessageService::DispatchOnDisconnect(content::RenderFrameHost* source, |
1003 int port_id, | 979 const PortId& port_id, |
1004 const std::string& error_message) { | 980 const std::string& error_message) { |
1005 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 981 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1006 | 982 |
1007 ExtensionMessagePort port(weak_factory_.GetWeakPtr(), | 983 ExtensionMessagePort port(weak_factory_.GetWeakPtr(), |
1008 GET_OPPOSITE_PORT_ID(port_id), "", source, false); | 984 port_id.GetOppositePortId(), "", source, false); |
1009 if (!port.IsValidPort()) | 985 if (!port.IsValidPort()) |
1010 return; | 986 return; |
1011 port.DispatchOnDisconnect(error_message); | 987 port.DispatchOnDisconnect(error_message); |
1012 } | 988 } |
1013 | 989 |
1014 void MessageService::DispatchPendingMessages(const PendingMessagesQueue& queue, | 990 void MessageService::DispatchPendingMessages(const PendingMessagesQueue& queue, |
1015 int channel_id) { | 991 const ChannelId& channel_id) { |
1016 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 992 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1017 | 993 |
1018 MessageChannelMap::iterator channel_iter = channels_.find(channel_id); | 994 MessageChannelMap::iterator channel_iter = channels_.find(channel_id); |
1019 if (channel_iter != channels_.end()) { | 995 if (channel_iter != channels_.end()) { |
1020 for (const PendingMessage& message : queue) { | 996 for (const PendingMessage& message : queue) { |
1021 DispatchMessage(message.first, channel_iter->second.get(), | 997 DispatchMessage(message.first, channel_iter->second.get(), |
1022 message.second); | 998 message.second); |
1023 } | 999 } |
1024 } | 1000 } |
1025 } | 1001 } |
1026 | 1002 |
1027 } // namespace extensions | 1003 } // namespace extensions |
OLD | NEW |