summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/extension_view_mac.mm
blob: b1f9d0e15c1720f4606ef1fd0ece7adc0c912877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/cocoa/extension_view_mac.h"

#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/render_widget_host_view_mac.h"

ExtensionViewMac::ExtensionViewMac(ExtensionHost* extension_host,
                                   Browser* browser)
    : is_toolstrip_(true),
      browser_(browser),
      extension_host_(extension_host),
      render_widget_host_view_(NULL) {
  DCHECK(extension_host_);
}

ExtensionViewMac::~ExtensionViewMac() {
  if (render_widget_host_view_)
    [render_widget_host_view_->native_view() release];
}

void ExtensionViewMac::Init() {
  CreateWidgetHostView();
}

gfx::NativeView ExtensionViewMac::native_view() {
  DCHECK(render_widget_host_view_);
  return render_widget_host_view_->native_view();
}

RenderViewHost* ExtensionViewMac::render_view_host() const {
  return extension_host_->render_view_host();
}

void ExtensionViewMac::SetBackground(const SkBitmap& background) {
  DCHECK(render_widget_host_view_);
  if (render_view_host()->IsRenderViewLive()) {
    render_widget_host_view_->SetBackground(background);
  } else {
    pending_background_ = background;
  }
}

void ExtensionViewMac::UpdatePreferredSize(const gfx::Size& new_size) {
  // TODO(thakis, erikkay): Windows does some tricks to resize the extension
  // view not before it's visible. Do something similar here.

  // No need to use CA here, our caller calls us repeatedly to animate the
  // resizing.
  NSView* view = native_view();
  NSRect frame = [view frame];
  frame.size.width = new_size.width();
  frame.size.height = new_size.height();

  // RenderWidgetHostViewCocoa overrides setFrame but not setFrameSize.
  [view setFrame:frame];
  [view setNeedsDisplay:YES];
}

void ExtensionViewMac::RenderViewCreated() {
  if (!pending_background_.empty() && render_view_host()->view()) {
    render_widget_host_view_->SetBackground(pending_background_);
    pending_background_.reset();
  }
}

void ExtensionViewMac::CreateWidgetHostView() {
  DCHECK(!render_widget_host_view_);
  render_widget_host_view_ = new RenderWidgetHostViewMac(render_view_host());

  // The RenderWidgetHostViewMac is owned by its native view, which is created
  // in an autoreleased state. retain it, so that it doesn't immediately
  // disappear.
  [render_widget_host_view_->native_view() retain];

  extension_host_->CreateRenderViewSoon(render_widget_host_view_);
}