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.
5 #ifndef CHROME_BROWSER_UI_COCOA_LOCATION_BAR_PAGE_ACTION_DECORATION_H_
6 #define CHROME_BROWSER_UI_COCOA_LOCATION_BAR_PAGE_ACTION_DECORATION_H_
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"
15 @class ExtensionActionContextMenuController;
17 class LocationBarViewMac;
23 namespace extensions {
27 // PageActionDecoration is used to display the icon for a given Page
28 // Action and notify the extension when the icon is clicked.
30 class PageActionDecoration : public ImageDecoration,
31 public ExtensionActionIconFactory::Observer,
32 public content::NotificationObserver {
34 PageActionDecoration(LocationBarViewMac* owner,
36 ExtensionAction* page_action);
37 virtual ~PageActionDecoration();
39 ExtensionAction* page_action() { return page_action_; }
40 int current_tab_id() { return current_tab_id_; }
41 void set_preview_enabled(bool enabled) { preview_enabled_ = enabled; }
42 bool preview_enabled() const { return preview_enabled_; }
44 // Overridden from |ExtensionActionIconFactory::Observer|.
45 virtual void OnIconUpdated() OVERRIDE;
47 // Called to notify the Page Action that it should determine whether
48 // to be visible or hidden. |contents| is the WebContents that is
49 // active, |url| is the current page URL.
50 void UpdateVisibility(content::WebContents* contents, const GURL& url);
52 // Sets the tooltip for this Page Action image.
53 void SetToolTip(NSString* tooltip);
54 void SetToolTip(std::string tooltip);
56 // Overridden from |LocationBarDecoration|
57 virtual CGFloat GetWidthForSpace(CGFloat width) OVERRIDE;
58 virtual bool AcceptsMousePress() OVERRIDE;
59 virtual bool OnMousePressed(NSRect frame, NSPoint location) OVERRIDE;
60 virtual NSString* GetToolTip() OVERRIDE;
61 virtual NSMenu* GetMenu() OVERRIDE;
62 virtual NSPoint GetBubblePointInFrame(NSRect frame) OVERRIDE;
64 // Activates the page action in its default frame, and, if |grant_active_tab|
65 // is true, grants active tab permission to the extension. Returns true if
67 bool ActivatePageAction(bool grant_active_tab);
70 // Activate the page action in the given |frame|.
71 bool ActivatePageAction(NSRect frame, bool grant_active_tab);
73 // Show the popup in the frame, with the given URL.
74 void ShowPopup(const NSRect& frame, const GURL& popup_url);
76 // Returns the extension associated with the page action.
77 const extensions::Extension* GetExtension();
79 // Overridden from NotificationObserver:
80 virtual void Observe(int type,
81 const content::NotificationSource& source,
82 const content::NotificationDetails& details) OVERRIDE;
84 // The location bar view that owns us.
85 LocationBarViewMac* owner_;
87 // The current browser (not owned by us).
90 // The Page Action that this view represents. The Page Action is not
91 // owned by us, it resides in the extension of this particular
93 ExtensionAction* page_action_;
95 // The object that will be used to get the page action icon for us.
96 // It may load the icon asynchronously (in which case the initial icon
97 // returned by the factory will be transparent), so we have to observe it for
98 // updates to the icon.
99 scoped_ptr<ExtensionActionIconFactory> icon_factory_;
101 // The tab id we are currently showing the icon for.
104 // The URL we are currently showing the icon for.
107 // The string to show for a tooltip.
108 base::scoped_nsobject<NSString> tooltip_;
110 // The context menu controller for the Page Action.
111 base::scoped_nsobject<
112 ExtensionActionContextMenuController> contextMenuController_;
114 // This is used for post-install visual feedback. The page_action
115 // icon is briefly shown even if it hasn't been enabled by its
117 bool preview_enabled_;
119 // Used to register for notifications received by
120 // NotificationObserver.
121 content::NotificationRegistrar registrar_;
123 DISALLOW_COPY_AND_ASSIGN(PageActionDecoration);
126 #endif // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_PAGE_ACTION_DECORATION_H_