1// Copyright (c) 2011 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.
4
5#include "chrome/browser/ui/gtk/infobars/extension_infobar_gtk.h"
6
7#include "chrome/browser/extensions/extension_host.h"
8#include "chrome/common/extensions/extension.h"
9#include "chrome/common/extensions/extension_icon_set.h"
10#include "chrome/common/extensions/extension_resource.h"
11#include "content/browser/renderer_host/render_view_host.h"
12#include "content/browser/renderer_host/render_widget_host_view.h"
13#include "grit/theme_resources.h"
14#include "ui/base/resource/resource_bundle.h"
15#include "ui/gfx/gtk_util.h"
16
17ExtensionInfoBarGtk::ExtensionInfoBarGtk(ExtensionInfoBarDelegate* delegate)
18    : InfoBar(delegate),
19      tracker_(this),
20      delegate_(delegate),
21      view_(NULL) {
22  delegate_->extension_host()->view()->SetContainer(this);
23  BuildWidgets();
24}
25
26ExtensionInfoBarGtk::~ExtensionInfoBarGtk() {
27  // This view is not owned by us, so unparent.
28  gtk_widget_unparent(view_->native_view());
29}
30
31void ExtensionInfoBarGtk::OnImageLoaded(
32    SkBitmap* image, const ExtensionResource& resource, int index) {
33  if (!delegate_)
34    return;  // The delegate can go away while we asynchronously load images.
35
36  // ResourceBundle& rb = ResourceBundle::GetSharedInstance();
37  //
38  // SkBitmap* icon;
39  // if (!image || image->empty())
40  //  icon = rb.GetBitmapNamed(IDR_EXTENSIONS_SECTION);
41  // else
42  //  icon = image;
43  // TODO(finnur): Use the above code.
44  // We now have the icon for the menu button, show the menu button and layout.
45}
46
47void ExtensionInfoBarGtk::BuildWidgets() {
48  // Start loading the image for the menu button.
49  const Extension* extension = delegate_->extension_host()->extension();
50  ExtensionResource icon_resource = extension->GetIconResource(
51      Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_EXACTLY);
52  if (!icon_resource.relative_path().empty()) {
53    // Create a tracker to load the image. It will report back on OnImageLoaded.
54    tracker_.LoadImage(extension, icon_resource,
55                       gfx::Size(Extension::EXTENSION_ICON_BITTY,
56                                 Extension::EXTENSION_ICON_BITTY),
57                       ImageLoadingTracker::DONT_CACHE);
58  } else {
59    OnImageLoaded(NULL, icon_resource, 0);  // |image|, |index|.
60  }
61
62  ExtensionHost* extension_host = delegate_->extension_host();
63  view_ = extension_host->view();
64  if (gtk_widget_get_parent(view_->native_view())) {
65    gtk_widget_reparent(view_->native_view(), hbox_);
66    gtk_box_set_child_packing(GTK_BOX(hbox_), view_->native_view(),
67                              TRUE, TRUE, 0, GTK_PACK_START);
68  } else {
69    gtk_box_pack_start(GTK_BOX(hbox_), view_->native_view(), TRUE, TRUE, 0);
70  }
71
72  g_signal_connect(view_->native_view(), "size_allocate",
73                   G_CALLBACK(&OnSizeAllocateThunk), this);
74}
75
76void ExtensionInfoBarGtk::OnSizeAllocate(GtkWidget* widget,
77                                         GtkAllocation* allocation) {
78  gfx::Size new_size(allocation->width, allocation->height);
79
80  delegate_->extension_host()->view()->render_view_host()->view()
81      ->SetSize(new_size);
82}
83
84void ExtensionInfoBarGtk::OnExtensionPreferredSizeChanged(
85    ExtensionViewGtk* view,
86    const gfx::Size& new_size) {
87  // TODO(rafaelw) - Size the InfobarGtk vertically based on the preferred size
88  // of the content.
89}
90
91InfoBar* ExtensionInfoBarDelegate::CreateInfoBar() {
92  return new ExtensionInfoBarGtk(this);
93}
94