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"> |
(...skipping 29 matching lines...) Expand all Loading... | |
40 NTP_GRAY_TILE_FALLBACK: 7, | 40 NTP_GRAY_TILE_FALLBACK: 7, |
41 // The visuals of that tile's fallback are handled externally. | 41 // The visuals of that tile's fallback are handled externally. |
42 NTP_EXTERNAL_TILE_FALLBACK: 8, | 42 NTP_EXTERNAL_TILE_FALLBACK: 8, |
43 // The user moused over an NTP tile or title. | 43 // The user moused over an NTP tile or title. |
44 NTP_MOUSEOVER: 9, | 44 NTP_MOUSEOVER: 9, |
45 // A NTP Tile has finished loading (successfully or failing). | 45 // A NTP Tile has finished loading (successfully or failing). |
46 NTP_TILE_LOADED: 10, | 46 NTP_TILE_LOADED: 10, |
47 }; | 47 }; |
48 | 48 |
49 /** | 49 /** |
50 * Type of the impression provider for a generic client-provided suggestion. | 50 * The different sources that an NTP tile can have. |
51 * @type {string} | 51 * Note: Keep in sync with common/ntp_logging_events.h |
52 * @enum {number} | |
52 * @const | 53 * @const |
53 */ | 54 */ |
54 var CLIENT_PROVIDER_NAME = 'client'; | 55 var NTP_LOGGING_TILE_SOURCE = { |
Bernhard Bauer
2016/07/05 14:31:54
Why do we duplicate this again?
Marc Treib
2016/07/05 14:37:53
most_visited_util.js is used by the old, multi-ifr
Bernhard Bauer
2016/07/05 14:43:42
Up to you. A cleanup CL is not the worst place to
Marc Treib
2016/07/05 14:46:42
Since that would be a fairly big and orthogonal ch
Bernhard Bauer
2016/07/05 14:59:42
Add a TODO then? :)
Marc Treib
2016/07/05 15:10:16
Done.
| |
55 | 56 NTP_TILE_SOURCE_CLIENT: 0, |
56 /** | 57 NTP_TILE_SOURCE_SERVER: 1, |
57 * Type of the impression provider for a generic server-provided suggestion. | 58 }; |
58 * @type {string} | |
59 * @const | |
60 */ | |
61 var SERVER_PROVIDER_NAME = 'server'; | |
62 | 59 |
63 /** | 60 /** |
64 * The origin of this request. | 61 * The origin of this request. |
65 * @const {string} | 62 * @const {string} |
66 */ | 63 */ |
67 var DOMAIN_ORIGIN = '{{ORIGIN}}'; | 64 var DOMAIN_ORIGIN = '{{ORIGIN}}'; |
68 | 65 |
69 /** | 66 /** |
70 * Parses query parameters from Location. | 67 * Parses query parameters from Location. |
71 * @param {string} location The URL to generate the CSS url for. | 68 * @param {string} location The URL to generate the CSS url for. |
(...skipping 19 matching lines...) Expand all Loading... | |
91 } | 88 } |
92 | 89 |
93 | 90 |
94 /** | 91 /** |
95 * Creates a new most visited link element. | 92 * Creates a new most visited link element. |
96 * @param {Object} params URL parameters containing styles for the link. | 93 * @param {Object} params URL parameters containing styles for the link. |
97 * @param {string} href The destination for the link. | 94 * @param {string} href The destination for the link. |
98 * @param {string} title The title for the link. | 95 * @param {string} title The title for the link. |
99 * @param {string|undefined} text The text for the link or none. | 96 * @param {string|undefined} text The text for the link or none. |
100 * @param {string|undefined} direction The text direction. | 97 * @param {string|undefined} direction The text direction. |
101 * @param {string|undefined} provider A provider name (max 8 alphanumeric | 98 * @param {number} tileSource The source from TILE_SOURCE. |
102 * characters) used for logging. Undefined if suggestion is not coming from | |
103 * the server. | |
104 * @return {HTMLAnchorElement} A new link element. | 99 * @return {HTMLAnchorElement} A new link element. |
105 */ | 100 */ |
106 function createMostVisitedLink(params, href, title, text, direction, provider) { | 101 function createMostVisitedLink( |
102 params, href, title, text, direction, tileSource) { | |
107 var styles = getMostVisitedStyles(params, !!text); | 103 var styles = getMostVisitedStyles(params, !!text); |
108 var link = document.createElement('a'); | 104 var link = document.createElement('a'); |
109 link.style.color = styles.color; | 105 link.style.color = styles.color; |
110 link.style.fontSize = styles.fontSize + 'px'; | 106 link.style.fontSize = styles.fontSize + 'px'; |
111 if (styles.fontFamily) | 107 if (styles.fontFamily) |
112 link.style.fontFamily = styles.fontFamily; | 108 link.style.fontFamily = styles.fontFamily; |
113 if (styles.textAlign) | 109 if (styles.textAlign) |
114 link.style.textAlign = styles.textAlign; | 110 link.style.textAlign = styles.textAlign; |
115 if (styles.textFadePos) { | 111 if (styles.textFadePos) { |
116 var dir = /^rtl$/i.test(direction) ? 'to left' : 'to right'; | 112 var dir = /^rtl$/i.test(direction) ? 'to left' : 'to right'; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 var isServerSuggestion = 'url' in params; | 147 var isServerSuggestion = 'url' in params; |
152 | 148 |
153 // Ping are only populated for server-side suggestions, never for MV. | 149 // Ping are only populated for server-side suggestions, never for MV. |
154 if (isServerSuggestion && params.ping) { | 150 if (isServerSuggestion && params.ping) { |
155 generatePing(DOMAIN_ORIGIN + params.ping); | 151 generatePing(DOMAIN_ORIGIN + params.ping); |
156 } | 152 } |
157 | 153 |
158 var ntpApiHandle = chrome.embeddedSearch.newTabPage; | 154 var ntpApiHandle = chrome.embeddedSearch.newTabPage; |
159 if ('pos' in params && isFinite(params.pos)) { | 155 if ('pos' in params && isFinite(params.pos)) { |
160 ntpApiHandle.logMostVisitedNavigation(parseInt(params.pos, 10), | 156 ntpApiHandle.logMostVisitedNavigation(parseInt(params.pos, 10), |
161 provider || ''); | 157 tileSource); |
162 } | 158 } |
163 | 159 |
164 // Follow <a> normally, so transition type will be LINK. | 160 // Follow <a> normally, so transition type will be LINK. |
165 }; | 161 }; |
166 | 162 |
167 link.addEventListener('click', navigateFunction); | 163 link.addEventListener('click', navigateFunction); |
168 link.addEventListener('keydown', function(event) { | 164 link.addEventListener('keydown', function(event) { |
169 if (event.keyCode == 46 /* DELETE */ || | 165 if (event.keyCode == 46 /* DELETE */ || |
170 event.keyCode == 8 /* BACKSPACE */) { | 166 event.keyCode == 8 /* BACKSPACE */) { |
171 event.preventDefault(); | 167 event.preventDefault(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 var data; | 266 var data; |
271 if (params.url) { | 267 if (params.url) { |
272 // Means that the suggestion data comes from the server. Create data object. | 268 // Means that the suggestion data comes from the server. Create data object. |
273 data = { | 269 data = { |
274 url: params.url, | 270 url: params.url, |
275 largeIconUrl: params.liu || '', | 271 largeIconUrl: params.liu || '', |
276 thumbnailUrl: params.tu || '', | 272 thumbnailUrl: params.tu || '', |
277 title: params.ti || '', | 273 title: params.ti || '', |
278 direction: params.di || '', | 274 direction: params.di || '', |
279 domain: params.dom || '', | 275 domain: params.dom || '', |
280 provider: params.pr || SERVER_PROVIDER_NAME | 276 tileSource: NTP_LOGGING_TILE_SOURCE.NTP_TILE_SOURCE_SERVER |
281 }; | 277 }; |
282 } else { | 278 } else { |
283 var apiHandle = chrome.embeddedSearch.searchBox; | 279 var apiHandle = chrome.embeddedSearch.searchBox; |
284 data = apiHandle.getMostVisitedItemData(params.rid); | 280 data = apiHandle.getMostVisitedItemData(params.rid); |
285 if (!data) | 281 if (!data) |
286 return; | 282 return; |
287 // Allow server-side provider override. | 283 data.tileSource: NTP_LOGGING_TILE_SOURCE.NTP_TILE_SOURCE_CLIENT; |
288 data.provider = params.pr || CLIENT_PROVIDER_NAME; | |
289 } | 284 } |
290 | 285 |
291 if (isFinite(params.dummy) && parseInt(params.dummy, 10)) { | 286 if (isFinite(params.dummy) && parseInt(params.dummy, 10)) { |
292 data.dummy = true; | 287 data.dummy = true; |
293 } | 288 } |
294 if (/^javascript:/i.test(data.url) || | 289 if (/^javascript:/i.test(data.url) || |
295 /^javascript:/i.test(data.thumbnailUrl) || | 290 /^javascript:/i.test(data.thumbnailUrl)) |
296 !/^[a-z0-9]{0,8}$/i.test(data.provider)) | |
297 return; | 291 return; |
298 if (data.direction) | 292 if (data.direction) |
299 document.body.dir = data.direction; | 293 document.body.dir = data.direction; |
300 fill(params, data); | 294 fill(params, data); |
301 } | 295 } |
302 | 296 |
303 | 297 |
304 /** | 298 /** |
305 * Sends a POST request to ping url. | 299 * Sends a POST request to ping url. |
306 * @param {string} url URL to be pinged. | 300 * @param {string} url URL to be pinged. |
307 */ | 301 */ |
308 function generatePing(url) { | 302 function generatePing(url) { |
309 if (navigator.sendBeacon) { | 303 if (navigator.sendBeacon) { |
310 navigator.sendBeacon(url); | 304 navigator.sendBeacon(url); |
311 } else { | 305 } else { |
312 // if sendBeacon is not enabled, we fallback for "a ping". | 306 // if sendBeacon is not enabled, we fallback for "a ping". |
313 var a = document.createElement('a'); | 307 var a = document.createElement('a'); |
314 a.href = '#'; | 308 a.href = '#'; |
315 a.ping = url; | 309 a.ping = url; |
316 a.click(); | 310 a.click(); |
317 } | 311 } |
318 } | 312 } |
OLD | NEW |