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
3 // found in the LICENSE file.
5 #include "extensions/renderer/guest_view/extensions_guest_view_container.h"
7 #include "content/public/renderer/render_frame.h"
8 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
9 #include "ui/gfx/geometry/size.h"
11 namespace extensions
{
13 ExtensionsGuestViewContainer::ExtensionsGuestViewContainer(
14 content::RenderFrame
* render_frame
)
15 : GuestViewContainer(render_frame
),
16 destruction_isolate_(nullptr),
17 element_resize_isolate_(nullptr),
18 weak_ptr_factory_(this) {
21 ExtensionsGuestViewContainer::~ExtensionsGuestViewContainer() {
22 // Call the destruction callback, if one is registered.
23 if (!destruction_callback_
.IsEmpty()) {
24 v8::HandleScope
handle_scope(destruction_isolate_
);
25 v8::Local
<v8::Function
> callback
= v8::Local
<v8::Function
>::New(
26 destruction_isolate_
, destruction_callback_
);
27 v8::Local
<v8::Context
> context
= callback
->CreationContext();
28 if (context
.IsEmpty())
31 v8::Context::Scope
context_scope(context
);
32 blink::WebScopedMicrotaskSuppression suppression
;
34 callback
->Call(context
->Global(), 0 /* argc */, nullptr);
38 void ExtensionsGuestViewContainer::RegisterDestructionCallback(
39 v8::Local
<v8::Function
> callback
,
40 v8::Isolate
* isolate
) {
41 destruction_callback_
.Reset(isolate
, callback
);
42 destruction_isolate_
= isolate
;
45 void ExtensionsGuestViewContainer::RegisterElementResizeCallback(
46 v8::Local
<v8::Function
> callback
,
47 v8::Isolate
* isolate
) {
48 element_resize_callback_
.Reset(isolate
, callback
);
49 element_resize_isolate_
= isolate
;
52 void ExtensionsGuestViewContainer::DidResizeElement(const gfx::Size
& new_size
) {
53 // Call the element resize callback, if one is registered.
54 if (element_resize_callback_
.IsEmpty())
57 base::MessageLoop::current()->PostTask(
59 base::Bind(&ExtensionsGuestViewContainer::CallElementResizeCallback
,
60 weak_ptr_factory_
.GetWeakPtr(), new_size
));
63 void ExtensionsGuestViewContainer::CallElementResizeCallback(
64 const gfx::Size
& new_size
) {
65 v8::HandleScope
handle_scope(element_resize_isolate_
);
66 v8::Local
<v8::Function
> callback
= v8::Local
<v8::Function
>::New(
67 element_resize_isolate_
, element_resize_callback_
);
68 v8::Local
<v8::Context
> context
= callback
->CreationContext();
69 if (context
.IsEmpty())
73 v8::Local
<v8::Value
> argv
[argc
] = {
74 v8::Integer::New(element_resize_isolate_
, new_size
.width()),
75 v8::Integer::New(element_resize_isolate_
, new_size
.height())};
77 v8::Context::Scope
context_scope(context
);
78 blink::WebScopedMicrotaskSuppression suppression
;
80 callback
->Call(context
->Global(), argc
, argv
);
83 } // namespace extensions