most_visited_thumbnail.js revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/**
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * @fileoverview Rendering for iframed most visited thumbnails.
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) */
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)window.addEventListener('DOMContentLoaded', function() {
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  'use strict';
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  fillMostVisited(document.location, function(params, data) {
143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    function logEvent(eventName) {
153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      chrome.embeddedSearch.newTabPage.logEvent(eventName);
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    function logMostVisitedImpression(tileIndex, provider) {
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      chrome.embeddedSearch.newTabPage.logMostVisitedImpression(
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          tileIndex, provider);
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    function displayLink(link) {
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      document.body.appendChild(link);
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      window.parent.postMessage('linkDisplayed', '{{ORIGIN}}');
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    function showDomainElement() {
260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      var link = createMostVisitedLink(
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          params, data.url, data.title, undefined, data.direction,
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          data.provider);
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      var domain = document.createElement('div');
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      domain.textContent = data.domain;
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      link.appendChild(domain);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      displayLink(link);
330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // Called on intentionally empty tiles for which the visuals are handled
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // externally by the page itself.
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    function showEmptyTile() {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      displayLink(createMostVisitedLink(
386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          params, data.url, data.title, undefined, data.direction,
396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          data.provider));
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Creates and adds an image.
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    function createThumbnail(src) {
43010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      var image = document.createElement('img');
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      image.onload = function() {
450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        var link = createMostVisitedLink(
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            params, data.url, data.title, undefined, data.direction,
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            data.provider);
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        // Use blocker to prevent context menu from showing image-related items.
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        var blocker = document.createElement('span');
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        blocker.className = 'blocker';
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        link.appendChild(blocker);
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        link.appendChild(image);
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        displayLink(link);
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      };
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      image.onerror = function() {
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        // If no external thumbnail fallback (etfb), and have domain.
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        if (!params.etfb && data.domain) {
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          showDomainElement();
59010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE_FALLBACK);
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        } else {
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          showEmptyTile();
62010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE_FALLBACK);
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        }
64010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR);
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      };
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      image.src = src;
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (data.thumbnailUrl) {
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      createThumbnail(data.thumbnailUrl);
71010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_TILE);
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    } else if (data.domain) {
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      showDomainElement();
74010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE);
75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    } else {
76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      showEmptyTile();
77010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE);
78010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
79010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE);
80010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
81010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // Log an impression if we know the position of the tile.
82010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (isFinite(params.pos) && data.provider) {
83010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      logMostVisitedImpression(parseInt(params.pos, 10), data.provider);
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  });
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)});
87