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.
5 #include "components/guest_view/renderer/guest_view_request.h"
7 #include "components/guest_view/common/guest_view_messages.h"
8 #include "components/guest_view/renderer/guest_view_container.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "content/public/renderer/render_view.h"
11 #include "third_party/WebKit/public/web/WebLocalFrame.h"
12 #include "third_party/WebKit/public/web/WebRemoteFrame.h"
13 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
14 #include "third_party/WebKit/public/web/WebView.h"
16 namespace guest_view
{
18 GuestViewRequest::GuestViewRequest(GuestViewContainer
* container
,
19 v8::Local
<v8::Function
> callback
,
21 : container_(container
),
22 callback_(isolate
, callback
),
26 GuestViewRequest::~GuestViewRequest() {
29 void GuestViewRequest::ExecuteCallbackIfAvailable(
31 scoped_ptr
<v8::Local
<v8::Value
>[]> argv
) {
32 if (callback_
.IsEmpty())
35 v8::HandleScope
handle_scope(isolate());
36 v8::Local
<v8::Function
> callback
=
37 v8::Local
<v8::Function
>::New(isolate_
, callback_
);
38 v8::Local
<v8::Context
> context
= callback
->CreationContext();
39 if (context
.IsEmpty())
42 v8::Context::Scope
context_scope(context
);
43 blink::WebScopedMicrotaskSuppression suppression
;
45 callback
->Call(context
->Global(), argc
, argv
.get());
48 GuestViewAttachRequest::GuestViewAttachRequest(
49 GuestViewContainer
* container
,
50 int guest_instance_id
,
51 scoped_ptr
<base::DictionaryValue
> params
,
52 v8::Local
<v8::Function
> callback
,
54 : GuestViewRequest(container
, callback
, isolate
),
55 guest_instance_id_(guest_instance_id
),
56 params_(params
.Pass()) {
59 GuestViewAttachRequest::~GuestViewAttachRequest() {
62 void GuestViewAttachRequest::PerformRequest() {
63 if (!container()->render_frame())
66 // Step 1, send the attach params to guest_view/.
67 container()->render_frame()->Send(
68 new GuestViewHostMsg_AttachGuest(container()->element_instance_id(),
72 // Step 2, attach plugin through content/.
73 container()->render_frame()->AttachGuest(container()->element_instance_id());
76 void GuestViewAttachRequest::HandleResponse(const IPC::Message
& message
) {
77 // TODO(fsamuel): Rename this message so that it's apparent that this is a
78 // response to GuestViewHostMsg_AttachGuest. Perhaps
79 // GuestViewMsg_AttachGuest_ACK?
80 GuestViewMsg_GuestAttached::Param param
;
81 if (!GuestViewMsg_GuestAttached::Read(&message
, ¶m
))
84 content::RenderView
* guest_proxy_render_view
=
85 content::RenderView::FromRoutingID(base::get
<1>(param
));
86 // TODO(fsamuel): Should we be reporting an error to JavaScript or DCHECKing?
87 if (!guest_proxy_render_view
)
90 v8::HandleScope
handle_scope(isolate());
91 blink::WebFrame
* frame
= guest_proxy_render_view
->GetWebView()->mainFrame();
92 // TODO(lazyboy,nasko): The WebLocalFrame branch is not used when running
93 // on top of out-of-process iframes. Remove it once the code is converted.
94 v8::Local
<v8::Value
> window
;
95 if (frame
->isWebLocalFrame()) {
96 window
= frame
->mainWorldScriptContext()->Global();
99 frame
->toWebRemoteFrame()->deprecatedMainWorldScriptContext()->Global();
103 scoped_ptr
<v8::Local
<v8::Value
>[]> argv(new v8::Local
<v8::Value
>[argc
]);
106 // Call the AttachGuest API's callback with the guest proxy as the first
108 ExecuteCallbackIfAvailable(argc
, argv
.Pass());
111 GuestViewDetachRequest::GuestViewDetachRequest(
112 GuestViewContainer
* container
,
113 v8::Local
<v8::Function
> callback
,
114 v8::Isolate
* isolate
)
115 : GuestViewRequest(container
, callback
, isolate
) {
118 GuestViewDetachRequest::~GuestViewDetachRequest() {
121 void GuestViewDetachRequest::PerformRequest() {
122 if (!container()->render_frame())
125 container()->render_frame()->DetachGuest(container()->element_instance_id());
128 void GuestViewDetachRequest::HandleResponse(const IPC::Message
& message
) {
129 ExecuteCallbackIfAvailable(0 /* argc */, nullptr);
132 } // namespace guest_view