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 |
| 48 this.details_.appendChild(summary); |
| 49 |
53 // The canvas upon which to draw the cached sections of a file. | 50 // The canvas upon which to draw the cached sections of a file. |
54 this.canvas_ = document.createElement('canvas'); | 51 this.canvas = document.createElement('canvas'); |
55 this.canvas_.width = CANVAS_WIDTH; | 52 this.canvas.width = media.BAR_WIDTH; |
56 this.canvas_.height = CANVAS_HEIGHT; | 53 this.canvas.height = 2 * media.BAR_HEIGHT + 1; |
57 summary.appendChild(this.canvas_); | 54 this.details_.appendChild(this.canvas); |
58 | |
59 this.details_.appendChild(summary); | |
60 | 55 |
61 // A tabular representation of the data in the above canvas. | 56 // A tabular representation of the data in the above canvas. |
62 this.detailTable_ = document.createElement('table'); | 57 this.detailTable_ = document.createElement('table'); |
63 this.detailTable_.className = 'cache-table'; | 58 this.detailTable_.className = 'cache-table'; |
64 this.details_.appendChild(this.detailTable_); | 59 this.details_.appendChild(this.detailTable_); |
65 }; | 60 }; |
66 | 61 |
67 CacheEntry.prototype = { | 62 CacheEntry.prototype = { |
68 /** | 63 /** |
69 * Mark a range of bytes as read from the cache. | 64 * 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_. | 103 * Clear all recorded ranges from this CacheEntry and redraw this.details_. |
109 */ | 104 */ |
110 clear: function() { | 105 clear: function() { |
111 this.read_ = new media.DisjointRangeSet; | 106 this.read_ = new media.DisjointRangeSet; |
112 this.written_ = new media.DisjointRangeSet; | 107 this.written_ = new media.DisjointRangeSet; |
113 this.available_ = new media.DisjointRangeSet; | 108 this.available_ = new media.DisjointRangeSet; |
114 this.generateDetails(); | 109 this.generateDetails(); |
115 }, | 110 }, |
116 | 111 |
117 /** | 112 /** |
118 * Redraw this.canvas_. | 113 * Redraw this.canvas. |
119 * It should consist of two horizontal bars with highlighted sections to | 114 * 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 | 115 * represent which parts of a file have been read from (top) and written to |
121 * (bottom) the cache. | 116 * (bottom) the cache. |
122 * e.g. |xxxxxx----------x| | 117 * e.g. |xxxxxx----------x| |
123 * |-----xxxxx-------| | 118 * |-----xxxxx-------| |
124 */ | 119 */ |
125 generateCanvas: function() { | 120 generateCanvas: function() { |
126 var context = this.canvas_.getContext('2d'); | 121 var width = this.canvas.width; |
| 122 var height = this.canvas.height; |
| 123 var context = this.canvas.getContext('2d'); |
127 context.textAlign = 'center'; | 124 context.textAlign = 'center'; |
128 context.textBaseline = 'middle'; | 125 context.textBaseline = 'middle'; |
129 | 126 |
130 context.fillStyle = '#aaa'; | 127 context.fillStyle = '#aaa'; |
131 context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); | 128 context.fillRect(0, 0, width, height); |
132 | 129 |
133 if (this.size) { | 130 if (this.size) { |
134 var fileSize = this.size; | 131 var fileSize = this.size; |
135 drawRange = function(start, end, top) { | 132 var drawRange = function(start, end, top) { |
136 var left = start / fileSize * CANVAS_WIDTH; | 133 var left = start / fileSize * width; |
137 var right = end / fileSize * CANVAS_WIDTH; | 134 var right = end / fileSize * width; |
138 context.fillRect(left, top, right - left, top + CANVAS_HEIGHT / 2); | 135 context.fillRect(left, top, right - left, top + height / 2); |
139 }; | 136 }; |
140 | 137 |
141 context.fillStyle = '#0a0'; | 138 context.fillStyle = '#0a0'; |
142 this.read_.map(function(start, end) { | 139 this.read_.map(function(start, end) { |
143 drawRange(start, end, 0); | 140 drawRange(start, end, 0); |
144 }); | 141 }); |
145 | 142 |
146 context.fillStyle = '#00a'; | 143 context.fillStyle = '#00a'; |
147 this.written_.map(function(start, end) { | 144 this.written_.map(function(start, end) { |
148 drawRange(start, end, CANVAS_HEIGHT / 2); | 145 drawRange(start, end, height / 2); |
149 }); | 146 }); |
150 | 147 |
151 // Overlay a description of each bar. | 148 // Overlay a description of each bar. |
152 context.fillStyle = '#fff'; | 149 context.fillStyle = '#fff'; |
153 context.fillText('Read from cache.', CANVAS_WIDTH / 2, | 150 context.fillText('Read from cache.', width / 2, |
154 CANVAS_HEIGHT / 4 - 0.5); | 151 height / 4 - 0.5); |
155 context.fillText('Written to cache.', CANVAS_WIDTH / 2, | 152 context.fillText('Written to cache.', width / 2, |
156 CANVAS_HEIGHT * 3 / 4 + 0.5); | 153 height * 3 / 4 + 0.5); |
157 | 154 |
158 // Add a 1px separator line. | 155 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 { | 156 } else { |
164 // We can't draw bars if we don't know how big the file is. | 157 // We can't draw bars if we don't know how big the file is. |
165 context.fillStyle = '#fff'; | 158 context.fillStyle = '#fff'; |
166 context.fillText('Unknown file size. See details below.', | 159 context.fillText('Unknown file size. See details below.', |
167 CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2); | 160 width / 2, height / 2); |
168 } | 161 } |
169 }, | 162 }, |
170 | 163 |
171 /** | 164 /** |
172 * Update this.details_ to contain everything we currently know about | 165 * Update this.details_ to contain everything we currently know about |
173 * this file. | 166 * this file. |
174 */ | 167 */ |
175 generateDetails: function() { | 168 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; | 169 this.details_.id = this.key; |
183 this.summaryText_.textContent = this.key || 'Unknown File'; | 170 this.summaryText_.textContent = media.clipURL(this.key) || 'Unknown File'; |
184 | 171 |
185 this.detailTable_.textContent = ''; | 172 this.detailTable_.textContent = ''; |
186 var header = document.createElement('thead'); | 173 var header = document.createElement('thead'); |
187 var footer = document.createElement('tfoot'); | 174 var footer = document.createElement('tfoot'); |
188 var body = document.createElement('tbody'); | 175 var body = document.createElement('tbody'); |
189 this.detailTable_.appendChild(header); | 176 this.detailTable_.appendChild(header); |
190 this.detailTable_.appendChild(footer); | 177 this.detailTable_.appendChild(footer); |
191 this.detailTable_.appendChild(body); | 178 this.detailTable_.appendChild(body); |
192 | 179 |
193 var headerRow = document.createElement('tr'); | 180 var headerRow = document.createElement('tr'); |
194 headerRow.appendChild(makeElement('th', 'Read From Cache')); | 181 headerRow.appendChild(media.makeElement('th', 'Read From Cache')); |
195 headerRow.appendChild(makeElement('th', 'Written To Cache')); | 182 headerRow.appendChild(media.makeElement('th', 'Written To Cache')); |
196 header.appendChild(headerRow); | 183 header.appendChild(headerRow); |
197 | 184 |
198 var footerRow = document.createElement('tr'); | 185 var footerRow = document.createElement('tr'); |
199 var footerCell = document.createElement('td'); | 186 var footerCell = document.createElement('td'); |
200 footerCell.textContent = 'Out of ' + (this.size || 'unkown size'); | 187 footerCell.textContent = 'Out of ' + (this.size || 'unkown size'); |
201 footerCell.setAttribute('colspan', 2); | 188 footerCell.setAttribute('colspan', 2); |
202 footerRow.appendChild(footerCell); | 189 footerRow.appendChild(footerCell); |
203 footer.appendChild(footerRow); | 190 footer.appendChild(footerRow); |
204 | 191 |
205 var read = this.read_.map(function(start, end) { | 192 var read = this.read_.map(function(start, end) { |
206 return start + ' - ' + end; | 193 return start + ' - ' + end; |
207 }); | 194 }); |
208 var written = this.written_.map(function(start, end) { | 195 var written = this.written_.map(function(start, end) { |
209 return start + ' - ' + end; | 196 return start + ' - ' + end; |
210 }); | 197 }); |
211 | 198 |
212 var length = Math.max(read.length, written.length); | 199 var length = Math.max(read.length, written.length); |
213 for (var i = 0; i < length; i++) { | 200 for (var i = 0; i < length; i++) { |
214 var row = document.createElement('tr'); | 201 var row = document.createElement('tr'); |
215 row.appendChild(makeElement('td', read[i] || '')); | 202 row.appendChild(media.makeElement('td', read[i] || '')); |
216 row.appendChild(makeElement('td', written[i] || '')); | 203 row.appendChild(media.makeElement('td', written[i] || '')); |
217 body.appendChild(row); | 204 body.appendChild(row); |
218 } | 205 } |
219 | 206 |
220 this.generateCanvas(); | 207 this.generateCanvas(); |
221 }, | 208 }, |
222 | 209 |
223 /** | 210 /** |
224 * Render this CacheEntry as a <li>. | 211 * Render this CacheEntry as a <li>. |
225 * @return {HTMLElement} A <li> representing this CacheEntry. | 212 * @return {HTMLElement} A <li> representing this CacheEntry. |
226 */ | 213 */ |
227 toListItem: function() { | 214 toListItem: function() { |
228 this.generateDetails(); | 215 this.generateDetails(); |
229 | 216 |
230 var result = document.createElement('li'); | 217 var result = document.createElement('li'); |
231 result.appendChild(this.details_); | 218 result.appendChild(this.details_); |
232 return result; | 219 return result; |
233 } | 220 } |
234 }; | 221 }; |
235 | 222 |
236 return { | 223 return { |
237 CacheEntry: CacheEntry | 224 CacheEntry: CacheEntry |
238 }; | 225 }; |
239 }); | 226 }); |
OLD | NEW |