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.
6 * Requests the database from the backend.
8 function requestResourcePrefetchPredictorDb() {
9 chrome
.send('requestResourcePrefetchPredictorDb');
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
19 function updateResourcePrefetchPredictorDb(database
) {
20 updateResourcePrefetchPredictorDbView(database
);
24 * Truncates the string to keep the database readable.
25 * @param {string} str The string to truncate.
26 * @return {string} The truncated string.
28 function truncateString(str
) {
29 return str
.length
< 100 ? str
: str
.substring(0, 99);
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.
38 function updateResourcePrefetchPredictorDbView(database
) {
39 if (!database
.enabled
) {
40 $('rpp_enabled').style
.display
= 'none';
41 $('rpp_disabled').style
.display
= 'block';
44 $('rpp_enabled').style
.display
= 'block';
45 $('rpp_disabled').style
.display
= 'none';
48 var hasUrlData
= database
.url_db
&& database
.url_db
.length
> 0;
49 var hasHostData
= database
.host_db
&& database
.host_db
.length
> 0;
52 renderCacheData($('rpp_url_body'), database
.url_db
);
54 renderCacheData($('rpp_host_body'), database
.host_db
);
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.
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');
72 var t
= document
.createElement('td');
73 t
.rowSpan
= main
.resources
.length
;
74 t
.textContent
= truncateString(main
.main_frame_url
);
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
=
94 row
.appendChild(document
.createElement('td')).textContent
=
96 body
.appendChild(row
);
101 document
.addEventListener('DOMContentLoaded',
102 requestResourcePrefetchPredictorDb
);