Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(835)

Side by Side Diff: ppapi/proxy/ppb_image_data_proxy.cc

Issue 6334016: Refactor PPAPI proxy resource handling to maintain which host they came from,... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/proxy/ppb_image_data_proxy.h ('k') | ppapi/proxy/ppb_instance_proxy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_image_data_proxy.h" 5 #include "ppapi/proxy/ppb_image_data_proxy.h"
6 6
7 #include <string.h> // For memcpy 7 #include <string.h> // For memcpy
8 8
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 #include "ppapi/c/pp_completion_callback.h" 13 #include "ppapi/c/pp_completion_callback.h"
14 #include "ppapi/c/pp_errors.h" 14 #include "ppapi/c/pp_errors.h"
15 #include "ppapi/c/pp_resource.h" 15 #include "ppapi/c/pp_resource.h"
16 #include "ppapi/c/trusted/ppb_image_data_trusted.h" 16 #include "ppapi/c/trusted/ppb_image_data_trusted.h"
17 #include "ppapi/proxy/image_data.h" 17 #include "ppapi/proxy/host_resource.h"
18 #include "ppapi/proxy/plugin_dispatcher.h" 18 #include "ppapi/proxy/plugin_dispatcher.h"
19 #include "ppapi/proxy/plugin_resource.h"
19 #include "ppapi/proxy/plugin_resource_tracker.h" 20 #include "ppapi/proxy/plugin_resource_tracker.h"
20 #include "ppapi/proxy/ppapi_messages.h" 21 #include "ppapi/proxy/ppapi_messages.h"
22 #include "ppapi/shared_impl/image_data_impl.h"
23
24 #if defined(OS_LINUX)
25 #include <sys/shm.h>
26 #elif defined(OS_MACOSX)
27 #include <sys/stat.h>
28 #include <sys/mman.h>
29 #endif
21 30
22 namespace pp { 31 namespace pp {
23 namespace proxy { 32 namespace proxy {
24 33
34 class ImageData : public PluginResource,
35 public pp::shared_impl::ImageDataImpl {
36 public:
37 ImageData(const HostResource& resource,
38 const PP_ImageDataDesc& desc,
39 ImageHandle handle);
40 virtual ~ImageData();
41
42 // Resource overrides.
43 virtual ImageData* AsImageData();
44
45 void* Map();
46 void Unmap();
47
48 const PP_ImageDataDesc& desc() const { return desc_; }
49
50 static const ImageHandle NullHandle;
51 static ImageHandle HandleFromInt(int32_t i);
52
53 private:
54 PP_ImageDataDesc desc_;
55 ImageHandle handle_;
56
57 void* mapped_data_;
58
59 DISALLOW_COPY_AND_ASSIGN(ImageData);
60 };
61
62 ImageData::ImageData(const HostResource& resource,
63 const PP_ImageDataDesc& desc,
64 ImageHandle handle)
65 : PluginResource(resource),
66 desc_(desc),
67 handle_(handle),
68 mapped_data_(NULL) {
69 }
70
71 ImageData::~ImageData() {
72 Unmap();
73 }
74
75 ImageData* ImageData::AsImageData() {
76 return this;
77 }
78
79 void* ImageData::Map() {
80 #if defined(OS_WIN)
81 NOTIMPLEMENTED();
82 return NULL;
83 #elif defined(OS_MACOSX)
84 struct stat st;
85 if (fstat(handle_.fd, &st) != 0)
86 return NULL;
87 void* memory = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE,
88 MAP_SHARED, handle_.fd, 0);
89 if (memory == MAP_FAILED)
90 return NULL;
91 mapped_data_ = memory;
92 return mapped_data_;
93 #else
94 int shmkey = handle_;
95 void* address = shmat(shmkey, NULL, 0);
96 // Mark for deletion in case we crash so the kernel will clean it up.
97 shmctl(shmkey, IPC_RMID, 0);
98 if (address == (void*)-1)
99 return NULL;
100 mapped_data_ = address;
101 return address;
102 #endif
103 }
104
105 void ImageData::Unmap() {
106 #if defined(OS_WIN)
107 NOTIMPLEMENTED();
108 #elif defined(OS_MACOSX)
109 if (mapped_data_) {
110 struct stat st;
111 if (fstat(handle_.fd, &st) == 0)
112 munmap(mapped_data_, st.st_size);
113 }
114 #else
115 if (mapped_data_)
116 shmdt(mapped_data_);
117 #endif
118 mapped_data_ = NULL;
119 }
120
121 #if defined(OS_WIN)
122 const ImageHandle ImageData::NullHandle = NULL;
123 #elif defined(OS_MACOSX)
124 const ImageHandle ImageData::NullHandle = ImageHandle();
125 #else
126 const ImageHandle ImageData::NullHandle = 0;
127 #endif
128
129 ImageHandle ImageData::HandleFromInt(int32_t i) {
130 #if defined(OS_WIN)
131 return reinterpret_cast<ImageHandle>(i);
132 #elif defined(OS_MACOSX)
133 return ImageHandle(i, false);
134 #else
135 return static_cast<ImageHandle>(i);
136 #endif
137 }
138
25 namespace { 139 namespace {
26 140
27 PP_ImageDataFormat GetNativeImageDataFormat() { 141 PP_ImageDataFormat GetNativeImageDataFormat() {
28 return ImageData::GetNativeImageDataFormat(); 142 return ImageData::GetNativeImageDataFormat();
29 } 143 }
30 144
31 PP_Bool IsImageDataFormatSupported(PP_ImageDataFormat format) { 145 PP_Bool IsImageDataFormatSupported(PP_ImageDataFormat format) {
32 return BoolToPPBool(ImageData::IsImageDataFormatSupported(format)); 146 return BoolToPPBool(ImageData::IsImageDataFormatSupported(format));
33 } 147 }
34 148
35 PP_Resource Create(PP_Instance instance, 149 PP_Resource Create(PP_Instance instance,
36 PP_ImageDataFormat format, 150 PP_ImageDataFormat format,
37 const PP_Size* size, 151 const PP_Size* size,
38 PP_Bool init_to_zero) { 152 PP_Bool init_to_zero) {
39 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); 153 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
40 if (!dispatcher) 154 if (!dispatcher)
41 return PP_ERROR_BADARGUMENT; 155 return PP_ERROR_BADARGUMENT;
42 156
43 PP_Resource result = 0; 157 HostResource result;
44 std::string image_data_desc; 158 std::string image_data_desc;
45 ImageHandle image_handle = ImageData::NullHandle; 159 ImageHandle image_handle = ImageData::NullHandle;
46 dispatcher->Send(new PpapiHostMsg_PPBImageData_Create( 160 dispatcher->Send(new PpapiHostMsg_PPBImageData_Create(
47 INTERFACE_ID_PPB_IMAGE_DATA, instance, format, *size, init_to_zero, 161 INTERFACE_ID_PPB_IMAGE_DATA, instance, format, *size, init_to_zero,
48 &result, &image_data_desc, &image_handle)); 162 &result, &image_data_desc, &image_handle));
49 163
50 if (result && image_data_desc.size() == sizeof(PP_ImageDataDesc)) { 164 if (result.is_null() || image_data_desc.size() != sizeof(PP_ImageDataDesc))
51 // We serialize the PP_ImageDataDesc just by copying to a string. 165 return 0;
52 PP_ImageDataDesc desc;
53 memcpy(&desc, image_data_desc.data(), sizeof(PP_ImageDataDesc));
54 166
55 linked_ptr<ImageData> object(new ImageData(instance, desc, image_handle)); 167 // We serialize the PP_ImageDataDesc just by copying to a string.
56 PluginResourceTracker::GetInstance()->AddResource(result, object); 168 PP_ImageDataDesc desc;
57 } 169 memcpy(&desc, image_data_desc.data(), sizeof(PP_ImageDataDesc));
58 return result; 170
171 linked_ptr<ImageData> object(new ImageData(result, desc, image_handle));
172 return PluginResourceTracker::GetInstance()->AddResource(object);
59 } 173 }
60 174
61 PP_Bool IsImageData(PP_Resource resource) { 175 PP_Bool IsImageData(PP_Resource resource) {
62 ImageData* object = PluginResource::GetAs<ImageData>(resource); 176 ImageData* object = PluginResource::GetAs<ImageData>(resource);
63 return BoolToPPBool(!!object); 177 return BoolToPPBool(!!object);
64 } 178 }
65 179
66 PP_Bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) { 180 PP_Bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) {
67 ImageData* object = PluginResource::GetAs<ImageData>(resource); 181 ImageData* object = PluginResource::GetAs<ImageData>(resource);
68 if (!object) 182 if (!object)
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 IPC_MESSAGE_UNHANDLED(handled = false) 233 IPC_MESSAGE_UNHANDLED(handled = false)
120 IPC_END_MESSAGE_MAP() 234 IPC_END_MESSAGE_MAP()
121 // FIXME(brettw) handle bad messages! 235 // FIXME(brettw) handle bad messages!
122 return handled; 236 return handled;
123 } 237 }
124 238
125 void PPB_ImageData_Proxy::OnMsgCreate(PP_Instance instance, 239 void PPB_ImageData_Proxy::OnMsgCreate(PP_Instance instance,
126 int32_t format, 240 int32_t format,
127 const PP_Size& size, 241 const PP_Size& size,
128 PP_Bool init_to_zero, 242 PP_Bool init_to_zero,
129 PP_Resource* result, 243 HostResource* result,
130 std::string* image_data_desc, 244 std::string* image_data_desc,
131 ImageHandle* result_image_handle) { 245 ImageHandle* result_image_handle) {
132 *result = ppb_image_data_target()->Create( 246 PP_Resource resource = ppb_image_data_target()->Create(
133 instance, static_cast<PP_ImageDataFormat>(format), &size, init_to_zero); 247 instance, static_cast<PP_ImageDataFormat>(format), &size, init_to_zero);
134 *result_image_handle = ImageData::NullHandle; 248 *result_image_handle = ImageData::NullHandle;
135 if (*result) { 249 if (resource) {
136 // The ImageDesc is just serialized as a string. 250 // The ImageDesc is just serialized as a string.
137 PP_ImageDataDesc desc; 251 PP_ImageDataDesc desc;
138 if (ppb_image_data_target()->Describe(*result, &desc)) { 252 if (ppb_image_data_target()->Describe(resource, &desc)) {
139 image_data_desc->resize(sizeof(PP_ImageDataDesc)); 253 image_data_desc->resize(sizeof(PP_ImageDataDesc));
140 memcpy(&(*image_data_desc)[0], &desc, sizeof(PP_ImageDataDesc)); 254 memcpy(&(*image_data_desc)[0], &desc, sizeof(PP_ImageDataDesc));
141 } 255 }
142 256
143 // Get the shared memory handle. 257 // Get the shared memory handle.
144 const PPB_ImageDataTrusted* trusted = 258 const PPB_ImageDataTrusted* trusted =
145 reinterpret_cast<const PPB_ImageDataTrusted*>( 259 reinterpret_cast<const PPB_ImageDataTrusted*>(
146 dispatcher()->GetLocalInterface(PPB_IMAGEDATA_TRUSTED_INTERFACE)); 260 dispatcher()->GetLocalInterface(PPB_IMAGEDATA_TRUSTED_INTERFACE));
147 uint32_t byte_count = 0; 261 uint32_t byte_count = 0;
148 if (trusted) { 262 if (trusted) {
149 int32_t handle; 263 int32_t handle;
150 if (trusted->GetSharedMemory(*result, &handle, &byte_count) == PP_OK) 264 if (trusted->GetSharedMemory(resource, &handle, &byte_count) == PP_OK)
151 *result_image_handle = ImageData::HandleFromInt(handle); 265 *result_image_handle = ImageData::HandleFromInt(handle);
152 } 266 }
267
268 result->SetHostResource(instance, resource);
153 } 269 }
154 } 270 }
155 271
156 } // namespace proxy 272 } // namespace proxy
157 } // namespace pp 273 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/proxy/ppb_image_data_proxy.h ('k') | ppapi/proxy/ppb_instance_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698