OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 /** | 5 /** |
6 * A global object that gets used by the C++ interface. | 6 * A global object that gets used by the C++ interface. |
7 */ | 7 */ |
8 var media = (function() { | 8 var media = (function() { |
9 'use strict'; | 9 'use strict'; |
10 | 10 |
11 var manager_ = null; | 11 var manager_ = null; |
12 | 12 |
13 // A number->string mapping that is populated through the backend that | |
14 // describes the phase that the network entity is in. | |
15 var eventPhases_ = {}; | |
16 // A number->string mapping that is populated through the backend that | |
17 // describes the type of event sent from the network. | |
18 var eventTypes_ = {}; | |
19 | |
20 // A mapping of number->CacheEntry where the number is a unique id for that | |
21 // network request. | |
22 var cacheEntries_ = {}; | |
scherkus (not reviewing)
2013/09/06 18:16:47
nit: add blink line after this line (you've got th
Ty Overby
2013/09/06 19:56:37
Done.
| |
23 // A mapping of url->CacheEntity where the url is the url of the resource. | |
24 var cacheEntriesByKey_ = {}; | |
25 | |
26 var requestURLs_ = {}; | |
27 | |
28 var media = { | |
29 BAR_WIDTH: 200, | |
30 BAR_HEIGHT: 25 | |
31 }; | |
32 | |
13 /** | 33 /** |
14 * Users of |media| must call initialize prior to calling other methods. | 34 * Users of |media| must call initialize prior to calling other methods. |
15 */ | 35 */ |
16 media.initialize = function(manager) { | 36 media.initialize = function(manager) { |
17 manager_ = manager; | 37 manager_ = manager; |
18 }; | 38 }; |
19 | 39 |
20 media.onReceiveEverything = function(everything) { | 40 media.onReceiveEverything = function(everything) { |
21 for (var key in everything.audio_streams) { | 41 for (var key in everything.audio_streams) { |
22 media.updateAudioStream(everything.audio_streams[key]); | 42 media.updateAudioStream(everything.audio_streams[key]); |
23 } | 43 } |
24 }; | 44 }; |
25 | 45 |
26 media.onNetUpdate = function(update) { | 46 media.onReceiveConstants = function(constants) { |
27 // TODO(tyoverby): Implement | 47 for (var key in constants.eventTypes) { |
48 var value = constants.eventTypes[key]; | |
49 eventTypes_[value] = key; | |
50 } | |
51 | |
52 for (var key in constants.eventPhases) { | |
53 var value = constants.eventPhases[key]; | |
54 eventPhases_[value] = key; | |
55 } | |
56 }; | |
57 | |
58 media.cacheForUrl = function(url) { | |
59 return cacheEntriesByKey_[url]; | |
60 }; | |
61 | |
62 media.onNetUpdate = function(updates) { | |
63 updates.forEach(function(update) { | |
64 var id = update.source.id; | |
65 if (!cacheEntries_[id]) | |
66 cacheEntries_[id] = new media.CacheEntry; | |
67 | |
68 switch (eventPhases_[update.phase] + '.' + eventTypes_[update.type]) { | |
69 case 'PHASE_BEGIN.DISK_CACHE_ENTRY_IMPL': | |
70 var key = update.params.key; | |
71 | |
72 // Merge this source with anything we already know about this key. | |
73 if (cacheEntriesByKey_[key]) { | |
74 cacheEntriesByKey_[key].merge(cacheEntries_[id]); | |
75 cacheEntries_[id] = cacheEntriesByKey_[key]; | |
76 } else { | |
77 cacheEntriesByKey_[key] = cacheEntries_[id]; | |
78 } | |
79 cacheEntriesByKey_[key].key = key; | |
80 break; | |
81 | |
82 case 'PHASE_BEGIN.SPARSE_READ': | |
83 cacheEntries_[id].readBytes(update.params.offset, | |
84 update.params.buff_len); | |
85 cacheEntries_[id].sparse = true; | |
86 break; | |
87 | |
88 case 'PHASE_BEGIN.SPARSE_WRITE': | |
89 cacheEntries_[id].writeBytes(update.params.offset, | |
90 update.params.buff_len); | |
91 cacheEntries_[id].sparse = true; | |
92 break; | |
93 | |
94 case 'PHASE_BEGIN.URL_REQUEST_START_JOB': | |
95 requestURLs_[update.source.id] = update.params.url; | |
96 break; | |
97 | |
98 case 'PHASE_NONE.HTTP_TRANSACTION_READ_RESPONSE_HEADERS': | |
99 // Record the total size of the file if this was a range request. | |
100 var range = /content-range:\s*bytes\s*\d+-\d+\/(\d+)/i.exec( | |
101 update.params.headers); | |
102 var key = requestURLs_[update.source.id]; | |
103 delete requestURLs_[update.source.id]; | |
104 if (range && key) { | |
105 if (!cacheEntriesByKey_[key]) { | |
106 cacheEntriesByKey_[key] = new media.CacheEntry; | |
107 cacheEntriesByKey_[key].key = key; | |
108 } | |
109 cacheEntriesByKey_[key].size = range[1]; | |
110 } | |
111 break; | |
112 } | |
113 }); | |
28 }; | 114 }; |
29 | 115 |
30 media.onRendererTerminated = function(renderId) { | 116 media.onRendererTerminated = function(renderId) { |
31 util.object.forEach(manager_.players_, function(playerInfo, id) { | 117 util.object.forEach(manager_.players_, function(playerInfo, id) { |
32 if (playerInfo.properties['render_id'] == renderId) { | 118 if (playerInfo.properties['render_id'] == renderId) { |
33 manager_.removePlayer(id); | 119 manager_.removePlayer(id); |
34 } | 120 } |
35 }); | 121 }); |
36 }; | 122 }; |
37 | 123 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 } | 178 } |
93 propertyCount += 1; | 179 propertyCount += 1; |
94 }); | 180 }); |
95 | 181 |
96 if (propertyCount === 0) { | 182 if (propertyCount === 0) { |
97 manager_.updatePlayerInfo( | 183 manager_.updatePlayerInfo( |
98 source, event.ticksMillis, 'EVENT', event.type); | 184 source, event.ticksMillis, 'EVENT', event.type); |
99 } | 185 } |
100 }; | 186 }; |
101 | 187 |
188 chrome.send('getEverything'); | |
102 return media; | 189 return media; |
103 }()); | 190 }()); |
OLD | NEW |