Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: extensions/renderer/messaging_bindings.cc

Issue 2331263002: [Extensions] Finish making port creation asynchronous (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 455 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 // This should be checked by our function routing code. 466 // This should be checked by our function routing code.
467 CHECK(context()->GetAvailability("runtime.connectNative").is_available()); 467 CHECK(context()->GetAvailability("runtime.connectNative").is_available());
468 468
469 content::RenderFrame* render_frame = context()->GetRenderFrame(); 469 content::RenderFrame* render_frame = context()->GetRenderFrame();
470 if (!render_frame) 470 if (!render_frame)
471 return; 471 return;
472 472
473 std::string native_app_name = *v8::String::Utf8Value(args[0]); 473 std::string native_app_name = *v8::String::Utf8Value(args[0]);
474 474
475 int local_id = GetNextLocalId(); 475 int local_id = GetNextLocalId();
476 ExtensionPort* port = 476 ports_[local_id] = base::MakeUnique<ExtensionPort>(context(), local_id);
477 ports_
478 .insert(std::make_pair(
479 local_id, base::MakeUnique<ExtensionPort>(context(), local_id)))
480 .first->second.get();
481 477
482 int global_id = -1; 478 ExtensionFrameHelper* frame_helper = ExtensionFrameHelper::Get(render_frame);
483 { 479 DCHECK(frame_helper);
lazyboy 2016/09/13 00:34:39 I've found from reviews that this is unncessary, s
Devlin 2016/09/13 16:49:55 I don't quite agree. For one thing, even if it cr
lazyboy 2016/09/13 17:22:08 In practice, I've found that too while running loc
484 SCOPED_UMA_HISTOGRAM_TIMER( 480 frame_helper->RequestNativePortId(
485 "Extensions.Messaging.GetPortIdSyncTime.NativeApp"); 481 native_app_name,
486 // TODO(devlin): Make this async. crbug.com/642380 482 base::Bind(&MessagingBindings::SetGlobalPortId,
487 render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp( 483 weak_ptr_factory_.GetWeakPtr(), local_id));
488 render_frame->GetRoutingID(), native_app_name, &global_id));
489 }
490
491 port->SetGlobalId(global_id);
492 args.GetReturnValue().Set(static_cast<int32_t>(local_id)); 484 args.GetReturnValue().Set(static_cast<int32_t>(local_id));
493 } 485 }
494 486
495 void MessagingBindings::OpenChannelToTab( 487 void MessagingBindings::OpenChannelToTab(
496 const v8::FunctionCallbackInfo<v8::Value>& args) { 488 const v8::FunctionCallbackInfo<v8::Value>& args) {
497 content::RenderFrame* render_frame = context()->GetRenderFrame(); 489 content::RenderFrame* render_frame = context()->GetRenderFrame();
498 if (!render_frame) 490 if (!render_frame)
499 return; 491 return;
500 492
501 // tabs_custom_bindings.js unwraps arguments to tabs.connect/sendMessage and 493 // tabs_custom_bindings.js unwraps arguments to tabs.connect/sendMessage and
502 // passes them to OpenChannelToTab, in the following order: 494 // passes them to OpenChannelToTab, in the following order:
503 // - |tab_id| - Positive number that specifies the destination of the channel. 495 // - |tab_id| - Positive number that specifies the destination of the channel.
504 // - |frame_id| - Target frame(s) in the tab where onConnect is dispatched: 496 // - |frame_id| - Target frame(s) in the tab where onConnect is dispatched:
505 // -1 for all frames, 0 for the main frame, >0 for a child frame. 497 // -1 for all frames, 0 for the main frame, >0 for a child frame.
506 // - |extension_id| - ID of the initiating extension. 498 // - |extension_id| - ID of the initiating extension.
507 // - |channel_name| - A user-defined channel name. 499 // - |channel_name| - A user-defined channel name.
508 CHECK(args.Length() == 4); 500 CHECK(args.Length() == 4);
509 CHECK(args[0]->IsInt32()); 501 CHECK(args[0]->IsInt32());
510 CHECK(args[1]->IsInt32()); 502 CHECK(args[1]->IsInt32());
511 CHECK(args[2]->IsString()); 503 CHECK(args[2]->IsString());
512 CHECK(args[3]->IsString()); 504 CHECK(args[3]->IsString());
513 505
514 int local_id = GetNextLocalId(); 506 int local_id = GetNextLocalId();
515 ExtensionPort* port = 507 ports_[local_id] = base::MakeUnique<ExtensionPort>(context(), local_id);
516 ports_
517 .insert(std::make_pair(
518 local_id, base::MakeUnique<ExtensionPort>(context(), local_id)))
519 .first->second.get();
520 508
521 ExtensionMsg_TabTargetConnectionInfo info; 509 ExtensionMsg_TabTargetConnectionInfo info;
522 info.tab_id = args[0]->Int32Value(); 510 info.tab_id = args[0]->Int32Value();
523 info.frame_id = args[1]->Int32Value(); 511 info.frame_id = args[1]->Int32Value();
512 // TODO(devlin): Why is this not part of info?
524 std::string extension_id = *v8::String::Utf8Value(args[2]); 513 std::string extension_id = *v8::String::Utf8Value(args[2]);
525 std::string channel_name = *v8::String::Utf8Value(args[3]); 514 std::string channel_name = *v8::String::Utf8Value(args[3]);
526 int global_id = -1; 515
527 { 516 ExtensionFrameHelper* frame_helper = ExtensionFrameHelper::Get(render_frame);
528 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.Messaging.GetPortIdSyncTime.Tab"); 517 DCHECK(frame_helper);
529 // TODO(devlin): Make this async. crbug.com/642380 518 frame_helper->RequestTabPortId(
530 render_frame->Send(new ExtensionHostMsg_OpenChannelToTab( 519 info, extension_id, channel_name,
531 render_frame->GetRoutingID(), info, extension_id, channel_name, 520 base::Bind(&MessagingBindings::SetGlobalPortId,
532 &global_id)); 521 weak_ptr_factory_.GetWeakPtr(), local_id));
533 } 522
534 port->SetGlobalId(global_id);
535 args.GetReturnValue().Set(static_cast<int32_t>(local_id)); 523 args.GetReturnValue().Set(static_cast<int32_t>(local_id));
536 } 524 }
537 525
538 void MessagingBindings::ClosePort(int local_port_id, bool force_close) { 526 void MessagingBindings::ClosePort(int local_port_id, bool force_close) {
539 // TODO(robwu): Merge this logic with CloseChannel once the TODO in BindToGC 527 // TODO(robwu): Merge this logic with CloseChannel once the TODO in BindToGC
540 // has been addressed. 528 // has been addressed.
541 auto iter = ports_.find(local_port_id); 529 auto iter = ports_.find(local_port_id);
542 if (iter != ports_.end()) { 530 if (iter != ports_.end()) {
543 std::unique_ptr<ExtensionPort> port = std::move(iter->second); 531 std::unique_ptr<ExtensionPort> port = std::move(iter->second);
544 ports_.erase(iter); 532 ports_.erase(iter);
(...skipping 20 matching lines...) Expand all
565 DCHECK(iter != disconnected_ports_.end()); 553 DCHECK(iter != disconnected_ports_.end());
566 iter->second->SetGlobalId(global_id); 554 iter->second->SetGlobalId(global_id);
567 // Setting the global id dispatches pending messages, so we can delete the 555 // Setting the global id dispatches pending messages, so we can delete the
568 // port now. 556 // port now.
569 disconnected_ports_.erase(iter); 557 disconnected_ports_.erase(iter);
570 } 558 }
571 559
572 int MessagingBindings::GetNextLocalId() { return next_local_id_++; } 560 int MessagingBindings::GetNextLocalId() { return next_local_id_++; }
573 561
574 } // namespace extensions 562 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698