| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/messaging_bindings.h" | 5 #include "extensions/renderer/messaging_bindings.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 RouteFunction("OpenChannelToNativeApp", "runtime.connectNative", | 262 RouteFunction("OpenChannelToNativeApp", "runtime.connectNative", |
| 263 base::Bind(&MessagingBindings::OpenChannelToNativeApp, | 263 base::Bind(&MessagingBindings::OpenChannelToNativeApp, |
| 264 base::Unretained(this))); | 264 base::Unretained(this))); |
| 265 RouteFunction( | 265 RouteFunction( |
| 266 "OpenChannelToTab", | 266 "OpenChannelToTab", |
| 267 base::Bind(&MessagingBindings::OpenChannelToTab, base::Unretained(this))); | 267 base::Bind(&MessagingBindings::OpenChannelToTab, base::Unretained(this))); |
| 268 } | 268 } |
| 269 | 269 |
| 270 MessagingBindings::~MessagingBindings() { | 270 MessagingBindings::~MessagingBindings() { |
| 271 g_messaging_map.Get().erase(context()); | 271 g_messaging_map.Get().erase(context()); |
| 272 if (ports_created_normal_ > 0 || ports_created_in_before_unload_ > 0 || |
| 273 ports_created_in_unload_ > 0) { |
| 274 UMA_HISTOGRAM_COUNTS_100( |
| 275 "Extensions.Messaging.ExtensionPortsCreated.InBeforeUnload", |
| 276 ports_created_in_before_unload_); |
| 277 UMA_HISTOGRAM_COUNTS_100( |
| 278 "Extensions.Messaging.ExtensionPortsCreated.InUnload", |
| 279 ports_created_in_unload_); |
| 280 UMA_HISTOGRAM_COUNTS_100( |
| 281 "Extensions.Messaging.ExtensionPortsCreated.Normal", |
| 282 ports_created_normal_); |
| 283 } |
| 272 } | 284 } |
| 273 | 285 |
| 274 // static | 286 // static |
| 275 void MessagingBindings::ValidateMessagePort( | 287 void MessagingBindings::ValidateMessagePort( |
| 276 const ScriptContextSet& context_set, | 288 const ScriptContextSet& context_set, |
| 277 int global_port_id, | 289 int global_port_id, |
| 278 content::RenderFrame* render_frame) { | 290 content::RenderFrame* render_frame) { |
| 279 int routing_id = render_frame->GetRoutingID(); | 291 int routing_id = render_frame->GetRoutingID(); |
| 280 | 292 |
| 281 bool has_port = false; | 293 bool has_port = false; |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 | 455 |
| 444 info.target_id = *v8::String::Utf8Value(args[0]); | 456 info.target_id = *v8::String::Utf8Value(args[0]); |
| 445 info.source_url = context()->url(); | 457 info.source_url = context()->url(); |
| 446 std::string channel_name = *v8::String::Utf8Value(args[1]); | 458 std::string channel_name = *v8::String::Utf8Value(args[1]); |
| 447 // TODO(devlin): Why is this not part of info? | 459 // TODO(devlin): Why is this not part of info? |
| 448 bool include_tls_channel_id = | 460 bool include_tls_channel_id = |
| 449 args.Length() > 2 ? args[2]->BooleanValue() : false; | 461 args.Length() > 2 ? args[2]->BooleanValue() : false; |
| 450 | 462 |
| 451 ExtensionFrameHelper* frame_helper = ExtensionFrameHelper::Get(render_frame); | 463 ExtensionFrameHelper* frame_helper = ExtensionFrameHelper::Get(render_frame); |
| 452 DCHECK(frame_helper); | 464 DCHECK(frame_helper); |
| 453 frame_helper->RequestPortId( | 465 |
| 454 info, channel_name, include_tls_channel_id, | 466 blink::WebLocalFrame* web_frame = render_frame->GetWebFrame(); |
| 455 base::Bind(&MessagingBindings::SetGlobalPortId, | 467 // If the frame is unloading, we need to assign the port id synchronously to |
| 456 weak_ptr_factory_.GetWeakPtr(), local_id)); | 468 // avoid dropping the message on the floor; see crbug.com/660706. |
| 469 // TODO(devlin): Investigate whether we need to continue supporting this long- |
| 470 // term, and, if so, find an alternative that doesn't require synchronous |
| 471 // IPCs. |
| 472 if (!web_frame->document().isNull() && |
| 473 (web_frame->document().unloadStarted() || |
| 474 web_frame->document().processingBeforeUnload())) { |
| 475 ports_created_in_before_unload_ += |
| 476 web_frame->document().processingBeforeUnload() ? 1 : 0; |
| 477 ports_created_in_unload_ += web_frame->document().unloadStarted() ? 1 : 0; |
| 478 int global_id = frame_helper->RequestSyncPortId(info, channel_name, |
| 479 include_tls_channel_id); |
| 480 ports_[local_id]->SetGlobalId(global_id); |
| 481 } else { |
| 482 ++ports_created_normal_; |
| 483 frame_helper->RequestPortId( |
| 484 info, channel_name, include_tls_channel_id, |
| 485 base::Bind(&MessagingBindings::SetGlobalPortId, |
| 486 weak_ptr_factory_.GetWeakPtr(), local_id)); |
| 487 } |
| 457 | 488 |
| 458 args.GetReturnValue().Set(static_cast<int32_t>(local_id)); | 489 args.GetReturnValue().Set(static_cast<int32_t>(local_id)); |
| 459 } | 490 } |
| 460 | 491 |
| 461 void MessagingBindings::OpenChannelToNativeApp( | 492 void MessagingBindings::OpenChannelToNativeApp( |
| 462 const v8::FunctionCallbackInfo<v8::Value>& args) { | 493 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 463 // The Javascript code should validate/fill the arguments. | 494 // The Javascript code should validate/fill the arguments. |
| 464 CHECK_EQ(args.Length(), 1); | 495 CHECK_EQ(args.Length(), 1); |
| 465 CHECK(args[0]->IsString()); | 496 CHECK(args[0]->IsString()); |
| 466 // This should be checked by our function routing code. | 497 // This should be checked by our function routing code. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 DCHECK(iter != disconnected_ports_.end()); | 584 DCHECK(iter != disconnected_ports_.end()); |
| 554 iter->second->SetGlobalId(global_id); | 585 iter->second->SetGlobalId(global_id); |
| 555 // Setting the global id dispatches pending messages, so we can delete the | 586 // Setting the global id dispatches pending messages, so we can delete the |
| 556 // port now. | 587 // port now. |
| 557 disconnected_ports_.erase(iter); | 588 disconnected_ports_.erase(iter); |
| 558 } | 589 } |
| 559 | 590 |
| 560 int MessagingBindings::GetNextLocalId() { return next_local_id_++; } | 591 int MessagingBindings::GetNextLocalId() { return next_local_id_++; } |
| 561 | 592 |
| 562 } // namespace extensions | 593 } // namespace extensions |
| OLD | NEW |