Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ppapi / proxy / ppb_image_data_proxy.h
blob6cd8ba31434041c1d766545cb12e581b513ce6e1
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 #ifndef PPAPI_PPB_IMAGE_DATA_PROXY_H_
6 #define PPAPI_PPB_IMAGE_DATA_PROXY_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/shared_memory.h"
10 #include "build/build_config.h"
11 #include "ipc/ipc_platform_file.h"
12 #include "ppapi/c/pp_bool.h"
13 #include "ppapi/c/pp_completion_callback.h"
14 #include "ppapi/c/pp_instance.h"
15 #include "ppapi/c/pp_module.h"
16 #include "ppapi/c/pp_resource.h"
17 #include "ppapi/c/pp_size.h"
18 #include "ppapi/c/pp_var.h"
19 #include "ppapi/c/ppb_image_data.h"
20 #include "ppapi/proxy/interface_proxy.h"
21 #include "ppapi/proxy/ppapi_proxy_export.h"
22 #include "ppapi/proxy/serialized_structs.h"
23 #include "ppapi/shared_impl/ppb_image_data_shared.h"
24 #include "ppapi/shared_impl/resource.h"
25 #include "ppapi/thunk/ppb_image_data_api.h"
27 class TransportDIB;
29 namespace ppapi {
30 namespace proxy {
32 class SerializedHandle;
34 // ImageData is an abstract base class for image data resources. Unlike most
35 // resources, ImageData must be public in the header since a number of other
36 // resources need to access it.
37 class PPAPI_PROXY_EXPORT ImageData
38 : public ppapi::Resource,
39 public NON_EXPORTED_BASE(ppapi::thunk::PPB_ImageData_API),
40 public ppapi::PPB_ImageData_Shared {
41 public:
42 ~ImageData() override;
44 // Resource overrides.
45 ppapi::thunk::PPB_ImageData_API* AsPPB_ImageData_API() override;
46 void LastPluginRefWasDeleted() override;
47 void InstanceWasDeleted() override;
49 // PPB_ImageData API.
50 PP_Bool Describe(PP_ImageDataDesc* desc) override;
51 int32_t GetSharedMemory(int* handle, uint32_t* byte_count) override;
52 void SetIsCandidateForReuse() override;
54 PPB_ImageData_Shared::ImageDataType type() const { return type_; }
55 const PP_ImageDataDesc& desc() const { return desc_; }
57 // Prepares this image data to be recycled to the plugin. Clears the contents
58 // if zero_contents is true.
59 void RecycleToPlugin(bool zero_contents);
61 protected:
62 ImageData(const ppapi::HostResource& resource,
63 PPB_ImageData_Shared::ImageDataType type,
64 const PP_ImageDataDesc& desc);
66 PPB_ImageData_Shared::ImageDataType type_;
67 PP_ImageDataDesc desc_;
69 // Set to true when this ImageData is a good candidate for reuse.
70 bool is_candidate_for_reuse_;
72 DISALLOW_COPY_AND_ASSIGN(ImageData);
75 // PlatformImageData is a full featured image data resource which can access
76 // the underlying platform-specific canvas and ImageHandle. This can't be used
77 // by NaCl apps.
78 #if !defined(OS_NACL)
79 class PPAPI_PROXY_EXPORT PlatformImageData : public ImageData {
80 public:
81 PlatformImageData(const ppapi::HostResource& resource,
82 const PP_ImageDataDesc& desc,
83 ImageHandle handle);
84 ~PlatformImageData() override;
86 // PPB_ImageData API.
87 void* Map() override;
88 void Unmap() override;
89 SkCanvas* GetPlatformCanvas() override;
90 SkCanvas* GetCanvas() override;
92 static ImageHandle NullHandle();
93 static ImageHandle HandleFromInt(int32_t i);
95 private:
96 scoped_ptr<TransportDIB> transport_dib_;
98 // Null when the image isn't mapped.
99 scoped_ptr<SkCanvas> mapped_canvas_;
101 DISALLOW_COPY_AND_ASSIGN(PlatformImageData);
103 #endif // !defined(OS_NACL)
105 // SimpleImageData is a simple, platform-independent image data resource which
106 // can be used by NaCl. It can also be used by trusted apps when access to the
107 // platform canvas isn't needed.
108 class PPAPI_PROXY_EXPORT SimpleImageData : public ImageData {
109 public:
110 SimpleImageData(const ppapi::HostResource& resource,
111 const PP_ImageDataDesc& desc,
112 const base::SharedMemoryHandle& handle);
113 ~SimpleImageData() override;
115 // PPB_ImageData API.
116 void* Map() override;
117 void Unmap() override;
118 SkCanvas* GetPlatformCanvas() override;
119 SkCanvas* GetCanvas() override;
121 private:
122 base::SharedMemory shm_;
123 uint32 size_;
124 int map_count_;
126 DISALLOW_COPY_AND_ASSIGN(SimpleImageData);
129 class PPB_ImageData_Proxy : public InterfaceProxy {
130 public:
131 PPB_ImageData_Proxy(Dispatcher* dispatcher);
132 ~PPB_ImageData_Proxy() override;
134 static PP_Resource CreateProxyResource(
135 PP_Instance instance,
136 PPB_ImageData_Shared::ImageDataType type,
137 PP_ImageDataFormat format,
138 const PP_Size& size,
139 PP_Bool init_to_zero);
141 // InterfaceProxy implementation.
142 bool OnMessageReceived(const IPC::Message& msg) override;
144 // Utility for creating ImageData resources.
145 // This can only be called on the host side of the proxy.
146 // On failure, will return invalid resource (0). On success it will return a
147 // valid resource and the out params will be written.
148 // |desc| contains the result of Describe.
149 // |image_handle| and |byte_count| contain the result of GetSharedMemory.
150 // NOTE: if |init_to_zero| is false, you should write over the entire image
151 // to avoid leaking sensitive data to a less privileged process.
152 PPAPI_PROXY_EXPORT static PP_Resource CreateImageData(
153 PP_Instance instance,
154 PPB_ImageData_Shared::ImageDataType type,
155 PP_ImageDataFormat format,
156 const PP_Size& size,
157 bool init_to_zero,
158 PP_ImageDataDesc* desc,
159 IPC::PlatformFileForTransit* image_handle,
160 uint32_t* byte_count);
162 static const ApiID kApiID = API_ID_PPB_IMAGE_DATA;
164 private:
165 // Plugin->Host message handlers.
166 void OnHostMsgCreatePlatform(
167 PP_Instance instance,
168 int32_t format,
169 const PP_Size& size,
170 PP_Bool init_to_zero,
171 HostResource* result,
172 PP_ImageDataDesc* desc,
173 ImageHandle* result_image_handle);
174 void OnHostMsgCreateSimple(
175 PP_Instance instance,
176 int32_t format,
177 const PP_Size& size,
178 PP_Bool init_to_zero,
179 HostResource* result,
180 PP_ImageDataDesc* desc,
181 ppapi::proxy::SerializedHandle* result_image_handle);
183 // Host->Plugin message handlers.
184 void OnPluginMsgNotifyUnusedImageData(const HostResource& old_image_data);
186 DISALLOW_COPY_AND_ASSIGN(PPB_ImageData_Proxy);
189 } // namespace proxy
190 } // namespace ppapi
192 #endif // PPAPI_PPB_IMAGE_DATA_PROXY_H_