Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / plugins / ppapi / ppb_buffer_impl.h
blob0bddd134240362a50e32de444b8038d97fad4846
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
3 // found in the LICENSE file.
5 #ifndef WEBKIT_PLUGINS_PPAPI_PPB_BUFFER_IMPL_H_
6 #define WEBKIT_PLUGINS_PPAPI_PPB_BUFFER_IMPL_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/shared_memory.h"
12 #include "ppapi/thunk/ppb_buffer_api.h"
13 #include "ppapi/shared_impl/resource.h"
14 #include "webkit/plugins/webkit_plugins_export.h"
16 namespace webkit {
17 namespace ppapi {
19 class WEBKIT_PLUGINS_EXPORT PPB_Buffer_Impl :
20 public ::ppapi::Resource,
21 public ::ppapi::thunk::PPB_Buffer_API {
22 public:
23 virtual ~PPB_Buffer_Impl();
25 static PP_Resource Create(PP_Instance instance, uint32_t size);
26 static scoped_refptr<PPB_Buffer_Impl> CreateResource(PP_Instance instance,
27 uint32_t size);
29 virtual PPB_Buffer_Impl* AsPPB_Buffer_Impl();
31 base::SharedMemory* shared_memory() const { return shared_memory_.get(); }
32 uint32_t size() const { return size_; }
34 // Resource overrides.
35 virtual ::ppapi::thunk::PPB_Buffer_API* AsPPB_Buffer_API() OVERRIDE;
37 // PPB_Buffer_API implementation.
38 virtual PP_Bool Describe(uint32_t* size_in_bytes) OVERRIDE;
39 virtual PP_Bool IsMapped() OVERRIDE;
40 virtual void* Map() OVERRIDE;
41 virtual void Unmap() OVERRIDE;
43 // Trusted.
44 virtual int32_t GetSharedMemory(int* handle) OVERRIDE;
46 private:
47 explicit PPB_Buffer_Impl(PP_Instance instance);
48 bool Init(uint32_t size);
50 scoped_ptr<base::SharedMemory> shared_memory_;
51 uint32_t size_;
52 int map_count_;
54 DISALLOW_COPY_AND_ASSIGN(PPB_Buffer_Impl);
57 // Ensures that the given buffer is mapped, and returns it to its previous
58 // mapped state in the destructor.
59 class BufferAutoMapper {
60 public:
61 explicit BufferAutoMapper(::ppapi::thunk::PPB_Buffer_API* api);
62 ~BufferAutoMapper();
64 // Will be NULL on failure to map.
65 void* data() { return data_; }
66 uint32_t size() { return size_; }
68 private:
69 ::ppapi::thunk::PPB_Buffer_API* api_;
71 bool needs_unmap_;
73 void* data_;
74 uint32_t size_;
76 DISALLOW_COPY_AND_ASSIGN(BufferAutoMapper);
79 } // namespace ppapi
80 } // namespace webkit
82 #endif // WEBKIT_PLUGINS_PPAPI_PPB_BUFFER_IMPL_H_