OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 package org.chromium.chrome.browser.ntp.snippets; | 4 package org.chromium.chrome.browser.ntp.snippets; |
5 | 5 |
6 import android.graphics.Bitmap; | 6 import android.graphics.Bitmap; |
7 | 7 |
8 import org.chromium.base.metrics.RecordHistogram; | 8 import org.chromium.base.metrics.RecordHistogram; |
9 import org.chromium.chrome.browser.ntp.NewTabPageUma; | 9 import org.chromium.chrome.browser.ntp.NewTabPageUma; |
10 import org.chromium.chrome.browser.ntp.cards.NewTabPageItem; | 10 import org.chromium.chrome.browser.ntp.cards.NewTabPageItem; |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 // If the max value below (72 hours) were to be changed, the histogr
am should be renamed | 174 // If the max value below (72 hours) were to be changed, the histogr
am should be renamed |
175 // since it will change the shape of buckets. | 175 // since it will change the shape of buckets. |
176 RecordHistogram.recordCustomCountHistogram(histogramName, ageInMinut
es, 1, 72 * 60, 50); | 176 RecordHistogram.recordCustomCountHistogram(histogramName, ageInMinut
es, 1, 72 * 60, 50); |
177 } | 177 } |
178 } | 178 } |
179 | 179 |
180 private static void recordScore(String histogramName, float score) { | 180 private static void recordScore(String histogramName, float score) { |
181 int recordedScore = Math.min((int) Math.ceil(score), 100000); | 181 int recordedScore = Math.min((int) Math.ceil(score), 100000); |
182 RecordHistogram.recordCustomCountHistogram(histogramName, recordedScore,
1, 100000, 50); | 182 RecordHistogram.recordCustomCountHistogram(histogramName, recordedScore,
1, 100000, 50); |
183 } | 183 } |
| 184 |
| 185 /** |
| 186 * Construct a data:text/html URI for loading from an inline HTML. Copied |
| 187 * from org.chromium.base.test.util.UrlUtils. |
| 188 * @param html An unencoded HTML |
| 189 * @return String An URI that contains the given HTML |
| 190 */ |
| 191 private static String encodeHtmlDataUri(String html) { |
| 192 try { |
| 193 // URLEncoder encodes into application/x-www-form-encoded, so |
| 194 // ' '->'+' needs to be undone and replaced with ' '->'%20' |
| 195 // to match the Data URI requirements. |
| 196 String encoded = "data:text/html;utf-8," + java.net.URLEncoder.encod
e(html, "UTF-8"); |
| 197 encoded = encoded.replace("+", "%20"); |
| 198 return encoded; |
| 199 } catch (java.io.UnsupportedEncodingException e) { |
| 200 return null; |
| 201 } |
| 202 } |
| 203 |
| 204 /** |
| 205 * Returns the URL that should actually be opened, which is either the |
| 206 * original URL associated to this article or a redirect implemented via |
| 207 * data: scheme (which prevents articles from showing up in NTP tiles). |
| 208 */ |
| 209 public String getPossiblyWrappedUrl() { |
| 210 if (mCategory != KnownCategories.ARTICLES) return mUrl; |
| 211 |
| 212 // Baked in HTML page that causes a redirect to mURL. Note that mUrl |
| 213 // is expected to specify a scheme for the redirect to work (guaranteed |
| 214 // because it's coming from a GURL). |
| 215 String wrappedUrl = encodeHtmlDataUri( |
| 216 "<head><meta http-equiv=\"refresh\" content=\"0; url=" + mUrl +
"\"></head>"); |
| 217 // TODO(mastiz): Revisit whether this should instead crash. |
| 218 if (wrappedUrl == null) return mUrl; |
| 219 return wrappedUrl; |
| 220 } |
184 } | 221 } |
OLD | NEW |