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