Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('media', function() { | 5 cr.define('media', function() { |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The width and height of a CacheEntry canvas in pixels. | |
| 9 */ | |
| 10 var CANVAS_WIDTH = 500; | |
| 11 var CANVAS_HEIGHT = 31; | |
| 12 | |
| 13 /** | |
| 8 * This class represents a file cached by net. | 14 * This class represents a file cached by net. |
| 9 */ | 15 */ |
| 10 function CacheEntry() { | 16 function CacheEntry() { |
| 11 this.read_ = new media.DisjointRangeSet; | 17 this.read_ = new media.DisjointRangeSet; |
| 12 this.written_ = new media.DisjointRangeSet; | 18 this.written_ = new media.DisjointRangeSet; |
| 13 this.available_ = new media.DisjointRangeSet; | 19 this.available_ = new media.DisjointRangeSet; |
| 14 | 20 |
| 15 // Set to true when we know the entry is sparse. | 21 // Set to true when we know the entry is sparse. |
| 16 this.sparse = false; | 22 this.sparse = false; |
| 17 this.key = null; | 23 this.key = null; |
| 18 this.size = null; | 24 this.size = null; |
| 25 | |
| 26 // The <details> element representing this CacheEntry. | |
| 27 this.details_ = document.createElement('details'); | |
| 28 this.details_.className = 'cache-entry'; | |
| 29 this.details_.open = false; | |
| 30 | |
| 31 // The <details> summary line. It contains a chart of requested file ranges | |
| 32 // and the url if we know it. | |
| 33 var summary = document.createElement('summary'); | |
| 34 | |
| 35 this.summary_text_ = document.createTextNode(''); | |
| 36 summary.appendChild(this.summary_text_); | |
| 37 | |
| 38 summary.appendChild(document.createTextNode(' ')); | |
| 39 | |
| 40 // Controls to modify this CacheEntry. | |
| 41 var controls = document.createElement('span'); | |
| 42 controls.className = 'cache-entry-controls'; | |
| 43 summary.appendChild(controls); | |
| 44 summary.appendChild(document.createElement('br')); | |
| 45 | |
| 46 // A link to clear recorded data from this CacheEntry. | |
| 47 var clear_control = document.createElement('a'); | |
| 48 clear_control.href = 'javascript:void(0)'; | |
| 49 clear_control.onclick = this.clear.bind(this); | |
| 50 clear_control.textContent = '(clear entry)'; | |
| 51 controls.appendChild(clear_control); | |
| 52 | |
| 53 // The canvas upon which to draw the cached sections of a file. | |
| 54 this.canvas_ = document.createElement('canvas'); | |
| 55 this.canvas_.width = CANVAS_WIDTH; | |
| 56 this.canvas_.height = CANVAS_HEIGHT; | |
| 57 summary.appendChild(this.canvas_); | |
| 58 | |
| 59 this.details_.appendChild(summary); | |
| 60 | |
| 61 // A tabular representation of the data in the above canvas. | |
| 62 this.detail_table_ = document.createElement('table'); | |
|
Evan Stade
2011/07/26 01:16:26
camelCase_
Scott Franklin
2011/07/26 20:26:34
Oops, reverted to C++. Done.
| |
| 63 this.detail_table_.className = 'cache-table'; | |
| 64 this.details_.appendChild(this.detail_table_); | |
| 19 }; | 65 }; |
| 20 | 66 |
| 21 CacheEntry.prototype = { | 67 CacheEntry.prototype = { |
| 22 /** | 68 /** |
| 23 * Mark a range of bytes as read from the cache. | 69 * Mark a range of bytes as read from the cache. |
| 24 * @param {int} start The first byte read. | 70 * @param {int} start The first byte read. |
| 25 * @param {int} length The number of bytes read. | 71 * @param {int} length The number of bytes read. |
| 26 */ | 72 */ |
| 27 readBytes: function(start, length) { | 73 readBytes: function(start, length) { |
| 28 start = parseInt(start); | 74 start = parseInt(start); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 52 merge: function(other) { | 98 merge: function(other) { |
| 53 this.read_.merge(other.read_); | 99 this.read_.merge(other.read_); |
| 54 this.written_.merge(other.written_); | 100 this.written_.merge(other.written_); |
| 55 this.available_.merge(other.available_); | 101 this.available_.merge(other.available_); |
| 56 this.sparse = this.sparse || other.sparse; | 102 this.sparse = this.sparse || other.sparse; |
| 57 this.key = this.key || other.key; | 103 this.key = this.key || other.key; |
| 58 this.size = this.size || other.size; | 104 this.size = this.size || other.size; |
| 59 }, | 105 }, |
| 60 | 106 |
| 61 /** | 107 /** |
| 108 * Clear all recorded ranges from this CacheEntry and redraw this.details_. | |
| 109 */ | |
| 110 clear: function() { | |
| 111 this.read_ = new media.DisjointRangeSet; | |
| 112 this.written_ = new media.DisjointRangeSet; | |
| 113 this.available_ = new media.DisjointRangeSet; | |
| 114 this.generateDetails(); | |
| 115 }, | |
| 116 | |
| 117 /** | |
| 118 * Redraw this.canvas_. | |
| 119 * It should consist of two horizontal bars with highlighted sections to | |
| 120 * represent which parts of a file have been read from (top) and written to | |
| 121 * (bottom) the cache. | |
| 122 * e.g. |xxxxxx----------x| | |
| 123 * |-----xxxxx-------| | |
| 124 */ | |
| 125 generateCanvas: function() { | |
| 126 var context = this.canvas_.getContext('2d'); | |
| 127 context.textAlign = 'center'; | |
| 128 context.textBaseline = 'middle'; | |
| 129 | |
| 130 context.fillStyle = '#aaa'; | |
| 131 context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); | |
| 132 | |
| 133 if (this.size) { | |
| 134 var file_size = this.size; | |
| 135 drawRange = function(start, end, top) { | |
| 136 var left = start / file_size * CANVAS_WIDTH; | |
| 137 var right = end / file_size * CANVAS_WIDTH; | |
| 138 context.fillRect(left, top, right - left, top + CANVAS_HEIGHT / 2); | |
| 139 }; | |
| 140 | |
| 141 context.fillStyle = '#0a0'; | |
| 142 this.read_.map(function(start, end) { | |
| 143 drawRange(start, end, 0); | |
| 144 }); | |
| 145 | |
| 146 context.fillStyle = '#00a'; | |
| 147 this.written_.map(function(start, end) { | |
| 148 drawRange(start, end, CANVAS_HEIGHT / 2); | |
| 149 }); | |
| 150 | |
| 151 // Overlay a description of each bar. | |
| 152 context.fillStyle = '#fff'; | |
| 153 context.fillText('Read from cache.', CANVAS_WIDTH / 2, | |
| 154 CANVAS_HEIGHT / 4 - 0.5); | |
| 155 context.fillText('Written to cache.', CANVAS_WIDTH / 2, | |
| 156 CANVAS_HEIGHT * 3 / 4 + 0.5); | |
| 157 | |
| 158 // Add a 1px separator line. | |
| 159 context.moveTo(0, CANVAS_HEIGHT / 2); | |
| 160 context.lineTo(CANVAS_WIDTH, CANVAS_HEIGHT / 2); | |
| 161 context.strokeStyle = '#fff'; | |
| 162 context.stroke(); | |
| 163 } else { | |
| 164 // We can't draw bars if we don't know how big the file is. | |
| 165 context.fillStyle = '#fff'; | |
| 166 context.fillText('Unknown file size. See details below.', | |
| 167 CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2); | |
| 168 } | |
| 169 }, | |
| 170 | |
| 171 /** | |
| 172 * Update this.details_ to contain everything we currently know about | |
| 173 * this file. | |
| 174 */ | |
| 175 generateDetails: function() { | |
| 176 function makeElement(type, content) { | |
| 177 var element = document.createElement(type); | |
| 178 element.textContent = content || ''; | |
|
Evan Stade
2011/07/26 01:16:26
I don't think the || part is necessary here?
Scott Franklin
2011/07/26 20:26:34
It's not. It was there in case the second argument
| |
| 179 return element; | |
| 180 }; | |
| 181 | |
| 182 this.details_.id = this.key; | |
| 183 this.summary_text_.textContent = this.key || 'Unknown File'; | |
| 184 | |
| 185 this.detail_table_.textContent = ''; | |
| 186 var header = document.createElement('thead'); | |
| 187 var footer = document.createElement('tfoot'); | |
| 188 var body = document.createElement('tbody'); | |
| 189 this.detail_table_.appendChild(header); | |
| 190 this.detail_table_.appendChild(footer); | |
| 191 this.detail_table_.appendChild(body); | |
| 192 | |
| 193 var header_row = document.createElement('tr'); | |
| 194 header_row.appendChild(makeElement('th', 'Read From Cache')); | |
| 195 header_row.appendChild(makeElement('th', 'Written To Cache')); | |
| 196 header.appendChild(header_row); | |
| 197 | |
| 198 var footer_row = document.createElement('tr'); | |
| 199 var footer_cell = document.createElement('td'); | |
| 200 footer_cell.textContent = 'Out of ' + (this.size || 'unkown size'); | |
| 201 footer_cell.setAttribute('colspan', 2); | |
| 202 footer_row.appendChild(footer_cell); | |
| 203 footer.appendChild(footer_row); | |
| 204 | |
| 205 var read = this.read_.map(function(start, end) { | |
| 206 return start + ' - ' + end; | |
| 207 }); | |
| 208 var written = this.written_.map(function(start, end) { | |
| 209 return start + ' - ' + end; | |
| 210 }); | |
| 211 | |
| 212 var length = Math.max(read.length, written.length); | |
| 213 for (var i = 0; i < length; i++) { | |
| 214 var row = document.createElement('tr'); | |
| 215 row.appendChild(makeElement('td', read[i] || '')); | |
| 216 row.appendChild(makeElement('td', written[i] || '')); | |
| 217 body.appendChild(row); | |
| 218 } | |
| 219 | |
| 220 this.generateCanvas(); | |
| 221 }, | |
| 222 | |
| 223 /** | |
| 62 * Render this CacheEntry as a <li>. | 224 * Render this CacheEntry as a <li>. |
| 63 * @return {HTMLElement} A <li> representing this CacheEntry. | 225 * @return {HTMLElement} A <li> representing this CacheEntry. |
| 64 */ | 226 */ |
| 65 toListItem: function() { | 227 toListItem: function() { |
| 228 this.generateDetails(); | |
| 229 | |
| 66 var result = document.createElement('li'); | 230 var result = document.createElement('li'); |
| 67 result.id = this.key; | 231 result.appendChild(this.details_); |
| 68 result.className = 'cache-entry'; | |
| 69 result.innerHTML = this.key + '<br />Read: '; | |
| 70 result.innerHTML += this.read_.map(function(start, end) { | |
| 71 return start + '-' + end; | |
| 72 }).join(', '); | |
| 73 | |
| 74 result.innerHTML += '. Written: '; | |
| 75 result.innerHTML += this.written_.map(function(start, end) { | |
| 76 return start + '-' + end; | |
| 77 }).join(', '); | |
| 78 | |
| 79 // Include the total size if we know it. | |
| 80 if (this.size) { | |
| 81 result.innerHTML += '. Out of '; | |
| 82 result.innerHTML += this.size; | |
| 83 } | |
| 84 return result; | 232 return result; |
| 85 } | 233 } |
| 86 }; | 234 }; |
| 87 | 235 |
| 88 return { | 236 return { |
| 89 CacheEntry: CacheEntry | 237 CacheEntry: CacheEntry |
| 90 }; | 238 }; |
| 91 }); | 239 }); |
| OLD | NEW |