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_COCOA_LOCATION_BAR_PAGE_ACTION_DECORATION_H_
      6 #define CHROME_BROWSER_UI_COCOA_LOCATION_BAR_PAGE_ACTION_DECORATION_H_
      7 
      8 #include "chrome/browser/extensions/extension_action.h"
      9 #include "chrome/browser/extensions/extension_action_icon_factory.h"
     10 #import "chrome/browser/ui/cocoa/location_bar/image_decoration.h"
     11 #include "content/public/browser/notification_observer.h"
     12 #include "content/public/browser/notification_registrar.h"
     13 #include "url/gurl.h"
     14 
     15 @class ExtensionActionContextMenuController;
     16 class Browser;
     17 class LocationBarViewMac;
     18 
     19 namespace content {
     20 class WebContents;
     21 }
     22 
     23 // PageActionDecoration is used to display the icon for a given Page
     24 // Action and notify the extension when the icon is clicked.
     25 
     26 class PageActionDecoration : public ImageDecoration,
     27                              public ExtensionActionIconFactory::Observer,
     28                              public content::NotificationObserver {
     29  public:
     30   PageActionDecoration(LocationBarViewMac* owner,
     31                        Browser* browser,
     32                        ExtensionAction* page_action);
     33   virtual ~PageActionDecoration();
     34 
     35   ExtensionAction* page_action() { return page_action_; }
     36   int current_tab_id() { return current_tab_id_; }
     37   void set_preview_enabled(bool enabled) { preview_enabled_ = enabled; }
     38   bool preview_enabled() const { return preview_enabled_; }
     39 
     40   // Overridden from |ExtensionActionIconFactory::Observer|.
     41   virtual void OnIconUpdated() OVERRIDE;
     42 
     43   // Called to notify the Page Action that it should determine whether
     44   // to be visible or hidden. |contents| is the WebContents that is
     45   // active, |url| is the current page URL.
     46   void UpdateVisibility(content::WebContents* contents, const GURL& url);
     47 
     48   // Sets the tooltip for this Page Action image.
     49   void SetToolTip(NSString* tooltip);
     50   void SetToolTip(std::string tooltip);
     51 
     52   // Overridden from |LocationBarDecoration|
     53   virtual CGFloat GetWidthForSpace(CGFloat width) OVERRIDE;
     54   virtual bool AcceptsMousePress() OVERRIDE;
     55   virtual bool OnMousePressed(NSRect frame, NSPoint location) OVERRIDE;
     56   virtual NSString* GetToolTip() OVERRIDE;
     57   virtual NSMenu* GetMenu() OVERRIDE;
     58   virtual NSPoint GetBubblePointInFrame(NSRect frame) OVERRIDE;
     59 
     60   // Activate the page action in its default frame.
     61   void ActivatePageAction();
     62 
     63  private:
     64   // Activate the page action in the given |frame|.
     65   bool ActivatePageAction(NSRect frame);
     66 
     67   // Show the popup in the frame, with the given URL.
     68   void ShowPopup(const NSRect& frame, const GURL& popup_url);
     69 
     70   // Overridden from NotificationObserver:
     71   virtual void Observe(int type,
     72                        const content::NotificationSource& source,
     73                        const content::NotificationDetails& details) OVERRIDE;
     74 
     75   // The location bar view that owns us.
     76   LocationBarViewMac* owner_;
     77 
     78   // The current browser (not owned by us).
     79   Browser* browser_;
     80 
     81   // The Page Action that this view represents. The Page Action is not
     82   // owned by us, it resides in the extension of this particular
     83   // profile.
     84   ExtensionAction* page_action_;
     85 
     86 
     87   // The object that will be used to get the page action icon for us.
     88   // It may load the icon asynchronously (in which case the initial icon
     89   // returned by the factory will be transparent), so we have to observe it for
     90   // updates to the icon.
     91   scoped_ptr<ExtensionActionIconFactory> icon_factory_;
     92 
     93   // The tab id we are currently showing the icon for.
     94   int current_tab_id_;
     95 
     96   // The URL we are currently showing the icon for.
     97   GURL current_url_;
     98 
     99   // The string to show for a tooltip.
    100   base::scoped_nsobject<NSString> tooltip_;
    101 
    102   // The context menu controller for the Page Action.
    103   base::scoped_nsobject<
    104       ExtensionActionContextMenuController> contextMenuController_;
    105 
    106   // This is used for post-install visual feedback. The page_action
    107   // icon is briefly shown even if it hasn't been enabled by its
    108   // extension.
    109   bool preview_enabled_;
    110 
    111   // Used to register for notifications received by
    112   // NotificationObserver.
    113   content::NotificationRegistrar registrar_;
    114 
    115   DISALLOW_COPY_AND_ASSIGN(PageActionDecoration);
    116 };
    117 
    118 #endif  // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_PAGE_ACTION_DECORATION_H_
    119