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
3 // found in the LICENSE file.
7 * @fileoverview Rendering for iframed most visited thumbnails.
10 window.addEventListener('DOMContentLoaded', function() {
13 fillMostVisited(document.location, function(params, data) {
14 function logEvent(eventName) {
15 chrome.embeddedSearch.newTabPage.logEvent(eventName);
17 function logMostVisitedImpression(tileIndex, provider) {
18 chrome.embeddedSearch.newTabPage.logMostVisitedImpression(
21 function displayLink(link) {
22 document.body.appendChild(link);
23 window.parent.postMessage('linkDisplayed', '{{ORIGIN}}');
25 function showDomainElement() {
26 var link = createMostVisitedLink(
27 params, data.url, data.title, undefined, data.direction,
29 var domain = document.createElement('div');
30 domain.textContent = data.domain;
31 link.appendChild(domain);
34 // Called on intentionally empty tiles for which the visuals are handled
35 // externally by the page itself.
36 function showEmptyTile() {
37 displayLink(createMostVisitedLink(
38 params, data.url, data.title, undefined, data.direction,
41 // Creates and adds an image.
42 function createThumbnail(src, imageClass) {
43 var image = document.createElement('img');
45 image.classList.add(imageClass);
47 image.onload = function() {
48 var link = createMostVisitedLink(
49 params, data.url, data.title, undefined, data.direction,
51 // Use blocker to prevent context menu from showing image-related items.
52 var blocker = document.createElement('span');
53 blocker.className = 'blocker';
54 link.appendChild(blocker);
55 link.appendChild(image);
57 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE_LOADED);
59 image.onerror = function() {
60 // If no external thumbnail fallback (etfb), and have domain.
61 if (!params.etfb && data.domain) {
63 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE_FALLBACK);
66 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE_FALLBACK);
68 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR);
69 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE_LOADED);
74 var useIcons = params['icons'] == '1';
77 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE);
78 } else if (useIcons && data.largeIconUrl) {
79 createThumbnail(data.largeIconUrl, 'large-icon');
80 // TODO(huangs): Log event for large icons.
81 } else if (!useIcons && data.thumbnailUrl) {
82 createThumbnail(data.thumbnailUrl, 'thumbnail');
83 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_TILE);
84 } else if (data.domain) {
86 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE);
89 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE);
91 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE);
93 // Log an impression if we know the position of the tile.
94 if (isFinite(params.pos) && data.provider) {
95 logMostVisitedImpression(parseInt(params.pos, 10), data.provider);