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; |
+ } |
} |