 Chromium Code Reviews
 Chromium Code Reviews Issue 359493005:
  Extend contextMenus API to support browser/page actions  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 359493005:
  Extend contextMenus API to support browser/page actions  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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_CONTEXT_MENU_MATCHER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_CONTEXT_MENU_MATCHER_H_ | 
| 6 #define CHROME_BROWSER_EXTENSIONS_CONTEXT_MENU_MATCHER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_CONTEXT_MENU_MATCHER_H_ | 
| 7 | 7 | 
| 8 #include <map> | 8 #include <map> | 
| 9 | 9 | 
| 10 #include "base/callback.h" | 10 #include "base/callback.h" | 
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" | 
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" | 
| 13 #include "chrome/browser/extensions/menu_manager.h" | 13 #include "chrome/browser/extensions/menu_manager.h" | 
| 14 #include "ui/base/models/simple_menu_model.h" | 14 #include "ui/base/models/simple_menu_model.h" | 
| 15 | 15 | 
| 16 class ExtensionContextMenuBrowserTest; | 16 class ExtensionContextMenuBrowserTest; | 
| 17 class ExtensionContextMenuModelTest; | |
| 17 class Profile; | 18 class Profile; | 
| 18 | 19 | 
| 19 namespace extensions { | 20 namespace extensions { | 
| 20 | 21 | 
| 21 // This class contains code that is shared between the various places where | 22 // This class contains code that is shared between the various places where | 
| 22 // context menu items added by the extension or app should be shown. | 23 // context menu items added by the extension or app should be shown. | 
| 23 class ContextMenuMatcher { | 24 class ContextMenuMatcher { | 
| 24 public: | 25 public: | 
| 25 static const size_t kMaxExtensionItemTitleLength; | 26 static const size_t kMaxExtensionItemTitleLength; | 
| 26 | 27 | 
| 27 // The |filter| will be called on possibly matching menu items, and its | 28 // The |filter| will be called on possibly matching menu items, and its | 
| 28 // result is used to determine which items to actually append to the menu. | 29 // result is used to determine which items to actually append to the menu. | 
| 29 ContextMenuMatcher(Profile* profile, | 30 ContextMenuMatcher(Profile* profile, | 
| 30 ui::SimpleMenuModel::Delegate* delegate, | 31 ui::SimpleMenuModel::Delegate* delegate, | 
| 31 ui::SimpleMenuModel* menu_model, | 32 ui::SimpleMenuModel* menu_model, | 
| 32 const base::Callback<bool(const MenuItem*)>& filter); | 33 const base::Callback<bool(const MenuItem*)>& filter); | 
| 33 | 34 | 
| 34 // This is a helper function to append items for one particular extension. | 35 // This is a helper function to append items for one particular extension. | 
| 35 // The |index| parameter is used for assigning id's, and is incremented for | 36 // The |index| parameter is used for assigning id's, and is incremented for | 
| 36 // each item actually added. | 37 // each item actually added. |is_action_menu| is used for browser and page | 
| 38 // action context menus, in which menu items are not placed in submenus | |
| 39 // and the extension's icon is not shown. | |
| 37 void AppendExtensionItems(const MenuItem::ExtensionKey& extension_key, | 40 void AppendExtensionItems(const MenuItem::ExtensionKey& extension_key, | 
| 38 const base::string16& selection_text, | 41 const base::string16& selection_text, | 
| 39 int* index); | 42 int* index, | 
| 43 bool is_action_menu); | |
| 40 | 44 | 
| 41 void Clear(); | 45 void Clear(); | 
| 42 | 46 | 
| 43 // This function returns the top level context menu title of an extension | 47 // This function returns the top level context menu title of an extension | 
| 44 // based on a printable selection text. | 48 // based on a printable selection text. | 
| 45 base::string16 GetTopLevelContextMenuTitle( | 49 base::string16 GetTopLevelContextMenuTitle( | 
| 46 const MenuItem::ExtensionKey& extension_key, | 50 const MenuItem::ExtensionKey& extension_key, | 
| 47 const base::string16& selection_text); | 51 const base::string16& selection_text); | 
| 48 | 52 | 
| 49 bool IsCommandIdChecked(int command_id) const; | 53 bool IsCommandIdChecked(int command_id) const; | 
| 50 bool IsCommandIdEnabled(int command_id) const; | 54 bool IsCommandIdEnabled(int command_id) const; | 
| 51 void ExecuteCommand(int command_id, | 55 void ExecuteCommand(int command_id, | 
| 52 content::WebContents* web_contents, | 56 content::WebContents* web_contents, | 
| 53 const content::ContextMenuParams& params); | 57 const content::ContextMenuParams& params); | 
| 54 | 58 | 
| 55 private: | 59 private: | 
| 56 friend class ::ExtensionContextMenuBrowserTest; | 60 friend class ::ExtensionContextMenuBrowserTest; | 
| 61 friend class ::ExtensionContextMenuModelTest; | |
| 
Devlin
2014/07/29 20:49:45
Do we need this?
 
gpdavis
2014/07/30 20:53:02
Oops, no, we don't.  I had an idea that would have
 | |
| 57 | 62 | 
| 58 bool GetRelevantExtensionTopLevelItems( | 63 bool GetRelevantExtensionTopLevelItems( | 
| 59 const MenuItem::ExtensionKey& extension_key, | 64 const MenuItem::ExtensionKey& extension_key, | 
| 60 const Extension** extension, | 65 const Extension** extension, | 
| 61 bool* can_cross_incognito, | 66 bool* can_cross_incognito, | 
| 62 MenuItem::List& items); | 67 MenuItem::List& items); | 
| 63 | 68 | 
| 64 MenuItem::List GetRelevantExtensionItems( | 69 MenuItem::List GetRelevantExtensionItems( | 
| 65 const MenuItem::List& items, | 70 const MenuItem::List& items, | 
| 66 bool can_cross_incognito); | 71 bool can_cross_incognito); | 
| 67 | 72 | 
| 68 // Used for recursively adding submenus of extension items. | 73 // Used for recursively adding submenus of extension items. | 
| 69 void RecursivelyAppendExtensionItems(const MenuItem::List& items, | 74 void RecursivelyAppendExtensionItems(const MenuItem::List& items, | 
| 70 bool can_cross_incognito, | 75 bool can_cross_incognito, | 
| 71 const base::string16& selection_text, | 76 const base::string16& selection_text, | 
| 72 ui::SimpleMenuModel* menu_model, | 77 ui::SimpleMenuModel* menu_model, | 
| 73 int* index); | 78 int* index, | 
| 79 bool is_action_menu_top_level); | |
| 74 | 80 | 
| 75 // Attempts to get an MenuItem given the id of a context menu item. | 81 // Attempts to get an MenuItem given the id of a context menu item. | 
| 76 extensions::MenuItem* GetExtensionMenuItem(int id) const; | 82 extensions::MenuItem* GetExtensionMenuItem(int id) const; | 
| 77 | 83 | 
| 78 // This will set the icon on the most recently-added item in the menu_model_. | 84 // This will set the icon on the most recently-added item in the menu_model_. | 
| 79 void SetExtensionIcon(const std::string& extension_id); | 85 void SetExtensionIcon(const std::string& extension_id); | 
| 80 | 86 | 
| 81 Profile* profile_; | 87 Profile* profile_; | 
| 82 ui::SimpleMenuModel* menu_model_; | 88 ui::SimpleMenuModel* menu_model_; | 
| 83 ui::SimpleMenuModel::Delegate* delegate_; | 89 ui::SimpleMenuModel::Delegate* delegate_; | 
| 84 | 90 | 
| 85 base::Callback<bool(const MenuItem*)> filter_; | 91 base::Callback<bool(const MenuItem*)> filter_; | 
| 86 | 92 | 
| 87 // Maps the id from a context menu item to the MenuItem's internal id. | 93 // Maps the id from a context menu item to the MenuItem's internal id. | 
| 88 std::map<int, extensions::MenuItem::Id> extension_item_map_; | 94 std::map<int, extensions::MenuItem::Id> extension_item_map_; | 
| 89 | 95 | 
| 90 // Keep track of and clean up menu models for submenus. | 96 // Keep track of and clean up menu models for submenus. | 
| 91 ScopedVector<ui::SimpleMenuModel> extension_menu_models_; | 97 ScopedVector<ui::SimpleMenuModel> extension_menu_models_; | 
| 92 | 98 | 
| 93 DISALLOW_COPY_AND_ASSIGN(ContextMenuMatcher); | 99 DISALLOW_COPY_AND_ASSIGN(ContextMenuMatcher); | 
| 94 }; | 100 }; | 
| 95 | 101 | 
| 96 } // namespace extensions | 102 } // namespace extensions | 
| 97 | 103 | 
| 98 #endif // CHROME_BROWSER_EXTENSIONS_CONTEXT_MENU_MATCHER_H_ | 104 #endif // CHROME_BROWSER_EXTENSIONS_CONTEXT_MENU_MATCHER_H_ | 
| OLD | NEW |