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/renderer/render_view.h"
11 #include "content/public/renderer/v8_value_converter.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)));
29 "RegisterDestructionCallback",
30 base::Bind(&GuestViewInternalCustomBindings::RegisterDestructionCallback
,
31 base::Unretained(this)));
34 void GuestViewInternalCustomBindings::AttachGuest(
35 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
36 // Allow for an optional callback parameter.
37 CHECK(args
.Length() >= 3 && args
.Length() <= 4);
38 // Element Instance ID.
39 CHECK(args
[0]->IsInt32());
41 CHECK(args
[1]->IsInt32());
43 CHECK(args
[2]->IsObject());
44 // Optional Callback Function.
45 CHECK(args
.Length() < 4 || args
[3]->IsFunction());
47 int element_instance_id
= args
[0]->Int32Value();
48 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer
49 // within a RenderView.
50 ExtensionsGuestViewContainer
* guest_view_container
=
51 ExtensionsGuestViewContainer::FromID(element_instance_id
);
53 // TODO(fsamuel): Should we be reporting an error if the element instance ID
55 if (!guest_view_container
)
58 int guest_instance_id
= args
[1]->Int32Value();
60 scoped_ptr
<base::DictionaryValue
> params
;
62 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
63 scoped_ptr
<base::Value
> params_as_value(
64 converter
->FromV8Value(args
[2], context()->v8_context()));
65 CHECK(params_as_value
->IsType(base::Value::TYPE_DICTIONARY
));
67 static_cast<base::DictionaryValue
*>(params_as_value
.release()));
70 linked_ptr
<ExtensionsGuestViewContainer::Request
> request(
71 new ExtensionsGuestViewContainer::AttachRequest(
75 args
.Length() == 4 ? args
[3].As
<v8::Function
>() :
76 v8::Handle
<v8::Function
>(),
78 guest_view_container
->IssueRequest(request
);
80 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
83 void GuestViewInternalCustomBindings::RegisterDestructionCallback(
84 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
85 // There are two parameters.
86 CHECK(args
.Length() == 2);
87 // Element Instance ID.
88 CHECK(args
[0]->IsInt32());
90 CHECK(args
[1]->IsFunction());
92 int element_instance_id
= args
[0]->Int32Value();
93 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer
94 // within a RenderView.
95 ExtensionsGuestViewContainer
* guest_view_container
=
96 ExtensionsGuestViewContainer::FromID(element_instance_id
);
97 if (!guest_view_container
)
100 guest_view_container
->RegisterDestructionCallback(args
[1].As
<v8::Function
>(),
103 args
.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true));
106 } // namespace extensions