| 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_EXTENSION_MENU_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 27 class Profile; | 27 class Profile; |
| 28 class SkBitmap; | 28 class SkBitmap; |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 class WebContents; | 31 class WebContents; |
| 32 struct ContextMenuParams; | 32 struct ContextMenuParams; |
| 33 } | 33 } |
| 34 | 34 |
| 35 namespace extensions { | 35 namespace extensions { |
| 36 class Extension; | 36 class Extension; |
| 37 } | |
| 38 | 37 |
| 39 // Represents a menu item added by an extension. | 38 // Represents a menu item added by an extension. |
| 40 class ExtensionMenuItem { | 39 class MenuItem { |
| 41 public: | 40 public: |
| 42 // A list of ExtensionMenuItem's. | 41 // A list of MenuItems. |
| 43 typedef std::vector<ExtensionMenuItem*> List; | 42 typedef std::vector<MenuItem*> List; |
| 44 | 43 |
| 45 // An Id uniquely identifies a context menu item registered by an extension. | 44 // An Id uniquely identifies a context menu item registered by an extension. |
| 46 struct Id { | 45 struct Id { |
| 47 Id(); | 46 Id(); |
| 48 // Since the unique ID (uid or string_uid) is parsed from API arguments, | 47 // 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 | 48 // the normal usage is to set the uid or string_uid immediately after |
| 50 // construction. | 49 // construction. |
| 51 Id(bool incognito, const std::string& extension_id); | 50 Id(bool incognito, const std::string& extension_id); |
| 52 ~Id(); | 51 ~Id(); |
| 53 | 52 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 if (!value.GetAsInteger(&int_value) || int_value < 0) | 118 if (!value.GetAsInteger(&int_value) || int_value < 0) |
| 120 return false; | 119 return false; |
| 121 value_ = int_value; | 120 value_ = int_value; |
| 122 return true; | 121 return true; |
| 123 } | 122 } |
| 124 | 123 |
| 125 private: | 124 private: |
| 126 uint32 value_; // A bitmask of Context values. | 125 uint32 value_; // A bitmask of Context values. |
| 127 }; | 126 }; |
| 128 | 127 |
| 129 ExtensionMenuItem(const Id& id, | 128 MenuItem(const Id& id, |
| 130 const std::string& title, | 129 const std::string& title, |
| 131 bool checked, | 130 bool checked, |
| 132 bool enabled, | 131 bool enabled, |
| 133 Type type, | 132 Type type, |
| 134 const ContextList& contexts); | 133 const ContextList& contexts); |
| 135 virtual ~ExtensionMenuItem(); | 134 virtual ~MenuItem(); |
| 136 | 135 |
| 137 // Simple accessor methods. | 136 // Simple accessor methods. |
| 138 bool incognito() const { return id_.incognito; } | 137 bool incognito() const { return id_.incognito; } |
| 139 const std::string& extension_id() const { return id_.extension_id; } | 138 const std::string& extension_id() const { return id_.extension_id; } |
| 140 const std::string& title() const { return title_; } | 139 const std::string& title() const { return title_; } |
| 141 const List& children() { return children_; } | 140 const List& children() { return children_; } |
| 142 const Id& id() const { return id_; } | 141 const Id& id() const { return id_; } |
| 143 Id* parent_id() const { return parent_id_.get(); } | 142 Id* parent_id() const { return parent_id_.get(); } |
| 144 int child_count() const { return children_.size(); } | 143 int child_count() const { return children_.size(); } |
| 145 ContextList contexts() const { return contexts_; } | 144 ContextList contexts() const { return contexts_; } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 169 // result will be no longer than |max_length|. | 168 // result will be no longer than |max_length|. |
| 170 string16 TitleWithReplacement(const string16& selection, | 169 string16 TitleWithReplacement(const string16& selection, |
| 171 size_t max_length) const; | 170 size_t max_length) const; |
| 172 | 171 |
| 173 // Sets the checked state to |checked|. Returns true if successful. | 172 // Sets the checked state to |checked|. Returns true if successful. |
| 174 bool SetChecked(bool checked); | 173 bool SetChecked(bool checked); |
| 175 | 174 |
| 176 // Converts to Value for serialization to preferences. | 175 // Converts to Value for serialization to preferences. |
| 177 scoped_ptr<base::DictionaryValue> ToValue() const; | 176 scoped_ptr<base::DictionaryValue> ToValue() const; |
| 178 | 177 |
| 179 // Returns a new ExtensionMenuItem created from |value|, or NULL if there is | 178 // Returns a new MenuItem created from |value|, or NULL if there is |
| 180 // an error. The caller takes ownership of the ExtensionMenuItem. | 179 // an error. The caller takes ownership of the MenuItem. |
| 181 static ExtensionMenuItem* Populate(const std::string& extension_id, | 180 static MenuItem* Populate(const std::string& extension_id, |
| 182 const DictionaryValue& value, | 181 const DictionaryValue& value, |
| 183 std::string* error); | 182 std::string* error); |
| 184 | 183 |
| 185 // Sets any document and target URL patterns from |properties|. | 184 // Sets any document and target URL patterns from |properties|. |
| 186 bool PopulateURLPatterns(const base::DictionaryValue& properties, | 185 bool PopulateURLPatterns(const base::DictionaryValue& properties, |
| 187 const char* document_url_patterns_key, | 186 const char* document_url_patterns_key, |
| 188 const char* target_url_patterns_key, | 187 const char* target_url_patterns_key, |
| 189 std::string* error); | 188 std::string* error); |
| 190 | 189 |
| 191 protected: | 190 protected: |
| 192 friend class ExtensionMenuManager; | 191 friend class MenuManager; |
| 193 | 192 |
| 194 // Takes ownership of |item| and sets its parent_id_. | 193 // Takes ownership of |item| and sets its parent_id_. |
| 195 void AddChild(ExtensionMenuItem* item); | 194 void AddChild(MenuItem* item); |
| 196 | 195 |
| 197 // Takes the child item from this parent. The item is returned and the caller | 196 // Takes the child item from this parent. The item is returned and the caller |
| 198 // then owns the pointer. | 197 // then owns the pointer. |
| 199 ExtensionMenuItem* ReleaseChild(const Id& child_id, bool recursive); | 198 MenuItem* ReleaseChild(const Id& child_id, bool recursive); |
| 200 | 199 |
| 201 // Recursively appends all descendant items (children, grandchildren, etc.) | 200 // Recursively appends all descendant items (children, grandchildren, etc.) |
| 202 // to the output |list|. | 201 // to the output |list|. |
| 203 void GetFlattenedSubtree(ExtensionMenuItem::List* list); | 202 void GetFlattenedSubtree(MenuItem::List* list); |
| 204 | 203 |
| 205 // Recursively removes all descendant items (children, grandchildren, etc.), | 204 // Recursively removes all descendant items (children, grandchildren, etc.), |
| 206 // returning the ids of the removed items. | 205 // returning the ids of the removed items. |
| 207 std::set<Id> RemoveAllDescendants(); | 206 std::set<Id> RemoveAllDescendants(); |
| 208 | 207 |
| 209 private: | 208 private: |
| 210 // The unique id for this item. | 209 // The unique id for this item. |
| 211 Id id_; | 210 Id id_; |
| 212 | 211 |
| 213 // What gets shown in the menu for this item. | 212 // What gets shown in the menu for this item. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 232 // applies to the frame where the click took place. | 231 // applies to the frame where the click took place. |
| 233 URLPatternSet document_url_patterns_; | 232 URLPatternSet document_url_patterns_; |
| 234 | 233 |
| 235 // Patterns for restricting where items appear based on the src/href | 234 // Patterns for restricting where items appear based on the src/href |
| 236 // attribute of IMAGE/AUDIO/VIDEO/LINK tags. | 235 // attribute of IMAGE/AUDIO/VIDEO/LINK tags. |
| 237 URLPatternSet target_url_patterns_; | 236 URLPatternSet target_url_patterns_; |
| 238 | 237 |
| 239 // Any children this item may have. | 238 // Any children this item may have. |
| 240 List children_; | 239 List children_; |
| 241 | 240 |
| 242 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuItem); | 241 DISALLOW_COPY_AND_ASSIGN(MenuItem); |
| 243 }; | 242 }; |
| 244 | 243 |
| 245 // This class keeps track of menu items added by extensions. | 244 // This class keeps track of menu items added by extensions. |
| 246 class ExtensionMenuManager | 245 class MenuManager : public content::NotificationObserver, |
| 247 : public content::NotificationObserver, | 246 public base::SupportsWeakPtr<MenuManager> { |
| 248 public base::SupportsWeakPtr<ExtensionMenuManager> { | |
| 249 public: | 247 public: |
| 250 explicit ExtensionMenuManager(Profile* profile); | 248 explicit MenuManager(Profile* profile); |
| 251 virtual ~ExtensionMenuManager(); | 249 virtual ~MenuManager(); |
| 252 | 250 |
| 253 // Returns the ids of extensions which have menu items registered. | 251 // Returns the ids of extensions which have menu items registered. |
| 254 std::set<std::string> ExtensionIds(); | 252 std::set<std::string> ExtensionIds(); |
| 255 | 253 |
| 256 // Returns a list of all the *top-level* menu items (added via AddContextItem) | 254 // Returns a list of all the *top-level* menu items (added via AddContextItem) |
| 257 // for the given extension id, *not* including child items (added via | 255 // for the given extension id, *not* including child items (added via |
| 258 // AddChildItem); although those can be reached via the top-level items' | 256 // AddChildItem); although those can be reached via the top-level items' |
| 259 // children. A view can then decide how to display these, including whether to | 257 // children. A view can then decide how to display these, including whether to |
| 260 // put them into a submenu if there are more than 1. | 258 // put them into a submenu if there are more than 1. |
| 261 const ExtensionMenuItem::List* MenuItems(const std::string& extension_id); | 259 const MenuItem::List* MenuItems(const std::string& extension_id); |
| 262 | 260 |
| 263 // Adds a top-level menu item for an extension, requiring the |extension| | 261 // Adds a top-level menu item for an extension, requiring the |extension| |
| 264 // pointer so it can load the icon for the extension. Takes ownership of | 262 // pointer so it can load the icon for the extension. Takes ownership of |
| 265 // |item|. Returns a boolean indicating success or failure. | 263 // |item|. Returns a boolean indicating success or failure. |
| 266 bool AddContextItem(const extensions::Extension* extension, | 264 bool AddContextItem(const Extension* extension, MenuItem* item); |
| 267 ExtensionMenuItem* item); | |
| 268 | 265 |
| 269 // Add an item as a child of another item which has been previously added, and | 266 // Add an item as a child of another item which has been previously added, and |
| 270 // takes ownership of |item|. Returns a boolean indicating success or failure. | 267 // takes ownership of |item|. Returns a boolean indicating success or failure. |
| 271 bool AddChildItem(const ExtensionMenuItem::Id& parent_id, | 268 bool AddChildItem(const MenuItem::Id& parent_id, |
| 272 ExtensionMenuItem* child); | 269 MenuItem* child); |
| 273 | 270 |
| 274 // Makes existing item with |child_id| a child of the item with |parent_id|. | 271 // Makes existing item with |child_id| a child of the item with |parent_id|. |
| 275 // If the child item was already a child of another parent, this will remove | 272 // If the child item was already a child of another parent, this will remove |
| 276 // it from that parent first. It is an error to try and move an item to be a | 273 // it from that parent first. It is an error to try and move an item to be a |
| 277 // child of one of its own descendants. It is legal to pass NULL for | 274 // child of one of its own descendants. It is legal to pass NULL for |
| 278 // |parent_id|, which means the item should be moved to the top-level. | 275 // |parent_id|, which means the item should be moved to the top-level. |
| 279 bool ChangeParent(const ExtensionMenuItem::Id& child_id, | 276 bool ChangeParent(const MenuItem::Id& child_id, |
| 280 const ExtensionMenuItem::Id* parent_id); | 277 const MenuItem::Id* parent_id); |
| 281 | 278 |
| 282 // Removes a context menu item with the given id (whether it is a top-level | 279 // Removes a context menu item with the given id (whether it is a top-level |
| 283 // item or a child of some other item), returning true if the item was found | 280 // item or a child of some other item), returning true if the item was found |
| 284 // and removed or false otherwise. | 281 // and removed or false otherwise. |
| 285 bool RemoveContextMenuItem(const ExtensionMenuItem::Id& id); | 282 bool RemoveContextMenuItem(const MenuItem::Id& id); |
| 286 | 283 |
| 287 // Removes all items for the given extension id. | 284 // Removes all items for the given extension id. |
| 288 void RemoveAllContextItems(const std::string& extension_id); | 285 void RemoveAllContextItems(const std::string& extension_id); |
| 289 | 286 |
| 290 // Returns the item with the given |id| or NULL. | 287 // Returns the item with the given |id| or NULL. |
| 291 ExtensionMenuItem* GetItemById(const ExtensionMenuItem::Id& id) const; | 288 MenuItem* GetItemById(const MenuItem::Id& id) const; |
| 292 | 289 |
| 293 // Notify the ExtensionMenuManager that an item has been updated not through | 290 // Notify the MenuManager that an item has been updated not through |
| 294 // an explicit call into ExtensionMenuManager. For example, if an item is | 291 // an explicit call into MenuManager. For example, if an item is |
| 295 // acquired by a call to GetItemById and changed, then this should be called. | 292 // acquired by a call to GetItemById and changed, then this should be called. |
| 296 // Returns true if the item was found or false otherwise. | 293 // Returns true if the item was found or false otherwise. |
| 297 bool ItemUpdated(const ExtensionMenuItem::Id& id); | 294 bool ItemUpdated(const MenuItem::Id& id); |
| 298 | 295 |
| 299 // Called when a menu item is clicked on by the user. | 296 // Called when a menu item is clicked on by the user. |
| 300 void ExecuteCommand(Profile* profile, | 297 void ExecuteCommand(Profile* profile, |
| 301 content::WebContents* web_contents, | 298 content::WebContents* web_contents, |
| 302 const content::ContextMenuParams& params, | 299 const content::ContextMenuParams& params, |
| 303 const ExtensionMenuItem::Id& menu_item_id); | 300 const MenuItem::Id& menu_item_id); |
| 304 | 301 |
| 305 // This returns a bitmap of width/height kFaviconSize, loaded either from an | 302 // This returns a bitmap of width/height kFaviconSize, loaded either from an |
| 306 // entry specified in the extension's 'icon' section of the manifest, or a | 303 // entry specified in the extension's 'icon' section of the manifest, or a |
| 307 // default extension icon. | 304 // default extension icon. |
| 308 const SkBitmap& GetIconForExtension(const std::string& extension_id); | 305 const SkBitmap& GetIconForExtension(const std::string& extension_id); |
| 309 | 306 |
| 310 // Implements the content::NotificationObserver interface. | 307 // Implements the content::NotificationObserver interface. |
| 311 virtual void Observe(int type, const content::NotificationSource& source, | 308 virtual void Observe(int type, const content::NotificationSource& source, |
| 312 const content::NotificationDetails& details) OVERRIDE; | 309 const content::NotificationDetails& details) OVERRIDE; |
| 313 | 310 |
| 314 // Stores the menu items for the extension in the state storage. | 311 // Stores the menu items for the extension in the state storage. |
| 315 void WriteToStorage(const extensions::Extension* extension); | 312 void WriteToStorage(const Extension* extension); |
| 316 | 313 |
| 317 // Reads menu items for the extension from the state storage. Any invalid | 314 // Reads menu items for the extension from the state storage. Any invalid |
| 318 // items are ignored. | 315 // items are ignored. |
| 319 void ReadFromStorage(const std::string& extension_id, | 316 void ReadFromStorage(const std::string& extension_id, |
| 320 scoped_ptr<base::Value> value); | 317 scoped_ptr<base::Value> value); |
| 321 | 318 |
| 322 private: | 319 private: |
| 323 FRIEND_TEST_ALL_PREFIXES(ExtensionMenuManagerTest, DeleteParent); | 320 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, DeleteParent); |
| 324 FRIEND_TEST_ALL_PREFIXES(ExtensionMenuManagerTest, RemoveOneByOne); | 321 FRIEND_TEST_ALL_PREFIXES(MenuManagerTest, RemoveOneByOne); |
| 325 | 322 |
| 326 // This is a helper function which takes care of de-selecting any other radio | 323 // This is a helper function which takes care of de-selecting any other radio |
| 327 // items in the same group (i.e. that are adjacent in the list). | 324 // items in the same group (i.e. that are adjacent in the list). |
| 328 void RadioItemSelected(ExtensionMenuItem* item); | 325 void RadioItemSelected(MenuItem* item); |
| 329 | 326 |
| 330 // Make sure that there is only one radio item selected at once in any run. | 327 // Make sure that there is only one radio item selected at once in any run. |
| 331 // If there are no radio items selected, then the first item in the run | 328 // If there are no radio items selected, then the first item in the run |
| 332 // will get selected. If there are multiple radio items selected, then only | 329 // will get selected. If there are multiple radio items selected, then only |
| 333 // the last one will get selcted. | 330 // the last one will get selcted. |
| 334 void SanitizeRadioList(const ExtensionMenuItem::List& item_list); | 331 void SanitizeRadioList(const MenuItem::List& item_list); |
| 335 | 332 |
| 336 // Returns true if item is a descendant of an item with id |ancestor_id|. | 333 // Returns true if item is a descendant of an item with id |ancestor_id|. |
| 337 bool DescendantOf(ExtensionMenuItem* item, | 334 bool DescendantOf(MenuItem* item, const MenuItem::Id& ancestor_id); |
| 338 const ExtensionMenuItem::Id& ancestor_id); | |
| 339 | 335 |
| 340 // We keep items organized by mapping an extension id to a list of items. | 336 // We keep items organized by mapping an extension id to a list of items. |
| 341 typedef std::map<std::string, ExtensionMenuItem::List> MenuItemMap; | 337 typedef std::map<std::string, MenuItem::List> MenuItemMap; |
| 342 MenuItemMap context_items_; | 338 MenuItemMap context_items_; |
| 343 | 339 |
| 344 // This lets us make lookup by id fast. It maps id to ExtensionMenuItem* for | 340 // This lets us make lookup by id fast. It maps id to MenuItem* for |
| 345 // all items the menu manager knows about, including all children of top-level | 341 // all items the menu manager knows about, including all children of top-level |
| 346 // items. | 342 // items. |
| 347 std::map<ExtensionMenuItem::Id, ExtensionMenuItem*> items_by_id_; | 343 std::map<MenuItem::Id, MenuItem*> items_by_id_; |
| 348 | 344 |
| 349 content::NotificationRegistrar registrar_; | 345 content::NotificationRegistrar registrar_; |
| 350 | 346 |
| 351 ExtensionIconManager icon_manager_; | 347 ExtensionIconManager icon_manager_; |
| 352 | 348 |
| 353 Profile* profile_; | 349 Profile* profile_; |
| 354 | 350 |
| 355 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuManager); | 351 DISALLOW_COPY_AND_ASSIGN(MenuManager); |
| 356 }; | 352 }; |
| 357 | 353 |
| 358 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MENU_MANAGER_H_ | 354 } // namespace extensions |
| 355 |
| 356 #endif // CHROME_BROWSER_EXTENSIONS_MENU_MANAGER_H_ |
| OLD | NEW |