OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/ppb_buffer_proxy.h" | 5 #include "ppapi/proxy/ppb_buffer_proxy.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "ppapi/c/pp_completion_callback.h" | 11 #include "ppapi/c/pp_completion_callback.h" |
| 12 #include "ppapi/c/pp_errors.h" |
12 #include "ppapi/c/pp_resource.h" | 13 #include "ppapi/c/pp_resource.h" |
13 #include "ppapi/c/dev/ppb_buffer_dev.h" | 14 #include "ppapi/c/dev/ppb_buffer_dev.h" |
| 15 #include "ppapi/proxy/host_dispatcher.h" |
14 #include "ppapi/proxy/plugin_dispatcher.h" | 16 #include "ppapi/proxy/plugin_dispatcher.h" |
15 #include "ppapi/proxy/plugin_resource.h" | 17 #include "ppapi/proxy/plugin_resource.h" |
16 #include "ppapi/proxy/ppapi_messages.h" | 18 #include "ppapi/proxy/ppapi_messages.h" |
| 19 #include "ppapi/thunk/enter.h" |
17 #include "ppapi/thunk/ppb_buffer_api.h" | 20 #include "ppapi/thunk/ppb_buffer_api.h" |
| 21 #include "ppapi/thunk/ppb_buffer_trusted_api.h" |
18 #include "ppapi/thunk/thunk.h" | 22 #include "ppapi/thunk/thunk.h" |
19 | 23 |
20 namespace pp { | 24 namespace pp { |
21 namespace proxy { | 25 namespace proxy { |
22 | 26 |
23 namespace { | 27 namespace { |
24 | 28 |
25 InterfaceProxy* CreateBufferProxy(Dispatcher* dispatcher, | 29 InterfaceProxy* CreateBufferProxy(Dispatcher* dispatcher, |
26 const void* target_interface) { | 30 const void* target_interface) { |
27 return new PPB_Buffer_Proxy(dispatcher, target_interface); | 31 return new PPB_Buffer_Proxy(dispatcher, target_interface); |
28 } | 32 } |
29 | 33 |
30 } // namespace | 34 } // namespace |
31 | 35 |
32 class Buffer : public ppapi::thunk::PPB_Buffer_API, | 36 class Buffer : public ppapi::thunk::PPB_Buffer_API, |
33 public PluginResource { | 37 public PluginResource { |
34 public: | 38 public: |
35 Buffer(const HostResource& resource, | 39 Buffer(const HostResource& resource, |
36 int memory_handle, | 40 const base::SharedMemoryHandle& shm_handle, |
37 uint32_t size); | 41 uint32_t size); |
38 virtual ~Buffer(); | 42 virtual ~Buffer(); |
39 | 43 |
40 // Resource overrides. | 44 // Resource overrides. |
41 virtual Buffer* AsBuffer() OVERRIDE; | 45 virtual Buffer* AsBuffer() OVERRIDE; |
42 | 46 |
43 // ResourceObjectBase overries. | 47 // ResourceObjectBase overries. |
44 virtual ppapi::thunk::PPB_Buffer_API* AsPPB_Buffer_API() OVERRIDE; | 48 virtual ppapi::thunk::PPB_Buffer_API* AsPPB_Buffer_API() OVERRIDE; |
45 | 49 |
46 // PPB_Buffer_API implementation. | 50 // PPB_Buffer_API implementation. |
47 virtual PP_Bool Describe(uint32_t* size_in_bytes) OVERRIDE; | 51 virtual PP_Bool Describe(uint32_t* size_in_bytes) OVERRIDE; |
48 virtual PP_Bool IsMapped() OVERRIDE; | 52 virtual PP_Bool IsMapped() OVERRIDE; |
49 virtual void* Map() OVERRIDE; | 53 virtual void* Map() OVERRIDE; |
50 virtual void Unmap() OVERRIDE; | 54 virtual void Unmap() OVERRIDE; |
51 | 55 |
52 private: | 56 private: |
53 int memory_handle_; | 57 base::SharedMemory shm_; |
54 uint32_t size_; | 58 uint32_t size_; |
55 | |
56 void* mapped_data_; | 59 void* mapped_data_; |
57 | 60 |
58 DISALLOW_COPY_AND_ASSIGN(Buffer); | 61 DISALLOW_COPY_AND_ASSIGN(Buffer); |
59 }; | 62 }; |
60 | 63 |
61 Buffer::Buffer(const HostResource& resource, | 64 Buffer::Buffer(const HostResource& resource, |
62 int memory_handle, | 65 const base::SharedMemoryHandle& shm_handle, |
63 uint32_t size) | 66 uint32_t size) |
64 : PluginResource(resource), | 67 : PluginResource(resource), |
65 memory_handle_(memory_handle), | 68 shm_(shm_handle, false), |
66 size_(size), | 69 size_(size), |
67 mapped_data_(NULL) { | 70 mapped_data_(NULL) { |
68 } | 71 } |
69 | 72 |
70 Buffer::~Buffer() { | 73 Buffer::~Buffer() { |
71 Unmap(); | 74 Unmap(); |
72 } | 75 } |
73 | 76 |
74 Buffer* Buffer::AsBuffer() { | 77 Buffer* Buffer::AsBuffer() { |
75 return this; | 78 return this; |
76 } | 79 } |
77 | 80 |
78 ppapi::thunk::PPB_Buffer_API* Buffer::AsPPB_Buffer_API() { | 81 ppapi::thunk::PPB_Buffer_API* Buffer::AsPPB_Buffer_API() { |
79 return this; | 82 return this; |
80 } | 83 } |
81 | 84 |
82 PP_Bool Buffer::Describe(uint32_t* size_in_bytes) { | 85 PP_Bool Buffer::Describe(uint32_t* size_in_bytes) { |
83 *size_in_bytes = size_; | 86 *size_in_bytes = size_; |
84 return PP_TRUE; | 87 return PP_TRUE; |
85 } | 88 } |
86 | 89 |
87 PP_Bool Buffer::IsMapped() { | 90 PP_Bool Buffer::IsMapped() { |
88 return PP_FromBool(!!mapped_data_); | 91 return PP_FromBool(!!mapped_data_); |
89 } | 92 } |
90 | 93 |
91 void* Buffer::Map() { | 94 void* Buffer::Map() { |
92 // TODO(brettw) implement this. | 95 if (!shm_.memory()) |
93 return mapped_data_; | 96 shm_.Map(size_); |
| 97 return shm_.memory(); |
94 } | 98 } |
95 | 99 |
96 void Buffer::Unmap() { | 100 void Buffer::Unmap() { |
97 // TODO(brettw) implement this. | 101 shm_.Unmap(); |
98 } | 102 } |
99 | 103 |
100 PPB_Buffer_Proxy::PPB_Buffer_Proxy(Dispatcher* dispatcher, | 104 PPB_Buffer_Proxy::PPB_Buffer_Proxy(Dispatcher* dispatcher, |
101 const void* target_interface) | 105 const void* target_interface) |
102 : InterfaceProxy(dispatcher, target_interface) { | 106 : InterfaceProxy(dispatcher, target_interface) { |
103 } | 107 } |
104 | 108 |
105 PPB_Buffer_Proxy::~PPB_Buffer_Proxy() { | 109 PPB_Buffer_Proxy::~PPB_Buffer_Proxy() { |
106 } | 110 } |
107 | 111 |
(...skipping 10 matching lines...) Expand all Loading... |
118 } | 122 } |
119 | 123 |
120 // static | 124 // static |
121 PP_Resource PPB_Buffer_Proxy::CreateProxyResource(PP_Instance instance, | 125 PP_Resource PPB_Buffer_Proxy::CreateProxyResource(PP_Instance instance, |
122 uint32_t size) { | 126 uint32_t size) { |
123 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 127 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
124 if (!dispatcher) | 128 if (!dispatcher) |
125 return 0; | 129 return 0; |
126 | 130 |
127 HostResource result; | 131 HostResource result; |
128 int32_t shm_handle = -1; | 132 base::SharedMemoryHandle shm_handle = base::SharedMemory::NULLHandle(); |
129 dispatcher->Send(new PpapiHostMsg_PPBBuffer_Create( | 133 dispatcher->Send(new PpapiHostMsg_PPBBuffer_Create( |
130 INTERFACE_ID_PPB_BUFFER, instance, size, | 134 INTERFACE_ID_PPB_BUFFER, instance, size, |
131 &result, &shm_handle)); | 135 &result, &shm_handle)); |
132 if (result.is_null()) | 136 if (result.is_null() || !base::SharedMemory::IsHandleValid(shm_handle)) |
133 return 0; | 137 return 0; |
134 | 138 |
135 linked_ptr<Buffer> object(new Buffer(result, | 139 linked_ptr<Buffer> object(new Buffer(result, shm_handle, size)); |
136 static_cast<int>(shm_handle), size)); | |
137 return PluginResourceTracker::GetInstance()->AddResource(object); | 140 return PluginResourceTracker::GetInstance()->AddResource(object); |
138 } | 141 } |
139 | 142 |
140 bool PPB_Buffer_Proxy::OnMessageReceived(const IPC::Message& msg) { | 143 bool PPB_Buffer_Proxy::OnMessageReceived(const IPC::Message& msg) { |
141 bool handled = true; | 144 bool handled = true; |
142 IPC_BEGIN_MESSAGE_MAP(PPB_Buffer_Proxy, msg) | 145 IPC_BEGIN_MESSAGE_MAP(PPB_Buffer_Proxy, msg) |
143 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBuffer_Create, OnMsgCreate) | 146 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBBuffer_Create, OnMsgCreate) |
144 IPC_MESSAGE_UNHANDLED(handled = false) | 147 IPC_MESSAGE_UNHANDLED(handled = false) |
145 IPC_END_MESSAGE_MAP() | 148 IPC_END_MESSAGE_MAP() |
146 // TODO(brettw) handle bad messages! | 149 // TODO(brettw) handle bad messages! |
147 return handled; | 150 return handled; |
148 } | 151 } |
149 | 152 |
150 void PPB_Buffer_Proxy::OnMsgCreate(PP_Instance instance, | 153 void PPB_Buffer_Proxy::OnMsgCreate( |
151 uint32_t size, | 154 PP_Instance instance, |
152 HostResource* result_resource, | 155 uint32_t size, |
153 int* result_shm_handle) { | 156 HostResource* result_resource, |
154 result_resource->SetHostResource( | 157 base::SharedMemoryHandle* result_shm_handle) { |
155 instance, | 158 // Overwritten below on success. |
156 ppb_buffer_target()->Create(instance, size)); | 159 *result_shm_handle = base::SharedMemory::NULLHandle(); |
157 // TODO(brettw) set the shm handle from a trusted interface. | 160 HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance); |
158 *result_shm_handle = 0; | 161 if (!dispatcher) |
| 162 return; |
| 163 PP_Resource local_buffer_resource = |
| 164 ppb_buffer_target()->Create(instance, size); |
| 165 if (local_buffer_resource == 0) |
| 166 return; |
| 167 ::ppapi::thunk::EnterResourceNoLock< ::ppapi::thunk::PPB_BufferTrusted_API> |
| 168 trusted_buffer(local_buffer_resource, false); |
| 169 if (trusted_buffer.failed()) |
| 170 return; |
| 171 int local_fd; |
| 172 if (trusted_buffer.object()->GetSharedMemory(&local_fd) != PP_OK) |
| 173 return; |
| 174 |
| 175 result_resource->SetHostResource(instance, local_buffer_resource); |
| 176 |
| 177 // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle, |
| 178 // those casts are ugly. |
| 179 base::PlatformFile platform_file = |
| 180 #if defined(OS_WIN) |
| 181 reinterpret_cast<HANDLE>(static_cast<intptr_t>(local_fd)); |
| 182 #elif defined(OS_POSIX) |
| 183 local_fd; |
| 184 #else |
| 185 #error Not implemented. |
| 186 #endif |
| 187 *result_shm_handle = dispatcher->ShareHandleWithRemote(platform_file, false); |
159 } | 188 } |
160 | 189 |
161 } // namespace proxy | 190 } // namespace proxy |
162 } // namespace pp | 191 } // namespace pp |
OLD | NEW |