| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/basictypes.h" |
| 14 #include "base/linked_ptr.h" |
| 15 #include "base/stl_util-inl.h" |
| 16 #include "base/string16.h" |
| 17 #include "chrome/common/notification_observer.h" |
| 18 #include "chrome/common/notification_registrar.h" |
| 19 |
| 20 struct ContextMenuParams; |
| 21 |
| 22 class ExtensionMessageService; |
| 23 class Profile; |
| 24 class TabContents; |
| 25 |
| 26 // Represents a menu item added by an extension. |
| 27 class ExtensionMenuItem { |
| 28 public: |
| 29 // A list of owned ExtensionMenuItem's. |
| 30 typedef std::vector<linked_ptr<ExtensionMenuItem> > List; |
| 31 |
| 32 // For context menus, these are the contexts where an item can appear and |
| 33 // potentially be enabled. |
| 34 enum Context { |
| 35 ALL = 1, |
| 36 PAGE = 2, |
| 37 SELECTION = 4, |
| 38 LINK = 8, |
| 39 EDITABLE = 16, |
| 40 IMAGE = 32, |
| 41 VIDEO = 64, |
| 42 AUDIO = 128, |
| 43 }; |
| 44 |
| 45 // An item can be only one of these types. |
| 46 enum Type { |
| 47 NORMAL, |
| 48 CHECKBOX, |
| 49 RADIO, |
| 50 SEPARATOR |
| 51 }; |
| 52 |
| 53 // A list of Contexts for an item (where it should be shown/enabled). |
| 54 class ContextList { |
| 55 public: |
| 56 ContextList() : value_(0) {} |
| 57 explicit ContextList(Context context) : value_(context) {} |
| 58 ContextList(const ContextList& other) : value_(other.value_) {} |
| 59 |
| 60 void operator=(const ContextList& other) { |
| 61 value_ = other.value_; |
| 62 } |
| 63 |
| 64 bool Contains(Context context) const { |
| 65 return (value_ & context) > 0; |
| 66 } |
| 67 |
| 68 void Add(Context context) { |
| 69 value_ |= context; |
| 70 } |
| 71 |
| 72 private: |
| 73 uint32 value_; // A bitmask of Context values. |
| 74 }; |
| 75 |
| 76 ExtensionMenuItem(const std::string& extension_id, std::string title, |
| 77 bool checked, Type type, const ContextList& contexts, |
| 78 const ContextList& enabled_contexts); |
| 79 virtual ~ExtensionMenuItem(); |
| 80 |
| 81 // Simple accessor methods. |
| 82 const std::string& extension_id() const { return extension_id_; } |
| 83 const std::string& title() const { return title_; } |
| 84 int id() const { return id_; } |
| 85 int parent_id() const { return parent_id_; } |
| 86 int child_count() const { return children_.size(); } |
| 87 ContextList contexts() const { return contexts_; } |
| 88 ContextList enabled_contexts() const { return enabled_contexts_; } |
| 89 Type type() const { return type_; } |
| 90 bool checked() const { return checked_; } |
| 91 |
| 92 // Returns the child at the given index, or NULL. |
| 93 ExtensionMenuItem* ChildAt(int index) const; |
| 94 |
| 95 // Returns the title with any instances of %s replaced by |selection|. |
| 96 string16 TitleWithReplacement(const string16& selection) const; |
| 97 |
| 98 protected: |
| 99 friend class ExtensionMenuManager; |
| 100 |
| 101 // This is protected because the ExtensionMenuManager is in charge of |
| 102 // assigning unique ids. |
| 103 virtual void set_id(int id) { |
| 104 id_ = id; |
| 105 } |
| 106 |
| 107 // Provides direct access to the children of this item. |
| 108 List* children() { return &children_; } |
| 109 |
| 110 // Set the checked state to |checked|. Returns true if successful. |
| 111 bool SetChecked(bool checked); |
| 112 |
| 113 // Takes ownership of |item| and sets its parent_id_. |
| 114 void AddChild(ExtensionMenuItem* item); |
| 115 |
| 116 // Removes child menu item with the given id, returning true if the item was |
| 117 // found and removed, or false otherwise. |
| 118 bool RemoveChild(int child_id); |
| 119 |
| 120 private: |
| 121 // The extension that added this item. |
| 122 std::string extension_id_; |
| 123 |
| 124 // What gets shown in the menu for this item. |
| 125 std::string title_; |
| 126 |
| 127 // A unique id for this item. The value 0 means "not yet assigned". |
| 128 int id_; |
| 129 |
| 130 Type type_; |
| 131 |
| 132 // This should only be true for items of type CHECKBOX or RADIO. |
| 133 bool checked_; |
| 134 |
| 135 // In what contexts should the item be shown? |
| 136 ContextList contexts_; |
| 137 |
| 138 // In what contexts should the item be enabled (i.e. not greyed out). This |
| 139 // should be a subset of contexts_. |
| 140 ContextList enabled_contexts_; |
| 141 |
| 142 // If this item is a child of another item, the unique id of its parent. If |
| 143 // this is a top-level item with no parent, this will be 0. |
| 144 int parent_id_; |
| 145 |
| 146 // Any children this item may have. |
| 147 List children_; |
| 148 |
| 149 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuItem); |
| 150 }; |
| 151 |
| 152 // This class keeps track of menu items added by extensions. |
| 153 class ExtensionMenuManager : public NotificationObserver { |
| 154 public: |
| 155 ExtensionMenuManager(); |
| 156 virtual ~ExtensionMenuManager(); |
| 157 |
| 158 // Returns the ids of extensions which have menu items registered. |
| 159 std::set<std::string> ExtensionIds(); |
| 160 |
| 161 // Returns a list of all the *top-level* menu items (added via AddContextItem) |
| 162 // for the given extension id, *not* including child items (added via |
| 163 // AddChildItem); although those can be reached via the top-level items' |
| 164 // ChildAt function. A view can then decide how to display these, including |
| 165 // whether to put them into a submenu if there are more than 1. |
| 166 std::vector<const ExtensionMenuItem*> MenuItems( |
| 167 const std::string& extension_id); |
| 168 |
| 169 // Takes ownership of |item|. Returns the id assigned to the item. Has the |
| 170 // side-effect of incrementing the next_item_id_ member. |
| 171 int AddContextItem(ExtensionMenuItem* item); |
| 172 |
| 173 // Add an item as a child of another item which has been previously added, and |
| 174 // takes ownership of |item|. Returns the id assigned to the item, or 0 on |
| 175 // error. Has the side-effect of incrementing the next_item_id_ member. |
| 176 int AddChildItem(int parent_id, ExtensionMenuItem* child); |
| 177 |
| 178 // Removes a context menu item with the given id (whether it is a top-level |
| 179 // item or a child of some other item), returning true if the item was found |
| 180 // and removed or false otherwise. |
| 181 bool RemoveContextMenuItem(int id); |
| 182 |
| 183 // Returns the item with the given |id| or NULL. |
| 184 ExtensionMenuItem* GetItemById(int id); |
| 185 |
| 186 // Called when a menu item is clicked on by the user. |
| 187 void ExecuteCommand(Profile* profile, TabContents* tab_contents, |
| 188 const ContextMenuParams& params, |
| 189 int menuItemId); |
| 190 |
| 191 // Implements the NotificationObserver interface. |
| 192 virtual void Observe(NotificationType type, const NotificationSource& source, |
| 193 const NotificationDetails& details); |
| 194 |
| 195 private: |
| 196 // This is a helper function which takes care of de-selecting any other radio |
| 197 // items in the same group (i.e. that are adjacent in the list). |
| 198 void RadioItemSelected(ExtensionMenuItem* item); |
| 199 |
| 200 // If an item with |id| is found, |item| will be set to point to it and |
| 201 // |index| will be set to its index within the containing list. |
| 202 void GetItemAndIndex(int id, ExtensionMenuItem** item, size_t* index); |
| 203 |
| 204 // We keep items organized by mapping an extension id to a list of items. |
| 205 typedef std::map<std::string, ExtensionMenuItem::List> MenuItemMap; |
| 206 MenuItemMap context_items_; |
| 207 |
| 208 // This lets us make lookup by id fast. It maps id to ExtensionMenuItem* for |
| 209 // all items the menu manager knows about, including all children of top-level |
| 210 // items. |
| 211 std::map<int, ExtensionMenuItem*> items_by_id_; |
| 212 |
| 213 // The id we will assign to the next item that gets added. |
| 214 int next_item_id_; |
| 215 |
| 216 NotificationRegistrar registrar_; |
| 217 |
| 218 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuManager); |
| 219 }; |
| 220 |
| 221 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ |
| OLD | NEW |