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 'use strict'; |
| 7 /** | |
| 8 * The width and height of a CacheEntry canvas in pixels. | |
| 9 */ | |
| 10 var CANVAS_WIDTH = 500; | |
| 11 var CANVAS_HEIGHT = 31; | |
| 12 | 7 |
| 13 /** | 8 /** |
| 14 * This class represents a file cached by net. | 9 * This class represents a file cached by net. |
| 15 */ | 10 */ |
| 16 function CacheEntry() { | 11 function CacheEntry() { |
| 17 this.read_ = new media.DisjointRangeSet; | 12 this.read_ = new media.DisjointRangeSet; |
| 18 this.written_ = new media.DisjointRangeSet; | 13 this.written_ = new media.DisjointRangeSet; |
| 19 this.available_ = new media.DisjointRangeSet; | 14 this.available_ = new media.DisjointRangeSet; |
| 20 | 15 |
| 21 // Set to true when we know the entry is sparse. | 16 // Set to true when we know the entry is sparse. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 43 summary.appendChild(controls); | 38 summary.appendChild(controls); |
| 44 summary.appendChild(document.createElement('br')); | 39 summary.appendChild(document.createElement('br')); |
| 45 | 40 |
| 46 // A link to clear recorded data from this CacheEntry. | 41 // A link to clear recorded data from this CacheEntry. |
| 47 var clearControl = document.createElement('a'); | 42 var clearControl = document.createElement('a'); |
| 48 clearControl.href = 'javascript:void(0)'; | 43 clearControl.href = 'javascript:void(0)'; |
| 49 clearControl.onclick = this.clear.bind(this); | 44 clearControl.onclick = this.clear.bind(this); |
| 50 clearControl.textContent = '(clear entry)'; | 45 clearControl.textContent = '(clear entry)'; |
| 51 controls.appendChild(clearControl); | 46 controls.appendChild(clearControl); |
| 52 | 47 |
| 53 // The canvas upon which to draw the cached sections of a file. | 48 this.details_.appendChild(summary); |
| 54 this.canvas_ = document.createElement('canvas'); | |
| 55 this.canvas_.width = CANVAS_WIDTH; | |
| 56 this.canvas_.height = CANVAS_HEIGHT; | |
| 57 summary.appendChild(this.canvas_); | |
| 58 | 49 |
| 59 this.details_.appendChild(summary); | 50 // The canvas for drawing cache writes. |
| 51 this.writeCanvas = document.createElement('canvas'); | |
| 52 this.writeCanvas.width = media.BAR_WIDTH; | |
| 53 this.writeCanvas.height = media.BAR_HEIGHT; | |
| 54 this.details_.appendChild(this.writeCanvas); | |
| 55 | |
| 56 // The canvas for drawing cache reads. | |
| 57 this.readCanvas = document.createElement('canvas'); | |
| 58 this.readCanvas.width = media.BAR_WIDTH; | |
| 59 this.readCanvas.height = media.BAR_HEIGHT; | |
| 60 this.details_.appendChild(this.readCanvas); | |
| 60 | 61 |
| 61 // A tabular representation of the data in the above canvas. | 62 // A tabular representation of the data in the above canvas. |
| 62 this.detailTable_ = document.createElement('table'); | 63 this.detailTable_ = document.createElement('table'); |
| 63 this.detailTable_.className = 'cache-table'; | 64 this.detailTable_.className = 'cache-table'; |
| 64 this.details_.appendChild(this.detailTable_); | 65 this.details_.appendChild(this.detailTable_); |
| 65 }; | 66 } |
| 66 | 67 |
| 67 CacheEntry.prototype = { | 68 CacheEntry.prototype = { |
| 68 /** | 69 /** |
| 69 * Mark a range of bytes as read from the cache. | 70 * Mark a range of bytes as read from the cache. |
| 70 * @param {int} start The first byte read. | 71 * @param {int} start The first byte read. |
| 71 * @param {int} length The number of bytes read. | 72 * @param {int} length The number of bytes read. |
| 72 */ | 73 */ |
| 73 readBytes: function(start, length) { | 74 readBytes: function(start, length) { |
| 74 start = parseInt(start); | 75 start = parseInt(start); |
| 75 length = parseInt(length); | 76 length = parseInt(length); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 * Clear all recorded ranges from this CacheEntry and redraw this.details_. | 109 * Clear all recorded ranges from this CacheEntry and redraw this.details_. |
| 109 */ | 110 */ |
| 110 clear: function() { | 111 clear: function() { |
| 111 this.read_ = new media.DisjointRangeSet; | 112 this.read_ = new media.DisjointRangeSet; |
| 112 this.written_ = new media.DisjointRangeSet; | 113 this.written_ = new media.DisjointRangeSet; |
| 113 this.available_ = new media.DisjointRangeSet; | 114 this.available_ = new media.DisjointRangeSet; |
| 114 this.generateDetails(); | 115 this.generateDetails(); |
| 115 }, | 116 }, |
| 116 | 117 |
| 117 /** | 118 /** |
| 118 * Redraw this.canvas_. | 119 * Draw cache writes to the given canvas. |
| 119 * It should consist of two horizontal bars with highlighted sections to | 120 * |
| 120 * represent which parts of a file have been read from (top) and written to | 121 * It should consist of a horizontal bar with highlighted sections to |
| 121 * (bottom) the cache. | 122 * represent which parts of a file have been written to the cache. |
| 123 * | |
| 122 * e.g. |xxxxxx----------x| | 124 * e.g. |xxxxxx----------x| |
| 123 * |-----xxxxx-------| | |
| 124 */ | 125 */ |
| 125 generateCanvas: function() { | 126 drawCacheWritesToCanvas: function(canvas) { |
| 126 var context = this.canvas_.getContext('2d'); | 127 // Don't bother drawing anything if we don't know the total size. |
| 127 context.textAlign = 'center'; | 128 if (!this.size) { |
| 128 context.textBaseline = 'middle'; | 129 return; |
| 130 } | |
| 131 | |
| 132 var width = canvas.width; | |
| 133 var height = canvas.height; | |
| 134 var context = canvas.getContext('2d'); | |
| 135 var fileSize = this.size; | |
| 129 | 136 |
| 130 context.fillStyle = '#aaa'; | 137 context.fillStyle = '#aaa'; |
| 131 context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); | 138 context.fillRect(0, 0, width, height); |
| 132 | 139 |
| 133 if (this.size) { | 140 var drawRange = function(start, end) { |
|
arv (Not doing code reviews)
2011/10/20 20:29:37
function drawRange(start, end) {
...
}
scherkus (not reviewing)
2011/10/24 18:12:28
Done.
| |
| 134 var fileSize = this.size; | 141 var left = start / fileSize * width; |
| 135 drawRange = function(start, end, top) { | 142 var right = end / fileSize * width; |
| 136 var left = start / fileSize * CANVAS_WIDTH; | 143 context.fillRect(left, 0, right - left, height); |
| 137 var right = end / fileSize * CANVAS_WIDTH; | 144 }; |
| 138 context.fillRect(left, top, right - left, top + CANVAS_HEIGHT / 2); | |
| 139 }; | |
| 140 | 145 |
| 141 context.fillStyle = '#0a0'; | 146 context.fillStyle = '#00a'; |
| 142 this.read_.map(function(start, end) { | 147 this.written_.map(function(start, end) { |
| 143 drawRange(start, end, 0); | 148 drawRange(start, end); |
| 144 }); | 149 }); |
| 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 }, | 150 }, |
| 170 | 151 |
| 171 /** | 152 /** |
| 153 * Draw cache reads to the given canvas. | |
| 154 * | |
| 155 * It should consist of a horizontal bar with highlighted sections to | |
| 156 * represent which parts of a file have been read from the cache. | |
| 157 * | |
| 158 * e.g. |xxxxxx----------x| | |
| 159 */ | |
| 160 drawCacheReadsToCanvas: function(canvas) { | |
|
arv (Not doing code reviews)
2011/10/20 20:29:37
This looks like copy pasted code. Can you refactor
scherkus (not reviewing)
2011/10/24 18:12:28
Busted :(
Refactored but keep in mind I'm not sur
| |
| 161 // Don't bother drawing anything if we don't know the total size. | |
| 162 if (!this.size) { | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 var width = canvas.width; | |
| 167 var height = canvas.height; | |
| 168 var context = canvas.getContext('2d'); | |
| 169 var fileSize = this.size; | |
| 170 | |
| 171 context.fillStyle = '#aaa'; | |
| 172 context.fillRect(0, 0, width, height); | |
| 173 | |
| 174 var drawRange = function(start, end) { | |
| 175 var left = start / fileSize * width; | |
| 176 var right = end / fileSize * width; | |
| 177 context.fillRect(left, 0, right - left, height); | |
| 178 }; | |
| 179 | |
| 180 context.fillStyle = '#0a0'; | |
| 181 this.read_.map(function(start, end) { | |
| 182 drawRange(start, end); | |
| 183 }); | |
| 184 }, | |
| 185 | |
| 186 /** | |
| 172 * Update this.details_ to contain everything we currently know about | 187 * Update this.details_ to contain everything we currently know about |
| 173 * this file. | 188 * this file. |
| 174 */ | 189 */ |
| 175 generateDetails: function() { | 190 generateDetails: function() { |
| 176 function makeElement(type, content) { | |
| 177 var element = document.createElement(type); | |
| 178 element.textContent = content; | |
| 179 return element; | |
| 180 }; | |
| 181 | |
| 182 this.details_.id = this.key; | 191 this.details_.id = this.key; |
| 183 this.summaryText_.textContent = this.key || 'Unknown File'; | 192 this.summaryText_.textContent = this.key || 'Unknown File'; |
| 184 | 193 |
| 185 this.detailTable_.textContent = ''; | 194 this.detailTable_.textContent = ''; |
| 186 var header = document.createElement('thead'); | 195 var header = document.createElement('thead'); |
| 187 var footer = document.createElement('tfoot'); | 196 var footer = document.createElement('tfoot'); |
| 188 var body = document.createElement('tbody'); | 197 var body = document.createElement('tbody'); |
| 189 this.detailTable_.appendChild(header); | 198 this.detailTable_.appendChild(header); |
| 190 this.detailTable_.appendChild(footer); | 199 this.detailTable_.appendChild(footer); |
| 191 this.detailTable_.appendChild(body); | 200 this.detailTable_.appendChild(body); |
| 192 | 201 |
| 193 var headerRow = document.createElement('tr'); | 202 var headerRow = document.createElement('tr'); |
| 194 headerRow.appendChild(makeElement('th', 'Read From Cache')); | 203 headerRow.appendChild(media.makeElement('th', 'Read From Cache')); |
| 195 headerRow.appendChild(makeElement('th', 'Written To Cache')); | 204 headerRow.appendChild(media.makeElement('th', 'Written To Cache')); |
| 196 header.appendChild(headerRow); | 205 header.appendChild(headerRow); |
| 197 | 206 |
| 198 var footerRow = document.createElement('tr'); | 207 var footerRow = document.createElement('tr'); |
| 199 var footerCell = document.createElement('td'); | 208 var footerCell = document.createElement('td'); |
| 200 footerCell.textContent = 'Out of ' + (this.size || 'unkown size'); | 209 footerCell.textContent = 'Out of ' + (this.size || 'unkown size'); |
| 201 footerCell.setAttribute('colspan', 2); | 210 footerCell.setAttribute('colspan', 2); |
| 202 footerRow.appendChild(footerCell); | 211 footerRow.appendChild(footerCell); |
| 203 footer.appendChild(footerRow); | 212 footer.appendChild(footerRow); |
| 204 | 213 |
| 205 var read = this.read_.map(function(start, end) { | 214 var read = this.read_.map(function(start, end) { |
| 206 return start + ' - ' + end; | 215 return start + ' - ' + end; |
| 207 }); | 216 }); |
| 208 var written = this.written_.map(function(start, end) { | 217 var written = this.written_.map(function(start, end) { |
| 209 return start + ' - ' + end; | 218 return start + ' - ' + end; |
| 210 }); | 219 }); |
| 211 | 220 |
| 212 var length = Math.max(read.length, written.length); | 221 var length = Math.max(read.length, written.length); |
| 213 for (var i = 0; i < length; i++) { | 222 for (var i = 0; i < length; i++) { |
| 214 var row = document.createElement('tr'); | 223 var row = document.createElement('tr'); |
| 215 row.appendChild(makeElement('td', read[i] || '')); | 224 row.appendChild(media.makeElement('td', read[i] || '')); |
| 216 row.appendChild(makeElement('td', written[i] || '')); | 225 row.appendChild(media.makeElement('td', written[i] || '')); |
| 217 body.appendChild(row); | 226 body.appendChild(row); |
| 218 } | 227 } |
| 219 | 228 |
| 220 this.generateCanvas(); | 229 this.drawCacheWritesToCanvas(this.writeCanvas); |
| 230 this.drawCacheReadsToCanvas(this.readCanvas); | |
| 221 }, | 231 }, |
| 222 | 232 |
| 223 /** | 233 /** |
| 224 * Render this CacheEntry as a <li>. | 234 * Render this CacheEntry as a <li>. |
| 225 * @return {HTMLElement} A <li> representing this CacheEntry. | 235 * @return {HTMLElement} A <li> representing this CacheEntry. |
| 226 */ | 236 */ |
| 227 toListItem: function() { | 237 toListItem: function() { |
| 228 this.generateDetails(); | 238 this.generateDetails(); |
| 229 | 239 |
| 230 var result = document.createElement('li'); | 240 var result = document.createElement('li'); |
| 231 result.appendChild(this.details_); | 241 result.appendChild(this.details_); |
| 232 return result; | 242 return result; |
| 233 } | 243 } |
| 234 }; | 244 }; |
| 235 | 245 |
| 236 return { | 246 return { |
| 237 CacheEntry: CacheEntry | 247 CacheEntry: CacheEntry |
| 238 }; | 248 }; |
| 239 }); | 249 }); |
| OLD | NEW |