Chromium Code Reviews| 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 #include "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 #include "chrome/app/chrome_command_ids.h" | 6 #include "chrome/app/chrome_command_ids.h" |
| 7 #include "chrome/browser/extensions/context_menu_matcher.h" | 7 #include "chrome/browser/extensions/context_menu_matcher.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" | 9 #include "chrome/browser/extensions/extension_system.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 ui::SimpleMenuModel* menu_model, | 23 ui::SimpleMenuModel* menu_model, |
| 24 const base::Callback<bool(const MenuItem*)>& filter) | 24 const base::Callback<bool(const MenuItem*)>& filter) |
| 25 : profile_(profile), menu_model_(menu_model), delegate_(delegate), | 25 : profile_(profile), menu_model_(menu_model), delegate_(delegate), |
| 26 filter_(filter) { | 26 filter_(filter) { |
| 27 } | 27 } |
| 28 | 28 |
| 29 void ContextMenuMatcher::AppendExtensionItems(const std::string& extension_id, | 29 void ContextMenuMatcher::AppendExtensionItems(const std::string& extension_id, |
| 30 const string16& selection_text, | 30 const string16& selection_text, |
| 31 int* index) | 31 int* index) |
| 32 { | 32 { |
| 33 ExtensionService* service = | |
| 34 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
| 35 MenuManager* manager = service->menu_manager(); | |
| 36 const Extension* extension = service->GetExtensionById(extension_id, false); | |
| 37 DCHECK_GE(*index, 0); | 33 DCHECK_GE(*index, 0); |
| 38 int max_index = | 34 int max_index = |
| 39 IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 35 IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
| 40 if (!extension || *index >= max_index) | 36 if (*index >= max_index) |
| 41 return; | 37 return; |
| 42 | 38 |
| 43 // Find matching items. | 39 const Extension* extension = NULL; |
| 44 const MenuItem::List* all_items = manager->MenuItems(extension_id); | 40 MenuItem::List items; |
| 45 if (!all_items || all_items->empty()) | 41 bool can_cross_incognito; |
| 42 if (!GetRelevantExtensionTopLevelItems(extension_id, &extension, | |
| 43 &can_cross_incognito, items)) | |
| 46 return; | 44 return; |
| 47 bool can_cross_incognito = service->CanCrossIncognito(extension); | |
| 48 MenuItem::List items = GetRelevantExtensionItems(*all_items, | |
| 49 can_cross_incognito); | |
| 50 | 45 |
| 51 if (items.empty()) | 46 if (items.empty()) |
| 52 return; | 47 return; |
| 53 | 48 |
| 54 // If this is the first extension-provided menu item, and there are other | 49 // If this is the first extension-provided menu item, and there are other |
| 55 // items in the menu, and the last item is not a separator add a separator. | 50 // items in the menu, and the last item is not a separator add a separator. |
| 56 if (*index == 0 && menu_model_->GetItemCount()) | 51 if (*index == 0 && menu_model_->GetItemCount()) |
| 57 menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 52 menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
| 58 | 53 |
| 59 // Extensions (other than platform apps) are only allowed one top-level slot | 54 // Extensions (other than platform apps) are only allowed one top-level slot |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 } | 87 } |
| 93 SetExtensionIcon(extension_id); | 88 SetExtensionIcon(extension_id); |
| 94 } | 89 } |
| 95 } | 90 } |
| 96 | 91 |
| 97 void ContextMenuMatcher::Clear() { | 92 void ContextMenuMatcher::Clear() { |
| 98 extension_item_map_.clear(); | 93 extension_item_map_.clear(); |
| 99 extension_menu_models_.clear(); | 94 extension_menu_models_.clear(); |
| 100 } | 95 } |
| 101 | 96 |
| 97 base::string16 ContextMenuMatcher::GetTopLevelContextMenuTitle( | |
| 98 const std::string& extension_id, | |
| 99 const string16& selection_text) { | |
| 100 const Extension* extension = NULL; | |
| 101 MenuItem::List items; | |
| 102 bool can_cross_incognito; | |
| 103 GetRelevantExtensionTopLevelItems(extension_id, &extension, | |
| 104 &can_cross_incognito, items); | |
| 105 | |
| 106 base::string16 title; | |
| 107 | |
| 108 if (items.empty() || | |
| 109 items.size() > 1 || | |
| 110 items[0]->type() != MenuItem::NORMAL) { | |
| 111 title = UTF8ToUTF16(extension->name()); | |
| 112 } else { | |
| 113 MenuItem* item = items[0]; | |
| 114 title = item->TitleWithReplacement( | |
| 115 selection_text, kMaxExtensionItemTitleLength); | |
| 116 } | |
| 117 return title; | |
| 118 } | |
| 119 | |
| 102 bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { | 120 bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { |
| 103 MenuItem* item = GetExtensionMenuItem(command_id); | 121 MenuItem* item = GetExtensionMenuItem(command_id); |
| 104 if (!item) | 122 if (!item) |
| 105 return false; | 123 return false; |
| 106 return item->checked(); | 124 return item->checked(); |
| 107 } | 125 } |
| 108 | 126 |
| 109 bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { | 127 bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { |
| 110 MenuItem* item = GetExtensionMenuItem(command_id); | 128 MenuItem* item = GetExtensionMenuItem(command_id); |
| 111 if (!item) | 129 if (!item) |
| 112 return true; | 130 return true; |
| 113 return item->enabled(); | 131 return item->enabled(); |
| 114 } | 132 } |
| 115 | 133 |
| 116 void ContextMenuMatcher::ExecuteCommand(int command_id, | 134 void ContextMenuMatcher::ExecuteCommand(int command_id, |
| 117 content::WebContents* web_contents, | 135 content::WebContents* web_contents, |
| 118 const content::ContextMenuParams& params) { | 136 const content::ContextMenuParams& params) { |
| 119 MenuManager* manager = extensions::ExtensionSystem::Get(profile_)-> | 137 MenuManager* manager = extensions::ExtensionSystem::Get(profile_)-> |
| 120 extension_service()->menu_manager(); | 138 extension_service()->menu_manager(); |
| 121 MenuItem* item = GetExtensionMenuItem(command_id); | 139 MenuItem* item = GetExtensionMenuItem(command_id); |
| 122 if (!item) | 140 if (!item) |
| 123 return; | 141 return; |
| 124 | 142 |
| 125 manager->ExecuteCommand(profile_, web_contents, params, item->id()); | 143 manager->ExecuteCommand(profile_, web_contents, params, item->id()); |
| 126 } | 144 } |
| 127 | 145 |
| 146 bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( | |
| 147 const std::string& extension_id, | |
| 148 const Extension** extension, | |
| 149 bool* can_cross_incognito, | |
| 150 MenuItem::List& items) { | |
| 151 ExtensionService* service = | |
| 152 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
| 153 MenuManager* manager = service->menu_manager(); | |
| 154 *extension = service->GetExtensionById(extension_id, false); | |
| 155 | |
| 156 if (!*extension) | |
| 157 return false; | |
| 158 | |
| 159 // Find matching items. | |
| 160 const MenuItem::List* all_items = manager->MenuItems(extension_id); | |
| 161 if (!all_items || all_items->empty()) | |
| 162 return false; | |
| 163 | |
| 164 *can_cross_incognito = service->CanCrossIncognito(*extension); | |
| 165 items = GetRelevantExtensionItems(*all_items, | |
| 166 can_cross_incognito); | |
|
Avi (use Gerrit)
2013/06/11 14:14:17
Oh, wow. I think that caught a bug.
GetRelevantEx
François Beaufort
2013/06/11 14:41:22
I have no excuse here.
I'm going to whip myself fo
Avi (use Gerrit)
2013/06/11 15:39:57
I will accept a new patch in lieu of hara kiri.
| |
| 167 | |
| 168 return true; | |
| 169 } | |
| 170 | |
| 128 MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( | 171 MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( |
| 129 const MenuItem::List& items, | 172 const MenuItem::List& items, |
| 130 bool can_cross_incognito) { | 173 bool can_cross_incognito) { |
| 131 MenuItem::List result; | 174 MenuItem::List result; |
| 132 for (MenuItem::List::const_iterator i = items.begin(); | 175 for (MenuItem::List::const_iterator i = items.begin(); |
| 133 i != items.end(); ++i) { | 176 i != items.end(); ++i) { |
| 134 const MenuItem* item = *i; | 177 const MenuItem* item = *i; |
| 135 | 178 |
| 136 if (!filter_.Run(item)) | 179 if (!filter_.Run(item)) |
| 137 continue; | 180 continue; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 DCHECK_GE(index, 0); | 267 DCHECK_GE(index, 0); |
| 225 | 268 |
| 226 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); | 269 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); |
| 227 DCHECK(icon.width() == gfx::kFaviconSize); | 270 DCHECK(icon.width() == gfx::kFaviconSize); |
| 228 DCHECK(icon.height() == gfx::kFaviconSize); | 271 DCHECK(icon.height() == gfx::kFaviconSize); |
| 229 | 272 |
| 230 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); | 273 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); |
| 231 } | 274 } |
| 232 | 275 |
| 233 } // namespace extensions | 276 } // namespace extensions |
| OLD | NEW |