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 24 matching lines...) Expand all Loading... |
35 namespace extensions { | 35 namespace extensions { |
36 class Extension; | 36 class Extension; |
37 class StateStore; | 37 class StateStore; |
38 | 38 |
39 // Represents a menu item added by an extension. | 39 // Represents a menu item added by an extension. |
40 class MenuItem { | 40 class MenuItem { |
41 public: | 41 public: |
42 // A list of MenuItems. | 42 // A list of MenuItems. |
43 typedef std::vector<MenuItem*> List; | 43 typedef std::vector<MenuItem*> List; |
44 | 44 |
| 45 // Key used to identify which extension a menu item belongs to. |
| 46 // A menu item can also belong to a <webview> inside an extension, |
| 47 // only in that case |webview_instance_id| would be |
| 48 // non-zero (i.e. != guestview::kInstanceIDNone). |
| 49 struct ExtensionKey { |
| 50 std::string extension_id; |
| 51 int webview_instance_id; |
| 52 |
| 53 ExtensionKey(); |
| 54 ExtensionKey(const std::string& extension_id, int webview_instance_id); |
| 55 explicit ExtensionKey(const std::string& extension_id); |
| 56 |
| 57 bool operator==(const ExtensionKey& other) const; |
| 58 bool operator!=(const ExtensionKey& other) const; |
| 59 bool operator<(const ExtensionKey& other) const; |
| 60 |
| 61 bool empty() const; |
| 62 }; |
| 63 |
45 // An Id uniquely identifies a context menu item registered by an extension. | 64 // An Id uniquely identifies a context menu item registered by an extension. |
46 struct Id { | 65 struct Id { |
47 Id(); | 66 Id(); |
48 // Since the unique ID (uid or string_uid) is parsed from API arguments, | 67 // Since the unique ID (uid or string_uid) is parsed from API arguments, |
49 // the normal usage is to set the uid or string_uid immediately after | 68 // the normal usage is to set the uid or string_uid immediately after |
50 // construction. | 69 // construction. |
51 Id(bool incognito, const std::string& extension_id); | 70 Id(bool incognito, const ExtensionKey& extension_key); |
52 ~Id(); | 71 ~Id(); |
53 | 72 |
54 bool operator==(const Id& other) const; | 73 bool operator==(const Id& other) const; |
55 bool operator!=(const Id& other) const; | 74 bool operator!=(const Id& other) const; |
56 bool operator<(const Id& other) const; | 75 bool operator<(const Id& other) const; |
57 | 76 |
58 bool incognito; | 77 bool incognito; |
59 std::string extension_id; | 78 ExtensionKey extension_key; |
60 // Only one of uid or string_uid will be defined. | 79 // Only one of uid or string_uid will be defined. |
61 int uid; | 80 int uid; |
62 std::string string_uid; | 81 std::string string_uid; |
63 }; | 82 }; |
64 | 83 |
65 // For context menus, these are the contexts where an item can appear. | 84 // For context menus, these are the contexts where an item can appear. |
66 enum Context { | 85 enum Context { |
67 ALL = 1, | 86 ALL = 1, |
68 PAGE = 2, | 87 PAGE = 2, |
69 SELECTION = 4, | 88 SELECTION = 4, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 MenuItem(const Id& id, | 150 MenuItem(const Id& id, |
132 const std::string& title, | 151 const std::string& title, |
133 bool checked, | 152 bool checked, |
134 bool enabled, | 153 bool enabled, |
135 Type type, | 154 Type type, |
136 const ContextList& contexts); | 155 const ContextList& contexts); |
137 virtual ~MenuItem(); | 156 virtual ~MenuItem(); |
138 | 157 |
139 // Simple accessor methods. | 158 // Simple accessor methods. |
140 bool incognito() const { return id_.incognito; } | 159 bool incognito() const { return id_.incognito; } |
141 const std::string& extension_id() const { return id_.extension_id; } | 160 const std::string& extension_id() const { |
| 161 return id_.extension_key.extension_id; |
| 162 } |
142 const std::string& title() const { return title_; } | 163 const std::string& title() const { return title_; } |
143 const List& children() { return children_; } | 164 const List& children() { return children_; } |
144 const Id& id() const { return id_; } | 165 const Id& id() const { return id_; } |
145 Id* parent_id() const { return parent_id_.get(); } | 166 Id* parent_id() const { return parent_id_.get(); } |
146 int child_count() const { return children_.size(); } | 167 int child_count() const { return children_.size(); } |
147 ContextList contexts() const { return contexts_; } | 168 ContextList contexts() const { return contexts_; } |
148 Type type() const { return type_; } | 169 Type type() const { return type_; } |
149 bool checked() const { return checked_; } | 170 bool checked() const { return checked_; } |
150 bool enabled() const { return enabled_; } | 171 bool enabled() const { return enabled_; } |
151 const URLPatternSet& document_url_patterns() const { | 172 const URLPatternSet& document_url_patterns() const { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 class MenuManager : public content::NotificationObserver, | 268 class MenuManager : public content::NotificationObserver, |
248 public base::SupportsWeakPtr<MenuManager>, | 269 public base::SupportsWeakPtr<MenuManager>, |
249 public BrowserContextKeyedService { | 270 public BrowserContextKeyedService { |
250 public: | 271 public: |
251 MenuManager(Profile* profile, StateStore* store_); | 272 MenuManager(Profile* profile, StateStore* store_); |
252 virtual ~MenuManager(); | 273 virtual ~MenuManager(); |
253 | 274 |
254 // Convenience function to get the MenuManager for a Profile. | 275 // Convenience function to get the MenuManager for a Profile. |
255 static MenuManager* Get(Profile* profile); | 276 static MenuManager* Get(Profile* profile); |
256 | 277 |
257 // Returns the ids of extensions which have menu items registered. | 278 // Returns the keys of extensions which have menu items registered. |
258 std::set<std::string> ExtensionIds(); | 279 std::set<MenuItem::ExtensionKey> ExtensionIds(); |
259 | 280 |
260 // Returns a list of all the *top-level* menu items (added via AddContextItem) | 281 // 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 | 282 // for the given extension specified by |extension_key|, *not* including child |
262 // AddChildItem); although those can be reached via the top-level items' | 283 // items (added via AddChildItem); although those can be reached via the |
263 // children. A view can then decide how to display these, including whether to | 284 // top-level items' children. A view can then decide how to display these, |
264 // put them into a submenu if there are more than 1. | 285 // including whether to put them into a submenu if there are more than 1. |
265 const MenuItem::List* MenuItems(const std::string& extension_id); | 286 const MenuItem::List* MenuItems(const MenuItem::ExtensionKey& extension_key); |
266 | 287 |
267 // Adds a top-level menu item for an extension, requiring the |extension| | 288 // 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 | 289 // pointer so it can load the icon for the extension. Takes ownership of |
269 // |item|. Returns a boolean indicating success or failure. | 290 // |item|. Returns a boolean indicating success or failure. |
270 bool AddContextItem(const Extension* extension, MenuItem* item); | 291 bool AddContextItem(const Extension* extension, MenuItem* item); |
271 | 292 |
272 // Add an item as a child of another item which has been previously added, and | 293 // 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. | 294 // takes ownership of |item|. Returns a boolean indicating success or failure. |
274 bool AddChildItem(const MenuItem::Id& parent_id, | 295 bool AddChildItem(const MenuItem::Id& parent_id, |
275 MenuItem* child); | 296 MenuItem* child); |
276 | 297 |
277 // Makes existing item with |child_id| a child of the item with |parent_id|. | 298 // 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 | 299 // 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 | 300 // 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 | 301 // 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. | 302 // |parent_id|, which means the item should be moved to the top-level. |
282 bool ChangeParent(const MenuItem::Id& child_id, | 303 bool ChangeParent(const MenuItem::Id& child_id, |
283 const MenuItem::Id* parent_id); | 304 const MenuItem::Id* parent_id); |
284 | 305 |
285 // Removes a context menu item with the given id (whether it is a top-level | 306 // 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 | 307 // item or a child of some other item), returning true if the item was found |
287 // and removed or false otherwise. | 308 // and removed or false otherwise. |
288 bool RemoveContextMenuItem(const MenuItem::Id& id); | 309 bool RemoveContextMenuItem(const MenuItem::Id& id); |
289 | 310 |
290 // Removes all items for the given extension id. | 311 // Removes all items for the given extension specified by |extension_key|. |
291 void RemoveAllContextItems(const std::string& extension_id); | 312 void RemoveAllContextItems(const MenuItem::ExtensionKey& extension_key); |
292 | 313 |
293 // Returns the item with the given |id| or NULL. | 314 // Returns the item with the given |id| or NULL. |
294 MenuItem* GetItemById(const MenuItem::Id& id) const; | 315 MenuItem* GetItemById(const MenuItem::Id& id) const; |
295 | 316 |
296 // Notify the MenuManager that an item has been updated not through | 317 // Notify the MenuManager that an item has been updated not through |
297 // an explicit call into MenuManager. For example, if an item is | 318 // 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. | 319 // acquired by a call to GetItemById and changed, then this should be called. |
299 // Returns true if the item was found or false otherwise. | 320 // Returns true if the item was found or false otherwise. |
300 bool ItemUpdated(const MenuItem::Id& id); | 321 bool ItemUpdated(const MenuItem::Id& id); |
301 | 322 |
302 // Called when a menu item is clicked on by the user. | 323 // Called when a menu item is clicked on by the user. |
303 void ExecuteCommand(Profile* profile, | 324 void ExecuteCommand(Profile* profile, |
304 content::WebContents* web_contents, | 325 content::WebContents* web_contents, |
305 const content::ContextMenuParams& params, | 326 const content::ContextMenuParams& params, |
306 const MenuItem::Id& menu_item_id); | 327 const MenuItem::Id& menu_item_id); |
307 | 328 |
308 // This returns a bitmap of width/height kFaviconSize, loaded either from an | 329 // This returns a bitmap of width/height kFaviconSize, loaded either from an |
309 // entry specified in the extension's 'icon' section of the manifest, or a | 330 // entry specified in the extension's 'icon' section of the manifest, or a |
310 // default extension icon. | 331 // default extension icon. |
311 const SkBitmap& GetIconForExtension(const std::string& extension_id); | 332 const SkBitmap& GetIconForExtension(const std::string& extension_id); |
312 | 333 |
313 // Implements the content::NotificationObserver interface. | 334 // Implements the content::NotificationObserver interface. |
314 virtual void Observe(int type, const content::NotificationSource& source, | 335 virtual void Observe(int type, const content::NotificationSource& source, |
315 const content::NotificationDetails& details) OVERRIDE; | 336 const content::NotificationDetails& details) OVERRIDE; |
316 | 337 |
317 // Stores the menu items for the extension in the state storage. | 338 // Stores the menu items for the extension in the state storage. |
318 void WriteToStorage(const Extension* extension); | 339 void WriteToStorage(const Extension* extension, |
| 340 const MenuItem::ExtensionKey& extension_key); |
319 | 341 |
320 // Reads menu items for the extension from the state storage. Any invalid | 342 // Reads menu items for the extension from the state storage. Any invalid |
321 // items are ignored. | 343 // items are ignored. |
322 void ReadFromStorage(const std::string& extension_id, | 344 void ReadFromStorage(const std::string& extension_id, |
323 scoped_ptr<base::Value> value); | 345 scoped_ptr<base::Value> value); |
324 | 346 |
325 // Removes all "incognito" "split" mode context items. | 347 // Removes all "incognito" "split" mode context items. |
326 void RemoveAllIncognitoContextItems(); | 348 void RemoveAllIncognitoContextItems(); |
327 | 349 |
328 private: | 350 private: |
329 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, DeleteParent); | 351 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, DeleteParent); |
330 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, RemoveOneByOne); | 352 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, RemoveOneByOne); |
331 | 353 |
332 // This is a helper function which takes care of de-selecting any other radio | 354 // This is a helper function which takes care of de-selecting any other radio |
333 // items in the same group (i.e. that are adjacent in the list). | 355 // items in the same group (i.e. that are adjacent in the list). |
334 void RadioItemSelected(MenuItem* item); | 356 void RadioItemSelected(MenuItem* item); |
335 | 357 |
336 // Make sure that there is only one radio item selected at once in any run. | 358 // 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 | 359 // 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 | 360 // will get selected. If there are multiple radio items selected, then only |
339 // the last one will get selcted. | 361 // the last one will get selcted. |
340 void SanitizeRadioList(const MenuItem::List& item_list); | 362 void SanitizeRadioList(const MenuItem::List& item_list); |
341 | 363 |
342 // Returns true if item is a descendant of an item with id |ancestor_id|. | 364 // Returns true if item is a descendant of an item with id |ancestor_id|. |
343 bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id); | 365 bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id); |
344 | 366 |
345 // We keep items organized by mapping an extension id to a list of items. | 367 // We keep items organized by mapping ExtensionKey to a list of items. |
346 typedef std::map<std::string, MenuItem::List> MenuItemMap; | 368 typedef std::map<MenuItem::ExtensionKey, MenuItem::List> MenuItemMap; |
347 MenuItemMap context_items_; | 369 MenuItemMap context_items_; |
348 | 370 |
349 // This lets us make lookup by id fast. It maps id to MenuItem* for | 371 // 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 | 372 // all items the menu manager knows about, including all children of top-level |
351 // items. | 373 // items. |
352 std::map<MenuItem::Id, MenuItem*> items_by_id_; | 374 std::map<MenuItem::Id, MenuItem*> items_by_id_; |
353 | 375 |
354 content::NotificationRegistrar registrar_; | 376 content::NotificationRegistrar registrar_; |
355 | 377 |
356 ExtensionIconManager icon_manager_; | 378 ExtensionIconManager icon_manager_; |
357 | 379 |
358 Profile* profile_; | 380 Profile* profile_; |
359 | 381 |
360 // Owned by ExtensionSystem. | 382 // Owned by ExtensionSystem. |
361 StateStore* store_; | 383 StateStore* store_; |
362 | 384 |
363 DISALLOW_COPY_AND_ASSIGN(MenuManager); | 385 DISALLOW_COPY_AND_ASSIGN(MenuManager); |
364 }; | 386 }; |
365 | 387 |
366 } // namespace extensions | 388 } // namespace extensions |
367 | 389 |
368 #endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ | 390 #endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
OLD | NEW |