diff options
author | dmichael@chromium.org <dmichael@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-07 06:49:00 +0000 |
---|---|---|
committer | dmichael@chromium.org <dmichael@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-07 06:49:00 +0000 |
commit | ddd61db54184ca4491521f8ce8f4e50a4ca5025f (patch) | |
tree | 5b39af5a2fc0910a9c33a7595432189ea716a9de /ppapi/cpp/dev | |
parent | d4067421c536731f986013afb0551dc0bcc164c9 (diff) | |
download | chromium_src-ddd61db54184ca4491521f8ce8f4e50a4ca5025f.zip chromium_src-ddd61db54184ca4491521f8ce8f4e50a4ca5025f.tar.gz chromium_src-ddd61db54184ca4491521f8ce8f4e50a4ca5025f.tar.bz2 |
Draft of a PPAPI interface for ArrayBuffer.
See the TypedArray spec for reference:
http://www.khronos.org/registry/typedarray/specs/latest/
Things in the spec that I'm omiting:
- slice (Having a view of the ArrayBuffer that has a different offset/length)
TODO in future CLs:
-Implementation for in-process/trusted + tests (almost ready)
-NaCl proxy
-OOP proxy
Later still:
- Support for ArrayBufferView and TypedArray based on that.
BUG=103435
TEST=N/A
Review URL: http://codereview.chromium.org/8502030
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113355 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/cpp/dev')
-rw-r--r-- | ppapi/cpp/dev/var_array_buffer_dev.cc | 66 | ||||
-rw-r--r-- | ppapi/cpp/dev/var_array_buffer_dev.h | 57 |
2 files changed, 123 insertions, 0 deletions
diff --git a/ppapi/cpp/dev/var_array_buffer_dev.cc b/ppapi/cpp/dev/var_array_buffer_dev.cc new file mode 100644 index 0000000..4ce6215 --- /dev/null +++ b/ppapi/cpp/dev/var_array_buffer_dev.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/cpp/dev/var_array_buffer_dev.h" + +#include "ppapi/c/dev/ppb_var_array_buffer_dev.h" +#include "ppapi/cpp/logging.h" +#include "ppapi/cpp/module_impl.h" + +namespace pp { + +namespace { + +template <> const char* interface_name<PPB_VarArrayBuffer_Dev>() { + return PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE; +} + +} // namespace + +VarArrayBuffer_Dev::VarArrayBuffer_Dev(const Var& var) + : Var(var), buffer_(NULL) { + if (var.is_array_buffer()) { + buffer_ = Map(); + } else { + PP_NOTREACHED(); + var_ = PP_MakeNull(); + } +} + +VarArrayBuffer_Dev::VarArrayBuffer_Dev(uint32_t size_in_bytes) + : buffer_(NULL) { + if (has_interface<PPB_VarArrayBuffer_Dev>()) { + var_ = get_interface<PPB_VarArrayBuffer_Dev>()->Create(size_in_bytes); + buffer_ = Map(); + } else { + PP_NOTREACHED(); + var_ = PP_MakeNull(); + } +} + +uint32_t VarArrayBuffer_Dev::ByteLength() const { + if (has_interface<PPB_VarArrayBuffer_Dev>()) { + return get_interface<PPB_VarArrayBuffer_Dev>()->ByteLength(var_); + } + PP_NOTREACHED(); + return 0; +} + +const void* VarArrayBuffer_Dev::Map() const { + return const_cast<VarArrayBuffer_Dev*>(this)->Map(); +} + +void* VarArrayBuffer_Dev::Map() { + // TODO(dmichael): This is not thread-safe. + if (buffer_) + return buffer_; + if (has_interface<PPB_VarArrayBuffer_Dev>()) { + buffer_ = get_interface<PPB_VarArrayBuffer_Dev>()->Map(var_); + return buffer_; + } + PP_NOTREACHED(); + return 0; +} + +} // namespace pp diff --git a/ppapi/cpp/dev/var_array_buffer_dev.h b/ppapi/cpp/dev/var_array_buffer_dev.h new file mode 100644 index 0000000..6f8c959 --- /dev/null +++ b/ppapi/cpp/dev/var_array_buffer_dev.h @@ -0,0 +1,57 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PPAPI_CPP_DEV_VAR_ARRAY_BUFFER_DEV_H_ +#define PPAPI_CPP_DEV_VAR_ARRAY_BUFFER_DEV_H_ + +#include "ppapi/cpp/var.h" + +/// @file +/// This file defines the API for interacting with an ArrayBuffer. + +namespace pp { + +/// VarArrayBuffer_Dev provides a way to interact with JavaScript ArrayBuffers, +/// which represent a contiguous sequence of bytes. Note that +/// VarArrayBuffer_Devs are not part of the embedding page's DOM, and can only +/// be shared with JavaScript via pp::Instance's PostMessage and HandleMessage +/// functions. +class VarArrayBuffer_Dev : public Var { + public: + /// Contruct a VarArrayBuffer_Dev given a var for which is_array_buffer() is + /// true. This will refer to the same buffer as var, but allows you to access + /// methods specific to VarArrayBuffer_Dev. + /// + /// @param[in] var An array buffer Var. + explicit VarArrayBuffer_Dev(const Var& var); + + VarArrayBuffer_Dev(const VarArrayBuffer_Dev& buffer) : Var(buffer) {} + + /// Construct a new VarArrayBuffer_Dev which is size_in_bytes bytes long and + /// initialized to zero. + /// + /// @param[in] size_in_bytes The size of the constructed array in bytes. + VarArrayBuffer_Dev(uint32_t size_in_bytes); + + /// Return the length of the VarArrayBuffer_Dev in bytes. + /// + /// @return The length of the VarArrayBuffer_Dev in bytes. + uint32_t ByteLength() const; + + /// Return a pointer to the buffer associated with this VarArrayBuffer_Dev. + /// + /// @return A pointer to the buffer associated with this VarArrayBuffer_Dev. + void* Map(); + const void* Map() const; + + virtual ~VarArrayBuffer_Dev() {} + + private: + // We cache the buffer so that repeated calls to Map() are quick. + void* buffer_; +}; + +} // namespace pp + +#endif // PPAPI_CPP_DEV_VAR_ARRAY_BUFFER_DEV_H_ |