[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 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 "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] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 12 | #include "ppapi/c/pp_errors.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 13 | #include "ppapi/c/pp_resource.h" |
| 14 | #include "ppapi/c/dev/ppb_buffer_dev.h" |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 15 | #include "ppapi/proxy/host_dispatcher.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 16 | #include "ppapi/proxy/plugin_dispatcher.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 17 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 18 | #include "ppapi/thunk/enter.h" |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 19 | #include "ppapi/thunk/resource_creation_api.h" |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 20 | #include "ppapi/thunk/thunk.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 21 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 22 | namespace ppapi { |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 23 | namespace proxy { |
| 24 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 25 | Buffer::Buffer(const HostResource& resource, |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 26 | const base::SharedMemoryHandle& shm_handle, |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 27 | uint32_t size) |
[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 28 | : Resource(OBJECT_IS_PROXY, resource), |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 29 | shm_(shm_handle, false), |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 30 | size_(size), |
[email protected] | 81fc59f | 2011-06-14 19:28:41 | [diff] [blame] | 31 | map_count_(0) { |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | Buffer::~Buffer() { |
| 35 | Unmap(); |
| 36 | } |
| 37 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 38 | thunk::PPB_Buffer_API* Buffer::AsPPB_Buffer_API() { |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 39 | return this; |
| 40 | } |
| 41 | |
| 42 | PP_Bool Buffer::Describe(uint32_t* size_in_bytes) { |
| 43 | *size_in_bytes = size_; |
| 44 | return PP_TRUE; |
| 45 | } |
| 46 | |
| 47 | PP_Bool Buffer::IsMapped() { |
[email protected] | 16b7b27 | 2012-07-25 21:54:49 | [diff] [blame] | 48 | return PP_FromBool(map_count_ > 0); |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 51 | void* Buffer::Map() { |
[email protected] | 81fc59f | 2011-06-14 19:28:41 | [diff] [blame] | 52 | if (map_count_++ == 0) |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 53 | shm_.Map(size_); |
| 54 | return shm_.memory(); |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void Buffer::Unmap() { |
[email protected] | 81fc59f | 2011-06-14 19:28:41 | [diff] [blame] | 58 | if (--map_count_ == 0) |
| 59 | shm_.Unmap(); |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 60 | } |
| 61 | |
erikchen | 4fc32d5 | 2015-06-02 02:16:38 | [diff] [blame] | 62 | int32_t Buffer::GetSharedMemory(base::SharedMemory** out_handle) { |
[email protected] | e3da4e80 | 2013-03-23 23:50:49 | [diff] [blame] | 63 | NOTREACHED(); |
| 64 | return PP_ERROR_NOTSUPPORTED; |
| 65 | } |
| 66 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 67 | PPB_Buffer_Proxy::PPB_Buffer_Proxy(Dispatcher* dispatcher) |
| 68 | : InterfaceProxy(dispatcher) { |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | PPB_Buffer_Proxy::~PPB_Buffer_Proxy() { |
| 72 | } |
| 73 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 74 | // static |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 75 | PP_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] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 82 | ppapi::proxy::SerializedHandle shm_handle; |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 83 | dispatcher->Send(new PpapiHostMsg_PPBBuffer_Create( |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 84 | API_ID_PPB_BUFFER, instance, size, &result, &shm_handle)); |
| 85 | if (result.is_null() || !shm_handle.IsHandleValid() || |
| 86 | !shm_handle.is_shmem()) |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 87 | return 0; |
| 88 | |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 89 | return AddProxyResource(result, shm_handle.shmem(), size); |
[email protected] | 0fa46e8 | 2011-08-09 07:31:49 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // static |
| 93 | PP_Resource PPB_Buffer_Proxy::AddProxyResource( |
| 94 | const HostResource& resource, |
| 95 | base::SharedMemoryHandle shm_handle, |
| 96 | uint32_t size) { |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 97 | return (new Buffer(resource, shm_handle, size))->GetReference(); |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 100 | bool PPB_Buffer_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 101 | bool handled = true; |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 102 | IPC_BEGIN_MESSAGE_MAP(PPB_Buffer_Proxy, msg) |
| 103 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBuffer_Create, OnMsgCreate) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 104 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 105 | IPC_END_MESSAGE_MAP() |
| 106 | // TODO(brettw) handle bad messages! |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 107 | return handled; |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 110 | void PPB_Buffer_Proxy::OnMsgCreate( |
| 111 | PP_Instance instance, |
| 112 | uint32_t size, |
| 113 | HostResource* result_resource, |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 114 | ppapi::proxy::SerializedHandle* result_shm_handle) { |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 115 | // Overwritten below on success. |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 116 | result_shm_handle->set_null_shmem(); |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 117 | HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance); |
| 118 | if (!dispatcher) |
| 119 | return; |
[email protected] | 7df0697 | 2012-12-08 06:40:33 | [diff] [blame] | 120 | if (!dispatcher->permissions().HasPermission(ppapi::PERMISSION_DEV)) |
| 121 | return; |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 122 | |
| 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] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 128 | if (local_buffer_resource == 0) |
| 129 | return; |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 130 | |
[email protected] | e3da4e80 | 2013-03-23 23:50:49 | [diff] [blame] | 131 | thunk::EnterResourceNoLock<thunk::PPB_Buffer_API> trusted_buffer( |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 132 | local_buffer_resource, false); |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 133 | if (trusted_buffer.failed()) |
| 134 | return; |
erikchen | 4fc32d5 | 2015-06-02 02:16:38 | [diff] [blame] | 135 | base::SharedMemory* local_shm; |
| 136 | if (trusted_buffer.object()->GetSharedMemory(&local_shm) != PP_OK) |
[email protected] | 3252338 | 2011-06-10 02:30:00 | [diff] [blame] | 137 | return; |
| 138 | |
| 139 | result_resource->SetHostResource(instance, local_buffer_resource); |
| 140 | |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 141 | result_shm_handle->set_shmem( |
erikchen | 4fc32d5 | 2015-06-02 02:16:38 | [diff] [blame] | 142 | dispatcher->ShareSharedMemoryHandleWithRemote(local_shm->handle()), size); |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 146 | } // namespace ppapi |