Chromium Code Reviews| Index: content/renderer/pepper/pepper_io_stream_host.cc |
| diff --git a/content/renderer/pepper/pepper_io_stream_host.cc b/content/renderer/pepper/pepper_io_stream_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8161430500da23d020d90237e1b56f46946d73a1 |
| --- /dev/null |
| +++ b/content/renderer/pepper/pepper_io_stream_host.cc |
| @@ -0,0 +1,122 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/pepper/pepper_io_stream_host.h" |
| + |
| +#include "content/public/renderer/render_thread.h" |
| +#include "content/public/renderer/renderer_ppapi_host.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/host/dispatch_host_message.h" |
| +#include "ppapi/host/ppapi_host.h" |
| +#include "ppapi/proxy/host_dispatcher.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| + |
| + |
| +using ppapi::host::HostMessageContext; |
| +using ppapi::host::ReplyMessageContext; |
| +using ppapi::proxy::HostDispatcher; |
| +using ppapi::proxy::SerializedHandle; |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +base::PlatformFile ToPlatformFile(const base::SharedMemoryHandle& handle) { |
| +#if defined(OS_WIN) |
| + return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); |
| +#elif defined(OS_POSIX) |
| + return handle.fd; |
| +#else |
| +#error Not implemented. |
| +#endif |
| +} |
| + |
| +} // namespace |
| + |
| +PepperIOStreamHost::PepperIOStreamHost( |
| + RendererPpapiHost* host, |
| + PP_Instance instance, |
| + PP_Resource resource, |
| + bool is_input) |
| + : ResourceHost(host->GetPpapiHost(), instance, resource), |
| + ppapi::IOStreamShared(is_input) { |
| +} |
| + |
| +PepperIOStreamHost::~PepperIOStreamHost() { |
| +} |
| + |
| +void PepperIOStreamHost::MovePeerLimit(uint32_t offset) { |
| + CHECK(offset); |
|
bbudge
2014/01/02 22:16:22
DCHECK?
Peng
2014/01/03 16:15:11
Done.
|
| + host()->SendUnsolicitedReply(pp_resource(), |
| + PpapiPluginMsg_IOStream_MoveLimit(offset)); |
| +} |
| + |
| +bool PepperIOStreamHost::InitSharedBufferAndNotifyPlugin(uint32_t size) { |
| + if (!InitSharedBuffer(size)) |
| + return false; |
| + |
| + base::SharedMemoryHandle shm_handle = shared_memory()->handle(); |
| + base::PlatformFile platform_file = ToPlatformFile(shm_handle); |
| + HostDispatcher* dispatcher = HostDispatcher::GetForInstance(pp_instance()); |
| + SerializedHandle handle( |
| + dispatcher->ShareHandleWithRemote(platform_file, false), |
| + size); |
| + host()->SendUnsolicitedReplyWithHandle(pp_resource(), |
| + PpapiPluginMsg_IOStream_Init(size), handle); |
| + return true; |
| +} |
| + |
| +bool PepperIOStreamHost::InitSharedBuffer(uint32_t size) { |
| + DCHECK(size); |
| + content::RenderThread* render_thread = content::RenderThread::Get(); |
| + scoped_ptr<base::SharedMemory> shm; |
| + shm = render_thread->HostAllocateSharedMemoryBuffer(size).Pass(); |
|
bbudge
2014/01/02 22:16:22
Why not assign directly (on line above) to avoid d
Peng
2014/01/03 16:15:11
Done
|
| + if (!shm) |
| + return false; |
| + |
| + SetBuffer(shm.Pass(), size); |
| + return true; |
| +} |
| + |
| +int32_t PepperIOStreamHost::OnResourceMessageReceived( |
| + const IPC::Message& msg, |
| + HostMessageContext* context) { |
| + IPC_BEGIN_MESSAGE_MAP(PepperIOStreamHost, msg) |
| + PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_IOStream_Init, |
| + OnHostMsgInit) |
| + PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_IOStream_MoveLimit, |
| + OnHostMsgMoveLimit) |
| + IPC_END_MESSAGE_MAP() |
| + return PP_ERROR_FAILED; |
| +} |
| + |
| +int32_t PepperIOStreamHost::OnHostMsgInit(HostMessageContext* context, |
| + uint32_t size) { |
| + if (!InitSharedBuffer(size)) |
| + return PP_ERROR_NOMEMORY; |
| + |
| + base::SharedMemoryHandle shm_handle = shared_memory()->handle(); |
| + base::PlatformFile platform_file = ToPlatformFile(shm_handle); |
| + // We need send the SerializedHandle back, so we have to send reply by |
| + // ourselves. |
|
bbudge
2014/01/02 22:16:22
This comment won't be needed if you have a reply m
Peng
2014/01/03 16:15:11
Done.
|
| + ReplyMessageContext reply_context = context->MakeReplyMessageContext(); |
| + HostDispatcher* dispatcher = HostDispatcher::GetForInstance(pp_instance()); |
| + SerializedHandle handle( |
| + dispatcher->ShareHandleWithRemote(platform_file, false), |
| + size); |
| + reply_context.params.AppendHandle(handle); |
| + reply_context.params.set_result(PP_OK); |
| + SendReply(reply_context, IPC::Message()); |
|
bbudge
2014/01/02 22:16:22
If a message requires a reply to get back some res
Peng
2014/01/03 16:15:11
Done.
|
| + |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t PepperIOStreamHost::OnHostMsgMoveLimit(HostMessageContext* context, |
| + uint32_t offset) { |
| + DCHECK(offset); |
| + MoveLimit(offset); |
| + return PP_OK; |
| +} |
| + |
| +} // namespace content |