| OLD | NEW |
| (Empty) | |
| 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 |
| 5 #include "base/utf_string_conversions.h" |
| 6 #include "chrome/app/chrome_command_ids.h" |
| 7 #include "chrome/browser/extensions/context_menu_matcher.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "content/public/common/context_menu_params.h" |
| 11 #include "ui/gfx/favicon_size.h" |
| 12 |
| 13 namespace extensions { |
| 14 |
| 15 // static |
| 16 const size_t ContextMenuMatcher::kMaxExtensionItemTitleLength = 75; |
| 17 |
| 18 ContextMenuMatcher::ContextMenuMatcher( |
| 19 Profile* profile, |
| 20 ui::SimpleMenuModel::Delegate* delegate, |
| 21 ui::SimpleMenuModel* menu_model, |
| 22 const base::Callback<bool(const MenuItem*)>& filter) |
| 23 : profile_(profile), menu_model_(menu_model), delegate_(delegate), |
| 24 filter_(filter) { |
| 25 } |
| 26 |
| 27 void ContextMenuMatcher::AppendExtensionItems(const std::string& extension_id, |
| 28 const string16& selection_text, |
| 29 int* index) |
| 30 { |
| 31 ExtensionService* service = profile_->GetExtensionService(); |
| 32 MenuManager* manager = service->menu_manager(); |
| 33 const Extension* extension = service->GetExtensionById(extension_id, false); |
| 34 DCHECK_GE(*index, 0); |
| 35 int max_index = |
| 36 IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
| 37 if (!extension || *index >= max_index) |
| 38 return; |
| 39 |
| 40 // Find matching items. |
| 41 const MenuItem::List* all_items = manager->MenuItems(extension_id); |
| 42 if (!all_items || all_items->empty()) |
| 43 return; |
| 44 bool can_cross_incognito = service->CanCrossIncognito(extension); |
| 45 MenuItem::List items = GetRelevantExtensionItems(*all_items, |
| 46 can_cross_incognito); |
| 47 |
| 48 if (items.empty()) |
| 49 return; |
| 50 |
| 51 // If this is the first extension-provided menu item, and there are other |
| 52 // items in the menu, add a separator. |
| 53 if (*index == 0 && menu_model_->GetItemCount()) |
| 54 menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
| 55 |
| 56 // Extensions (other than platform apps) are only allowed one top-level slot |
| 57 // (and it can't be a radio or checkbox item because we are going to put the |
| 58 // extension icon next to it). |
| 59 // If they have more than that, we automatically push them into a submenu. |
| 60 if (extension->is_platform_app()) { |
| 61 RecursivelyAppendExtensionItems(items, can_cross_incognito, selection_text, |
| 62 menu_model_, index); |
| 63 } else { |
| 64 int menu_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + (*index)++; |
| 65 string16 title; |
| 66 MenuItem::List submenu_items; |
| 67 |
| 68 if (items.size() > 1 || items[0]->type() != MenuItem::NORMAL) { |
| 69 title = UTF8ToUTF16(extension->name()); |
| 70 submenu_items = items; |
| 71 } else { |
| 72 MenuItem* item = items[0]; |
| 73 extension_item_map_[menu_id] = item->id(); |
| 74 title = item->TitleWithReplacement(selection_text, |
| 75 kMaxExtensionItemTitleLength); |
| 76 submenu_items = GetRelevantExtensionItems(item->children(), |
| 77 can_cross_incognito); |
| 78 } |
| 79 |
| 80 // Now add our item(s) to the menu_model_. |
| 81 if (submenu_items.empty()) { |
| 82 menu_model_->AddItem(menu_id, title); |
| 83 } else { |
| 84 ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_); |
| 85 extension_menu_models_.push_back(submenu); |
| 86 menu_model_->AddSubMenu(menu_id, title, submenu); |
| 87 RecursivelyAppendExtensionItems(submenu_items, can_cross_incognito, |
| 88 selection_text, submenu, index); |
| 89 } |
| 90 SetExtensionIcon(extension_id); |
| 91 } |
| 92 } |
| 93 |
| 94 void ContextMenuMatcher::Clear() { |
| 95 extension_item_map_.clear(); |
| 96 extension_menu_models_.clear(); |
| 97 } |
| 98 |
| 99 bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { |
| 100 MenuItem* item = GetExtensionMenuItem(command_id); |
| 101 if (!item) |
| 102 return false; |
| 103 return item->checked(); |
| 104 } |
| 105 |
| 106 bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { |
| 107 MenuItem* item = GetExtensionMenuItem(command_id); |
| 108 if (!item) |
| 109 return true; |
| 110 return item->enabled(); |
| 111 } |
| 112 |
| 113 void ContextMenuMatcher::ExecuteCommand(int command_id, |
| 114 const content::ContextMenuParams& params) { |
| 115 MenuManager* manager = profile_->GetExtensionService()->menu_manager(); |
| 116 MenuItem* item = GetExtensionMenuItem(command_id); |
| 117 if (!item) |
| 118 return; |
| 119 |
| 120 manager->ExecuteCommand(profile_, NULL, params, item->id()); |
| 121 } |
| 122 |
| 123 MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( |
| 124 const MenuItem::List& items, |
| 125 bool can_cross_incognito) { |
| 126 MenuItem::List result; |
| 127 for (MenuItem::List::const_iterator i = items.begin(); |
| 128 i != items.end(); ++i) { |
| 129 const MenuItem* item = *i; |
| 130 |
| 131 if (!filter_.Run(item)) |
| 132 continue; |
| 133 |
| 134 if (item->id().incognito == profile_->IsOffTheRecord() || |
| 135 can_cross_incognito) |
| 136 result.push_back(*i); |
| 137 } |
| 138 return result; |
| 139 } |
| 140 |
| 141 void ContextMenuMatcher::RecursivelyAppendExtensionItems( |
| 142 const MenuItem::List& items, |
| 143 bool can_cross_incognito, |
| 144 const string16& selection_text, |
| 145 ui::SimpleMenuModel* menu_model, |
| 146 int* index) |
| 147 { |
| 148 MenuItem::Type last_type = MenuItem::NORMAL; |
| 149 int radio_group_id = 1; |
| 150 |
| 151 for (MenuItem::List::const_iterator i = items.begin(); |
| 152 i != items.end(); ++i) { |
| 153 MenuItem* item = *i; |
| 154 |
| 155 // If last item was of type radio but the current one isn't, auto-insert |
| 156 // a separator. The converse case is handled below. |
| 157 if (last_type == MenuItem::RADIO && |
| 158 item->type() != MenuItem::RADIO) { |
| 159 menu_model->AddSeparator(ui::NORMAL_SEPARATOR); |
| 160 last_type = MenuItem::SEPARATOR; |
| 161 } |
| 162 |
| 163 int menu_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + (*index)++; |
| 164 if (menu_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) |
| 165 return; |
| 166 extension_item_map_[menu_id] = item->id(); |
| 167 string16 title = item->TitleWithReplacement(selection_text, |
| 168 kMaxExtensionItemTitleLength); |
| 169 if (item->type() == MenuItem::NORMAL) { |
| 170 MenuItem::List children = |
| 171 GetRelevantExtensionItems(item->children(), can_cross_incognito); |
| 172 if (children.empty()) { |
| 173 menu_model->AddItem(menu_id, title); |
| 174 } else { |
| 175 ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_); |
| 176 extension_menu_models_.push_back(submenu); |
| 177 menu_model->AddSubMenu(menu_id, title, submenu); |
| 178 RecursivelyAppendExtensionItems(children, can_cross_incognito, |
| 179 selection_text, submenu, index); |
| 180 } |
| 181 } else if (item->type() == MenuItem::CHECKBOX) { |
| 182 menu_model->AddCheckItem(menu_id, title); |
| 183 } else if (item->type() == MenuItem::RADIO) { |
| 184 if (i != items.begin() && |
| 185 last_type != MenuItem::RADIO) { |
| 186 radio_group_id++; |
| 187 |
| 188 // Auto-append a separator if needed. |
| 189 if (last_type != MenuItem::SEPARATOR) |
| 190 menu_model->AddSeparator(ui::NORMAL_SEPARATOR); |
| 191 } |
| 192 |
| 193 menu_model->AddRadioItem(menu_id, title, radio_group_id); |
| 194 } else if (item->type() == MenuItem::SEPARATOR) { |
| 195 if (i != items.begin() && last_type != MenuItem::SEPARATOR) { |
| 196 menu_model->AddSeparator(ui::NORMAL_SEPARATOR); |
| 197 } |
| 198 } |
| 199 last_type = item->type(); |
| 200 } |
| 201 } |
| 202 |
| 203 MenuItem* ContextMenuMatcher::GetExtensionMenuItem(int id) const { |
| 204 MenuManager* manager = profile_->GetExtensionService()->menu_manager(); |
| 205 std::map<int, MenuItem::Id>::const_iterator i = |
| 206 extension_item_map_.find(id); |
| 207 if (i != extension_item_map_.end()) { |
| 208 MenuItem* item = manager->GetItemById(i->second); |
| 209 if (item) |
| 210 return item; |
| 211 } |
| 212 return NULL; |
| 213 } |
| 214 |
| 215 void ContextMenuMatcher::SetExtensionIcon(const std::string& extension_id) { |
| 216 ExtensionService* service = profile_->GetExtensionService(); |
| 217 MenuManager* menu_manager = service->menu_manager(); |
| 218 |
| 219 int index = menu_model_->GetItemCount() - 1; |
| 220 DCHECK_GE(index, 0); |
| 221 |
| 222 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); |
| 223 DCHECK(icon.width() == gfx::kFaviconSize); |
| 224 DCHECK(icon.height() == gfx::kFaviconSize); |
| 225 |
| 226 menu_model_->SetIcon(index, gfx::Image(icon)); |
| 227 } |
| 228 |
| 229 } // namespace extensions |
| OLD | NEW |