Chromium Code Reviews| 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 /** |
| 11 * The different types of events that are logged from the NTP. This enum is | 11 * The different types of events that are logged from the NTP. This enum is |
| 12 * used to transfer information from the NTP JavaScript to the renderer and is | 12 * used to transfer information from the NTP JavaScript to the renderer and is |
| 13 * not used as a UMA enum histogram's logged value. | 13 * not used as a UMA enum histogram's logged value. |
| 14 * Note: Keep in sync with common/ntp_logging_events.h | 14 * Note: Keep in sync with common/ntp_logging_events.h |
| 15 * @enum {number} | 15 * @enum {number} |
| 16 * @const | 16 * @const |
| 17 */ | 17 */ |
| 18 var LOG_TYPE = { | 18 var LOG_TYPE = { |
| 19 // The suggestion is coming from the server. Unused here. | 19 // The suggestion is coming from the server. |
| 20 NTP_SERVER_SIDE_SUGGESTION: 0, | 20 NTP_SERVER_SIDE_SUGGESTION: 0, |
| 21 // The suggestion is coming from the client. | 21 // The suggestion is coming from the client. |
| 22 NTP_CLIENT_SIDE_SUGGESTION: 1, | 22 NTP_CLIENT_SIDE_SUGGESTION: 1, |
| 23 // Indicates a tile was rendered, no matter if it's a thumbnail, a gray tile | 23 // Indicates a tile was rendered, no matter if it's a thumbnail, a gray tile |
| 24 // or an external tile. | 24 // or an external tile. |
| 25 NTP_TILE: 2, | 25 NTP_TILE: 2, |
| 26 // The tile uses a local thumbnail image. | 26 // The tile uses a local thumbnail image. |
| 27 NTP_THUMBNAIL_TILE: 3, | 27 NTP_THUMBNAIL_TILE: 3, |
| 28 // Used when no thumbnail is specified and a gray tile with the domain is used | 28 // Used when no thumbnail is specified and a gray tile with the domain is used |
| 29 // as the main tile. Unused here. | 29 // as the main tile. Unused here. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 40 // The visuals of that tile's fallback are handled externally. Unused here. | 40 // The visuals of that tile's fallback are handled externally. Unused here. |
| 41 NTP_EXTERNAL_TILE_FALLBACK: 8, | 41 NTP_EXTERNAL_TILE_FALLBACK: 8, |
| 42 // The user moused over an NTP tile. | 42 // The user moused over an NTP tile. |
| 43 NTP_MOUSEOVER: 9, | 43 NTP_MOUSEOVER: 9, |
| 44 // A NTP Tile has finished loading (successfully or failing). | 44 // A NTP Tile has finished loading (successfully or failing). |
| 45 NTP_TILE_LOADED: 10, | 45 NTP_TILE_LOADED: 10, |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 | 48 |
| 49 /** | 49 /** |
| 50 * The different sources that an NTP tile can have. | |
| 51 * Note: Keep in sync with common/ntp_logging_events.h | |
| 52 * @enum {number} | |
| 53 * @const | |
| 54 */ | |
| 55 var TILE_SOURCE = { | |
| 56 NTP_TILE_SOURCE_CLIENT: 0, | |
|
Bernhard Bauer
2016/07/05 14:31:54
Nit: you get the equivalent of namespacing here --
Marc Treib
2016/07/05 14:37:53
I'd like to keep the entries identical to the C++
| |
| 57 NTP_TILE_SOURCE_SERVER: 1, | |
| 58 }; | |
| 59 | |
| 60 | |
| 61 /** | |
| 50 * Total number of tiles to show at any time. If the host page doesn't send | 62 * Total number of tiles to show at any time. If the host page doesn't send |
| 51 * enough tiles, we fill them blank. | 63 * enough tiles, we fill them blank. |
| 52 * @const {number} | 64 * @const {number} |
| 53 */ | 65 */ |
| 54 var NUMBER_OF_TILES = 8; | 66 var NUMBER_OF_TILES = 8; |
| 55 | 67 |
| 56 | 68 |
| 57 /** | 69 /** |
| 58 * Whether to use icons instead of thumbnails. | 70 * Whether to use icons instead of thumbnails. |
| 59 * @type {boolean} | 71 * @type {boolean} |
| 60 */ | 72 */ |
| 61 var USE_ICONS = false; | 73 var USE_ICONS = false; |
| 62 | 74 |
| 63 | 75 |
| 64 /** | 76 /** |
| 65 * Number of lines to display in titles. | 77 * Number of lines to display in titles. |
| 66 * @type {number} | 78 * @type {number} |
| 67 */ | 79 */ |
| 68 var NUM_TITLE_LINES = 1; | 80 var NUM_TITLE_LINES = 1; |
| 69 | 81 |
| 70 /** | |
| 71 * Type of the impression provider for a generic client-provided suggestion. | |
| 72 * @type {string} | |
| 73 * @const | |
| 74 */ | |
| 75 var CLIENT_PROVIDER_NAME = 'client'; | |
| 76 | |
| 77 /** | |
| 78 * Type of the impression provider for a generic server-provided suggestion. | |
| 79 * @type {string} | |
| 80 * @const | |
| 81 */ | |
| 82 var SERVER_PROVIDER_NAME = 'server'; | |
| 83 | 82 |
| 84 /** | 83 /** |
| 85 * The origin of this request. | 84 * The origin of this request. |
| 86 * @const {string} | 85 * @const {string} |
| 87 */ | 86 */ |
| 88 var DOMAIN_ORIGIN = '{{ORIGIN}}'; | 87 var DOMAIN_ORIGIN = '{{ORIGIN}}'; |
| 89 | 88 |
| 90 | 89 |
| 91 /** | 90 /** |
| 92 * Counter for DOM elements that we are waiting to finish loading. | 91 * Counter for DOM elements that we are waiting to finish loading. |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 117 | 116 |
| 118 /** | 117 /** |
| 119 * Log an event on the NTP. | 118 * Log an event on the NTP. |
| 120 * @param {number} eventType Event from LOG_TYPE. | 119 * @param {number} eventType Event from LOG_TYPE. |
| 121 */ | 120 */ |
| 122 var logEvent = function(eventType) { | 121 var logEvent = function(eventType) { |
| 123 chrome.embeddedSearch.newTabPage.logEvent(eventType); | 122 chrome.embeddedSearch.newTabPage.logEvent(eventType); |
| 124 }; | 123 }; |
| 125 | 124 |
| 126 /** | 125 /** |
| 127 * Log impression of a most visited tile on the NTP. | 126 * Log impression of an NTP tile. |
| 128 * @param {number} tileIndex position of the tile, >= 0 and < NUMBER_OF_TILES | 127 * @param {number} tileIndex Position of the tile, >= 0 and < NUMBER_OF_TILES. |
| 129 * @param {string} provider specifies the UMA histogram to be reported | 128 * @param {number} tileSource The source from TILE_SOURCE. |
| 130 * (NewTabPage.SuggestionsImpression.{provider}) | |
| 131 */ | 129 */ |
| 132 function logMostVisitedImpression(tileIndex, provider) { | 130 function logMostVisitedImpression(tileIndex, tileSource) { |
| 133 chrome.embeddedSearch.newTabPage.logMostVisitedImpression(tileIndex, | 131 chrome.embeddedSearch.newTabPage.logMostVisitedImpression(tileIndex, |
| 134 provider); | 132 tileSource); |
| 135 } | 133 } |
| 136 | 134 |
| 137 /** | 135 /** |
| 138 * Log click on a most visited tile on the NTP. | 136 * Log click on an NTP tile. |
| 139 * @param {number} tileIndex position of the tile, >= 0 and < NUMBER_OF_TILES | 137 * @param {number} tileIndex Position of the tile, >= 0 and < NUMBER_OF_TILES. |
| 140 * @param {string} provider specifies the UMA histogram to be reported | 138 * @param {number} tileSource The source from TILE_SOURCE. |
| 141 * (NewTabPage.SuggestionsImpression.{provider}) | |
| 142 */ | 139 */ |
| 143 function logMostVisitedNavigation(tileIndex, provider) { | 140 function logMostVisitedNavigation(tileIndex, tileSource) { |
| 144 chrome.embeddedSearch.newTabPage.logMostVisitedNavigation(tileIndex, | 141 chrome.embeddedSearch.newTabPage.logMostVisitedNavigation(tileIndex, |
| 145 provider); | 142 tileSource); |
| 146 } | 143 } |
| 147 | 144 |
| 148 /** | 145 /** |
| 149 * Down counts the DOM elements that we are waiting for the page to load. | 146 * Down counts the DOM elements that we are waiting for the page to load. |
| 150 * When we get to 0, we send a message to the parent window. | 147 * When we get to 0, we send a message to the parent window. |
| 151 * This is usually used as an EventListener of onload/onerror. | 148 * This is usually used as an EventListener of onload/onerror. |
| 152 */ | 149 */ |
| 153 var countLoad = function() { | 150 var countLoad = function() { |
| 154 loadedCounter -= 1; | 151 loadedCounter -= 1; |
| 155 if (loadedCounter <= 0) { | 152 if (loadedCounter <= 0) { |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 * @param {object} args Data for the tile to be rendered. | 315 * @param {object} args Data for the tile to be rendered. |
| 319 */ | 316 */ |
| 320 var addTile = function(args) { | 317 var addTile = function(args) { |
| 321 if (isFinite(args.rid)) { | 318 if (isFinite(args.rid)) { |
| 322 // If a valid number passed in |args.rid|: a local chrome suggestion. | 319 // If a valid number passed in |args.rid|: a local chrome suggestion. |
| 323 var data = chrome.embeddedSearch.searchBox.getMostVisitedItemData(args.rid); | 320 var data = chrome.embeddedSearch.searchBox.getMostVisitedItemData(args.rid); |
| 324 if (!data) | 321 if (!data) |
| 325 return; | 322 return; |
| 326 | 323 |
| 327 data.tid = data.rid; | 324 data.tid = data.rid; |
| 328 data.provider = CLIENT_PROVIDER_NAME; | 325 data.tileSource = TILE_SOURCE.NTP_TILE_SOURCE_CLIENT; |
| 329 if (!data.faviconUrl) { | 326 if (!data.faviconUrl) { |
| 330 data.faviconUrl = 'chrome-search://favicon/size/16@' + | 327 data.faviconUrl = 'chrome-search://favicon/size/16@' + |
| 331 window.devicePixelRatio + 'x/' + data.renderViewId + '/' + data.tid; | 328 window.devicePixelRatio + 'x/' + data.renderViewId + '/' + data.tid; |
| 332 } | 329 } |
| 333 logEvent(LOG_TYPE.NTP_CLIENT_SIDE_SUGGESTION); | 330 logEvent(LOG_TYPE.NTP_CLIENT_SIDE_SUGGESTION); |
| 334 tiles.appendChild(renderTile(data)); | 331 tiles.appendChild(renderTile(data)); |
| 335 } else if (args.url) { | 332 } else if (args.url) { |
| 336 // If a URL is passed: a server-side suggestion. | 333 // If a URL is passed: a server-side suggestion. |
| 337 args.provider = args.provider || SERVER_PROVIDER_NAME; | 334 args.tileSource = TILE_SOURCE.NTP_TILE_SOURCE_SERVER; |
| 338 // check sanity of the arguments | 335 // check sanity of the arguments |
| 339 if (/^javascript:/i.test(args.url) || | 336 if (/^javascript:/i.test(args.url) || |
| 340 /^javascript:/i.test(args.thumbnailUrl) || | 337 /^javascript:/i.test(args.thumbnailUrl)) |
| 341 !/^[a-z0-9]{0,8}$/i.test(args.provider)) | |
| 342 return; | 338 return; |
| 343 logEvent(LOG_TYPE.NTP_SERVER_SIDE_SUGGESTION); | 339 logEvent(LOG_TYPE.NTP_SERVER_SIDE_SUGGESTION); |
| 344 tiles.appendChild(renderTile(args)); | 340 tiles.appendChild(renderTile(args)); |
| 345 } else { // an empty tile | 341 } else { // an empty tile |
| 346 tiles.appendChild(renderTile(null)); | 342 tiles.appendChild(renderTile(null)); |
| 347 } | 343 } |
| 348 }; | 344 }; |
| 349 | 345 |
| 350 /** | 346 /** |
| 351 * Called when the user decided to add a tile to the blacklist. | 347 * Called when the user decided to add a tile to the blacklist. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 var tile = document.createElement('a'); | 381 var tile = document.createElement('a'); |
| 386 | 382 |
| 387 if (data == null) { | 383 if (data == null) { |
| 388 tile.className = 'mv-empty-tile'; | 384 tile.className = 'mv-empty-tile'; |
| 389 return tile; | 385 return tile; |
| 390 } | 386 } |
| 391 | 387 |
| 392 logEvent(LOG_TYPE.NTP_TILE); | 388 logEvent(LOG_TYPE.NTP_TILE); |
| 393 // The tile will be appended to tiles. | 389 // The tile will be appended to tiles. |
| 394 var position = tiles.children.length; | 390 var position = tiles.children.length; |
| 395 if (data.provider) { | 391 logMostVisitedImpression(position, data.tileSource); |
| 396 logMostVisitedImpression(position, data.provider); | |
| 397 } | |
| 398 | 392 |
| 399 tile.className = 'mv-tile'; | 393 tile.className = 'mv-tile'; |
| 400 tile.setAttribute('data-tid', data.tid); | 394 tile.setAttribute('data-tid', data.tid); |
| 401 var html = []; | 395 var html = []; |
| 402 if (!USE_ICONS) { | 396 if (!USE_ICONS) { |
| 403 html.push('<div class="mv-favicon"></div>'); | 397 html.push('<div class="mv-favicon"></div>'); |
| 404 } | 398 } |
| 405 html.push('<div class="mv-title"></div><div class="mv-thumb"></div>'); | 399 html.push('<div class="mv-title"></div><div class="mv-thumb"></div>'); |
| 406 html.push('<div class="mv-x"></div>'); | 400 html.push('<div class="mv-x"></div>'); |
| 407 tile.innerHTML = html.join(''); | 401 tile.innerHTML = html.join(''); |
| 408 tile.lastElementChild.title = queryArgs['removeTooltip'] || ''; | 402 tile.lastElementChild.title = queryArgs['removeTooltip'] || ''; |
| 409 | 403 |
| 410 if (isSchemeAllowed(data.url)) { | 404 if (isSchemeAllowed(data.url)) { |
| 411 tile.href = data.url; | 405 tile.href = data.url; |
| 412 } | 406 } |
| 413 tile.title = data.title; | 407 tile.title = data.title; |
| 414 if (data.impressionUrl) { | 408 if (data.impressionUrl) { |
| 415 impressionUrl = data.impressionUrl; | 409 impressionUrl = data.impressionUrl; |
| 416 } | 410 } |
| 417 if (data.pingUrl) { | 411 if (data.pingUrl) { |
| 418 tile.addEventListener('click', function(ev) { | 412 tile.addEventListener('click', function(ev) { |
| 419 navigator.sendBeacon(data.pingUrl); | 413 navigator.sendBeacon(data.pingUrl); |
| 420 }); | 414 }); |
| 421 } | 415 } |
| 422 | 416 |
| 423 tile.addEventListener('click', function(ev) { | 417 tile.addEventListener('click', function(ev) { |
| 424 if (data.provider) { | 418 logMostVisitedNavigation(position, data.tileSource); |
| 425 logMostVisitedNavigation(position, data.provider); | |
| 426 } | |
| 427 }); | 419 }); |
| 428 | 420 |
| 429 tile.addEventListener('keydown', function(event) { | 421 tile.addEventListener('keydown', function(event) { |
| 430 if (event.keyCode == 46 /* DELETE */ || | 422 if (event.keyCode == 46 /* DELETE */ || |
| 431 event.keyCode == 8 /* BACKSPACE */) { | 423 event.keyCode == 8 /* BACKSPACE */) { |
| 432 event.preventDefault(); | 424 event.preventDefault(); |
| 433 event.stopPropagation(); | 425 event.stopPropagation(); |
| 434 blacklistTile(this); | 426 blacklistTile(this); |
| 435 } else if (event.keyCode == 13 /* ENTER */ || | 427 } else if (event.keyCode == 13 /* ENTER */ || |
| 436 event.keyCode == 32 /* SPACE */) { | 428 event.keyCode == 32 /* SPACE */) { |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 645 var html = document.querySelector('html'); | 637 var html = document.querySelector('html'); |
| 646 html.dir = 'rtl'; | 638 html.dir = 'rtl'; |
| 647 } | 639 } |
| 648 | 640 |
| 649 window.addEventListener('message', handlePostMessage); | 641 window.addEventListener('message', handlePostMessage); |
| 650 }; | 642 }; |
| 651 | 643 |
| 652 | 644 |
| 653 window.addEventListener('DOMContentLoaded', init); | 645 window.addEventListener('DOMContentLoaded', init); |
| 654 })(); | 646 })(); |
| OLD | NEW |