| OLD | NEW |
| 1 /* Copyright 2015 The Chromium Authors. All rights reserved. | 1 /* Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. */ | 3 * found in the LICENSE file. */ |
| 4 | 4 |
| 5 // Single iframe for NTP tiles. | 5 // Single iframe for NTP tiles. |
| 6 (function() { | 6 (function() { |
| 7 'use strict'; | 7 'use strict'; |
| 8 | 8 |
| 9 | 9 |
| 10 /** | 10 /** |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 | 107 |
| 108 | 108 |
| 109 /** | 109 /** |
| 110 * Down counts the DOM elements that we are waiting for the page to load. | 110 * Down counts the DOM elements that we are waiting for the page to load. |
| 111 * When we get to 0, we send a message to the parent window. | 111 * When we get to 0, we send a message to the parent window. |
| 112 * This is usually used as an EventListener of onload/onerror. | 112 * This is usually used as an EventListener of onload/onerror. |
| 113 */ | 113 */ |
| 114 var countLoad = function() { | 114 var countLoad = function() { |
| 115 loadedCounter -= 1; | 115 loadedCounter -= 1; |
| 116 if (loadedCounter <= 0) { | 116 if (loadedCounter <= 0) { |
| 117 showTiles(); |
| 117 logEvent(LOG_TYPE.NTP_TILE_LOADED); | 118 logEvent(LOG_TYPE.NTP_TILE_LOADED); |
| 118 window.parent.postMessage({cmd: 'loaded'}, DOMAIN_ORIGIN); | 119 window.parent.postMessage({cmd: 'loaded'}, DOMAIN_ORIGIN); |
| 119 loadedCounter = 1; | 120 loadedCounter = 1; |
| 120 } | 121 } |
| 121 }; | 122 }; |
| 122 | 123 |
| 123 | 124 |
| 124 /** | 125 /** |
| 125 * Handles postMessages coming from the host page to the iframe. | 126 * Handles postMessages coming from the host page to the iframe. |
| 126 * Mostly, it dispatches every command to handleCommand. | 127 * Mostly, it dispatches every command to handleCommand. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 140 * Handles a single command coming from the host page to the iframe. | 141 * Handles a single command coming from the host page to the iframe. |
| 141 * We try to keep the logic here to a minimum and just dispatch to the relevant | 142 * We try to keep the logic here to a minimum and just dispatch to the relevant |
| 142 * functions. | 143 * functions. |
| 143 **/ | 144 **/ |
| 144 var handleCommand = function(data) { | 145 var handleCommand = function(data) { |
| 145 var cmd = data.cmd; | 146 var cmd = data.cmd; |
| 146 | 147 |
| 147 if (cmd == 'tile') { | 148 if (cmd == 'tile') { |
| 148 addTile(data); | 149 addTile(data); |
| 149 } else if (cmd == 'show') { | 150 } else if (cmd == 'show') { |
| 150 showTiles(); | 151 countLoad(); |
| 151 hideOverflowTiles(data); | 152 hideOverflowTiles(data); |
| 152 countLoad(); | |
| 153 } else if (cmd == 'updateTheme') { | 153 } else if (cmd == 'updateTheme') { |
| 154 updateTheme(data); | 154 updateTheme(data); |
| 155 } else if (cmd == 'tilesVisible') { | 155 } else if (cmd == 'tilesVisible') { |
| 156 hideOverflowTiles(data); | 156 hideOverflowTiles(data); |
| 157 } else { | 157 } else { |
| 158 console.error('Unknown command: ' + JSON.stringify(data)); | 158 console.error('Unknown command: ' + JSON.stringify(data)); |
| 159 } | 159 } |
| 160 }; | 160 }; |
| 161 | 161 |
| 162 | 162 |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 thumb.classList.add('failed-img'); | 374 thumb.classList.add('failed-img'); |
| 375 thumb.removeChild(img); | 375 thumb.removeChild(img); |
| 376 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); | 376 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); |
| 377 }); | 377 }); |
| 378 thumb.appendChild(img); | 378 thumb.appendChild(img); |
| 379 logEvent(LOG_TYPE.NTP_THUMBNAIL_TILE); | 379 logEvent(LOG_TYPE.NTP_THUMBNAIL_TILE); |
| 380 } else { | 380 } else { |
| 381 thumb.classList.add('failed-img'); | 381 thumb.classList.add('failed-img'); |
| 382 } | 382 } |
| 383 } else { // THUMBNAILS | 383 } else { // THUMBNAILS |
| 384 var thumb = tile.querySelector('.mv-thumb'); | |
| 385 var img = document.createElement('img'); | |
| 386 var loaded = false; | |
| 387 | |
| 388 // We keep track of the outcome of loading possible thumbnails for this | 384 // We keep track of the outcome of loading possible thumbnails for this |
| 389 // tile. Possible values: | 385 // tile. Possible values: |
| 390 // - null: waiting for load/error | 386 // - null: waiting for load/error |
| 391 // - false: error | 387 // - false: error |
| 392 // - a string: URL that loaded correctly. | 388 // - a string: URL that loaded correctly. |
| 393 // This is populated by acceptImage/rejectImage and loadBestImage | 389 // This is populated by acceptImage/rejectImage and loadBestImage |
| 394 // decides the best one to load. | 390 // decides the best one to load. |
| 395 var results = []; | 391 var results = []; |
| 392 var thumb = tile.querySelector('.mv-thumb'); |
| 393 var img = document.createElement('img'); |
| 394 var loaded = false; |
| 396 | 395 |
| 397 var loadBestImage = function() { | 396 var loadBestImage = function() { |
| 398 if (loaded) { | 397 if (loaded) { |
| 399 return; | 398 return; |
| 400 } | 399 } |
| 401 for (i = 0; i < results.length; ++i) { | 400 for (var i = 0; i < results.length; ++i) { |
| 402 if (results[i] === null) { | 401 if (results[i] === null) { |
| 403 return; | 402 return; |
| 404 } | 403 } |
| 405 if (results[i] != false) { | 404 if (results[i] != false) { |
| 406 img.src = results[i]; | 405 img.src = results[i]; |
| 407 loaded = true; | 406 loaded = true; |
| 408 return; | 407 return; |
| 409 } | 408 } |
| 410 } | 409 } |
| 411 thumb.classList.add('failed-img'); | 410 thumb.classList.add('failed-img'); |
| 412 thumb.removeChild(img); | 411 thumb.removeChild(img); |
| 413 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); | 412 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); |
| 413 countLoad(); |
| 414 }; | 414 }; |
| 415 | 415 |
| 416 var acceptImage = function(idx, url) { | 416 var acceptImage = function(idx, url) { |
| 417 return function(ev) { | 417 return function(ev) { |
| 418 results[idx] = url; | 418 results[idx] = url; |
| 419 loadBestImage(); | 419 loadBestImage(); |
| 420 }; | 420 }; |
| 421 }; | 421 }; |
| 422 | 422 |
| 423 var rejectImage = function(idx) { | 423 var rejectImage = function(idx) { |
| 424 return function(ev) { | 424 return function(ev) { |
| 425 results[idx] = false; | 425 results[idx] = false; |
| 426 loadBestImage(); | 426 loadBestImage(); |
| 427 }; | 427 }; |
| 428 }; | 428 }; |
| 429 | 429 |
| 430 img.title = data.title; |
| 431 img.classList.add('thumbnail'); |
| 432 loadedCounter += 1; |
| 433 img.addEventListener('load', countLoad); |
| 434 img.addEventListener('error', countLoad); |
| 435 img.addEventListener('error', function(ev) { |
| 436 thumb.classList.add('failed-img'); |
| 437 thumb.removeChild(img); |
| 438 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); |
| 439 }); |
| 440 thumb.appendChild(img); |
| 441 logEvent(LOG_TYPE.NTP_THUMBNAIL_TILE); |
| 442 |
| 430 // Get all thumbnailUrls for the tile. | 443 // Get all thumbnailUrls for the tile. |
| 431 // They are ordered from best one to be used to worst. | 444 // They are ordered from best one to be used to worst. |
| 432 for (var i = 0; i < data.thumbnailUrls.length; ++i) { | 445 for (var i = 0; i < data.thumbnailUrls.length; ++i) { |
| 433 results.push(null); | 446 results.push(null); |
| 434 } | 447 } |
| 435 for (var i = 0; i < data.thumbnailUrls.length; ++i) { | 448 for (var i = 0; i < data.thumbnailUrls.length; ++i) { |
| 436 if (data.thumbnailUrls[i]) { | 449 if (data.thumbnailUrls[i]) { |
| 437 var image = new Image(); | 450 var image = new Image(); |
| 438 image.src = data.thumbnailUrls[i]; | 451 image.src = data.thumbnailUrls[i]; |
| 439 image.onload = acceptImage(i, data.thumbnailUrls[i]); | 452 image.onload = acceptImage(i, data.thumbnailUrls[i]); |
| 440 image.onerror = rejectImage(i); | 453 image.onerror = rejectImage(i); |
| 441 } else { | 454 } else { |
| 442 rejectImage(i)(null); | 455 rejectImage(i)(null); |
| 443 } | 456 } |
| 444 } | 457 } |
| 445 | 458 |
| 446 img.title = data.title; | |
| 447 img.classList.add('thumbnail'); | |
| 448 loadedCounter += 1; | |
| 449 img.addEventListener('load', countLoad); | |
| 450 img.addEventListener('error', countLoad); | |
| 451 img.addEventListener('error', function(ev) { | |
| 452 thumb.classList.add('failed-img'); | |
| 453 thumb.removeChild(img); | |
| 454 logEvent(LOG_TYPE.NTP_THUMBNAIL_ERROR); | |
| 455 }); | |
| 456 thumb.appendChild(img); | |
| 457 logEvent(LOG_TYPE.NTP_THUMBNAIL_TILE); | |
| 458 | |
| 459 var favicon = tile.querySelector('.mv-favicon'); | 459 var favicon = tile.querySelector('.mv-favicon'); |
| 460 if (data.faviconUrl) { | 460 if (data.faviconUrl) { |
| 461 var fi = document.createElement('img'); | 461 var fi = document.createElement('img'); |
| 462 fi.src = data.faviconUrl; | 462 fi.src = data.faviconUrl; |
| 463 // Set the title to empty so screen readers won't say the image name. | 463 // Set the title to empty so screen readers won't say the image name. |
| 464 fi.title = ''; | 464 fi.title = ''; |
| 465 loadedCounter += 1; | 465 loadedCounter += 1; |
| 466 fi.addEventListener('load', countLoad); | 466 fi.addEventListener('load', countLoad); |
| 467 fi.addEventListener('error', countLoad); | 467 fi.addEventListener('error', countLoad); |
| 468 fi.addEventListener('error', function(ev) { | 468 fi.addEventListener('error', function(ev) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 var html = document.querySelector('html'); | 519 var html = document.querySelector('html'); |
| 520 html.dir = 'rtl'; | 520 html.dir = 'rtl'; |
| 521 } | 521 } |
| 522 | 522 |
| 523 window.addEventListener('message', handlePostMessage); | 523 window.addEventListener('message', handlePostMessage); |
| 524 }; | 524 }; |
| 525 | 525 |
| 526 | 526 |
| 527 window.addEventListener('DOMContentLoaded', init); | 527 window.addEventListener('DOMContentLoaded', init); |
| 528 })(); | 528 })(); |
| OLD | NEW |