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))); |
| 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 |
| 251 // ExtensionsIframeGuestViewContainer within a RenderView. |
| 252 auto* guest_view_container = static_cast<ExtensionsIframeGuestViewContainer*>( |
| 253 guest_view::GuestViewContainer::FromID(element_instance_id)); |
| 254 // This is the first time we hear about the |element_instance_id|. |
| 255 DCHECK(!guest_view_container); |
| 256 // The <webview> element's GC takes ownership of |guest_view_container|. |
| 257 guest_view_container = |
| 258 new extensions::ExtensionsIframeGuestViewContainer(embedder_parent_frame); |
| 259 guest_view_container->SetElementInstanceID(element_instance_id); |
| 260 |
| 261 linked_ptr<guest_view::GuestViewRequest> request( |
| 262 new GuestViewAttachIframeRequest( |
| 263 guest_view_container, render_frame->GetRoutingID(), guest_instance_id, |
| 264 params.Pass(), args.Length() == (num_required_params + 1) |
| 265 ? args[num_required_params].As<v8::Function>() |
| 266 : v8::Local<v8::Function>(), |
| 267 args.GetIsolate())); |
| 268 guest_view_container->IssueRequest(request); |
| 269 |
| 270 args.GetReturnValue().Set(v8::Boolean::New(context()->isolate(), true)); |
| 271 } |
| 272 |
180 void GuestViewInternalCustomBindings::DestroyContainer( | 273 void GuestViewInternalCustomBindings::DestroyContainer( |
181 const v8::FunctionCallbackInfo<v8::Value>& args) { | 274 const v8::FunctionCallbackInfo<v8::Value>& args) { |
182 args.GetReturnValue().SetNull(); | 275 args.GetReturnValue().SetNull(); |
183 | 276 |
184 if (args.Length() != 1) | 277 if (args.Length() != 1) |
185 return; | 278 return; |
186 | 279 |
187 // Element Instance ID. | 280 // Element Instance ID. |
188 if (!args[0]->IsInt32()) | 281 if (!args[0]->IsInt32()) |
189 return; | 282 return; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 const v8::FunctionCallbackInfo<v8::Value>& args) { | 421 const v8::FunctionCallbackInfo<v8::Value>& args) { |
329 // Gesture is required to request fullscreen. | 422 // Gesture is required to request fullscreen. |
330 blink::WebScopedUserGesture user_gesture; | 423 blink::WebScopedUserGesture user_gesture; |
331 CHECK_EQ(args.Length(), 1); | 424 CHECK_EQ(args.Length(), 1); |
332 CHECK(args[0]->IsFunction()); | 425 CHECK(args[0]->IsFunction()); |
333 v8::Local<v8::Value> no_args; | 426 v8::Local<v8::Value> no_args; |
334 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args); | 427 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 0, &no_args); |
335 } | 428 } |
336 | 429 |
337 } // namespace extensions | 430 } // namespace extensions |
OLD | NEW |