- add sources.
[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 showDomainElement() {
18       logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR);
19       var link = createMostVisitedLink(
20           params, data.url, data.title, undefined, data.ping);
21       var domain = document.createElement('div');
22       domain.textContent = data.domain;
23       link.appendChild(domain);
24       document.body.appendChild(link);
25     }
26     function createAndAppendThumbnail(isVisible) {
27       var image = new Image();
28       image.onload = function() {
29         var shadow = document.createElement('span');
30         shadow.classList.add('shadow');
31         var link = createMostVisitedLink(
32             params, data.url, data.title, undefined, data.ping);
33         link.appendChild(shadow);
34         link.appendChild(image);
35         // We add 'position: absolute' in anticipation that there could be more
36         // than one thumbnail. This will superpose the elements.
37         link.style.position = 'absolute';
38         document.body.appendChild(link);
39       };
40       if (!isVisible) {
41         image.style.visibility = 'hidden';
42       }
43       return image;
44     }
45     if (data.thumbnailUrl) {
46       var image = createAndAppendThumbnail(true);
47       // If a backup thumbnail URL was provided, preload it in case the first
48       // thumbnail errors. The backup thumbnail is always preloaded so that the
49       // server can't gain knowledge on the local thumbnail DB by specifying a
50       // second URL that is only sometimes fetched.
51       if (data.thumbnailUrl2) {
52         var image2 = createAndAppendThumbnail(false);
53         var imageFailed = false;
54         var image2Failed = false;
55         image2.onerror = function() {
56           image2Failed = true;
57           image2.style.visibility = 'hidden';
58           if (imageFailed) {
59             showDomainElement();
60           }
61         };
62         image2.src = data.thumbnailUrl2;
63         // The first thumbnail's onerror function will swap the visibility of
64         // the two thumbnails.
65         image.onerror = function() {
66           logEvent(NTP_LOGGING_EVENT_TYPE.NTP_FALLBACK_THUMBNAIL_USED);
67           imageFailed = true;
68           image.style.visibility = 'hidden';
69           if (image2Failed) {
70             showDomainElement();
71           } else {
72             image2.style.visibility = 'visible';
73           }
74         };
75         logEvent(NTP_LOGGING_EVENT_TYPE.NTP_FALLBACK_THUMBNAIL_REQUESTED);
76       } else {
77         image.onerror = showDomainElement;
78       }
79       image.src = data.thumbnailUrl;
80       logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ATTEMPT);
81     } else {
82       showDomainElement();
83     }
84   });
85 });