| 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 <include src="window_disposition_util.js"> |
| 12 |
| 11 | 13 |
| 12 /** | 14 /** |
| 13 * The different types of events that are logged from the NTP. This enum is | 15 * The different types of events that are logged from the NTP. This enum is |
| 14 * used to transfer information from the NTP javascript to the renderer and is | 16 * used to transfer information from the NTP javascript to the renderer and is |
| 15 * not used as a UMA enum histogram's logged value. | 17 * not used as a UMA enum histogram's logged value. |
| 16 * Note: Keep in sync with common/ntp_logging_events.h | 18 * Note: Keep in sync with common/ntp_logging_events.h |
| 17 * @enum {number} | 19 * @enum {number} |
| 18 * @const | 20 * @const |
| 19 */ | 21 */ |
| 20 var NTP_LOGGING_EVENT_TYPE = { | 22 var NTP_LOGGING_EVENT_TYPE = { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 36 // (if it was provided), resulting in a grey tile. | 38 // (if it was provided), resulting in a grey tile. |
| 37 NTP_THUMBNAIL_ERROR: 6, | 39 NTP_THUMBNAIL_ERROR: 6, |
| 38 // 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. |
| 39 NTP_GRAY_TILE_FALLBACK: 7, | 41 NTP_GRAY_TILE_FALLBACK: 7, |
| 40 // The visuals of that tile's fallback are handled externally. | 42 // The visuals of that tile's fallback are handled externally. |
| 41 NTP_EXTERNAL_TILE_FALLBACK: 8, | 43 NTP_EXTERNAL_TILE_FALLBACK: 8, |
| 42 // The user moused over an NTP tile or title. | 44 // The user moused over an NTP tile or title. |
| 43 NTP_MOUSEOVER: 9 | 45 NTP_MOUSEOVER: 9 |
| 44 }; | 46 }; |
| 45 | 47 |
| 48 |
| 46 /** | 49 /** |
| 47 * Type of the impression provider for a generic client-provided suggestion. | 50 * Type of the impression provider for a generic client-provided suggestion. |
| 48 * @type {string} | 51 * @type {string} |
| 49 * @const | 52 * @const |
| 50 */ | 53 */ |
| 51 var CLIENT_PROVIDER_NAME = 'client'; | 54 var CLIENT_PROVIDER_NAME = 'client'; |
| 52 | 55 |
| 53 /** | 56 /** |
| 54 * Type of the impression provider for a generic server-provided suggestion. | 57 * Type of the impression provider for a generic server-provided suggestion. |
| 55 * @type {string} | 58 * @type {string} |
| (...skipping 25 matching lines...) Expand all Loading... |
| 81 return params; | 84 return params; |
| 82 } | 85 } |
| 83 | 86 |
| 84 | 87 |
| 85 /** | 88 /** |
| 86 * Creates a new most visited link element. | 89 * Creates a new most visited link element. |
| 87 * @param {Object} params URL parameters containing styles for the link. | 90 * @param {Object} params URL parameters containing styles for the link. |
| 88 * @param {string} href The destination for the link. | 91 * @param {string} href The destination for the link. |
| 89 * @param {string} title The title for the link. | 92 * @param {string} title The title for the link. |
| 90 * @param {string|undefined} text The text for the link or none. | 93 * @param {string|undefined} text The text for the link or none. |
| 91 * @param {string|undefined} ping If specified, a location relative to the | |
| 92 * referrer of this iframe, to ping when the link is clicked. Only works if | |
| 93 * the referrer is HTTPS. | |
| 94 * @param {string|undefined} provider A provider name (max 8 alphanumeric | 94 * @param {string|undefined} provider A provider name (max 8 alphanumeric |
| 95 * characters) used for logging. Undefined if suggestion is not coming from | 95 * characters) used for logging. Undefined if suggestion is not coming from |
| 96 * the server. | 96 * the server. |
| 97 * @return {HTMLAnchorElement} A new link element. | 97 * @return {HTMLAnchorElement} A new link element. |
| 98 */ | 98 */ |
| 99 function createMostVisitedLink(params, href, title, text, ping, provider) { | 99 function createMostVisitedLink(params, href, title, text, provider) { |
| 100 var styles = getMostVisitedStyles(params, !!text); | 100 var styles = getMostVisitedStyles(params, !!text); |
| 101 var link = document.createElement('a'); | 101 var link = document.createElement('a'); |
| 102 link.style.color = styles.color; | 102 link.style.color = styles.color; |
| 103 link.style.fontSize = styles.fontSize + 'px'; | 103 link.style.fontSize = styles.fontSize + 'px'; |
| 104 if (styles.fontFamily) | 104 if (styles.fontFamily) |
| 105 link.style.fontFamily = styles.fontFamily; | 105 link.style.fontFamily = styles.fontFamily; |
| 106 |
| 106 link.href = href; | 107 link.href = href; |
| 107 if ('pos' in params && isFinite(params.pos)) { | |
| 108 link.ping = '/log.html?pos=' + params.pos; | |
| 109 if (provider) | |
| 110 link.ping += '&pr=' + provider; | |
| 111 // If a ping parameter was specified, add it to the list of pings, relative | |
| 112 // to the referrer of this iframe, which is the default search provider. | |
| 113 if (ping) { | |
| 114 var parentUrl = document.createElement('a'); | |
| 115 parentUrl.href = document.referrer; | |
| 116 if (parentUrl.protocol == 'https:') { | |
| 117 link.ping += ' ' + parentUrl.origin + '/' + ping; | |
| 118 } | |
| 119 } | |
| 120 } | |
| 121 link.title = title; | 108 link.title = title; |
| 122 link.target = '_top'; | 109 link.target = '_top'; |
| 123 // Exclude links from the tab order. The tabIndex is added to the thumbnail | 110 // Exclude links from the tab order. The tabIndex is added to the thumbnail |
| 124 // parent container instead. | 111 // parent container instead. |
| 125 link.tabIndex = '-1'; | 112 link.tabIndex = '-1'; |
| 126 if (text) | 113 if (text) |
| 127 link.textContent = text; | 114 link.textContent = text; |
| 128 link.addEventListener('mouseover', function() { | 115 link.addEventListener('mouseover', function() { |
| 129 var ntpApiHandle = chrome.embeddedSearch.newTabPage; | 116 var ntpApiHandle = chrome.embeddedSearch.newTabPage; |
| 130 ntpApiHandle.logEvent(NTP_LOGGING_EVENT_TYPE.NTP_MOUSEOVER); | 117 ntpApiHandle.logEvent(NTP_LOGGING_EVENT_TYPE.NTP_MOUSEOVER); |
| 131 }); | 118 }); |
| 119 |
| 120 // Webkit's security policy prevents some Most Visited thumbnails from |
| 121 // working (those with schemes different from http and https). Therefore, |
| 122 // navigateContentWindow is being used in order to get all schemes working. |
| 123 link.addEventListener('click', function handleNavigation(e) { |
| 124 e.preventDefault(); |
| 125 var ntpApiHandle = chrome.embeddedSearch.newTabPage; |
| 126 if ('pos' in params && isFinite(params.pos)) |
| 127 ntpApiHandle.logNavigation(parseInt(params.pos, 10), provider || ''); |
| 128 ntpApiHandle.navigateContentWindow(href, getDispositionFromEvent(e)); |
| 129 }); |
| 130 |
| 132 return link; | 131 return link; |
| 133 } | 132 } |
| 134 | 133 |
| 135 | 134 |
| 136 /** | 135 /** |
| 137 * Decodes most visited styles from URL parameters. | 136 * Decodes most visited styles from URL parameters. |
| 138 * - f: font-family | 137 * - f: font-family |
| 139 * - fs: font-size as a number in pixels. | 138 * - fs: font-size as a number in pixels. |
| 140 * - c: A hexadecimal number interpreted as a hex color code. | 139 * - c: A hexadecimal number interpreted as a hex color code. |
| 141 * @param {Object.<string, string>} params URL parameters specifying style. | 140 * @param {Object.<string, string>} params URL parameters specifying style. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 NTP_LOGGING_EVENT_TYPE.NTP_SERVER_SIDE_SUGGESTION : | 178 NTP_LOGGING_EVENT_TYPE.NTP_SERVER_SIDE_SUGGESTION : |
| 180 NTP_LOGGING_EVENT_TYPE.NTP_CLIENT_SIDE_SUGGESTION); | 179 NTP_LOGGING_EVENT_TYPE.NTP_CLIENT_SIDE_SUGGESTION); |
| 181 var data = {}; | 180 var data = {}; |
| 182 if (params.url) { | 181 if (params.url) { |
| 183 // Means that the suggestion data comes from the server. Create data object. | 182 // Means that the suggestion data comes from the server. Create data object. |
| 184 data.url = params.url; | 183 data.url = params.url; |
| 185 data.thumbnailUrl = params.tu || ''; | 184 data.thumbnailUrl = params.tu || ''; |
| 186 data.title = params.ti || ''; | 185 data.title = params.ti || ''; |
| 187 data.direction = params.di || ''; | 186 data.direction = params.di || ''; |
| 188 data.domain = params.dom || ''; | 187 data.domain = params.dom || ''; |
| 189 data.ping = params.ping || ''; | |
| 190 data.provider = params.pr || SERVER_PROVIDER_NAME; | 188 data.provider = params.pr || SERVER_PROVIDER_NAME; |
| 191 | 189 |
| 192 // Log the fact that suggestion was obtained from the server. | 190 // Log the fact that suggestion was obtained from the server. |
| 193 var ntpApiHandle = chrome.embeddedSearch.newTabPage; | 191 var ntpApiHandle = chrome.embeddedSearch.newTabPage; |
| 194 ntpApiHandle.logEvent(NTP_LOGGING_EVENT_TYPE.NTP_SERVER_SIDE_SUGGESTION); | 192 ntpApiHandle.logEvent(NTP_LOGGING_EVENT_TYPE.NTP_SERVER_SIDE_SUGGESTION); |
| 195 } else { | 193 } else { |
| 196 var apiHandle = chrome.embeddedSearch.searchBox; | 194 var apiHandle = chrome.embeddedSearch.searchBox; |
| 197 data = apiHandle.getMostVisitedItemData(params.rid); | 195 data = apiHandle.getMostVisitedItemData(params.rid); |
| 198 if (!data) | 196 if (!data) |
| 199 return; | 197 return; |
| 200 data.provider = CLIENT_PROVIDER_NAME; | 198 data.provider = CLIENT_PROVIDER_NAME; |
| 201 delete data.ping; | |
| 202 } | 199 } |
| 203 if (/^javascript:/i.test(data.url) || | 200 if (/^javascript:/i.test(data.url) || |
| 204 /^javascript:/i.test(data.thumbnailUrl) || | 201 /^javascript:/i.test(data.thumbnailUrl) || |
| 205 !/^[a-z0-9]{0,8}$/i.test(data.provider)) | 202 !/^[a-z0-9]{0,8}$/i.test(data.provider)) |
| 206 return; | 203 return; |
| 207 if (data.direction) | 204 if (data.direction) |
| 208 document.body.dir = data.direction; | 205 document.body.dir = data.direction; |
| 209 fill(params, data); | 206 fill(params, data); |
| 210 } | 207 } |
| OLD | NEW |