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 Rendering for iframed most visited thumbnails. | 7 * @fileoverview Rendering for iframed most visited thumbnails. |
8 */ | 8 */ |
9 | 9 |
10 window.addEventListener('DOMContentLoaded', function() { | 10 window.addEventListener('DOMContentLoaded', function() { |
11 'use strict'; | 11 'use strict'; |
12 | 12 |
13 fillMostVisited(document.location, function(params, data) { | 13 fillMostVisited(document.location, function(params, data) { |
14 function logEvent(eventName) { | 14 function logEvent(eventName) { |
15 chrome.embeddedSearch.newTabPage.logEvent(eventName); | 15 chrome.embeddedSearch.newTabPage.logEvent(eventName); |
16 } | 16 } |
17 function logImpression(tileIndex, provider) { | 17 function logImpression(tileIndex, provider) { |
18 chrome.embeddedSearch.newTabPage.logImpression(tileIndex, provider); | 18 chrome.embeddedSearch.newTabPage.logImpression(tileIndex, provider); |
19 } | 19 } |
| 20 function displayLink(link) { |
| 21 document.body.appendChild(link); |
| 22 window.parent.postMessage('linkDisplayed', '*'); |
| 23 } |
20 function showDomainElement() { | 24 function showDomainElement() { |
21 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR); | |
22 var link = createMostVisitedLink( | 25 var link = createMostVisitedLink( |
23 params, data.url, data.title, undefined, data.ping, data.provider); | 26 params, data.url, data.title, undefined, data.ping, data.provider); |
24 var domain = document.createElement('div'); | 27 var domain = document.createElement('div'); |
25 domain.textContent = data.domain; | 28 domain.textContent = data.domain; |
26 link.appendChild(domain); | 29 link.appendChild(domain); |
27 document.body.appendChild(link); | 30 displayLink(link); |
28 } | 31 } |
29 // Called on intentionally empty tiles for which the visuals are handled | 32 // Called on intentionally empty tiles for which the visuals are handled |
30 // externally by the page itself. | 33 // externally by the page itself. |
31 function showEmptyTile() { | 34 function showEmptyTile() { |
32 var link = createMostVisitedLink( | 35 displayLink(createMostVisitedLink( |
33 params, data.url, data.title, undefined, data.ping, data.provider); | 36 params, data.url, data.title, undefined, data.ping, data.provider)); |
34 document.body.appendChild(link); | |
35 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE); | |
36 } | 37 } |
37 function createAndAppendThumbnail(isVisible) { | 38 // Creates and adds an image. |
| 39 function createThumbnail(src) { |
38 var image = new Image(); | 40 var image = new Image(); |
39 image.onload = function() { | 41 image.onload = function() { |
40 var shadow = document.createElement('span'); | 42 var shadow = document.createElement('span'); |
41 shadow.classList.add('shadow'); | 43 shadow.classList.add('shadow'); |
42 var link = createMostVisitedLink( | 44 var link = createMostVisitedLink( |
43 params, data.url, data.title, undefined, data.ping, data.provider); | 45 params, data.url, data.title, undefined, data.ping, |
| 46 data.provider); |
44 link.appendChild(shadow); | 47 link.appendChild(shadow); |
45 link.appendChild(image); | 48 link.appendChild(image); |
46 // We add 'position: absolute' in anticipation that there could be more | 49 displayLink(link); |
47 // than one thumbnail. This will superpose the elements. | |
48 link.style.position = 'absolute'; | |
49 document.body.appendChild(link); | |
50 }; | 50 }; |
51 if (!isVisible) { | 51 image.onerror = function() { |
52 image.style.visibility = 'hidden'; | 52 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR); |
53 } | 53 if (data.domain) { |
54 return image; | 54 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE_FALLBACK); |
| 55 showDomainElement(); |
| 56 } else { |
| 57 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE_FALLBACK); |
| 58 showEmptyTile(); |
| 59 } |
| 60 }; |
| 61 image.src = src; |
55 } | 62 } |
| 63 |
56 // Log an impression if we know the position of the tile. | 64 // Log an impression if we know the position of the tile. |
57 if (isFinite(params.pos) && data.provider) { | 65 if (isFinite(params.pos) && data.provider) { |
58 logImpression(parseInt(params.pos, 10), data.provider); | 66 logImpression(parseInt(params.pos, 10), data.provider); |
59 } | 67 } |
60 | 68 |
| 69 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE); |
61 if (data.thumbnailUrl) { | 70 if (data.thumbnailUrl) { |
62 var image = createAndAppendThumbnail(true); | 71 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_TILE); |
63 // If a backup thumbnail URL was provided, preload it in case the first | 72 createThumbnail(data.thumbnailUrl); |
64 // thumbnail errors. The backup thumbnail is always preloaded so that the | |
65 // server can't gain knowledge on the local thumbnail DB by specifying a | |
66 // second URL that is only sometimes fetched. | |
67 if (data.thumbnailUrl2) { | |
68 var image2 = createAndAppendThumbnail(false); | |
69 var imageFailed = false; | |
70 var image2Failed = false; | |
71 image2.onerror = function() { | |
72 image2Failed = true; | |
73 image2.style.visibility = 'hidden'; | |
74 if (imageFailed) { | |
75 showDomainElement(); | |
76 } | |
77 }; | |
78 image2.src = data.thumbnailUrl2; | |
79 // The first thumbnail's onerror function will swap the visibility of | |
80 // the two thumbnails. | |
81 image.onerror = function() { | |
82 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_FALLBACK_THUMBNAIL_USED); | |
83 imageFailed = true; | |
84 image.style.visibility = 'hidden'; | |
85 if (image2Failed) { | |
86 showDomainElement(); | |
87 } else { | |
88 image2.style.visibility = 'visible'; | |
89 } | |
90 }; | |
91 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_FALLBACK_THUMBNAIL_REQUESTED); | |
92 } else { | |
93 image.onerror = showDomainElement; | |
94 } | |
95 image.src = data.thumbnailUrl; | |
96 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ATTEMPT); | |
97 } else if (data.domain) { | 73 } else if (data.domain) { |
| 74 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE); |
98 showDomainElement(); | 75 showDomainElement(); |
99 } else { | 76 } else { |
| 77 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE); |
100 showEmptyTile(); | 78 showEmptyTile(); |
101 } | 79 } |
102 }); | 80 }); |
103 }); | 81 }); |
OLD | NEW |