diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-27 00:10:52 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-27 00:10:52 +0000 |
commit | 56ce6e5e9c393626dc3da68e9703423fffa273df (patch) | |
tree | 838bc7600dbcd7b37ff6180c9ed256856b960ead /chrome/browser/gtk | |
parent | 530a94a66cc9a68075bce7c625cb6e1d480e22a8 (diff) | |
download | chromium_src-56ce6e5e9c393626dc3da68e9703423fffa273df.zip chromium_src-56ce6e5e9c393626dc3da68e9703423fffa273df.tar.gz chromium_src-56ce6e5e9c393626dc3da68e9703423fffa273df.tar.bz2 |
Move page actions over to ExtensionAction2 and get rid of
extension_action.*.
Final bit of refactor will be to rename ExtensionAction2 to
ExtensionAction will be the next CL.
BUG=24472,25844
Review URL: http://codereview.chromium.org/332021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30133 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/gtk')
-rw-r--r-- | chrome/browser/gtk/browser_actions_toolbar_gtk.cc | 10 | ||||
-rw-r--r-- | chrome/browser/gtk/location_bar_view_gtk.cc | 128 | ||||
-rw-r--r-- | chrome/browser/gtk/location_bar_view_gtk.h | 16 |
3 files changed, 92 insertions, 62 deletions
diff --git a/chrome/browser/gtk/browser_actions_toolbar_gtk.cc b/chrome/browser/gtk/browser_actions_toolbar_gtk.cc index b137203..583f8ce 100644 --- a/chrome/browser/gtk/browser_actions_toolbar_gtk.cc +++ b/chrome/browser/gtk/browser_actions_toolbar_gtk.cc @@ -53,7 +53,7 @@ class BrowserActionButton : public NotificationObserver, // The Browser Action API does not allow the default icon path to be // changed at runtime, so we can load this now and cache it. - std::string path = extension_->browser_action()->GetDefaultIconPath(); + std::string path = extension_->browser_action()->default_icon_path(); if (!path.empty()) { tracker_ = new ImageLoadingTracker(this, 1); tracker_->PostLoadImageTask(extension_->GetResource(path), @@ -103,7 +103,8 @@ class BrowserActionButton : public NotificationObserver, // ImageLoadingTracker::Observer implementation. void OnImageLoaded(SkBitmap* image, size_t index) { - default_icon_ = gfx::GdkPixbufFromSkBitmap(image); + if (image) + default_icon_ = gfx::GdkPixbufFromSkBitmap(image); UpdateState(); } @@ -167,10 +168,7 @@ class BrowserActionButton : public NotificationObserver, gfx::CanvasPaint canvas(event, false); gfx::Rect bounding_rect(widget->allocation); - ExtensionActionState::PaintBadge(&canvas, bounding_rect, - action->GetBadgeText(tab_id), - action->GetBadgeTextColor(tab_id), - action->GetBadgeBackgroundColor(tab_id)); + action->PaintBadge(&canvas, bounding_rect, tab_id); return FALSE; } diff --git a/chrome/browser/gtk/location_bar_view_gtk.cc b/chrome/browser/gtk/location_bar_view_gtk.cc index 38c24c0..198aa31 100644 --- a/chrome/browser/gtk/location_bar_view_gtk.cc +++ b/chrome/browser/gtk/location_bar_view_gtk.cc @@ -33,7 +33,6 @@ #include "chrome/common/page_transition_types.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" -#include "third_party/skia/include/core/SkBitmap.h" #include "webkit/glue/window_open_disposition.h" namespace { @@ -406,17 +405,15 @@ void LocationBarViewGtk::FocusSearch() { } void LocationBarViewGtk::UpdatePageActions() { - std::vector<ExtensionAction*> page_actions; - if (profile_->GetExtensionsService()) - page_actions = profile_->GetExtensionsService()->GetPageActions(); - - // Page actions can be created without an icon, so make sure we count only - // those that have been given an icon. - for (size_t i = 0; i < page_actions.size();) { - if (page_actions[i]->icon_paths().empty()) - page_actions.erase(page_actions.begin() + i); - else - ++i; + std::vector<ExtensionAction2*> page_actions; + ExtensionsService* service = profile_->GetExtensionsService(); + if (!service) + return; + + // Find all the page actions. + for (size_t i = 0; i < service->extensions()->size(); ++i) { + if (service->extensions()->at(i)->page_action()) + page_actions.push_back(service->extensions()->at(i)->page_action()); } // Initialize on the first call, or re-inialize if more extensions have been @@ -690,11 +687,10 @@ gboolean LocationBarViewGtk::OnSecurityIconPressed( LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( LocationBarViewGtk* owner, Profile* profile, - const ExtensionAction* page_action) + ExtensionAction2* page_action) : owner_(owner), profile_(profile), page_action_(page_action), - last_icon_skbitmap_(NULL), last_icon_pixbuf_(NULL) { event_box_.Own(gtk_event_box_new()); gtk_widget_set_size_request(event_box_.get(), kButtonSize, kButtonSize); @@ -713,11 +709,14 @@ LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( page_action->extension_id()); DCHECK(extension); - DCHECK(!page_action->icon_paths().empty()); - const std::vector<std::string>& icon_paths = page_action->icon_paths(); - pixbufs_.resize(icon_paths.size()); + // Load all the icons declared in the manifest. This is the contents of the + // icons array, plus the default_icon property, if any. + std::vector<std::string> icon_paths(*page_action->icon_paths()); + if (!page_action_->default_icon_path().empty()) + icon_paths.push_back(page_action_->default_icon_path()); + tracker_ = new ImageLoadingTracker(this, icon_paths.size()); - for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); + for (std::vector<std::string>::iterator iter = icon_paths.begin(); iter != icon_paths.end(); ++iter) { tracker_->PostLoadImageTask( extension->GetResource(*iter), @@ -731,9 +730,9 @@ LocationBarViewGtk::PageActionViewGtk::~PageActionViewGtk() { tracker_->StopTrackingImageLoad(); image_.Destroy(); event_box_.Destroy(); - for (size_t i = 0; i < pixbufs_.size(); ++i) { - if (pixbufs_[i]) - g_object_unref(pixbufs_[i]); + for (PixbufMap::iterator iter = pixbufs_.begin(); iter != pixbufs_.end(); + ++iter) { + g_object_unref(iter->second); } if (last_icon_pixbuf_) g_object_unref(last_icon_pixbuf_); @@ -746,35 +745,48 @@ void LocationBarViewGtk::PageActionViewGtk::UpdateVisibility( current_tab_id_ = ExtensionTabUtil::GetTabId(contents); current_url_ = url; - const ExtensionActionState* state = - contents->GetPageActionState(page_action_); - bool visible = state && !state->hidden(); + bool visible = page_action_->GetIsVisible(current_tab_id_); if (visible) { // Set the tooltip. - if (state->title().empty()) - gtk_widget_set_tooltip_text(event_box_.get(), - page_action_->title().c_str()); - else - gtk_widget_set_tooltip_text(event_box_.get(), state->title().c_str()); + gtk_widget_set_tooltip_text(event_box_.get(), + page_action_->GetTitle(current_tab_id_).c_str()); // Set the image. - SkBitmap* icon = state->icon(); + // It can come from three places. In descending order of priority: + // - The developer can set it dynamically by path or bitmap. It will be in + // page_action_->GetIcon(). + // - The developer can set it dyanmically by index. It will be in + // page_action_->GetIconIndex(). + // - It can be set in the manifest by path. It will be in page_action_-> + // default_icon_path(). + + // First look for a dynamically set bitmap. + SkBitmap icon = page_action_->GetIcon(current_tab_id_); GdkPixbuf* pixbuf = NULL; - if (icon) { - if (icon != last_icon_skbitmap_) { + if (!icon.isNull()) { + if (icon.pixelRef() != last_icon_skbitmap_.pixelRef()) { if (last_icon_pixbuf_) g_object_unref(last_icon_pixbuf_); last_icon_skbitmap_ = icon; - last_icon_pixbuf_ = gfx::GdkPixbufFromSkBitmap(icon); + last_icon_pixbuf_ = gfx::GdkPixbufFromSkBitmap(&icon); } DCHECK(last_icon_pixbuf_); pixbuf = last_icon_pixbuf_; } else { - int index = state->icon_index(); - // The image index (if not within bounds) will be set to the first image. - if (index < 0 || index >= static_cast<int>(pixbufs_.size())) - index = 0; - pixbuf = pixbufs_[index]; + // Otherwise look for a dynamically set index, or fall back to the + // default path. + int icon_index = page_action_->GetIconIndex(current_tab_id_); + std::string icon_path; + if (icon_index >= 0) + icon_path = page_action_->icon_paths()->at(icon_index); + else + icon_path = page_action_->default_icon_path(); + + if (!icon_path.empty()) { + PixbufMap::iterator iter = pixbufs_.find(icon_path); + if (iter != pixbufs_.end()) + pixbuf = iter->second; + } } // The pixbuf might not be loaded yet. @@ -793,10 +805,27 @@ void LocationBarViewGtk::PageActionViewGtk::UpdateVisibility( void LocationBarViewGtk::PageActionViewGtk::OnImageLoaded(SkBitmap* image, size_t index) { - DCHECK(index < pixbufs_.size()); - if (index == pixbufs_.size() - 1) - tracker_ = NULL; // The tracker object will delete itself when we return. - pixbufs_[index] = gfx::GdkPixbufFromSkBitmap(image); + // We loaded icons()->size() icons, plus one extra if the page action had + // a default icon. + size_t total_icons = page_action_->icon_paths()->size(); + if (!page_action_->default_icon_path().empty()) + total_icons++; + DCHECK(index < total_icons); + + // Map the index of the loaded image back to its name. If we ever get an + // index greater than the number of icons, it must be the default icon. + if (image) { + GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(image); + if (index < page_action_->icon_paths()->size()) + pixbufs_[page_action_->icon_paths()->at(index)] = pixbuf; + else + pixbufs_[page_action_->default_icon_path()] = pixbuf; + } + + // If we are done, release the tracker. + if (index == (total_icons - 1)) + tracker_ = NULL; + owner_->UpdatePageActions(); } @@ -821,16 +850,17 @@ gboolean LocationBarViewGtk::PageActionViewGtk::OnExposeEvent( TabContents* contents = view->owner_->browser_->GetSelectedTabContents(); if (!contents) return FALSE; - const ExtensionActionState* state = - contents->GetPageActionState(view->page_action_); - if (!state || state->badge_text().empty()) + + int tab_id = ExtensionTabUtil::GetTabId(contents); + if (tab_id < 0) + return FALSE; + + std::string badge_text = view->page_action_->GetBadgeText(tab_id); + if (badge_text.empty()) return FALSE; gfx::CanvasPaint canvas(event, false); gfx::Rect bounding_rect(widget->allocation); - ExtensionActionState::PaintBadge(&canvas, bounding_rect, - state->badge_text(), - state->badge_text_color(), - state->badge_background_color()); + view->page_action_->PaintBadge(&canvas, bounding_rect, tab_id); return FALSE; } diff --git a/chrome/browser/gtk/location_bar_view_gtk.h b/chrome/browser/gtk/location_bar_view_gtk.h index bedf724..3d38c4e 100644 --- a/chrome/browser/gtk/location_bar_view_gtk.h +++ b/chrome/browser/gtk/location_bar_view_gtk.h @@ -7,8 +7,8 @@ #include <gtk/gtk.h> +#include <map> #include <string> -#include <vector> #include "base/basictypes.h" #include "base/scoped_ptr.h" @@ -21,14 +21,15 @@ #include "chrome/common/notification_registrar.h" #include "chrome/common/owned_widget_gtk.h" #include "chrome/common/page_transition_types.h" +#include "third_party/skia/include/core/SkBitmap.h" #include "webkit/glue/window_open_disposition.h" class AutocompleteEditViewGtk; class BubblePositioner; class Browser; class CommandUpdater; +class ExtensionAction2; class GtkThemeProvider; -class ExtensionAction; class Profile; class SkBitmap; class TabContents; @@ -105,7 +106,7 @@ class LocationBarViewGtk : public AutocompleteEditController, public: PageActionViewGtk( LocationBarViewGtk* owner, Profile* profile, - const ExtensionAction* page_action); + ExtensionAction2* page_action); virtual ~PageActionViewGtk(); GtkWidget* widget() { return event_box_.get(); } @@ -133,15 +134,16 @@ class LocationBarViewGtk : public AutocompleteEditController, // The PageAction that this view represents. The PageAction is not owned by // us, it resides in the extension of this particular profile. - const ExtensionAction* page_action_; + ExtensionAction2* page_action_; - // The icons representing different states for the page action. - std::vector<GdkPixbuf*> pixbufs_; + // A cache of all the different icon paths associated with this page action. + typedef std::map<std::string, GdkPixbuf*> PixbufMap; + PixbufMap pixbufs_; // A cache of the last dynamically generated bitmap and the pixbuf that // corresponds to it. We keep track of both so we can free old pixbufs as // their icons are replaced. - SkBitmap* last_icon_skbitmap_; + SkBitmap last_icon_skbitmap_; GdkPixbuf* last_icon_pixbuf_; // The object that is waiting for the image loading to complete |