Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ppapi / proxy / ppb_image_data_proxy.h
blob47dccc4e2f59f4c584b7caa95b7d25d902ae749f
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(base::SharedMemory** shm,
52 uint32_t* byte_count) override;
53 void SetIsCandidateForReuse() override;
55 PPB_ImageData_Shared::ImageDataType type() const { return type_; }
56 const PP_ImageDataDesc& desc() const { return desc_; }
58 // Prepares this image data to be recycled to the plugin. Clears the contents
59 // if zero_contents is true.
60 void RecycleToPlugin(bool zero_contents);
62 protected:
63 ImageData(const ppapi::HostResource& resource,
64 PPB_ImageData_Shared::ImageDataType type,
65 const PP_ImageDataDesc& desc);
67 PPB_ImageData_Shared::ImageDataType type_;
68 PP_ImageDataDesc desc_;
70 // Set to true when this ImageData is a good candidate for reuse.
71 bool is_candidate_for_reuse_;
73 DISALLOW_COPY_AND_ASSIGN(ImageData);
76 // PlatformImageData is a full featured image data resource which can access
77 // the underlying platform-specific canvas and ImageHandle. This can't be used
78 // by NaCl apps.
79 #if !defined(OS_NACL)
80 class PPAPI_PROXY_EXPORT PlatformImageData : public ImageData {
81 public:
82 PlatformImageData(const ppapi::HostResource& resource,
83 const PP_ImageDataDesc& desc,
84 ImageHandle handle);
85 ~PlatformImageData() override;
87 // PPB_ImageData API.
88 void* Map() override;
89 void Unmap() override;
90 SkCanvas* GetPlatformCanvas() override;
91 SkCanvas* GetCanvas() override;
93 static ImageHandle NullHandle();
94 static ImageHandle HandleFromInt(int32_t i);
96 private:
97 scoped_ptr<TransportDIB> transport_dib_;
99 // Null when the image isn't mapped.
100 scoped_ptr<SkCanvas> mapped_canvas_;
102 DISALLOW_COPY_AND_ASSIGN(PlatformImageData);
104 #endif // !defined(OS_NACL)
106 // SimpleImageData is a simple, platform-independent image data resource which
107 // can be used by NaCl. It can also be used by trusted apps when access to the
108 // platform canvas isn't needed.
109 class PPAPI_PROXY_EXPORT SimpleImageData : public ImageData {
110 public:
111 SimpleImageData(const ppapi::HostResource& resource,
112 const PP_ImageDataDesc& desc,
113 const base::SharedMemoryHandle& handle);
114 ~SimpleImageData() override;
116 // PPB_ImageData API.
117 void* Map() override;
118 void Unmap() override;
119 SkCanvas* GetPlatformCanvas() override;
120 SkCanvas* GetCanvas() override;
122 private:
123 base::SharedMemory shm_;
124 uint32 size_;
125 int map_count_;
127 DISALLOW_COPY_AND_ASSIGN(SimpleImageData);
130 class PPB_ImageData_Proxy : public InterfaceProxy {
131 public:
132 PPB_ImageData_Proxy(Dispatcher* dispatcher);
133 ~PPB_ImageData_Proxy() override;
135 static PP_Resource CreateProxyResource(
136 PP_Instance instance,
137 PPB_ImageData_Shared::ImageDataType type,
138 PP_ImageDataFormat format,
139 const PP_Size& size,
140 PP_Bool init_to_zero);
142 // InterfaceProxy implementation.
143 bool OnMessageReceived(const IPC::Message& msg) override;
145 // Utility for creating ImageData resources.
146 // This can only be called on the host side of the proxy.
147 // On failure, will return invalid resource (0). On success it will return a
148 // valid resource and the out params will be written.
149 // |desc| contains the result of Describe.
150 // |image_handle| and |byte_count| contain the result of GetSharedMemory.
151 // NOTE: if |init_to_zero| is false, you should write over the entire image
152 // to avoid leaking sensitive data to a less privileged process.
153 PPAPI_PROXY_EXPORT static PP_Resource CreateImageData(
154 PP_Instance instance,
155 PPB_ImageData_Shared::ImageDataType type,
156 PP_ImageDataFormat format,
157 const PP_Size& size,
158 bool init_to_zero,
159 PP_ImageDataDesc* desc,
160 base::SharedMemoryHandle* image_handle,
161 uint32_t* byte_count);
163 static const ApiID kApiID = API_ID_PPB_IMAGE_DATA;
165 private:
166 // Plugin->Host message handlers.
167 void OnHostMsgCreatePlatform(
168 PP_Instance instance,
169 int32_t format,
170 const PP_Size& size,
171 PP_Bool init_to_zero,
172 HostResource* result,
173 PP_ImageDataDesc* desc,
174 ImageHandle* result_image_handle);
175 void OnHostMsgCreateSimple(
176 PP_Instance instance,
177 int32_t format,
178 const PP_Size& size,
179 PP_Bool init_to_zero,
180 HostResource* result,
181 PP_ImageDataDesc* desc,
182 ppapi::proxy::SerializedHandle* result_image_handle);
184 // Host->Plugin message handlers.
185 void OnPluginMsgNotifyUnusedImageData(const HostResource& old_image_data);
187 DISALLOW_COPY_AND_ASSIGN(PPB_ImageData_Proxy);
190 } // namespace proxy
191 } // namespace ppapi
193 #endif // PPAPI_PPB_IMAGE_DATA_PROXY_H_