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 "chrome/browser/extensions/context_menu_matcher.h" | 5 #include "chrome/browser/extensions/context_menu_matcher.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
Devlin
2014/08/14 21:39:49
if you remove the service I commented on, don't ne
Lei Zhang
2014/08/14 21:47:14
Done.
| |
10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
11 #include "chrome/common/extensions/api/context_menus.h" | |
11 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
12 #include "content/public/common/context_menu_params.h" | 13 #include "content/public/common/context_menu_params.h" |
13 #include "extensions/browser/extension_system.h" | 14 #include "extensions/browser/extension_system.h" |
14 #include "ui/gfx/favicon_size.h" | 15 #include "ui/gfx/favicon_size.h" |
15 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
16 | 17 |
17 #if defined(ENABLE_EXTENSIONS) | |
18 #include "chrome/common/extensions/api/context_menus.h" | |
19 #endif | |
20 | |
21 namespace extensions { | 18 namespace extensions { |
22 | 19 |
23 namespace { | 20 namespace { |
24 | 21 |
25 int GetActionMenuTopLevelLimit() { | 22 int GetActionMenuTopLevelLimit() { |
Devlin
2014/08/14 21:39:50
could we just use ACTION_MENU_TOP_LEVEL_LIMIT dire
Lei Zhang
2014/08/14 21:47:14
Done.
| |
26 #if defined(ENABLE_EXTENSIONS) | |
27 return api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT; | 23 return api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT; |
28 #else | |
29 return 0; | |
30 #endif | |
31 } | 24 } |
32 | 25 |
33 // The range of command IDs reserved for extension's custom menus. | 26 // The range of command IDs reserved for extension's custom menus. |
34 // TODO(oshima): These values will be injected by embedders. | 27 // TODO(oshima): These values will be injected by embedders. |
35 int extensions_context_custom_first = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 28 int extensions_context_custom_first = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
36 int extensions_context_custom_last = IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST; | 29 int extensions_context_custom_last = IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST; |
37 | 30 |
38 } // namespace | 31 } // namespace |
39 | 32 |
40 // static | 33 // static |
(...skipping 29 matching lines...) Expand all Loading... | |
70 DCHECK_GE(*index, 0); | 63 DCHECK_GE(*index, 0); |
71 int max_index = | 64 int max_index = |
72 extensions_context_custom_last - extensions_context_custom_first; | 65 extensions_context_custom_last - extensions_context_custom_first; |
73 if (*index >= max_index) | 66 if (*index >= max_index) |
74 return; | 67 return; |
75 | 68 |
76 const Extension* extension = NULL; | 69 const Extension* extension = NULL; |
77 MenuItem::List items; | 70 MenuItem::List items; |
78 bool can_cross_incognito; | 71 bool can_cross_incognito; |
79 if (!GetRelevantExtensionTopLevelItems( | 72 if (!GetRelevantExtensionTopLevelItems( |
80 extension_key, &extension, &can_cross_incognito, items)) | 73 extension_key, &extension, &can_cross_incognito, &items)) |
81 return; | 74 return; |
82 | 75 |
83 if (items.empty()) | 76 if (items.empty()) |
84 return; | 77 return; |
85 | 78 |
86 // If this is the first extension-provided menu item, and there are other | 79 // If this is the first extension-provided menu item, and there are other |
87 // items in the menu, and the last item is not a separator add a separator. | 80 // items in the menu, and the last item is not a separator add a separator. |
88 if (*index == 0 && menu_model_->GetItemCount()) | 81 if (*index == 0 && menu_model_->GetItemCount()) |
89 menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 82 menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
90 | 83 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 extension_menu_models_.clear(); | 137 extension_menu_models_.clear(); |
145 } | 138 } |
146 | 139 |
147 base::string16 ContextMenuMatcher::GetTopLevelContextMenuTitle( | 140 base::string16 ContextMenuMatcher::GetTopLevelContextMenuTitle( |
148 const MenuItem::ExtensionKey& extension_key, | 141 const MenuItem::ExtensionKey& extension_key, |
149 const base::string16& selection_text) { | 142 const base::string16& selection_text) { |
150 const Extension* extension = NULL; | 143 const Extension* extension = NULL; |
151 MenuItem::List items; | 144 MenuItem::List items; |
152 bool can_cross_incognito; | 145 bool can_cross_incognito; |
153 GetRelevantExtensionTopLevelItems( | 146 GetRelevantExtensionTopLevelItems( |
154 extension_key, &extension, &can_cross_incognito, items); | 147 extension_key, &extension, &can_cross_incognito, &items); |
155 | 148 |
156 base::string16 title; | 149 base::string16 title; |
157 | 150 |
158 if (items.empty() || | 151 if (items.empty() || |
159 items.size() > 1 || | 152 items.size() > 1 || |
160 items[0]->type() != MenuItem::NORMAL) { | 153 items[0]->type() != MenuItem::NORMAL) { |
161 title = base::UTF8ToUTF16(extension->name()); | 154 title = base::UTF8ToUTF16(extension->name()); |
162 } else { | 155 } else { |
163 MenuItem* item = items[0]; | 156 MenuItem* item = items[0]; |
164 title = item->TitleWithReplacement( | 157 title = item->TitleWithReplacement( |
(...skipping 24 matching lines...) Expand all Loading... | |
189 return; | 182 return; |
190 | 183 |
191 MenuManager* manager = MenuManager::Get(browser_context_); | 184 MenuManager* manager = MenuManager::Get(browser_context_); |
192 manager->ExecuteCommand(browser_context_, web_contents, params, item->id()); | 185 manager->ExecuteCommand(browser_context_, web_contents, params, item->id()); |
193 } | 186 } |
194 | 187 |
195 bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( | 188 bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( |
196 const MenuItem::ExtensionKey& extension_key, | 189 const MenuItem::ExtensionKey& extension_key, |
197 const Extension** extension, | 190 const Extension** extension, |
198 bool* can_cross_incognito, | 191 bool* can_cross_incognito, |
199 MenuItem::List& items) { | 192 MenuItem::List* items) { |
200 ExtensionService* service = | 193 ExtensionService* service = |
201 extensions::ExtensionSystem::Get(browser_context_)->extension_service(); | 194 ExtensionSystem::Get(browser_context_)->extension_service(); |
202 *extension = service->GetExtensionById(extension_key.extension_id, false); | 195 *extension = service->GetExtensionById(extension_key.extension_id, false); |
Devlin
2014/08/14 21:39:49
Since you're hear, mind changing this to Extension
Lei Zhang
2014/08/14 21:47:14
Done.
| |
203 | 196 |
204 if (!*extension) | 197 if (!*extension) |
205 return false; | 198 return false; |
206 | 199 |
207 // Find matching items. | 200 // Find matching items. |
208 MenuManager* manager = MenuManager::Get(browser_context_); | 201 MenuManager* manager = MenuManager::Get(browser_context_); |
209 const MenuItem::List* all_items = manager->MenuItems(extension_key); | 202 const MenuItem::List* all_items = manager->MenuItems(extension_key); |
210 if (!all_items || all_items->empty()) | 203 if (!all_items || all_items->empty()) |
211 return false; | 204 return false; |
212 | 205 |
213 *can_cross_incognito = util::CanCrossIncognito(*extension, browser_context_); | 206 *can_cross_incognito = util::CanCrossIncognito(*extension, browser_context_); |
214 items = GetRelevantExtensionItems(*all_items, | 207 *items = GetRelevantExtensionItems(*all_items, *can_cross_incognito); |
215 *can_cross_incognito); | |
216 | 208 |
217 return true; | 209 return true; |
218 } | 210 } |
219 | 211 |
220 MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( | 212 MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( |
221 const MenuItem::List& items, | 213 const MenuItem::List& items, |
222 bool can_cross_incognito) { | 214 bool can_cross_incognito) { |
223 MenuItem::List result; | 215 MenuItem::List result; |
224 for (MenuItem::List::const_iterator i = items.begin(); | 216 for (MenuItem::List::const_iterator i = items.begin(); |
225 i != items.end(); ++i) { | 217 i != items.end(); ++i) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
325 DCHECK_GE(index, 0); | 317 DCHECK_GE(index, 0); |
326 | 318 |
327 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); | 319 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); |
328 DCHECK(icon.width() == gfx::kFaviconSize); | 320 DCHECK(icon.width() == gfx::kFaviconSize); |
329 DCHECK(icon.height() == gfx::kFaviconSize); | 321 DCHECK(icon.height() == gfx::kFaviconSize); |
330 | 322 |
331 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); | 323 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); |
332 } | 324 } |
333 | 325 |
334 } // namespace extensions | 326 } // namespace extensions |
OLD | NEW |