summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/browser_actions_toolbar_gtk.cc
blob: a5f8796150583e60b700af165ad0536e458f1489 (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
// 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/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/gtk_chrome_button.h"
#include "chrome/browser/profile.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_details.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()) {
    DCHECK(extension_->browser_action());

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

    browser_action_icons_.resize(
        extension->browser_action()->icon_paths().size(), NULL);
    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]));
    }

    OnStateUpdated();

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

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

  ~BrowserActionButton() {
    for (size_t i = 0; i < browser_action_icons_.size(); ++i) {
      if (browser_action_icons_[i])
        g_object_unref(browser_action_icons_[i]);
    }

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

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

  static void OnButtonClicked(GtkWidget* widget, BrowserActionButton* action) {
    ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
        action->browser_->profile(), action->extension_->id(),
        action->browser_);
  }

  // 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());

    if (browser_action_icons_.empty())
      return;

    GdkPixbuf* image =
        browser_action_icons_[extension_->browser_action_state()->icon_index()];
    if (image) {
      gtk_button_set_image(GTK_BUTTON(button_.get()),
                           gtk_image_new_from_pixbuf(image));
    }
  }

  // NotificationObserver implementation.
  void Observe(NotificationType type,
               const NotificationSource& source,
               const NotificationDetails& details) {
    OnStateUpdated();
  }

  // ImageLoadingTracker::Observer implementation.
  void OnImageLoaded(SkBitmap* image, size_t index) {
    if (image) {
      browser_action_icons_[index] = gfx::GdkPixbufFromSkBitmap(image);
    } else {
      SkBitmap empty;
      browser_action_icons_[index] = gfx::GdkPixbufFromSkBitmap(&empty);
    }

    OnStateUpdated();
  }

 private:
  // 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 NULL
  // while they are loading.
  std::vector<GdkPixbuf*> browser_action_icons_;

  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]->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());
}