| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/media/media_internals.h" | 5 #include "chrome/browser/media/media_internals.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/string16.h" | 8 #include "base/string16.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "chrome/browser/media/media_internals_observer.h" | 10 #include "chrome/browser/media/media_internals_observer.h" |
| 11 #include "chrome/browser/media/media_stream_capture_indicator.h" | 11 #include "chrome/browser/media/media_stream_capture_indicator.h" |
| 12 #include "chrome/browser/tab_contents/tab_util.h" |
| 12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/browser/web_ui.h" | 15 #include "content/public/browser/web_ui.h" |
| 14 #include "media/base/media_log.h" | 16 #include "media/base/media_log.h" |
| 15 #include "media/base/media_log_event.h" | 17 #include "media/base/media_log_event.h" |
| 16 | 18 |
| 17 using content::BrowserThread; | 19 using content::BrowserThread; |
| 18 | 20 |
| 21 namespace { |
| 22 |
| 23 std::string GetStreamID(int render_process_id, |
| 24 int render_view_id, |
| 25 int stream_id) { |
| 26 return base::StringPrintf("audio_streams.%d:%d:%d", |
| 27 render_process_id, render_view_id, stream_id); |
| 28 } |
| 29 |
| 30 } |
| 31 |
| 19 MediaInternals* MediaInternals::GetInstance() { | 32 MediaInternals* MediaInternals::GetInstance() { |
| 20 return Singleton<MediaInternals>::get(); | 33 return Singleton<MediaInternals>::get(); |
| 21 } | 34 } |
| 22 | 35 |
| 23 MediaInternals::~MediaInternals() {} | 36 MediaInternals::~MediaInternals() {} |
| 24 | 37 |
| 25 void MediaInternals::OnDeleteAudioStream(void* host, int stream_id) { | 38 void MediaInternals::OnDeleteAudioStream(int render_process_id, |
| 39 int render_view_id, |
| 40 int stream_id) { |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 27 std::string stream = base::StringPrintf("audio_streams.%p:%d", | 42 DeleteItem(GetStreamID(render_process_id, render_view_id, stream_id)); |
| 28 host, stream_id); | |
| 29 DeleteItem(stream); | |
| 30 } | 43 } |
| 31 | 44 |
| 32 void MediaInternals::OnSetAudioStreamPlaying( | 45 void MediaInternals::OnSetAudioStreamPlaying(int render_process_id, |
| 33 void* host, int stream_id, bool playing) { | 46 int render_view_id, |
| 47 int stream_id, |
| 48 bool playing) { |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 35 UpdateAudioStream(host, stream_id, | 50 UpdateAudioStream(render_process_id, render_view_id, stream_id, |
| 36 "playing", Value::CreateBooleanValue(playing)); | 51 "playing", Value::CreateBooleanValue(playing)); |
| 37 } | 52 } |
| 38 | 53 |
| 39 void MediaInternals::OnSetAudioStreamStatus( | 54 void MediaInternals::OnSetAudioStreamStatus(int render_process_id, |
| 40 void* host, int stream_id, const std::string& status) { | 55 int render_view_id, |
| 56 int stream_id, |
| 57 const std::string& status) { |
| 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 42 UpdateAudioStream(host, stream_id, | 59 UpdateAudioStream(render_process_id, render_view_id, stream_id, |
| 43 "status", Value::CreateStringValue(status)); | 60 "status", Value::CreateStringValue(status)); |
| 44 } | 61 } |
| 45 | 62 |
| 46 void MediaInternals::OnSetAudioStreamVolume( | 63 void MediaInternals::OnSetAudioStreamVolume(int render_process_id, |
| 47 void* host, int stream_id, double volume) { | 64 int render_view_id, |
| 65 int stream_id, |
| 66 double volume) { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 49 UpdateAudioStream(host, stream_id, | 68 UpdateAudioStream(render_process_id, render_view_id, stream_id, |
| 50 "volume", Value::CreateDoubleValue(volume)); | 69 "volume", Value::CreateDoubleValue(volume)); |
| 51 } | 70 } |
| 52 | 71 |
| 53 void MediaInternals::OnMediaEvent( | 72 void MediaInternals::OnMediaEvent( |
| 54 int render_process_id, const media::MediaLogEvent& event) { | 73 int render_process_id, const media::MediaLogEvent& event) { |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 56 | 75 |
| 57 // Notify observers that |event| has occured. | 76 // Notify observers that |event| has occured. |
| 58 DictionaryValue dict; | 77 DictionaryValue dict; |
| 59 dict.SetInteger("renderer", render_process_id); | 78 dict.SetInteger("renderer", render_process_id); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 observers_.RemoveObserver(observer); | 115 observers_.RemoveObserver(observer); |
| 97 } | 116 } |
| 98 | 117 |
| 99 void MediaInternals::SendEverything() { | 118 void MediaInternals::SendEverything() { |
| 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 101 SendUpdate("media.onReceiveEverything", &data_); | 120 SendUpdate("media.onReceiveEverything", &data_); |
| 102 } | 121 } |
| 103 | 122 |
| 104 MediaInternals::MediaInternals() {} | 123 MediaInternals::MediaInternals() {} |
| 105 | 124 |
| 106 void MediaInternals::UpdateAudioStream( | 125 void MediaInternals::UpdateAudioStream(int render_process_id, |
| 107 void* host, int stream_id, const std::string& property, Value* value) { | 126 int render_view_id, |
| 108 std::string stream = base::StringPrintf("audio_streams.%p:%d", | 127 int stream_id, |
| 109 host, stream_id); | 128 const std::string& property, |
| 110 UpdateItem("media.addAudioStream", stream, property, value); | 129 Value* value) { |
| 130 const std::string id = GetStreamID( |
| 131 render_process_id, render_view_id, stream_id); |
| 132 UpdateItem("media.addAudioStream", render_process_id, render_view_id, id, |
| 133 property, value); |
| 111 } | 134 } |
| 112 | 135 |
| 113 void MediaInternals::DeleteItem(const std::string& item) { | 136 void MediaInternals::DeleteItem(const std::string& item) { |
| 114 data_.Remove(item, NULL); | 137 data_.Remove(item, NULL); |
| 115 scoped_ptr<Value> value(Value::CreateStringValue(item)); | 138 scoped_ptr<Value> value(Value::CreateStringValue(item)); |
| 116 SendUpdate("media.onItemDeleted", value.get()); | 139 SendUpdate("media.onItemDeleted", value.get()); |
| 117 } | 140 } |
| 118 | 141 |
| 119 void MediaInternals::UpdateItem( | 142 void MediaInternals::UpdateItem( |
| 120 const std::string& update_fn, const std::string& id, | 143 const std::string& update_fn, |
| 121 const std::string& property, Value* value) { | 144 int render_process_id, |
| 145 int render_view_id, |
| 146 const std::string& id, |
| 147 const std::string& property, |
| 148 Value* value) { |
| 122 DictionaryValue* item_properties; | 149 DictionaryValue* item_properties; |
| 123 if (!data_.GetDictionary(id, &item_properties)) { | 150 if (!data_.GetDictionary(id, &item_properties)) { |
| 124 item_properties = new DictionaryValue(); | 151 item_properties = new DictionaryValue(); |
| 125 data_.Set(id, item_properties); | 152 data_.Set(id, item_properties); |
| 126 item_properties->SetString("id", id); | 153 item_properties->SetString("id", id); |
| 127 } | 154 } |
| 128 item_properties->Set(property, value); | 155 item_properties->Set(property, value); |
| 156 |
| 157 content::WebContents* web_contents = |
| 158 tab_util::GetWebContentsByID(render_process_id, render_view_id); |
| 159 if (web_contents) { |
| 160 string16 tab_title = web_contents->GetTitle(); |
| 161 item_properties->Set("tab_title", Value::CreateStringValue(tab_title)); |
| 162 } |
| 163 |
| 129 SendUpdate(update_fn, item_properties); | 164 SendUpdate(update_fn, item_properties); |
| 130 } | 165 } |
| 131 | 166 |
| 132 void MediaInternals::SendUpdate(const std::string& function, Value* value) { | 167 void MediaInternals::SendUpdate(const std::string& function, Value* value) { |
| 133 // Only bother serializing the update to JSON if someone is watching. | 168 // Only bother serializing the update to JSON if someone is watching. |
| 134 if (observers_.size()) { | 169 if (observers_.size()) { |
| 135 std::vector<const Value*> args; | 170 std::vector<const Value*> args; |
| 136 args.push_back(value); | 171 args.push_back(value); |
| 137 string16 update = content::WebUI::GetJavascriptCall(function, args); | 172 string16 update = content::WebUI::GetJavascriptCall(function, args); |
| 138 FOR_EACH_OBSERVER(MediaInternalsObserver, observers_, OnUpdate(update)); | 173 FOR_EACH_OBSERVER(MediaInternalsObserver, observers_, OnUpdate(update)); |
| 139 } | 174 } |
| 140 } | 175 } |
| OLD | NEW |