Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / ppb_buffer_impl.h
blobb2067809a2bbedbfb4d4c02415884537f2bb9d74
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 CONTENT_RENDERER_PEPPER_PPB_BUFFER_IMPL_H_
6 #define CONTENT_RENDERER_PEPPER_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/memory/shared_memory.h"
12 #include "ppapi/shared_impl/resource.h"
13 #include "ppapi/thunk/ppb_buffer_api.h"
15 namespace content {
17 class PPB_Buffer_Impl : public ppapi::Resource,
18 public ppapi::thunk::PPB_Buffer_API {
19 public:
20 static PP_Resource Create(PP_Instance instance, uint32_t size);
21 static scoped_refptr<PPB_Buffer_Impl> CreateResource(PP_Instance instance,
22 uint32_t size);
24 virtual PPB_Buffer_Impl* AsPPB_Buffer_Impl();
26 base::SharedMemory* shared_memory() const { return shared_memory_.get(); }
27 uint32_t size() const { return size_; }
29 // Resource overrides.
30 ppapi::thunk::PPB_Buffer_API* AsPPB_Buffer_API() override;
32 // PPB_Buffer_API implementation.
33 PP_Bool Describe(uint32_t* size_in_bytes) override;
34 PP_Bool IsMapped() override;
35 void* Map() override;
36 void Unmap() override;
38 // Trusted.
39 int32_t GetSharedMemory(int* handle) override;
41 private:
42 ~PPB_Buffer_Impl() override;
44 explicit PPB_Buffer_Impl(PP_Instance instance);
45 bool Init(uint32_t size);
47 scoped_ptr<base::SharedMemory> shared_memory_;
48 uint32_t size_;
49 int map_count_;
51 DISALLOW_COPY_AND_ASSIGN(PPB_Buffer_Impl);
54 // Ensures that the given buffer is mapped, and returns it to its previous
55 // mapped state in the destructor.
56 class BufferAutoMapper {
57 public:
58 explicit BufferAutoMapper(ppapi::thunk::PPB_Buffer_API* api);
59 ~BufferAutoMapper();
61 // Will be NULL on failure to map.
62 void* data() { return data_; }
63 uint32_t size() { return size_; }
65 private:
66 ppapi::thunk::PPB_Buffer_API* api_;
68 bool needs_unmap_;
70 void* data_;
71 uint32_t size_;
73 DISALLOW_COPY_AND_ASSIGN(BufferAutoMapper);
76 } // namespace content
78 #endif // CONTENT_RENDERER_PEPPER_PPB_BUFFER_IMPL_H_