page_action_decoration.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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;
16class Browser;
17class LocationBarViewMac;
18
19namespace content {
20class WebContents;
21}
22
23namespace extensions {
24class Extension;
25}
26
27// PageActionDecoration is used to display the icon for a given Page
28// Action and notify the extension when the icon is clicked.
29
30class PageActionDecoration : public ImageDecoration,
31                             public ExtensionActionIconFactory::Observer,
32                             public content::NotificationObserver {
33 public:
34  PageActionDecoration(LocationBarViewMac* owner,
35                       Browser* browser,
36                       ExtensionAction* page_action);
37  virtual ~PageActionDecoration();
38
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_; }
43
44  // Overridden from |ExtensionActionIconFactory::Observer|.
45  virtual void OnIconUpdated() OVERRIDE;
46
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);
51
52  // Sets the tooltip for this Page Action image.
53  void SetToolTip(NSString* tooltip);
54  void SetToolTip(std::string tooltip);
55
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;
63
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
66  // a popup was shown.
67  bool ActivatePageAction(bool grant_active_tab);
68
69 private:
70  // Activate the page action in the given |frame|.
71  bool ActivatePageAction(NSRect frame, bool grant_active_tab);
72
73  // Show the popup in the frame, with the given URL.
74  void ShowPopup(const NSRect& frame, const GURL& popup_url);
75
76  // Returns the extension associated with the page action.
77  const extensions::Extension* GetExtension();
78
79  // Overridden from NotificationObserver:
80  virtual void Observe(int type,
81                       const content::NotificationSource& source,
82                       const content::NotificationDetails& details) OVERRIDE;
83
84  // The location bar view that owns us.
85  LocationBarViewMac* owner_;
86
87  // The current browser (not owned by us).
88  Browser* browser_;
89
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
92  // profile.
93  ExtensionAction* page_action_;
94
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_;
100
101  // The tab id we are currently showing the icon for.
102  int current_tab_id_;
103
104  // The URL we are currently showing the icon for.
105  GURL current_url_;
106
107  // The string to show for a tooltip.
108  base::scoped_nsobject<NSString> tooltip_;
109
110  // The context menu controller for the Page Action.
111  base::scoped_nsobject<
112      ExtensionActionContextMenuController> contextMenuController_;
113
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
116  // extension.
117  bool preview_enabled_;
118
119  // Used to register for notifications received by
120  // NotificationObserver.
121  content::NotificationRegistrar registrar_;
122
123  DISALLOW_COPY_AND_ASSIGN(PageActionDecoration);
124};
125
126#endif  // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_PAGE_ACTION_DECORATION_H_
127