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 13 matching lines...) Expand all Loading... |
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 MenuItem::ExtensionKey& extension_key, | 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 ContextMenuMatcher::AppendExtensionItemsImpl(extension_key, |
| 35 selection_text, |
| 36 index, |
| 37 true); |
| 38 } |
| 39 |
| 40 void ContextMenuMatcher::AppendExtensionItemsWithoutIcons( |
| 41 const MenuItem::ExtensionKey& extension_key, |
| 42 const base::string16& selection_text, |
| 43 int* index) { |
| 44 ContextMenuMatcher::AppendExtensionItemsImpl(extension_key, |
| 45 selection_text, |
| 46 index, |
| 47 false); |
| 48 } |
| 49 |
| 50 void ContextMenuMatcher::Clear() { |
| 51 extension_item_map_.clear(); |
| 52 extension_menu_models_.clear(); |
| 53 } |
| 54 |
| 55 base::string16 ContextMenuMatcher::GetTopLevelContextMenuTitle( |
| 56 const MenuItem::ExtensionKey& extension_key, |
| 57 const base::string16& selection_text) { |
| 58 const Extension* extension = NULL; |
| 59 MenuItem::List items; |
| 60 bool can_cross_incognito; |
| 61 GetRelevantExtensionTopLevelItems( |
| 62 extension_key, &extension, &can_cross_incognito, items); |
| 63 |
| 64 base::string16 title; |
| 65 |
| 66 if (items.empty() || |
| 67 items.size() > 1 || |
| 68 items[0]->type() != MenuItem::NORMAL) { |
| 69 title = base::UTF8ToUTF16(extension->name()); |
| 70 } else { |
| 71 MenuItem* item = items[0]; |
| 72 title = item->TitleWithReplacement( |
| 73 selection_text, kMaxExtensionItemTitleLength); |
| 74 } |
| 75 return title; |
| 76 } |
| 77 |
| 78 bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { |
| 79 MenuItem* item = GetExtensionMenuItem(command_id); |
| 80 if (!item) |
| 81 return false; |
| 82 return item->checked(); |
| 83 } |
| 84 |
| 85 bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { |
| 86 MenuItem* item = GetExtensionMenuItem(command_id); |
| 87 if (!item) |
| 88 return true; |
| 89 return item->enabled(); |
| 90 } |
| 91 |
| 92 void ContextMenuMatcher::ExecuteCommand(int command_id, |
| 93 content::WebContents* web_contents, |
| 94 const content::ContextMenuParams& params) { |
| 95 MenuItem* item = GetExtensionMenuItem(command_id); |
| 96 if (!item) |
| 97 return; |
| 98 |
| 99 MenuManager* manager = MenuManager::Get(profile_); |
| 100 manager->ExecuteCommand(profile_, web_contents, params, item->id()); |
| 101 } |
| 102 |
| 103 void ContextMenuMatcher::AppendExtensionItemsImpl( |
| 104 const MenuItem::ExtensionKey& extension_key, |
| 105 const base::string16& selection_text, |
| 106 int* index, |
| 107 bool include_icons) { |
34 DCHECK_GE(*index, 0); | 108 DCHECK_GE(*index, 0); |
35 int max_index = | 109 int max_index = |
36 IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; | 110 IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST - IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST; |
37 if (*index >= max_index) | 111 if (*index >= max_index) |
38 return; | 112 return; |
39 | 113 |
40 const Extension* extension = NULL; | 114 const Extension* extension = NULL; |
41 MenuItem::List items; | 115 MenuItem::List items; |
42 bool can_cross_incognito; | 116 bool can_cross_incognito; |
43 if (!GetRelevantExtensionTopLevelItems( | 117 if (!GetRelevantExtensionTopLevelItems( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 // Now add our item(s) to the menu_model_. | 153 // Now add our item(s) to the menu_model_. |
80 if (submenu_items.empty()) { | 154 if (submenu_items.empty()) { |
81 menu_model_->AddItem(menu_id, title); | 155 menu_model_->AddItem(menu_id, title); |
82 } else { | 156 } else { |
83 ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_); | 157 ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate_); |
84 extension_menu_models_.push_back(submenu); | 158 extension_menu_models_.push_back(submenu); |
85 menu_model_->AddSubMenu(menu_id, title, submenu); | 159 menu_model_->AddSubMenu(menu_id, title, submenu); |
86 RecursivelyAppendExtensionItems(submenu_items, can_cross_incognito, | 160 RecursivelyAppendExtensionItems(submenu_items, can_cross_incognito, |
87 selection_text, submenu, index); | 161 selection_text, submenu, index); |
88 } | 162 } |
89 SetExtensionIcon(extension_key.extension_id); | 163 if (include_icons) |
| 164 SetExtensionIcon(extension_key.extension_id); |
90 } | 165 } |
91 } | 166 } |
92 | 167 |
93 void ContextMenuMatcher::Clear() { | |
94 extension_item_map_.clear(); | |
95 extension_menu_models_.clear(); | |
96 } | |
97 | |
98 base::string16 ContextMenuMatcher::GetTopLevelContextMenuTitle( | |
99 const MenuItem::ExtensionKey& extension_key, | |
100 const base::string16& selection_text) { | |
101 const Extension* extension = NULL; | |
102 MenuItem::List items; | |
103 bool can_cross_incognito; | |
104 GetRelevantExtensionTopLevelItems( | |
105 extension_key, &extension, &can_cross_incognito, items); | |
106 | |
107 base::string16 title; | |
108 | |
109 if (items.empty() || | |
110 items.size() > 1 || | |
111 items[0]->type() != MenuItem::NORMAL) { | |
112 title = base::UTF8ToUTF16(extension->name()); | |
113 } else { | |
114 MenuItem* item = items[0]; | |
115 title = item->TitleWithReplacement( | |
116 selection_text, kMaxExtensionItemTitleLength); | |
117 } | |
118 return title; | |
119 } | |
120 | |
121 bool ContextMenuMatcher::IsCommandIdChecked(int command_id) const { | |
122 MenuItem* item = GetExtensionMenuItem(command_id); | |
123 if (!item) | |
124 return false; | |
125 return item->checked(); | |
126 } | |
127 | |
128 bool ContextMenuMatcher::IsCommandIdEnabled(int command_id) const { | |
129 MenuItem* item = GetExtensionMenuItem(command_id); | |
130 if (!item) | |
131 return true; | |
132 return item->enabled(); | |
133 } | |
134 | |
135 void ContextMenuMatcher::ExecuteCommand(int command_id, | |
136 content::WebContents* web_contents, | |
137 const content::ContextMenuParams& params) { | |
138 MenuItem* item = GetExtensionMenuItem(command_id); | |
139 if (!item) | |
140 return; | |
141 | |
142 MenuManager* manager = MenuManager::Get(profile_); | |
143 manager->ExecuteCommand(profile_, web_contents, params, item->id()); | |
144 } | |
145 | |
146 bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( | 168 bool ContextMenuMatcher::GetRelevantExtensionTopLevelItems( |
147 const MenuItem::ExtensionKey& extension_key, | 169 const MenuItem::ExtensionKey& extension_key, |
148 const Extension** extension, | 170 const Extension** extension, |
149 bool* can_cross_incognito, | 171 bool* can_cross_incognito, |
150 MenuItem::List& items) { | 172 MenuItem::List& items) { |
151 ExtensionService* service = | 173 ExtensionService* service = |
152 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 174 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
153 *extension = service->GetExtensionById(extension_key.extension_id, false); | 175 *extension = service->GetExtensionById(extension_key.extension_id, false); |
154 | 176 |
155 if (!*extension) | 177 if (!*extension) |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 DCHECK_GE(index, 0); | 286 DCHECK_GE(index, 0); |
265 | 287 |
266 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); | 288 const SkBitmap& icon = menu_manager->GetIconForExtension(extension_id); |
267 DCHECK(icon.width() == gfx::kFaviconSize); | 289 DCHECK(icon.width() == gfx::kFaviconSize); |
268 DCHECK(icon.height() == gfx::kFaviconSize); | 290 DCHECK(icon.height() == gfx::kFaviconSize); |
269 | 291 |
270 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); | 292 menu_model_->SetIcon(index, gfx::Image::CreateFrom1xBitmap(icon)); |
271 } | 293 } |
272 | 294 |
273 } // namespace extensions | 295 } // namespace extensions |
OLD | NEW |