1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/audio_helper.h"
9 #include "base/check.h"
10 #include "base/logging.h"
11 #include "content/common/pepper_file_util.h"
12 #include "ppapi/c/pp_completion_callback.h"
13 #include "ppapi/c/pp_errors.h"
15 using ppapi::TrackedCallback;
19 // AudioHelper -----------------------------------------------------------------
21 AudioHelper::AudioHelper() {}
23 AudioHelper::~AudioHelper() {}
25 int32_t AudioHelper::GetSyncSocketImpl(int* sync_socket) {
26 if (socket_for_create_callback_) {
27 *sync_socket = IntegerFromSyncSocketHandle(
28 socket_for_create_callback_->handle());
31 LOG(ERROR) << "Cannot get sync socket";
32 return PP_ERROR_FAILED;
35 int32_t AudioHelper::GetSharedMemoryImpl(base::UnsafeSharedMemoryRegion** shm) {
36 if (shared_memory_for_create_callback_.IsValid()) {
37 *shm = &shared_memory_for_create_callback_;
40 LOG(ERROR) << "Cannot get shared memory";
41 return PP_ERROR_FAILED;
44 void AudioHelper::StreamCreated(
45 base::UnsafeSharedMemoryRegion shared_memory_region,
46 base::SyncSocket::ScopedHandle socket_handle) {
47 if (TrackedCallback::IsPending(create_callback_)) {
48 // Trusted side of proxy can specify a callback to receive handles. In
49 // this case we don't need to map any data or start the thread since it
50 // will be handled by the proxy.
51 shared_memory_for_create_callback_ = std::move(shared_memory_region);
52 socket_for_create_callback_ =
53 std::make_unique<base::SyncSocket>(std::move(socket_handle));
55 create_callback_->Run(PP_OK);
57 // It might be nice to close the handles here to free up some system
58 // resources, but we can't since there's a race condition. The handles must
59 // be valid until they're sent over IPC, which is done from the I/O thread
60 // which will often get done after this code executes. We could do
61 // something more elaborate like an ACK from the plugin or post a task to
62 // the I/O thread and back, but this extra complexity doesn't seem worth it
63 // just to clean up these handles faster.
65 OnSetStreamInfo(std::move(shared_memory_region), std::move(socket_handle));
69 void AudioHelper::SetCreateCallback(
70 scoped_refptr<ppapi::TrackedCallback> create_callback) {
71 DCHECK(!TrackedCallback::IsPending(create_callback_));
72 create_callback_ = create_callback;
75 } // namespace content