OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <algorithm> | 5 #include <algorithm> |
6 #include <set> | 6 #include <set> |
7 | 7 |
8 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 8 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
9 | 9 |
10 #include "app/clipboard/clipboard.h" | 10 #include "app/clipboard/clipboard.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 RenderViewContextMenu::~RenderViewContextMenu() { | 82 RenderViewContextMenu::~RenderViewContextMenu() { |
83 } | 83 } |
84 | 84 |
85 // Menu construction functions ------------------------------------------------- | 85 // Menu construction functions ------------------------------------------------- |
86 | 86 |
87 void RenderViewContextMenu::Init() { | 87 void RenderViewContextMenu::Init() { |
88 InitMenu(); | 88 InitMenu(); |
89 PlatformInit(); | 89 PlatformInit(); |
90 } | 90 } |
91 | 91 |
92 static bool ExtensionContextMatch(ContextMenuParams params, | 92 static bool ExtensionContextMatch(const ContextMenuParams& params, |
93 ExtensionMenuItem::ContextList contexts) { | 93 ExtensionMenuItem::ContextList contexts) { |
94 bool has_link = !params.link_url.is_empty(); | 94 bool has_link = !params.link_url.is_empty(); |
95 bool has_selection = !params.selection_text.empty(); | 95 bool has_selection = !params.selection_text.empty(); |
96 | 96 |
97 if (contexts.Contains(ExtensionMenuItem::ALL) || | 97 if (contexts.Contains(ExtensionMenuItem::ALL) || |
98 (has_selection && contexts.Contains(ExtensionMenuItem::SELECTION)) || | 98 (has_selection && contexts.Contains(ExtensionMenuItem::SELECTION)) || |
99 (has_link && contexts.Contains(ExtensionMenuItem::LINK)) || | 99 (has_link && contexts.Contains(ExtensionMenuItem::LINK)) || |
100 (params.is_editable && contexts.Contains(ExtensionMenuItem::EDITABLE))) { | 100 (params.is_editable && contexts.Contains(ExtensionMenuItem::EDITABLE))) { |
101 return true; | 101 return true; |
102 } | 102 } |
(...skipping 15 matching lines...) Expand all Loading... |
118 // PAGE is the least specific context, so we only examine that if none of the | 118 // PAGE is the least specific context, so we only examine that if none of the |
119 // other contexts apply. | 119 // other contexts apply. |
120 if (!has_link && !has_selection && !params.is_editable && | 120 if (!has_link && !has_selection && !params.is_editable && |
121 params.media_type == WebContextMenuData::MediaTypeNone && | 121 params.media_type == WebContextMenuData::MediaTypeNone && |
122 contexts.Contains(ExtensionMenuItem::PAGE)) | 122 contexts.Contains(ExtensionMenuItem::PAGE)) |
123 return true; | 123 return true; |
124 | 124 |
125 return false; | 125 return false; |
126 } | 126 } |
127 | 127 |
| 128 static bool ExtensionPatternMatch(const ExtensionExtent& patterns, |
| 129 const GURL& url) { |
| 130 // No patterns means no restriction, so that implicitly matches. |
| 131 if (patterns.is_empty()) |
| 132 return true; |
| 133 return patterns.ContainsURL(url); |
| 134 } |
| 135 |
| 136 static const GURL& GetDocumentURL(const ContextMenuParams& params) { |
| 137 return params.frame_url.is_empty() ? params.page_url : params.frame_url; |
| 138 } |
| 139 |
128 // Given a list of items, returns the ones that match given the contents | 140 // Given a list of items, returns the ones that match given the contents |
129 // of |params|. | 141 // of |params|. |
130 static ExtensionMenuItem::List GetRelevantExtensionItems( | 142 static ExtensionMenuItem::List GetRelevantExtensionItems( |
131 const ExtensionMenuItem::List& items, | 143 const ExtensionMenuItem::List& items, |
132 ContextMenuParams params) { | 144 const ContextMenuParams& params) { |
133 ExtensionMenuItem::List result; | 145 ExtensionMenuItem::List result; |
134 for (ExtensionMenuItem::List::const_iterator i = items.begin(); | 146 for (ExtensionMenuItem::List::const_iterator i = items.begin(); |
135 i != items.end(); ++i) { | 147 i != items.end(); ++i) { |
136 if (ExtensionContextMatch(params, (*i)->contexts())) | 148 const ExtensionMenuItem* item = *i; |
137 result.push_back(*i); | 149 |
| 150 if (!ExtensionContextMatch(params, item->contexts())) |
| 151 continue; |
| 152 |
| 153 const GURL& document_url = GetDocumentURL(params); |
| 154 if (!ExtensionPatternMatch(item->document_url_patterns(), document_url)) |
| 155 continue; |
| 156 |
| 157 const GURL& target_url = |
| 158 params.src_url.is_empty() ? params.link_url : params.src_url; |
| 159 if (!target_url.is_empty() && |
| 160 !ExtensionPatternMatch(item->target_url_patterns(), target_url)) |
| 161 continue; |
| 162 |
| 163 result.push_back(*i); |
138 } | 164 } |
139 return result; | 165 return result; |
140 } | 166 } |
141 | 167 |
142 void RenderViewContextMenu::AppendExtensionItems( | 168 void RenderViewContextMenu::AppendExtensionItems( |
143 const std::string& extension_id, int* index) { | 169 const std::string& extension_id, int* index) { |
144 ExtensionsService* service = profile_->GetExtensionsService(); | 170 ExtensionsService* service = profile_->GetExtensionsService(); |
145 ExtensionMenuManager* manager = service->menu_manager(); | 171 ExtensionMenuManager* manager = service->menu_manager(); |
146 Extension* extension = service->GetExtensionById(extension_id, false); | 172 Extension* extension = service->GetExtensionById(extension_id, false); |
147 DCHECK_GE(*index, 0); | 173 DCHECK_GE(*index, 0); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 | 287 |
262 menu_model_.SetIcon(index, icon); | 288 menu_model_.SetIcon(index, icon); |
263 } | 289 } |
264 | 290 |
265 void RenderViewContextMenu::AppendAllExtensionItems() { | 291 void RenderViewContextMenu::AppendAllExtensionItems() { |
266 extension_item_map_.clear(); | 292 extension_item_map_.clear(); |
267 ExtensionsService* service = profile_->GetExtensionsService(); | 293 ExtensionsService* service = profile_->GetExtensionsService(); |
268 if (!service) | 294 if (!service) |
269 return; // In unit-tests, we may not have an ExtensionService. | 295 return; // In unit-tests, we may not have an ExtensionService. |
270 ExtensionMenuManager* menu_manager = service->menu_manager(); | 296 ExtensionMenuManager* menu_manager = service->menu_manager(); |
| 297 const GURL& document_url = GetDocumentURL(params_); |
| 298 if (!menu_manager->HasAllowedScheme(document_url)) |
| 299 return; |
271 | 300 |
272 // Get a list of extension id's that have context menu items, and sort it by | 301 // Get a list of extension id's that have context menu items, and sort it by |
273 // the extension's name. | 302 // the extension's name. |
274 std::set<std::string> ids = menu_manager->ExtensionIds(); | 303 std::set<std::string> ids = menu_manager->ExtensionIds(); |
275 std::vector<std::pair<std::string, std::string> > sorted_ids; | 304 std::vector<std::pair<std::string, std::string> > sorted_ids; |
276 for (std::set<std::string>::iterator i = ids.begin(); i != ids.end(); ++i) { | 305 for (std::set<std::string>::iterator i = ids.begin(); i != ids.end(); ++i) { |
277 Extension* extension = service->GetExtensionById(*i, false); | 306 Extension* extension = service->GetExtensionById(*i, false); |
278 if (extension) | 307 if (extension) |
279 sorted_ids.push_back( | 308 sorted_ids.push_back( |
280 std::pair<std::string, std::string>(extension->name(), *i)); | 309 std::pair<std::string, std::string>(extension->name(), *i)); |
(...skipping 1074 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1355 UTF8ToWide(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), | 1384 UTF8ToWide(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), |
1356 g_browser_process->clipboard()); | 1385 g_browser_process->clipboard()); |
1357 } | 1386 } |
1358 | 1387 |
1359 void RenderViewContextMenu::MediaPlayerActionAt( | 1388 void RenderViewContextMenu::MediaPlayerActionAt( |
1360 const gfx::Point& location, | 1389 const gfx::Point& location, |
1361 const WebMediaPlayerAction& action) { | 1390 const WebMediaPlayerAction& action) { |
1362 source_tab_contents_->render_view_host()->MediaPlayerActionAt( | 1391 source_tab_contents_->render_view_host()->MediaPlayerActionAt( |
1363 location, action); | 1392 location, action); |
1364 } | 1393 } |
OLD | NEW |