blob: 5f597637c186817752c68806bd3cca07768a8a13 [file] [log] [blame]
[email protected]00d320a2012-02-14 00:27:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]43a40202010-11-12 16:25:012// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/proxy/ppb_buffer_proxy.h"
6
7#include <vector>
8
9#include "base/logging.h"
10#include "build/build_config.h"
11#include "ppapi/c/pp_completion_callback.h"
[email protected]32523382011-06-10 02:30:0012#include "ppapi/c/pp_errors.h"
[email protected]43a40202010-11-12 16:25:0113#include "ppapi/c/pp_resource.h"
14#include "ppapi/c/dev/ppb_buffer_dev.h"
[email protected]32523382011-06-10 02:30:0015#include "ppapi/proxy/host_dispatcher.h"
[email protected]43a40202010-11-12 16:25:0116#include "ppapi/proxy/plugin_dispatcher.h"
[email protected]43a40202010-11-12 16:25:0117#include "ppapi/proxy/ppapi_messages.h"
[email protected]32523382011-06-10 02:30:0018#include "ppapi/thunk/enter.h"
[email protected]5c966022011-09-13 18:09:3719#include "ppapi/thunk/resource_creation_api.h"
[email protected]9815108e2011-05-27 21:50:2820#include "ppapi/thunk/thunk.h"
[email protected]43a40202010-11-12 16:25:0121
[email protected]4d2efd22011-08-18 21:58:0222namespace ppapi {
[email protected]43a40202010-11-12 16:25:0123namespace proxy {
24
[email protected]f24448db2011-01-27 20:40:3925Buffer::Buffer(const HostResource& resource,
[email protected]32523382011-06-10 02:30:0026 const base::SharedMemoryHandle& shm_handle,
[email protected]f24448db2011-01-27 20:40:3927 uint32_t size)
[email protected]00d320a2012-02-14 00:27:0428 : Resource(OBJECT_IS_PROXY, resource),
[email protected]32523382011-06-10 02:30:0029 shm_(shm_handle, false),
[email protected]43a40202010-11-12 16:25:0130 size_(size),
[email protected]81fc59f2011-06-14 19:28:4131 map_count_(0) {
[email protected]43a40202010-11-12 16:25:0132}
33
34Buffer::~Buffer() {
35 Unmap();
36}
37
[email protected]4d2efd22011-08-18 21:58:0238thunk::PPB_Buffer_API* Buffer::AsPPB_Buffer_API() {
[email protected]9815108e2011-05-27 21:50:2839 return this;
40}
41
42PP_Bool Buffer::Describe(uint32_t* size_in_bytes) {
43 *size_in_bytes = size_;
44 return PP_TRUE;
45}
46
47PP_Bool Buffer::IsMapped() {
[email protected]16b7b272012-07-25 21:54:4948 return PP_FromBool(map_count_ > 0);
[email protected]9815108e2011-05-27 21:50:2849}
50
[email protected]43a40202010-11-12 16:25:0151void* Buffer::Map() {
[email protected]81fc59f2011-06-14 19:28:4152 if (map_count_++ == 0)
[email protected]32523382011-06-10 02:30:0053 shm_.Map(size_);
54 return shm_.memory();
[email protected]43a40202010-11-12 16:25:0155}
56
57void Buffer::Unmap() {
[email protected]81fc59f2011-06-14 19:28:4158 if (--map_count_ == 0)
59 shm_.Unmap();
[email protected]43a40202010-11-12 16:25:0160}
61
erikchen4fc32d52015-06-02 02:16:3862int32_t Buffer::GetSharedMemory(base::SharedMemory** out_handle) {
[email protected]e3da4e802013-03-23 23:50:4963 NOTREACHED();
64 return PP_ERROR_NOTSUPPORTED;
65}
66
[email protected]5c966022011-09-13 18:09:3767PPB_Buffer_Proxy::PPB_Buffer_Proxy(Dispatcher* dispatcher)
68 : InterfaceProxy(dispatcher) {
[email protected]43a40202010-11-12 16:25:0169}
70
71PPB_Buffer_Proxy::~PPB_Buffer_Proxy() {
72}
73
[email protected]465faa22011-02-08 16:31:4674// static
[email protected]9815108e2011-05-27 21:50:2875PP_Resource PPB_Buffer_Proxy::CreateProxyResource(PP_Instance instance,
76 uint32_t size) {
77 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
78 if (!dispatcher)
79 return 0;
80
81 HostResource result;
[email protected]246fc492012-08-27 20:28:1882 ppapi::proxy::SerializedHandle shm_handle;
[email protected]9815108e2011-05-27 21:50:2883 dispatcher->Send(new PpapiHostMsg_PPBBuffer_Create(
[email protected]246fc492012-08-27 20:28:1884 API_ID_PPB_BUFFER, instance, size, &result, &shm_handle));
85 if (result.is_null() || !shm_handle.IsHandleValid() ||
86 !shm_handle.is_shmem())
[email protected]9815108e2011-05-27 21:50:2887 return 0;
88
[email protected]246fc492012-08-27 20:28:1889 return AddProxyResource(result, shm_handle.shmem(), size);
[email protected]0fa46e82011-08-09 07:31:4990}
91
92// static
93PP_Resource PPB_Buffer_Proxy::AddProxyResource(
94 const HostResource& resource,
95 base::SharedMemoryHandle shm_handle,
96 uint32_t size) {
[email protected]7f8b26b2011-08-18 15:41:0197 return (new Buffer(resource, shm_handle, size))->GetReference();
[email protected]9815108e2011-05-27 21:50:2898}
99
[email protected]a95986a82010-12-24 06:19:28100bool PPB_Buffer_Proxy::OnMessageReceived(const IPC::Message& msg) {
101 bool handled = true;
[email protected]43a40202010-11-12 16:25:01102 IPC_BEGIN_MESSAGE_MAP(PPB_Buffer_Proxy, msg)
103 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBuffer_Create, OnMsgCreate)
[email protected]a95986a82010-12-24 06:19:28104 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]43a40202010-11-12 16:25:01105 IPC_END_MESSAGE_MAP()
106 // TODO(brettw) handle bad messages!
[email protected]a95986a82010-12-24 06:19:28107 return handled;
[email protected]43a40202010-11-12 16:25:01108}
109
[email protected]32523382011-06-10 02:30:00110void PPB_Buffer_Proxy::OnMsgCreate(
111 PP_Instance instance,
112 uint32_t size,
113 HostResource* result_resource,
[email protected]246fc492012-08-27 20:28:18114 ppapi::proxy::SerializedHandle* result_shm_handle) {
[email protected]32523382011-06-10 02:30:00115 // Overwritten below on success.
[email protected]246fc492012-08-27 20:28:18116 result_shm_handle->set_null_shmem();
[email protected]32523382011-06-10 02:30:00117 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance);
118 if (!dispatcher)
119 return;
[email protected]7df06972012-12-08 06:40:33120 if (!dispatcher->permissions().HasPermission(ppapi::PERMISSION_DEV))
121 return;
[email protected]5c966022011-09-13 18:09:37122
123 thunk::EnterResourceCreation enter(instance);
124 if (enter.failed())
125 return;
126 PP_Resource local_buffer_resource = enter.functions()->CreateBuffer(instance,
127 size);
[email protected]32523382011-06-10 02:30:00128 if (local_buffer_resource == 0)
129 return;
[email protected]5c966022011-09-13 18:09:37130
[email protected]e3da4e802013-03-23 23:50:49131 thunk::EnterResourceNoLock<thunk::PPB_Buffer_API> trusted_buffer(
[email protected]4d2efd22011-08-18 21:58:02132 local_buffer_resource, false);
[email protected]32523382011-06-10 02:30:00133 if (trusted_buffer.failed())
134 return;
erikchen4fc32d52015-06-02 02:16:38135 base::SharedMemory* local_shm;
136 if (trusted_buffer.object()->GetSharedMemory(&local_shm) != PP_OK)
[email protected]32523382011-06-10 02:30:00137 return;
138
139 result_resource->SetHostResource(instance, local_buffer_resource);
140
[email protected]246fc492012-08-27 20:28:18141 result_shm_handle->set_shmem(
erikchen4fc32d52015-06-02 02:16:38142 dispatcher->ShareSharedMemoryHandleWithRemote(local_shm->handle()), size);
[email protected]43a40202010-11-12 16:25:01143}
144
145} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02146} // namespace ppapi