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. | |
Yoyo Zhou
2014/03/07 02:40:30
This sentence should mention webviews.
lazyboy
2014/03/07 06:38:03
Done.
| |
46 // |webview_instance_id| = 0 for regular extension items. | |
47 struct ExtensionKey { | |
48 std::string extension_id; | |
49 int webview_instance_id; | |
50 | |
51 ExtensionKey(); | |
52 ExtensionKey(const std::string& extension_id, int webview_instance_id); | |
53 explicit ExtensionKey(const std::string& extension_id); | |
54 | |
55 bool operator==(const ExtensionKey& other) const; | |
56 bool operator!=(const ExtensionKey& other) const; | |
57 bool operator<(const ExtensionKey& other) const; | |
58 | |
59 bool empty() const; | |
60 }; | |
61 | |
45 // An Id uniquely identifies a context menu item registered by an extension. | 62 // An Id uniquely identifies a context menu item registered by an extension. |
46 struct Id { | 63 struct Id { |
47 Id(); | 64 Id(); |
48 // Since the unique ID (uid or string_uid) is parsed from API arguments, | 65 // 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 | 66 // the normal usage is to set the uid or string_uid immediately after |
50 // construction. | 67 // construction. |
51 Id(bool incognito, const std::string& extension_id); | 68 Id(bool incognito, const ExtensionKey& extension_key); |
52 ~Id(); | 69 ~Id(); |
53 | 70 |
54 bool operator==(const Id& other) const; | 71 bool operator==(const Id& other) const; |
55 bool operator!=(const Id& other) const; | 72 bool operator!=(const Id& other) const; |
56 bool operator<(const Id& other) const; | 73 bool operator<(const Id& other) const; |
57 | 74 |
58 bool incognito; | 75 bool incognito; |
59 std::string extension_id; | 76 ExtensionKey extension_key; |
60 // Only one of uid or string_uid will be defined. | 77 // Only one of uid or string_uid will be defined. |
61 int uid; | 78 int uid; |
62 std::string string_uid; | 79 std::string string_uid; |
63 }; | 80 }; |
64 | 81 |
65 // For context menus, these are the contexts where an item can appear. | 82 // For context menus, these are the contexts where an item can appear. |
66 enum Context { | 83 enum Context { |
67 ALL = 1, | 84 ALL = 1, |
68 PAGE = 2, | 85 PAGE = 2, |
69 SELECTION = 4, | 86 SELECTION = 4, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 MenuItem(const Id& id, | 148 MenuItem(const Id& id, |
132 const std::string& title, | 149 const std::string& title, |
133 bool checked, | 150 bool checked, |
134 bool enabled, | 151 bool enabled, |
135 Type type, | 152 Type type, |
136 const ContextList& contexts); | 153 const ContextList& contexts); |
137 virtual ~MenuItem(); | 154 virtual ~MenuItem(); |
138 | 155 |
139 // Simple accessor methods. | 156 // Simple accessor methods. |
140 bool incognito() const { return id_.incognito; } | 157 bool incognito() const { return id_.incognito; } |
141 const std::string& extension_id() const { return id_.extension_id; } | 158 const std::string& extension_id() const { |
159 return id_.extension_key.extension_id; | |
160 } | |
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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 class MenuManager : public content::NotificationObserver, | 266 class MenuManager : public content::NotificationObserver, |
248 public base::SupportsWeakPtr<MenuManager>, | 267 public base::SupportsWeakPtr<MenuManager>, |
249 public BrowserContextKeyedService { | 268 public BrowserContextKeyedService { |
250 public: | 269 public: |
251 MenuManager(Profile* profile, StateStore* store_); | 270 MenuManager(Profile* profile, StateStore* store_); |
252 virtual ~MenuManager(); | 271 virtual ~MenuManager(); |
253 | 272 |
254 // Convenience function to get the MenuManager for a Profile. | 273 // Convenience function to get the MenuManager for a Profile. |
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. |
Yoyo Zhou
2014/03/07 02:40:30
update comment
lazyboy
2014/03/07 06:38:03
Done.
| |
258 std::set<std::string> ExtensionIds(); | 277 std::set<MenuItem::ExtensionKey> 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 |
302 // Called when a menu item is clicked on by the user. | 321 // Called when a menu item is clicked on by the user. |
303 void ExecuteCommand(Profile* profile, | 322 void ExecuteCommand(Profile* profile, |
304 content::WebContents* web_contents, | 323 content::WebContents* web_contents, |
305 const content::ContextMenuParams& params, | 324 const content::ContextMenuParams& params, |
306 const MenuItem::Id& menu_item_id); | 325 const MenuItem::Id& menu_item_id); |
307 | 326 |
308 // This returns a bitmap of width/height kFaviconSize, loaded either from an | 327 // 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 | 328 // entry specified in the extension's 'icon' section of the manifest, or a |
310 // default extension icon. | 329 // default extension icon. |
311 const SkBitmap& GetIconForExtension(const std::string& extension_id); | 330 const SkBitmap& GetIconForExtension(const std::string& extension_id); |
312 | 331 |
313 // Implements the content::NotificationObserver interface. | 332 // Implements the content::NotificationObserver interface. |
314 virtual void Observe(int type, const content::NotificationSource& source, | 333 virtual void Observe(int type, const content::NotificationSource& source, |
315 const content::NotificationDetails& details) OVERRIDE; | 334 const content::NotificationDetails& details) OVERRIDE; |
316 | 335 |
317 // Stores the menu items for the extension in the state storage. | 336 // Stores the menu items for the extension in the state storage. |
318 void WriteToStorage(const Extension* extension); | 337 void WriteToStorage(const Extension* extension, |
338 const MenuItem::ExtensionKey& extension_key); | |
319 | 339 |
320 // Reads menu items for the extension from the state storage. Any invalid | 340 // Reads menu items for the extension from the state storage. Any invalid |
321 // items are ignored. | 341 // items are ignored. |
322 void ReadFromStorage(const std::string& extension_id, | 342 void ReadFromStorage(const std::string& extension_id, |
323 scoped_ptr<base::Value> value); | 343 scoped_ptr<base::Value> value); |
324 | 344 |
325 // Removes all "incognito" "split" mode context items. | 345 // Removes all "incognito" "split" mode context items. |
326 void RemoveAllIncognitoContextItems(); | 346 void RemoveAllIncognitoContextItems(); |
327 | 347 |
328 private: | 348 private: |
329 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, DeleteParent); | 349 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, DeleteParent); |
330 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, RemoveOneByOne); | 350 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, RemoveOneByOne); |
331 | 351 |
332 // This is a helper function which takes care of de-selecting any other radio | 352 // 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). | 353 // items in the same group (i.e. that are adjacent in the list). |
334 void RadioItemSelected(MenuItem* item); | 354 void RadioItemSelected(MenuItem* item); |
335 | 355 |
336 // Make sure that there is only one radio item selected at once in any run. | 356 // 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 | 357 // 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 | 358 // will get selected. If there are multiple radio items selected, then only |
339 // the last one will get selcted. | 359 // the last one will get selcted. |
340 void SanitizeRadioList(const MenuItem::List& item_list); | 360 void SanitizeRadioList(const MenuItem::List& item_list); |
341 | 361 |
342 // Returns true if item is a descendant of an item with id |ancestor_id|. | 362 // Returns true if item is a descendant of an item with id |ancestor_id|. |
343 bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id); | 363 bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id); |
344 | 364 |
345 // We keep items organized by mapping an extension id to a list of items. | 365 // We keep items organized by mapping an extension id to a list of items. |
Yoyo Zhou
2014/03/07 02:40:30
update comment
lazyboy
2014/03/07 06:38:03
Done.
| |
346 typedef std::map<std::string, MenuItem::List> MenuItemMap; | 366 typedef std::map<MenuItem::ExtensionKey, MenuItem::List> MenuItemMap; |
347 MenuItemMap context_items_; | 367 MenuItemMap context_items_; |
348 | 368 |
349 // This lets us make lookup by id fast. It maps id to MenuItem* for | 369 // 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 | 370 // all items the menu manager knows about, including all children of top-level |
351 // items. | 371 // items. |
352 std::map<MenuItem::Id, MenuItem*> items_by_id_; | 372 std::map<MenuItem::Id, MenuItem*> items_by_id_; |
353 | 373 |
354 content::NotificationRegistrar registrar_; | 374 content::NotificationRegistrar registrar_; |
355 | 375 |
356 ExtensionIconManager icon_manager_; | 376 ExtensionIconManager icon_manager_; |
357 | 377 |
358 Profile* profile_; | 378 Profile* profile_; |
359 | 379 |
360 // Owned by ExtensionSystem. | 380 // Owned by ExtensionSystem. |
361 StateStore* store_; | 381 StateStore* store_; |
362 | 382 |
363 DISALLOW_COPY_AND_ASSIGN(MenuManager); | 383 DISALLOW_COPY_AND_ASSIGN(MenuManager); |
364 }; | 384 }; |
365 | 385 |
366 } // namespace extensions | 386 } // namespace extensions |
367 | 387 |
368 #endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ | 388 #endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
OLD | NEW |