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. |
22 this.sparse = false; | 17 this.sparse = false; |
23 this.key = null; | 18 this.key = null; |
24 this.size = null; | 19 this.size = null; |
25 | 20 |
26 // The <details> element representing this CacheEntry. | 21 // The <details> element representing this CacheEntry. |
27 this.details_ = document.createElement('details'); | 22 this.details_ = document.createElement('details'); |
28 this.details_.className = 'cache-entry'; | 23 this.details_.className = 'cache-entry'; |
29 this.details_.open = false; | 24 this.details_.open = false; |
30 | 25 |
31 // The <details> summary line. It contains a chart of requested file ranges | 26 // The <details> summary line. It contains a chart of requested file ranges |
32 // and the url if we know it. | 27 // and the url if we know it. |
33 var summary = document.createElement('summary'); | 28 var summary = document.createElement('summary'); |
29 summary.className = 'ellipsis'; | |
arv (Not doing code reviews)
2011/09/23 15:59:01
class name should not describe the rendering ut th
scherkus (not reviewing)
2011/09/23 17:43:23
I ditched the entire ellipsis thing instead showin
| |
34 | 30 |
35 this.summaryText_ = document.createTextNode(''); | 31 this.summaryText_ = document.createTextNode(''); |
36 summary.appendChild(this.summaryText_); | 32 summary.appendChild(this.summaryText_); |
37 | 33 |
38 summary.appendChild(document.createTextNode(' ')); | 34 summary.appendChild(document.createTextNode(' ')); |
39 | 35 |
40 // Controls to modify this CacheEntry. | 36 // Controls to modify this CacheEntry. |
41 var controls = document.createElement('span'); | 37 var controls = document.createElement('span'); |
42 controls.className = 'cache-entry-controls'; | 38 controls.className = 'cache-entry-controls'; |
43 summary.appendChild(controls); | 39 summary.appendChild(controls); |
44 summary.appendChild(document.createElement('br')); | 40 summary.appendChild(document.createElement('br')); |
45 | 41 |
46 // A link to clear recorded data from this CacheEntry. | 42 // A link to clear recorded data from this CacheEntry. |
47 var clearControl = document.createElement('a'); | 43 var clearControl = document.createElement('a'); |
48 clearControl.href = 'javascript:void(0)'; | 44 clearControl.href = 'javascript:void(0)'; |
49 clearControl.onclick = this.clear.bind(this); | 45 clearControl.onclick = this.clear.bind(this); |
50 clearControl.textContent = '(clear entry)'; | 46 clearControl.textContent = '(clear entry)'; |
51 controls.appendChild(clearControl); | 47 controls.appendChild(clearControl); |
52 | 48 |
49 this.details_.appendChild(summary); | |
50 | |
53 // The canvas upon which to draw the cached sections of a file. | 51 // The canvas upon which to draw the cached sections of a file. |
54 this.canvas_ = document.createElement('canvas'); | 52 this.canvas = document.createElement('canvas'); |
55 this.canvas_.width = CANVAS_WIDTH; | 53 this.canvas.width = media.BAR_WIDTH; |
56 this.canvas_.height = CANVAS_HEIGHT; | 54 this.canvas.height = 2 * media.BAR_HEIGHT + 1; |
57 summary.appendChild(this.canvas_); | 55 this.details_.appendChild(this.canvas); |
58 | |
59 this.details_.appendChild(summary); | |
60 | 56 |
61 // A tabular representation of the data in the above canvas. | 57 // A tabular representation of the data in the above canvas. |
62 this.detailTable_ = document.createElement('table'); | 58 this.detailTable_ = document.createElement('table'); |
63 this.detailTable_.className = 'cache-table'; | 59 this.detailTable_.className = 'cache-table'; |
64 this.details_.appendChild(this.detailTable_); | 60 this.details_.appendChild(this.detailTable_); |
65 }; | 61 }; |
66 | 62 |
67 CacheEntry.prototype = { | 63 CacheEntry.prototype = { |
68 /** | 64 /** |
69 * Mark a range of bytes as read from the cache. | 65 * Mark a range of bytes as read from the cache. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 * Clear all recorded ranges from this CacheEntry and redraw this.details_. | 104 * Clear all recorded ranges from this CacheEntry and redraw this.details_. |
109 */ | 105 */ |
110 clear: function() { | 106 clear: function() { |
111 this.read_ = new media.DisjointRangeSet; | 107 this.read_ = new media.DisjointRangeSet; |
112 this.written_ = new media.DisjointRangeSet; | 108 this.written_ = new media.DisjointRangeSet; |
113 this.available_ = new media.DisjointRangeSet; | 109 this.available_ = new media.DisjointRangeSet; |
114 this.generateDetails(); | 110 this.generateDetails(); |
115 }, | 111 }, |
116 | 112 |
117 /** | 113 /** |
118 * Redraw this.canvas_. | 114 * Redraw this.canvas. |
119 * It should consist of two horizontal bars with highlighted sections to | 115 * 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 | 116 * represent which parts of a file have been read from (top) and written to |
121 * (bottom) the cache. | 117 * (bottom) the cache. |
122 * e.g. |xxxxxx----------x| | 118 * e.g. |xxxxxx----------x| |
123 * |-----xxxxx-------| | 119 * |-----xxxxx-------| |
124 */ | 120 */ |
125 generateCanvas: function() { | 121 generateCanvas: function() { |
126 var context = this.canvas_.getContext('2d'); | 122 var width = this.canvas.width; |
123 var height = this.canvas.height; | |
124 var context = this.canvas.getContext('2d'); | |
127 context.textAlign = 'center'; | 125 context.textAlign = 'center'; |
128 context.textBaseline = 'middle'; | 126 context.textBaseline = 'middle'; |
129 | 127 |
130 context.fillStyle = '#aaa'; | 128 context.fillStyle = '#aaa'; |
131 context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); | 129 context.fillRect(0, 0, width, height); |
132 | 130 |
133 if (this.size) { | 131 if (this.size) { |
134 var fileSize = this.size; | 132 var fileSize = this.size; |
135 drawRange = function(start, end, top) { | 133 var drawRange = function(start, end, top) { |
136 var left = start / fileSize * CANVAS_WIDTH; | 134 var left = start / fileSize * width; |
137 var right = end / fileSize * CANVAS_WIDTH; | 135 var right = end / fileSize * width; |
138 context.fillRect(left, top, right - left, top + CANVAS_HEIGHT / 2); | 136 context.fillRect(left, top, right - left, top + height / 2); |
139 }; | 137 }; |
140 | 138 |
141 context.fillStyle = '#0a0'; | 139 context.fillStyle = '#0a0'; |
142 this.read_.map(function(start, end) { | 140 this.read_.map(function(start, end) { |
143 drawRange(start, end, 0); | 141 drawRange(start, end, 0); |
144 }); | 142 }); |
145 | 143 |
146 context.fillStyle = '#00a'; | 144 context.fillStyle = '#00a'; |
147 this.written_.map(function(start, end) { | 145 this.written_.map(function(start, end) { |
148 drawRange(start, end, CANVAS_HEIGHT / 2); | 146 drawRange(start, end, height / 2); |
149 }); | 147 }); |
150 | 148 |
151 // Overlay a description of each bar. | 149 // Overlay a description of each bar. |
152 context.fillStyle = '#fff'; | 150 context.fillStyle = '#fff'; |
153 context.fillText('Read from cache.', CANVAS_WIDTH / 2, | 151 context.fillText('Read from cache.', width / 2, |
154 CANVAS_HEIGHT / 4 - 0.5); | 152 height / 4 - 0.5); |
155 context.fillText('Written to cache.', CANVAS_WIDTH / 2, | 153 context.fillText('Written to cache.', width / 2, |
156 CANVAS_HEIGHT * 3 / 4 + 0.5); | 154 height * 3 / 4 + 0.5); |
157 | 155 |
158 // Add a 1px separator line. | 156 media.drawLine(context, height / 2); |
159 context.moveTo(0, CANVAS_HEIGHT / 2); | |
160 context.lineTo(CANVAS_WIDTH, CANVAS_HEIGHT / 2); | |
161 context.strokeStyle = '#fff'; | |
162 context.stroke(); | |
163 } else { | 157 } else { |
164 // We can't draw bars if we don't know how big the file is. | 158 // We can't draw bars if we don't know how big the file is. |
165 context.fillStyle = '#fff'; | 159 context.fillStyle = '#fff'; |
166 context.fillText('Unknown file size. See details below.', | 160 context.fillText('Unknown file size. See details below.', |
167 CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2); | 161 width / 2, height / 2); |
168 } | 162 } |
169 }, | 163 }, |
170 | 164 |
171 /** | 165 /** |
172 * Update this.details_ to contain everything we currently know about | 166 * Update this.details_ to contain everything we currently know about |
173 * this file. | 167 * this file. |
174 */ | 168 */ |
175 generateDetails: function() { | 169 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; | 170 this.details_.id = this.key; |
183 this.summaryText_.textContent = this.key || 'Unknown File'; | 171 this.summaryText_.textContent = this.key || 'Unknown File'; |
184 | 172 |
185 this.detailTable_.textContent = ''; | 173 this.detailTable_.textContent = ''; |
186 var header = document.createElement('thead'); | 174 var header = document.createElement('thead'); |
187 var footer = document.createElement('tfoot'); | 175 var footer = document.createElement('tfoot'); |
188 var body = document.createElement('tbody'); | 176 var body = document.createElement('tbody'); |
189 this.detailTable_.appendChild(header); | 177 this.detailTable_.appendChild(header); |
190 this.detailTable_.appendChild(footer); | 178 this.detailTable_.appendChild(footer); |
191 this.detailTable_.appendChild(body); | 179 this.detailTable_.appendChild(body); |
192 | 180 |
193 var headerRow = document.createElement('tr'); | 181 var headerRow = document.createElement('tr'); |
194 headerRow.appendChild(makeElement('th', 'Read From Cache')); | 182 headerRow.appendChild(media.makeElement('th', 'Read From Cache')); |
195 headerRow.appendChild(makeElement('th', 'Written To Cache')); | 183 headerRow.appendChild(media.makeElement('th', 'Written To Cache')); |
196 header.appendChild(headerRow); | 184 header.appendChild(headerRow); |
197 | 185 |
198 var footerRow = document.createElement('tr'); | 186 var footerRow = document.createElement('tr'); |
199 var footerCell = document.createElement('td'); | 187 var footerCell = document.createElement('td'); |
200 footerCell.textContent = 'Out of ' + (this.size || 'unkown size'); | 188 footerCell.textContent = 'Out of ' + (this.size || 'unkown size'); |
201 footerCell.setAttribute('colspan', 2); | 189 footerCell.setAttribute('colspan', 2); |
202 footerRow.appendChild(footerCell); | 190 footerRow.appendChild(footerCell); |
203 footer.appendChild(footerRow); | 191 footer.appendChild(footerRow); |
204 | 192 |
205 var read = this.read_.map(function(start, end) { | 193 var read = this.read_.map(function(start, end) { |
206 return start + ' - ' + end; | 194 return start + ' - ' + end; |
207 }); | 195 }); |
208 var written = this.written_.map(function(start, end) { | 196 var written = this.written_.map(function(start, end) { |
209 return start + ' - ' + end; | 197 return start + ' - ' + end; |
210 }); | 198 }); |
211 | 199 |
212 var length = Math.max(read.length, written.length); | 200 var length = Math.max(read.length, written.length); |
213 for (var i = 0; i < length; i++) { | 201 for (var i = 0; i < length; i++) { |
214 var row = document.createElement('tr'); | 202 var row = document.createElement('tr'); |
215 row.appendChild(makeElement('td', read[i] || '')); | 203 row.appendChild(media.makeElement('td', read[i] || '')); |
216 row.appendChild(makeElement('td', written[i] || '')); | 204 row.appendChild(media.makeElement('td', written[i] || '')); |
217 body.appendChild(row); | 205 body.appendChild(row); |
218 } | 206 } |
219 | 207 |
220 this.generateCanvas(); | 208 this.generateCanvas(); |
221 }, | 209 }, |
222 | 210 |
223 /** | 211 /** |
224 * Render this CacheEntry as a <li>. | 212 * Render this CacheEntry as a <li>. |
225 * @return {HTMLElement} A <li> representing this CacheEntry. | 213 * @return {HTMLElement} A <li> representing this CacheEntry. |
226 */ | 214 */ |
227 toListItem: function() { | 215 toListItem: function() { |
228 this.generateDetails(); | 216 this.generateDetails(); |
229 | 217 |
230 var result = document.createElement('li'); | 218 var result = document.createElement('li'); |
231 result.appendChild(this.details_); | 219 result.appendChild(this.details_); |
232 return result; | 220 return result; |
233 } | 221 } |
234 }; | 222 }; |
235 | 223 |
236 return { | 224 return { |
237 CacheEntry: CacheEntry | 225 CacheEntry: CacheEntry |
238 }; | 226 }; |
239 }); | 227 }); |
OLD | NEW |