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 * @const {string} | |
65 */ | |
66 var DOMAIN_ORIGIN = '{{ORIGIN}}'; | |
67 | |
68 /** | |
64 * Parses query parameters from Location. | 69 * Parses query parameters from Location. |
65 * @param {string} location The URL to generate the CSS url for. | 70 * @param {string} location The URL to generate the CSS url for. |
66 * @return {Object} Dictionary containing name value pairs for URL. | 71 * @return {Object} Dictionary containing name value pairs for URL. |
67 */ | 72 */ |
68 function parseQueryParams(location) { | 73 function parseQueryParams(location) { |
69 var params = Object.create(null); | 74 var params = Object.create(null); |
70 var query = location.search.substring(1); | 75 var query = location.search.substring(1); |
71 var vars = query.split('&'); | 76 var vars = query.split('&'); |
72 for (var i = 0; i < vars.length; i++) { | 77 for (var i = 0; i < vars.length; i++) { |
73 var pair = vars[i].split('='); | 78 var pair = vars[i].split('='); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 link.textContent = text; | 119 link.textContent = text; |
115 link.addEventListener('mouseover', function() { | 120 link.addEventListener('mouseover', function() { |
116 var ntpApiHandle = chrome.embeddedSearch.newTabPage; | 121 var ntpApiHandle = chrome.embeddedSearch.newTabPage; |
117 ntpApiHandle.logEvent(NTP_LOGGING_EVENT_TYPE.NTP_MOUSEOVER); | 122 ntpApiHandle.logEvent(NTP_LOGGING_EVENT_TYPE.NTP_MOUSEOVER); |
118 }); | 123 }); |
119 | 124 |
120 // Webkit's security policy prevents some Most Visited thumbnails from | 125 // Webkit's security policy prevents some Most Visited thumbnails from |
121 // working (those with schemes different from http and https). Therefore, | 126 // working (those with schemes different from http and https). Therefore, |
122 // navigateContentWindow is being used in order to get all schemes working. | 127 // navigateContentWindow is being used in order to get all schemes working. |
123 link.addEventListener('click', function handleNavigation(e) { | 128 link.addEventListener('click', function handleNavigation(e) { |
129 if (params.ping) { | |
Mathieu
2014/07/31 18:56:05
A possibility, while avoiding a separate request j
Jered
2014/08/01 16:09:44
Can you provide a bit more context for this change
| |
130 generatePing(DOMAIN_ORIGIN + params.ping); | |
131 } | |
132 | |
124 var ntpApiHandle = chrome.embeddedSearch.newTabPage; | 133 var ntpApiHandle = chrome.embeddedSearch.newTabPage; |
125 if ('pos' in params && isFinite(params.pos)) { | 134 if ('pos' in params && isFinite(params.pos)) { |
126 ntpApiHandle.logMostVisitedNavigation(parseInt(params.pos, 10), | 135 ntpApiHandle.logMostVisitedNavigation(parseInt(params.pos, 10), |
127 provider || ''); | 136 provider || ''); |
128 } | 137 } |
129 var isServerSuggestion = 'url' in params; | 138 var isServerSuggestion = 'url' in params; |
130 if (!isServerSuggestion) { | 139 if (!isServerSuggestion) { |
131 e.preventDefault(); | 140 e.preventDefault(); |
132 ntpApiHandle.navigateContentWindow(href, getDispositionFromEvent(e)); | 141 ntpApiHandle.navigateContentWindow(href, getDispositionFromEvent(e)); |
133 } | 142 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 return styles; | 177 return styles; |
169 } | 178 } |
170 | 179 |
171 | 180 |
172 /** | 181 /** |
173 * @param {string} location A location containing URL parameters. | 182 * @param {string} location A location containing URL parameters. |
174 * @param {function(Object, Object)} fill A function called with styles and | 183 * @param {function(Object, Object)} fill A function called with styles and |
175 * data to fill. | 184 * data to fill. |
176 */ | 185 */ |
177 function fillMostVisited(location, fill) { | 186 function fillMostVisited(location, fill) { |
178 var params = parseQueryParams(document.location); | 187 var params = parseQueryParams(location); |
179 params.rid = parseInt(params.rid, 10); | 188 params.rid = parseInt(params.rid, 10); |
180 if (!isFinite(params.rid) && !params.url) | 189 if (!isFinite(params.rid) && !params.url) |
181 return; | 190 return; |
182 // Log whether the suggestion was obtained from the server or the client. | 191 // Log whether the suggestion was obtained from the server or the client. |
183 chrome.embeddedSearch.newTabPage.logEvent(params.url ? | 192 chrome.embeddedSearch.newTabPage.logEvent(params.url ? |
184 NTP_LOGGING_EVENT_TYPE.NTP_SERVER_SIDE_SUGGESTION : | 193 NTP_LOGGING_EVENT_TYPE.NTP_SERVER_SIDE_SUGGESTION : |
185 NTP_LOGGING_EVENT_TYPE.NTP_CLIENT_SIDE_SUGGESTION); | 194 NTP_LOGGING_EVENT_TYPE.NTP_CLIENT_SIDE_SUGGESTION); |
186 var data = {}; | 195 var data = {}; |
187 if (params.url) { | 196 if (params.url) { |
188 // Means that the suggestion data comes from the server. Create data object. | 197 // 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; | 214 data.provider = params.pr || CLIENT_PROVIDER_NAME; |
206 } | 215 } |
207 if (/^javascript:/i.test(data.url) || | 216 if (/^javascript:/i.test(data.url) || |
208 /^javascript:/i.test(data.thumbnailUrl) || | 217 /^javascript:/i.test(data.thumbnailUrl) || |
209 !/^[a-z0-9]{0,8}$/i.test(data.provider)) | 218 !/^[a-z0-9]{0,8}$/i.test(data.provider)) |
210 return; | 219 return; |
211 if (data.direction) | 220 if (data.direction) |
212 document.body.dir = data.direction; | 221 document.body.dir = data.direction; |
213 fill(params, data); | 222 fill(params, data); |
214 } | 223 } |
224 | |
225 | |
226 /** | |
227 * Sends a POST request to ping url. | |
228 * @param {string} url URL to be pinged. | |
229 */ | |
230 function generatePing(url) { | |
231 if (navigator.sendBeacon) { | |
232 navigator.sendBeacon(url); | |
233 } else { | |
234 // if sendBeacon is not enabled, we fallback for "a ping". | |
235 var a = document.createElement('a'); | |
236 a.href = '#'; | |
237 a.ping = url; | |
238 a.click(); | |
239 } | |
240 } | |
OLD | NEW |