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