summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/local_ntp/most_visited_thumbnail.js
blob: ff038a0fe48791039142f700f613070004b6ff08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


/**
 * @fileoverview Rendering for iframed most visited thumbnails.
 */

window.addEventListener('DOMContentLoaded', function() {
  'use strict';

  fillMostVisited(document.location, function(params, data) {
    function logEvent(eventName) {
      chrome.embeddedSearch.newTabPage.logEvent(eventName);
    }
    function logMostVisitedImpression(tileIndex, provider) {
      chrome.embeddedSearch.newTabPage.logMostVisitedImpression(
          tileIndex, provider);
    }
    function displayLink(link) {
      document.body.appendChild(link);
      window.parent.postMessage('linkDisplayed', '{{ORIGIN}}');
    }
    function showDomainElement() {
      var link = createMostVisitedLink(
          params, data.url, data.title, undefined, data.provider);
      var domain = document.createElement('div');
      domain.textContent = data.domain;
      link.appendChild(domain);
      displayLink(link);
    }
    // Called on intentionally empty tiles for which the visuals are handled
    // externally by the page itself.
    function showEmptyTile() {
      displayLink(createMostVisitedLink(
          params, data.url, data.title, undefined, data.provider));
    }
    // 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.provider);
        link.appendChild(shadow);
        link.appendChild(image);
        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();
        }
      };
      image.src = src;
    }

    // Log an impression if we know the position of the tile.
    if (isFinite(params.pos) && data.provider) {
      logMostVisitedImpression(parseInt(params.pos, 10), data.provider);
    }

    logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE);
    if (data.thumbnailUrl) {
      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();
    }
  });
});