| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/gtk/browser_actions_toolbar_gtk.h" | 5 #include "chrome/browser/gtk/browser_actions_toolbar_gtk.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "app/gfx/canvas_paint.h" | 10 #include "app/gfx/canvas_paint.h" |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 GdkPixbuf* default_icon_; | 196 GdkPixbuf* default_icon_; |
| 197 | 197 |
| 198 NotificationRegistrar registrar_; | 198 NotificationRegistrar registrar_; |
| 199 | 199 |
| 200 friend class BrowserActionsToolbarGtk; | 200 friend class BrowserActionsToolbarGtk; |
| 201 }; | 201 }; |
| 202 | 202 |
| 203 BrowserActionsToolbarGtk::BrowserActionsToolbarGtk(Browser* browser) | 203 BrowserActionsToolbarGtk::BrowserActionsToolbarGtk(Browser* browser) |
| 204 : browser_(browser), | 204 : browser_(browser), |
| 205 profile_(browser->profile()), | 205 profile_(browser->profile()), |
| 206 model_(NULL), |
| 206 hbox_(gtk_hbox_new(FALSE, kBrowserActionButtonPadding)) { | 207 hbox_(gtk_hbox_new(FALSE, kBrowserActionButtonPadding)) { |
| 207 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | 208 ExtensionsService* extension_service = profile_->GetExtensionsService(); |
| 208 Source<Profile>(profile_)); | 209 // The |extension_service| can be NULL in Incognito. |
| 209 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | 210 if (extension_service) { |
| 210 Source<Profile>(profile_)); | 211 model_ = extension_service->toolbar_model(); |
| 211 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED, | 212 model_->AddObserver(this); |
| 212 Source<Profile>(profile_)); | 213 CreateAllButtons(); |
| 213 | 214 } |
| 214 CreateAllButtons(); | |
| 215 } | 215 } |
| 216 | 216 |
| 217 BrowserActionsToolbarGtk::~BrowserActionsToolbarGtk() { | 217 BrowserActionsToolbarGtk::~BrowserActionsToolbarGtk() { |
| 218 if (model_) |
| 219 model_->RemoveObserver(this); |
| 218 hbox_.Destroy(); | 220 hbox_.Destroy(); |
| 219 } | 221 } |
| 220 | 222 |
| 221 int BrowserActionsToolbarGtk::GetCurrentTabId() { | 223 int BrowserActionsToolbarGtk::GetCurrentTabId() { |
| 222 TabContents* selected_tab = browser_->GetSelectedTabContents(); | 224 TabContents* selected_tab = browser_->GetSelectedTabContents(); |
| 223 if (!selected_tab) | 225 if (!selected_tab) |
| 224 return -1; | 226 return -1; |
| 225 | 227 |
| 226 return selected_tab->controller().session_id().id(); | 228 return selected_tab->controller().session_id().id(); |
| 227 } | 229 } |
| 228 | 230 |
| 229 void BrowserActionsToolbarGtk::Update() { | 231 void BrowserActionsToolbarGtk::Update() { |
| 230 for (ExtensionButtonMap::iterator iter = extension_button_map_.begin(); | 232 for (ExtensionButtonMap::iterator iter = extension_button_map_.begin(); |
| 231 iter != extension_button_map_.end(); ++iter) { | 233 iter != extension_button_map_.end(); ++iter) { |
| 232 iter->second->UpdateState(); | 234 iter->second->UpdateState(); |
| 233 } | 235 } |
| 234 } | 236 } |
| 235 | 237 |
| 236 void BrowserActionsToolbarGtk::Observe(NotificationType type, | |
| 237 const NotificationSource& source, | |
| 238 const NotificationDetails& details) { | |
| 239 Extension* extension = Details<Extension>(details).ptr(); | |
| 240 | |
| 241 if (type == NotificationType::EXTENSION_LOADED) { | |
| 242 CreateButtonForExtension(extension); | |
| 243 } else if (type == NotificationType::EXTENSION_UNLOADED || | |
| 244 type == NotificationType::EXTENSION_UNLOADED_DISABLED) { | |
| 245 RemoveButtonForExtension(extension); | |
| 246 } else { | |
| 247 NOTREACHED() << "Received unexpected notification"; | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 void BrowserActionsToolbarGtk::CreateAllButtons() { | 238 void BrowserActionsToolbarGtk::CreateAllButtons() { |
| 252 ExtensionsService* extension_service = profile_->GetExtensionsService(); | 239 for (ExtensionList::iterator iter = model_->begin(); |
| 253 if (!extension_service) // The |extension_service| can be NULL in Incognito. | 240 iter != model_->end(); ++iter) { |
| 254 return; | 241 CreateButtonForExtension(*iter); |
| 255 | |
| 256 for (size_t i = 0; i < extension_service->extensions()->size(); ++i) { | |
| 257 Extension* extension = extension_service->GetExtensionById( | |
| 258 extension_service->extensions()->at(i)->id(), false); | |
| 259 CreateButtonForExtension(extension); | |
| 260 } | 242 } |
| 261 } | 243 } |
| 262 | 244 |
| 263 void BrowserActionsToolbarGtk::CreateButtonForExtension(Extension* extension) { | 245 void BrowserActionsToolbarGtk::CreateButtonForExtension(Extension* extension) { |
| 264 // Only show extensions with browser actions. | |
| 265 if (!extension->browser_action()) | |
| 266 return; | |
| 267 | |
| 268 RemoveButtonForExtension(extension); | 246 RemoveButtonForExtension(extension); |
| 269 linked_ptr<BrowserActionButton> button( | 247 linked_ptr<BrowserActionButton> button( |
| 270 new BrowserActionButton(this, extension)); | 248 new BrowserActionButton(this, extension)); |
| 271 gtk_box_pack_end(GTK_BOX(hbox_.get()), button->widget(), FALSE, FALSE, 0); | 249 gtk_box_pack_end(GTK_BOX(hbox_.get()), button->widget(), FALSE, FALSE, 0); |
| 272 gtk_widget_show(button->widget()); | 250 gtk_widget_show(button->widget()); |
| 273 extension_button_map_[extension->id()] = button; | 251 extension_button_map_[extension->id()] = button; |
| 274 | 252 |
| 275 UpdateVisibility(); | 253 UpdateVisibility(); |
| 276 } | 254 } |
| 277 | 255 |
| 278 void BrowserActionsToolbarGtk::RemoveButtonForExtension(Extension* extension) { | 256 void BrowserActionsToolbarGtk::RemoveButtonForExtension(Extension* extension) { |
| 279 if (extension_button_map_.erase(extension->id())) | 257 if (extension_button_map_.erase(extension->id())) |
| 280 UpdateVisibility(); | 258 UpdateVisibility(); |
| 281 } | 259 } |
| 282 | 260 |
| 283 void BrowserActionsToolbarGtk::UpdateVisibility() { | 261 void BrowserActionsToolbarGtk::UpdateVisibility() { |
| 284 if (button_count() == 0) | 262 if (button_count() == 0) |
| 285 gtk_widget_hide(widget()); | 263 gtk_widget_hide(widget()); |
| 286 else | 264 else |
| 287 gtk_widget_show(widget()); | 265 gtk_widget_show(widget()); |
| 288 } | 266 } |
| 267 |
| 268 void BrowserActionsToolbarGtk::ExtensionAdded(Extension* extension, |
| 269 int index) { |
| 270 // TODO(estade): respect |index|. |
| 271 CreateButtonForExtension(extension); |
| 272 } |
| 273 |
| 274 void BrowserActionsToolbarGtk::ExtensionRemoved(Extension* extension) { |
| 275 RemoveButtonForExtension(extension); |
| 276 } |
| OLD | NEW |