1 // Copyright 2013 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.
5 package org.chromium.chrome.browser.contextmenu;
7 import android.text.TextUtils;
9 import org.chromium.base.CalledByNative;
10 import org.chromium.base.JNINamespace;
11 import org.chromium.content_public.common.Referrer;
13 import java.util.ArrayList;
16 * A list of parameters that explain what kind of context menu to show the user. This data is
17 * generated from content/public/common/context_menu_params.h.
19 @JNINamespace("ContextMenuParamsAndroid")
20 public class ContextMenuParams {
21 /** Must correspond to the MediaType enum in WebKit/chromium/public/WebContextMenuData.h */
22 @SuppressWarnings("unused")
23 private static interface MediaType {
24 public static final int MEDIA_TYPE_NONE = 0;
25 public static final int MEDIA_TYPE_IMAGE = 1;
26 public static final int MEDIA_TYPE_VIDEO = 2;
27 public static final int MEDIA_TYPE_AUDIO = 3;
28 public static final int MEDIA_TYPE_FILE = 4;
29 public static final int MEDIA_TYPE_PLUGIN = 5;
32 private static class CustomMenuItem {
33 public final String label;
34 public final int action;
36 public CustomMenuItem(String label, int action) {
42 private final String mLinkUrl;
43 private final String mLinkText;
44 private final String mUnfilteredLinkUrl;
45 private final String mSrcUrl;
46 private final boolean mIsEditable;
47 private final Referrer mReferrer;
49 private final boolean mIsAnchor;
50 private final boolean mIsSelectedText;
51 private final boolean mIsImage;
52 private final boolean mIsVideo;
54 private final ArrayList<CustomMenuItem> mCustomMenuItems = new ArrayList<CustomMenuItem>();
57 * @return Whether or not the context menu should consist of custom items.
59 public boolean isCustomMenu() {
60 return !mCustomMenuItems.isEmpty();
64 * @return The number of custom items in this context menu.
66 public int getCustomMenuSize() {
67 return mCustomMenuItems.size();
71 * The label that should be shown for the custom menu item at {@code index}.
72 * @param index The index of the custom menu item.
73 * @return The label to show.
75 public String getCustomLabelAt(int index) {
76 assert index >= 0 && index < mCustomMenuItems.size();
77 return mCustomMenuItems.get(index).label;
81 * The action that should be returned for the custom menu item at {@code index}.
82 * @param index The index of the custom menu item.
83 * @return The action to return.
85 public int getCustomActionAt(int index) {
86 assert index >= 0 && index < mCustomMenuItems.size();
87 return mCustomMenuItems.get(index).action;
91 * @return The link URL, if any.
93 public String getLinkUrl() {
98 * @return The link text, if any.
100 public String getLinkText() {
105 * @return The unfiltered link URL, if any.
107 public String getUnfilteredLinkUrl() {
108 return mUnfilteredLinkUrl;
112 * @return The source URL.
114 public String getSrcUrl() {
119 * @return Whether or not the context menu is being shown for an editable piece of content.
121 public boolean isEditable() {
126 * @return the referrer associated with the frame on which the menu is invoked
128 public Referrer getReferrer() {
133 * @return Whether or not the context menu is being shown for an anchor.
135 public boolean isAnchor() {
140 * @return Whether or not the context menu is being shown for selected text.
142 public boolean isSelectedText() {
143 return mIsSelectedText;
147 * @return Whether or not the context menu is being shown for an image.
149 public boolean isImage() {
154 * @return Whether or not the context menu is being shown for a video.
156 public boolean isVideo() {
160 private ContextMenuParams(int mediaType, String linkUrl, String linkText,
161 String unfilteredLinkUrl, String srcUrl, String selectionText, boolean isEditable,
164 mLinkText = linkText;
165 mUnfilteredLinkUrl = unfilteredLinkUrl;
167 mIsEditable = isEditable;
168 mReferrer = referrer;
170 mIsAnchor = !TextUtils.isEmpty(linkUrl);
171 mIsSelectedText = !TextUtils.isEmpty(selectionText);
172 mIsImage = mediaType == MediaType.MEDIA_TYPE_IMAGE;
173 mIsVideo = mediaType == MediaType.MEDIA_TYPE_VIDEO;
177 private static ContextMenuParams create(int mediaType, String linkUrl, String linkText,
178 String unfilteredLinkUrl, String srcUrl, String selectionText, boolean isEditable,
179 String sanitizedReferrer, int referrerPolicy) {
180 Referrer referrer = TextUtils.isEmpty(sanitizedReferrer) ?
181 null : new Referrer(sanitizedReferrer, referrerPolicy);
182 return new ContextMenuParams(mediaType, linkUrl, linkText, unfilteredLinkUrl, srcUrl,
183 selectionText, isEditable, referrer);
187 private void addCustomItem(String label, int action) {
188 mCustomMenuItems.add(new CustomMenuItem(label, action));