Re-add resource speculative prefetching code
[chromium-blink-merge.git] / chrome / browser / resources / predictors / resource_prefetch_predictor.js
blobf725105441dfbcb0fe71a6c068127ffbcc5c68f5
1 // Copyright 2014 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.
5 /**
6  * Requests the database from the backend.
7  */
8 function requestResourcePrefetchPredictorDb() {
9   chrome.send('requestResourcePrefetchPredictorDb');
12 /**
13  * Callback from backend with the database contents. Sets up some globals and
14  * calls to create the UI.
15  * @param {Dictionary} database Information about ResourcePrefetchPredictor
16  *     including the database as a flattened list, a boolean indicating if the
17  *     system is enabled.
18  */
19 function updateResourcePrefetchPredictorDb(database) {
20   updateResourcePrefetchPredictorDbView(database);
23 /**
24  * Truncates the string to keep the database readable.
25  * @param {string} str The string to truncate.
26  * @return {string} The truncated string.
27  */
28 function truncateString(str) {
29   return str.length < 100 ? str : str.substring(0, 99);
32 /**
33  * Updates the table from the database.
34  * @param {Dictionary} database Information about ResourcePrefetchPredictor
35  *     including the database as a flattened list, a boolean indicating if the
36  *     system is enabled and the current hit weight.
37  */
38 function updateResourcePrefetchPredictorDbView(database) {
39   if (!database.enabled) {
40     $('rpp_enabled').style.display = 'none';
41     $('rpp_disabled').style.display = 'block';
42     return;
43   } else {
44     $('rpp_enabled').style.display = 'block';
45     $('rpp_disabled').style.display = 'none';
46   }
48   var hasUrlData = database.url_db && database.url_db.length > 0;
49   var hasHostData = database.host_db && database.host_db.length > 0;
51   if (hasUrlData)
52     renderCacheData($('rpp_url_body'), database.url_db);
53   if (hasHostData)
54     renderCacheData($('rpp_host_body'), database.host_db);
57 /**
58  * Renders cache data for URL or host based data.
59  * @param {HTMLElement} body element of table to render into.
60  * @param {Dictionary} database to render.
61  */
62 function renderCacheData(body, database) {
63   body.textContent = '';
64   for (var i = 0; i < database.length; ++i) {
65     var main = database[i];
67     for (var j = 0; j < main.resources.length; ++j) {
68       var resource = main.resources[j];
69       var row = document.createElement('tr');
71       if (j == 0) {
72         var t = document.createElement('td');
73         t.rowSpan = main.resources.length;
74         t.textContent = truncateString(main.main_frame_url);
75         t.className = 'last';
76         row.appendChild(t);
77       }
79       if (j == main.resources.length - 1)
80         row.className = 'last';
82       row.appendChild(document.createElement('td')).textContent =
83           truncateString(resource.resource_url);
84       row.appendChild(document.createElement('td')).textContent =
85           resource.resource_type;
86       row.appendChild(document.createElement('td')).textContent =
87           resource.number_of_hits;
88       row.appendChild(document.createElement('td')).textContent =
89           resource.number_of_misses;
90       row.appendChild(document.createElement('td')).textContent =
91           resource.consecutive_misses;
92       row.appendChild(document.createElement('td')).textContent =
93           resource.position;
94       row.appendChild(document.createElement('td')).textContent =
95           resource.score;
96       body.appendChild(row);
97     }
98   }
101 document.addEventListener('DOMContentLoaded',
102                           requestResourcePrefetchPredictorDb);