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