Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / local_ntp / most_visited_thumbnail.js
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.
4
5
6 /**
7  * @fileoverview Rendering for iframed most visited thumbnails.
8  */
9
10 window.addEventListener('DOMContentLoaded', function() {
11   'use strict';
12
13   fillMostVisited(document.location, function(params, data) {
14     function logEvent(eventName) {
15       chrome.embeddedSearch.newTabPage.logEvent(eventName);
16     }
17     function logMostVisitedImpression(tileIndex, provider) {
18       chrome.embeddedSearch.newTabPage.logMostVisitedImpression(
19           tileIndex, provider);
20     }
21     function displayLink(link) {
22       document.body.appendChild(link);
23       window.parent.postMessage('linkDisplayed', '{{ORIGIN}}');
24     }
25     function showDomainElement() {
26       var link = createMostVisitedLink(
27           params, data.url, data.title, undefined, data.direction,
28           data.provider);
29       var domain = document.createElement('div');
30       domain.textContent = data.domain;
31       link.appendChild(domain);
32       displayLink(link);
33     }
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,
39           data.provider));
40     }
41     // Creates and adds an image.
42     function createThumbnail(src) {
43       var image = document.createElement('img');
44       image.onload = function() {
45         var link = createMostVisitedLink(
46             params, data.url, data.title, undefined, data.direction,
47             data.provider);
48         link.appendChild(image);
49         displayLink(link);
50       };
51       image.onerror = function() {
52         // If no external thumbnail fallback (etfb), and have domain.
53         if (!params.etfb && data.domain) {
54           showDomainElement();
55           logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE_FALLBACK);
56         } else {
57           showEmptyTile();
58           logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE_FALLBACK);
59         }
60         logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR);
61       };
62       image.src = src;
63     }
64
65     if (data.thumbnailUrl) {
66       createThumbnail(data.thumbnailUrl);
67       logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_TILE);
68     } else if (data.domain) {
69       showDomainElement();
70       logEvent(NTP_LOGGING_EVENT_TYPE.NTP_GRAY_TILE);
71     } else {
72       showEmptyTile();
73       logEvent(NTP_LOGGING_EVENT_TYPE.NTP_EXTERNAL_TILE);
74     }
75     logEvent(NTP_LOGGING_EVENT_TYPE.NTP_TILE);
76
77     // Log an impression if we know the position of the tile.
78     if (isFinite(params.pos) && data.provider) {
79       logMostVisitedImpression(parseInt(params.pos, 10), data.provider);
80     }
81   });
82 });