Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / audio_helper.cc
bloba9a9568a379583a7cd5d9dffa165f7aebe264cbb
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 "content/renderer/pepper/audio_helper.h"
7 #include "base/logging.h"
8 #include "content/common/pepper_file_util.h"
9 #include "ppapi/c/pp_completion_callback.h"
10 #include "ppapi/c/pp_errors.h"
12 using ppapi::TrackedCallback;
14 namespace content {
16 // AudioHelper -----------------------------------------------------------------
18 AudioHelper::AudioHelper() : shared_memory_size_for_create_callback_(0) {}
20 AudioHelper::~AudioHelper() {}
22 int32_t AudioHelper::GetSyncSocketImpl(int* sync_socket) {
23 if (socket_for_create_callback_) {
24 *sync_socket = IntegerFromSyncSocketHandle(
25 socket_for_create_callback_->handle());
26 return PP_OK;
28 return PP_ERROR_FAILED;
31 int32_t AudioHelper::GetSharedMemoryImpl(int* shm_handle, uint32_t* shm_size) {
32 if (shared_memory_for_create_callback_) {
33 *shm_handle = reinterpret_cast<int>(PlatformFileFromSharedMemoryHandle(
34 shared_memory_for_create_callback_->handle()));
35 *shm_size = shared_memory_size_for_create_callback_;
36 return PP_OK;
38 return PP_ERROR_FAILED;
41 void AudioHelper::StreamCreated(base::SharedMemoryHandle shared_memory_handle,
42 size_t shared_memory_size,
43 base::SyncSocket::Handle socket_handle) {
44 if (TrackedCallback::IsPending(create_callback_)) {
45 // Trusted side of proxy can specify a callback to receive handles. In
46 // this case we don't need to map any data or start the thread since it
47 // will be handled by the proxy.
48 shared_memory_for_create_callback_.reset(
49 new base::SharedMemory(shared_memory_handle, false));
50 shared_memory_size_for_create_callback_ = shared_memory_size;
51 socket_for_create_callback_.reset(new base::SyncSocket(socket_handle));
53 create_callback_->Run(PP_OK);
55 // It might be nice to close the handles here to free up some system
56 // resources, but we can't since there's a race condition. The handles must
57 // be valid until they're sent over IPC, which is done from the I/O thread
58 // which will often get done after this code executes. We could do
59 // something more elaborate like an ACK from the plugin or post a task to
60 // the I/O thread and back, but this extra complexity doesn't seem worth it
61 // just to clean up these handles faster.
62 } else {
63 OnSetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle);
67 void AudioHelper::SetCreateCallback(
68 scoped_refptr<ppapi::TrackedCallback> create_callback) {
69 DCHECK(!TrackedCallback::IsPending(create_callback_));
70 create_callback_ = create_callback;
73 } // namespace content