Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java

Issue 2342453003: [NTP] Fix article suggestion clicks contributing to Most Visited tiles (Closed)
Patch Set: Added TODO as suggested. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java
index 7fb037243e4133847d51008b3c1b771c26403b64..ec22e960618929a16a2ba57cb64471bca77320f4 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetArticle.java
@@ -181,4 +181,41 @@ public class SnippetArticle implements NewTabPageItem {
int recordedScore = Math.min((int) Math.ceil(score), 100000);
RecordHistogram.recordCustomCountHistogram(histogramName, recordedScore, 1, 100000, 50);
}
+
+ /**
+ * Construct a data:text/html URI for loading from an inline HTML. Copied
+ * from org.chromium.base.test.util.UrlUtils.
+ * @param html An unencoded HTML
+ * @return String An URI that contains the given HTML
+ */
+ private static String encodeHtmlDataUri(String html) {
+ try {
+ // URLEncoder encodes into application/x-www-form-encoded, so
+ // ' '->'+' needs to be undone and replaced with ' '->'%20'
+ // to match the Data URI requirements.
+ String encoded = "data:text/html;utf-8," + java.net.URLEncoder.encode(html, "UTF-8");
+ encoded = encoded.replace("+", "%20");
+ return encoded;
+ } catch (java.io.UnsupportedEncodingException e) {
+ return null;
+ }
+ }
+
+ /**
+ * Returns the URL that should actually be opened, which is either the
+ * original URL associated to this article or a redirect implemented via
+ * data: scheme (which prevents articles from showing up in NTP tiles).
+ */
+ public String getPossiblyWrappedUrl() {
+ if (mCategory != KnownCategories.ARTICLES) return mUrl;
+
+ // Baked in HTML page that causes a redirect to mURL. Note that mUrl
+ // is expected to specify a scheme for the redirect to work (guaranteed
+ // because it's coming from a GURL).
+ String wrappedUrl = encodeHtmlDataUri(
+ "<head><meta http-equiv=\"refresh\" content=\"0; url=" + mUrl + "\"></head>");
+ // TODO(mastiz): Revisit whether this should instead crash.
+ if (wrappedUrl == null) return mUrl;
+ return wrappedUrl;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698