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. 4 5 #ifndef PPAPI_PPB_IMAGE_DATA_PROXY_H_ 6 #define PPAPI_PPB_IMAGE_DATA_PROXY_H_ 7 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" 26 27 class TransportDIB; 28 29 namespace ppapi { 30 namespace proxy { 31 32 class SerializedHandle; 33 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 virtual ~ImageData(); 43 44 // Resource overrides. 45 virtual ppapi::thunk::PPB_ImageData_API* AsPPB_ImageData_API() OVERRIDE; 46 virtual void LastPluginRefWasDeleted() OVERRIDE; 47 virtual void InstanceWasDeleted() OVERRIDE; 48 49 // PPB_ImageData API. 50 virtual PP_Bool Describe(PP_ImageDataDesc* desc) OVERRIDE; 51 virtual int32_t GetSharedMemory(int* handle, uint32_t* byte_count) OVERRIDE; 52 virtual void SetIsCandidateForReuse() OVERRIDE; 53 type()54 PPB_ImageData_Shared::ImageDataType type() const { return type_; } desc()55 const PP_ImageDataDesc& desc() const { return desc_; } 56 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); 60 61 protected: 62 ImageData(const ppapi::HostResource& resource, 63 PPB_ImageData_Shared::ImageDataType type, 64 const PP_ImageDataDesc& desc); 65 66 PPB_ImageData_Shared::ImageDataType type_; 67 PP_ImageDataDesc desc_; 68 69 // Set to true when this ImageData is a good candidate for reuse. 70 bool is_candidate_for_reuse_; 71 72 DISALLOW_COPY_AND_ASSIGN(ImageData); 73 }; 74 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 virtual ~PlatformImageData(); 85 86 // PPB_ImageData API. 87 virtual void* Map() OVERRIDE; 88 virtual void Unmap() OVERRIDE; 89 virtual SkCanvas* GetPlatformCanvas() OVERRIDE; 90 virtual SkCanvas* GetCanvas() OVERRIDE; 91 92 static ImageHandle NullHandle(); 93 static ImageHandle HandleFromInt(int32_t i); 94 95 private: 96 scoped_ptr<TransportDIB> transport_dib_; 97 98 // Null when the image isn't mapped. 99 scoped_ptr<SkCanvas> mapped_canvas_; 100 101 DISALLOW_COPY_AND_ASSIGN(PlatformImageData); 102 }; 103 #endif // !defined(OS_NACL) 104 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 virtual ~SimpleImageData(); 114 115 // PPB_ImageData API. 116 virtual void* Map() OVERRIDE; 117 virtual void Unmap() OVERRIDE; 118 virtual SkCanvas* GetPlatformCanvas() OVERRIDE; 119 virtual SkCanvas* GetCanvas() OVERRIDE; 120 121 private: 122 base::SharedMemory shm_; 123 uint32 size_; 124 int map_count_; 125 126 DISALLOW_COPY_AND_ASSIGN(SimpleImageData); 127 }; 128 129 class PPB_ImageData_Proxy : public InterfaceProxy { 130 public: 131 PPB_ImageData_Proxy(Dispatcher* dispatcher); 132 virtual ~PPB_ImageData_Proxy(); 133 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); 140 141 // InterfaceProxy implementation. 142 virtual bool OnMessageReceived(const IPC::Message& msg); 143 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); 161 162 static const ApiID kApiID = API_ID_PPB_IMAGE_DATA; 163 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); 182 183 // Host->Plugin message handlers. 184 void OnPluginMsgNotifyUnusedImageData(const HostResource& old_image_data); 185 186 DISALLOW_COPY_AND_ASSIGN(PPB_ImageData_Proxy); 187 }; 188 189 } // namespace proxy 190 } // namespace ppapi 191 192 #endif // PPAPI_PPB_IMAGE_DATA_PROXY_H_ 193