[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 5 | #include "chrome/browser/extensions/context_menu_matcher.h" |
| 6 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 7 | #include "base/memory/ptr_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 9 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | a7ff4b7 | 2013-10-17 20:56:02 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_util.h" |
[email protected] | fc103da | 2014-08-16 01:09:32 | [diff] [blame] | 11 | #include "chrome/common/extensions/api/context_menus.h" |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 12 | #include "content/public/browser/browser_context.h" |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 13 | #include "content/public/common/context_menu_params.h" |
[email protected] | fc103da | 2014-08-16 01:09:32 | [diff] [blame] | 14 | #include "extensions/browser/extension_registry.h" |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 15 | #include "ui/gfx/favicon_size.h" |
[email protected] | f34efa2 | 2013-03-05 19:14:23 | [diff] [blame] | 16 | #include "ui/gfx/image/image.h" |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 17 | |
| 18 | namespace extensions { |
| 19 | |
[email protected] | a146532b | 2014-07-30 11:20:09 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | // The range of command IDs reserved for extension's custom menus. |
| 23 | // TODO(oshima): These values will be injected by embedders. |
| 24 | int extensions_context_custom_first = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
| 25 | int extensions_context_custom_last = IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST; |
| 26 | |
| 27 | } // namespace |
| 28 | |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 29 | // static |
| 30 | const size_t ContextMenuMatcher::kMaxExtensionItemTitleLength = 75; |
| 31 | |
[email protected] | a146532b | 2014-07-30 11:20:09 | [diff] [blame] | 32 | // static |
| 33 | int ContextMenuMatcher::ConvertToExtensionsCustomCommandId(int id) { |
| 34 | return extensions_context_custom_first + id; |
| 35 | } |
| 36 | |
| 37 | // static |
| 38 | bool ContextMenuMatcher::IsExtensionsCustomCommandId(int id) { |
| 39 | return id >= extensions_context_custom_first && |
| 40 | id <= extensions_context_custom_last; |
| 41 | } |
| 42 | |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 43 | ContextMenuMatcher::ContextMenuMatcher( |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 44 | content::BrowserContext* browser_context, |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 45 | ui::SimpleMenuModel::Delegate* delegate, |
| 46 | ui::SimpleMenuModel* menu_model, |
| 47 | const base::Callback<bool(const MenuItem*)>& filter) |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 48 | : browser_context_(browser_context), |
| 49 | menu_model_(menu_model), |
| 50 | delegate_(delegate), |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 51 | filter_(filter) { |
| 52 | } |
| 53 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 54 | void ContextMenuMatcher::AppendExtensionItems( |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 55 | const MenuItem::ExtensionKey& extension_key, |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 56 | const base::string16& selection_text, |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 57 | int* index, |
| 58 | bool is_action_menu) { |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 59 | DCHECK_GE(*index, 0); |
| 60 | int max_index = |
[email protected] | a146532b | 2014-07-30 11:20:09 | [diff] [blame] | 61 | extensions_context_custom_last - extensions_context_custom_first; |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 62 | if (*index >= max_index) |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 63 | return; |
| 64 | |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 65 | const Extension* extension = NULL; |
| 66 | MenuItem::List items; |
| 67 | bool can_cross_incognito; |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 68 | if (!GetRelevantExtensionTopLevelItems( |
[email protected] | fc103da | 2014-08-16 01:09:32 | [diff] [blame] | 69 | extension_key, &extension, &can_cross_incognito, &items)) |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 70 | return; |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 71 | |
| 72 | if (items.empty()) |
| 73 | return; |
| 74 | |
| 75 | // If this is the first extension-provided menu item, and there are other |
| 76 | // items in the menu, and the last item is not a separator add a separator. |
Dave Schuyler | e83bf88 | 2017-08-22 18:50:04 | [diff] [blame] | 77 | if (*index == 0 && menu_model_->GetItemCount()) |
| 78 | menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 79 | |
| 80 | // Extensions (other than platform apps) are only allowed one top-level slot |
| 81 | // (and it can't be a radio or checkbox item because we are going to put the |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 82 | // extension icon next to it), unless the context menu is an an action menu. |
| 83 | // Action menus do not include the extension action, and they only include |
| 84 | // items from one extension, so they are not placed within a submenu. |
| 85 | // Otherwise, we automatically push them into a submenu if there is more than |
| 86 | // one top-level item. |
| 87 | if (extension->is_platform_app() || is_action_menu) { |
| 88 | RecursivelyAppendExtensionItems(items, |
| 89 | can_cross_incognito, |
| 90 | selection_text, |
| 91 | menu_model_, |
| 92 | index, |
| 93 | is_action_menu); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 94 | } else { |
[email protected] | a146532b | 2014-07-30 11:20:09 | [diff] [blame] | 95 | int menu_id = ConvertToExtensionsCustomCommandId(*index); |
| 96 | (*index)++; |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 97 | base::string16 title; |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 98 | MenuItem::List submenu_items; |
| 99 | |
| 100 | if (items.size() > 1 || items[0]->type() != MenuItem::NORMAL) { |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 101 | title = base::UTF8ToUTF16(extension->name()); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 102 | submenu_items = items; |
| 103 | } else { |
| 104 | MenuItem* item = items[0]; |
| 105 | extension_item_map_[menu_id] = item->id(); |
| 106 | title = item->TitleWithReplacement(selection_text, |
| 107 | kMaxExtensionItemTitleLength); |
| 108 | submenu_items = GetRelevantExtensionItems(item->children(), |
| 109 | can_cross_incognito); |
| 110 | } |
| 111 | |
| 112 | // Now add our item(s) to the menu_model_. |
| 113 | if (submenu_items.empty()) { |
| 114 | menu_model_->AddItem(menu_id, title); |
| 115 | } else { |
| 116 | ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 117 | extension_menu_models_.push_back(base::WrapUnique(submenu)); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 118 | menu_model_->AddSubMenu(menu_id, title, submenu); |
limasdf | 7e955d2 | 2015-12-15 05:17:39 | [diff] [blame] | 119 | RecursivelyAppendExtensionItems(submenu_items, can_cross_incognito, |
| 120 | selection_text, submenu, index, |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 121 | false); // is_action_menu_top_level |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 122 | } |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 123 | if (!is_action_menu) |
| 124 | SetExtensionIcon(extension_key.extension_id); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | void ContextMenuMatcher::Clear() { |
| 129 | extension_item_map_.clear(); |
| 130 | extension_menu_models_.clear(); |
| 131 | } |
| 132 | |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 133 | base::string16 ContextMenuMatcher::GetTopLevelContextMenuTitle( |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 134 | const MenuItem::ExtensionKey& extension_key, |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 135 | const base::string16& selection_text) { |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 136 | const Extension* extension = NULL; |
| 137 | MenuItem::List items; |
| 138 | bool can_cross_incognito; |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 139 | GetRelevantExtensionTopLevelItems( |
[email protected] | fc103da | 2014-08-16 01:09:32 | [diff] [blame] | 140 | extension_key, &extension, &can_cross_incognito, &items); |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 141 | |
| 142 | base::string16 title; |
| 143 | |
| 144 | if (items.empty() || |
| 145 | items.size() > 1 || |
| 146 | items[0]->type() != MenuItem::NORMAL) { |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 147 | title = base::UTF8ToUTF16(extension->name()); |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 148 | } else { |
| 149 | MenuItem* item = items[0]; |
| 150 | title = item->TitleWithReplacement( |
| 151 | selection_text, kMaxExtensionItemTitleLength); |
| 152 | } |
| 153 | return title; |
| 154 | } |
| 155 | |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 156 | bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { |
| 157 | MenuItem* item = GetExtensionMenuItem(command_id); |
| 158 | if (!item) |
| 159 | return false; |
| 160 | return item->checked(); |
| 161 | } |
| 162 | |
| 163 | bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { |
| 164 | MenuItem* item = GetExtensionMenuItem(command_id); |
| 165 | if (!item) |
| 166 | return true; |
| 167 | return item->enabled(); |
| 168 | } |
| 169 | |
rob | cbe35ba | 2016-03-10 01:20:49 | [diff] [blame] | 170 | void ContextMenuMatcher::ExecuteCommand( |
| 171 | int command_id, |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 172 | content::WebContents* web_contents, |
rob | cbe35ba | 2016-03-10 01:20:49 | [diff] [blame] | 173 | content::RenderFrameHost* render_frame_host, |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 174 | const content::ContextMenuParams& params) { |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 175 | MenuItem* item = GetExtensionMenuItem(command_id); |
| 176 | if (!item) |
| 177 | return; |
| 178 | |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 179 | MenuManager* manager = MenuManager::Get(browser_context_); |
rob | cbe35ba | 2016-03-10 01:20:49 | [diff] [blame] | 180 | manager->ExecuteCommand(browser_context_, web_contents, render_frame_host, |
| 181 | params, item->id()); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 184 | bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 185 | const MenuItem::ExtensionKey& extension_key, |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 186 | const Extension** extension, |
| 187 | bool* can_cross_incognito, |
[email protected] | fc103da | 2014-08-16 01:09:32 | [diff] [blame] | 188 | MenuItem::List* items) { |
| 189 | *extension = ExtensionRegistry::Get( |
| 190 | browser_context_)->enabled_extensions().GetByID( |
| 191 | extension_key.extension_id); |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 192 | if (!*extension) |
| 193 | return false; |
| 194 | |
| 195 | // Find matching items. |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 196 | MenuManager* manager = MenuManager::Get(browser_context_); |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 197 | const MenuItem::OwnedList* all_items = manager->MenuItems(extension_key); |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 198 | if (!all_items || all_items->empty()) |
| 199 | return false; |
| 200 | |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 201 | *can_cross_incognito = util::CanCrossIncognito(*extension, browser_context_); |
[email protected] | fc103da | 2014-08-16 01:09:32 | [diff] [blame] | 202 | *items = GetRelevantExtensionItems(*all_items, *can_cross_incognito); |
[email protected] | 0ea8fac | 2013-06-12 15:31:35 | [diff] [blame] | 203 | |
| 204 | return true; |
| 205 | } |
| 206 | |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 207 | MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 208 | const MenuItem::OwnedList& items, |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 209 | bool can_cross_incognito) { |
| 210 | MenuItem::List result; |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 211 | for (auto i = items.begin(); i != items.end(); ++i) { |
| 212 | MenuItem* item = i->get(); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 213 | |
| 214 | if (!filter_.Run(item)) |
| 215 | continue; |
| 216 | |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 217 | if (item->id().incognito == browser_context_->IsOffTheRecord() || |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 218 | can_cross_incognito) |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 219 | result.push_back(item); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 220 | } |
| 221 | return result; |
| 222 | } |
| 223 | |
| 224 | void ContextMenuMatcher::RecursivelyAppendExtensionItems( |
| 225 | const MenuItem::List& items, |
| 226 | bool can_cross_incognito, |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 227 | const base::string16& selection_text, |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 228 | ui::SimpleMenuModel* menu_model, |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 229 | int* index, |
| 230 | bool is_action_menu_top_level) { |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 231 | MenuItem::Type last_type = MenuItem::NORMAL; |
| 232 | int radio_group_id = 1; |
Dave Schuyler | e83bf88 | 2017-08-22 18:50:04 | [diff] [blame] | 233 | int num_items = 0; |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 234 | |
avi | 5d5b7e9 | 2016-10-21 01:11:40 | [diff] [blame] | 235 | for (auto i = items.begin(); i != items.end(); ++i) { |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 236 | MenuItem* item = *i; |
| 237 | |
| 238 | // If last item was of type radio but the current one isn't, auto-insert |
| 239 | // a separator. The converse case is handled below. |
| 240 | if (last_type == MenuItem::RADIO && |
| 241 | item->type() != MenuItem::RADIO) { |
| 242 | menu_model->AddSeparator(ui::NORMAL_SEPARATOR); |
| 243 | last_type = MenuItem::SEPARATOR; |
| 244 | } |
| 245 | |
[email protected] | a146532b | 2014-07-30 11:20:09 | [diff] [blame] | 246 | int menu_id = ConvertToExtensionsCustomCommandId(*index); |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 247 | // Action context menus have a limit for top level extension items to |
| 248 | // prevent control items from being pushed off the screen, since extension |
| 249 | // items will not be placed in a submenu. |
[email protected] | fc103da | 2014-08-16 01:09:32 | [diff] [blame] | 250 | const int top_level_limit = api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT; |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 251 | if (menu_id >= extensions_context_custom_last || |
Dave Schuyler | e83bf88 | 2017-08-22 18:50:04 | [diff] [blame] | 252 | (is_action_menu_top_level && num_items >= top_level_limit)) |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 253 | return; |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 254 | |
lazyboy | 413226da | 2015-05-14 22:18:20 | [diff] [blame] | 255 | ++(*index); |
Dave Schuyler | e83bf88 | 2017-08-22 18:50:04 | [diff] [blame] | 256 | ++num_items; |
lazyboy | 413226da | 2015-05-14 22:18:20 | [diff] [blame] | 257 | |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 258 | extension_item_map_[menu_id] = item->id(); |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 259 | base::string16 title = item->TitleWithReplacement(selection_text, |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 260 | kMaxExtensionItemTitleLength); |
| 261 | if (item->type() == MenuItem::NORMAL) { |
| 262 | MenuItem::List children = |
| 263 | GetRelevantExtensionItems(item->children(), can_cross_incognito); |
| 264 | if (children.empty()) { |
| 265 | menu_model->AddItem(menu_id, title); |
| 266 | } else { |
| 267 | ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 268 | extension_menu_models_.push_back(base::WrapUnique(submenu)); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 269 | menu_model->AddSubMenu(menu_id, title, submenu); |
limasdf | 7e955d2 | 2015-12-15 05:17:39 | [diff] [blame] | 270 | RecursivelyAppendExtensionItems(children, can_cross_incognito, |
| 271 | selection_text, submenu, index, |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 272 | false); // is_action_menu_top_level |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 273 | } |
| 274 | } else if (item->type() == MenuItem::CHECKBOX) { |
| 275 | menu_model->AddCheckItem(menu_id, title); |
| 276 | } else if (item->type() == MenuItem::RADIO) { |
| 277 | if (i != items.begin() && |
| 278 | last_type != MenuItem::RADIO) { |
| 279 | radio_group_id++; |
| 280 | |
| 281 | // Auto-append a separator if needed. |
[email protected] | 00491c05 | 2013-02-08 10:53:25 | [diff] [blame] | 282 | menu_model->AddSeparator(ui::NORMAL_SEPARATOR); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | menu_model->AddRadioItem(menu_id, title, radio_group_id); |
| 286 | } else if (item->type() == MenuItem::SEPARATOR) { |
[email protected] | 00491c05 | 2013-02-08 10:53:25 | [diff] [blame] | 287 | menu_model->AddSeparator(ui::NORMAL_SEPARATOR); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 288 | } |
| 289 | last_type = item->type(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | MenuItem* ContextMenuMatcher::GetExtensionMenuItem(int id) const { |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 294 | MenuManager* manager = MenuManager::Get(browser_context_); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 295 | std::map<int, MenuItem::Id>::const_iterator i = |
| 296 | extension_item_map_.find(id); |
| 297 | if (i != extension_item_map_.end()) { |
| 298 | MenuItem* item = manager->GetItemById(i->second); |
| 299 | if (item) |
| 300 | return item; |
| 301 | } |
| 302 | return NULL; |
| 303 | } |
| 304 | |
| 305 | void ContextMenuMatcher::SetExtensionIcon(const std::string& extension_id) { |
[email protected] | f5fede0 | 2014-07-29 02:48:21 | [diff] [blame] | 306 | MenuManager* menu_manager = MenuManager::Get(browser_context_); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 307 | |
| 308 | int index = menu_model_->GetItemCount() - 1; |
| 309 | DCHECK_GE(index, 0); |
| 310 | |
estade | 32426e0 | 2016-12-18 01:26:17 | [diff] [blame] | 311 | gfx::Image icon = menu_manager->GetIconForExtension(extension_id); |
| 312 | DCHECK_EQ(gfx::kFaviconSize, icon.Width()); |
| 313 | DCHECK_EQ(gfx::kFaviconSize, icon.Height()); |
| 314 | menu_model_->SetIcon(index, icon); |
[email protected] | 4f8a4d1 | 2012-09-28 19:23:09 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | } // namespace extensions |