OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/gtk/location_bar_view_gtk.h" | 5 #include "chrome/browser/ui/gtk/location_bar_view_gtk.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 1681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1692 } | 1692 } |
1693 | 1693 |
1694 //////////////////////////////////////////////////////////////////////////////// | 1694 //////////////////////////////////////////////////////////////////////////////// |
1695 // LocationBarViewGtk::PageActionViewGtk | 1695 // LocationBarViewGtk::PageActionViewGtk |
1696 | 1696 |
1697 LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( | 1697 LocationBarViewGtk::PageActionViewGtk::PageActionViewGtk( |
1698 LocationBarViewGtk* owner, | 1698 LocationBarViewGtk* owner, |
1699 ExtensionAction* page_action) | 1699 ExtensionAction* page_action) |
1700 : owner_(NULL), | 1700 : owner_(NULL), |
1701 page_action_(page_action), | 1701 page_action_(page_action), |
1702 last_icon_pixbuf_(NULL), | |
1703 tracker_(this), | 1702 tracker_(this), |
1704 current_tab_id_(-1), | 1703 current_tab_id_(-1), |
1705 window_(NULL), | 1704 window_(NULL), |
1706 accel_group_(NULL), | 1705 accel_group_(NULL), |
1707 preview_enabled_(false), | 1706 preview_enabled_(false), |
1708 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_icon_animation_observer_( | 1707 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_icon_animation_observer_( |
1709 page_action->GetIconAnimation( | 1708 page_action->GetIconAnimation( |
1710 SessionID::IdForTab(owner->GetTabContents())), | 1709 SessionID::IdForTab(owner->GetTabContents())), |
1711 this)) { | 1710 this)) { |
1712 event_box_.Own(gtk_event_box_new()); | 1711 event_box_.Own(gtk_event_box_new()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1748 // We set the owner last of all so that we can determine whether we are in | 1747 // We set the owner last of all so that we can determine whether we are in |
1749 // the process of initializing this class or not. | 1748 // the process of initializing this class or not. |
1750 owner_ = owner; | 1749 owner_ = owner; |
1751 } | 1750 } |
1752 | 1751 |
1753 LocationBarViewGtk::PageActionViewGtk::~PageActionViewGtk() { | 1752 LocationBarViewGtk::PageActionViewGtk::~PageActionViewGtk() { |
1754 DisconnectPageActionAccelerator(); | 1753 DisconnectPageActionAccelerator(); |
1755 | 1754 |
1756 image_.Destroy(); | 1755 image_.Destroy(); |
1757 event_box_.Destroy(); | 1756 event_box_.Destroy(); |
1758 for (PixbufMap::iterator iter = pixbufs_.begin(); iter != pixbufs_.end(); | |
1759 ++iter) { | |
1760 g_object_unref(iter->second); | |
1761 } | |
1762 if (last_icon_pixbuf_) | |
1763 g_object_unref(last_icon_pixbuf_); | |
1764 } | 1757 } |
1765 | 1758 |
1766 bool LocationBarViewGtk::PageActionViewGtk::IsVisible() { | 1759 bool LocationBarViewGtk::PageActionViewGtk::IsVisible() { |
1767 return gtk_widget_get_visible(widget()); | 1760 return gtk_widget_get_visible(widget()); |
1768 } | 1761 } |
1769 | 1762 |
1770 void LocationBarViewGtk::PageActionViewGtk::UpdateVisibility( | 1763 void LocationBarViewGtk::PageActionViewGtk::UpdateVisibility( |
1771 WebContents* contents, const GURL& url) { | 1764 WebContents* contents, const GURL& url) { |
1772 // Save this off so we can pass it back to the extension when the action gets | 1765 // Save this off so we can pass it back to the extension when the action gets |
1773 // executed. See PageActionImageView::OnMousePressed. | 1766 // executed. See PageActionImageView::OnMousePressed. |
1774 current_tab_id_ = contents ? ExtensionTabUtil::GetTabId(contents) : -1; | 1767 current_tab_id_ = contents ? ExtensionTabUtil::GetTabId(contents) : -1; |
1775 current_url_ = url; | 1768 current_url_ = url; |
1776 | 1769 |
1777 bool visible = contents && | 1770 bool visible = contents && |
1778 (preview_enabled_ || page_action_->GetIsVisible(current_tab_id_)); | 1771 (preview_enabled_ || page_action_->GetIsVisible(current_tab_id_)); |
1779 if (visible) { | 1772 if (visible) { |
1780 // Set the tooltip. | 1773 // Set the tooltip. |
1781 gtk_widget_set_tooltip_text(event_box_.get(), | 1774 gtk_widget_set_tooltip_text(event_box_.get(), |
1782 page_action_->GetTitle(current_tab_id_).c_str()); | 1775 page_action_->GetTitle(current_tab_id_).c_str()); |
1783 | 1776 |
1784 // Set the image. | 1777 // Set the image. |
1785 // It can come from three places. In descending order of priority: | 1778 gfx::Image icon = page_action_->GetIcon(current_tab_id_); |
1786 // - The developer can set it dynamically by path or bitmap. It will be in | 1779 if (!icon.IsEmpty()) { |
1787 // page_action_->GetIcon(). | 1780 GdkPixbuf* pixbuf = icon.ToGdkPixbuf(); |
1788 // - The developer can set it dyanmically by index. It will be in | 1781 DCHECK(pixbuf); |
1789 // page_action_->GetIconIndex(). | 1782 gtk_image_set_from_pixbuf(GTK_IMAGE(image_.get()), pixbuf); |
1790 // - It can be set in the manifest by path. It will be in page_action_-> | |
1791 // default_icon_path(). | |
1792 | |
1793 // First look for a dynamically set bitmap. | |
1794 SkBitmap icon = page_action_->GetIcon(current_tab_id_); | |
1795 GdkPixbuf* pixbuf = NULL; | |
1796 if (!icon.isNull()) { | |
1797 if (icon.pixelRef() != last_icon_skbitmap_.pixelRef()) { | |
1798 if (last_icon_pixbuf_) | |
1799 g_object_unref(last_icon_pixbuf_); | |
1800 last_icon_skbitmap_ = icon; | |
1801 last_icon_pixbuf_ = gfx::GdkPixbufFromSkBitmap(icon); | |
1802 } | |
1803 DCHECK(last_icon_pixbuf_); | |
1804 pixbuf = last_icon_pixbuf_; | |
1805 } else { | |
1806 // Otherwise look for a dynamically set index, or fall back to the | |
1807 // default path. | |
1808 int icon_index = page_action_->GetIconIndex(current_tab_id_); | |
1809 std::string icon_path = (icon_index < 0) ? | |
1810 page_action_->default_icon_path() : | |
1811 page_action_->icon_paths()->at(icon_index); | |
1812 if (!icon_path.empty()) { | |
1813 PixbufMap::iterator iter = pixbufs_.find(icon_path); | |
1814 if (iter != pixbufs_.end()) | |
1815 pixbuf = iter->second; | |
1816 } | |
1817 } | |
1818 // The pixbuf might not be loaded yet. | |
1819 if (pixbuf) { | |
1820 const ExtensionAction::IconAnimation* icon_animation = | |
1821 scoped_icon_animation_observer_.icon_animation(); | |
1822 if (icon_animation) { | |
1823 // Draw |pixbuf| with the fade-in |icon_animation_| applied to it. | |
1824 // Use a temporary gfx::Image to do the conversion to/from a SkBitmap. | |
1825 g_object_ref(pixbuf); // don't let gfx::Image take ownership. | |
1826 gfx::Image animated_image( | |
1827 icon_animation->Apply(*gfx::Image(pixbuf).ToSkBitmap())); | |
1828 gtk_image_set_from_pixbuf(GTK_IMAGE(image_.get()), | |
1829 animated_image.ToGdkPixbuf()); | |
1830 } else { | |
1831 gtk_image_set_from_pixbuf(GTK_IMAGE(image_.get()), pixbuf); | |
1832 } | |
1833 } | 1783 } |
1834 } | 1784 } |
1835 | 1785 |
1836 bool old_visible = IsVisible(); | 1786 bool old_visible = IsVisible(); |
1837 if (visible) | 1787 if (visible) |
1838 gtk_widget_show_all(event_box_.get()); | 1788 gtk_widget_show_all(event_box_.get()); |
1839 else | 1789 else |
1840 gtk_widget_hide_all(event_box_.get()); | 1790 gtk_widget_hide_all(event_box_.get()); |
1841 | 1791 |
1842 if (visible != old_visible) { | 1792 if (visible != old_visible) { |
(...skipping 10 matching lines...) Expand all Loading... |
1853 int index) { | 1803 int index) { |
1854 // We loaded icons()->size() icons, plus one extra if the page action had | 1804 // We loaded icons()->size() icons, plus one extra if the page action had |
1855 // a default icon. | 1805 // a default icon. |
1856 int total_icons = static_cast<int>(page_action_->icon_paths()->size()); | 1806 int total_icons = static_cast<int>(page_action_->icon_paths()->size()); |
1857 if (!page_action_->default_icon_path().empty()) | 1807 if (!page_action_->default_icon_path().empty()) |
1858 total_icons++; | 1808 total_icons++; |
1859 DCHECK(index < total_icons); | 1809 DCHECK(index < total_icons); |
1860 | 1810 |
1861 // Map the index of the loaded image back to its name. If we ever get an | 1811 // Map the index of the loaded image back to its name. If we ever get an |
1862 // index greater than the number of icons, it must be the default icon. | 1812 // index greater than the number of icons, it must be the default icon. |
1863 if (!image.IsEmpty()) { | 1813 if (index < static_cast<int>(page_action_->icon_paths()->size())) |
1864 GdkPixbuf* pixbuf = | 1814 page_action_->CacheIcon(page_action_->icon_paths()->at(index), image); |
1865 static_cast<GdkPixbuf*>(g_object_ref(image.ToGdkPixbuf())); | 1815 else |
1866 if (index < static_cast<int>(page_action_->icon_paths()->size())) | 1816 page_action_->CacheIcon(page_action_->default_icon_path(), image); |
1867 pixbufs_[page_action_->icon_paths()->at(index)] = pixbuf; | |
1868 else | |
1869 pixbufs_[page_action_->default_icon_path()] = pixbuf; | |
1870 } | |
1871 | 1817 |
1872 // If we have no owner, that means this class is still being constructed. | 1818 // If we have no owner, that means this class is still being constructed. |
1873 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; | 1819 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; |
1874 if (tab_contents) | 1820 if (tab_contents) |
1875 UpdateVisibility(tab_contents->web_contents(), current_url_); | 1821 UpdateVisibility(tab_contents->web_contents(), current_url_); |
1876 } | 1822 } |
1877 | 1823 |
1878 void LocationBarViewGtk::PageActionViewGtk::TestActivatePageAction() { | 1824 void LocationBarViewGtk::PageActionViewGtk::TestActivatePageAction() { |
1879 GdkEventButton event = {}; | 1825 GdkEventButton event = {}; |
1880 event.button = 1; | 1826 event.button = 1; |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2050 } | 1996 } |
2051 | 1997 |
2052 void LocationBarViewGtk::PageActionViewGtk::InspectPopup( | 1998 void LocationBarViewGtk::PageActionViewGtk::InspectPopup( |
2053 ExtensionAction* action) { | 1999 ExtensionAction* action) { |
2054 ExtensionPopupGtk::Show( | 2000 ExtensionPopupGtk::Show( |
2055 action->GetPopupUrl(current_tab_id_), | 2001 action->GetPopupUrl(current_tab_id_), |
2056 owner_->browser_, | 2002 owner_->browser_, |
2057 event_box_.get(), | 2003 event_box_.get(), |
2058 ExtensionPopupGtk::SHOW_AND_INSPECT); | 2004 ExtensionPopupGtk::SHOW_AND_INSPECT); |
2059 } | 2005 } |
OLD | NEW |