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
3 // found in the LICENSE file.
6 * A global object that gets used by the C++ interface.
8 var media
= (function() {
13 // A number->string mapping that is populated through the backend that
14 // describes the phase that the network entity is in.
17 // A number->string mapping that is populated through the backend that
18 // describes the type of event sent from the network.
21 // A mapping of number->CacheEntry where the number is a unique id for that
23 var cacheEntries
= {};
25 // A mapping of url->CacheEntity where the url is the url of the resource.
26 var cacheEntriesByKey
= {};
28 var requrestURLs
= {};
36 * Users of |media| must call initialize prior to calling other methods.
38 media
.initialize = function(theManager
) {
42 media
.onReceiveEverything = function(everything
) {
43 for (var component
in everything
) {
44 media
.updateAudioComponent(everything
[component
]);
48 media
.onReceiveConstants = function(constants
) {
49 for (var key
in constants
.eventTypes
) {
50 var value
= constants
.eventTypes
[key
];
51 eventTypes
[value
] = key
;
54 for (var key
in constants
.eventPhases
) {
55 var value
= constants
.eventPhases
[key
];
56 eventPhases
[value
] = key
;
60 media
.cacheForUrl = function(url
) {
61 return cacheEntriesByKey
[url
];
64 media
.onNetUpdate = function(updates
) {
65 updates
.forEach(function(update
) {
66 var id
= update
.source
.id
;
67 if (!cacheEntries
[id
])
68 cacheEntries
[id
] = new media
.CacheEntry
;
70 switch (eventPhases
[update
.phase
] + '.' + eventTypes
[update
.type
]) {
71 case 'PHASE_BEGIN.DISK_CACHE_ENTRY_IMPL':
72 var key
= update
.params
.key
;
74 // Merge this source with anything we already know about this key.
75 if (cacheEntriesByKey
[key
]) {
76 cacheEntriesByKey
[key
].merge(cacheEntries
[id
]);
77 cacheEntries
[id
] = cacheEntriesByKey
[key
];
79 cacheEntriesByKey
[key
] = cacheEntries
[id
];
81 cacheEntriesByKey
[key
].key
= key
;
84 case 'PHASE_BEGIN.SPARSE_READ':
85 cacheEntries
[id
].readBytes(update
.params
.offset
,
86 update
.params
.buff_len
);
87 cacheEntries
[id
].sparse
= true;
90 case 'PHASE_BEGIN.SPARSE_WRITE':
91 cacheEntries
[id
].writeBytes(update
.params
.offset
,
92 update
.params
.buff_len
);
93 cacheEntries
[id
].sparse
= true;
96 case 'PHASE_BEGIN.URL_REQUEST_START_JOB':
97 requrestURLs
[update
.source
.id
] = update
.params
.url
;
100 case 'PHASE_NONE.HTTP_TRANSACTION_READ_RESPONSE_HEADERS':
101 // Record the total size of the file if this was a range request.
102 var range
= /content-range:\s*bytes\s*\d+-\d+\/(\d+)/i.exec(
103 update
.params
.headers
);
104 var key
= requrestURLs
[update
.source
.id
];
105 delete requrestURLs
[update
.source
.id
];
107 if (!cacheEntriesByKey
[key
]) {
108 cacheEntriesByKey
[key
] = new media
.CacheEntry
;
109 cacheEntriesByKey
[key
].key
= key
;
111 cacheEntriesByKey
[key
].size
= range
[1];
118 media
.onRendererTerminated = function(renderId
) {
119 util
.object
.forEach(manager
.players_
, function(playerInfo
, id
) {
120 if (playerInfo
.properties
['render_id'] == renderId
) {
121 manager
.removePlayer(id
);
126 media
.updateAudioComponent = function(component
) {
127 var uniqueComponentId
= component
.owner_id
+ ':' + component
.component_id
;
128 switch (component
.status
) {
130 manager
.removeAudioComponent(
131 component
.component_type
, uniqueComponentId
);
134 manager
.updateAudioComponent(
135 component
.component_type
, uniqueComponentId
, component
);
140 media
.onPlayerOpen = function(id
, timestamp
) {
141 manager
.addPlayer(id
, timestamp
);
144 media
.onMediaEvent = function(event
) {
145 var source
= event
.renderer
+ ':' + event
.player
;
147 // Although this gets called on every event, there is nothing we can do
148 // because there is no onOpen event.
149 media
.onPlayerOpen(source
);
150 manager
.updatePlayerInfoNoRecord(
151 source
, event
.ticksMillis
, 'render_id', event
.renderer
);
152 manager
.updatePlayerInfoNoRecord(
153 source
, event
.ticksMillis
, 'player_id', event
.player
);
155 var propertyCount
= 0;
156 util
.object
.forEach(event
.params
, function(value
, key
) {
159 // These keys get spammed *a lot*, so put them on the display
160 // but don't log list.
161 if (key
=== 'buffer_start' ||
162 key
=== 'buffer_end' ||
163 key
=== 'buffer_current' ||
164 key
=== 'is_downloading_data') {
165 manager
.updatePlayerInfoNoRecord(
166 source
, event
.ticksMillis
, key
, value
);
168 manager
.updatePlayerInfo(source
, event
.ticksMillis
, key
, value
);
173 if (propertyCount
=== 0) {
174 manager
.updatePlayerInfo(
175 source
, event
.ticksMillis
, 'EVENT', event
.type
);
179 // |chrome| is not defined during tests.
180 if (window
.chrome
&& window
.chrome
.send
) {
181 chrome
.send('getEverything');