Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: chrome/browser/resources/local_ntp/most_visited_thumbnail.js

Issue 102433009: Most visited iframe now postMessage to signal the iframing page that the link has been displayed (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed support for multiple thumbnail URLs, refactored UMA logging. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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', '*');
Jered 2014/01/10 15:12:36 If this is always going to happen, can you just us
Jered 2014/01/10 15:12:36 '*' makes me nervous. While I think this message i
beaudoin 2014/01/15 23:39:56 This event is fired after the onload, specifically
beaudoin 2014/01/15 23:39:56 The problem here is that I cannot know the URL of
Jered 2014/01/16 18:07:55 That makes sense. We should probably also be using
Jered 2014/01/16 18:07:55 I solved this problem once before for suggestion i
beaudoin 2014/01/17 03:51:46 Good point, I'll add that as a TODO on my side. :)
beaudoin 2014/01/17 03:51:46 This was significantly harder than expected, you'l
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
Mathieu 2014/01/09 18:37:18 nit: remove space here to be consistent
beaudoin 2014/01/15 23:39:56 Done.
39 // Creates and add an image.
Mathieu 2014/01/09 18:37:18 nit: adds
beaudoin 2014/01/15 23:39:56 Done.
40 function createThumbnail(src) {
38 var image = new Image(); 41 var image = new Image();
39 image.onload = function() { 42 image.onload = function() {
40 var shadow = document.createElement('span'); 43 var shadow = document.createElement('span');
41 shadow.classList.add('shadow'); 44 shadow.classList.add('shadow');
42 var link = createMostVisitedLink( 45 var link = createMostVisitedLink(
43 params, data.url, data.title, undefined, data.ping, data.provider); 46 params, data.url, data.title, undefined, data.ping,
47 data.provider);
44 link.appendChild(shadow); 48 link.appendChild(shadow);
45 link.appendChild(image); 49 link.appendChild(image);
46 // We add 'position: absolute' in anticipation that there could be more 50 displayLink(link);
47 // than one thumbnail. This will superpose the elements.
48 link.style.position = 'absolute';
49 document.body.appendChild(link);
50 }; 51 };
51 if (!isVisible) { 52 image.onerror = function() {
52 image.style.visibility = 'hidden'; 53 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR);
53 } 54 if (data.domain) {
54 return image; 55 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE_FALLBACK);
56 showDomainElement();
57 } else {
58 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_FALLBACK);
59 showEmptyTile();
60 }
61 };
62 image.src = src;
55 } 63 }
64
56 // Log an impression if we know the position of the tile. 65 // Log an impression if we know the position of the tile.
57 if (isFinite(params.pos) && data.provider) { 66 if (isFinite(params.pos) && data.provider) {
58 logImpression(parseInt(params.pos, 10), data.provider); 67 logImpression(parseInt(params.pos, 10), data.provider);
59 } 68 }
60 69
70 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE);
61 if (data.thumbnailUrl) { 71 if (data.thumbnailUrl) {
62 var image = createAndAppendThumbnail(true);
63 // If a backup thumbnail URL was provided, preload it in case the first
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); 72 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ATTEMPT);
73 createThumbnail(data.thumbnailUrl);
97 } else if (data.domain) { 74 } else if (data.domain) {
75 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE);
98 showDomainElement(); 76 showDomainElement();
99 } else { 77 } else {
78 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE);
100 showEmptyTile(); 79 showEmptyTile();
101 } 80 }
102 }); 81 });
103 }); 82 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698