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
3 // found in the LICENSE file.
5 #include "extensions/renderer/guest_view/guest_view_internal_custom_bindings.h"
10 #include "content/public/child/v8_value_converter.h"
11 #include "content/public/renderer/render_view.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_messages.h"
14 #include "extensions/common/guest_view/guest_view_constants.h"
15 #include "extensions/renderer/guest_view/extensions_guest_view_container.h"
16 #include "extensions/renderer/script_context.h"
17 #include "third_party/WebKit/public/web/WebFrame.h"
18 #include "third_party/WebKit/public/web/WebView.h"
19 #include "v8/include/v8.h"
21 using content::V8ValueConverter
;
23 namespace extensions
{
25 GuestViewInternalCustomBindings::GuestViewInternalCustomBindings(
26 ScriptContext
* context
)
27 : ObjectBackedNativeHandler(context
) {
28 RouteFunction("AttachGuest",
29 base::Bind(&GuestViewInternalCustomBindings::AttachGuest
,
30 base::Unretained(this)));
31 RouteFunction("DetachGuest",
32 base::Bind(&GuestViewInternalCustomBindings::DetachGuest
,
33 base::Unretained(this)));
34 RouteFunction("GetContentWindow",
35 base::Bind(&GuestViewInternalCustomBindings::GetContentWindow
,
36 base::Unretained(this)));
38 "RegisterDestructionCallback",
39 base::Bind(&GuestViewInternalCustomBindings::RegisterDestructionCallback
,
40 base::Unretained(this)));
42 "RegisterElementResizeCallback",
44 &GuestViewInternalCustomBindings::RegisterElementResizeCallback
,
45 base::Unretained(this)));
48 void GuestViewInternalCustomBindings::AttachGuest(
49 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
50 // Allow for an optional callback parameter.
51 CHECK(args
.Length() >= 3 && args
.Length() <= 4);
52 // Element Instance ID.
53 CHECK(args
[0]->IsInt32());
55 CHECK(args
[1]->IsInt32());
57 CHECK(args
[2]->IsObject());
58 // Optional Callback Function.
59 CHECK(args
.Length() < 4 || args
[3]->IsFunction());
61 int element_instance_id
= args
[0]->Int32Value();
62 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer.
63 ExtensionsGuestViewContainer
* guest_view_container
=
64 ExtensionsGuestViewContainer::FromID(element_instance_id
);
66 // TODO(fsamuel): Should we be reporting an error if the element instance ID
68 if (!guest_view_container
)
71 int guest_instance_id
= args
[1]->Int32Value();
73 scoped_ptr
<base::DictionaryValue
> params
;
75 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
76 scoped_ptr
<base::Value
> params_as_value(
77 converter
->FromV8Value(args
[2], context()->v8_context()));
78 CHECK(params_as_value
->IsType(base::Value::TYPE_DICTIONARY
));
80 static_cast<base::DictionaryValue
*>(params_as_value
.release()));
83 // Add flag to |params| to indicate that the element size is specified in
85 params
->SetBoolean(guestview::kElementSizeIsLogical
, true);
87 linked_ptr
<ExtensionsGuestViewContainer::Request
> request(
88 new ExtensionsGuestViewContainer::AttachRequest(
92 args
.Length() == 4 ? args
[3].As
<v8::Function
>() :
93 v8::Handle
<v8::Function
>(),
95 guest_view_container
->IssueRequest(request
);
97 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
100 void GuestViewInternalCustomBindings::DetachGuest(
101 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
102 // Allow for an optional callback parameter.
103 CHECK(args
.Length() >= 1 && args
.Length() <= 2);
104 // Element Instance ID.
105 CHECK(args
[0]->IsInt32());
106 // Optional Callback Function.
107 CHECK(args
.Length() < 2 || args
[1]->IsFunction());
109 int element_instance_id
= args
[0]->Int32Value();
110 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer.
111 ExtensionsGuestViewContainer
* guest_view_container
=
112 ExtensionsGuestViewContainer::FromID(element_instance_id
);
114 // TODO(fsamuel): Should we be reporting an error if the element instance ID
116 if (!guest_view_container
)
119 linked_ptr
<ExtensionsGuestViewContainer::Request
> request(
120 new ExtensionsGuestViewContainer::DetachRequest(
121 guest_view_container
,
122 args
.Length() == 2 ? args
[1].As
<v8::Function
>() :
123 v8::Handle
<v8::Function
>(),
125 guest_view_container
->IssueRequest(request
);
127 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
130 void GuestViewInternalCustomBindings::GetContentWindow(
131 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
132 // Default to returning null.
133 args
.GetReturnValue().SetNull();
135 if (args
.Length() != 1)
138 // The routing ID for the RenderView.
139 if (!args
[0]->IsInt32())
142 int view_id
= args
[0]->Int32Value();
143 if (view_id
== MSG_ROUTING_NONE
)
146 content::RenderView
* view
= content::RenderView::FromRoutingID(view_id
);
150 blink::WebFrame
* frame
= view
->GetWebView()->mainFrame();
151 v8::Local
<v8::Value
> window
= frame
->mainWorldScriptContext()->Global();
152 args
.GetReturnValue().Set(window
);
155 void GuestViewInternalCustomBindings::RegisterDestructionCallback(
156 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
157 // There are two parameters.
158 CHECK(args
.Length() == 2);
159 // Element Instance ID.
160 CHECK(args
[0]->IsInt32());
161 // Callback function.
162 CHECK(args
[1]->IsFunction());
164 int element_instance_id
= args
[0]->Int32Value();
165 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer
166 // within a RenderView.
167 ExtensionsGuestViewContainer
* guest_view_container
=
168 ExtensionsGuestViewContainer::FromID(element_instance_id
);
169 if (!guest_view_container
)
172 guest_view_container
->RegisterDestructionCallback(args
[1].As
<v8::Function
>(),
175 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
178 void GuestViewInternalCustomBindings::RegisterElementResizeCallback(
179 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
180 // There are two parameters.
181 CHECK(args
.Length() == 2);
182 // Element Instance ID.
183 CHECK(args
[0]->IsInt32());
184 // Callback function.
185 CHECK(args
[1]->IsFunction());
187 int element_instance_id
= args
[0]->Int32Value();
188 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer
189 // within a RenderView.
190 ExtensionsGuestViewContainer
* guest_view_container
=
191 ExtensionsGuestViewContainer::FromID(element_instance_id
);
192 if (!guest_view_container
)
195 guest_view_container
->RegisterElementResizeCallback(
196 args
[1].As
<v8::Function
>(), args
.GetIsolate());
198 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
201 } // namespace extensions