| // Copyright 2014 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. |
| |
| /** |
| * Requests the database from the backend. |
| */ |
| function requestResourcePrefetchPredictorDb() { |
| chrome.send('requestResourcePrefetchPredictorDb'); |
| } |
| |
| /** |
| * Callback from backend with the database contents. Sets up some globals and |
| * calls to create the UI. |
| * @param {Object} database Information about ResourcePrefetchPredictor |
| * including the database as a flattened list, a boolean indicating if the |
| * system is enabled. |
| */ |
| function updateResourcePrefetchPredictorDb(database) { |
| updateResourcePrefetchPredictorDbView(database); |
| } |
| |
| /** |
| * Truncates the string to keep the database readable. |
| * @param {string} str The string to truncate. |
| * @return {string} The truncated string. |
| */ |
| function truncateString(str) { |
| return str.length < 100 ? str : str.substring(0, 99); |
| } |
| |
| /** |
| * Updates the table from the database. |
| * @param {Object} database Information about ResourcePrefetchPredictor |
| * including the database as a flattened list, a boolean indicating if the |
| * system is enabled and the current hit weight. |
| */ |
| function updateResourcePrefetchPredictorDbView(database) { |
| if (!database.enabled) { |
| $('rpp_enabled').style.display = 'none'; |
| $('rpp_disabled').style.display = 'block'; |
| return; |
| } |
| |
| $('rpp_enabled').style.display = 'block'; |
| $('rpp_disabled').style.display = 'none'; |
| |
| const hasOriginData = database.origin_db && database.origin_db.length > 0; |
| |
| if (hasOriginData) |
| renderOriginData($('rpp_origin_body'), database.origin_db); |
| } |
| |
| /** |
| * Renders the content of the predictor origin table. |
| * @param {HTMLElement} body element of table to render into. |
| * @param {Object} database to render. |
| */ |
| function renderOriginData(body, database) { |
| body.textContent = ''; |
| for (const main of database) { |
| for (let j = 0; j < main.origins.length; ++j) { |
| const origin = main.origins[j]; |
| const row = document.createElement('tr'); |
| |
| if (j == 0) { |
| const t = document.createElement('td'); |
| t.rowSpan = main.origins.length; |
| t.textContent = truncateString(main.main_frame_host); |
| row.appendChild(t); |
| } |
| |
| row.className = 'action-none'; |
| row.appendChild(document.createElement('td')).textContent = |
| truncateString(origin.origin); |
| row.appendChild(document.createElement('td')).textContent = |
| origin.number_of_hits; |
| row.appendChild(document.createElement('td')).textContent = |
| origin.number_of_misses; |
| row.appendChild(document.createElement('td')).textContent = |
| origin.consecutive_misses; |
| row.appendChild(document.createElement('td')).textContent = |
| origin.position; |
| row.appendChild(document.createElement('td')).textContent = |
| origin.always_access_network; |
| row.appendChild(document.createElement('td')).textContent = |
| origin.accessed_network; |
| row.appendChild(document.createElement('td')).textContent = |
| origin.score; |
| body.appendChild(row); |
| } |
| } |
| } |
| |
| document.addEventListener( |
| 'DOMContentLoaded', requestResourcePrefetchPredictorDb); |