Home | History | Annotate | Download | only in location_bar
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_
      6 #define CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "chrome/browser/extensions/extension_action.h"
     13 #include "chrome/browser/extensions/extension_action_icon_factory.h"
     14 #include "chrome/browser/extensions/extension_context_menu_model.h"
     15 #include "chrome/browser/ui/views/extensions/extension_popup.h"
     16 #include "ui/views/context_menu_controller.h"
     17 #include "ui/views/controls/image_view.h"
     18 #include "ui/views/widget/widget_observer.h"
     19 
     20 class Browser;
     21 class LocationBarView;
     22 
     23 namespace content {
     24 class WebContents;
     25 }
     26 namespace views {
     27 class MenuRunner;
     28 }
     29 
     30 // PageActionImageView is used by the LocationBarView to display the icon for a
     31 // given PageAction and notify the extension when the icon is clicked.
     32 class PageActionImageView : public views::ImageView,
     33                             public ExtensionContextMenuModel::PopupDelegate,
     34                             public views::WidgetObserver,
     35                             public views::ContextMenuController,
     36                             public ExtensionActionIconFactory::Observer,
     37                             public ExtensionAction::IconAnimation::Observer {
     38  public:
     39   PageActionImageView(LocationBarView* owner,
     40                       ExtensionAction* page_action,
     41                       Browser* browser);
     42   virtual ~PageActionImageView();
     43 
     44   ExtensionAction* page_action() { return page_action_; }
     45 
     46   int current_tab_id() { return current_tab_id_; }
     47 
     48   void set_preview_enabled(bool preview_enabled) {
     49     preview_enabled_ = preview_enabled;
     50   }
     51 
     52   // Overridden from views::View:
     53   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
     54   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
     55   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
     56   virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
     57 
     58   // Overridden from ExtensionContextMenuModel::Delegate
     59   virtual void InspectPopup(ExtensionAction* action) OVERRIDE;
     60 
     61   // Overridden from views::WidgetObserver:
     62   virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE;
     63 
     64   // Overridden from views::ContextMenuController.
     65   virtual void ShowContextMenuForView(View* source,
     66                                       const gfx::Point& point,
     67                                       ui::MenuSourceType source_type) OVERRIDE;
     68 
     69   // Overriden from ExtensionActionIconFactory::Observer.
     70   virtual void OnIconUpdated() OVERRIDE;
     71 
     72   // Overridden from ui::AcceleratorTarget:
     73   virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
     74   virtual bool CanHandleAccelerators() const OVERRIDE;
     75 
     76   // Called to notify the PageAction that it should determine whether to be
     77   // visible or hidden. |contents| is the WebContents that is active, |url| is
     78   // the current page URL.
     79   void UpdateVisibility(content::WebContents* contents, const GURL& url);
     80 
     81   // Either notify listeners or show a popup depending on the page action.
     82   void ExecuteAction(ExtensionPopup::ShowAction show_action);
     83 
     84  private:
     85   // Overridden from ExtensionAction::IconAnimation::Observer:
     86   virtual void OnIconChanged() OVERRIDE;
     87 
     88   // Overridden from View.
     89   virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;
     90 
     91   // Shows the popup, with the given URL.
     92   void ShowPopupWithURL(const GURL& popup_url,
     93                         ExtensionPopup::ShowAction show_action);
     94 
     95   // Hides the active popup, if there is one.
     96   void HidePopup();
     97 
     98   // The location bar view that owns us.
     99   LocationBarView* owner_;
    100 
    101   // The PageAction that this view represents. The PageAction is not owned by
    102   // us, it resides in the extension of this particular profile.
    103   ExtensionAction* page_action_;
    104 
    105   // The corresponding browser.
    106   Browser* browser_;
    107 
    108   // The object that will be used to get the page action icon for us.
    109   // It may load the icon asynchronously (in which case the initial icon
    110   // returned by the factory will be transparent), so we have to observe it for
    111   // updates to the icon.
    112   scoped_ptr<ExtensionActionIconFactory> icon_factory_;
    113 
    114   // The tab id we are currently showing the icon for.
    115   int current_tab_id_;
    116 
    117   // The URL we are currently showing the icon for.
    118   GURL current_url_;
    119 
    120   // The string to show for a tooltip;
    121   std::string tooltip_;
    122 
    123   // This is used for post-install visual feedback. The page_action icon is
    124   // briefly shown even if it hasn't been enabled by its extension.
    125   bool preview_enabled_;
    126 
    127   // The current popup and the button it came from.  NULL if no popup.
    128   ExtensionPopup* popup_;
    129 
    130   // The extension command accelerator this page action is listening for (to
    131   // show the popup).
    132   scoped_ptr<ui::Accelerator> page_action_keybinding_;
    133 
    134   // The extension command accelerator this script badge is listening for (to
    135   // show the popup).
    136   scoped_ptr<ui::Accelerator> script_badge_keybinding_;
    137 
    138   scoped_ptr<views::MenuRunner> menu_runner_;
    139 
    140   // Fade-in animation for the icon with observer scoped to this.
    141   ExtensionAction::IconAnimation::ScopedObserver
    142       scoped_icon_animation_observer_;
    143 
    144   DISALLOW_IMPLICIT_CONSTRUCTORS(PageActionImageView);
    145 };
    146 
    147 #endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_
    148