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 in pixels. | |
9 */ | |
10 var CANVAS_WIDTH = 500; | |
11 var CANVAS_HEIGHT = 31; | |
12 | |
13 /** | |
14 * This class represents a file cached by net. | 8 * This class represents a file cached by net. |
15 */ | 9 */ |
16 function CacheEntry() { | 10 function CacheEntry() { |
17 this.read_ = new media.DisjointRangeSet; | 11 this.read_ = new media.DisjointRangeSet; |
18 this.written_ = new media.DisjointRangeSet; | 12 this.written_ = new media.DisjointRangeSet; |
19 this.available_ = new media.DisjointRangeSet; | 13 this.available_ = new media.DisjointRangeSet; |
20 | 14 |
21 // Set to true when we know the entry is sparse. | 15 // Set to true when we know the entry is sparse. |
22 this.sparse = false; | 16 this.sparse = false; |
23 this.key = null; | 17 this.key = null; |
(...skipping 14 matching lines...) Expand all Loading... | |
38 summary.appendChild(document.createTextNode(' ')); | 32 summary.appendChild(document.createTextNode(' ')); |
39 | 33 |
40 // Controls to modify this CacheEntry. | 34 // Controls to modify this CacheEntry. |
41 var controls = document.createElement('span'); | 35 var controls = document.createElement('span'); |
42 controls.className = 'cache-entry-controls'; | 36 controls.className = 'cache-entry-controls'; |
43 summary.appendChild(controls); | 37 summary.appendChild(controls); |
44 summary.appendChild(document.createElement('br')); | 38 summary.appendChild(document.createElement('br')); |
45 | 39 |
46 // A link to clear recorded data from this CacheEntry. | 40 // A link to clear recorded data from this CacheEntry. |
47 var clearControl = document.createElement('a'); | 41 var clearControl = document.createElement('a'); |
48 clearControl.href = 'javascript:void(0)'; | 42 clearControl.href = '#'; |
arv (Not doing code reviews)
2011/08/16 19:29:03
this change is not for the better. Now we get a hi
Scott Franklin
2011/08/16 23:33:52
It did work fine before, but Content Security Poli
| |
49 clearControl.onclick = this.clear.bind(this); | 43 clearControl.onclick = this.clear.bind(this); |
50 clearControl.textContent = '(clear entry)'; | 44 clearControl.textContent = '(clear entry)'; |
51 controls.appendChild(clearControl); | 45 controls.appendChild(clearControl); |
52 | 46 |
47 this.details_.appendChild(summary); | |
48 | |
53 // The canvas upon which to draw the cached sections of a file. | 49 // The canvas upon which to draw the cached sections of a file. |
54 this.canvas_ = document.createElement('canvas'); | 50 this.canvas = document.createElement('canvas'); |
55 this.canvas_.width = CANVAS_WIDTH; | 51 this.canvas.width = media.BAR_WIDTH; |
56 this.canvas_.height = CANVAS_HEIGHT; | 52 this.canvas.height = 2 * media.BAR_HEIGHT + 1; |
57 summary.appendChild(this.canvas_); | 53 this.details_.appendChild(this.canvas); |
58 | |
59 this.details_.appendChild(summary); | |
60 | 54 |
61 // A tabular representation of the data in the above canvas. | 55 // A tabular representation of the data in the above canvas. |
62 this.detailTable_ = document.createElement('table'); | 56 this.detailTable_ = document.createElement('table'); |
63 this.detailTable_.className = 'cache-table'; | 57 this.detailTable_.className = 'cache-table'; |
64 this.details_.appendChild(this.detailTable_); | 58 this.details_.appendChild(this.detailTable_); |
65 }; | 59 }; |
66 | 60 |
67 CacheEntry.prototype = { | 61 CacheEntry.prototype = { |
68 /** | 62 /** |
69 * Mark a range of bytes as read from the cache. | 63 * 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_. | 102 * Clear all recorded ranges from this CacheEntry and redraw this.details_. |
109 */ | 103 */ |
110 clear: function() { | 104 clear: function() { |
111 this.read_ = new media.DisjointRangeSet; | 105 this.read_ = new media.DisjointRangeSet; |
112 this.written_ = new media.DisjointRangeSet; | 106 this.written_ = new media.DisjointRangeSet; |
113 this.available_ = new media.DisjointRangeSet; | 107 this.available_ = new media.DisjointRangeSet; |
114 this.generateDetails(); | 108 this.generateDetails(); |
115 }, | 109 }, |
116 | 110 |
117 /** | 111 /** |
118 * Redraw this.canvas_. | 112 * Redraw this.canvas. |
119 * It should consist of two horizontal bars with highlighted sections to | 113 * 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 | 114 * represent which parts of a file have been read from (top) and written to |
121 * (bottom) the cache. | 115 * (bottom) the cache. |
122 * e.g. |xxxxxx----------x| | 116 * e.g. |xxxxxx----------x| |
123 * |-----xxxxx-------| | 117 * |-----xxxxx-------| |
124 */ | 118 */ |
125 generateCanvas: function() { | 119 generateCanvas: function() { |
126 var context = this.canvas_.getContext('2d'); | 120 var width = this.canvas.width; |
121 var height = this.canvas.height; | |
122 var context = this.canvas.getContext('2d'); | |
127 context.textAlign = 'center'; | 123 context.textAlign = 'center'; |
128 context.textBaseline = 'middle'; | 124 context.textBaseline = 'middle'; |
129 | 125 |
130 context.fillStyle = '#aaa'; | 126 context.fillStyle = '#aaa'; |
131 context.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); | 127 context.fillRect(0, 0, width, height); |
132 | 128 |
133 if (this.size) { | 129 if (this.size) { |
134 var fileSize = this.size; | 130 var fileSize = this.size; |
135 drawRange = function(start, end, top) { | 131 drawRange = function(start, end, top) { |
136 var left = start / fileSize * CANVAS_WIDTH; | 132 var left = start / fileSize * width; |
137 var right = end / fileSize * CANVAS_WIDTH; | 133 var right = end / fileSize * width; |
138 context.fillRect(left, top, right - left, top + CANVAS_HEIGHT / 2); | 134 context.fillRect(left, top, right - left, top + height / 2); |
139 }; | 135 }; |
140 | 136 |
141 context.fillStyle = '#0a0'; | 137 context.fillStyle = '#0a0'; |
142 this.read_.map(function(start, end) { | 138 this.read_.map(function(start, end) { |
143 drawRange(start, end, 0); | 139 drawRange(start, end, 0); |
144 }); | 140 }); |
145 | 141 |
146 context.fillStyle = '#00a'; | 142 context.fillStyle = '#00a'; |
147 this.written_.map(function(start, end) { | 143 this.written_.map(function(start, end) { |
148 drawRange(start, end, CANVAS_HEIGHT / 2); | 144 drawRange(start, end, height / 2); |
149 }); | 145 }); |
150 | 146 |
151 // Overlay a description of each bar. | 147 // Overlay a description of each bar. |
152 context.fillStyle = '#fff'; | 148 context.fillStyle = '#fff'; |
153 context.fillText('Read from cache.', CANVAS_WIDTH / 2, | 149 context.fillText('Read from cache.', width / 2, |
154 CANVAS_HEIGHT / 4 - 0.5); | 150 height / 4 - 0.5); |
155 context.fillText('Written to cache.', CANVAS_WIDTH / 2, | 151 context.fillText('Written to cache.', width / 2, |
156 CANVAS_HEIGHT * 3 / 4 + 0.5); | 152 height * 3 / 4 + 0.5); |
157 | 153 |
158 // Add a 1px separator line. | 154 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 { | 155 } else { |
164 // We can't draw bars if we don't know how big the file is. | 156 // We can't draw bars if we don't know how big the file is. |
165 context.fillStyle = '#fff'; | 157 context.fillStyle = '#fff'; |
166 context.fillText('Unknown file size. See details below.', | 158 context.fillText('Unknown file size. See details below.', |
167 CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2); | 159 width / 2, height / 2); |
168 } | 160 } |
169 }, | 161 }, |
170 | 162 |
171 /** | 163 /** |
172 * Update this.details_ to contain everything we currently know about | 164 * Update this.details_ to contain everything we currently know about |
173 * this file. | 165 * this file. |
174 */ | 166 */ |
175 generateDetails: function() { | 167 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; | 168 this.details_.id = this.key; |
183 this.summaryText_.textContent = this.key || 'Unknown File'; | 169 this.summaryText_.textContent = clipURL(this.key) || 'Unknown File'; |
184 | 170 |
185 this.detailTable_.textContent = ''; | 171 this.detailTable_.textContent = ''; |
186 var header = document.createElement('thead'); | 172 var header = document.createElement('thead'); |
187 var footer = document.createElement('tfoot'); | 173 var footer = document.createElement('tfoot'); |
188 var body = document.createElement('tbody'); | 174 var body = document.createElement('tbody'); |
189 this.detailTable_.appendChild(header); | 175 this.detailTable_.appendChild(header); |
190 this.detailTable_.appendChild(footer); | 176 this.detailTable_.appendChild(footer); |
191 this.detailTable_.appendChild(body); | 177 this.detailTable_.appendChild(body); |
192 | 178 |
193 var headerRow = document.createElement('tr'); | 179 var headerRow = document.createElement('tr'); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 toListItem: function() { | 213 toListItem: function() { |
228 this.generateDetails(); | 214 this.generateDetails(); |
229 | 215 |
230 var result = document.createElement('li'); | 216 var result = document.createElement('li'); |
231 result.appendChild(this.details_); | 217 result.appendChild(this.details_); |
232 return result; | 218 return result; |
233 } | 219 } |
234 }; | 220 }; |
235 | 221 |
236 return { | 222 return { |
237 CacheEntry: CacheEntry | 223 CacheEntry: CacheEntry, |
scherkus (not reviewing)
2011/08/16 19:18:29
aren't trailing , bad in JS?
arv (Not doing code reviews)
2011/08/16 19:29:03
undo change
Scott Franklin
2011/08/16 23:33:52
Done.
| |
238 }; | 224 }; |
239 }); | 225 }); |
OLD | NEW |