Refactor android test results logging.
[chromium-blink-merge.git] / ppapi / proxy / ppb_buffer_proxy.cc
bloba8f814bb5b317214a8ed61a633fe5f547d208c33
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/proxy/ppb_buffer_proxy.h"
7 #include <vector>
9 #include "base/logging.h"
10 #include "build/build_config.h"
11 #include "ppapi/c/pp_completion_callback.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/c/pp_resource.h"
14 #include "ppapi/c/dev/ppb_buffer_dev.h"
15 #include "ppapi/proxy/host_dispatcher.h"
16 #include "ppapi/proxy/plugin_dispatcher.h"
17 #include "ppapi/proxy/ppapi_messages.h"
18 #include "ppapi/thunk/enter.h"
19 #include "ppapi/thunk/ppb_buffer_trusted_api.h"
20 #include "ppapi/thunk/resource_creation_api.h"
21 #include "ppapi/thunk/thunk.h"
23 namespace ppapi {
24 namespace proxy {
26 Buffer::Buffer(const HostResource& resource,
27 const base::SharedMemoryHandle& shm_handle,
28 uint32_t size)
29 : Resource(OBJECT_IS_PROXY, resource),
30 shm_(shm_handle, false),
31 size_(size),
32 map_count_(0) {
35 Buffer::~Buffer() {
36 Unmap();
39 thunk::PPB_Buffer_API* Buffer::AsPPB_Buffer_API() {
40 return this;
43 PP_Bool Buffer::Describe(uint32_t* size_in_bytes) {
44 *size_in_bytes = size_;
45 return PP_TRUE;
48 PP_Bool Buffer::IsMapped() {
49 return PP_FromBool(map_count_ > 0);
52 void* Buffer::Map() {
53 if (map_count_++ == 0)
54 shm_.Map(size_);
55 return shm_.memory();
58 void Buffer::Unmap() {
59 if (--map_count_ == 0)
60 shm_.Unmap();
63 PPB_Buffer_Proxy::PPB_Buffer_Proxy(Dispatcher* dispatcher)
64 : InterfaceProxy(dispatcher) {
67 PPB_Buffer_Proxy::~PPB_Buffer_Proxy() {
70 // static
71 PP_Resource PPB_Buffer_Proxy::CreateProxyResource(PP_Instance instance,
72 uint32_t size) {
73 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
74 if (!dispatcher)
75 return 0;
77 HostResource result;
78 ppapi::proxy::SerializedHandle shm_handle;
79 dispatcher->Send(new PpapiHostMsg_PPBBuffer_Create(
80 API_ID_PPB_BUFFER, instance, size, &result, &shm_handle));
81 if (result.is_null() || !shm_handle.IsHandleValid() ||
82 !shm_handle.is_shmem())
83 return 0;
85 return AddProxyResource(result, shm_handle.shmem(), size);
88 // static
89 PP_Resource PPB_Buffer_Proxy::AddProxyResource(
90 const HostResource& resource,
91 base::SharedMemoryHandle shm_handle,
92 uint32_t size) {
93 return (new Buffer(resource, shm_handle, size))->GetReference();
96 bool PPB_Buffer_Proxy::OnMessageReceived(const IPC::Message& msg) {
97 bool handled = true;
98 IPC_BEGIN_MESSAGE_MAP(PPB_Buffer_Proxy, msg)
99 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBuffer_Create, OnMsgCreate)
100 IPC_MESSAGE_UNHANDLED(handled = false)
101 IPC_END_MESSAGE_MAP()
102 // TODO(brettw) handle bad messages!
103 return handled;
106 void PPB_Buffer_Proxy::OnMsgCreate(
107 PP_Instance instance,
108 uint32_t size,
109 HostResource* result_resource,
110 ppapi::proxy::SerializedHandle* result_shm_handle) {
111 // Overwritten below on success.
112 result_shm_handle->set_null_shmem();
113 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance);
114 if (!dispatcher)
115 return;
116 if (!dispatcher->permissions().HasPermission(ppapi::PERMISSION_DEV))
117 return;
119 thunk::EnterResourceCreation enter(instance);
120 if (enter.failed())
121 return;
122 PP_Resource local_buffer_resource = enter.functions()->CreateBuffer(instance,
123 size);
124 if (local_buffer_resource == 0)
125 return;
127 thunk::EnterResourceNoLock<thunk::PPB_BufferTrusted_API> trusted_buffer(
128 local_buffer_resource, false);
129 if (trusted_buffer.failed())
130 return;
131 int local_fd;
132 if (trusted_buffer.object()->GetSharedMemory(&local_fd) != PP_OK)
133 return;
135 result_resource->SetHostResource(instance, local_buffer_resource);
137 // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle,
138 // those casts are ugly.
139 base::PlatformFile platform_file =
140 #if defined(OS_WIN)
141 reinterpret_cast<HANDLE>(static_cast<intptr_t>(local_fd));
142 #elif defined(OS_POSIX)
143 local_fd;
144 #else
145 #error Not implemented.
146 #endif
147 result_shm_handle->set_shmem(
148 dispatcher->ShareHandleWithRemote(platform_file, false), size);
151 } // namespace proxy
152 } // namespace ppapi