context_menu_content_type.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2014 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. #include "components/renderer_context_menu/context_menu_content_type.h"
  5. #include "base/bind.h"
  6. #include "content/public/browser/web_contents.h"
  7. #include "content/public/common/url_constants.h"
  8. #include "printing/buildflags/buildflags.h"
  9. #include "third_party/blink/public/mojom/context_menu/context_menu.mojom.h"
  10. using blink::mojom::ContextMenuDataMediaType;
  11. using content::WebContents;
  12. namespace {
  13. bool IsDevToolsURL(const GURL& url) {
  14. return url.SchemeIs(content::kChromeDevToolsScheme);
  15. }
  16. } // namespace
  17. ContextMenuContentType::ContextMenuContentType(
  18. content::WebContents* web_contents,
  19. const content::ContextMenuParams& params,
  20. bool supports_custom_items)
  21. : params_(params),
  22. source_web_contents_(web_contents),
  23. supports_custom_items_(supports_custom_items) {}
  24. ContextMenuContentType::~ContextMenuContentType() {
  25. }
  26. bool ContextMenuContentType::SupportsGroup(int group) {
  27. const bool has_selection = !params_.selection_text.empty();
  28. if (supports_custom_items_ && !params_.custom_items.empty()) {
  29. if (group == ITEM_GROUP_CUSTOM)
  30. return true;
  31. if (!has_selection) {
  32. // For menus with custom items, if there is no selection, we do not
  33. // add items other than developer items. And for Pepper menu, don't even
  34. // add developer items.
  35. return group == ITEM_GROUP_DEVELOPER;
  36. }
  37. // If there's a selection when there are custom items, fall through to
  38. // adding the normal ones after the custom ones.
  39. }
  40. if (IsDevToolsURL(params_.page_url)) {
  41. // DevTools mostly provides custom context menu and uses
  42. // only the following default options.
  43. if (group != ITEM_GROUP_CUSTOM && group != ITEM_GROUP_EDITABLE &&
  44. group != ITEM_GROUP_COPY && group != ITEM_GROUP_DEVELOPER &&
  45. group != ITEM_GROUP_SEARCH_PROVIDER) {
  46. return false;
  47. }
  48. }
  49. return SupportsGroupInternal(group);
  50. }
  51. bool ContextMenuContentType::SupportsGroupInternal(int group) {
  52. const bool has_link = !params_.unfiltered_link_url.is_empty();
  53. const bool has_selection = !params_.selection_text.empty();
  54. const bool is_password =
  55. params_.input_field_type ==
  56. blink::mojom::ContextMenuDataInputFieldType::kPassword;
  57. const bool existing_highlight = params_.opened_from_highlight;
  58. switch (group) {
  59. case ITEM_GROUP_CUSTOM:
  60. return supports_custom_items_ && !params_.custom_items.empty();
  61. case ITEM_GROUP_PAGE: {
  62. bool is_candidate =
  63. params_.media_type == ContextMenuDataMediaType::kNone && !has_link &&
  64. !params_.is_editable && !has_selection && !existing_highlight;
  65. if (!is_candidate && params_.page_url.is_empty())
  66. DCHECK(params_.frame_url.is_empty());
  67. return is_candidate && !params_.page_url.is_empty();
  68. }
  69. case ITEM_GROUP_FRAME: {
  70. bool page_group_supported = SupportsGroupInternal(ITEM_GROUP_PAGE);
  71. return page_group_supported && !params_.frame_url.is_empty();
  72. }
  73. case ITEM_GROUP_LINK:
  74. return has_link;
  75. case ITEM_GROUP_SMART_SELECTION:
  76. return has_selection && !has_link;
  77. case ITEM_GROUP_MEDIA_IMAGE:
  78. return params_.media_type == ContextMenuDataMediaType::kImage;
  79. case ITEM_GROUP_SEARCHWEBFORIMAGE:
  80. // Image menu items imply search web for image item.
  81. return SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
  82. case ITEM_GROUP_MEDIA_VIDEO:
  83. return params_.media_type == ContextMenuDataMediaType::kVideo;
  84. case ITEM_GROUP_MEDIA_AUDIO:
  85. return params_.media_type == ContextMenuDataMediaType::kAudio;
  86. case ITEM_GROUP_MEDIA_CANVAS:
  87. return params_.media_type == ContextMenuDataMediaType::kCanvas;
  88. case ITEM_GROUP_MEDIA_PLUGIN:
  89. return params_.media_type == ContextMenuDataMediaType::kPlugin;
  90. case ITEM_GROUP_MEDIA_FILE:
  91. return params_.media_type == ContextMenuDataMediaType::kFile;
  92. case ITEM_GROUP_EDITABLE:
  93. return params_.is_editable;
  94. case ITEM_GROUP_COPY:
  95. return !params_.is_editable && has_selection;
  96. case ITEM_GROUP_PARTIAL_TRANSLATE:
  97. return has_selection;
  98. case ITEM_GROUP_EXISTING_LINK_TO_TEXT:
  99. return params_.opened_from_highlight;
  100. case ITEM_GROUP_SEARCH_PROVIDER:
  101. return has_selection && !is_password;
  102. case ITEM_GROUP_PRINT: {
  103. // Image menu items also imply print items.
  104. return has_selection || SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
  105. }
  106. case ITEM_GROUP_ALL_EXTENSION:
  107. return true;
  108. case ITEM_GROUP_CURRENT_EXTENSION:
  109. return false;
  110. case ITEM_GROUP_DEVELOPER:
  111. return true;
  112. case ITEM_GROUP_DEVTOOLS_UNPACKED_EXT:
  113. return false;
  114. case ITEM_GROUP_PRINT_PREVIEW:
  115. #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
  116. return true;
  117. #else
  118. return false;
  119. #endif
  120. case ITEM_GROUP_PASSWORD:
  121. return params_.input_field_type ==
  122. blink::mojom::ContextMenuDataInputFieldType::kPassword;
  123. case ITEM_GROUP_AUTOFILL:
  124. return params_.input_field_type !=
  125. blink::mojom::ContextMenuDataInputFieldType::kNone;
  126. default:
  127. NOTREACHED();
  128. return false;
  129. }
  130. }