| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #include "content/renderer/media/audio_ipc_factory.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "content/renderer/media/audio_message_filter.h" |
| 12 #include "content/renderer/media/mojo_audio_output_ipc.h" |
| 13 #include "services/service_manager/public/cpp/interface_provider.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 AudioIPCFactory* AudioIPCFactory::instance_ = nullptr; |
| 18 |
| 19 AudioIPCFactory::AudioIPCFactory( |
| 20 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 21 : audio_message_filter_(new AudioMessageFilter(io_task_runner)), |
| 22 io_task_runner_(io_task_runner) { |
| 23 // TODO(maxmorin): In the future, set |audio_message_filter_| to nullptr based |
| 24 // on a feature. |
| 25 DCHECK(!instance_); |
| 26 instance_ = this; |
| 27 } |
| 28 |
| 29 AudioIPCFactory::~AudioIPCFactory() { |
| 30 // Allow destruction in tests. |
| 31 DCHECK_EQ(instance_, this); |
| 32 instance_ = nullptr; |
| 33 } |
| 34 |
| 35 std::unique_ptr<media::AudioOutputIPC> AudioIPCFactory::CreateAudioOutputIPC( |
| 36 int frame_id) const { |
| 37 if (UsingMojoFactories()) { |
| 38 // Unretained is safe because |this| is leaked. |
| 39 return base::MakeUnique<MojoAudioOutputIPC>(base::Bind( |
| 40 &AudioIPCFactory::GetRemoteFactory, base::Unretained(this), frame_id)); |
| 41 } |
| 42 return audio_message_filter_->CreateAudioOutputIPC(frame_id); |
| 43 } |
| 44 |
| 45 void AudioIPCFactory::MaybeRegisterRemoteFactory( |
| 46 int frame_id, |
| 47 service_manager::InterfaceProvider* interface_provider) { |
| 48 mojom::RendererAudioOutputStreamFactoryPtr factory_ptr; |
| 49 interface_provider->GetInterface(&factory_ptr); |
| 50 // PassInterface unbinds the message pipe from the current thread. This |
| 51 // allows us to bind it to the IO thread. |
| 52 // Unretained is safe because |this| is leaked. |
| 53 io_task_runner_->PostTask( |
| 54 FROM_HERE, base::Bind(&AudioIPCFactory::RegisterRemoteFactoryOnIOThread, |
| 55 base::Unretained(this), frame_id, |
| 56 base::Passed(factory_ptr.PassInterface()))); |
| 57 } |
| 58 |
| 59 void AudioIPCFactory::MaybeDeregisterRemoteFactory(int frame_id) { |
| 60 if (!UsingMojoFactories()) |
| 61 return; |
| 62 if (!io_task_runner_->BelongsToCurrentThread()) { |
| 63 // Unretained is safe because |this| is leaked. |
| 64 io_task_runner_->PostTask( |
| 65 FROM_HERE, base::Bind(&AudioIPCFactory::MaybeDeregisterRemoteFactory, |
| 66 base::Unretained(this), frame_id)); |
| 67 return; |
| 68 } |
| 69 factory_ptrs_.erase(frame_id); |
| 70 } |
| 71 |
| 72 mojom::RendererAudioOutputStreamFactory* AudioIPCFactory::GetRemoteFactory( |
| 73 int frame_id) const { |
| 74 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 75 DCHECK(UsingMojoFactories()); |
| 76 auto it = factory_ptrs_.find(frame_id); |
| 77 return it == factory_ptrs_.end() ? nullptr : it->second.get(); |
| 78 } |
| 79 |
| 80 void AudioIPCFactory::RegisterRemoteFactoryOnIOThread( |
| 81 int frame_id, |
| 82 mojom::RendererAudioOutputStreamFactoryPtrInfo factory_ptr_info) { |
| 83 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 84 DCHECK(UsingMojoFactories()); |
| 85 std::pair<StreamFactoryMap::iterator, bool> emplace_result = |
| 86 factory_ptrs_.emplace(frame_id, |
| 87 mojo::MakeProxy(std::move(factory_ptr_info))); |
| 88 |
| 89 DCHECK(emplace_result.second) << "Attempt to register a factory for a " |
| 90 "frame which already has a factory " |
| 91 "registered."; |
| 92 |
| 93 auto& emplaced_factory = emplace_result.first->second; |
| 94 DCHECK(emplaced_factory.is_bound()) |
| 95 << "Factory is not bound to a remote implementation."; |
| 96 |
| 97 // Unretained is safe because |this| owns the binding, so a connection error |
| 98 // cannot trigger after destruction. |
| 99 emplaced_factory.set_connection_error_handler( |
| 100 base::Bind(&AudioIPCFactory::MaybeDeregisterRemoteFactory, |
| 101 base::Unretained(this), frame_id)); |
| 102 } |
| 103 |
| 104 bool AudioIPCFactory::UsingMojoFactories() const { |
| 105 return audio_message_filter_ == nullptr; |
| 106 } |
| 107 |
| 108 } // namespace content |
| OLD | NEW |