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 | |
Yoyo Zhou
2013/05/08 22:54:30
looks like this was accidentally deleted
| |
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, | |
Yoyo Zhou
2013/05/08 22:54:30
See https://google-styleguide.googlecode.com/svn/t
François Beaufort
2013/06/04 13:09:20
Done.
| |
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() || items.size() > 1 || items[0]->type() != MenuItem::NORMAL) { | |
Yoyo Zhou
2013/05/08 22:54:30
indent +1
François Beaufort
2013/06/04 13:09:20
Done.
| |
108 title = extension->name(); | |
109 } else { | |
110 MenuItem* item = items[0]; | |
111 title = UTF16ToUTF8(item->TitleWithReplacement( | |
112 selection_text, kMaxExtensionItemTitleLength)); | |
113 } | |
114 return title; | |
115 } | |
116 | |
102 bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { | 117 bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { |
103 MenuItem* item = GetExtensionMenuItem(command_id); | 118 MenuItem* item = GetExtensionMenuItem(command_id); |
104 if (!item) | 119 if (!item) |
105 return false; | 120 return false; |
106 return item->checked(); | 121 return item->checked(); |
107 } | 122 } |
108 | 123 |
109 bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { | 124 bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { |
110 MenuItem* item = GetExtensionMenuItem(command_id); | 125 MenuItem* item = GetExtensionMenuItem(command_id); |
111 if (!item) | 126 if (!item) |
112 return true; | 127 return true; |
113 return item->enabled(); | 128 return item->enabled(); |
114 } | 129 } |
115 | 130 |
116 void ContextMenuMatcher::ExecuteCommand(int command_id, | 131 void ContextMenuMatcher::ExecuteCommand(int command_id, |
117 content::WebContents* web_contents, | 132 content::WebContents* web_contents, |
118 const content::ContextMenuParams& params) { | 133 const content::ContextMenuParams& params) { |
119 MenuManager* manager = extensions::ExtensionSystem::Get(profile_)-> | 134 MenuManager* manager = extensions::ExtensionSystem::Get(profile_)-> |
120 extension_service()->menu_manager(); | 135 extension_service()->menu_manager(); |
121 MenuItem* item = GetExtensionMenuItem(command_id); | 136 MenuItem* item = GetExtensionMenuItem(command_id); |
122 if (!item) | 137 if (!item) |
123 return; | 138 return; |
124 | 139 |
125 manager->ExecuteCommand(profile_, web_contents, params, item->id()); | 140 manager->ExecuteCommand(profile_, web_contents, params, item->id()); |
126 } | 141 } |
127 | 142 |
143 bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( | |
144 const std::string& extension_id, | |
145 const Extension** extension, | |
146 bool& can_cross_incognito, | |
Yoyo Zhou
2013/05/08 22:54:30
As per the style guide, if you are modifying these
François Beaufort
2013/06/04 13:09:20
Done.
| |
147 MenuItem::List& items) { | |
148 ExtensionService* service = | |
149 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
150 MenuManager* manager = service->menu_manager(); | |
151 *extension = service->GetExtensionById(extension_id, false); | |
152 | |
153 if (!extension) | |
Yoyo Zhou
2013/05/08 22:54:30
Seems this should be *extension.
François Beaufort
2013/06/04 13:09:20
Done.
| |
154 return false; | |
155 | |
156 // Find matching items. | |
157 const MenuItem::List* all_items = manager->MenuItems(extension_id); | |
158 CHECK(all_items && !all_items->empty()); | |
159 can_cross_incognito = service->CanCrossIncognito(*extension); | |
160 items = GetRelevantExtensionItems(*all_items, | |
161 can_cross_incognito); | |
162 | |
163 return true; | |
164 } | |
165 | |
128 MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( | 166 MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems( |
129 const MenuItem::List& items, | 167 const MenuItem::List& items, |
130 bool can_cross_incognito) { | 168 bool can_cross_incognito) { |
131 MenuItem::List result; | 169 MenuItem::List result; |
132 for (MenuItem::List::const_iterator i = items.begin(); | 170 for (MenuItem::List::const_iterator i = items.begin(); |
133 i != items.end(); ++i) { | 171 i != items.end(); ++i) { |
134 const MenuItem* item = *i; | 172 const MenuItem* item = *i; |
135 | 173 |
136 if (!filter_.Run(item)) | 174 if (!filter_.Run(item)) |
137 continue; | 175 continue; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 DCHECK_GE(index, 0); | 262 DCHECK_GE(index, 0); |
225 | 263 |
226 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); | 264 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); |
227 DCHECK(icon.width() == gfx::kFaviconSize); | 265 DCHECK(icon.width() == gfx::kFaviconSize); |
228 DCHECK(icon.height() == gfx::kFaviconSize); | 266 DCHECK(icon.height() == gfx::kFaviconSize); |
229 | 267 |
230 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); | 268 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); |
231 } | 269 } |
232 | 270 |
233 } // namespace extensions | 271 } // namespace extensions |
OLD | NEW |