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/renderer/guest_view/extensions_guest_view_container.h"
15 #include "extensions/renderer/script_context.h"
16 #include "v8/include/v8.h"
18 using content::V8ValueConverter
;
20 namespace extensions
{
22 GuestViewInternalCustomBindings::GuestViewInternalCustomBindings(
23 ScriptContext
* context
)
24 : ObjectBackedNativeHandler(context
) {
25 RouteFunction("AttachGuest",
26 base::Bind(&GuestViewInternalCustomBindings::AttachGuest
,
27 base::Unretained(this)));
28 RouteFunction("DetachGuest",
29 base::Bind(&GuestViewInternalCustomBindings::DetachGuest
,
30 base::Unretained(this)));
32 "RegisterDestructionCallback",
33 base::Bind(&GuestViewInternalCustomBindings::RegisterDestructionCallback
,
34 base::Unretained(this)));
36 "RegisterElementResizeCallback",
38 &GuestViewInternalCustomBindings::RegisterElementResizeCallback
,
39 base::Unretained(this)));
42 void GuestViewInternalCustomBindings::AttachGuest(
43 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
44 // Allow for an optional callback parameter.
45 CHECK(args
.Length() >= 3 && args
.Length() <= 4);
46 // Element Instance ID.
47 CHECK(args
[0]->IsInt32());
49 CHECK(args
[1]->IsInt32());
51 CHECK(args
[2]->IsObject());
52 // Optional Callback Function.
53 CHECK(args
.Length() < 4 || args
[3]->IsFunction());
55 int element_instance_id
= args
[0]->Int32Value();
56 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer.
57 ExtensionsGuestViewContainer
* guest_view_container
=
58 ExtensionsGuestViewContainer::FromID(element_instance_id
);
60 // TODO(fsamuel): Should we be reporting an error if the element instance ID
62 if (!guest_view_container
)
65 int guest_instance_id
= args
[1]->Int32Value();
67 scoped_ptr
<base::DictionaryValue
> params
;
69 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
70 scoped_ptr
<base::Value
> params_as_value(
71 converter
->FromV8Value(args
[2], context()->v8_context()));
72 CHECK(params_as_value
->IsType(base::Value::TYPE_DICTIONARY
));
74 static_cast<base::DictionaryValue
*>(params_as_value
.release()));
77 linked_ptr
<ExtensionsGuestViewContainer::Request
> request(
78 new ExtensionsGuestViewContainer::AttachRequest(
82 args
.Length() == 4 ? args
[3].As
<v8::Function
>() :
83 v8::Handle
<v8::Function
>(),
85 guest_view_container
->IssueRequest(request
);
87 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
90 void GuestViewInternalCustomBindings::DetachGuest(
91 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
92 // Allow for an optional callback parameter.
93 CHECK(args
.Length() >= 1 && args
.Length() <= 2);
94 // Element Instance ID.
95 CHECK(args
[0]->IsInt32());
96 // Optional Callback Function.
97 CHECK(args
.Length() < 2 || args
[1]->IsFunction());
99 int element_instance_id
= args
[0]->Int32Value();
100 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer.
101 ExtensionsGuestViewContainer
* guest_view_container
=
102 ExtensionsGuestViewContainer::FromID(element_instance_id
);
104 // TODO(fsamuel): Should we be reporting an error if the element instance ID
106 if (!guest_view_container
)
109 linked_ptr
<ExtensionsGuestViewContainer::Request
> request(
110 new ExtensionsGuestViewContainer::DetachRequest(
111 guest_view_container
,
112 args
.Length() == 2 ? args
[1].As
<v8::Function
>() :
113 v8::Handle
<v8::Function
>(),
115 guest_view_container
->IssueRequest(request
);
117 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
120 void GuestViewInternalCustomBindings::RegisterDestructionCallback(
121 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
122 // There are two parameters.
123 CHECK(args
.Length() == 2);
124 // Element Instance ID.
125 CHECK(args
[0]->IsInt32());
126 // Callback function.
127 CHECK(args
[1]->IsFunction());
129 int element_instance_id
= args
[0]->Int32Value();
130 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer
131 // within a RenderView.
132 ExtensionsGuestViewContainer
* guest_view_container
=
133 ExtensionsGuestViewContainer::FromID(element_instance_id
);
134 if (!guest_view_container
)
137 guest_view_container
->RegisterDestructionCallback(args
[1].As
<v8::Function
>(),
140 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
143 void GuestViewInternalCustomBindings::RegisterElementResizeCallback(
144 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
145 // There are two parameters.
146 CHECK(args
.Length() == 2);
147 // Element Instance ID.
148 CHECK(args
[0]->IsInt32());
149 // Callback function.
150 CHECK(args
[1]->IsFunction());
152 int element_instance_id
= args
[0]->Int32Value();
153 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer
154 // within a RenderView.
155 ExtensionsGuestViewContainer
* guest_view_container
=
156 ExtensionsGuestViewContainer::FromID(element_instance_id
);
157 if (!guest_view_container
)
160 guest_view_container
->RegisterElementResizeCallback(
161 args
[1].As
<v8::Function
>(), args
.GetIsolate());
163 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
166 } // namespace extensions