OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/guest_view/guest_view_internal_custom_bindings.h" | 5 #include "extensions/renderer/guest_view/guest_view_internal_custom_bindings.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "content/public/child/v8_value_converter.h" | 10 #include "content/public/child/v8_value_converter.h" |
11 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
12 #include "extensions/common/extension.h" | 12 #include "extensions/common/extension.h" |
13 #include "extensions/common/extension_messages.h" | 13 #include "extensions/common/extension_messages.h" |
14 #include "extensions/common/guest_view/guest_view_constants.h" | 14 #include "extensions/common/guest_view/guest_view_constants.h" |
| 15 #include "extensions/common/guest_view/guest_view_messages.h" |
15 #include "extensions/renderer/guest_view/extensions_guest_view_container.h" | 16 #include "extensions/renderer/guest_view/extensions_guest_view_container.h" |
16 #include "extensions/renderer/script_context.h" | 17 #include "extensions/renderer/script_context.h" |
17 #include "third_party/WebKit/public/web/WebFrame.h" | 18 #include "third_party/WebKit/public/web/WebFrame.h" |
18 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 19 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
19 #include "third_party/WebKit/public/web/WebView.h" | 20 #include "third_party/WebKit/public/web/WebView.h" |
20 #include "v8/include/v8.h" | 21 #include "v8/include/v8.h" |
| 22 #include "third_party/WebKit/public/web/WebBindings.h" |
| 23 #include "third_party/WebKit/public/web/WebElement.h" |
| 24 #include "third_party/WebKit/public/web/WebFrame.h" |
| 25 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 26 #include "third_party/WebKit/public/web/WebRemoteFrame.h" |
| 27 #include "third_party/WebKit/public/web/WebNode.h" |
| 28 #include "content/public/renderer/render_frame.h" |
21 | 29 |
22 using content::V8ValueConverter; | 30 using content::V8ValueConverter; |
23 | 31 |
24 namespace extensions { | 32 namespace extensions { |
25 | 33 |
26 GuestViewInternalCustomBindings::GuestViewInternalCustomBindings( | 34 GuestViewInternalCustomBindings::GuestViewInternalCustomBindings( |
27 ScriptContext* context) | 35 ScriptContext* context) |
28 : ObjectBackedNativeHandler(context) { | 36 : ObjectBackedNativeHandler(context) { |
29 RouteFunction("AttachGuest", | 37 RouteFunction("AttachGuest", |
30 base::Bind(&GuestViewInternalCustomBindings::AttachGuest, | 38 base::Bind(&GuestViewInternalCustomBindings::AttachGuest, |
(...skipping 10 matching lines...) Expand all Loading... |
41 base::Unretained(this))); | 49 base::Unretained(this))); |
42 RouteFunction( | 50 RouteFunction( |
43 "RegisterElementResizeCallback", | 51 "RegisterElementResizeCallback", |
44 base::Bind( | 52 base::Bind( |
45 &GuestViewInternalCustomBindings::RegisterElementResizeCallback, | 53 &GuestViewInternalCustomBindings::RegisterElementResizeCallback, |
46 base::Unretained(this))); | 54 base::Unretained(this))); |
47 RouteFunction( | 55 RouteFunction( |
48 "RunWithGesture", | 56 "RunWithGesture", |
49 base::Bind(&GuestViewInternalCustomBindings::RunWithGesture, | 57 base::Bind(&GuestViewInternalCustomBindings::RunWithGesture, |
50 base::Unretained(this))); | 58 base::Unretained(this))); |
| 59 RouteFunction("AttachIframeGuest", |
| 60 base::Bind(&GuestViewInternalCustomBindings::AttachIframeGuest, |
| 61 base::Unretained(this))); |
51 } | 62 } |
52 | 63 |
53 void GuestViewInternalCustomBindings::AttachGuest( | 64 void GuestViewInternalCustomBindings::AttachGuest( |
54 const v8::FunctionCallbackInfo<v8::Value>& args) { | 65 const v8::FunctionCallbackInfo<v8::Value>& args) { |
55 // Allow for an optional callback parameter. | 66 // Allow for an optional callback parameter. |
56 CHECK(args.Length() >= 3 && args.Length() <= 4); | 67 CHECK(args.Length() >= 3 && args.Length() <= 4); |
57 // Element Instance ID. | 68 // Element Instance ID. |
58 CHECK(args[0]->IsInt32()); | 69 CHECK(args[0]->IsInt32()); |
59 // Guest Instance ID. | 70 // Guest Instance ID. |
60 CHECK(args[1]->IsInt32()); | 71 CHECK(args[1]->IsInt32()); |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 void GuestViewInternalCustomBindings::RunWithGesture( | 217 void GuestViewInternalCustomBindings::RunWithGesture( |
207 const v8::FunctionCallbackInfo<v8::Value>& args) { | 218 const v8::FunctionCallbackInfo<v8::Value>& args) { |
208 // Gesture is required to request fullscreen. | 219 // Gesture is required to request fullscreen. |
209 blink::WebScopedUserGesture user_gesture; | 220 blink::WebScopedUserGesture user_gesture; |
210 CHECK_EQ(args.Length(), 1); | 221 CHECK_EQ(args.Length(), 1); |
211 CHECK(args[0]->IsFunction()); | 222 CHECK(args[0]->IsFunction()); |
212 v8::Handle<v8::Value> no_args; | 223 v8::Handle<v8::Value> no_args; |
213 context()->CallFunction(v8::Handle<v8::Function>::Cast(args[0]), 0, &no_args); | 224 context()->CallFunction(v8::Handle<v8::Function>::Cast(args[0]), 0, &no_args); |
214 } | 225 } |
215 | 226 |
| 227 bool GetWebNodeFromValue(v8::Handle<v8::Value> value, blink::WebNode* node) { |
| 228 NPVariant result; |
| 229 blink::WebBindings::toNPVariant(value, NULL, &result); |
| 230 if (result.type != NPVariantType_Object) { |
| 231 return false; |
| 232 } |
| 233 NPObject* obj = result.value.objectValue; |
| 234 return blink::WebBindings::getNode(obj, node); |
| 235 } |
| 236 |
| 237 void GuestViewInternalCustomBindings::AttachIframeGuest( |
| 238 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 239 // Allow for an optional callback parameter. |
| 240 CHECK(args.Length() >= 4 && args.Length() <= 5); |
| 241 // Element Instance ID. |
| 242 CHECK(args[0]->IsInt32()); |
| 243 // Guest Instance ID. |
| 244 CHECK(args[1]->IsInt32()); |
| 245 // Node. |
| 246 CHECK(args[2]->IsObject()); |
| 247 // Optional Callback Function. |
| 248 CHECK(args.Length() < 4 || args[3]->IsFunction()); |
| 249 LOG(INFO) << "has_callback: " << (args.Length() >= 4); |
| 250 |
| 251 int element_instance_id = args[0]->Int32Value(); |
| 252 int guest_instance_id = args[1]->Int32Value(); |
| 253 LOG(INFO) << "guest_instance_id: " << guest_instance_id << |
| 254 "element_instance_id: " << element_instance_id; |
| 255 |
| 256 blink::WebNode node; |
| 257 bool got_webnode = GetWebNodeFromValue(args[2], &node); |
| 258 CHECK(got_webnode); |
| 259 blink::WebElement element = node.to<blink::WebElement>(); |
| 260 blink::WebLocalFrame* frame = |
| 261 blink::WebLocalFrame::fromFrameOwnerElement(element); |
| 262 DCHECK(frame); |
| 263 content::RenderFrame* render_frame = |
| 264 content::RenderFrame::FromWebFrame(frame); |
| 265 |
| 266 // Parent must exist. |
| 267 blink::WebFrame* parent_frame = frame->parent(); |
| 268 DCHECK(parent_frame); |
| 269 DCHECK(parent_frame->isWebLocalFrame()); |
| 270 |
| 271 render_frame->Send( |
| 272 new GuestViewHostMsg_AttachLocalFrameToGuest( |
| 273 render_frame->GetRoutingID(), |
| 274 element_instance_id, |
| 275 guest_instance_id)); |
| 276 content::RenderFrame* embedder_parent_frame = |
| 277 content::RenderFrame::FromWebFrame(parent_frame); |
| 278 |
| 279 // Create a GuestViewContainer if it does not exist. |
| 280 ExtensionsGuestViewContainer* guest_view_container = |
| 281 ExtensionsGuestViewContainer::FromID(element_instance_id); |
| 282 // This is the first time we hear about the |element_instance_id|. |
| 283 DCHECK(!guest_view_container); |
| 284 // TODO(lazyboy): Leaked, make this render_frame observer and destruct. |
| 285 guest_view_container = new extensions::ExtensionsGuestViewContainer( |
| 286 embedder_parent_frame); |
| 287 guest_view_container->SetElementInstanceID(element_instance_id); |
| 288 |
| 289 linked_ptr<ExtensionsGuestViewContainer::Request> request( |
| 290 new ExtensionsGuestViewContainer::AttachIframeRequest( |
| 291 guest_view_container, |
| 292 guest_instance_id, |
| 293 args.Length() == 4 ? args[3].As<v8::Function>() : |
| 294 v8::Handle<v8::Function>(), |
| 295 args.GetIsolate())); |
| 296 guest_view_container->IssueRequest(request); |
| 297 |
| 298 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); |
| 299 |
| 300 LOG(INFO) << "AttachLocalFrameToGuest complete"; |
| 301 } |
| 302 |
216 } // namespace extensions | 303 } // namespace extensions |
OLD | NEW |