OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_MENU_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 53 |
54 bool operator==(const Id& other) const; | 54 bool operator==(const Id& other) const; |
55 bool operator!=(const Id& other) const; | 55 bool operator!=(const Id& other) const; |
56 bool operator<(const Id& other) const; | 56 bool operator<(const Id& other) const; |
57 | 57 |
58 bool incognito; | 58 bool incognito; |
59 std::string extension_id; | 59 std::string extension_id; |
60 // Only one of uid or string_uid will be defined. | 60 // Only one of uid or string_uid will be defined. |
61 int uid; | 61 int uid; |
62 std::string string_uid; | 62 std::string string_uid; |
| 63 int webview_instance_id; |
63 }; | 64 }; |
64 | 65 |
65 // For context menus, these are the contexts where an item can appear. | 66 // For context menus, these are the contexts where an item can appear. |
66 enum Context { | 67 enum Context { |
67 ALL = 1, | 68 ALL = 1, |
68 PAGE = 2, | 69 PAGE = 2, |
69 SELECTION = 4, | 70 SELECTION = 4, |
70 LINK = 8, | 71 LINK = 8, |
71 EDITABLE = 16, | 72 EDITABLE = 16, |
72 IMAGE = 32, | 73 IMAGE = 32, |
73 VIDEO = 64, | 74 VIDEO = 64, |
74 AUDIO = 128, | 75 AUDIO = 128, |
75 FRAME = 256, | 76 FRAME = 256, |
76 LAUNCHER = 512 | 77 LAUNCHER = 512 |
77 }; | 78 }; |
78 | 79 |
79 // An item can be only one of these types. | 80 // An item can be only one of these types. |
80 enum Type { | 81 enum Type { |
81 NORMAL, | 82 NORMAL, |
82 CHECKBOX, | 83 CHECKBOX, |
83 RADIO, | 84 RADIO, |
84 SEPARATOR | 85 SEPARATOR |
85 }; | 86 }; |
86 | 87 |
| 88 // Key used to identify which extension a menu item belongs to. |
| 89 // |webview_instance_id| = 0 for regular extension items. |
| 90 struct ExtensionKey { |
| 91 std::string extension_id; |
| 92 int webview_instance_id; |
| 93 |
| 94 ExtensionKey(const std::string& extension_id, int webview_instance_id); |
| 95 explicit ExtensionKey(const std::string& extension_id); |
| 96 explicit ExtensionKey(const MenuItem::Id& item_id); |
| 97 |
| 98 bool operator==(const ExtensionKey& other) const; |
| 99 bool operator!=(const ExtensionKey& other) const; |
| 100 bool operator<(const ExtensionKey& other) const; |
| 101 |
| 102 bool empty() const; |
| 103 }; |
| 104 |
87 // A list of Contexts for an item. | 105 // A list of Contexts for an item. |
88 class ContextList { | 106 class ContextList { |
89 public: | 107 public: |
90 ContextList() : value_(0) {} | 108 ContextList() : value_(0) {} |
91 explicit ContextList(Context context) : value_(context) {} | 109 explicit ContextList(Context context) : value_(context) {} |
92 ContextList(const ContextList& other) : value_(other.value_) {} | 110 ContextList(const ContextList& other) : value_(other.value_) {} |
93 | 111 |
94 void operator=(const ContextList& other) { | 112 void operator=(const ContextList& other) { |
95 value_ = other.value_; | 113 value_ = other.value_; |
96 } | 114 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 const std::string& title, | 150 const std::string& title, |
133 bool checked, | 151 bool checked, |
134 bool enabled, | 152 bool enabled, |
135 Type type, | 153 Type type, |
136 const ContextList& contexts); | 154 const ContextList& contexts); |
137 virtual ~MenuItem(); | 155 virtual ~MenuItem(); |
138 | 156 |
139 // Simple accessor methods. | 157 // Simple accessor methods. |
140 bool incognito() const { return id_.incognito; } | 158 bool incognito() const { return id_.incognito; } |
141 const std::string& extension_id() const { return id_.extension_id; } | 159 const std::string& extension_id() const { return id_.extension_id; } |
| 160 // const ExtensionKey extension_key() const { return ExtensionKey(id_); } |
142 const std::string& title() const { return title_; } | 161 const std::string& title() const { return title_; } |
143 const List& children() { return children_; } | 162 const List& children() { return children_; } |
144 const Id& id() const { return id_; } | 163 const Id& id() const { return id_; } |
145 Id* parent_id() const { return parent_id_.get(); } | 164 Id* parent_id() const { return parent_id_.get(); } |
146 int child_count() const { return children_.size(); } | 165 int child_count() const { return children_.size(); } |
147 ContextList contexts() const { return contexts_; } | 166 ContextList contexts() const { return contexts_; } |
148 Type type() const { return type_; } | 167 Type type() const { return type_; } |
149 bool checked() const { return checked_; } | 168 bool checked() const { return checked_; } |
150 bool enabled() const { return enabled_; } | 169 bool enabled() const { return enabled_; } |
151 const URLPatternSet& document_url_patterns() const { | 170 const URLPatternSet& document_url_patterns() const { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 static MenuManager* Get(Profile* profile); | 274 static MenuManager* Get(Profile* profile); |
256 | 275 |
257 // Returns the ids of extensions which have menu items registered. | 276 // Returns the ids of extensions which have menu items registered. |
258 std::set<std::string> ExtensionIds(); | 277 std::set<std::string> ExtensionIds(); |
259 | 278 |
260 // Returns a list of all the *top-level* menu items (added via AddContextItem) | 279 // Returns a list of all the *top-level* menu items (added via AddContextItem) |
261 // for the given extension id, *not* including child items (added via | 280 // for the given extension id, *not* including child items (added via |
262 // AddChildItem); although those can be reached via the top-level items' | 281 // AddChildItem); although those can be reached via the top-level items' |
263 // children. A view can then decide how to display these, including whether to | 282 // children. A view can then decide how to display these, including whether to |
264 // put them into a submenu if there are more than 1. | 283 // put them into a submenu if there are more than 1. |
265 const MenuItem::List* MenuItems(const std::string& extension_id); | 284 const MenuItem::List* MenuItems(const MenuItem::ExtensionKey& extension_key); |
266 | 285 |
267 // Adds a top-level menu item for an extension, requiring the |extension| | 286 // Adds a top-level menu item for an extension, requiring the |extension| |
268 // pointer so it can load the icon for the extension. Takes ownership of | 287 // pointer so it can load the icon for the extension. Takes ownership of |
269 // |item|. Returns a boolean indicating success or failure. | 288 // |item|. Returns a boolean indicating success or failure. |
270 bool AddContextItem(const Extension* extension, MenuItem* item); | 289 bool AddContextItem(const Extension* extension, MenuItem* item); |
271 | 290 |
272 // Add an item as a child of another item which has been previously added, and | 291 // Add an item as a child of another item which has been previously added, and |
273 // takes ownership of |item|. Returns a boolean indicating success or failure. | 292 // takes ownership of |item|. Returns a boolean indicating success or failure. |
274 bool AddChildItem(const MenuItem::Id& parent_id, | 293 bool AddChildItem(const MenuItem::Id& parent_id, |
275 MenuItem* child); | 294 MenuItem* child); |
276 | 295 |
277 // Makes existing item with |child_id| a child of the item with |parent_id|. | 296 // Makes existing item with |child_id| a child of the item with |parent_id|. |
278 // If the child item was already a child of another parent, this will remove | 297 // If the child item was already a child of another parent, this will remove |
279 // it from that parent first. It is an error to try and move an item to be a | 298 // it from that parent first. It is an error to try and move an item to be a |
280 // child of one of its own descendants. It is legal to pass NULL for | 299 // child of one of its own descendants. It is legal to pass NULL for |
281 // |parent_id|, which means the item should be moved to the top-level. | 300 // |parent_id|, which means the item should be moved to the top-level. |
282 bool ChangeParent(const MenuItem::Id& child_id, | 301 bool ChangeParent(const MenuItem::Id& child_id, |
283 const MenuItem::Id* parent_id); | 302 const MenuItem::Id* parent_id); |
284 | 303 |
285 // Removes a context menu item with the given id (whether it is a top-level | 304 // Removes a context menu item with the given id (whether it is a top-level |
286 // item or a child of some other item), returning true if the item was found | 305 // item or a child of some other item), returning true if the item was found |
287 // and removed or false otherwise. | 306 // and removed or false otherwise. |
288 bool RemoveContextMenuItem(const MenuItem::Id& id); | 307 bool RemoveContextMenuItem(const MenuItem::Id& id); |
289 | 308 |
290 // Removes all items for the given extension id. | 309 // Removes all items for the given extension specified by |extension_key|. |
291 void RemoveAllContextItems(const std::string& extension_id); | 310 void RemoveAllContextItems(const MenuItem::ExtensionKey& extension_key); |
292 | 311 |
293 // Returns the item with the given |id| or NULL. | 312 // Returns the item with the given |id| or NULL. |
294 MenuItem* GetItemById(const MenuItem::Id& id) const; | 313 MenuItem* GetItemById(const MenuItem::Id& id) const; |
295 | 314 |
296 // Notify the MenuManager that an item has been updated not through | 315 // Notify the MenuManager that an item has been updated not through |
297 // an explicit call into MenuManager. For example, if an item is | 316 // an explicit call into MenuManager. For example, if an item is |
298 // acquired by a call to GetItemById and changed, then this should be called. | 317 // acquired by a call to GetItemById and changed, then this should be called. |
299 // Returns true if the item was found or false otherwise. | 318 // Returns true if the item was found or false otherwise. |
300 bool ItemUpdated(const MenuItem::Id& id); | 319 bool ItemUpdated(const MenuItem::Id& id); |
301 | 320 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 // Make sure that there is only one radio item selected at once in any run. | 355 // Make sure that there is only one radio item selected at once in any run. |
337 // If there are no radio items selected, then the first item in the run | 356 // If there are no radio items selected, then the first item in the run |
338 // will get selected. If there are multiple radio items selected, then only | 357 // will get selected. If there are multiple radio items selected, then only |
339 // the last one will get selcted. | 358 // the last one will get selcted. |
340 void SanitizeRadioList(const MenuItem::List& item_list); | 359 void SanitizeRadioList(const MenuItem::List& item_list); |
341 | 360 |
342 // Returns true if item is a descendant of an item with id |ancestor_id|. | 361 // Returns true if item is a descendant of an item with id |ancestor_id|. |
343 bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id); | 362 bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id); |
344 | 363 |
345 // We keep items organized by mapping an extension id to a list of items. | 364 // We keep items organized by mapping an extension id to a list of items. |
346 typedef std::map<std::string, MenuItem::List> MenuItemMap; | 365 typedef std::map<MenuItem::ExtensionKey, MenuItem::List> MenuItemMap; |
347 MenuItemMap context_items_; | 366 MenuItemMap context_items_; |
348 | 367 |
349 // This lets us make lookup by id fast. It maps id to MenuItem* for | 368 // This lets us make lookup by id fast. It maps id to MenuItem* for |
350 // all items the menu manager knows about, including all children of top-level | 369 // all items the menu manager knows about, including all children of top-level |
351 // items. | 370 // items. |
352 std::map<MenuItem::Id, MenuItem*> items_by_id_; | 371 std::map<MenuItem::Id, MenuItem*> items_by_id_; |
353 | 372 |
354 content::NotificationRegistrar registrar_; | 373 content::NotificationRegistrar registrar_; |
355 | 374 |
356 ExtensionIconManager icon_manager_; | 375 ExtensionIconManager icon_manager_; |
357 | 376 |
358 Profile* profile_; | 377 Profile* profile_; |
359 | 378 |
360 // Owned by ExtensionSystem. | 379 // Owned by ExtensionSystem. |
361 StateStore* store_; | 380 StateStore* store_; |
362 | 381 |
363 DISALLOW_COPY_AND_ASSIGN(MenuManager); | 382 DISALLOW_COPY_AND_ASSIGN(MenuManager); |
364 }; | 383 }; |
365 | 384 |
366 } // namespace extensions | 385 } // namespace extensions |
367 | 386 |
368 #endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ | 387 #endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
OLD | NEW |