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 "components/guest_view/renderer/iframe_guest_view_container.h" |
| 15 #include "components/guest_view/renderer/iframe_guest_view_request.h" |
14 #include "content/public/child/v8_value_converter.h" | 16 #include "content/public/child/v8_value_converter.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" |
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))); |
| 70 RouteFunction("AttachIframeGuest", |
| 71 base::Bind(&GuestViewInternalCustomBindings::AttachIframeGuest, |
| 72 base::Unretained(this))); |
50 RouteFunction("DestroyContainer", | 73 RouteFunction("DestroyContainer", |
51 base::Bind(&GuestViewInternalCustomBindings::DestroyContainer, | 74 base::Bind(&GuestViewInternalCustomBindings::DestroyContainer, |
52 base::Unretained(this))); | 75 base::Unretained(this))); |
53 RouteFunction("GetContentWindow", | 76 RouteFunction("GetContentWindow", |
54 base::Bind(&GuestViewInternalCustomBindings::GetContentWindow, | 77 base::Bind(&GuestViewInternalCustomBindings::GetContentWindow, |
55 base::Unretained(this))); | 78 base::Unretained(this))); |
56 RouteFunction("GetViewFromID", | 79 RouteFunction("GetViewFromID", |
57 base::Bind(&GuestViewInternalCustomBindings::GetViewFromID, | 80 base::Bind(&GuestViewInternalCustomBindings::GetViewFromID, |
58 base::Unretained(this))); | 81 base::Unretained(this))); |
59 RouteFunction( | 82 RouteFunction( |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 linked_ptr<guest_view::GuestViewRequest> request( | 193 linked_ptr<guest_view::GuestViewRequest> request( |
171 new guest_view::GuestViewDetachRequest( | 194 new guest_view::GuestViewDetachRequest( |
172 guest_view_container, args.Length() == 2 ? args[1].As<v8::Function>() | 195 guest_view_container, args.Length() == 2 ? args[1].As<v8::Function>() |
173 : v8::Local<v8::Function>(), | 196 : v8::Local<v8::Function>(), |
174 args.GetIsolate())); | 197 args.GetIsolate())); |
175 guest_view_container->IssueRequest(request); | 198 guest_view_container->IssueRequest(request); |
176 | 199 |
177 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); | 200 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); |
178 } | 201 } |
179 | 202 |
| 203 void GuestViewInternalCustomBindings::AttachIframeGuest( |
| 204 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 205 // Allow for an optional callback parameter. |
| 206 const int num_required_params = 4; |
| 207 CHECK(args.Length() >= num_required_params && |
| 208 args.Length() <= (num_required_params + 1)); |
| 209 // Element Instance ID. |
| 210 CHECK(args[0]->IsInt32()); |
| 211 // Guest Instance ID. |
| 212 CHECK(args[1]->IsInt32()); |
| 213 // Attach Parameters. |
| 214 CHECK(args[2]->IsObject()); |
| 215 // <iframe>.contentWindow. |
| 216 CHECK(args[3]->IsObject()); |
| 217 // Optional Callback Function. |
| 218 CHECK(args.Length() <= num_required_params || |
| 219 args[num_required_params]->IsFunction()); |
| 220 |
| 221 int element_instance_id = args[0]->Int32Value(); |
| 222 int guest_instance_id = args[1]->Int32Value(); |
| 223 |
| 224 scoped_ptr<base::DictionaryValue> params; |
| 225 { |
| 226 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 227 scoped_ptr<base::Value> params_as_value( |
| 228 converter->FromV8Value(args[2], context()->v8_context())); |
| 229 CHECK(params_as_value->IsType(base::Value::TYPE_DICTIONARY)); |
| 230 params.reset( |
| 231 static_cast<base::DictionaryValue*>(params_as_value.release())); |
| 232 } |
| 233 |
| 234 // Add flag to |params| to indicate that the element size is specified in |
| 235 // logical units. |
| 236 params->SetBoolean(guest_view::kElementSizeIsLogical, true); |
| 237 |
| 238 content::RenderFrame* render_frame = GetRenderFrame(args[3]); |
| 239 blink::WebLocalFrame* frame = render_frame->GetWebFrame(); |
| 240 |
| 241 // Parent must exist. |
| 242 blink::WebFrame* parent_frame = frame->parent(); |
| 243 DCHECK(parent_frame); |
| 244 DCHECK(parent_frame->isWebLocalFrame()); |
| 245 |
| 246 content::RenderFrame* embedder_parent_frame = |
| 247 content::RenderFrame::FromWebFrame(parent_frame); |
| 248 |
| 249 // Create a GuestViewContainer if it does not exist. |
| 250 // An element instance ID uniquely identifies an IframeGuestViewContainer |
| 251 // within a RenderView. |
| 252 auto* guest_view_container = |
| 253 static_cast<guest_view::IframeGuestViewContainer*>( |
| 254 guest_view::GuestViewContainer::FromID(element_instance_id)); |
| 255 // This is the first time we hear about the |element_instance_id|. |
| 256 DCHECK(!guest_view_container); |
| 257 // The <webview> element's GC takes ownership of |guest_view_container|. |
| 258 guest_view_container = |
| 259 new guest_view::IframeGuestViewContainer(embedder_parent_frame); |
| 260 guest_view_container->SetElementInstanceID(element_instance_id); |
| 261 |
| 262 linked_ptr<guest_view::GuestViewRequest> request( |
| 263 new guest_view::GuestViewAttachIframeRequest( |
| 264 guest_view_container, render_frame->GetRoutingID(), guest_instance_id, |
| 265 params.Pass(), args.Length() == (num_required_params + 1) |
| 266 ? args[num_required_params].As<v8::Function>() |
| 267 : v8::Local<v8::Function>(), |
| 268 args.GetIsolate())); |
| 269 guest_view_container->IssueRequest(request); |
| 270 |
| 271 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); |
| 272 } |
| 273 |
180 void GuestViewInternalCustomBindings::DestroyContainer( | 274 void GuestViewInternalCustomBindings::DestroyContainer( |
181 const v8::FunctionCallbackInfo<v8::Value>& args) { | 275 const v8::FunctionCallbackInfo<v8::Value>& args) { |
182 args.GetReturnValue().SetNull(); | 276 args.GetReturnValue().SetNull(); |
183 | 277 |
184 if (args.Length() != 1) | 278 if (args.Length() != 1) |
185 return; | 279 return; |
186 | 280 |
187 // Element Instance ID. | 281 // Element Instance ID. |
188 if (!args[0]->IsInt32()) | 282 if (!args[0]->IsInt32()) |
189 return; | 283 return; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 void GuestViewInternalCustomBindings::RegisterDestructionCallback( | 343 void GuestViewInternalCustomBindings::RegisterDestructionCallback( |
250 const v8::FunctionCallbackInfo<v8::Value>& args) { | 344 const v8::FunctionCallbackInfo<v8::Value>& args) { |
251 // There are two parameters. | 345 // There are two parameters. |
252 CHECK(args.Length() == 2); | 346 CHECK(args.Length() == 2); |
253 // Element Instance ID. | 347 // Element Instance ID. |
254 CHECK(args[0]->IsInt32()); | 348 CHECK(args[0]->IsInt32()); |
255 // Callback function. | 349 // Callback function. |
256 CHECK(args[1]->IsFunction()); | 350 CHECK(args[1]->IsFunction()); |
257 | 351 |
258 int element_instance_id = args[0]->Int32Value(); | 352 int element_instance_id = args[0]->Int32Value(); |
259 // An element instance ID uniquely identifies a ExtensionsGuestViewContainer | 353 // An element instance ID uniquely identifies a GuestViewContainer within a |
260 // within a RenderView. | 354 // RenderView. |
261 auto guest_view_container = static_cast<ExtensionsGuestViewContainer*>( | 355 auto* guest_view_container = |
262 guest_view::GuestViewContainer::FromID(element_instance_id)); | 356 guest_view::GuestViewContainer::FromID(element_instance_id); |
263 if (!guest_view_container) | 357 if (!guest_view_container) |
264 return; | 358 return; |
265 | 359 |
266 guest_view_container->RegisterDestructionCallback(args[1].As<v8::Function>(), | 360 guest_view_container->RegisterDestructionCallback(args[1].As<v8::Function>(), |
267 args.GetIsolate()); | 361 args.GetIsolate()); |
268 | 362 |
269 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); | 363 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); |
270 } | 364 } |
271 | 365 |
272 void GuestViewInternalCustomBindings::RegisterElementResizeCallback( | 366 void GuestViewInternalCustomBindings::RegisterElementResizeCallback( |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 const v8::FunctionCallbackInfo<v8::Value>& args) { | 422 const v8::FunctionCallbackInfo<v8::Value>& args) { |
329 // Gesture is required to request fullscreen. | 423 // Gesture is required to request fullscreen. |
330 blink::WebScopedUserGesture user_gesture; | 424 blink::WebScopedUserGesture user_gesture; |
331 CHECK_EQ(args.Length(), 1); | 425 CHECK_EQ(args.Length(), 1); |
332 CHECK(args[0]->IsFunction()); | 426 CHECK(args[0]->IsFunction()); |
333 v8::Local<v8::Value> no_args; | 427 v8::Local<v8::Value> no_args; |
334 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args); | 428 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args); |
335 } | 429 } |
336 | 430 |
337 } // namespace extensions | 431 } // namespace extensions |
OLD | NEW |