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 "base/command_line.h" |
10 #include "components/guest_view/common/guest_view_constants.h" | 11 #include "components/guest_view/common/guest_view_constants.h" |
11 #include "components/guest_view/renderer/guest_view_request.h" | 12 #include "components/guest_view/renderer/guest_view_request.h" |
12 #include "content/public/child/v8_value_converter.h" | 13 #include "content/public/child/v8_value_converter.h" |
| 14 #include "content/public/common/content_switches.h" |
| 15 #include "content/public/renderer/render_frame.h" |
13 #include "content/public/renderer/render_view.h" | 16 #include "content/public/renderer/render_view.h" |
14 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
15 #include "extensions/common/extension_messages.h" | 18 #include "extensions/common/extension_messages.h" |
| 19 #include "extensions/common/guest_view/extensions_guest_view_messages.h" |
16 #include "extensions/renderer/guest_view/extensions_guest_view_container.h" | 20 #include "extensions/renderer/guest_view/extensions_guest_view_container.h" |
| 21 #include "extensions/renderer/guest_view/extensions_guest_view_request.h" |
17 #include "extensions/renderer/script_context.h" | 22 #include "extensions/renderer/script_context.h" |
18 #include "third_party/WebKit/public/web/WebFrame.h" | 23 #include "third_party/WebKit/public/web/WebFrame.h" |
| 24 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
19 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 25 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
20 #include "third_party/WebKit/public/web/WebView.h" | 26 #include "third_party/WebKit/public/web/WebView.h" |
21 #include "v8/include/v8.h" | 27 #include "v8/include/v8.h" |
22 | 28 |
23 using content::V8ValueConverter; | 29 using content::V8ValueConverter; |
24 | 30 |
25 namespace extensions { | 31 namespace extensions { |
26 | 32 |
| 33 namespace { |
| 34 |
| 35 content::RenderFrame* GetRenderFrame(v8::Handle<v8::Value> value) { |
| 36 v8::Local<v8::Context> context = |
| 37 v8::Local<v8::Object>::Cast(value)->CreationContext(); |
| 38 if (context.IsEmpty()) |
| 39 return nullptr; |
| 40 blink::WebLocalFrame* frame = blink::WebLocalFrame::frameForContext(context); |
| 41 if (!frame) |
| 42 return nullptr; |
| 43 return content::RenderFrame::FromWebFrame(frame); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
27 GuestViewInternalCustomBindings::GuestViewInternalCustomBindings( | 48 GuestViewInternalCustomBindings::GuestViewInternalCustomBindings( |
28 ScriptContext* context) | 49 ScriptContext* context) |
29 : ObjectBackedNativeHandler(context) { | 50 : ObjectBackedNativeHandler(context) { |
30 RouteFunction("AttachGuest", | 51 RouteFunction("AttachGuest", |
31 base::Bind(&GuestViewInternalCustomBindings::AttachGuest, | 52 base::Bind(&GuestViewInternalCustomBindings::AttachGuest, |
32 base::Unretained(this))); | 53 base::Unretained(this))); |
33 RouteFunction("DetachGuest", | 54 RouteFunction("DetachGuest", |
34 base::Bind(&GuestViewInternalCustomBindings::DetachGuest, | 55 base::Bind(&GuestViewInternalCustomBindings::DetachGuest, |
35 base::Unretained(this))); | 56 base::Unretained(this))); |
36 RouteFunction("GetContentWindow", | 57 RouteFunction("GetContentWindow", |
37 base::Bind(&GuestViewInternalCustomBindings::GetContentWindow, | 58 base::Bind(&GuestViewInternalCustomBindings::GetContentWindow, |
38 base::Unretained(this))); | 59 base::Unretained(this))); |
39 RouteFunction( | 60 RouteFunction( |
40 "RegisterDestructionCallback", | 61 "RegisterDestructionCallback", |
41 base::Bind(&GuestViewInternalCustomBindings::RegisterDestructionCallback, | 62 base::Bind(&GuestViewInternalCustomBindings::RegisterDestructionCallback, |
42 base::Unretained(this))); | 63 base::Unretained(this))); |
43 RouteFunction( | 64 RouteFunction( |
44 "RegisterElementResizeCallback", | 65 "RegisterElementResizeCallback", |
45 base::Bind( | 66 base::Bind( |
46 &GuestViewInternalCustomBindings::RegisterElementResizeCallback, | 67 &GuestViewInternalCustomBindings::RegisterElementResizeCallback, |
47 base::Unretained(this))); | 68 base::Unretained(this))); |
48 RouteFunction( | 69 RouteFunction( |
49 "RunWithGesture", | 70 "RunWithGesture", |
50 base::Bind(&GuestViewInternalCustomBindings::RunWithGesture, | 71 base::Bind(&GuestViewInternalCustomBindings::RunWithGesture, |
51 base::Unretained(this))); | 72 base::Unretained(this))); |
| 73 RouteFunction("AttachIframeGuest", |
| 74 base::Bind(&GuestViewInternalCustomBindings::AttachIframeGuest, |
| 75 base::Unretained(this))); |
| 76 // TODO(lazyboy): There must be a better way to query command line from JS. |
| 77 RouteFunction("IsSitePerProcess", |
| 78 base::Bind(&GuestViewInternalCustomBindings::IsSitePerProcess, |
| 79 base::Unretained(this))); |
52 } | 80 } |
53 | 81 |
54 void GuestViewInternalCustomBindings::AttachGuest( | 82 void GuestViewInternalCustomBindings::AttachGuest( |
55 const v8::FunctionCallbackInfo<v8::Value>& args) { | 83 const v8::FunctionCallbackInfo<v8::Value>& args) { |
56 // Allow for an optional callback parameter. | 84 // Allow for an optional callback parameter. |
57 CHECK(args.Length() >= 3 && args.Length() <= 4); | 85 CHECK(args.Length() >= 3 && args.Length() <= 4); |
58 // Element Instance ID. | 86 // Element Instance ID. |
59 CHECK(args[0]->IsInt32()); | 87 CHECK(args[0]->IsInt32()); |
60 // Guest Instance ID. | 88 // Guest Instance ID. |
61 CHECK(args[1]->IsInt32()); | 89 CHECK(args[1]->IsInt32()); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 void GuestViewInternalCustomBindings::RunWithGesture( | 232 void GuestViewInternalCustomBindings::RunWithGesture( |
205 const v8::FunctionCallbackInfo<v8::Value>& args) { | 233 const v8::FunctionCallbackInfo<v8::Value>& args) { |
206 // Gesture is required to request fullscreen. | 234 // Gesture is required to request fullscreen. |
207 blink::WebScopedUserGesture user_gesture; | 235 blink::WebScopedUserGesture user_gesture; |
208 CHECK_EQ(args.Length(), 1); | 236 CHECK_EQ(args.Length(), 1); |
209 CHECK(args[0]->IsFunction()); | 237 CHECK(args[0]->IsFunction()); |
210 v8::Local<v8::Value> no_args; | 238 v8::Local<v8::Value> no_args; |
211 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args); | 239 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args); |
212 } | 240 } |
213 | 241 |
| 242 void GuestViewInternalCustomBindings::AttachIframeGuest( |
| 243 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 244 // Allow for an optional callback parameter. |
| 245 const int num_required_params = 4; |
| 246 CHECK(args.Length() >= num_required_params && |
| 247 args.Length() <= (num_required_params + 1)); |
| 248 // Element Instance ID. |
| 249 CHECK(args[0]->IsInt32()); |
| 250 // Guest Instance ID. |
| 251 CHECK(args[1]->IsInt32()); |
| 252 // Attach Parameters. |
| 253 CHECK(args[2]->IsObject()); |
| 254 // <iframe>.contentWindow. |
| 255 CHECK(args[3]->IsObject()); |
| 256 // Optional Callback Function. |
| 257 CHECK(args.Length() <= num_required_params || |
| 258 args[num_required_params]->IsFunction()); |
| 259 LOG(WARNING) << "has_callback: " |
| 260 << (args.Length() >= (num_required_params + 1)); |
| 261 |
| 262 int element_instance_id = args[0]->Int32Value(); |
| 263 int guest_instance_id = args[1]->Int32Value(); |
| 264 LOG(WARNING) << "guest_instance_id: " << guest_instance_id |
| 265 << ", element_instance_id: " << element_instance_id; |
| 266 |
| 267 scoped_ptr<base::DictionaryValue> params; |
| 268 { |
| 269 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 270 scoped_ptr<base::Value> params_as_value( |
| 271 converter->FromV8Value(args[2], context()->v8_context())); |
| 272 CHECK(params_as_value->IsType(base::Value::TYPE_DICTIONARY)); |
| 273 params.reset( |
| 274 static_cast<base::DictionaryValue*>(params_as_value.release())); |
| 275 } |
| 276 |
| 277 // Add flag to |params| to indicate that the element size is specified in |
| 278 // logical units. |
| 279 params->SetBoolean(guest_view::kElementSizeIsLogical, true); |
| 280 |
| 281 content::RenderFrame* render_frame = GetRenderFrame(args[3]); |
| 282 blink::WebLocalFrame* frame = render_frame->GetWebFrame(); |
| 283 |
| 284 // Parent must exist. |
| 285 blink::WebFrame* parent_frame = frame->parent(); |
| 286 DCHECK(parent_frame); |
| 287 DCHECK(parent_frame->isWebLocalFrame()); |
| 288 |
| 289 content::RenderFrame* embedder_parent_frame = |
| 290 content::RenderFrame::FromWebFrame(parent_frame); |
| 291 |
| 292 // Create a GuestViewContainer if it does not exist. |
| 293 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer |
| 294 // within a RenderView. |
| 295 auto* guest_view_container = static_cast<ExtensionsGuestViewContainer*>( |
| 296 guest_view::GuestViewContainer::FromID(element_instance_id)); |
| 297 // This is the first time we hear about the |element_instance_id|. |
| 298 DCHECK(!guest_view_container); |
| 299 // TODO(lazyboy): Leaked, make this render_frame observer and destruct. |
| 300 guest_view_container = |
| 301 new extensions::ExtensionsGuestViewContainer(embedder_parent_frame); |
| 302 guest_view_container->SetElementInstanceID(element_instance_id); |
| 303 |
| 304 linked_ptr<guest_view::GuestViewRequest> request( |
| 305 new GuestViewAttachIframeRequest( |
| 306 guest_view_container, render_frame->GetRoutingID(), guest_instance_id, |
| 307 params.Pass(), args.Length() == (num_required_params + 1) |
| 308 ? args[num_required_params].As<v8::Function>() |
| 309 : v8::Local<v8::Function>(), |
| 310 args.GetIsolate())); |
| 311 guest_view_container->IssueRequest(request); |
| 312 |
| 313 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); |
| 314 |
| 315 LOG(WARNING) << "AttachLocalFrameToGuest complete"; |
| 316 } |
| 317 |
| 318 void GuestViewInternalCustomBindings::IsSitePerProcess( |
| 319 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 320 bool is_site_per_process = base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 321 switches::kSitePerProcess); |
| 322 args.GetReturnValue().Set( |
| 323 v8::Boolean::New(context()->isolate(), is_site_per_process)); |
| 324 } |
| 325 |
214 } // namespace extensions | 326 } // namespace extensions |
OLD | NEW |