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" |
11 #include "content/public/common/context_menu_params.h" | 11 #include "content/public/common/context_menu_params.h" |
12 #include "ui/gfx/favicon_size.h" | 12 #include "ui/gfx/favicon_size.h" |
13 #include "ui/gfx/image/image.h" | 13 #include "ui/gfx/image/image.h" |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 // static | 17 // static |
18 const size_t ContextMenuMatcher::kMaxExtensionItemTitleLength = 75; | 18 const size_t ContextMenuMatcher::kMaxExtensionItemTitleLength = 75; |
19 | 19 |
20 ContextMenuMatcher::ContextMenuMatcher( | 20 ContextMenuMatcher::ContextMenuMatcher( |
21 Profile* profile, | 21 Profile* profile, |
22 ui::SimpleMenuModel::Delegate* delegate, | 22 ui::SimpleMenuModel::Delegate* delegate, |
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 | |
29 void ContextMenuMatcher::AppendExtensionItems(const std::string& extension_id, | 28 void ContextMenuMatcher::AppendExtensionItems(const std::string& extension_id, |
30 const string16& selection_text, | 29 const string16& selection_text, |
31 int* index) | 30 int* index) |
32 { | 31 { |
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); | 32 DCHECK_GE(*index, 0); |
38 int max_index = | 33 int max_index = |
39 IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 34 IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
40 if (!extension || *index >= max_index) | 35 if (*index >= max_index) |
41 return; | 36 return; |
42 | 37 |
43 // Find matching items. | 38 const Extension* extension = NULL; |
44 const MenuItem::List* all_items = manager->MenuItems(extension_id); | 39 MenuItem::List items; |
45 if (!all_items || all_items->empty()) | 40 bool can_cross_incognito; |
41 if (!GetRelevantExtensionTopLevelItems(extension_id, &extension, | |
42 &can_cross_incognito, items)) | |
46 return; | 43 return; |
47 bool can_cross_incognito = service->CanCrossIncognito(extension); | |
48 MenuItem::List items = GetRelevantExtensionItems(*all_items, | |
49 can_cross_incognito); | |
50 | 44 |
51 if (items.empty()) | 45 if (items.empty()) |
52 return; | 46 return; |
53 | 47 |
54 // If this is the first extension-provided menu item, and there are other | 48 // 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. | 49 // items in the menu, and the last item is not a separator add a separator. |
56 if (*index == 0 && menu_model_->GetItemCount()) | 50 if (*index == 0 && menu_model_->GetItemCount()) |
57 menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); | 51 menu_model_->AddSeparator(ui::NORMAL_SEPARATOR); |
58 | 52 |
59 // Extensions (other than platform apps) are only allowed one top-level slot | 53 // 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 } | 86 } |
93 SetExtensionIcon(extension_id); | 87 SetExtensionIcon(extension_id); |
94 } | 88 } |
95 } | 89 } |
96 | 90 |
97 void ContextMenuMatcher::Clear() { | 91 void ContextMenuMatcher::Clear() { |
98 extension_item_map_.clear(); | 92 extension_item_map_.clear(); |
99 extension_menu_models_.clear(); | 93 extension_menu_models_.clear(); |
100 } | 94 } |
101 | 95 |
96 std::string ContextMenuMatcher::GetTopLevelContextMenuTitle( | |
97 const std::string& extension_id, | |
98 const string16& selection_text) { | |
99 const Extension* extension = NULL; | |
100 MenuItem::List items; | |
101 bool can_cross_incognito; | |
102 GetRelevantExtensionTopLevelItems(extension_id, &extension, | |
103 &can_cross_incognito, items); | |
104 | |
105 std::string title; | |
106 | |
107 if (items.empty() || | |
108 items.size() > 1 || | |
109 items[0]->type() != MenuItem::NORMAL) { | |
110 title = extension->name(); | |
111 } else { | |
112 MenuItem* item = items[0]; | |
113 title = UTF16ToUTF8(item->TitleWithReplacement( | |
Avi (use Gerrit)
2013/06/05 14:34:10
MenuItem properly follows the rule of UI strings b
François Beaufort
2013/06/06 09:07:37
Title is a string and not tied to the MenuItem her
| |
114 selection_text, kMaxExtensionItemTitleLength)); | |
115 } | |
116 return title; | |
117 } | |
118 | |
102 bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { | 119 bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { |
103 MenuItem* item = GetExtensionMenuItem(command_id); | 120 MenuItem* item = GetExtensionMenuItem(command_id); |
104 if (!item) | 121 if (!item) |
105 return false; | 122 return false; |
106 return item->checked(); | 123 return item->checked(); |
107 } | 124 } |
108 | 125 |
109 bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { | 126 bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { |
110 MenuItem* item = GetExtensionMenuItem(command_id); | 127 MenuItem* item = GetExtensionMenuItem(command_id); |
111 if (!item) | 128 if (!item) |
112 return true; | 129 return true; |
113 return item->enabled(); | 130 return item->enabled(); |
114 } | 131 } |
115 | 132 |
116 void ContextMenuMatcher::ExecuteCommand(int command_id, | 133 void ContextMenuMatcher::ExecuteCommand(int command_id, |
117 content::WebContents* web_contents, | 134 content::WebContents* web_contents, |
118 const content::ContextMenuParams& params) { | 135 const content::ContextMenuParams& params) { |
119 MenuManager* manager = extensions::ExtensionSystem::Get(profile_)-> | 136 MenuManager* manager = extensions::ExtensionSystem::Get(profile_)-> |
120 extension_service()->menu_manager(); | 137 extension_service()->menu_manager(); |
121 MenuItem* item = GetExtensionMenuItem(command_id); | 138 MenuItem* item = GetExtensionMenuItem(command_id); |
122 if (!item) | 139 if (!item) |
123 return; | 140 return; |
124 | 141 |
125 manager->ExecuteCommand(profile_, web_contents, params, item->id()); | 142 manager->ExecuteCommand(profile_, web_contents, params, item->id()); |
126 } | 143 } |
127 | 144 |
145 bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( | |
146 const std::string& extension_id, | |
147 const Extension** extension, | |
148 bool* can_cross_incognito, | |
149 MenuItem::List& items) { | |
150 ExtensionService* service = | |
151 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
152 MenuManager* manager = service->menu_manager(); | |
153 *extension = service->GetExtensionById(extension_id, false); | |
154 | |
155 if (!*extension) | |
156 return false; | |
157 | |
158 // Find matching items. | |
159 const MenuItem::List* all_items = manager->MenuItems(extension_id); | |
160 CHECK(all_items && !all_items->empty()); | |
161 *can_cross_incognito = service->CanCrossIncognito(*extension); | |
162 items = GetRelevantExtensionItems(*all_items, | |
163 can_cross_incognito); | |
164 | |
165 return true; | |
166 } | |
167 | |
128 MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( | 168 MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( |
129 const MenuItem::List& items, | 169 const MenuItem::List& items, |
130 bool can_cross_incognito) { | 170 bool can_cross_incognito) { |
131 MenuItem::List result; | 171 MenuItem::List result; |
132 for (MenuItem::List::const_iterator i = items.begin(); | 172 for (MenuItem::List::const_iterator i = items.begin(); |
133 i != items.end(); ++i) { | 173 i != items.end(); ++i) { |
134 const MenuItem* item = *i; | 174 const MenuItem* item = *i; |
135 | 175 |
136 if (!filter_.Run(item)) | 176 if (!filter_.Run(item)) |
137 continue; | 177 continue; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 DCHECK_GE(index, 0); | 264 DCHECK_GE(index, 0); |
225 | 265 |
226 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); | 266 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); |
227 DCHECK(icon.width() == gfx::kFaviconSize); | 267 DCHECK(icon.width() == gfx::kFaviconSize); |
228 DCHECK(icon.height() == gfx::kFaviconSize); | 268 DCHECK(icon.height() == gfx::kFaviconSize); |
229 | 269 |
230 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); | 270 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); |
231 } | 271 } |
232 | 272 |
233 } // namespace extensions | 273 } // namespace extensions |
OLD | NEW |