Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 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/pepper_io_stream_host.h" | |
| 6 | |
| 7 #include "content/public/renderer/render_thread.h" | |
| 8 #include "content/public/renderer/renderer_ppapi_host.h" | |
| 9 #include "ppapi/c/pp_errors.h" | |
| 10 #include "ppapi/host/dispatch_host_message.h" | |
| 11 #include "ppapi/host/ppapi_host.h" | |
| 12 #include "ppapi/proxy/host_dispatcher.h" | |
| 13 #include "ppapi/proxy/ppapi_messages.h" | |
| 14 | |
| 15 | |
| 16 using ppapi::host::HostMessageContext; | |
| 17 using ppapi::host::ReplyMessageContext; | |
| 18 using ppapi::proxy::HostDispatcher; | |
| 19 using ppapi::proxy::SerializedHandle; | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 base::PlatformFile ToPlatformFile(const base::SharedMemoryHandle& handle) { | |
| 26 #if defined(OS_WIN) | |
| 27 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); | |
| 28 #elif defined(OS_POSIX) | |
| 29 return handle.fd; | |
| 30 #else | |
| 31 #error Not implemented. | |
| 32 #endif | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 PepperIOStreamHost::PepperIOStreamHost( | |
| 38 RendererPpapiHost* host, | |
| 39 PP_Instance instance, | |
| 40 PP_Resource resource, | |
| 41 bool is_input) | |
| 42 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
| 43 ppapi::IOStreamShared(is_input) { | |
| 44 } | |
| 45 | |
| 46 PepperIOStreamHost::~PepperIOStreamHost() { | |
| 47 } | |
| 48 | |
| 49 void PepperIOStreamHost::MovePeerLimit(uint32_t offset) { | |
| 50 CHECK(offset); | |
|
bbudge
2014/01/02 22:16:22
DCHECK?
Peng
2014/01/03 16:15:11
Done.
| |
| 51 host()->SendUnsolicitedReply(pp_resource(), | |
| 52 PpapiPluginMsg_IOStream_MoveLimit(offset)); | |
| 53 } | |
| 54 | |
| 55 bool PepperIOStreamHost::InitSharedBufferAndNotifyPlugin(uint32_t size) { | |
| 56 if (!InitSharedBuffer(size)) | |
| 57 return false; | |
| 58 | |
| 59 base::SharedMemoryHandle shm_handle = shared_memory()->handle(); | |
| 60 base::PlatformFile platform_file = ToPlatformFile(shm_handle); | |
| 61 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(pp_instance()); | |
| 62 SerializedHandle handle( | |
| 63 dispatcher->ShareHandleWithRemote(platform_file, false), | |
| 64 size); | |
| 65 host()->SendUnsolicitedReplyWithHandle(pp_resource(), | |
| 66 PpapiPluginMsg_IOStream_Init(size), handle); | |
| 67 return true; | |
| 68 } | |
| 69 | |
| 70 bool PepperIOStreamHost::InitSharedBuffer(uint32_t size) { | |
| 71 DCHECK(size); | |
| 72 content::RenderThread* render_thread = content::RenderThread::Get(); | |
| 73 scoped_ptr<base::SharedMemory> shm; | |
| 74 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
| |
| 75 if (!shm) | |
| 76 return false; | |
| 77 | |
| 78 SetBuffer(shm.Pass(), size); | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 int32_t PepperIOStreamHost::OnResourceMessageReceived( | |
| 83 const IPC::Message& msg, | |
| 84 HostMessageContext* context) { | |
| 85 IPC_BEGIN_MESSAGE_MAP(PepperIOStreamHost, msg) | |
| 86 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_IOStream_Init, | |
| 87 OnHostMsgInit) | |
| 88 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_IOStream_MoveLimit, | |
| 89 OnHostMsgMoveLimit) | |
| 90 IPC_END_MESSAGE_MAP() | |
| 91 return PP_ERROR_FAILED; | |
| 92 } | |
| 93 | |
| 94 int32_t PepperIOStreamHost::OnHostMsgInit(HostMessageContext* context, | |
| 95 uint32_t size) { | |
| 96 if (!InitSharedBuffer(size)) | |
| 97 return PP_ERROR_NOMEMORY; | |
| 98 | |
| 99 base::SharedMemoryHandle shm_handle = shared_memory()->handle(); | |
| 100 base::PlatformFile platform_file = ToPlatformFile(shm_handle); | |
| 101 // We need send the SerializedHandle back, so we have to send reply by | |
| 102 // 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.
| |
| 103 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); | |
| 104 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(pp_instance()); | |
| 105 SerializedHandle handle( | |
| 106 dispatcher->ShareHandleWithRemote(platform_file, false), | |
| 107 size); | |
| 108 reply_context.params.AppendHandle(handle); | |
| 109 reply_context.params.set_result(PP_OK); | |
| 110 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.
| |
| 111 | |
| 112 return PP_OK_COMPLETIONPENDING; | |
| 113 } | |
| 114 | |
| 115 int32_t PepperIOStreamHost::OnHostMsgMoveLimit(HostMessageContext* context, | |
| 116 uint32_t offset) { | |
| 117 DCHECK(offset); | |
| 118 MoveLimit(offset); | |
| 119 return PP_OK; | |
| 120 } | |
| 121 | |
| 122 } // namespace content | |
| OLD | NEW |