Index: chrome/browser/resources/local_ntp/most_visited_thumbnail.js |
diff --git a/chrome/browser/resources/local_ntp/most_visited_thumbnail.js b/chrome/browser/resources/local_ntp/most_visited_thumbnail.js |
index 9201259e31e8c30aa82b6f54fe21481bd88085a8..6c99bc98f93e280160ffd0c2ffbd9dec09625edd 100644 |
--- a/chrome/browser/resources/local_ntp/most_visited_thumbnail.js |
+++ b/chrome/browser/resources/local_ntp/most_visited_thumbnail.js |
@@ -17,86 +17,64 @@ window.addEventListener('DOMContentLoaded', function() { |
function logImpression(tileIndex, provider) { |
chrome.embeddedSearch.newTabPage.logImpression(tileIndex, provider); |
} |
+ function displayLink(link) { |
+ document.body.appendChild(link); |
+ window.parent.postMessage('linkDisplayed', '*'); |
+ } |
function showDomainElement() { |
- logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR); |
var link = createMostVisitedLink( |
params, data.url, data.title, undefined, data.ping, data.provider); |
var domain = document.createElement('div'); |
domain.textContent = data.domain; |
link.appendChild(domain); |
- document.body.appendChild(link); |
+ displayLink(link); |
} |
// Called on intentionally empty tiles for which the visuals are handled |
// externally by the page itself. |
function showEmptyTile() { |
- var link = createMostVisitedLink( |
- params, data.url, data.title, undefined, data.ping, data.provider); |
- document.body.appendChild(link); |
- logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE); |
+ displayLink(createMostVisitedLink( |
+ params, data.url, data.title, undefined, data.ping, data.provider)); |
} |
- function createAndAppendThumbnail(isVisible) { |
+ // Creates and adds an image. |
+ function createThumbnail(src) { |
var image = new Image(); |
image.onload = function() { |
var shadow = document.createElement('span'); |
shadow.classList.add('shadow'); |
var link = createMostVisitedLink( |
- params, data.url, data.title, undefined, data.ping, data.provider); |
+ params, data.url, data.title, undefined, data.ping, |
+ data.provider); |
link.appendChild(shadow); |
link.appendChild(image); |
- // We add 'position: absolute' in anticipation that there could be more |
- // than one thumbnail. This will superpose the elements. |
- link.style.position = 'absolute'; |
- document.body.appendChild(link); |
+ displayLink(link); |
+ }; |
+ image.onerror = function() { |
+ logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR); |
+ if (data.domain) { |
+ logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE_FALLBACK); |
+ showDomainElement(); |
+ } else { |
+ logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE_FALLBACK); |
+ showEmptyTile(); |
+ } |
}; |
- if (!isVisible) { |
- image.style.visibility = 'hidden'; |
- } |
- return image; |
+ image.src = src; |
} |
+ |
// Log an impression if we know the position of the tile. |
if (isFinite(params.pos) && data.provider) { |
logImpression(parseInt(params.pos, 10), data.provider); |
} |
+ logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE); |
if (data.thumbnailUrl) { |
- var image = createAndAppendThumbnail(true); |
- // If a backup thumbnail URL was provided, preload it in case the first |
- // thumbnail errors. The backup thumbnail is always preloaded so that the |
- // server can't gain knowledge on the local thumbnail DB by specifying a |
- // second URL that is only sometimes fetched. |
- if (data.thumbnailUrl2) { |
- var image2 = createAndAppendThumbnail(false); |
- var imageFailed = false; |
- var image2Failed = false; |
- image2.onerror = function() { |
- image2Failed = true; |
- image2.style.visibility = 'hidden'; |
- if (imageFailed) { |
- showDomainElement(); |
- } |
- }; |
- image2.src = data.thumbnailUrl2; |
- // The first thumbnail's onerror function will swap the visibility of |
- // the two thumbnails. |
- image.onerror = function() { |
- logEvent(NTP_LOGGING_EVENT_TYPE.NTP_FALLBACK_THUMBNAIL_USED); |
- imageFailed = true; |
- image.style.visibility = 'hidden'; |
- if (image2Failed) { |
- showDomainElement(); |
- } else { |
- image2.style.visibility = 'visible'; |
- } |
- }; |
- logEvent(NTP_LOGGING_EVENT_TYPE.NTP_FALLBACK_THUMBNAIL_REQUESTED); |
- } else { |
- image.onerror = showDomainElement; |
- } |
- image.src = data.thumbnailUrl; |
- logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ATTEMPT); |
+ logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_TILE); |
+ createThumbnail(data.thumbnailUrl); |
} else if (data.domain) { |
+ logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE); |
showDomainElement(); |
} else { |
+ logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE); |
showEmptyTile(); |
} |
}); |