Commit 5c338d32 authored by Marcin Wiacek's avatar Marcin Wiacek Committed by Commit Bot

Align Context Menu in Android Chrome to desktop/FF

Desktop Chrome / Firefox are decoding % sequences in URLs in the
status bar, additionally desktop Chrome is omitting HTTP scheme there.

Patch is including this functionality in the Android Chrome
in the context menu URL and is replacing or extending:

1. crrev.com/2774143002/
2. https://chromium-review.googlesource.com/c/chromium/src/+/877759
3. https://chromium-review.googlesource.com/c/chromium/src/+/1015229

BUG=705233

Change-Id: I70509448108e267b66413a9c8869518706375b68
Reviewed-on: https://chromium-review.googlesource.com/1037123
Commit-Queue: Marcin Wiącek <marcin@mwiacek.com>
Reviewed-by: default avatarMaria Khomenko <mariakhomenko@chromium.org>
Reviewed-by: default avatarChristopher Thompson <cthomp@chromium.org>
Reviewed-by: default avatarTommy Li <tommycli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#560182}
parent bdc9b10b
...@@ -26,6 +26,7 @@ import org.chromium.chrome.browser.search_engines.TemplateUrlService; ...@@ -26,6 +26,7 @@ import org.chromium.chrome.browser.search_engines.TemplateUrlService;
import org.chromium.chrome.browser.share.ShareHelper; import org.chromium.chrome.browser.share.ShareHelper;
import org.chromium.chrome.browser.share.ShareParams; import org.chromium.chrome.browser.share.ShareParams;
import org.chromium.chrome.browser.util.UrlUtilities; import org.chromium.chrome.browser.util.UrlUtilities;
import org.chromium.components.url_formatter.UrlFormatter;
import org.chromium.content.browser.BrowserStartupController; import org.chromium.content.browser.BrowserStartupController;
import org.chromium.content_public.common.ContentUrlConstants; import org.chromium.content_public.common.ContentUrlConstants;
...@@ -275,7 +276,15 @@ public class ChromeContextMenuPopulator implements ContextMenuPopulator { ...@@ -275,7 +276,15 @@ public class ChromeContextMenuPopulator implements ContextMenuPopulator {
String titleText = ""; String titleText = "";
if (!TextUtils.isEmpty(params.getLinkUrl()) if (!TextUtils.isEmpty(params.getLinkUrl())
&& !params.getLinkUrl().equals(ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL)) { && !params.getLinkUrl().equals(ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL)) {
titleText = params.getLinkUrl(); // The context menu can be created without native library
// being loaded. Only use native URL formatting methods
// if the native libraries have been loaded.
if (BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER)
.isStartupSuccessfullyCompleted()) {
titleText = UrlFormatter.formatUrlForDisplayOmitHTTPScheme(params.getLinkUrl());
} else {
titleText = params.getLinkUrl();
}
} else if (!TextUtils.isEmpty(params.getTitleText())) { } else if (!TextUtils.isEmpty(params.getTitleText())) {
titleText = params.getTitleText(); titleText = params.getTitleText();
} }
......
...@@ -11,6 +11,9 @@ import android.view.LayoutInflater; ...@@ -11,6 +11,9 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.is;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
...@@ -143,17 +146,22 @@ public class TabularContextMenuUiTest { ...@@ -143,17 +146,22 @@ public class TabularContextMenuUiTest {
final List<? extends ContextMenuItem> item = final List<? extends ContextMenuItem> item =
CollectionUtil.newArrayList(ChromeContextMenuItem.ADD_TO_CONTACTS, CollectionUtil.newArrayList(ChromeContextMenuItem.ADD_TO_CONTACTS,
ChromeContextMenuItem.CALL, ChromeContextMenuItem.COPY_LINK_ADDRESS); ChromeContextMenuItem.CALL, ChromeContextMenuItem.COPY_LINK_ADDRESS);
final String expectedUrl = "http://google.com"; final String createdUrl = "http://google.com";
final String expectedUrlWithFormatUrlForDisplayOmitHTTPScheme = "google.com";
View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() { View view = ThreadUtils.runOnUiThreadBlocking(new Callable<View>() {
@Override @Override
public View call() { public View call() {
return dialog.createContextMenuPageUi(mActivityTestRule.getActivity(), return dialog.createContextMenuPageUi(mActivityTestRule.getActivity(),
new MockMenuParams(expectedUrl), Collections.unmodifiableList(item), false); new MockMenuParams(createdUrl), Collections.unmodifiableList(item), false);
} }
}); });
TextView textView = (TextView) view.findViewById(R.id.context_header_text); TextView textView = (TextView) view.findViewById(R.id.context_header_text);
Assert.assertEquals(expectedUrl, String.valueOf(textView.getText())); // URL in the header of the context menu can be rendered with
// or without formatUrlForDisplayWithOmitHTTPScheme (depends on it,
// if JNI code is loaded or not) and expected URL can look differently.
Assert.assertThat(String.valueOf(textView.getText()),
anyOf(is(createdUrl), is(expectedUrlWithFormatUrlForDisplayOmitHTTPScheme)));
} }
@Test @Test
......
...@@ -47,6 +47,26 @@ public final class UrlFormatter { ...@@ -47,6 +47,26 @@ public final class UrlFormatter {
return nativeFormatUrlForDisplayOmitScheme(uri); return nativeFormatUrlForDisplayOmitScheme(uri);
} }
/**
* Builds a String representation of <code>uri</code> suitable for display to the user,
* omitting the HTTP scheme, the username and password, trailing slash on a bare hostname,
* and converting %20 to spaces.
*
* The IDN hostname is turned to Unicode if the Unicode representation is deemed safe.
* For more information, see <code>url_formatter::FormatUrl(const GURL&)</code>.
*
* Example:
* - "http://user:password@example.com/%20test" -> "example.com/ test"
* - "http://user:password@example.com/" -> "example.com"
* - "http://www.xn--frgbolaget-q5a.se" -> "www.färgbolaget.se"
*
* @param uri URI to format.
* @return Formatted URL.
*/
public static String formatUrlForDisplayOmitHTTPScheme(String uri) {
return nativeFormatUrlForDisplayOmitHTTPScheme(uri);
}
/** /**
* Builds a String representation of <code>uri</code> suitable for copying to the clipboard. * Builds a String representation of <code>uri</code> suitable for copying to the clipboard.
* It does not omit any components, and it performs normal escape decoding. Spaces are left * It does not omit any components, and it performs normal escape decoding. Spaces are left
...@@ -90,6 +110,7 @@ public final class UrlFormatter { ...@@ -90,6 +110,7 @@ public final class UrlFormatter {
private static native String nativeFixupUrl(String url); private static native String nativeFixupUrl(String url);
private static native String nativeFormatUrlForDisplayOmitScheme(String url); private static native String nativeFormatUrlForDisplayOmitScheme(String url);
private static native String nativeFormatUrlForDisplayOmitHTTPScheme(String url);
private static native String nativeFormatUrlForCopy(String url); private static native String nativeFormatUrlForCopy(String url);
private static native String nativeFormatUrlForSecurityDisplay(String url); private static native String nativeFormatUrlForSecurityDisplay(String url);
private static native String nativeFormatUrlForSecurityDisplayOmitScheme(String url); private static native String nativeFormatUrlForSecurityDisplayOmitScheme(String url);
......
...@@ -53,6 +53,18 @@ JNI_UrlFormatter_FormatUrlForDisplayOmitScheme( ...@@ -53,6 +53,18 @@ JNI_UrlFormatter_FormatUrlForDisplayOmitScheme(
net::UnescapeRule::SPACES, nullptr, nullptr, nullptr)); net::UnescapeRule::SPACES, nullptr, nullptr, nullptr));
} }
static ScopedJavaLocalRef<jstring>
JNI_UrlFormatter_FormatUrlForDisplayOmitHTTPScheme(
JNIEnv* env,
const JavaParamRef<jclass>& clazz,
const JavaParamRef<jstring>& url) {
return base::android::ConvertUTF16ToJavaString(
env, url_formatter::FormatUrl(
JNI_UrlFormatter_ConvertJavaStringToGURL(env, url),
url_formatter::kFormatUrlOmitDefaults, net::UnescapeRule::SPACES,
nullptr, nullptr, nullptr));
}
static ScopedJavaLocalRef<jstring> JNI_UrlFormatter_FormatUrlForCopy( static ScopedJavaLocalRef<jstring> JNI_UrlFormatter_FormatUrlForCopy(
JNIEnv* env, JNIEnv* env,
const JavaParamRef<jclass>& clazz, const JavaParamRef<jclass>& clazz,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment