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
3 // found in the LICENSE file.
4
5 #include "content/renderer/pepper/audio_helper.h"
6
7 #include "base/logging.h"
8 #include "ppapi/c/pp_completion_callback.h"
9 #include "ppapi/c/pp_errors.h"
10
11 using ppapi::TrackedCallback;
12
13 namespace content {
14
15 // AudioHelper -----------------------------------------------------------------
16
AudioHelper()17 AudioHelper::AudioHelper() : shared_memory_size_for_create_callback_(0) {}
18
~AudioHelper()19 AudioHelper::~AudioHelper() {}
20
GetSyncSocketImpl(int * sync_socket)21 int32_t AudioHelper::GetSyncSocketImpl(int* sync_socket) {
22 if (socket_for_create_callback_) {
23 #if defined(OS_POSIX)
24 *sync_socket = socket_for_create_callback_->handle();
25 #elif defined(OS_WIN)
26 *sync_socket = reinterpret_cast<int>(socket_for_create_callback_->handle());
27 #else
28 #error "Platform not supported."
29 #endif
30 return PP_OK;
31 }
32 return PP_ERROR_FAILED;
33 }
34
GetSharedMemoryImpl(int * shm_handle,uint32_t * shm_size)35 int32_t AudioHelper::GetSharedMemoryImpl(int* shm_handle, uint32_t* shm_size) {
36 if (shared_memory_for_create_callback_) {
37 #if defined(OS_POSIX)
38 *shm_handle = shared_memory_for_create_callback_->handle().fd;
39 #elif defined(OS_WIN)
40 *shm_handle =
41 reinterpret_cast<int>(shared_memory_for_create_callback_->handle());
42 #else
43 #error "Platform not supported."
44 #endif
45 *shm_size = shared_memory_size_for_create_callback_;
46 return PP_OK;
47 }
48 return PP_ERROR_FAILED;
49 }
50
StreamCreated(base::SharedMemoryHandle shared_memory_handle,size_t shared_memory_size,base::SyncSocket::Handle socket_handle)51 void AudioHelper::StreamCreated(base::SharedMemoryHandle shared_memory_handle,
52 size_t shared_memory_size,
53 base::SyncSocket::Handle socket_handle) {
54 if (TrackedCallback::IsPending(create_callback_)) {
55 // Trusted side of proxy can specify a callback to receive handles. In
56 // this case we don't need to map any data or start the thread since it
57 // will be handled by the proxy.
58 shared_memory_for_create_callback_.reset(
59 new base::SharedMemory(shared_memory_handle, false));
60 shared_memory_size_for_create_callback_ = shared_memory_size;
61 socket_for_create_callback_.reset(new base::SyncSocket(socket_handle));
62
63 create_callback_->Run(PP_OK);
64
65 // It might be nice to close the handles here to free up some system
66 // resources, but we can't since there's a race condition. The handles must
67 // be valid until they're sent over IPC, which is done from the I/O thread
68 // which will often get done after this code executes. We could do
69 // something more elaborate like an ACK from the plugin or post a task to
70 // the I/O thread and back, but this extra complexity doesn't seem worth it
71 // just to clean up these handles faster.
72 } else {
73 OnSetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle);
74 }
75 }
76
SetCreateCallback(scoped_refptr<ppapi::TrackedCallback> create_callback)77 void AudioHelper::SetCreateCallback(
78 scoped_refptr<ppapi::TrackedCallback> create_callback) {
79 DCHECK(!TrackedCallback::IsPending(create_callback_));
80 create_callback_ = create_callback;
81 }
82
83 } // namespace content
84