1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/context_menu_matcher.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/common/context_menu_params.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/image/image.h"
namespace extensions {
// static
const size_t ContextMenuMatcher::kMaxExtensionItemTitleLength = 75;
ContextMenuMatcher::ContextMenuMatcher(
Profile* profile,
ui::SimpleMenuModel::Delegate* delegate,
ui::SimpleMenuModel* menu_model,
const base::Callback<bool(const MenuItem*)>& filter)
: profile_(profile), menu_model_(menu_model), delegate_(delegate),
filter_(filter) {
}
void ContextMenuMatcher::AppendExtensionItems(const std::string& extension_id,
const string16& selection_text,
int* index)
{
DCHECK_GE(*index, 0);
int max_index =
IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST;
if (*index >= max_index)
return;
const Extension* extension = NULL;
MenuItem::List items;
bool can_cross_incognito;
if (!GetRelevantExtensionTopLevelItems(extension_id, &extension,
&can_cross_incognito, items))
return;
if (items.empty())
return;
// If this is the first extension-provided menu item, and there are other
// items in the menu, and the last item is not a separator add a separator.
if (*index == 0 && menu_model_->GetItemCount())
menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
// Extensions (other than platform apps) are only allowed one top-level slot
// (and it can't be a radio or checkbox item because we are going to put the
// extension icon next to it).
// If they have more than that, we automatically push them into a submenu.
if (extension->is_platform_app()) {
RecursivelyAppendExtensionItems(items, can_cross_incognito, selection_text,
menu_model_, index);
} else {
int menu_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + (*index)++;
string16 title;
MenuItem::List submenu_items;
if (items.size() > 1 || items[0]->type() != MenuItem::NORMAL) {
title = UTF8ToUTF16(extension->name());
submenu_items = items;
} else {
MenuItem* item = items[0];
extension_item_map_[menu_id] = item->id();
title = item->TitleWithReplacement(selection_text,
kMaxExtensionItemTitleLength);
submenu_items = GetRelevantExtensionItems(item->children(),
can_cross_incognito);
}
// Now add our item(s) to the menu_model_.
if (submenu_items.empty()) {
menu_model_->AddItem(menu_id, title);
} else {
ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_);
extension_menu_models_.push_back(submenu);
menu_model_->AddSubMenu(menu_id, title, submenu);
RecursivelyAppendExtensionItems(submenu_items, can_cross_incognito,
selection_text, submenu, index);
}
SetExtensionIcon(extension_id);
}
}
void ContextMenuMatcher::Clear() {
extension_item_map_.clear();
extension_menu_models_.clear();
}
base::string16 ContextMenuMatcher::GetTopLevelContextMenuTitle(
const std::string& extension_id,
const string16& selection_text) {
const Extension* extension = NULL;
MenuItem::List items;
bool can_cross_incognito;
GetRelevantExtensionTopLevelItems(extension_id, &extension,
&can_cross_incognito, items);
base::string16 title;
if (items.empty() ||
items.size() > 1 ||
items[0]->type() != MenuItem::NORMAL) {
title = UTF8ToUTF16(extension->name());
} else {
MenuItem* item = items[0];
title = item->TitleWithReplacement(
selection_text, kMaxExtensionItemTitleLength);
}
return title;
}
bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const {
MenuItem* item = GetExtensionMenuItem(command_id);
if (!item)
return false;
return item->checked();
}
bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const {
MenuItem* item = GetExtensionMenuItem(command_id);
if (!item)
return true;
return item->enabled();
}
void ContextMenuMatcher::ExecuteCommand(int command_id,
content::WebContents* web_contents,
const content::ContextMenuParams& params) {
MenuManager* manager = extensions::ExtensionSystem::Get(profile_)->
extension_service()->menu_manager();
MenuItem* item = GetExtensionMenuItem(command_id);
if (!item)
return;
manager->ExecuteCommand(profile_, web_contents, params, item->id());
}
bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems(
const std::string& extension_id,
const Extension** extension,
bool* can_cross_incognito,
MenuItem::List& items) {
ExtensionService* service =
extensions::ExtensionSystem::Get(profile_)->extension_service();
MenuManager* manager = service->menu_manager();
*extension = service->GetExtensionById(extension_id, false);
if (!*extension)
return false;
// Find matching items.
const MenuItem::List* all_items = manager->MenuItems(extension_id);
if (!all_items || all_items->empty())
return false;
*can_cross_incognito = service->CanCrossIncognito(*extension);
items = GetRelevantExtensionItems(*all_items,
*can_cross_incognito);
return true;
}
MenuItem::List ContextMenuMatcher::GetRelevantExtensionItems(
const MenuItem::List& items,
bool can_cross_incognito) {
MenuItem::List result;
for (MenuItem::List::const_iterator i = items.begin();
i != items.end(); ++i) {
const MenuItem* item = *i;
if (!filter_.Run(item))
continue;
if (item->id().incognito == profile_->IsOffTheRecord() ||
can_cross_incognito)
result.push_back(*i);
}
return result;
}
void ContextMenuMatcher::RecursivelyAppendExtensionItems(
const MenuItem::List& items,
bool can_cross_incognito,
const string16& selection_text,
ui::SimpleMenuModel* menu_model,
int* index)
{
MenuItem::Type last_type = MenuItem::NORMAL;
int radio_group_id = 1;
for (MenuItem::List::const_iterator i = items.begin();
i != items.end(); ++i) {
MenuItem* item = *i;
// If last item was of type radio but the current one isn't, auto-insert
// a separator. The converse case is handled below.
if (last_type == MenuItem::RADIO &&
item->type() != MenuItem::RADIO) {
menu_model->AddSeparator(ui::NORMAL_SEPARATOR);
last_type = MenuItem::SEPARATOR;
}
int menu_id = IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST + (*index)++;
if (menu_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST)
return;
extension_item_map_[menu_id] = item->id();
string16 title = item->TitleWithReplacement(selection_text,
kMaxExtensionItemTitleLength);
if (item->type() == MenuItem::NORMAL) {
MenuItem::List children =
GetRelevantExtensionItems(item->children(), can_cross_incognito);
if (children.empty()) {
menu_model->AddItem(menu_id, title);
} else {
ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_);
extension_menu_models_.push_back(submenu);
menu_model->AddSubMenu(menu_id, title, submenu);
RecursivelyAppendExtensionItems(children, can_cross_incognito,
selection_text, submenu, index);
}
} else if (item->type() == MenuItem::CHECKBOX) {
menu_model->AddCheckItem(menu_id, title);
} else if (item->type() == MenuItem::RADIO) {
if (i != items.begin() &&
last_type != MenuItem::RADIO) {
radio_group_id++;
// Auto-append a separator if needed.
menu_model->AddSeparator(ui::NORMAL_SEPARATOR);
}
menu_model->AddRadioItem(menu_id, title, radio_group_id);
} else if (item->type() == MenuItem::SEPARATOR) {
menu_model->AddSeparator(ui::NORMAL_SEPARATOR);
}
last_type = item->type();
}
}
MenuItem* ContextMenuMatcher::GetExtensionMenuItem(int id) const {
MenuManager* manager = extensions::ExtensionSystem::Get(profile_)->
extension_service()->menu_manager();
std::map<int, MenuItem::Id>::const_iterator i =
extension_item_map_.find(id);
if (i != extension_item_map_.end()) {
MenuItem* item = manager->GetItemById(i->second);
if (item)
return item;
}
return NULL;
}
void ContextMenuMatcher::SetExtensionIcon(const std::string& extension_id) {
ExtensionService* service =
extensions::ExtensionSystem::Get(profile_)->extension_service();
MenuManager* menu_manager = service->menu_manager();
int index = menu_model_->GetItemCount() - 1;
DCHECK_GE(index, 0);
const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id);
DCHECK(icon.width() == gfx::kFaviconSize);
DCHECK(icon.height() == gfx::kFaviconSize);
menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon));
}
} // namespace extensions
|