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/WebScopedMicrotaskSuppression.h"
13 #include "third_party/WebKit/public/web/WebView.h"
15 namespace guest_view
{
17 GuestViewRequest::GuestViewRequest(GuestViewContainer
* container
,
18 v8::Local
<v8::Function
> callback
,
20 : container_(container
),
21 callback_(isolate
, callback
),
25 GuestViewRequest::~GuestViewRequest() {
28 void GuestViewRequest::ExecuteCallbackIfAvailable(
30 scoped_ptr
<v8::Local
<v8::Value
>[]> argv
) {
31 if (callback_
.IsEmpty())
34 v8::HandleScope
handle_scope(isolate());
35 v8::Local
<v8::Function
> callback
=
36 v8::Local
<v8::Function
>::New(isolate_
, callback_
);
37 v8::Local
<v8::Context
> context
= callback
->CreationContext();
38 if (context
.IsEmpty())
41 v8::Context::Scope
context_scope(context
);
42 blink::WebScopedMicrotaskSuppression suppression
;
44 callback
->Call(context
->Global(), argc
, argv
.get());
47 GuestViewAttachRequest::GuestViewAttachRequest(
48 GuestViewContainer
* container
,
49 int guest_instance_id
,
50 scoped_ptr
<base::DictionaryValue
> params
,
51 v8::Local
<v8::Function
> callback
,
53 : GuestViewRequest(container
, callback
, isolate
),
54 guest_instance_id_(guest_instance_id
),
55 params_(params
.Pass()) {
58 GuestViewAttachRequest::~GuestViewAttachRequest() {
61 void GuestViewAttachRequest::PerformRequest() {
62 if (!container()->render_frame())
65 // Step 1, send the attach params to guest_view/.
66 container()->render_frame()->Send(
67 new GuestViewHostMsg_AttachGuest(container()->element_instance_id(),
71 // Step 2, attach plugin through content/.
72 container()->render_frame()->AttachGuest(container()->element_instance_id());
75 void GuestViewAttachRequest::HandleResponse(const IPC::Message
& message
) {
76 // TODO(fsamuel): Rename this message so that it's apparent that this is a
77 // response to GuestViewHostMsg_AttachGuest. Perhaps
78 // GuestViewMsg_AttachGuest_ACK?
79 GuestViewMsg_GuestAttached::Param param
;
80 if (!GuestViewMsg_GuestAttached::Read(&message
, ¶m
))
83 content::RenderView
* guest_proxy_render_view
=
84 content::RenderView::FromRoutingID(base::get
<1>(param
));
85 // TODO(fsamuel): Should we be reporting an error to JavaScript or DCHECKing?
86 if (!guest_proxy_render_view
)
89 v8::HandleScope
handle_scope(isolate());
90 blink::WebFrame
* frame
= guest_proxy_render_view
->GetWebView()->mainFrame();
91 v8::Local
<v8::Value
> window
= frame
->mainWorldScriptContext()->Global();
94 scoped_ptr
<v8::Local
<v8::Value
>[]> argv(new v8::Local
<v8::Value
>[argc
]);
97 // Call the AttachGuest API's callback with the guest proxy as the first
99 ExecuteCallbackIfAvailable(argc
, argv
.Pass());
102 GuestViewDetachRequest::GuestViewDetachRequest(
103 GuestViewContainer
* container
,
104 v8::Local
<v8::Function
> callback
,
105 v8::Isolate
* isolate
)
106 : GuestViewRequest(container
, callback
, isolate
) {
109 GuestViewDetachRequest::~GuestViewDetachRequest() {
112 void GuestViewDetachRequest::PerformRequest() {
113 if (!container()->render_frame())
116 container()->render_frame()->DetachGuest(container()->element_instance_id());
119 void GuestViewDetachRequest::HandleResponse(const IPC::Message
& message
) {
120 ExecuteCallbackIfAvailable(0 /* argc */, nullptr);
123 } // namespace guest_view