Chromium Code Reviews| Index: chrome/browser/resources/media_internals/cache_entry.js |
| diff --git a/chrome/browser/resources/media_internals/cache_entry.js b/chrome/browser/resources/media_internals/cache_entry.js |
| index 60deab4cfe29a91628d454fd9895f01746a647e6..9893347f27be3737b8009ce3ba39453cc21aa763 100644 |
| --- a/chrome/browser/resources/media_internals/cache_entry.js |
| +++ b/chrome/browser/resources/media_internals/cache_entry.js |
| @@ -3,12 +3,7 @@ |
| // found in the LICENSE file. |
| cr.define('media', function() { |
| - |
| - /** |
| - * The width and height of a CacheEntry canvas in pixels. |
| - */ |
| - var CANVAS_WIDTH = 500; |
| - var CANVAS_HEIGHT = 31; |
| + 'use strict'; |
| /** |
| * This class represents a file cached by net. |
| @@ -50,19 +45,25 @@ cr.define('media', function() { |
| clearControl.textContent = '(clear entry)'; |
| controls.appendChild(clearControl); |
| - // The canvas upon which to draw the cached sections of a file. |
| - this.canvas_ = document.createElement('canvas'); |
| - this.canvas_.width = CANVAS_WIDTH; |
| - this.canvas_.height = CANVAS_HEIGHT; |
| - summary.appendChild(this.canvas_); |
| - |
| this.details_.appendChild(summary); |
| + // The canvas for drawing cache writes. |
| + this.writeCanvas = document.createElement('canvas'); |
| + this.writeCanvas.width = media.BAR_WIDTH; |
| + this.writeCanvas.height = media.BAR_HEIGHT; |
| + this.details_.appendChild(this.writeCanvas); |
| + |
| + // The canvas for drawing cache reads. |
| + this.readCanvas = document.createElement('canvas'); |
| + this.readCanvas.width = media.BAR_WIDTH; |
| + this.readCanvas.height = media.BAR_HEIGHT; |
| + this.details_.appendChild(this.readCanvas); |
| + |
| // A tabular representation of the data in the above canvas. |
| this.detailTable_ = document.createElement('table'); |
| this.detailTable_.className = 'cache-table'; |
| this.details_.appendChild(this.detailTable_); |
| - }; |
| + } |
| CacheEntry.prototype = { |
| /** |
| @@ -115,57 +116,71 @@ cr.define('media', function() { |
| }, |
| /** |
| - * Redraw this.canvas_. |
| - * It should consist of two horizontal bars with highlighted sections to |
| - * represent which parts of a file have been read from (top) and written to |
| - * (bottom) the cache. |
| + * Draw cache writes to the given canvas. |
| + * |
| + * It should consist of a horizontal bar with highlighted sections to |
| + * represent which parts of a file have been written to the cache. |
| + * |
| * e.g. |xxxxxx----------x| |
| - * |-----xxxxx-------| |
| */ |
| - generateCanvas: function() { |
| - var context = this.canvas_.getContext('2d'); |
| - context.textAlign = 'center'; |
| - context.textBaseline = 'middle'; |
| + drawCacheWritesToCanvas: function(canvas) { |
| + // Don't bother drawing anything if we don't know the total size. |
| + if (!this.size) { |
| + return; |
| + } |
| + |
| + var width = canvas.width; |
| + var height = canvas.height; |
| + var context = canvas.getContext('2d'); |
| + var fileSize = this.size; |
| context.fillStyle = '#aaa'; |
| - context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); |
| - |
| - if (this.size) { |
| - var fileSize = this.size; |
| - drawRange = function(start, end, top) { |
| - var left = start / fileSize * CANVAS_WIDTH; |
| - var right = end / fileSize * CANVAS_WIDTH; |
| - context.fillRect(left, top, right - left, top + CANVAS_HEIGHT / 2); |
| - }; |
| - |
| - context.fillStyle = '#0a0'; |
| - this.read_.map(function(start, end) { |
| - drawRange(start, end, 0); |
| - }); |
| - |
| - context.fillStyle = '#00a'; |
| - this.written_.map(function(start, end) { |
| - drawRange(start, end, CANVAS_HEIGHT / 2); |
| - }); |
| - |
| - // Overlay a description of each bar. |
| - context.fillStyle = '#fff'; |
| - context.fillText('Read from cache.', CANVAS_WIDTH / 2, |
| - CANVAS_HEIGHT / 4 - 0.5); |
| - context.fillText('Written to cache.', CANVAS_WIDTH / 2, |
| - CANVAS_HEIGHT * 3 / 4 + 0.5); |
| - |
| - // Add a 1px separator line. |
| - context.moveTo(0, CANVAS_HEIGHT / 2); |
| - context.lineTo(CANVAS_WIDTH, CANVAS_HEIGHT / 2); |
| - context.strokeStyle = '#fff'; |
| - context.stroke(); |
| - } else { |
| - // We can't draw bars if we don't know how big the file is. |
| - context.fillStyle = '#fff'; |
| - context.fillText('Unknown file size. See details below.', |
| - CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2); |
| + context.fillRect(0, 0, width, height); |
| + |
| + 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.
|
| + var left = start / fileSize * width; |
| + var right = end / fileSize * width; |
| + context.fillRect(left, 0, right - left, height); |
| + }; |
| + |
| + context.fillStyle = '#00a'; |
| + this.written_.map(function(start, end) { |
| + drawRange(start, end); |
| + }); |
| + }, |
| + |
| + /** |
| + * Draw cache reads to the given canvas. |
| + * |
| + * It should consist of a horizontal bar with highlighted sections to |
| + * represent which parts of a file have been read from the cache. |
| + * |
| + * e.g. |xxxxxx----------x| |
| + */ |
| + 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
|
| + // Don't bother drawing anything if we don't know the total size. |
| + if (!this.size) { |
| + return; |
| } |
| + |
| + var width = canvas.width; |
| + var height = canvas.height; |
| + var context = canvas.getContext('2d'); |
| + var fileSize = this.size; |
| + |
| + context.fillStyle = '#aaa'; |
| + context.fillRect(0, 0, width, height); |
| + |
| + var drawRange = function(start, end) { |
| + var left = start / fileSize * width; |
| + var right = end / fileSize * width; |
| + context.fillRect(left, 0, right - left, height); |
| + }; |
| + |
| + context.fillStyle = '#0a0'; |
| + this.read_.map(function(start, end) { |
| + drawRange(start, end); |
| + }); |
| }, |
| /** |
| @@ -173,12 +188,6 @@ cr.define('media', function() { |
| * this file. |
| */ |
| generateDetails: function() { |
| - function makeElement(type, content) { |
| - var element = document.createElement(type); |
| - element.textContent = content; |
| - return element; |
| - }; |
| - |
| this.details_.id = this.key; |
| this.summaryText_.textContent = this.key || 'Unknown File'; |
| @@ -191,8 +200,8 @@ cr.define('media', function() { |
| this.detailTable_.appendChild(body); |
| var headerRow = document.createElement('tr'); |
| - headerRow.appendChild(makeElement('th', 'Read From Cache')); |
| - headerRow.appendChild(makeElement('th', 'Written To Cache')); |
| + headerRow.appendChild(media.makeElement('th', 'Read From Cache')); |
| + headerRow.appendChild(media.makeElement('th', 'Written To Cache')); |
| header.appendChild(headerRow); |
| var footerRow = document.createElement('tr'); |
| @@ -212,12 +221,13 @@ cr.define('media', function() { |
| var length = Math.max(read.length, written.length); |
| for (var i = 0; i < length; i++) { |
| var row = document.createElement('tr'); |
| - row.appendChild(makeElement('td', read[i] || '')); |
| - row.appendChild(makeElement('td', written[i] || '')); |
| + row.appendChild(media.makeElement('td', read[i] || '')); |
| + row.appendChild(media.makeElement('td', written[i] || '')); |
| body.appendChild(row); |
| } |
| - this.generateCanvas(); |
| + this.drawCacheWritesToCanvas(this.writeCanvas); |
| + this.drawCacheReadsToCanvas(this.readCanvas); |
| }, |
| /** |