summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/local_ntp/most_visited_thumbnail.js
blob: 3bdfd612e368626717a864b35b6d3be6b4ec1354 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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.direction,
          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.direction,
          data.provider));
    }
    // Creates and adds an image.
    function createThumbnail(src, imageClass) {
      var image = document.createElement('img');
      if (imageClass) {
        image.classList.add(imageClass);
      }
      image.onload = function() {
        var link = createMostVisitedLink(
            params, data.url, data.title, undefined, data.direction,
            data.provider);
        // Use blocker to prevent context menu from showing image-related items.
        var blocker = document.createElement('span');
        blocker.className = 'blocker';
        link.appendChild(blocker);
        link.appendChild(image);
        displayLink(link);
        logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE_LOADED);
      };
      image.onerror = function() {
        // If no external thumbnail fallback (etfb), and have domain.
        if (!params.etfb && data.domain) {
          showDomainElement();
          logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE_FALLBACK);
        } else {
          showEmptyTile();
          logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE_FALLBACK);
        }
        logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR);
        logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE_LOADED);
      };
      image.src = src;
    }

    var useIcons = params['icons'] == '1';
    if (data.dummy) {
      showEmptyTile();
      logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE);
    } else if (useIcons && data.largeIconUrl) {
      createThumbnail(data.largeIconUrl, 'large-icon');
      // TODO(huangs): Log event for large icons.
    } else if (!useIcons && data.thumbnailUrl) {
      createThumbnail(data.thumbnailUrl, 'thumbnail');
      logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_TILE);
    } else if (data.domain) {
      showDomainElement();
      logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE);
    } else {
      showEmptyTile();
      logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE);
    }
    logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE);

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