| 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 * Helper for drawCacheReadsToCanvas() and drawCacheWritesToCanvas(). |
| 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 * Accepts the entries to draw, a canvas fill style, and the canvas to |
| 121 * (bottom) the cache. | 122 * draw on. |
| 122 * e.g. |xxxxxx----------x| | |
| 123 * |-----xxxxx-------| | |
| 124 */ | 123 */ |
| 125 generateCanvas: function() { | 124 drawCacheEntriesToCanvas: function(entries, fillStyle, canvas) { |
| 126 var context = this.canvas_.getContext('2d'); | 125 // Don't bother drawing anything if we don't know the total size. |
| 127 context.textAlign = 'center'; | 126 if (!this.size) { |
| 128 context.textBaseline = 'middle'; | 127 return; |
| 128 } |
| 129 |
| 130 var width = canvas.width; |
| 131 var height = canvas.height; |
| 132 var context = canvas.getContext('2d'); |
| 133 var fileSize = this.size; |
| 129 | 134 |
| 130 context.fillStyle = '#aaa'; | 135 context.fillStyle = '#aaa'; |
| 131 context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); | 136 context.fillRect(0, 0, width, height); |
| 132 | 137 |
| 133 if (this.size) { | 138 function drawRange(start, end) { |
| 134 var fileSize = this.size; | 139 var left = start / fileSize * width; |
| 135 drawRange = function(start, end, top) { | 140 var right = end / fileSize * width; |
| 136 var left = start / fileSize * CANVAS_WIDTH; | 141 context.fillRect(left, 0, right - left, height); |
| 137 var right = end / fileSize * CANVAS_WIDTH; | 142 } |
| 138 context.fillRect(left, top, right - left, top + CANVAS_HEIGHT / 2); | |
| 139 }; | |
| 140 | 143 |
| 141 context.fillStyle = '#0a0'; | 144 context.fillStyle = fillStyle; |
| 142 this.read_.map(function(start, end) { | 145 entries.map(function(start, end) { |
| 143 drawRange(start, end, 0); | 146 drawRange(start, end); |
| 144 }); | 147 }); |
| 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 }, | 148 }, |
| 170 | 149 |
| 171 /** | 150 /** |
| 151 * Draw cache writes to the given canvas. |
| 152 * |
| 153 * It should consist of a horizontal bar with highlighted sections to |
| 154 * represent which parts of a file have been written to the cache. |
| 155 * |
| 156 * e.g. |xxxxxx----------x| |
| 157 */ |
| 158 drawCacheWritesToCanvas: function(canvas) { |
| 159 this.drawCacheEntriesToCanvas(this.written_, '#00a', canvas); |
| 160 }, |
| 161 |
| 162 /** |
| 163 * Draw cache reads to the given canvas. |
| 164 * |
| 165 * It should consist of a horizontal bar with highlighted sections to |
| 166 * represent which parts of a file have been read from the cache. |
| 167 * |
| 168 * e.g. |xxxxxx----------x| |
| 169 */ |
| 170 drawCacheReadsToCanvas: function(canvas) { |
| 171 this.drawCacheEntriesToCanvas(this.read_, '#0a0', canvas); |
| 172 }, |
| 173 |
| 174 /** |
| 172 * Update this.details_ to contain everything we currently know about | 175 * Update this.details_ to contain everything we currently know about |
| 173 * this file. | 176 * this file. |
| 174 */ | 177 */ |
| 175 generateDetails: function() { | 178 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; | 179 this.details_.id = this.key; |
| 183 this.summaryText_.textContent = this.key || 'Unknown File'; | 180 this.summaryText_.textContent = this.key || 'Unknown File'; |
| 184 | 181 |
| 185 this.detailTable_.textContent = ''; | 182 this.detailTable_.textContent = ''; |
| 186 var header = document.createElement('thead'); | 183 var header = document.createElement('thead'); |
| 187 var footer = document.createElement('tfoot'); | 184 var footer = document.createElement('tfoot'); |
| 188 var body = document.createElement('tbody'); | 185 var body = document.createElement('tbody'); |
| 189 this.detailTable_.appendChild(header); | 186 this.detailTable_.appendChild(header); |
| 190 this.detailTable_.appendChild(footer); | 187 this.detailTable_.appendChild(footer); |
| 191 this.detailTable_.appendChild(body); | 188 this.detailTable_.appendChild(body); |
| 192 | 189 |
| 193 var headerRow = document.createElement('tr'); | 190 var headerRow = document.createElement('tr'); |
| 194 headerRow.appendChild(makeElement('th', 'Read From Cache')); | 191 headerRow.appendChild(media.makeElement('th', 'Read From Cache')); |
| 195 headerRow.appendChild(makeElement('th', 'Written To Cache')); | 192 headerRow.appendChild(media.makeElement('th', 'Written To Cache')); |
| 196 header.appendChild(headerRow); | 193 header.appendChild(headerRow); |
| 197 | 194 |
| 198 var footerRow = document.createElement('tr'); | 195 var footerRow = document.createElement('tr'); |
| 199 var footerCell = document.createElement('td'); | 196 var footerCell = document.createElement('td'); |
| 200 footerCell.textContent = 'Out of ' + (this.size || 'unkown size'); | 197 footerCell.textContent = 'Out of ' + (this.size || 'unkown size'); |
| 201 footerCell.setAttribute('colspan', 2); | 198 footerCell.setAttribute('colspan', 2); |
| 202 footerRow.appendChild(footerCell); | 199 footerRow.appendChild(footerCell); |
| 203 footer.appendChild(footerRow); | 200 footer.appendChild(footerRow); |
| 204 | 201 |
| 205 var read = this.read_.map(function(start, end) { | 202 var read = this.read_.map(function(start, end) { |
| 206 return start + ' - ' + end; | 203 return start + ' - ' + end; |
| 207 }); | 204 }); |
| 208 var written = this.written_.map(function(start, end) { | 205 var written = this.written_.map(function(start, end) { |
| 209 return start + ' - ' + end; | 206 return start + ' - ' + end; |
| 210 }); | 207 }); |
| 211 | 208 |
| 212 var length = Math.max(read.length, written.length); | 209 var length = Math.max(read.length, written.length); |
| 213 for (var i = 0; i < length; i++) { | 210 for (var i = 0; i < length; i++) { |
| 214 var row = document.createElement('tr'); | 211 var row = document.createElement('tr'); |
| 215 row.appendChild(makeElement('td', read[i] || '')); | 212 row.appendChild(media.makeElement('td', read[i] || '')); |
| 216 row.appendChild(makeElement('td', written[i] || '')); | 213 row.appendChild(media.makeElement('td', written[i] || '')); |
| 217 body.appendChild(row); | 214 body.appendChild(row); |
| 218 } | 215 } |
| 219 | 216 |
| 220 this.generateCanvas(); | 217 this.drawCacheWritesToCanvas(this.writeCanvas); |
| 218 this.drawCacheReadsToCanvas(this.readCanvas); |
| 221 }, | 219 }, |
| 222 | 220 |
| 223 /** | 221 /** |
| 224 * Render this CacheEntry as a <li>. | 222 * Render this CacheEntry as a <li>. |
| 225 * @return {HTMLElement} A <li> representing this CacheEntry. | 223 * @return {HTMLElement} A <li> representing this CacheEntry. |
| 226 */ | 224 */ |
| 227 toListItem: function() { | 225 toListItem: function() { |
| 228 this.generateDetails(); | 226 this.generateDetails(); |
| 229 | 227 |
| 230 var result = document.createElement('li'); | 228 var result = document.createElement('li'); |
| 231 result.appendChild(this.details_); | 229 result.appendChild(this.details_); |
| 232 return result; | 230 return result; |
| 233 } | 231 } |
| 234 }; | 232 }; |
| 235 | 233 |
| 236 return { | 234 return { |
| 237 CacheEntry: CacheEntry | 235 CacheEntry: CacheEntry |
| 238 }; | 236 }; |
| 239 }); | 237 }); |
| OLD | NEW |