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 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "components/guest_view/common/guest_view_constants.h" | 11 #include "components/guest_view/common/guest_view_constants.h" |
12 #include "components/guest_view/common/guest_view_messages.h" | 12 #include "components/guest_view/common/guest_view_messages.h" |
13 #include "components/guest_view/renderer/guest_view_request.h" | 13 #include "components/guest_view/renderer/guest_view_request.h" |
14 #include "content/public/child/v8_value_converter.h" | 14 #include "content/public/child/v8_value_converter.h" |
| 15 #include "content/public/renderer/render_frame.h" |
15 #include "content/public/renderer/render_thread.h" | 16 #include "content/public/renderer/render_thread.h" |
16 #include "content/public/renderer/render_view.h" | 17 #include "content/public/renderer/render_view.h" |
17 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
18 #include "extensions/common/extension_messages.h" | 19 #include "extensions/common/extension_messages.h" |
| 20 #include "extensions/common/guest_view/extensions_guest_view_messages.h" |
19 #include "extensions/renderer/guest_view/extensions_guest_view_container.h" | 21 #include "extensions/renderer/guest_view/extensions_guest_view_container.h" |
| 22 #include "extensions/renderer/guest_view/extensions_iframe_guest_view_container.
h" |
| 23 #include "extensions/renderer/guest_view/extensions_iframe_guest_view_request.h" |
20 #include "extensions/renderer/script_context.h" | 24 #include "extensions/renderer/script_context.h" |
21 #include "third_party/WebKit/public/web/WebFrame.h" | 25 #include "third_party/WebKit/public/web/WebFrame.h" |
| 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
22 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 27 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
23 #include "third_party/WebKit/public/web/WebView.h" | 28 #include "third_party/WebKit/public/web/WebView.h" |
24 #include "v8/include/v8.h" | 29 #include "v8/include/v8.h" |
25 | 30 |
26 using content::V8ValueConverter; | 31 using content::V8ValueConverter; |
27 | 32 |
28 namespace { | 33 namespace { |
29 | 34 |
30 // A map from view instance ID to view object (stored via weak V8 reference). | 35 // A map from view instance ID to view object (stored via weak V8 reference). |
31 // Views are registered into this map via | 36 // Views are registered into this map via |
32 // GuestViewInternalCustomBindings::RegisterView(), and accessed via | 37 // GuestViewInternalCustomBindings::RegisterView(), and accessed via |
33 // GuestViewInternalCustomBindings::GetViewFromID(). | 38 // GuestViewInternalCustomBindings::GetViewFromID(). |
34 using ViewMap = std::map<int, v8::Global<v8::Object>*>; | 39 using ViewMap = std::map<int, v8::Global<v8::Object>*>; |
35 static base::LazyInstance<ViewMap> weak_view_map = LAZY_INSTANCE_INITIALIZER; | 40 static base::LazyInstance<ViewMap> weak_view_map = LAZY_INSTANCE_INITIALIZER; |
36 | 41 |
37 } // namespace | 42 } // namespace |
38 | 43 |
39 namespace extensions { | 44 namespace extensions { |
40 | 45 |
| 46 namespace { |
| 47 |
| 48 content::RenderFrame* GetRenderFrame(v8::Handle<v8::Value> value) { |
| 49 v8::Local<v8::Context> context = |
| 50 v8::Local<v8::Object>::Cast(value)->CreationContext(); |
| 51 if (context.IsEmpty()) |
| 52 return nullptr; |
| 53 blink::WebLocalFrame* frame = blink::WebLocalFrame::frameForContext(context); |
| 54 if (!frame) |
| 55 return nullptr; |
| 56 return content::RenderFrame::FromWebFrame(frame); |
| 57 } |
| 58 |
| 59 } // namespace |
| 60 |
41 GuestViewInternalCustomBindings::GuestViewInternalCustomBindings( | 61 GuestViewInternalCustomBindings::GuestViewInternalCustomBindings( |
42 ScriptContext* context) | 62 ScriptContext* context) |
43 : ObjectBackedNativeHandler(context) { | 63 : ObjectBackedNativeHandler(context) { |
44 RouteFunction("AttachGuest", | 64 RouteFunction("AttachGuest", |
45 base::Bind(&GuestViewInternalCustomBindings::AttachGuest, | 65 base::Bind(&GuestViewInternalCustomBindings::AttachGuest, |
46 base::Unretained(this))); | 66 base::Unretained(this))); |
47 RouteFunction("DetachGuest", | 67 RouteFunction("DetachGuest", |
48 base::Bind(&GuestViewInternalCustomBindings::DetachGuest, | 68 base::Bind(&GuestViewInternalCustomBindings::DetachGuest, |
49 base::Unretained(this))); | 69 base::Unretained(this))); |
50 RouteFunction("DestroyContainer", | 70 RouteFunction("DestroyContainer", |
(...skipping 14 matching lines...) Expand all Loading... |
65 base::Bind( | 85 base::Bind( |
66 &GuestViewInternalCustomBindings::RegisterElementResizeCallback, | 86 &GuestViewInternalCustomBindings::RegisterElementResizeCallback, |
67 base::Unretained(this))); | 87 base::Unretained(this))); |
68 RouteFunction("RegisterView", | 88 RouteFunction("RegisterView", |
69 base::Bind(&GuestViewInternalCustomBindings::RegisterView, | 89 base::Bind(&GuestViewInternalCustomBindings::RegisterView, |
70 base::Unretained(this))); | 90 base::Unretained(this))); |
71 RouteFunction( | 91 RouteFunction( |
72 "RunWithGesture", | 92 "RunWithGesture", |
73 base::Bind(&GuestViewInternalCustomBindings::RunWithGesture, | 93 base::Bind(&GuestViewInternalCustomBindings::RunWithGesture, |
74 base::Unretained(this))); | 94 base::Unretained(this))); |
| 95 RouteFunction("AttachIframeGuest", |
| 96 base::Bind(&GuestViewInternalCustomBindings::AttachIframeGuest, |
| 97 base::Unretained(this))); |
75 } | 98 } |
76 | 99 |
77 GuestViewInternalCustomBindings::~GuestViewInternalCustomBindings() {} | 100 GuestViewInternalCustomBindings::~GuestViewInternalCustomBindings() {} |
78 | 101 |
79 // static | 102 // static |
80 void GuestViewInternalCustomBindings::ResetMapEntry( | 103 void GuestViewInternalCustomBindings::ResetMapEntry( |
81 const v8::WeakCallbackInfo<int>& data) { | 104 const v8::WeakCallbackInfo<int>& data) { |
82 int* param = data.GetParameter(); | 105 int* param = data.GetParameter(); |
83 int view_instance_id = *param; | 106 int view_instance_id = *param; |
84 delete param; | 107 delete param; |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 void GuestViewInternalCustomBindings::RunWithGesture( | 342 void GuestViewInternalCustomBindings::RunWithGesture( |
320 const v8::FunctionCallbackInfo<v8::Value>& args) { | 343 const v8::FunctionCallbackInfo<v8::Value>& args) { |
321 // Gesture is required to request fullscreen. | 344 // Gesture is required to request fullscreen. |
322 blink::WebScopedUserGesture user_gesture; | 345 blink::WebScopedUserGesture user_gesture; |
323 CHECK_EQ(args.Length(), 1); | 346 CHECK_EQ(args.Length(), 1); |
324 CHECK(args[0]->IsFunction()); | 347 CHECK(args[0]->IsFunction()); |
325 v8::Local<v8::Value> no_args; | 348 v8::Local<v8::Value> no_args; |
326 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args); | 349 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args); |
327 } | 350 } |
328 | 351 |
| 352 void GuestViewInternalCustomBindings::AttachIframeGuest( |
| 353 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 354 // Allow for an optional callback parameter. |
| 355 const int num_required_params = 4; |
| 356 CHECK(args.Length() >= num_required_params && |
| 357 args.Length() <= (num_required_params + 1)); |
| 358 // Element Instance ID. |
| 359 CHECK(args[0]->IsInt32()); |
| 360 // Guest Instance ID. |
| 361 CHECK(args[1]->IsInt32()); |
| 362 // Attach Parameters. |
| 363 CHECK(args[2]->IsObject()); |
| 364 // <iframe>.contentWindow. |
| 365 CHECK(args[3]->IsObject()); |
| 366 // Optional Callback Function. |
| 367 CHECK(args.Length() <= num_required_params || |
| 368 args[num_required_params]->IsFunction()); |
| 369 LOG(WARNING) << "has_callback: " |
| 370 << (args.Length() >= (num_required_params + 1)); |
| 371 |
| 372 int element_instance_id = args[0]->Int32Value(); |
| 373 int guest_instance_id = args[1]->Int32Value(); |
| 374 LOG(WARNING) << "guest_instance_id: " << guest_instance_id |
| 375 << ", element_instance_id: " << element_instance_id; |
| 376 |
| 377 scoped_ptr<base::DictionaryValue> params; |
| 378 { |
| 379 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 380 scoped_ptr<base::Value> params_as_value( |
| 381 converter->FromV8Value(args[2], context()->v8_context())); |
| 382 CHECK(params_as_value->IsType(base::Value::TYPE_DICTIONARY)); |
| 383 params.reset( |
| 384 static_cast<base::DictionaryValue*>(params_as_value.release())); |
| 385 } |
| 386 |
| 387 // Add flag to |params| to indicate that the element size is specified in |
| 388 // logical units. |
| 389 params->SetBoolean(guest_view::kElementSizeIsLogical, true); |
| 390 |
| 391 content::RenderFrame* render_frame = GetRenderFrame(args[3]); |
| 392 blink::WebLocalFrame* frame = render_frame->GetWebFrame(); |
| 393 |
| 394 // Parent must exist. |
| 395 blink::WebFrame* parent_frame = frame->parent(); |
| 396 DCHECK(parent_frame); |
| 397 DCHECK(parent_frame->isWebLocalFrame()); |
| 398 |
| 399 content::RenderFrame* embedder_parent_frame = |
| 400 content::RenderFrame::FromWebFrame(parent_frame); |
| 401 |
| 402 // Create a GuestViewContainer if it does not exist. |
| 403 // An element instance ID uniquely identifies an |
| 404 // ExtensionsIframeGuestViewContainer within a RenderView. |
| 405 auto* guest_view_container = static_cast<ExtensionsIframeGuestViewContainer*>( |
| 406 guest_view::GuestViewContainer::FromID(element_instance_id)); |
| 407 // This is the first time we hear about the |element_instance_id|. |
| 408 DCHECK(!guest_view_container); |
| 409 // The <webview> element's GC takes ownership of |guest_view_container|. |
| 410 guest_view_container = |
| 411 new extensions::ExtensionsIframeGuestViewContainer(embedder_parent_frame); |
| 412 guest_view_container->SetElementInstanceID(element_instance_id); |
| 413 |
| 414 linked_ptr<guest_view::GuestViewRequest> request( |
| 415 new GuestViewAttachIframeRequest( |
| 416 guest_view_container, render_frame->GetRoutingID(), guest_instance_id, |
| 417 params.Pass(), args.Length() == (num_required_params + 1) |
| 418 ? args[num_required_params].As<v8::Function>() |
| 419 : v8::Local<v8::Function>(), |
| 420 args.GetIsolate())); |
| 421 guest_view_container->IssueRequest(request); |
| 422 |
| 423 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); |
| 424 |
| 425 LOG(WARNING) << "AttachLocalFrameToGuest complete"; |
| 426 } |
| 427 |
329 } // namespace extensions | 428 } // namespace extensions |
OLD | NEW |