summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/browser_actions_toolbar_gtk.cc
blob: 74b46c299407f5d5d89ccceb334e4fa52b1dac52 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// 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/gtk/browser_actions_toolbar_gtk.h"

#include <gtk/gtk.h>
#include <vector>

#include "app/gfx/canvas_paint.h"
#include "app/gfx/gtk_util.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/extensions/image_loading_tracker.h"
#include "chrome/browser/gtk/extension_popup_gtk.h"
#include "chrome/browser/gtk/gtk_chrome_button.h"
#include "chrome/browser/gtk/gtk_theme_provider.h"
#include "chrome/browser/profile.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/gtk_util.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_source.h"
#include "chrome/common/notification_type.h"

// The size of each button on the toolbar.
static const int kButtonSize = 29;

class BrowserActionButton : public NotificationObserver,
                            public ImageLoadingTracker::Observer {
 public:
  BrowserActionButton(Browser* browser, Extension* extension)
      : browser_(browser),
        extension_(extension),
        button_(gtk_chrome_button_new()),
        gdk_icon_(NULL) {
    DCHECK(extension_->browser_action());

    gtk_widget_set_size_request(button_.get(), kButtonSize, kButtonSize);

    browser_action_icons_.resize(
        extension->browser_action()->icon_paths().size());
    tracker_ = new ImageLoadingTracker(this, browser_action_icons_.size());
    for (size_t i = 0; i < extension->browser_action()->icon_paths().size();
         ++i) {
      tracker_->PostLoadImageTask(
          extension->GetResource(extension->browser_action()->icon_paths()[i]),
          gfx::Size(Extension::kBrowserActionIconMaxSize,
                    Extension::kBrowserActionIconMaxSize));
    }

    OnStateUpdated();

    // We need to hook up extension popups here. http://crbug.com/23897
    g_signal_connect(button_.get(), "clicked",
                     G_CALLBACK(OnButtonClicked), this);
    g_signal_connect_after(button_.get(), "expose-event",
                           G_CALLBACK(OnExposeEvent), this);

    registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
                   Source<ExtensionAction>(extension->browser_action()));
    registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
                   NotificationService::AllSources());

    OnThemeChanged();
  }

  ~BrowserActionButton() {
    if (gdk_icon_)
      g_object_unref(gdk_icon_);

    button_.Destroy();
    tracker_->StopTrackingImageLoad();
  }

  GtkWidget* widget() { return button_.get(); }

  void Observe(NotificationType type,
               const NotificationSource& source,
               const NotificationDetails& details) {
    if (type == NotificationType::EXTENSION_BROWSER_ACTION_UPDATED)
      OnStateUpdated();
    else if (type == NotificationType::BROWSER_THEME_CHANGED)
      OnThemeChanged();
    else
      NOTREACHED();
  }

  // ImageLoadingTracker::Observer implementation.
  void OnImageLoaded(SkBitmap* image, size_t index) {
    DCHECK(index < browser_action_icons_.size());
    browser_action_icons_[index] = image ? *image : SkBitmap();

    OnStateUpdated();
  }

 private:
  // Called when the tooltip has changed or an image has loaded.
  void OnStateUpdated() {
    gtk_widget_set_tooltip_text(button_.get(),
        extension_->browser_action_state()->title().c_str());

    SkBitmap* image = extension_->browser_action_state()->icon();
    if (!image) {
      if (static_cast<size_t>(
          extension_->browser_action_state()->icon_index()) <
              browser_action_icons_.size()) {
        image = &browser_action_icons_[
            extension_->browser_action_state()->icon_index()];
      }
    }

    if (image && !image->empty()) {
      GdkPixbuf* current_gdk_icon = gdk_icon_;
      gdk_icon_ = gfx::GdkPixbufFromSkBitmap(image);
      gtk_button_set_image(GTK_BUTTON(button_.get()),
                           gtk_image_new_from_pixbuf(gdk_icon_));
      if (current_gdk_icon)
        g_object_unref(current_gdk_icon);
    }
  }

  void OnThemeChanged() {
    gtk_chrome_button_set_use_gtk_rendering(GTK_CHROME_BUTTON(button_.get()),
        GtkThemeProvider::GetFrom(browser_->profile())->UseGtkTheme());
  }

  static void OnButtonClicked(GtkWidget* widget, BrowserActionButton* action) {
    if (action->extension_->browser_action()->is_popup()) {
      ExtensionPopupGtk::Show(
          action->extension_->browser_action()->popup_url(),
          action->browser_,
          gtk_util::GetWidgetRectRelativeToToplevel(widget));
    } else {
      ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
          action->browser_->profile(), action->extension_->id(),
          action->browser_);
    }
  }

  static gboolean OnExposeEvent(GtkWidget* widget,
                                GdkEventExpose* event,
                                BrowserActionButton* action) {
    if (action->extension_->browser_action_state()->badge_text().empty())
      return FALSE;

    gfx::CanvasPaint canvas(event, false);
    gfx::Rect bounding_rect(widget->allocation);
    action->extension_->browser_action_state()->PaintBadge(&canvas,
                                                           bounding_rect);
    return FALSE;
  }

  // The Browser that executes a command when the button is pressed.
  Browser* browser_;

  // The extension that contains this browser action.
  Extension* extension_;

  // The gtk widget for this browser action.
  OwnedWidgetGtk button_;

  // Loads the button's icons for us on the file thread.
  ImageLoadingTracker* tracker_;

  // Icons for all the different states the button can be in. These will be
  // empty while they are loading.
  std::vector<SkBitmap> browser_action_icons_;

  // SkBitmap must be converted to GdkPixbuf before assignment to the button.
  // This stores the current icon while it is in use.
  GdkPixbuf* gdk_icon_;

  NotificationRegistrar registrar_;
};

BrowserActionsToolbarGtk::BrowserActionsToolbarGtk(Browser* browser)
    : browser_(browser),
      profile_(browser->profile()),
      hbox_(gtk_hbox_new(0, FALSE)) {
  ExtensionsService* extension_service = profile_->GetExtensionsService();
  registrar_.Add(this, NotificationType::EXTENSION_LOADED,
                 Source<ExtensionsService>(extension_service));
  registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
                 Source<ExtensionsService>(extension_service));
  registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
                 Source<ExtensionsService>(extension_service));

  CreateAllButtons();
}

BrowserActionsToolbarGtk::~BrowserActionsToolbarGtk() {
  hbox_.Destroy();
}

void BrowserActionsToolbarGtk::Observe(NotificationType type,
                                       const NotificationSource& source,
                                       const NotificationDetails& details) {
  Extension* extension = Details<Extension>(details).ptr();

  if (type == NotificationType::EXTENSION_LOADED) {
    CreateButtonForExtension(extension);
  } else if (type == NotificationType::EXTENSION_UNLOADED ||
             type == NotificationType::EXTENSION_UNLOADED_DISABLED) {
    RemoveButtonForExtension(extension);
  } else {
    NOTREACHED() << "Received unexpected notification";
  }
}

void BrowserActionsToolbarGtk::CreateAllButtons() {
  ExtensionsService* extension_service = profile_->GetExtensionsService();
  if (!extension_service)  // The |extension_service| can be NULL in Incognito.
    return;

  // Get all browser actions, including those with popups.
  std::vector<ExtensionAction*> browser_actions =
      extension_service->GetBrowserActions(true);

  for (size_t i = 0; i < browser_actions.size(); ++i) {
    Extension* extension = extension_service->GetExtensionById(
        browser_actions[i]->extension_id());
    CreateButtonForExtension(extension);
  }
}

void BrowserActionsToolbarGtk::CreateButtonForExtension(Extension* extension) {
  // Only show extensions with browser actions and that have an icon.
  if (!extension->browser_action() ||
      extension->browser_action()->icon_paths().empty()) {
    return;
  }

  RemoveButtonForExtension(extension);
  linked_ptr<BrowserActionButton> button(
      new BrowserActionButton(browser_, extension));
  gtk_box_pack_end(GTK_BOX(hbox_.get()), button->widget(), FALSE, FALSE, 0);
  gtk_widget_show(button->widget());
  extension_button_map_[extension->id()] = button;

  UpdateVisibility();
}

void BrowserActionsToolbarGtk::RemoveButtonForExtension(Extension* extension) {
  if (extension_button_map_.erase(extension->id()))
    UpdateVisibility();
}

void BrowserActionsToolbarGtk::UpdateVisibility() {
  if (button_count() == 0)
    gtk_widget_hide(widget());
  else
    gtk_widget_show(widget());
}