1// Copyright (c) 2011 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#pragma once
8
9#include <map>
10#include <string>
11
12#include "base/memory/scoped_ptr.h"
13#include "chrome/browser/extensions/image_loading_tracker.h"
14#include "chrome/browser/extensions/extension_context_menu_model.h"
15#include "chrome/browser/ui/views/extensions/extension_popup.h"
16#include "views/controls/image_view.h"
17
18class LocationBarView;
19namespace views {
20class Menu2;
21};
22
23// PageActionImageView is used by the LocationBarView to display the icon for a
24// given PageAction and notify the extension when the icon is clicked.
25class PageActionImageView : public views::ImageView,
26                            public ImageLoadingTracker::Observer,
27                            public ExtensionContextMenuModel::PopupDelegate,
28                            public ExtensionPopup::Observer {
29 public:
30  PageActionImageView(LocationBarView* owner,
31                      Profile* profile,
32                      ExtensionAction* page_action);
33  virtual ~PageActionImageView();
34
35  ExtensionAction* page_action() { return page_action_; }
36
37  int current_tab_id() { return current_tab_id_; }
38
39  void set_preview_enabled(bool preview_enabled) {
40    preview_enabled_ = preview_enabled;
41  }
42
43  // Overridden from view.
44  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
45  virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE;
46  virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE;
47  virtual bool OnKeyPressed(const views::KeyEvent& event) OVERRIDE;
48  virtual void ShowContextMenu(const gfx::Point& p,
49                               bool is_mouse_gesture) OVERRIDE;
50
51  // Overridden from ImageLoadingTracker.
52  virtual void OnImageLoaded(SkBitmap* image,
53                             const ExtensionResource& resource,
54                             int index) OVERRIDE;
55
56  // Overridden from ExtensionContextMenuModelModel::Delegate
57  virtual void InspectPopup(ExtensionAction* action) OVERRIDE;
58
59  // Overridden from ExtensionPopup::Observer
60  virtual void ExtensionPopupIsClosing(ExtensionPopup* popup) OVERRIDE;
61
62  // Called to notify the PageAction that it should determine whether to be
63  // visible or hidden. |contents| is the TabContents that is active, |url| is
64  // the current page URL.
65  void UpdateVisibility(TabContents* contents, const GURL& url);
66
67  // Either notify listeners or show a popup depending on the page action.
68  void ExecuteAction(int button, bool inspect_with_devtools);
69
70 private:
71  // Hides the active popup, if there is one.
72  void HidePopup();
73
74  // The location bar view that owns us.
75  LocationBarView* owner_;
76
77  // The current profile (not owned by us).
78  Profile* profile_;
79
80  // The PageAction that this view represents. The PageAction is not owned by
81  // us, it resides in the extension of this particular profile.
82  ExtensionAction* page_action_;
83
84  // A cache of bitmaps the page actions might need to show, mapped by path.
85  typedef std::map<std::string, SkBitmap> PageActionMap;
86  PageActionMap page_action_icons_;
87
88  // The context menu for this page action.
89  scoped_refptr<ExtensionContextMenuModel> context_menu_contents_;
90  scoped_ptr<views::Menu2> context_menu_menu_;
91
92  // The object that is waiting for the image loading to complete
93  // asynchronously.
94  ImageLoadingTracker tracker_;
95
96  // The tab id we are currently showing the icon for.
97  int current_tab_id_;
98
99  // The URL we are currently showing the icon for.
100  GURL current_url_;
101
102  // The string to show for a tooltip;
103  std::string tooltip_;
104
105  // This is used for post-install visual feedback. The page_action icon is
106  // briefly shown even if it hasn't been enabled by its extension.
107  bool preview_enabled_;
108
109  // The current popup and the button it came from.  NULL if no popup.
110  ExtensionPopup* popup_;
111
112  DISALLOW_IMPLICIT_CONSTRUCTORS(PageActionImageView);
113};
114
115#endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_
116