guest_view_request.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/guest_view/renderer/guest_view_request.h"
  5. #include <tuple>
  6. #include <utility>
  7. #include "base/no_destructor.h"
  8. #include "components/guest_view/common/guest_view.mojom.h"
  9. #include "components/guest_view/renderer/guest_view_container.h"
  10. #include "content/public/renderer/render_frame.h"
  11. #include "content/public/renderer/render_thread.h"
  12. #include "ipc/ipc_sync_channel.h"
  13. #include "third_party/blink/public/web/web_local_frame.h"
  14. #include "third_party/blink/public/web/web_remote_frame.h"
  15. #include "third_party/blink/public/web/web_view.h"
  16. #include "v8/include/v8-context.h"
  17. #include "v8/include/v8-function.h"
  18. #include "v8/include/v8-microtask-queue.h"
  19. namespace guest_view {
  20. namespace {
  21. mojom::GuestViewHost* GetGuestViewHost() {
  22. static base::NoDestructor<mojo::AssociatedRemote<mojom::GuestViewHost>>
  23. guest_view_host;
  24. if (!*guest_view_host) {
  25. content::RenderThread::Get()->GetChannel()->GetRemoteAssociatedInterface(
  26. guest_view_host.get());
  27. }
  28. return guest_view_host->get();
  29. }
  30. } // namespace
  31. GuestViewAttachRequest::GuestViewAttachRequest(
  32. guest_view::GuestViewContainer* container,
  33. int render_frame_routing_id,
  34. int guest_instance_id,
  35. base::Value::Dict params,
  36. v8::Local<v8::Function> callback,
  37. v8::Isolate* isolate)
  38. : container_(container),
  39. callback_(isolate, callback),
  40. isolate_(isolate),
  41. render_frame_routing_id_(render_frame_routing_id),
  42. guest_instance_id_(guest_instance_id),
  43. params_(std::move(params)) {}
  44. GuestViewAttachRequest::~GuestViewAttachRequest() = default;
  45. void GuestViewAttachRequest::PerformRequest() {
  46. GetGuestViewHost()->AttachToEmbedderFrame(
  47. render_frame_routing_id_, container_->element_instance_id(),
  48. guest_instance_id_, params_.Clone(),
  49. base::BindOnce(&GuestViewAttachRequest::OnAcknowledged,
  50. weak_ptr_factory_.GetWeakPtr()));
  51. }
  52. void GuestViewAttachRequest::OnAcknowledged() {
  53. // Destroys `this`.
  54. container_->OnRequestAcknowledged(this);
  55. }
  56. void GuestViewAttachRequest::ExecuteCallbackIfAvailable(
  57. int argc,
  58. std::unique_ptr<v8::Local<v8::Value>[]> argv) {
  59. if (callback_.IsEmpty())
  60. return;
  61. v8::HandleScope handle_scope(isolate_);
  62. v8::Local<v8::Function> callback =
  63. v8::Local<v8::Function>::New(isolate_, callback_);
  64. v8::Local<v8::Context> context;
  65. if (!callback->GetCreationContext().ToLocal(&context))
  66. return;
  67. v8::Context::Scope context_scope(context);
  68. v8::MicrotasksScope microtasks(isolate_,
  69. v8::MicrotasksScope::kDoNotRunMicrotasks);
  70. callback->Call(context, context->Global(), argc, argv.get())
  71. .FromMaybe(v8::Local<v8::Value>());
  72. }
  73. } // namespace guest_view