summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/local_ntp/most_visited_single.js
blob: c497599fdb4b40c315dd594d1e4c3f7cf7bb0264 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
/* Copyright 2015 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. */

// Single iframe for NTP tiles.

/**
 * Controls rendering the Most Visited iframe.
 * @return {Object} A limited interface for testing the iframe.
 */
function MostVisited() {
'use strict';

/**
 * Enum for key codes.
 * @enum {number}
 * @const
 */
const KEYCODES = {
  BACKSPACE: 8,
  DELETE: 46,
  DOWN: 40,
  ENTER: 13,
  ESC: 27,
  LEFT: 37,
  RIGHT: 39,
  SPACE: 32,
  TAB: 9,
  UP: 38,
};

/**
 * Enum for ids.
 * @enum {string}
 * @const
 */
const IDS = {
  MOST_VISITED: 'most-visited',  // Container for all tilesets.
  MV_TILES: 'mv-tiles',          // Most Visited tiles container.
};

/**
 * Enum for classnames.
 * @enum {string}
 * @const
 */
const CLASSES = {
  FAILED_FAVICON: 'failed-favicon',  // Applied when the favicon fails to load.
  GRID_LAYOUT: 'grid-layout',
  // Applied to the grid tile being moved while reordering.
  GRID_REORDER: 'grid-reorder',
  GRID_TILE: 'grid-tile',
  GRID_TILE_CONTAINER: 'grid-tile-container',
  REORDER: 'reorder',  // Applied to the tile being moved while reordering.
  // Applied while we are reordering. Disables hover styling.
  REORDERING: 'reordering',
  MAC_CHROMEOS: 'mac-chromeos',  // Reduces font weight for MacOS and ChromeOS.
  // Material Design classes.
  MD_EMPTY_TILE: 'md-empty-tile',
  MD_FALLBACK_LETTER: 'md-fallback-letter',
  MD_ICON: 'md-icon',
  MD_ADD_ICON: 'md-add-icon',
  MD_MENU: 'md-menu',
  MD_EDIT_MENU: 'md-edit-menu',
  MD_TILE: 'md-tile',
  MD_TILE_INNER: 'md-tile-inner',
  MD_TITLE: 'md-title',
  NO_INITIAL_FADE: 'no-initial-fade',
};

/**
 * The different types of events that are logged from the NTP.  This enum is
 * used to transfer information from the NTP JavaScript to the renderer and is
 * not used as a UMA enum histogram's logged value.
 * Note: Keep in sync with common/ntp_logging_events.h
 * @enum {number}
 * @const
 */
const LOG_TYPE = {
  // All NTP tiles have finished loading (successfully or failing).
  NTP_ALL_TILES_LOADED: 11,
  // The 'Add shortcut' link was clicked.
  NTP_CUSTOMIZE_ADD_SHORTCUT_CLICKED: 44,
  // The 'Edit shortcut' link was clicked.
  NTP_CUSTOMIZE_EDIT_SHORTCUT_CLICKED: 45,
};

/**
 * The different (visual) types that an NTP tile can have.
 * Note: Keep in sync with components/ntp_tiles/tile_visual_type.h
 * @enum {number}
 * @const
 */
const TileVisualType = {
  NONE: 0,
  ICON_REAL: 1,
  ICON_COLOR: 2,
  ICON_DEFAULT: 3,
  THUMBNAIL: 7,
  THUMBNAIL_FAILED: 8,
};

/**
 * Timeout delay for the window.onresize event throttle. Set to 15 frame per
 * second.
 * @const {number}
 */
const RESIZE_TIMEOUT_DELAY = 66;

/**
 * Maximum number of tiles if custom links is enabled.
 * @const {number}
 */
const MD_MAX_NUM_CUSTOM_LINK_TILES = 10;

/**
 * Maximum number of tiles per row for Material Design.
 * @const {number}
 */
const MD_MAX_TILES_PER_ROW = 5;

/**
 * Height of a tile for Material Design. Keep in sync with
 * most_visited_single.css.
 * @const {number}
 */
const MD_TILE_HEIGHT = 128;

/**
 * Width of a tile for Material Design. Keep in sync with
 * most_visited_single.css.
 * @const {number}
 */
const MD_TILE_WIDTH = 112;

/**
 * Number of tiles that will always be visible for Material Design. Calculated
 * by dividing minimum |--content-width| (see local_ntp.css) by |MD_TILE_WIDTH|
 * and multiplying by 2 rows.
 * @const {number}
 */
const MD_NUM_TILES_ALWAYS_VISIBLE = 6;

/**
 * The origin of this request, i.e. 'https://www.google.TLD' for the remote NTP,
 * or 'chrome-search://local-ntp' for the local NTP.
 * @const {string}
 */
const DOMAIN_ORIGIN = '{{ORIGIN}}';

/**
 * Counter for DOM elements that we are waiting to finish loading. Starts out
 * at 1 because initially we're waiting for the "show" message from the parent.
 * @type {number}
 */
let loadedCounter = 1;

/**
 * DOM element containing the tiles we are going to present next.
 * Works as a double-buffer that is shown when we receive a "show" postMessage.
 * @type {Element}
 */
let tiles = null;

/**
 * Maximum number of MostVisited tiles to show at any time. If the host page
 * doesn't send enough tiles and custom links is not enabled, we fill them blank
 * tiles. This can be changed depending on what feature is enabled. Set by the
 * host page, while 8 is default.
 * @type {number}
 */
let maxNumTiles = 8;

/**
 * List of parameters passed by query args.
 * @type {Object}
 */
let queryArgs = {};

/**
 * True if we are currently reordering the tiles.
 * @type {boolean}
 */
let reordering = false;

/**
 * The tile that is being moved during the reorder flow. Null if we are
 * currently not reordering.
 * @type {?Element}
 */
let elementToReorder = null;

/**
 * True if the custom links feature is enabled, i.e. when this is a Google NTP.
 * Set when the iframe is initialized.
 * @type {boolean}
 */
let customLinksFeatureEnabled = false;

/**
 * True if the grid layout is enabled.
 * @type {boolean}
 */
let isGridEnabled = false;

/**
 * The current grid of tiles.
 * @type {?Grid}
 */
let currGrid = null;

/**
 * Called by tests to enable the grid layout.
 */
function enableGridLayoutForTesting() {
  isGridEnabled = true;
  document.body.classList.add(CLASSES.GRID_LAYOUT);
}

/**
 * Additional API for Array. Moves the item at index |from| to index |to|.
 * @param {number} from Index of the item to move.
 * @param {number} to Index to move the item to.
 */
Array.prototype.move = function(from, to) {
  this.splice(to, 0, this.splice(from, 1)[0]);
};

/**
 * Class that handles layouts and animations for the tile grid. This includes
 * animations for adding, deleting, and reordering.
 */
class Grid {
  constructor() {
    /** @private {number} */
    this.tileHeight_ = 0;
    /** @private {number} */
    this.tileWidth_ = 0;
    /** @private {number} */
    this.tilesAlwaysVisible_ = 0;
    /**
     * The maximum number of tiles per row allowed by the grid parameters.
     * @private {number}
     */
    this.maxTilesPerRow_ = 0;
    /** @private {number} */
    this.maxTiles_ = 0;

    /** @private {number} */
    this.gridWidth_ = 0;
    /**
     * The maximum number of tiles per row allowed by the window width.
     * @private {number}
     */
    this.maxTilesPerRowWindow_ = 0;

    /** @private {?Element} */
    this.container_ = null;
    /** @private {?HTMLCollection} */
    this.tiles_ = null;

    /**
     * Array that stores the {x,y} positions of the tile layout.
     * @private {?Array<!Object>}
     */
    this.position_ = null;

    /**
     * Stores the current order of the tiles. Index corresponds to grid
     * position, while value is the index of the tile in |this.tiles_|.
     * @private {?Array<number>}
     */
    this.order_ = null;

    /** @private {number} The index of the tile we're reordering. */
    this.itemToReorder_ = -1;
    /** @private {number} The index to move the tile we're reordering to. */
    this.newIndexOfItemToReorder_ = -1;

    /** @private {boolean} True if the user is currently touching a tile. */
    this.touchStarted_ = false;
  }

  /**
   * Sets up the grid for the new tileset in |container|. The old tileset is
   * discarded.
   * @param {!Element} container The grid container element.
   * @param {Object=} params Customizable parameters for the grid. Used in
   *     testing.
   */
  init(container, params = {}) {
    this.container_ = container;

    this.tileHeight_ = params.tileHeight || MD_TILE_HEIGHT;
    this.tileWidth_ = params.tileWidth || MD_TILE_WIDTH;
    this.tilesAlwaysVisible_ =
        params.tilesAlwaysVisible || MD_NUM_TILES_ALWAYS_VISIBLE;
    this.maxTilesPerRow_ = params.maxTilesPerRow || MD_MAX_TILES_PER_ROW;
    this.maxTiles_ = params.maxTiles || maxNumTiles;

    this.maxTilesPerRowWindow_ = this.getMaxTilesPerRow_();

    this.tiles_ =
        this.container_.getElementsByClassName(CLASSES.GRID_TILE_CONTAINER);
    if (this.tiles_.length > this.maxTiles_) {
      throw new Error(
          'The number of tiles (' + this.tiles_.length +
          ') exceeds the maximum (' + this.maxTiles_ + ').');
    }
    this.position_ = new Array(this.maxTiles_);
    this.order_ = new Array(this.maxTiles_);
    for (let i = 0; i < this.maxTiles_; i++) {
      this.position_[i] = {x: 0, y: 0};
      this.order_[i] = i;
    }

    if (isCustomLinksEnabled() || params.enableReorder) {
      // Set up reordering for all tiles except the add shortcut button.
      for (let i = 0; i < this.tiles_.length; i++) {
        if (this.tiles_[i].getAttribute('add') !== 'true') {
          this.setupReorder_(this.tiles_[i], i);
        }
      }
    }

    this.updateLayout();
  }

  /**
   * Returns a grid tile wrapper that contains |tile|.
   * @param {!Element} tile The tile element.
   * @param {number} rid The tile's restricted id.
   * @param {boolean} isAddButton True if this is the add shortcut button.
   * @return {!Element} A grid tile wrapper.
   */
  createGridTile(tile, rid, isAddButton) {
    const gridTileContainer = document.createElement('div');
    gridTileContainer.className = CLASSES.GRID_TILE_CONTAINER;
    gridTileContainer.setAttribute('rid', rid);
    gridTileContainer.setAttribute('add', isAddButton);
    const gridTile = document.createElement('div');
    gridTile.className = CLASSES.GRID_TILE;
    gridTile.appendChild(tile);
    gridTileContainer.appendChild(gridTile);
    return gridTileContainer;
  }

  /**
   * Updates the layout of the tiles. This is called for new tilesets and when
   * the window is resized or zoomed. Translates each tile's
   * |CLASSES.GRID_TILE_CONTAINER| to the correct position.
   */
  updateLayout() {
    const tilesPerRow = this.getTilesPerRow_();

    this.gridWidth_ = tilesPerRow * this.tileWidth_;
    this.container_.style.width = this.gridWidth_ + 'px';

    const maxVisibleTiles = tilesPerRow * 2;
    let x = 0;
    let y = 0;
    for (let i = 0; i < this.tiles_.length; i++) {
      const tile = this.tiles_[i];
      // Reset the offset for row 2.
      if (i === tilesPerRow) {
        x = this.getRow2Offset_(tilesPerRow);
        y = this.tileHeight_;
      }
      // Update the tile's position.
      this.translate_(tile, x, y);
      this.position_[i].x = x;
      this.position_[i].y = y;
      x += this.tileWidth_;  // Increment for the next tile.

      // Update visibility for tiles that may be hidden by the iframe border in
      // order to prevent keyboard navigation from reaching them. Ignores tiles
      // that will always be visible, since changing 'display' prevents
      // transitions from working.
      if (i >= this.tilesAlwaysVisible_) {
        const isVisible = i < maxVisibleTiles;
        tile.style.display = isVisible ? 'block' : 'none';
      }
    }
  }

  /**
   * Called when the window is resized/zoomed. Recalculates maximums for the new
   * window size and calls |updateLayout| if necessary.
   */
  onResize() {
    // Update the layout if the max number of tiles per row changes due to the
    // new window size.
    const maxPerRowWindow = this.getMaxTilesPerRow_();
    if (maxPerRowWindow !== this.maxTilesPerRowWindow_) {
      this.maxTilesPerRowWindow_ = maxPerRowWindow;
      this.updateLayout();
    }
  }

  /**
   * Returns the number of tiles per row. This may be balanced in order to make
   * even rows.
   * @return {number} The number of tiles per row.
   * @private
   */
  getTilesPerRow_() {
    const maxTilesPerRow =
        Math.min(this.maxTilesPerRow_, this.maxTilesPerRowWindow_);
    if (this.tiles_.length >= maxTilesPerRow * 2) {
      // We have enough for two full rows, so just return the max.
      return maxTilesPerRow;
    } else if (this.tiles_.length > maxTilesPerRow) {
      // We have have a little more than one full row, so we need to rebalance.
      return Math.ceil(this.tiles_.length / 2);
    } else {
      // We have (less than) a full row, so just return the tiles we have.
      return this.tiles_.length;
    }
  }

  /**
   * Returns the maximum number of tiles per row allowed by the window size.
   * @return {number} The maximum number of tiles per row.
   * @private
   */
  getMaxTilesPerRow_() {
    return Math.floor(window.innerWidth / this.tileWidth_);
  }

  /**
   * Returns row 2's x offset from row 1 in px. This will either be 0 or half a
   * tile length.
   * @param {number} tilesPerRow The number of tiles per row.
   * @return {number} The offset for row 2.
   * @private
   */
  getRow2Offset_(tilesPerRow) {
    // An odd number of tiles requires a half tile offset in the second row,
    // unless both rows are full (i.e. for smaller window widths).
    if (this.tiles_.length % 2 === 1 && this.tiles_.length / tilesPerRow < 2) {
      return Math.round(this.tileWidth_ / 2);
    }
    return 0;
  }

  /**
   * Returns true if the browser is in RTL.
   * @return {boolean}
   * @private
   */
  isRtl_() {
    return document.documentElement.dir === 'rtl';
  }

  /**
   * Translates the |element| by (x, y).
   * @param {?Element} element The element to apply the transform to.
   * @param {number} x The x value.
   * @param {number} y The y value.
   * @private
   */
  translate_(element, x, y) {
    if (!element) {
      throw new Error('Invalid element: cannot apply transform');
    }
    const rtlX = x * (this.isRtl_() ? -1 : 1);
    element.style.transform = 'translate(' + rtlX + 'px, ' + y + 'px)';
  }

  /**
   * Sets up event listeners necessary for tile reordering.
   * @param {!Element} tile Tile on which to set the event listeners.
   * @param {number} index The tile's index.
   * @private
   */
  setupReorder_(tile, index) {
    tile.setAttribute('index', index);

    // Set up mouse support.
    // Listen for the drag event on the tile instead of the tile container. The
    // tile container remains static during the reorder flow.
    tile.firstChild.draggable = true;
    // Prevent default drag events on the shortcut link.
    const tileItem = tile.firstChild.firstChild;
    tileItem.draggable = false;
    tile.firstChild.addEventListener('dragstart', (event) => {
      // Support link dragging (i.e. dragging the URL to the omnibox).
      event.dataTransfer.setData('text/uri-list', tileItem.href);
      // Remove the ghost image that appears when dragging.
      const emptyImg = new Image();
      event.dataTransfer.setDragImage(emptyImg, 0, 0);

      this.startReorder_(tile, event, /*mouseMode=*/ true);
    });
    // Show a 'move' cursor while dragging the tile within the grid bounds. This
    // is mostly intended for Windows, which will otherwise show a 'prohibited'
    // cursor.
    tile.addEventListener('dragover', (event) => {
      event.preventDefault();
      event.dataTransfer.dropEffect = 'move';
    });

    // Set up touch support.
    tile.firstChild.addEventListener('touchstart', (startEvent) => {
      // Ignore subsequent touchstart events, which can be triggered if a
      // different finger is placed on this tile.
      if (this.touchStarted_) {
        return;
      }
      this.touchStarted_ = true;

      // Start the reorder flow once the user moves their finger.
      const startReorder = (moveEvent) => {
        // Use the cursor position from 'touchstart' as the starting location.
        this.startReorder_(tile, startEvent, /*mouseMode=*/ false);
      };
      // Insert the held tile at the index we are hovering over.
      const moveOver = (moveEvent) => {
        // Touch events do not have a 'mouseover' equivalent, so we need to
        // manually check if we are hovering over a tile. If so, insert the held
        // tile there.
        // Note: The first item in |changedTouches| is the current position.
        const x = moveEvent.changedTouches[0].pageX;
        const y = moveEvent.changedTouches[0].pageY;
        this.reorderToIndexAtPoint_(x, y);
      };
      // Allow 'touchstart' events again when reordering stops/was never
      // started.
      const touchEnd = (endEvent) => {
        tile.firstChild.removeEventListener('touchmove', startReorder);
        tile.firstChild.removeEventListener('touchmove', moveOver);
        tile.firstChild.removeEventListener('touchend', touchEnd);
        tile.firstChild.removeEventListener('touchcancel', touchEnd);
        this.touchStarted_ = false;
      };

      tile.firstChild.addEventListener('touchmove', startReorder, {once: true});
      tile.firstChild.addEventListener('touchmove', moveOver);
      tile.firstChild.addEventListener('touchend', touchEnd, {once: true});
      tile.firstChild.addEventListener('touchcancel', touchEnd, {once: true});
    });
  }

  /**
   * Starts the reorder flow. Updates the visual style of the held tile to
   * indicate that it is being moved and sets up the relevant event listeners.
   * @param {!Element} tile Tile that is being moved.
   * @param {!Event} event The 'dragstart'/'touchmove' event. Used to obtain the
   *     current cursor position
   * @param {boolean} mouseMode True if the user is using a mouse.
   * @private
   */
  startReorder_(tile, event, mouseMode) {
    const index = Number(tile.getAttribute('index'));

    this.itemToReorder_ = index;
    this.newIndexOfItemToReorder_ = index;

    // Apply reorder styling.
    tile.classList.add(CLASSES.GRID_REORDER);
    // Disable other hover/active styling for all tiles.
    document.body.classList.add(CLASSES.REORDERING);

    // Set up event listeners for the reorder flow. Listen for drag events if
    // |mouseMode|, touch events otherwise.
    if (mouseMode) {
      const trackCursor =
          this.trackCursor_(tile, event.pageX, event.pageY, true);
      // The 'dragover' event must be tracked at the document level, since the
      // currently dragged tile will interfere with 'dragover' events on the
      // other tiles.
      const dragOver = (dragOverEvent) => {
        trackCursor(dragOverEvent);
        // Since the 'dragover' event is not tied to a specific tile, we need to
        // manually check if we are hovering over a tile. If so, insert the held
        // tile there.
        this.reorderToIndexAtPoint_(dragOverEvent.pageX, dragOverEvent.pageY);
      };
      document.addEventListener('dragover', dragOver);
      document.addEventListener('dragend', () => {
        document.removeEventListener('dragover', dragOver);
        this.stopReorder_(tile);
      }, {once: true});
    } else {
      // Track the cursor on subsequent 'touchmove' events (the first
      // 'touchmove' event that starts the reorder flow is ignored).
      const trackCursor = this.trackCursor_(
          tile, event.changedTouches[0].pageX, event.changedTouches[0].pageY,
          false);
      const touchEnd = (touchEndEvent) => {
        tile.firstChild.removeEventListener('touchmove', trackCursor);
        tile.firstChild.removeEventListener('touchend', touchEnd);
        tile.firstChild.removeEventListener('touchcancel', touchEnd);
        this.stopReorder_(tile);  // Stop the reorder flow.
      };
      tile.firstChild.addEventListener('touchmove', trackCursor);
      tile.firstChild.addEventListener('touchend', touchEnd, {once: true});
      tile.firstChild.addEventListener('touchcancel', touchEnd, {once: true});
    }
  }

  /**
   * Stops the reorder flow. Resets the held tile's visual style and tells the
   * EmbeddedSearchAPI that a tile has been moved.
   * @param {!Element} tile Tile that has been moved.
   * @private
   */
  stopReorder_(tile) {
    const index = Number(tile.getAttribute('index'));

    // Remove reorder styling.
    tile.classList.remove(CLASSES.GRID_REORDER);
    document.body.classList.remove(CLASSES.REORDERING);

    // Move the tile to its new position and notify EmbeddedSearchAPI that the
    // tile has been moved.
    this.applyReorder_(tile, this.newIndexOfItemToReorder_);
    chrome.embeddedSearch.newTabPage.reorderCustomLink(
        Number(this.tiles_[index].getAttribute('rid')),
        this.newIndexOfItemToReorder_);

    this.itemToReorder_ = -1;
    this.newIndexOfItemToReorder_ = -1;
  }

  /**
   * Attempts to insert the currently held tile at the index located at (x, y).
   * Does nothing if there is no tile at (x, y) or the reorder flow is not
   * ongoing.
   * @param {number} x The x coordinate.
   * @param {number} y The y coordinate.
   * @private
   */
  reorderToIndexAtPoint_(x, y) {
    const elements = document.elementsFromPoint(x, y);
    for (let i = 0; i < elements.length; i++) {
      if (elements[i].classList.contains('grid-tile-container') &&
          elements[i].getAttribute('index') !== null) {
        this.reorderToIndex_(Number(elements[i].getAttribute('index')));
        return;
      }
    }
  }

  /**
   * Executed only when the reorder flow is ongoing. Inserts the currently held
   * tile at |index| and shifts tiles accordingly.
   * @param {number} index The index to insert the held tile at.
   * @private
   */
  reorderToIndex_(index) {
    if (this.newIndexOfItemToReorder_ === index ||
        !document.body.classList.contains(CLASSES.REORDERING)) {
      return;
    }

    // Moves the held tile from its current position to |index|.
    this.order_.move(this.newIndexOfItemToReorder_, index);
    this.newIndexOfItemToReorder_ = index;
    // Shift tiles according to the new order.
    for (let i = 0; i < this.tiles_.length; i++) {
      const tileIndex = this.order_[i];
      // Don't move the tile we're holding nor the add shortcut button.
      if (tileIndex === this.itemToReorder_ ||
          this.tiles_[i].getAttribute('add') === 'true') {
        continue;
      }
      this.applyReorder_(this.tiles_[tileIndex], i);
    }
  }

  /**
   * Translates the |tile|'s |CLASSES.GRID_TILE| from |index| to |newIndex|.
   * This is done to prevent interference with event listeners on the |tile|'s
   * |CLASSES.GRID_TILE_CONTAINER|, particularly 'mouseover'.
   * @param {!Element} tile Tile that is being shifted.
   * @param {number} newIndex New index for the tile.
   * @private
   */
  applyReorder_(tile, newIndex) {
    if (tile.getAttribute('index') === null) {
      throw new Error('Tile does not have an index.');
    }
    const index = Number(tile.getAttribute('index'));
    const x = this.position_[newIndex].x - this.position_[index].x;
    const y = this.position_[newIndex].y - this.position_[index].y;
    this.translate_(tile.children[0], x, y);
  }

  /**
   * Moves |tile| so that it tracks the cursor's position. This is done by
   * translating the |tile|'s |CLASSES.GRID_TILE|, which prevents interference
   * with event listeners on the |tile|'s |CLASSES.GRID_TILE_CONTAINER|.
   * @param {!Element} tile Tile that is being moved.
   * @param {number} origCursorX Original x cursor position.
   * @param {number} origCursorY Original y cursor position.
   * @param {boolean} mouseMode True if the user is using a mouse.
   * @private
   */
  trackCursor_(tile, origCursorX, origCursorY, mouseMode) {
    const index = Number(tile.getAttribute('index'));
    // RTL positions align with the right side of the grid. Therefore, the x
    // value must be recalculated to align with the left.
    const origPosX = this.isRtl_() ?
        (this.gridWidth_ - (this.position_[index].x + this.tileWidth_)) :
        this.position_[index].x;
    const origPosY = this.position_[index].y;

    // Get the max translation allowed by the grid boundaries. This will be the
    // x of the last tile in a row and the y of the tiles in the second row.
    const maxTranslateX = this.gridWidth_ - this.tileWidth_;
    const maxTranslateY = this.tileHeight_;

    const maxX = maxTranslateX - origPosX;
    const maxY = maxTranslateY - origPosY;
    const minX = 0 - origPosX;
    const minY = 0 - origPosY;

    return (event) => {
      const currX = mouseMode ? event.pageX : event.changedTouches[0].pageX;
      const currY = mouseMode ? event.pageY : event.changedTouches[0].pageY;
      // Do not exceed the iframe borders.
      const x = Math.max(Math.min(currX - origCursorX, maxX), minX);
      const y = Math.max(Math.min(currY - origCursorY, maxY), minY);
      tile.firstChild.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
    };
  }
}

/**
 * Log an event on the NTP.
 * @param {number} eventType Event from LOG_TYPE.
 */
function logEvent(eventType) {
  chrome.embeddedSearch.newTabPage.logEvent(eventType);
}

/**
 * Log impression of an NTP tile.
 * @param {number} tileIndex Position of the tile, >= 0 and < |maxNumTiles|.
 * @param {number} tileTitleSource The source of the tile's title as received
 *     from getMostVisitedItemData.
 * @param {number} tileSource The tile's source as received from
 *     getMostVisitedItemData.
 * @param {number} tileType The tile's visual type from TileVisualType.
 * @param {Date} dataGenerationTime Timestamp representing when the tile was
 *     produced by a ranking algorithm.
 */
function logMostVisitedImpression(
    tileIndex, tileTitleSource, tileSource, tileType, dataGenerationTime) {
  chrome.embeddedSearch.newTabPage.logMostVisitedImpression(
      tileIndex, tileTitleSource, tileSource, tileType, dataGenerationTime);
}

/**
 * Log click on an NTP tile.
 * @param {number} tileIndex Position of the tile, >= 0 and < |maxNumTiles|.
 * @param {number} tileTitleSource The source of the tile's title as received
 *     from getMostVisitedItemData.
 * @param {number} tileSource The tile's source as received from
 *     getMostVisitedItemData.
 * @param {number} tileType The tile's visual type from TileVisualType.
 * @param {Date} dataGenerationTime Timestamp representing when the tile was
 *     produced by a ranking algorithm.
 */
function logMostVisitedNavigation(
    tileIndex, tileTitleSource, tileSource, tileType, dataGenerationTime) {
  chrome.embeddedSearch.newTabPage.logMostVisitedNavigation(
      tileIndex, tileTitleSource, tileSource, tileType, dataGenerationTime);
}

/**
 * Returns true if custom links are enabled.
 */
function isCustomLinksEnabled() {
  return customLinksFeatureEnabled &&
      !chrome.embeddedSearch.newTabPage.isUsingMostVisited;
}

/**
 * Down counts the DOM elements that we are waiting for the page to load.
 * When we get to 0, we send a message to the parent window.
 * This is usually used as an EventListener of onload/onerror.
 */
function countLoad() {
  loadedCounter -= 1;
  if (loadedCounter <= 0) {
    swapInNewTiles();
    logEvent(LOG_TYPE.NTP_ALL_TILES_LOADED);
    let tilesAreCustomLinks = isCustomLinksEnabled() &&
        chrome.embeddedSearch.newTabPage.isCustomLinks;
    // Tell the parent page whether to show the restore default shortcuts option
    // in the menu.
    window.parent.postMessage(
        {cmd: 'loaded', showRestoreDefault: tilesAreCustomLinks},
        DOMAIN_ORIGIN);
    tilesAreCustomLinks = false;
    // Reset to 1, so that any further 'show' message will cause us to swap in
    // fresh tiles.
    loadedCounter = 1;
  }
}

/**
 * Handles postMessages coming from the host page to the iframe.
 * Mostly, it dispatches every command to handleCommand.
 */
function handlePostMessage(event) {
  if (event.data instanceof Array) {
    for (let i = 0; i < event.data.length; ++i) {
      handleCommand(event.data[i]);
    }
  } else {
    handleCommand(event.data);
  }
}

/**
 * Handles a single command coming from the host page to the iframe.
 * We try to keep the logic here to a minimum and just dispatch to the relevant
 * functions.
 */
function handleCommand(data) {
  const cmd = data.cmd;

  if (cmd == 'tile') {
    addTile(data);
  } else if (cmd == 'show') {
    // TODO(crbug.com/946225): If this happens before we have finished loading
    // the previous tiles, we probably get into a bad state. If/when the iframe
    // is removed this might no longer be a concern.
    showTiles();
  } else if (cmd == 'updateTheme') {
    updateTheme(data);
  } else if (cmd === 'focusMenu') {
    focusTileMenu(data);
  } else {
    console.error('Unknown command: ' + JSON.stringify(data));
  }
}

/**
 * Handler for the 'show' message from the host page.
 */
function showTiles() {
  utils.setPlatformClass(document.body);
  countLoad();
}

/**
 * Handler for the 'updateTheme' message from the host page.
 * @param {!Object} info Data received in the message.
 */
function updateTheme(info) {
  document.body.style.setProperty('--tile-title-color', info.tileTitleColor);
  document.body.style.setProperty(
      '--icon-background-color', info.iconBackgroundColor);
  document.body.classList.toggle('dark-theme', info.isThemeDark);
  document.body.classList.toggle('use-title-container', info.useTitleContainer);
  document.body.classList.toggle('custom-background', info.customBackground);
  document.body.classList.toggle('use-white-add-icon', info.useWhiteAddIcon);

  // Reduce font weight on the default(white) background for Mac and CrOS.
  document.body.classList.toggle(
      CLASSES.MAC_CHROMEOS,
      !info.isThemeDark && !info.useTitleContainer &&
          (navigator.userAgent.indexOf('Mac') > -1 ||
           navigator.userAgent.indexOf('CrOS') > -1));
}

/**
 * Handler for 'focusMenu' message from the host page. Focuses the edited tile's
 * menu or the add shortcut tile after closing the custom link edit dialog
 * without saving.
 * @param {!Object} info Data received in the message.
 */
function focusTileMenu(info) {
  const tile = document.querySelector(`a.md-tile[data-rid="${info.rid}"]`);
  if (info.rid === -1 /* Add shortcut tile */) {
    tile.focus();
  } else {
    tile.parentNode.childNodes[1].focus();
  }
}

/**
 * Removes all old instances of |IDS.MV_TILES| that are pending for deletion.
 */
function removeAllOldTiles() {
  const parent = document.querySelector('#' + IDS.MOST_VISITED);
  const oldList = parent.querySelectorAll('.mv-tiles-old');
  for (let i = 0; i < oldList.length; ++i) {
    parent.removeChild(oldList[i]);
  }
}

/**
 * Called when all tiles have finished loading (successfully or not), including
 * their thumbnail images, and we are ready to show the new tiles and drop the
 * old ones.
 */
function swapInNewTiles() {
  // Store the tiles on the current closure.
  const cur = tiles;

  // Add an "add new custom link" button if we haven't reached the maximum
  // number of tiles.
  if (isCustomLinksEnabled() && cur.childNodes.length < maxNumTiles) {
    const data = {
      'rid': -1,
      'title': queryArgs['addLink'],
      'url': '',
      'isAddButton': true,
      'dataGenerationTime': new Date(),
      'tileSource': -1,
      'tileTitleSource': -1
    };
    tiles.appendChild(renderMaterialDesignTile(data));
  }

  const parent = document.querySelector('#' + IDS.MOST_VISITED);

  const old = parent.querySelector('#' + IDS.MV_TILES);
  if (old) {
    // Mark old tile DIV for removal after the transition animation is done.
    old.removeAttribute('id');
    old.classList.add('mv-tiles-old');
    old.style.opacity = 0.0;
    cur.addEventListener('transitionend', function(ev) {
      if (ev.target === cur) {
        removeAllOldTiles();
      }
    });
  }

  // Add new tileset.
  cur.id = IDS.MV_TILES;
  parent.appendChild(cur);

  if (isGridEnabled) {
    // Initialize the new tileset before modifying opacity. This will prevent
    // the transform transition from applying after the tiles fade in.
    currGrid.init(cur);
  } else {
    // Re-balance the tiles if there are more than |MD_MAX_TILES_PER_ROW| in
    // order to make even rows.
    if (cur.childNodes.length > MD_MAX_TILES_PER_ROW) {
      cur.style.maxWidth = 'calc(var(--md-tile-size) * ' +
          Math.ceil(cur.childNodes.length / 2) + ')';
    }
  }

  const flushOpacity = () => window.getComputedStyle(cur).opacity;

  // getComputedStyle causes the initial style (opacity 0) to be applied, so
  // that when we then set it to 1, that triggers the CSS transition.
  flushOpacity();
  cur.style.opacity = 1.0;

  if (document.documentElement.classList.contains(CLASSES.NO_INITIAL_FADE)) {
    flushOpacity();
    document.documentElement.classList.remove(CLASSES.NO_INITIAL_FADE);
  }

  // Make sure the tiles variable contain the next tileset we'll use if the host
  // page sends us an updated set of tiles.
  tiles = document.createElement('div');
}

/**
 * Explicitly hide tiles that are not visible in order to prevent keyboard
 * navigation.
 */
function updateTileVisibility() {
  const allTiles =
      document.querySelectorAll('#' + IDS.MV_TILES + ' .' + CLASSES.MD_TILE);
  if (allTiles.length === 0) {
    return;
  }

  // Get the current number of tiles per row. Hide any tile after the first two
  // rows.
  const tilesPerRow = Math.trunc(document.body.offsetWidth / MD_TILE_WIDTH);
  for (let i = MD_NUM_TILES_ALWAYS_VISIBLE; i < allTiles.length; i++) {
    allTiles[i].style.display = (i < tilesPerRow * 2) ? 'block' : 'none';
  }
}

/**
 * Handler for the 'show' message from the host page, called when it wants to
 * add a suggestion tile.
 * It's also used to fill up our tiles to |maxNumTiles| if necessary.
 * @param {?MostVisitedData} args Data for the tile to be rendered.
 */
function addTile(args) {
  if (isFinite(args.rid)) {
    // An actual suggestion. Grab the data from the embeddedSearch API.
    const data =
        chrome.embeddedSearch.newTabPage.getMostVisitedItemData(args.rid);
    if (!data) {
      return;
    }

    if (!data.faviconUrl) {
      data.faviconUrl = 'chrome-search://favicon/size/16@' +
          window.devicePixelRatio + 'x/' + data.renderViewId + '/' + data.rid;
    }
    tiles.appendChild(renderTile(data));
  } else {
    // An empty tile
    tiles.appendChild(renderTile(null));
  }
}

/**
 * Called when the user decided to add a tile to the blacklist.
 * It sets off the animation for the blacklist and sends the blacklisted id
 * to the host page.
 * @param {Element} tile DOM node of the tile we want to remove.
 */
function blacklistTile(tile) {
  const rid = Number(tile.getAttribute('data-rid'));

  if (isCustomLinksEnabled()) {
    chrome.embeddedSearch.newTabPage.deleteMostVisitedItem(rid);
  } else {
    tile.classList.add('blacklisted');
    tile.addEventListener('transitionend', function(ev) {
      if (ev.propertyName != 'width') {
        return;
      }
      window.parent.postMessage(
          {cmd: 'tileBlacklisted', rid: Number(rid)}, DOMAIN_ORIGIN);
    });
  }
}

/**
 * Starts edit custom link flow. Tells host page to show the edit custom link
 * dialog and pre-populate it with data obtained using the link's id.
 * @param {?number} rid Restricted id of the tile we want to edit.
 */
function editCustomLink(rid) {
  window.parent.postMessage({cmd: 'startEditLink', rid: rid}, DOMAIN_ORIGIN);
}

/**
 * Starts the reorder flow. Updates the visual style of the held tile to
 * indicate that it is being moved.
 * @param {!Element} tile Tile that is being moved.
 */
function startReorder(tile) {
  reordering = true;
  elementToReorder = tile;

  tile.classList.add(CLASSES.REORDER);
  // Disable other hover/active styling for all tiles.
  document.body.classList.add(CLASSES.REORDERING);

  document.addEventListener('dragend', () => {
    stopReorder(tile);
  }, {once: true});
}

/**
 * Stops the reorder flow. Resets the held tile's visual style and tells the
 * EmbeddedSearchAPI that a tile has been moved.
 * @param {!Element} tile Tile that has been moved.
 */
function stopReorder(tile) {
  reordering = false;
  elementToReorder = null;

  tile.classList.remove(CLASSES.REORDER);
  document.body.classList.remove(CLASSES.REORDERING);

  // Update |data-pos| for all tiles and notify EmbeddedSearchAPI that the tile
  // has been moved.
  const allTiles = document.querySelectorAll('#mv-tiles .' + CLASSES.MD_TILE);
  for (let i = 0; i < allTiles.length; i++) {
    allTiles[i].setAttribute('data-pos', i);
  }
  chrome.embeddedSearch.newTabPage.reorderCustomLink(
      Number(tile.getAttribute('data-rid')),
      Number(tile.getAttribute('data-pos')));
}

/**
 * Sets up event listeners necessary for tile reordering.
 * @param {!Element} tile Tile on which to set the event listeners.
 */
function setupReorder(tile) {
  // Starts the reorder flow.
  tile.addEventListener('dragstart', (event) => {
    if (!reordering) {
      startReorder(tile);
    }
  });

  tile.addEventListener('dragover', (event) => {
    // Only executed when the reorder flow is ongoing. Inserts the tile that is
    // being moved before/after this |tile| according to order in the list.
    if (reordering && elementToReorder && elementToReorder != tile) {
      // Determine which side to insert the element on:
      // - If the held tile comes after the current tile, insert behind the
      //   current tile.
      // - If the held tile comes before the current tile, insert in front of
      //   the current tile.
      let insertBefore;  // Element to insert the held tile behind.
      if (tile.compareDocumentPosition(elementToReorder) &
          Node.DOCUMENT_POSITION_FOLLOWING) {
        insertBefore = tile;
      } else {
        insertBefore = tile.nextSibling;
      }
      $('mv-tiles').insertBefore(elementToReorder, insertBefore);
    }
  });
}

/**
 * Renders a MostVisited tile to the DOM.
 * @param {?MostVisitedData} data Object containing rid, url, title, favicon,
 *     thumbnail, and optionally isAddButton. isAddButton is true if you want to
 *     construct an add custom link button. data is null if you want to
 *     construct an empty tile. isAddButton can only be set if custom links is
 *     enabled.
 */
function renderTile(data) {
  return renderMaterialDesignTile(data);
}

/**
 * Renders a MostVisited tile with Material Design styles.
 * @param {?MostVisitedData} data Object containing rid, url, title, favicon,
 *     and optionally isAddButton. isAddButton is if you want to construct an
 *     add custom link button. data is null if you want to construct an empty
 *     tile.
 * @return {Element}
 */
function renderMaterialDesignTile(data) {
  const mdTile = document.createElement('a');
  if (data == null) {
    mdTile.className = CLASSES.MD_EMPTY_TILE;
    return mdTile;
  }
  mdTile.className = CLASSES.MD_TILE;

  // The tile will be appended to |tiles|.
  const position = tiles.children.length;
  // This is set in the load/error event for the favicon image.
  let tileType = TileVisualType.NONE;

  mdTile.setAttribute('data-rid', data.rid);
  mdTile.setAttribute('data-pos', position);
  if (utils.isSchemeAllowed(data.url)) {
    mdTile.href = data.url;
  }
  mdTile.setAttribute('aria-label', data.title);
  mdTile.title = data.title;

  mdTile.addEventListener('click', function(ev) {
    if (data.isAddButton) {
      editCustomLink(null);
      logEvent(LOG_TYPE.NTP_CUSTOMIZE_ADD_SHORTCUT_CLICKED);
    } else {
      logMostVisitedNavigation(
          position, data.tileTitleSource, data.tileSource, tileType,
          data.dataGenerationTime);
    }
  });
  mdTile.addEventListener('keydown', function(event) {
    if ((event.keyCode === KEYCODES.DELETE ||
         event.keyCode === KEYCODES.BACKSPACE) &&
        !data.isAddButton) {
      event.preventDefault();
      event.stopPropagation();
      blacklistTile(mdTile);
    } else if (
        event.keyCode === KEYCODES.ENTER || event.keyCode === KEYCODES.SPACE) {
      event.preventDefault();
      this.click();
    } else if (event.keyCode === KEYCODES.LEFT) {
      const tiles = document.querySelectorAll(
          '#' + IDS.MV_TILES + ' .' + CLASSES.MD_TILE);
      tiles[Math.max(Number(this.getAttribute('data-pos')) - 1, 0)].focus();
    } else if (event.keyCode === KEYCODES.RIGHT) {
      const tiles = document.querySelectorAll(
          '#' + IDS.MV_TILES + ' .' + CLASSES.MD_TILE);
      tiles[Math.min(
                Number(this.getAttribute('data-pos')) + 1, tiles.length - 1)]
          .focus();
    }
  });
  utils.disableOutlineOnMouseClick(mdTile);

  const mdTileInner = document.createElement('div');
  mdTileInner.className = CLASSES.MD_TILE_INNER;

  if (data.isAddButton) {
    mdTile.tabIndex = 0;

    const mdIconAdd = document.createElement('div');
    mdIconAdd.classList.add(CLASSES.MD_ICON);
    mdIconAdd.classList.add(CLASSES.MD_ADD_ICON);

    mdTileInner.appendChild(mdIconAdd);
  } else {
    const mdIcon = document.createElement('img');
    mdIcon.classList.add(CLASSES.MD_ICON);
    // Set title and alt to empty so screen readers won't say the image name.
    mdIcon.title = '';
    mdIcon.alt = '';
    const url = new URL('chrome-search://ntpicon/');
    url.searchParams.set('size', '24@' + window.devicePixelRatio + 'x');
    url.searchParams.set('url', data.url);
    mdIcon.src = url.toString();
    loadedCounter += 1;
    mdIcon.addEventListener('load', function(ev) {
      // Store the type for a potential later navigation.
      tileType = TileVisualType.ICON_REAL;
      logMostVisitedImpression(
          position, data.tileTitleSource, data.tileSource, tileType,
          data.dataGenerationTime);
      // Note: It's important to call countLoad last, because that might emit
      // the NTP_ALL_TILES_LOADED event, which must happen after the impression
      // log.
      countLoad();
    });
    mdIcon.addEventListener('error', function(ev) {
      const fallbackBackground = document.createElement('div');
      fallbackBackground.className = CLASSES.MD_ICON;
      const fallbackLetter = document.createElement('div');
      fallbackLetter.className = CLASSES.MD_FALLBACK_LETTER;
      fallbackLetter.textContent = data.title.charAt(0).toUpperCase();
      fallbackBackground.classList.add(CLASSES.FAILED_FAVICON);

      fallbackBackground.appendChild(fallbackLetter);
      mdTileInner.replaceChild(fallbackBackground, mdIcon);

      // Store the type for a potential later navigation.
      tileType = TileVisualType.ICON_DEFAULT;
      logMostVisitedImpression(
          position, data.tileTitleSource, data.tileSource, tileType,
          data.dataGenerationTime);
      // Note: It's important to call countLoad last, because that might emit
      // the NTP_ALL_TILES_LOADED event, which must happen after the impression
      // log.
      countLoad();
    });

    mdTileInner.appendChild(mdIcon);
  }

  const mdTitle = document.createElement('div');
  mdTitle.className = CLASSES.MD_TITLE;
  mdTitle.style.direction = data.direction || 'ltr';
  const mdTitleTextwrap = document.createElement('span');
  mdTitleTextwrap.innerText = data.title;
  mdTitle.appendChild(mdTitleTextwrap);
  mdTileInner.appendChild(mdTitle);
  mdTile.appendChild(mdTileInner);

  if (!data.isAddButton) {
    const mdMenu = document.createElement('button');
    mdMenu.className = CLASSES.MD_MENU;
    if (isCustomLinksEnabled()) {
      mdMenu.classList.add(CLASSES.MD_EDIT_MENU);
      mdMenu.title = queryArgs['editLinkTooltip'] || '';
      mdMenu.setAttribute(
          'aria-label',
          (queryArgs['editLinkTooltip'] || '') + ' ' + data.title);
      mdMenu.addEventListener('click', function(ev) {
        editCustomLink(data.rid);
        ev.preventDefault();
        ev.stopPropagation();
        logEvent(LOG_TYPE.NTP_CUSTOMIZE_EDIT_SHORTCUT_CLICKED);
      });
    } else {
      mdMenu.title = queryArgs['removeTooltip'] || '';
      mdMenu.setAttribute(
          'aria-label', (queryArgs['removeTooltip'] || '') + ' ' + data.title);
      mdMenu.addEventListener('click', function(ev) {
        removeAllOldTiles();
        blacklistTile(mdTile);
        ev.preventDefault();
        ev.stopPropagation();
      });
    }
    // Don't allow the event to bubble out to the containing tile, as that would
    // trigger navigation to the tile URL.
    mdMenu.addEventListener('keydown', function(ev) {
      ev.stopPropagation();
    });
    utils.disableOutlineOnMouseClick(mdMenu);

    mdTile.appendChild(mdMenu);
  }

  if (isGridEnabled) {
    return currGrid.createGridTile(mdTile, data.rid, !!data.isAddButton);
  } else {
    // Enable reordering.
    if (isCustomLinksEnabled() && !data.isAddButton) {
      mdTile.draggable = 'true';
      setupReorder(mdTile);
    }
    return mdTile;
  }
}

/**
 * Does some initialization and parses the query arguments passed to the iframe.
 */
function init() {
  // Create a new DOM element to hold the tiles. The tiles will be added
  // one-by-one via addTile, and the whole thing will be inserted into the page
  // in swapInNewTiles, after the parent has sent us the 'show' message, and all
  // thumbnails and favicons have loaded.
  tiles = document.createElement('div');

  // Parse query arguments.
  const query = window.location.search.substring(1).split('&');
  queryArgs = {};
  for (let i = 0; i < query.length; ++i) {
    const val = query[i].split('=');
    if (val[0] == '') {
      continue;
    }
    queryArgs[decodeURIComponent(val[0])] = decodeURIComponent(val[1]);
  }

  document.title = queryArgs['title'];

  // Enable RTL.
  if (queryArgs['rtl'] == '1') {
    document.documentElement.dir = 'rtl';
  }

  // Enable custom links.
  if (queryArgs['enableCustomLinks'] == '1') {
    customLinksFeatureEnabled = true;
  }

  // Enable grid layout.
  if (queryArgs['enableGrid'] == '1') {
    isGridEnabled = true;
    document.body.classList.add(CLASSES.GRID_LAYOUT);
  }

  // Set the maximum number of tiles to show.
  if (isCustomLinksEnabled()) {
    maxNumTiles = MD_MAX_NUM_CUSTOM_LINK_TILES;
  }

  currGrid = new Grid();
  // Set up layout updates on window resize. Throttled according to
  // |RESIZE_TIMEOUT_DELAY|.
  let resizeTimeout;
  window.onresize = () => {
    if (resizeTimeout) {
      window.clearTimeout(resizeTimeout);
    }
    resizeTimeout = window.setTimeout(() => {
      resizeTimeout = null;
      if (isGridEnabled) {
        currGrid.onResize();
      } else {
        updateTileVisibility();
      }
    }, RESIZE_TIMEOUT_DELAY);
  };

  window.addEventListener('message', handlePostMessage);
}

/**
 * Binds event listeners.
 */
function listen() {
  document.addEventListener('DOMContentLoaded', init);
}

return {
  Grid: Grid,  // Exposed for testing.
  init: init,  // Exposed for testing.
  enableGridLayoutForTesting: enableGridLayoutForTesting,
  listen: listen,
};
}

if (!window.mostVisitedUnitTest) {
  MostVisited().listen();
}