Home | History | Annotate | Download | only in pepper
      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.
      4 
      5 #ifndef CONTENT_RENDERER_PEPPER_PPB_BUFFER_IMPL_H_
      6 #define CONTENT_RENDERER_PEPPER_PPB_BUFFER_IMPL_H_
      7 
      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"
     14 
     15 namespace content {
     16 
     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);
     23 
     24   virtual PPB_Buffer_Impl* AsPPB_Buffer_Impl();
     25 
     26   base::SharedMemory* shared_memory() const { return shared_memory_.get(); }
     27   uint32_t size() const { return size_; }
     28 
     29   // Resource overrides.
     30   virtual ppapi::thunk::PPB_Buffer_API* AsPPB_Buffer_API() OVERRIDE;
     31 
     32   // PPB_Buffer_API implementation.
     33   virtual PP_Bool Describe(uint32_t* size_in_bytes) OVERRIDE;
     34   virtual PP_Bool IsMapped() OVERRIDE;
     35   virtual void* Map() OVERRIDE;
     36   virtual void Unmap() OVERRIDE;
     37 
     38   // Trusted.
     39   virtual int32_t GetSharedMemory(int* handle) OVERRIDE;
     40 
     41  private:
     42   virtual ~PPB_Buffer_Impl();
     43 
     44   explicit PPB_Buffer_Impl(PP_Instance instance);
     45   bool Init(uint32_t size);
     46 
     47   scoped_ptr<base::SharedMemory> shared_memory_;
     48   uint32_t size_;
     49   int map_count_;
     50 
     51   DISALLOW_COPY_AND_ASSIGN(PPB_Buffer_Impl);
     52 };
     53 
     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();
     60 
     61   // Will be NULL on failure to map.
     62   void* data() { return data_; }
     63   uint32_t size() { return size_; }
     64 
     65  private:
     66   ppapi::thunk::PPB_Buffer_API* api_;
     67 
     68   bool needs_unmap_;
     69 
     70   void* data_;
     71   uint32_t size_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(BufferAutoMapper);
     74 };
     75 
     76 }  // namespace content
     77 
     78 #endif  // CONTENT_RENDERER_PEPPER_PPB_BUFFER_IMPL_H_
     79