OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/linked_ptr.h" | 14 #include "base/linked_ptr.h" |
15 #include "base/stl_util-inl.h" | 15 #include "base/stl_util-inl.h" |
16 #include "base/string16.h" | 16 #include "base/string16.h" |
| 17 #include "chrome/browser/extensions/image_loading_tracker.h" |
17 #include "chrome/common/notification_observer.h" | 18 #include "chrome/common/notification_observer.h" |
18 #include "chrome/common/notification_registrar.h" | 19 #include "chrome/common/notification_registrar.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" |
19 | 21 |
20 struct ContextMenuParams; | 22 struct ContextMenuParams; |
21 | 23 |
| 24 class Extension; |
22 class ExtensionMessageService; | 25 class ExtensionMessageService; |
23 class Profile; | 26 class Profile; |
24 class TabContents; | 27 class TabContents; |
25 | 28 |
26 // Represents a menu item added by an extension. | 29 // Represents a menu item added by an extension. |
27 class ExtensionMenuItem { | 30 class ExtensionMenuItem { |
28 public: | 31 public: |
29 // A list of ExtensionMenuItem's. | 32 // A list of ExtensionMenuItem's. |
30 typedef std::vector<ExtensionMenuItem*> List; | 33 typedef std::vector<ExtensionMenuItem*> List; |
31 | 34 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 // this is a top-level item with no parent, this will be 0. | 163 // this is a top-level item with no parent, this will be 0. |
161 int parent_id_; | 164 int parent_id_; |
162 | 165 |
163 // Any children this item may have. | 166 // Any children this item may have. |
164 List children_; | 167 List children_; |
165 | 168 |
166 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuItem); | 169 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuItem); |
167 }; | 170 }; |
168 | 171 |
169 // This class keeps track of menu items added by extensions. | 172 // This class keeps track of menu items added by extensions. |
170 class ExtensionMenuManager : public NotificationObserver { | 173 class ExtensionMenuManager |
| 174 : public ImageLoadingTracker::Observer, |
| 175 public NotificationObserver { |
171 public: | 176 public: |
172 ExtensionMenuManager(); | 177 ExtensionMenuManager(); |
173 virtual ~ExtensionMenuManager(); | 178 virtual ~ExtensionMenuManager(); |
174 | 179 |
175 // Returns the ids of extensions which have menu items registered. | 180 // Returns the ids of extensions which have menu items registered. |
176 std::set<std::string> ExtensionIds(); | 181 std::set<std::string> ExtensionIds(); |
177 | 182 |
178 // Returns a list of all the *top-level* menu items (added via AddContextItem) | 183 // Returns a list of all the *top-level* menu items (added via AddContextItem) |
179 // for the given extension id, *not* including child items (added via | 184 // for the given extension id, *not* including child items (added via |
180 // AddChildItem); although those can be reached via the top-level items' | 185 // AddChildItem); although those can be reached via the top-level items' |
181 // children. A view can then decide how to display these, including whether to | 186 // children. A view can then decide how to display these, including whether to |
182 // put them into a submenu if there are more than 1. | 187 // put them into a submenu if there are more than 1. |
183 const ExtensionMenuItem::List* MenuItems(const std::string& extension_id); | 188 const ExtensionMenuItem::List* MenuItems(const std::string& extension_id); |
184 | 189 |
185 // Takes ownership of |item|. Returns the id assigned to the item. Has the | 190 // Adds a top-level menu item for an extension, requiring the |extension| |
186 // side-effect of incrementing the next_item_id_ member. | 191 // pointer so it can load the icon for the extension. Takes ownership of |
187 int AddContextItem(ExtensionMenuItem* item); | 192 // |item|. Returns the id assigned to the item. Has the side-effect of |
| 193 // incrementing the next_item_id_ member. |
| 194 int AddContextItem(Extension* extension, ExtensionMenuItem* item); |
188 | 195 |
189 // Add an item as a child of another item which has been previously added, and | 196 // Add an item as a child of another item which has been previously added, and |
190 // takes ownership of |item|. Returns the id assigned to the item, or 0 on | 197 // takes ownership of |item|. Returns the id assigned to the item, or 0 on |
191 // error. Has the side-effect of incrementing the next_item_id_ member. | 198 // error. Has the side-effect of incrementing the next_item_id_ member. |
192 int AddChildItem(int parent_id, ExtensionMenuItem* child); | 199 int AddChildItem(int parent_id, ExtensionMenuItem* child); |
193 | 200 |
194 // Makes existing item with |child_id| a child of the item with |parent_id|. | 201 // Makes existing item with |child_id| a child of the item with |parent_id|. |
195 // If the child item was already a child of another parent, this will remove | 202 // If the child item was already a child of another parent, this will remove |
196 // it from that parent first. It is an error to try and move an item to be a | 203 // it from that parent first. It is an error to try and move an item to be a |
197 // child of one of its own descendants. | 204 // child of one of its own descendants. |
(...skipping 12 matching lines...) Expand all Loading... |
210 | 217 |
211 // Called when a menu item is clicked on by the user. | 218 // Called when a menu item is clicked on by the user. |
212 void ExecuteCommand(Profile* profile, TabContents* tab_contents, | 219 void ExecuteCommand(Profile* profile, TabContents* tab_contents, |
213 const ContextMenuParams& params, | 220 const ContextMenuParams& params, |
214 int menuItemId); | 221 int menuItemId); |
215 | 222 |
216 // Implements the NotificationObserver interface. | 223 // Implements the NotificationObserver interface. |
217 virtual void Observe(NotificationType type, const NotificationSource& source, | 224 virtual void Observe(NotificationType type, const NotificationSource& source, |
218 const NotificationDetails& details); | 225 const NotificationDetails& details); |
219 | 226 |
| 227 // This returns a bitmap of width/height kFavIconSize, loaded either from an |
| 228 // entry specified in the extension's 'icon' section of the manifest, or a |
| 229 // default extension icon. |
| 230 const SkBitmap& GetIconForExtension(const std::string& extension_id); |
| 231 |
| 232 // Implements the ImageLoadingTracker::Observer interface. |
| 233 virtual void OnImageLoaded(SkBitmap* image, ExtensionResource resource, |
| 234 int index); |
| 235 |
220 private: | 236 private: |
221 // This is a helper function which takes care of de-selecting any other radio | 237 // This is a helper function which takes care of de-selecting any other radio |
222 // items in the same group (i.e. that are adjacent in the list). | 238 // items in the same group (i.e. that are adjacent in the list). |
223 void RadioItemSelected(ExtensionMenuItem* item); | 239 void RadioItemSelected(ExtensionMenuItem* item); |
224 | 240 |
225 // Returns true if item is a descendant of an item with id |ancestor_id|. | 241 // Returns true if item is a descendant of an item with id |ancestor_id|. |
226 bool DescendantOf(ExtensionMenuItem* item, int ancestor_id); | 242 bool DescendantOf(ExtensionMenuItem* item, int ancestor_id); |
227 | 243 |
| 244 // Makes sure we've done one-time initialization of the default extension icon |
| 245 // default_icon_. |
| 246 void EnsureDefaultIcon(); |
| 247 |
| 248 // Helper function to return a copy of |src| scaled to kFavIconSize. |
| 249 SkBitmap ScaleToFavIconSize(const SkBitmap& src); |
| 250 |
228 // We keep items organized by mapping an extension id to a list of items. | 251 // We keep items organized by mapping an extension id to a list of items. |
229 typedef std::map<std::string, ExtensionMenuItem::List> MenuItemMap; | 252 typedef std::map<std::string, ExtensionMenuItem::List> MenuItemMap; |
230 MenuItemMap context_items_; | 253 MenuItemMap context_items_; |
231 | 254 |
232 // This lets us make lookup by id fast. It maps id to ExtensionMenuItem* for | 255 // This lets us make lookup by id fast. It maps id to ExtensionMenuItem* for |
233 // all items the menu manager knows about, including all children of top-level | 256 // all items the menu manager knows about, including all children of top-level |
234 // items. | 257 // items. |
235 std::map<int, ExtensionMenuItem*> items_by_id_; | 258 std::map<int, ExtensionMenuItem*> items_by_id_; |
236 | 259 |
237 // The id we will assign to the next item that gets added. | 260 // The id we will assign to the next item that gets added. |
238 int next_item_id_; | 261 int next_item_id_; |
239 | 262 |
240 NotificationRegistrar registrar_; | 263 NotificationRegistrar registrar_; |
241 | 264 |
| 265 // Used for loading extension icons. |
| 266 ImageLoadingTracker image_tracker_; |
| 267 |
| 268 // Maps extension id to an SkBitmap with the icon for that extension. |
| 269 std::map<std::string, SkBitmap> extension_icons_; |
| 270 |
| 271 // The default icon we'll use if an extension doesn't have one. |
| 272 SkBitmap default_icon_; |
| 273 |
242 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuManager); | 274 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuManager); |
243 }; | 275 }; |
244 | 276 |
245 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ | 277 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ |
OLD | NEW |