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/strings/utf_string_conversions.h" | 5 #include "base/strings/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_util.h" | 9 #include "chrome/browser/extensions/extension_util.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 ContextMenuMatcher::ContextMenuMatcher( | 21 ContextMenuMatcher::ContextMenuMatcher( |
22 Profile* profile, | 22 Profile* profile, |
23 ui::SimpleMenuModel::Delegate* delegate, | 23 ui::SimpleMenuModel::Delegate* delegate, |
24 ui::SimpleMenuModel* menu_model, | 24 ui::SimpleMenuModel* menu_model, |
25 const base::Callback<bool(const MenuItem*)>& filter) | 25 const base::Callback<bool(const MenuItem*)>& filter) |
26 : profile_(profile), menu_model_(menu_model), delegate_(delegate), | 26 : profile_(profile), menu_model_(menu_model), delegate_(delegate), |
27 filter_(filter) { | 27 filter_(filter) { |
28 } | 28 } |
29 | 29 |
30 void ContextMenuMatcher::AppendExtensionItems( | 30 void ContextMenuMatcher::AppendExtensionItems( |
31 const std::string& extension_id, | 31 const MenuItem::ExtensionKey& extension_key, |
32 const base::string16& selection_text, | 32 const base::string16& selection_text, |
33 int* index) { | 33 int* index) { |
34 DCHECK_GE(*index, 0); | 34 DCHECK_GE(*index, 0); |
35 int max_index = | 35 int max_index = |
36 IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 36 IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
37 if (*index >= max_index) | 37 if (*index >= max_index) |
38 return; | 38 return; |
39 | 39 |
40 const Extension* extension = NULL; | 40 const Extension* extension = NULL; |
41 MenuItem::List items; | 41 MenuItem::List items; |
42 bool can_cross_incognito; | 42 bool can_cross_incognito; |
43 if (!GetRelevantExtensionTopLevelItems(extension_id, &extension, | 43 if (!GetRelevantExtensionTopLevelItems( |
44 &can_cross_incognito, items)) | 44 extension_key, &extension, &can_cross_incognito, items)) |
45 return; | 45 return; |
46 | 46 |
47 if (items.empty()) | 47 if (items.empty()) |
48 return; | 48 return; |
49 | 49 |
50 // If this is the first extension-provided menu item, and there are other | 50 // If this is the first extension-provided menu item, and there are other |
51 // items in the menu, and the last item is not a separator add a separator. | 51 // items in the menu, and the last item is not a separator add a separator. |
52 if (*index == 0 && menu_model_->GetItemCount()) | 52 if (*index == 0 && menu_model_->GetItemCount()) |
53 menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 53 menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
54 | 54 |
(...skipping 24 matching lines...) Expand all Loading... |
79 // Now add our item(s) to the menu_model_. | 79 // Now add our item(s) to the menu_model_. |
80 if (submenu_items.empty()) { | 80 if (submenu_items.empty()) { |
81 menu_model_->AddItem(menu_id, title); | 81 menu_model_->AddItem(menu_id, title); |
82 } else { | 82 } else { |
83 ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_); | 83 ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_); |
84 extension_menu_models_.push_back(submenu); | 84 extension_menu_models_.push_back(submenu); |
85 menu_model_->AddSubMenu(menu_id, title, submenu); | 85 menu_model_->AddSubMenu(menu_id, title, submenu); |
86 RecursivelyAppendExtensionItems(submenu_items, can_cross_incognito, | 86 RecursivelyAppendExtensionItems(submenu_items, can_cross_incognito, |
87 selection_text, submenu, index); | 87 selection_text, submenu, index); |
88 } | 88 } |
89 SetExtensionIcon(extension_id); | 89 SetExtensionIcon(extension_key.extension_id); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 void ContextMenuMatcher::Clear() { | 93 void ContextMenuMatcher::Clear() { |
94 extension_item_map_.clear(); | 94 extension_item_map_.clear(); |
95 extension_menu_models_.clear(); | 95 extension_menu_models_.clear(); |
96 } | 96 } |
97 | 97 |
98 base::string16 ContextMenuMatcher::GetTopLevelContextMenuTitle( | 98 base::string16 ContextMenuMatcher::GetTopLevelContextMenuTitle( |
99 const std::string& extension_id, | 99 const MenuItem::ExtensionKey& extension_key, |
100 const base::string16& selection_text) { | 100 const base::string16& selection_text) { |
101 const Extension* extension = NULL; | 101 const Extension* extension = NULL; |
102 MenuItem::List items; | 102 MenuItem::List items; |
103 bool can_cross_incognito; | 103 bool can_cross_incognito; |
104 GetRelevantExtensionTopLevelItems(extension_id, &extension, | 104 GetRelevantExtensionTopLevelItems( |
105 &can_cross_incognito, items); | 105 extension_key, &extension, &can_cross_incognito, items); |
106 | 106 |
107 base::string16 title; | 107 base::string16 title; |
108 | 108 |
109 if (items.empty() || | 109 if (items.empty() || |
110 items.size() > 1 || | 110 items.size() > 1 || |
111 items[0]->type() != MenuItem::NORMAL) { | 111 items[0]->type() != MenuItem::NORMAL) { |
112 title = base::UTF8ToUTF16(extension->name()); | 112 title = base::UTF8ToUTF16(extension->name()); |
113 } else { | 113 } else { |
114 MenuItem* item = items[0]; | 114 MenuItem* item = items[0]; |
115 title = item->TitleWithReplacement( | 115 title = item->TitleWithReplacement( |
(...skipping 21 matching lines...) Expand all Loading... |
137 const content::ContextMenuParams& params) { | 137 const content::ContextMenuParams& params) { |
138 MenuItem* item = GetExtensionMenuItem(command_id); | 138 MenuItem* item = GetExtensionMenuItem(command_id); |
139 if (!item) | 139 if (!item) |
140 return; | 140 return; |
141 | 141 |
142 MenuManager* manager = MenuManager::Get(profile_); | 142 MenuManager* manager = MenuManager::Get(profile_); |
143 manager->ExecuteCommand(profile_, web_contents, params, item->id()); | 143 manager->ExecuteCommand(profile_, web_contents, params, item->id()); |
144 } | 144 } |
145 | 145 |
146 bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( | 146 bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( |
147 const std::string& extension_id, | 147 const MenuItem::ExtensionKey& extension_key, |
148 const Extension** extension, | 148 const Extension** extension, |
149 bool* can_cross_incognito, | 149 bool* can_cross_incognito, |
150 MenuItem::List& items) { | 150 MenuItem::List& items) { |
151 ExtensionService* service = | 151 ExtensionService* service = |
152 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 152 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
153 *extension = service->GetExtensionById(extension_id, false); | 153 *extension = service->GetExtensionById(extension_key.extension_id, false); |
154 | 154 |
155 if (!*extension) | 155 if (!*extension) |
156 return false; | 156 return false; |
157 | 157 |
158 // Find matching items. | 158 // Find matching items. |
159 MenuManager* manager = MenuManager::Get(profile_); | 159 MenuManager* manager = MenuManager::Get(profile_); |
160 const MenuItem::List* all_items = manager->MenuItems(extension_id); | 160 const MenuItem::List* all_items = manager->MenuItems(extension_key); |
161 if (!all_items || all_items->empty()) | 161 if (!all_items || all_items->empty()) |
162 return false; | 162 return false; |
163 | 163 |
164 *can_cross_incognito = util::CanCrossIncognito(*extension, profile_); | 164 *can_cross_incognito = util::CanCrossIncognito(*extension, profile_); |
165 items = GetRelevantExtensionItems(*all_items, | 165 items = GetRelevantExtensionItems(*all_items, |
166 *can_cross_incognito); | 166 *can_cross_incognito); |
167 | 167 |
168 return true; | 168 return true; |
169 } | 169 } |
170 | 170 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 DCHECK_GE(index, 0); | 264 DCHECK_GE(index, 0); |
265 | 265 |
266 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); | 266 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); |
267 DCHECK(icon.width() == gfx::kFaviconSize); | 267 DCHECK(icon.width() == gfx::kFaviconSize); |
268 DCHECK(icon.height() == gfx::kFaviconSize); | 268 DCHECK(icon.height() == gfx::kFaviconSize); |
269 | 269 |
270 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); | 270 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); |
271 } | 271 } |
272 | 272 |
273 } // namespace extensions | 273 } // namespace extensions |
OLD | NEW |