Commit ddd61db5 authored by dmichael@chromium.org's avatar dmichael@chromium.org

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
parent d4067421
/* 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.
*/
/**
* This file defines the <code>PPB_VarArrayBuffer_Dev</code> struct.
*/
label Chrome {
M17 = 0.1
};
/**
* PPB_VarArrayBuffer_Dev API. This provides a way to interact with JavaScript
* ArrayBuffers, which represent a contiguous sequence of bytes. To manage the
* reference count for a VarArrayBuffer, please see PPB_Var. Note that
* these Vars are not part of the embedding page's DOM, and can only be shared
* with JavaScript via pp::Instance's PostMessage and HandleMessage functions.
*/
[macro="PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE"]
interface PPB_VarArrayBuffer_Dev {
/**
* Create a zero-initialized VarArrayBuffer.
*
* @param[in] size_in_bytes The size of the array buffer that will be created.
*
* @return A PP_Var which represents an VarArrayBuffer of the requested size
* with a reference count of 1.
*/
PP_Var Create([in] uint32_t size_in_bytes);
/**
* Returns the length of the VarArrayBuffer in bytes.
*
* @return The length of the VarArrayBuffer in bytes.
*/
uint32_t ByteLength([in] PP_Var array);
/**
* Returns a pointer to the beginning of the buffer for the given array.
*
* @param[in] array The array whose buffer should be returned.
*
* @return A pointer to the buffer for this array.
*/
mem_t Map([in] PP_Var array);
};
......@@ -61,7 +61,16 @@ enum PP_VarType {
* module will continue to work with future versions of the API.
*/
PP_VARTYPE_ARRAY = 7,
PP_VARTYPE_DICTIONARY = 8
PP_VARTYPE_DICTIONARY = 8,
/**
* ArrayBuffer represents a JavaScript ArrayBuffer. This is the type which
* represents Typed Arrays in JavaScript. Unlike JavaScript 'Array', it is
* only meant to contain basic numeric types, and is always stored
* contiguously. See PPB_VarArrayBuffer_Dev for functions special to
* ArrayBuffer vars.
*/
PP_VARTYPE_ARRAY_BUFFER = 9
};
......
/* 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.
*/
/* From dev/ppb_var_array_buffer_dev.idl modified Fri Dec 2 16:17:08 2011. */
#ifndef PPAPI_C_DEV_PPB_VAR_ARRAY_BUFFER_DEV_H_
#define PPAPI_C_DEV_PPB_VAR_ARRAY_BUFFER_DEV_H_
#include "ppapi/c/pp_bool.h"
#include "ppapi/c/pp_macros.h"
#include "ppapi/c/pp_stdint.h"
#include "ppapi/c/pp_var.h"
#define PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE_0_1 "PPB_VarArrayBuffer(Dev);0.1"
#define PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE \
PPB_VAR_ARRAY_BUFFER_DEV_INTERFACE_0_1
/**
* @file
* This file defines the <code>PPB_VarArrayBuffer_Dev</code> struct.
*/
/**
* @addtogroup Interfaces
* @{
*/
/**
* PPB_VarArrayBuffer_Dev API. This provides a way to interact with JavaScript
* ArrayBuffers, which represent a contiguous sequence of bytes. To manage the
* reference count for a VarArrayBuffer, please see PPB_Var. Note that
* these Vars are not part of the embedding page's DOM, and can only be shared
* with JavaScript via pp::Instance's PostMessage and HandleMessage functions.
*/
struct PPB_VarArrayBuffer_Dev {
/**
* Create a zero-initialized VarArrayBuffer.
*
* @param[in] size_in_bytes The size of the array buffer that will be created.
*
* @return A PP_Var which represents an VarArrayBuffer of the requested size
* with a reference count of 1.
*/
struct PP_Var (*Create)(uint32_t size_in_bytes);
/**
* Returns the length of the VarArrayBuffer in bytes.
*
* @return The length of the VarArrayBuffer in bytes.
*/
uint32_t (*ByteLength)(struct PP_Var array);
/**
* Returns a pointer to the beginning of the buffer for the given array.
*
* @param[in] array The array whose buffer should be returned.
*
* @return A pointer to the buffer for this array.
*/
void* (*Map)(struct PP_Var array);
};
/**
* @}
*/
#endif /* PPAPI_C_DEV_PPB_VAR_ARRAY_BUFFER_DEV_H_ */
......@@ -3,7 +3,7 @@
* found in the LICENSE file.
*/
/* From pp_var.idl modified Fri Nov 11 19:57:17 2011. */
/* From pp_var.idl modified Fri Dec 2 16:45:08 2011. */
#ifndef PPAPI_C_PP_VAR_H_
#define PPAPI_C_PP_VAR_H_
......@@ -68,7 +68,15 @@ typedef enum {
* module will continue to work with future versions of the API.
*/
PP_VARTYPE_ARRAY = 7,
PP_VARTYPE_DICTIONARY = 8
PP_VARTYPE_DICTIONARY = 8,
/**
* ArrayBuffer represents a JavaScript ArrayBuffer. This is the type which
* represents Typed Arrays in JavaScript. Unlike JavaScript 'Array', it is
* only meant to contain basic numeric types, and is always stored
* contiguously. See PPB_VarArrayBuffer_Dev for functions special to
* ArrayBuffer vars.
*/
PP_VARTYPE_ARRAY_BUFFER = 9
} PP_VarType;
PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_VarType, 4);
/**
......
// 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
// 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_
......@@ -158,6 +158,9 @@ class Var {
var_.type == PP_VARTYPE_DOUBLE;
}
/// This function determines if this <code>Var</code> is an ArrayBuffer.
bool is_array_buffer() const { return var_.type == PP_VARTYPE_ARRAY_BUFFER; }
/// AsBool() converts this <code>Var</code> to a bool. Assumes the
/// internal representation is_bool(). If it's not, it will assert in debug
/// mode, and return false.
......
......@@ -131,6 +131,7 @@ uint32_t PpVarSize(const PP_Var& var) {
case PP_VARTYPE_OBJECT:
case PP_VARTYPE_ARRAY:
case PP_VARTYPE_DICTIONARY:
case PP_VARTYPE_ARRAY_BUFFER:
NACL_NOTREACHED();
break;
}
......@@ -215,6 +216,7 @@ bool SerializePpVar(const PP_Var* vars,
case PP_VARTYPE_OBJECT:
case PP_VARTYPE_ARRAY:
case PP_VARTYPE_DICTIONARY:
case PP_VARTYPE_ARRAY_BUFFER:
NACL_NOTREACHED();
default:
return false;
......@@ -306,6 +308,7 @@ uint32_t DeserializePpVarSize(char* p,
case PP_VARTYPE_OBJECT:
case PP_VARTYPE_ARRAY:
case PP_VARTYPE_DICTIONARY:
case PP_VARTYPE_ARRAY_BUFFER:
NACL_NOTREACHED();
break;
}
......@@ -377,6 +380,7 @@ bool DeserializePpVar(NaClSrpcChannel* channel,
case PP_VARTYPE_OBJECT:
case PP_VARTYPE_ARRAY:
case PP_VARTYPE_DICTIONARY:
case PP_VARTYPE_ARRAY_BUFFER:
NACL_NOTREACHED();
default:
return false;
......
......@@ -129,6 +129,7 @@ std::string PluginVar::DebugString(const PP_Var& var) {
}
case PP_VARTYPE_ARRAY:
case PP_VARTYPE_DICTIONARY:
case PP_VARTYPE_ARRAY_BUFFER:
NACL_NOTREACHED();
break;
}
......@@ -191,6 +192,7 @@ void PluginVar::Print(const PP_Var& var) {
break;
case PP_VARTYPE_ARRAY:
case PP_VARTYPE_DICTIONARY:
case PP_VARTYPE_ARRAY_BUFFER:
NACL_NOTREACHED();
break;
}
......
......@@ -65,6 +65,7 @@
'c/dev/ppb_scrollbar_dev.h',
'c/dev/ppb_testing_dev.h',
'c/dev/ppb_url_util_dev.h',
'c/dev/ppb_var_array_buffer_dev.h',
'c/dev/ppb_video_decoder_dev.h',
'c/dev/ppb_websocket_dev.h',
'c/dev/ppb_widget_dev.h',
......@@ -200,6 +201,8 @@
'cpp/dev/text_input_dev.h',
'cpp/dev/url_util_dev.cc',
'cpp/dev/url_util_dev.h',
'cpp/dev/var_array_buffer_dev.cc',
'cpp/dev/var_array_buffer_dev.h',
'cpp/dev/video_capture_client_dev.cc',
'cpp/dev/video_capture_client_dev.h',
'cpp/dev/video_capture_dev.cc',
......@@ -380,4 +383,4 @@
},
}],
],
}
\ No newline at end of file
}
......@@ -157,6 +157,7 @@ void SerializedVar::Inner::WriteToMessage(IPC::Message* m) const {
break;
case PP_VARTYPE_ARRAY:
case PP_VARTYPE_DICTIONARY:
case PP_VARTYPE_ARRAY_BUFFER:
// TODO(brettw) when these are supported, implement this.
NOTIMPLEMENTED();
break;
......
......@@ -85,13 +85,13 @@ bool PPVarToV8Value(PP_Var var, v8::Handle<v8::Value>* result) {
break;
}
case PP_VARTYPE_OBJECT:
// Objects are not currently supported.
case PP_VARTYPE_ARRAY:
case PP_VARTYPE_DICTIONARY:
case PP_VARTYPE_ARRAY_BUFFER:
// These are not currently supported.
NOTIMPLEMENTED();
result->Clear();
return false;
default:
result->Clear();
return false;
}
return true;
}
......
......@@ -75,6 +75,7 @@ bool PPVarToNPVariant(PP_Var var, NPVariant* result) {
}
case PP_VARTYPE_ARRAY:
case PP_VARTYPE_DICTIONARY:
case PP_VARTYPE_ARRAY_BUFFER:
VOID_TO_NPVARIANT(*result);
break;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment