OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/tab_contents/context_menu_utils.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "webkit/glue/context_menu.h" |
| 11 #include "webkit/glue/webmenuitem.h" |
| 12 |
| 13 using WebKit::WebContextMenuData; |
| 14 |
| 15 namespace { |
| 16 |
| 17 bool ExtensionContextMatch(const ContextMenuParams& params, |
| 18 ExtensionMenuItem::ContextList contexts) { |
| 19 bool has_link = !params.link_url.is_empty(); |
| 20 bool has_selection = !params.selection_text.empty(); |
| 21 bool in_frame = !params.frame_url.is_empty(); |
| 22 |
| 23 if (contexts.Contains(ExtensionMenuItem::ALL) || |
| 24 (has_selection && contexts.Contains(ExtensionMenuItem::SELECTION)) || |
| 25 (has_link && contexts.Contains(ExtensionMenuItem::LINK)) || |
| 26 (params.is_editable && contexts.Contains(ExtensionMenuItem::EDITABLE)) || |
| 27 (in_frame && contexts.Contains(ExtensionMenuItem::FRAME))) { |
| 28 return true; |
| 29 } |
| 30 |
| 31 switch (params.media_type) { |
| 32 case WebKit::WebContextMenuData::MediaTypeImage: |
| 33 return contexts.Contains(ExtensionMenuItem::IMAGE); |
| 34 |
| 35 case WebKit::WebContextMenuData::MediaTypeVideo: |
| 36 return contexts.Contains(ExtensionMenuItem::VIDEO); |
| 37 |
| 38 case WebKit::WebContextMenuData::MediaTypeAudio: |
| 39 return contexts.Contains(ExtensionMenuItem::AUDIO); |
| 40 |
| 41 default: |
| 42 break; |
| 43 } |
| 44 |
| 45 |
| 46 // PAGE is the least specific context, so we only examine that if none of the |
| 47 // other contexts apply (except for FRAME, which is included in PAGE for |
| 48 // backwards compatibility). |
| 49 if (!has_link && !has_selection && !params.is_editable && |
| 50 params.media_type == WebContextMenuData::MediaTypeNone && |
| 51 contexts.Contains(ExtensionMenuItem::PAGE)) |
| 52 return true; |
| 53 |
| 54 return false; |
| 55 } |
| 56 |
| 57 bool ExtensionPatternMatch(const ExtensionExtent& patterns, |
| 58 const GURL& url) { |
| 59 // No patterns means no restriction, so that implicitly matches. |
| 60 if (patterns.is_empty()) |
| 61 return true; |
| 62 return patterns.ContainsURL(url); |
| 63 } |
| 64 |
| 65 } |
| 66 |
| 67 const GURL& ContextMenuUtils::GetDocumentURL(const ContextMenuParams& params) { |
| 68 return params.frame_url.is_empty() ? params.page_url : params.frame_url; |
| 69 } |
| 70 |
| 71 // Given a list of items, returns the ones that match given the contents |
| 72 // of |params| and the profile. |
| 73 ExtensionMenuItem::List ContextMenuUtils::GetRelevantExtensionItems( |
| 74 const ExtensionMenuItem::List& items, |
| 75 const ContextMenuParams& params, |
| 76 Profile* profile, |
| 77 bool can_cross_incognito) { |
| 78 ExtensionMenuItem::List result; |
| 79 for (ExtensionMenuItem::List::const_iterator i = items.begin(); |
| 80 i != items.end(); ++i) { |
| 81 const ExtensionMenuItem* item = *i; |
| 82 |
| 83 if (!ExtensionContextMatch(params, item->contexts())) |
| 84 continue; |
| 85 |
| 86 const GURL& document_url = GetDocumentURL(params); |
| 87 if (!ExtensionPatternMatch(item->document_url_patterns(), document_url)) |
| 88 continue; |
| 89 |
| 90 const GURL& target_url = |
| 91 params.src_url.is_empty() ? params.link_url : params.src_url; |
| 92 if (!ExtensionPatternMatch(item->target_url_patterns(), target_url)) |
| 93 continue; |
| 94 |
| 95 if (item->id().profile == profile || can_cross_incognito) |
| 96 result.push_back(*i); |
| 97 } |
| 98 return result; |
| 99 } |
| 100 |
| 101 void ContextMenuUtils::GetSortedContextMenuExtensionIds( |
| 102 ExtensionService* service, |
| 103 ExtensionMenuManager* menu_manager, |
| 104 std::vector<std::pair<std::string, std::string> >* sorted_ids) { |
| 105 std::set<std::string> ids = menu_manager->ExtensionIds(); |
| 106 for (std::set<std::string>::iterator i = ids.begin(); i != ids.end(); ++i) { |
| 107 const Extension* extension = service->GetExtensionById(*i, false); |
| 108 if (extension) |
| 109 sorted_ids->push_back( |
| 110 std::pair<std::string, std::string>(extension->name(), *i)); |
| 111 } |
| 112 // TODO(asargent) - See if this works properly for i18n names (bug 32363). |
| 113 std::sort(sorted_ids->begin(), sorted_ids->end()); |
| 114 } |
| 115 |
OLD | NEW |