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