| 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 #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 "content/browser/browser_thread.h" | 11 #include "content/browser/browser_thread.h" |
| 12 #include "content/browser/webui/web_ui.h" | 12 #include "content/browser/webui/web_ui.h" |
| 13 | 13 |
| 14 // The names of the javascript functions to call with updates. | 14 // The names of the javascript functions to call with updates. |
| 15 static const char kDeleteItemFunction[] = "onItemDeleted"; | 15 static const char kDeleteItemFunction[] = "onItemDeleted"; |
| 16 static const char kAudioUpdateFunction[] = "onAudioUpdate"; | 16 static const char kAudioUpdateFunction[] = "onAudioUpdate"; |
| 17 static const char kSendEverythingFunction[] = "onReceiveEverything"; | 17 static const char kSendEverythingFunction[] = "onReceiveEverything"; |
| 18 | 18 |
| 19 MediaInternals::~MediaInternals() {} | 19 MediaInternals::~MediaInternals() {} |
| 20 | 20 |
| 21 void MediaInternals::OnDeleteAudioStream( | 21 void MediaInternals::OnDeleteAudioStream(void* host, int stream_id) { |
| 22 void* host, int32 render_view, int stream_id) { | 22 std::string stream = base::StringPrintf("audio_streams.%p:%d", |
| 23 std::string stream = base::StringPrintf("audio_streams.%p:%d:%d", | 23 host, stream_id); |
| 24 host, render_view, stream_id); | |
| 25 DeleteItem(stream); | 24 DeleteItem(stream); |
| 26 } | 25 } |
| 27 | 26 |
| 28 void MediaInternals::OnSetAudioStreamPlaying( | 27 void MediaInternals::OnSetAudioStreamPlaying( |
| 29 void* host, int32 render_view, int stream_id, bool playing) { | 28 void* host, int stream_id, bool playing) { |
| 30 UpdateAudioStream(host, render_view, stream_id, | 29 UpdateAudioStream(host, stream_id, |
| 31 "playing", Value::CreateBooleanValue(playing)); | 30 "playing", Value::CreateBooleanValue(playing)); |
| 32 } | 31 } |
| 33 | 32 |
| 34 void MediaInternals::OnSetAudioStreamStatus( | 33 void MediaInternals::OnSetAudioStreamStatus( |
| 35 void* host, int32 render_view, int stream_id, const std::string& status) { | 34 void* host, int stream_id, const std::string& status) { |
| 36 UpdateAudioStream(host, render_view, stream_id, | 35 UpdateAudioStream(host, stream_id, |
| 37 "status", Value::CreateStringValue(status)); | 36 "status", Value::CreateStringValue(status)); |
| 38 } | 37 } |
| 39 | 38 |
| 40 void MediaInternals::OnSetAudioStreamVolume( | 39 void MediaInternals::OnSetAudioStreamVolume( |
| 41 void* host, int32 render_view, int stream_id, double volume) { | 40 void* host, int stream_id, double volume) { |
| 42 UpdateAudioStream(host, render_view, stream_id, | 41 UpdateAudioStream(host, stream_id, |
| 43 "volume", Value::CreateDoubleValue(volume)); | 42 "volume", Value::CreateDoubleValue(volume)); |
| 44 } | 43 } |
| 45 | 44 |
| 46 void MediaInternals::AddUI(MediaInternalsObserver* ui) { | 45 void MediaInternals::AddUI(MediaInternalsObserver* ui) { |
| 47 observers_->AddObserver(ui); | 46 observers_->AddObserver(ui); |
| 48 } | 47 } |
| 49 | 48 |
| 50 void MediaInternals::RemoveUI(MediaInternalsObserver* ui) { | 49 void MediaInternals::RemoveUI(MediaInternalsObserver* ui) { |
| 51 observers_->RemoveObserver(ui); | 50 observers_->RemoveObserver(ui); |
| 52 } | 51 } |
| 53 | 52 |
| 54 void MediaInternals::SendEverything() { | 53 void MediaInternals::SendEverything() { |
| 55 SendUpdate(kSendEverythingFunction, &data_); | 54 SendUpdate(kSendEverythingFunction, &data_); |
| 56 } | 55 } |
| 57 | 56 |
| 58 MediaInternals::MediaInternals() | 57 MediaInternals::MediaInternals() |
| 59 : observers_(new ObserverListThreadSafe<MediaInternalsObserver>()) {} | 58 : observers_(new ObserverListThreadSafe<MediaInternalsObserver>()) {} |
| 60 | 59 |
| 61 void MediaInternals::UpdateAudioStream( | 60 void MediaInternals::UpdateAudioStream( |
| 62 void* host, int32 render_view, int stream_id, | 61 void* host, int stream_id, const std::string& property, Value* value) { |
| 63 const std::string& property, Value* value) { | 62 std::string stream = base::StringPrintf("audio_streams.%p:%d", |
| 64 std::string stream = base::StringPrintf("audio_streams.%p:%d:%d", | 63 host, stream_id); |
| 65 host, render_view, stream_id); | |
| 66 UpdateItem(kAudioUpdateFunction, stream, property, value); | 64 UpdateItem(kAudioUpdateFunction, stream, property, value); |
| 67 } | 65 } |
| 68 | 66 |
| 69 void MediaInternals::DeleteItem(const std::string& item) { | 67 void MediaInternals::DeleteItem(const std::string& item) { |
| 70 data_.Remove(item, NULL); | 68 data_.Remove(item, NULL); |
| 71 scoped_ptr<Value> value(Value::CreateStringValue(item)); | 69 scoped_ptr<Value> value(Value::CreateStringValue(item)); |
| 72 SendUpdate(kDeleteItemFunction, value.get()); | 70 SendUpdate(kDeleteItemFunction, value.get()); |
| 73 } | 71 } |
| 74 | 72 |
| 75 void MediaInternals::UpdateItem( | 73 void MediaInternals::UpdateItem( |
| 76 const std::string& update_fn, const std::string& id, | 74 const std::string& update_fn, const std::string& id, |
| 77 const std::string& property, Value* value) { | 75 const std::string& property, Value* value) { |
| 78 DictionaryValue* item_properties; | 76 DictionaryValue* item_properties; |
| 79 if (!data_.GetDictionary(id, &item_properties)) { | 77 if (!data_.GetDictionary(id, &item_properties)) { |
| 80 item_properties = new DictionaryValue(); | 78 item_properties = new DictionaryValue(); |
| 81 data_.Set(id, item_properties); | 79 data_.Set(id, item_properties); |
| 82 item_properties->SetString("id", id); | 80 item_properties->SetString("id", id); |
| 83 } | 81 } |
| 84 item_properties->Set(property, value); | 82 item_properties->Set(property, value); |
| 85 SendUpdate(update_fn, item_properties); | 83 SendUpdate(update_fn, item_properties); |
| 86 } | 84 } |
| 87 | 85 |
| 88 void MediaInternals::SendUpdate(const std::string& function, Value* value) { | 86 void MediaInternals::SendUpdate(const std::string& function, Value* value) { |
| 89 scoped_ptr<std::vector<const Value*> > args(new std::vector<const Value*>()); | 87 scoped_ptr<std::vector<const Value*> > args(new std::vector<const Value*>()); |
| 90 args->push_back(value); | 88 args->push_back(value); |
| 91 string16 update = WebUI::GetJavascriptCall(function, *args.get()); | 89 string16 update = WebUI::GetJavascriptCall(function, *args.get()); |
| 92 observers_->Notify(&MediaInternalsObserver::OnUpdate, update); | 90 observers_->Notify(&MediaInternalsObserver::OnUpdate, update); |
| 93 } | 91 } |
| OLD | NEW |