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 27 matching lines...) Expand all Loading... | |
38 // (if it was provided), resulting in a grey tile. | 38 // (if it was provided), resulting in a grey tile. |
39 NTP_THUMBNAIL_ERROR: 6, | 39 NTP_THUMBNAIL_ERROR: 6, |
40 // Used a gray tile with the domain as the fallback for a failed thumbnail. | 40 // Used a gray tile with the domain as the fallback for a failed thumbnail. |
41 NTP_GRAY_TILE_FALLBACK: 7, | 41 NTP_GRAY_TILE_FALLBACK: 7, |
42 // The visuals of that tile's fallback are handled externally. | 42 // The visuals of that tile's fallback are handled externally. |
43 NTP_EXTERNAL_TILE_FALLBACK: 8, | 43 NTP_EXTERNAL_TILE_FALLBACK: 8, |
44 // The user moused over an NTP tile or title. | 44 // The user moused over an NTP tile or title. |
45 NTP_MOUSEOVER: 9 | 45 NTP_MOUSEOVER: 9 |
46 }; | 46 }; |
47 | 47 |
48 | |
49 /** | 48 /** |
50 * Type of the impression provider for a generic client-provided suggestion. | 49 * Type of the impression provider for a generic client-provided suggestion. |
51 * @type {string} | 50 * @type {string} |
52 * @const | 51 * @const |
53 */ | 52 */ |
54 var CLIENT_PROVIDER_NAME = 'client'; | 53 var CLIENT_PROVIDER_NAME = 'client'; |
55 | 54 |
56 /** | 55 /** |
57 * Type of the impression provider for a generic server-provided suggestion. | 56 * Type of the impression provider for a generic server-provided suggestion. |
58 * @type {string} | 57 * @type {string} |
59 * @const | 58 * @const |
60 */ | 59 */ |
61 var SERVER_PROVIDER_NAME = 'server'; | 60 var SERVER_PROVIDER_NAME = 'server'; |
62 | 61 |
63 /** | 62 /** |
63 * The origin of this request. | |
64 * @type {string} | |
65 * @const | |
Mathieu
2014/07/30 14:12:54
You can simplify to @const {string}
fserb
2014/07/31 18:18:47
Done.
| |
66 */ | |
67 var DOMAIN_ORIGIN = '{{ORIGIN}}'; | |
68 | |
69 /** | |
64 * Parses query parameters from Location. | 70 * Parses query parameters from Location. |
65 * @param {string} location The URL to generate the CSS url for. | 71 * @param {string} location The URL to generate the CSS url for. |
66 * @return {Object} Dictionary containing name value pairs for URL. | 72 * @return {Object} Dictionary containing name value pairs for URL. |
67 */ | 73 */ |
68 function parseQueryParams(location) { | 74 function parseQueryParams(location) { |
69 var params = Object.create(null); | 75 var params = Object.create(null); |
70 var query = location.search.substring(1); | 76 var query = location.search.substring(1); |
71 var vars = query.split('&'); | 77 var vars = query.split('&'); |
72 for (var i = 0; i < vars.length; i++) { | 78 for (var i = 0; i < vars.length; i++) { |
73 var pair = vars[i].split('='); | 79 var pair = vars[i].split('='); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 link.textContent = text; | 120 link.textContent = text; |
115 link.addEventListener('mouseover', function() { | 121 link.addEventListener('mouseover', function() { |
116 var ntpApiHandle = chrome.embeddedSearch.newTabPage; | 122 var ntpApiHandle = chrome.embeddedSearch.newTabPage; |
117 ntpApiHandle.logEvent(NTP_LOGGING_EVENT_TYPE.NTP_MOUSEOVER); | 123 ntpApiHandle.logEvent(NTP_LOGGING_EVENT_TYPE.NTP_MOUSEOVER); |
118 }); | 124 }); |
119 | 125 |
120 // Webkit's security policy prevents some Most Visited thumbnails from | 126 // Webkit's security policy prevents some Most Visited thumbnails from |
121 // working (those with schemes different from http and https). Therefore, | 127 // working (those with schemes different from http and https). Therefore, |
122 // navigateContentWindow is being used in order to get all schemes working. | 128 // navigateContentWindow is being used in order to get all schemes working. |
123 link.addEventListener('click', function handleNavigation(e) { | 129 link.addEventListener('click', function handleNavigation(e) { |
130 if (params.ping) { | |
Mathieu
2014/07/30 14:12:54
nit: you can omit braces
fserb
2014/07/31 18:18:47
Acknowledged.
| |
131 generatePing(DOMAIN_ORIGIN + params.ping); | |
132 } | |
133 | |
124 var ntpApiHandle = chrome.embeddedSearch.newTabPage; | 134 var ntpApiHandle = chrome.embeddedSearch.newTabPage; |
125 if ('pos' in params && isFinite(params.pos)) { | 135 if ('pos' in params && isFinite(params.pos)) { |
126 ntpApiHandle.logMostVisitedNavigation(parseInt(params.pos, 10), | 136 ntpApiHandle.logMostVisitedNavigation(parseInt(params.pos, 10), |
127 provider || ''); | 137 provider || ''); |
128 } | 138 } |
129 var isServerSuggestion = 'url' in params; | 139 var isServerSuggestion = 'url' in params; |
130 if (!isServerSuggestion) { | 140 if (!isServerSuggestion) { |
131 e.preventDefault(); | 141 e.preventDefault(); |
132 ntpApiHandle.navigateContentWindow(href, getDispositionFromEvent(e)); | 142 ntpApiHandle.navigateContentWindow(href, getDispositionFromEvent(e)); |
133 } | 143 } |
134 // Else follow <a> normally, so transition type would be LINK. | 144 // Else follow <a> normally, so transition type would be LINK. |
Mathieu
2014/07/30 14:12:54
This comment seems to say that for server suggesti
fserb
2014/07/31 18:18:47
as discussed offline, trying not to rely on a ping
| |
135 }); | 145 }); |
136 | 146 |
137 return link; | 147 return link; |
138 } | 148 } |
139 | 149 |
140 | 150 |
141 /** | 151 /** |
142 * Decodes most visited styles from URL parameters. | 152 * Decodes most visited styles from URL parameters. |
143 * - f: font-family | 153 * - f: font-family |
144 * - fs: font-size as a number in pixels. | 154 * - fs: font-size as a number in pixels. |
(...skipping 23 matching lines...) Expand all Loading... | |
168 return styles; | 178 return styles; |
169 } | 179 } |
170 | 180 |
171 | 181 |
172 /** | 182 /** |
173 * @param {string} location A location containing URL parameters. | 183 * @param {string} location A location containing URL parameters. |
174 * @param {function(Object, Object)} fill A function called with styles and | 184 * @param {function(Object, Object)} fill A function called with styles and |
175 * data to fill. | 185 * data to fill. |
176 */ | 186 */ |
177 function fillMostVisited(location, fill) { | 187 function fillMostVisited(location, fill) { |
178 var params = parseQueryParams(document.location); | 188 var params = parseQueryParams(location); |
179 params.rid = parseInt(params.rid, 10); | 189 params.rid = parseInt(params.rid, 10); |
180 if (!isFinite(params.rid) && !params.url) | 190 if (!isFinite(params.rid) && !params.url) |
181 return; | 191 return; |
182 // Log whether the suggestion was obtained from the server or the client. | 192 // Log whether the suggestion was obtained from the server or the client. |
183 chrome.embeddedSearch.newTabPage.logEvent(params.url ? | 193 chrome.embeddedSearch.newTabPage.logEvent(params.url ? |
184 NTP_LOGGING_EVENT_TYPE.NTP_SERVER_SIDE_SUGGESTION : | 194 NTP_LOGGING_EVENT_TYPE.NTP_SERVER_SIDE_SUGGESTION : |
185 NTP_LOGGING_EVENT_TYPE.NTP_CLIENT_SIDE_SUGGESTION); | 195 NTP_LOGGING_EVENT_TYPE.NTP_CLIENT_SIDE_SUGGESTION); |
186 var data = {}; | 196 var data = {}; |
187 if (params.url) { | 197 if (params.url) { |
188 // Means that the suggestion data comes from the server. Create data object. | 198 // Means that the suggestion data comes from the server. Create data object. |
(...skipping 16 matching lines...) Expand all Loading... | |
205 data.provider = params.pr || CLIENT_PROVIDER_NAME; | 215 data.provider = params.pr || CLIENT_PROVIDER_NAME; |
206 } | 216 } |
207 if (/^javascript:/i.test(data.url) || | 217 if (/^javascript:/i.test(data.url) || |
208 /^javascript:/i.test(data.thumbnailUrl) || | 218 /^javascript:/i.test(data.thumbnailUrl) || |
209 !/^[a-z0-9]{0,8}$/i.test(data.provider)) | 219 !/^[a-z0-9]{0,8}$/i.test(data.provider)) |
210 return; | 220 return; |
211 if (data.direction) | 221 if (data.direction) |
212 document.body.dir = data.direction; | 222 document.body.dir = data.direction; |
213 fill(params, data); | 223 fill(params, data); |
214 } | 224 } |
225 | |
226 | |
227 /** | |
228 * Sends a POST request to ping url. | |
229 * @param {string} url URL to be pinged. | |
230 */ | |
231 function generatePing(url) { | |
232 if (navigator.sendBeacon) { | |
233 navigator.sendBeacon(url); | |
234 } else { | |
235 // sendBeacon is still in experimental, so for now we use 'a ping' | |
Mathieu
2014/07/30 14:12:54
you could say: "If sendBeacon is not enabled, use
fserb
2014/07/31 18:18:47
Done.
| |
236 var a = document.createElement('a'); | |
237 a.href = '#'; | |
238 a.ping = url; | |
239 a.click(); | |
240 } | |
241 } | |
OLD | NEW |