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