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. | |
|
Evan Stade
2011/07/22 19:48:12
in pixels (I presume)
Scott Franklin
2011/07/22 22:27:20
Oh right.
| |
| 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 summary.appendChild(document.createElement('br')); | |
| 38 | |
| 39 // The canvas upon which to draw the cached sections of a file. | |
| 40 this.canvas_ = document.createElement('canvas'); | |
| 41 this.canvas_.width = CANVAS_WIDTH; | |
| 42 this.canvas_.height = CANVAS_HEIGHT; | |
| 43 summary.appendChild(this.canvas_); | |
| 44 | |
| 45 // A detailed writen description of the above. | |
|
scherkus (not reviewing)
2011/07/22 19:26:46
s/writen/written
Scott Franklin
2011/07/22 22:27:20
Done.
| |
| 46 this.detail_text_ = document.createElement('p'); | |
| 47 | |
| 48 // Some controls to modify this CacheEntry. | |
| 49 var controls = document.createElement('footer'); | |
| 50 | |
| 51 // A link to manually set the file size of this CacheEntry. | |
| 52 var set_size_control = document.createElement('a'); | |
| 53 set_size_control.href = 'javascript:void(0)'; | |
| 54 set_size_control.onclick = this.setSize.bind(this); | |
| 55 set_size_control.textContent = '(set size)'; | |
| 56 controls.appendChild(set_size_control); | |
| 57 | |
| 58 controls.appendChild(document.createTextNode(' - ')); | |
| 59 | |
| 60 // A link to clear recorded data from this CacheEntry. | |
| 61 var clear_control = document.createElement('a'); | |
| 62 clear_control.href = 'javascript:void(0)'; | |
| 63 clear_control.onclick = this.clear.bind(this); | |
| 64 clear_control.textContent = '(clear)'; | |
| 65 controls.appendChild(clear_control); | |
| 66 | |
| 67 this.details_.appendChild(summary); | |
| 68 this.details_.appendChild(this.detail_text_); | |
| 69 this.details_.appendChild(controls); | |
| 19 }; | 70 }; |
| 20 | 71 |
| 21 CacheEntry.prototype = { | 72 CacheEntry.prototype = { |
| 22 /** | 73 /** |
| 23 * Mark a range of bytes as read from the cache. | 74 * Mark a range of bytes as read from the cache. |
| 24 * @param {int} start The first byte read. | 75 * @param {int} start The first byte read. |
| 25 * @param {int} length The number of bytes read. | 76 * @param {int} length The number of bytes read. |
| 26 */ | 77 */ |
| 27 readBytes: function(start, length) { | 78 readBytes: function(start, length) { |
| 28 start = parseInt(start); | 79 start = parseInt(start); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 52 merge: function(other) { | 103 merge: function(other) { |
| 53 this.read_.merge(other.read_); | 104 this.read_.merge(other.read_); |
| 54 this.written_.merge(other.written_); | 105 this.written_.merge(other.written_); |
| 55 this.available_.merge(other.available_); | 106 this.available_.merge(other.available_); |
| 56 this.sparse = this.sparse || other.sparse; | 107 this.sparse = this.sparse || other.sparse; |
| 57 this.key = this.key || other.key; | 108 this.key = this.key || other.key; |
| 58 this.size = this.size || other.size; | 109 this.size = this.size || other.size; |
| 59 }, | 110 }, |
| 60 | 111 |
| 61 /** | 112 /** |
| 113 * Clear all recorded ranges from this CacheEntry and redraw this.details_. | |
| 114 */ | |
| 115 clear: function() { | |
| 116 this.read_ = new media.DisjointRangeSet; | |
| 117 this.written_ = new media.DisjointRangeSet; | |
| 118 this.available_ = new media.DisjointRangeSet; | |
| 119 this.generateDetails(); | |
| 120 }, | |
| 121 | |
| 122 /** | |
| 123 * Prompt the user for the size of the file, using the maximum seen size | |
| 124 * as the defualt. Also redraw this.details_. | |
| 125 * There is currently no way to reliably infer the size of a file that is | |
| 126 * already fully cached. This provides a (probably rarely used) way to set | |
| 127 * it manually. | |
| 128 */ | |
| 129 setSize: function() { | |
|
scherkus (not reviewing)
2011/07/22 19:26:46
are we better off prompting or simply documenting
Scott Franklin
2011/07/22 22:27:20
I used it quite a bit when debugging canvas issues
| |
| 130 size = prompt("file size:", Math.max(this.size, this.available_.max())); | |
| 131 if (size) | |
| 132 this.size = size; | |
| 133 this.generateDetails(); | |
| 134 }, | |
| 135 | |
| 136 /** | |
| 137 * Redraw this.canvas_. | |
|
Evan Stade
2011/07/22 19:48:12
maybe a little context about the desired outcome?
Scott Franklin
2011/07/22 22:27:20
Done.
| |
| 138 */ | |
| 139 generateCanvas: function() { | |
| 140 var context = this.canvas_.getContext('2d'); | |
| 141 context.textAlign = 'center'; | |
| 142 context.textBaseline = 'middle'; | |
| 143 | |
| 144 context.fillStyle = '#aaa'; | |
| 145 context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); | |
| 146 | |
| 147 if (this.size) { | |
| 148 var file_size = this.size; | |
| 149 drawRange = function(start, end, top) { | |
| 150 var left = start / file_size * CANVAS_WIDTH; | |
| 151 var right = end / file_size * CANVAS_WIDTH; | |
| 152 context.fillRect(left, top, right - left, top + CANVAS_HEIGHT / 2); | |
| 153 }; | |
| 154 | |
| 155 context.fillStyle = '#0a0'; | |
| 156 this.read_.map(function(start, end) { | |
| 157 drawRange(start, end, 0); | |
| 158 }); | |
| 159 | |
| 160 context.fillStyle = '#00a'; | |
| 161 this.written_.map(function(start, end) { | |
| 162 drawRange(start, end, CANVAS_HEIGHT / 2); | |
| 163 }); | |
| 164 | |
| 165 // Overlay a description of each bar. | |
| 166 context.fillStyle = '#fff'; | |
| 167 context.fillText('Read from cache.', CANVAS_WIDTH / 2, | |
| 168 CANVAS_HEIGHT / 4 - 0.5); | |
| 169 context.fillText('Written to cache.', CANVAS_WIDTH / 2, | |
| 170 CANVAS_HEIGHT * 3 / 4 + 0.5); | |
| 171 | |
| 172 // Add a 1px separator line. | |
| 173 context.moveTo(0, CANVAS_HEIGHT / 2); | |
| 174 context.lineTo(CANVAS_WIDTH, CANVAS_HEIGHT / 2); | |
| 175 context.strokeStyle = '#fff'; | |
| 176 context.stroke(); | |
| 177 } else { | |
| 178 // We can't draw bars if we don't know how big the file is. | |
| 179 context.fillStyle = '#fff'; | |
| 180 context.fillText('Unknown file size. See details below.', | |
| 181 CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2); | |
| 182 } | |
| 183 }, | |
| 184 | |
| 185 /** | |
| 186 * Update this.details_. | |
| 187 */ | |
| 188 generateDetails: function() { | |
| 189 this.details_.id = this.key; | |
| 190 | |
| 191 if (this.key) | |
| 192 this.summary_text_.textContent = this.key; | |
| 193 else | |
| 194 this.summary_text_.textContent = 'Unknown File'; | |
| 195 | |
| 196 this.detail_text_.innerHTML = '<strong>Read from cache:</strong><br />'; | |
|
Evan Stade
2011/07/22 19:48:12
I think you should use css instead of strong.
Thi
Scott Franklin
2011/07/22 22:27:20
The tables look good; I like it.
| |
| 197 this.detail_text_.innerHTML += this.read_.map(function(start, end) { | |
| 198 return start + '-' + end; | |
| 199 }).join('<br />'); | |
| 200 | |
| 201 this.detail_text_.innerHTML += '<br /><strong>Written to cache:</strong>'; | |
| 202 this.detail_text_.innerHTML += '<br />'; | |
| 203 this.detail_text_.innerHTML += this.written_.map(function(start, end) { | |
| 204 return start + '-' + end; | |
| 205 }).join('<br />'); | |
| 206 | |
| 207 this.detail_text_.innerHTML += '<br /><strong>Out of</strong> '; | |
| 208 if (this.size) | |
| 209 this.detail_text_.innerHTML += this.size; | |
|
Evan Stade
2011/07/22 19:48:12
I always prefer the ternary operator
Scott Franklin
2011/07/22 22:27:20
And plain old || is even better.
| |
| 210 else | |
| 211 this.detail_text_.innerHTML += 'unknown size'; | |
| 212 | |
| 213 this.generateCanvas(); | |
| 214 }, | |
| 215 | |
| 216 /** | |
| 62 * Render this CacheEntry as a <li>. | 217 * Render this CacheEntry as a <li>. |
| 63 * @return {HTMLElement} A <li> representing this CacheEntry. | 218 * @return {HTMLElement} A <li> representing this CacheEntry. |
| 64 */ | 219 */ |
| 65 toListItem: function() { | 220 toListItem: function() { |
| 221 this.generateDetails(); | |
| 222 | |
| 66 var result = document.createElement('li'); | 223 var result = document.createElement('li'); |
| 67 result.id = this.key; | 224 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; | 225 return result; |
| 85 } | 226 } |
| 86 }; | 227 }; |
| 87 | 228 |
| 88 return { | 229 return { |
| 89 CacheEntry: CacheEntry | 230 CacheEntry: CacheEntry |
| 90 }; | 231 }; |
| 91 }); | 232 }); |
| OLD | NEW |