| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 | 4 |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * @fileoverview Utilities for rendering most visited thumbnails and titles. | 7 * @fileoverview Utilities for rendering most visited thumbnails and titles. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 <include src="instant_iframe_validation.js"> | 10 <include src="instant_iframe_validation.js"> |
| 11 | 11 |
| 12 | 12 |
| 13 // TODO(treib): A number of things from this file (e.g. the "enums" below) are | |
| 14 // duplicated in most_visited_single.js. Pull those out into a shared file. | |
| 15 | |
| 16 | |
| 17 /** | |
| 18 * The different types of events that are logged from the NTP. This enum is | |
| 19 * used to transfer information from the NTP javascript to the renderer and is | |
| 20 * not used as a UMA enum histogram's logged value. | |
| 21 * Note: Keep in sync with common/ntp_logging_events.h | |
| 22 * @enum {number} | |
| 23 * @const | |
| 24 */ | |
| 25 var NTP_LOGGING_EVENT_TYPE = { | |
| 26 // The suggestion is coming from the server. | |
| 27 NTP_SERVER_SIDE_SUGGESTION: 0, | |
| 28 // The suggestion is coming from the client. | |
| 29 NTP_CLIENT_SIDE_SUGGESTION: 1, | |
| 30 // Indicates a tile was rendered, no matter if it's a thumbnail, a gray tile | |
| 31 // or an external tile. | |
| 32 NTP_TILE: 2, | |
| 33 // A NTP Tile has finished loading (successfully or failing). | |
| 34 NTP_TILE_LOADED: 10, | |
| 35 }; | |
| 36 | |
| 37 /** | |
| 38 * The different sources that an NTP tile can have. | |
| 39 * Note: Keep in sync with common/ntp_logging_events.h | |
| 40 * @enum {number} | |
| 41 * @const | |
| 42 */ | |
| 43 var NTPLoggingTileSource = { | |
| 44 CLIENT: 0, | |
| 45 SERVER: 1, | |
| 46 }; | |
| 47 | |
| 48 /** | 13 /** |
| 49 * The origin of this request. | 14 * The origin of this request. |
| 50 * @const {string} | 15 * @const {string} |
| 51 */ | 16 */ |
| 52 var DOMAIN_ORIGIN = '{{ORIGIN}}'; | 17 var DOMAIN_ORIGIN = '{{ORIGIN}}'; |
| 53 | 18 |
| 54 /** | 19 /** |
| 55 * Parses query parameters from Location. | 20 * Parses query parameters from Location. |
| 56 * @param {string} location The URL to generate the CSS url for. | 21 * @param {string} location The URL to generate the CSS url for. |
| 57 * @return {Object} Dictionary containing name value pairs for URL. | 22 * @return {Object} Dictionary containing name value pairs for URL. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 76 } | 41 } |
| 77 | 42 |
| 78 | 43 |
| 79 /** | 44 /** |
| 80 * Creates a new most visited link element. | 45 * Creates a new most visited link element. |
| 81 * @param {Object} params URL parameters containing styles for the link. | 46 * @param {Object} params URL parameters containing styles for the link. |
| 82 * @param {string} href The destination for the link. | 47 * @param {string} href The destination for the link. |
| 83 * @param {string} title The title for the link. | 48 * @param {string} title The title for the link. |
| 84 * @param {string|undefined} text The text for the link or none. | 49 * @param {string|undefined} text The text for the link or none. |
| 85 * @param {string|undefined} direction The text direction. | 50 * @param {string|undefined} direction The text direction. |
| 86 * @param {number} tileSource The source from NTPLoggingTileSource. | |
| 87 * @return {HTMLAnchorElement} A new link element. | 51 * @return {HTMLAnchorElement} A new link element. |
| 88 */ | 52 */ |
| 89 function createMostVisitedLink( | 53 function createMostVisitedLink(params, href, title, text, direction) { |
| 90 params, href, title, text, direction, tileSource) { | |
| 91 var styles = getMostVisitedStyles(params, !!text); | 54 var styles = getMostVisitedStyles(params, !!text); |
| 92 var link = document.createElement('a'); | 55 var link = document.createElement('a'); |
| 93 link.style.color = styles.color; | 56 link.style.color = styles.color; |
| 94 link.style.fontSize = styles.fontSize + 'px'; | 57 link.style.fontSize = styles.fontSize + 'px'; |
| 95 if (styles.fontFamily) | 58 if (styles.fontFamily) |
| 96 link.style.fontFamily = styles.fontFamily; | 59 link.style.fontFamily = styles.fontFamily; |
| 97 if (styles.textAlign) | 60 if (styles.textAlign) |
| 98 link.style.textAlign = styles.textAlign; | 61 link.style.textAlign = styles.textAlign; |
| 99 if (styles.textFadePos) { | 62 if (styles.textFadePos) { |
| 100 var dir = /^rtl$/i.test(direction) ? 'to left' : 'to right'; | 63 var dir = /^rtl$/i.test(direction) ? 'to left' : 'to right'; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 128 }); | 91 }); |
| 129 | 92 |
| 130 var navigateFunction = function handleNavigation(e) { | 93 var navigateFunction = function handleNavigation(e) { |
| 131 var isServerSuggestion = 'url' in params; | 94 var isServerSuggestion = 'url' in params; |
| 132 | 95 |
| 133 // Ping are only populated for server-side suggestions, never for MV. | 96 // Ping are only populated for server-side suggestions, never for MV. |
| 134 if (isServerSuggestion && params.ping) { | 97 if (isServerSuggestion && params.ping) { |
| 135 generatePing(DOMAIN_ORIGIN + params.ping); | 98 generatePing(DOMAIN_ORIGIN + params.ping); |
| 136 } | 99 } |
| 137 | 100 |
| 138 var ntpApiHandle = chrome.embeddedSearch.newTabPage; | |
| 139 if ('pos' in params && isFinite(params.pos)) { | |
| 140 ntpApiHandle.logMostVisitedNavigation(parseInt(params.pos, 10), | |
| 141 tileSource); | |
| 142 } | |
| 143 | |
| 144 // Follow <a> normally, so transition type will be LINK. | 101 // Follow <a> normally, so transition type will be LINK. |
| 145 }; | 102 }; |
| 146 | 103 |
| 147 link.addEventListener('click', navigateFunction); | 104 link.addEventListener('click', navigateFunction); |
| 148 link.addEventListener('keydown', function(event) { | 105 link.addEventListener('keydown', function(event) { |
| 149 if (event.keyCode == 46 /* DELETE */ || | 106 if (event.keyCode == 46 /* DELETE */ || |
| 150 event.keyCode == 8 /* BACKSPACE */) { | 107 event.keyCode == 8 /* BACKSPACE */) { |
| 151 event.preventDefault(); | 108 event.preventDefault(); |
| 152 window.parent.postMessage('tileBlacklisted,' + params.pos, DOMAIN_ORIGIN); | 109 window.parent.postMessage('tileBlacklisted,' + params.pos, DOMAIN_ORIGIN); |
| 153 } else if (event.keyCode == 13 /* ENTER */ || | 110 } else if (event.keyCode == 13 /* ENTER */ || |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 /** | 193 /** |
| 237 * @param {string} location A location containing URL parameters. | 194 * @param {string} location A location containing URL parameters. |
| 238 * @param {function(Object, Object)} fill A function called with styles and | 195 * @param {function(Object, Object)} fill A function called with styles and |
| 239 * data to fill. | 196 * data to fill. |
| 240 */ | 197 */ |
| 241 function fillMostVisited(location, fill) { | 198 function fillMostVisited(location, fill) { |
| 242 var params = parseQueryParams(location); | 199 var params = parseQueryParams(location); |
| 243 params.rid = parseInt(params.rid, 10); | 200 params.rid = parseInt(params.rid, 10); |
| 244 if (!isFinite(params.rid) && !params.url) | 201 if (!isFinite(params.rid) && !params.url) |
| 245 return; | 202 return; |
| 246 // Log whether the suggestion was obtained from the server or the client. | |
| 247 chrome.embeddedSearch.newTabPage.logEvent(params.url ? | |
| 248 NTP_LOGGING_EVENT_TYPE.NTP_SERVER_SIDE_SUGGESTION : | |
| 249 NTP_LOGGING_EVENT_TYPE.NTP_CLIENT_SIDE_SUGGESTION); | |
| 250 var data; | 203 var data; |
| 251 if (params.url) { | 204 if (params.url) { |
| 252 // Means that the suggestion data comes from the server. Create data object. | 205 // Means that the suggestion data comes from the server. Create data object. |
| 253 data = { | 206 data = { |
| 254 url: params.url, | 207 url: params.url, |
| 255 largeIconUrl: params.liu || '', | 208 largeIconUrl: params.liu || '', |
| 256 thumbnailUrl: params.tu || '', | 209 thumbnailUrl: params.tu || '', |
| 257 title: params.ti || '', | 210 title: params.ti || '', |
| 258 direction: params.di || '', | 211 direction: params.di || '', |
| 259 domain: params.dom || '', | 212 domain: params.dom || '' |
| 260 tileSource: NTPLoggingTileSource.SERVER | |
| 261 }; | 213 }; |
| 262 } else { | 214 } else { |
| 263 var apiHandle = chrome.embeddedSearch.searchBox; | 215 var apiHandle = chrome.embeddedSearch.searchBox; |
| 264 data = apiHandle.getMostVisitedItemData(params.rid); | 216 data = apiHandle.getMostVisitedItemData(params.rid); |
| 265 if (!data) | 217 if (!data) |
| 266 return; | 218 return; |
| 267 data.tileSource = NTPLoggingTileSource.CLIENT; | |
| 268 } | 219 } |
| 269 | 220 |
| 270 if (isFinite(params.dummy) && parseInt(params.dummy, 10)) { | 221 if (isFinite(params.dummy) && parseInt(params.dummy, 10)) { |
| 271 data.dummy = true; | 222 data.dummy = true; |
| 272 } | 223 } |
| 273 if (/^javascript:/i.test(data.url) || | 224 if (/^javascript:/i.test(data.url) || |
| 274 /^javascript:/i.test(data.thumbnailUrl)) | 225 /^javascript:/i.test(data.thumbnailUrl)) |
| 275 return; | 226 return; |
| 276 if (data.direction) | 227 if (data.direction) |
| 277 document.body.dir = data.direction; | 228 document.body.dir = data.direction; |
| 278 fill(params, data); | 229 fill(params, data); |
| 279 } | 230 } |
| 280 | 231 |
| 281 | 232 |
| 282 /** | 233 /** |
| 283 * Sends a POST request to ping url. | 234 * Sends a POST request to ping url. |
| 284 * @param {string} url URL to be pinged. | 235 * @param {string} url URL to be pinged. |
| 285 */ | 236 */ |
| 286 function generatePing(url) { | 237 function generatePing(url) { |
| 287 if (navigator.sendBeacon) { | 238 if (navigator.sendBeacon) { |
| 288 navigator.sendBeacon(url); | 239 navigator.sendBeacon(url); |
| 289 } else { | 240 } else { |
| 290 // if sendBeacon is not enabled, we fallback for "a ping". | 241 // if sendBeacon is not enabled, we fallback for "a ping". |
| 291 var a = document.createElement('a'); | 242 var a = document.createElement('a'); |
| 292 a.href = '#'; | 243 a.href = '#'; |
| 293 a.ping = url; | 244 a.ping = url; |
| 294 a.click(); | 245 a.click(); |
| 295 } | 246 } |
| 296 } | 247 } |
| OLD | NEW |