Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / renderer / guest_view / extensions_guest_view_container.cc
blob3fa9882a9cd013d6cd689f5677108529eb883450
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 element_resize_isolate_(nullptr),
17 weak_ptr_factory_(this) {
20 ExtensionsGuestViewContainer::~ExtensionsGuestViewContainer() {
23 void ExtensionsGuestViewContainer::OnDestroy(bool embedder_frame_destroyed) {
26 void ExtensionsGuestViewContainer::RegisterElementResizeCallback(
27 v8::Local<v8::Function> callback,
28 v8::Isolate* isolate) {
29 element_resize_callback_.Reset(isolate, callback);
30 element_resize_isolate_ = isolate;
33 void ExtensionsGuestViewContainer::DidResizeElement(const gfx::Size& new_size) {
34 // Call the element resize callback, if one is registered.
35 if (element_resize_callback_.IsEmpty())
36 return;
38 base::MessageLoop::current()->PostTask(
39 FROM_HERE,
40 base::Bind(&ExtensionsGuestViewContainer::CallElementResizeCallback,
41 weak_ptr_factory_.GetWeakPtr(), new_size));
44 void ExtensionsGuestViewContainer::CallElementResizeCallback(
45 const gfx::Size& new_size) {
46 v8::HandleScope handle_scope(element_resize_isolate_);
47 v8::Local<v8::Function> callback = v8::Local<v8::Function>::New(
48 element_resize_isolate_, element_resize_callback_);
49 v8::Local<v8::Context> context = callback->CreationContext();
50 if (context.IsEmpty())
51 return;
53 const int argc = 2;
54 v8::Local<v8::Value> argv[argc] = {
55 v8::Integer::New(element_resize_isolate_, new_size.width()),
56 v8::Integer::New(element_resize_isolate_, new_size.height())};
58 v8::Context::Scope context_scope(context);
59 blink::WebScopedMicrotaskSuppression suppression;
61 callback->Call(context->Global(), argc, argv);
64 } // namespace extensions