OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 /** |
| 6 * Requests the database from the backend. |
| 7 */ |
| 8 function requestResourcePrefetchPredictorDb() { |
| 9 chrome.send('requestResourcePrefetchPredictorDb'); |
| 10 } |
| 11 |
| 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); |
| 21 } |
| 22 |
| 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); |
| 30 } |
| 31 |
| 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 $('resource_prefetch_predictor_enabled').style.display = 'none'; |
| 41 $('resource_prefetch_predictor_disabled').style.display = 'block'; |
| 42 return; |
| 43 } else { |
| 44 $('resource_prefetch_predictor_enabled').style.display = 'block'; |
| 45 $('resource_prefetch_predictor_disabled').style.display = 'none'; |
| 46 } |
| 47 |
| 48 if (database.db) { |
| 49 if (database.db.length > 0) { |
| 50 $('resource_prefetch_predictor_db').style.display = 'block'; |
| 51 $('empty_resource_prefetch_predictor_db').style.display = 'none'; |
| 52 } else { |
| 53 $('resource_prefetch_predictor_db').style.display = 'none'; |
| 54 $('empty_resource_prefetch_predictor_db').style.display = 'block'; |
| 55 } |
| 56 |
| 57 var dbSection = $('resource_prefetch_predictor_db_body'); |
| 58 dbSection.textContent = ''; |
| 59 for (var i = 0; i < database.db.length; ++i) { |
| 60 var main = database.db[i]; |
| 61 |
| 62 for (var j = 0; j < main.resources.length; ++j) { |
| 63 var resource = main.resources[j]; |
| 64 var row = document.createElement('tr'); |
| 65 |
| 66 if (j == 0) { |
| 67 var t = document.createElement('td'); |
| 68 t.rowSpan = main.resources.length; |
| 69 t.textContent = truncateString(main.main_frame_url); |
| 70 t.className = 'last'; |
| 71 row.appendChild(t); |
| 72 } |
| 73 |
| 74 if (j == main.resources.length - 1) |
| 75 row.className = 'last'; |
| 76 |
| 77 row.appendChild(document.createElement('td')).textContent = |
| 78 truncateString(resource.resource_url); |
| 79 row.appendChild(document.createElement('td')).textContent = |
| 80 resource.resource_type; |
| 81 row.appendChild(document.createElement('td')).textContent = |
| 82 resource.number_of_hits; |
| 83 row.appendChild(document.createElement('td')).textContent = |
| 84 resource.number_of_misses; |
| 85 row.appendChild(document.createElement('td')).textContent = |
| 86 resource.consecutive_misses; |
| 87 row.appendChild(document.createElement('td')).textContent = |
| 88 resource.position; |
| 89 row.appendChild(document.createElement('td')).textContent = |
| 90 resource.score; |
| 91 dbSection.appendChild(row); |
| 92 } |
| 93 } |
| 94 } |
| 95 } |
| 96 |
| 97 document.addEventListener('DOMContentLoaded', |
| 98 requestResourcePrefetchPredictorDb); |
OLD | NEW |