Commit 329adc16 authored by yzshen@chromium.org's avatar yzshen@chromium.org

App APIs in Pepper: C++ APIs

This CL contains supporting code for C++ APIs and the C++ equivalent of chrome.alarms.

BUG=327197
TEST=None

Review URL: https://codereview.chromium.org/116963003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243158 0039d316-1c4b-4281-b951-d872f2087c98
parent 6dbfac47
......@@ -60,6 +60,7 @@
'websocket.cc',
# ppapi/cpp/dev
'alarms_dev.cc',
'widget_client_dev.cc',
'resource_array_dev.cc',
'video_capture_client_dev.cc',
......@@ -85,6 +86,7 @@
'scriptable_object_deprecated.cc',
'audio_input_dev.cc',
'scrollbar_dev.cc',
'string_wrapper_dev.cc',
'graphics_2d_dev.cc',
'widget_dev.cc',
'var_resource_dev.cc',
......@@ -163,6 +165,8 @@
},
{
'FILES': [
'alarms_dev.h',
'array_dev.h',
'audio_input_dev.h',
'buffer_dev.h',
'crypto_dev.h',
......@@ -173,13 +177,18 @@
'font_dev.h',
'graphics_2d_dev.h',
'ime_input_event_dev.h',
'may_own_ptr_dev.h',
'memory_dev.h',
'optional_dev.h',
'printing_dev.h',
'resource_array_dev.h',
'scriptable_object_deprecated.h',
'scrollbar_dev.h',
'selection_dev.h',
'string_wrapper_dev.h',
'struct_wrapper_output_traits_dev.h',
'text_input_dev.h',
'to_c_type_converter_dev.h',
'truetype_font_dev.h',
'url_util_dev.h',
'var_resource_dev.h',
......
// Copyright 2014 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/alarms_dev.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/dev/array_dev.h"
#include "ppapi/cpp/dev/to_c_type_converter_dev.h"
#include "ppapi/cpp/module_impl.h"
namespace pp {
namespace {
template <> const char* interface_name<PPB_Alarms_Dev_0_1>() {
return PPB_ALARMS_DEV_INTERFACE_0_1;
}
} // namespace
namespace alarms {
Alarm_Dev::Alarm_Dev()
: name_wrapper_(&storage_->name, NOT_OWNED),
period_in_minutes_wrapper_(&storage_->period_in_minutes, NOT_OWNED) {
}
Alarm_Dev::Alarm_Dev(const Alarm_Dev& other)
: name_wrapper_(&storage_->name, NOT_OWNED),
period_in_minutes_wrapper_(&storage_->period_in_minutes, NOT_OWNED) {
operator=(other);
}
Alarm_Dev::Alarm_Dev(const PP_Alarms_Alarm_Dev& other)
: name_wrapper_(&storage_->name, NOT_OWNED),
period_in_minutes_wrapper_(&storage_->period_in_minutes, NOT_OWNED) {
operator=(other);
}
Alarm_Dev::Alarm_Dev(PP_Alarms_Alarm_Dev* storage, NotOwned)
: storage_(storage, NOT_OWNED),
name_wrapper_(&storage_->name, NOT_OWNED),
period_in_minutes_wrapper_(&storage_->period_in_minutes, NOT_OWNED) {
}
Alarm_Dev::~Alarm_Dev() {
}
Alarm_Dev& Alarm_Dev::operator=(const Alarm_Dev& other) {
return operator=(*other.storage_);
}
Alarm_Dev& Alarm_Dev::operator=(const PP_Alarms_Alarm_Dev& other) {
if (storage_.get() == &other)
return *this;
name_wrapper_ = other.name;
storage_->scheduled_time = other.scheduled_time;
period_in_minutes_wrapper_ = other.period_in_minutes;
return *this;
}
std::string Alarm_Dev::name() const {
return name_wrapper_.get();
}
void Alarm_Dev::set_name(const std::string& value) {
name_wrapper_.set(value);
}
double Alarm_Dev::scheduled_time() const {
return storage_->scheduled_time;
}
void Alarm_Dev::set_scheduled_time(double value) {
storage_->scheduled_time = value;
}
bool Alarm_Dev::is_period_in_minutes_set() const {
return period_in_minutes_wrapper_.is_set();
}
void Alarm_Dev::unset_period_in_minutes() {
period_in_minutes_wrapper_.unset();
}
double Alarm_Dev::period_in_minutes() const {
return period_in_minutes_wrapper_.get();
}
void Alarm_Dev::set_period_in_minutes(double value) {
period_in_minutes_wrapper_.set(value);
}
const PP_Alarms_Alarm_Dev* Alarm_Dev::ToStruct() const {
return storage_.get();
}
PP_Alarms_Alarm_Dev* Alarm_Dev::StartRawUpdate() {
name_wrapper_.StartRawUpdate();
period_in_minutes_wrapper_.StartRawUpdate();
return storage_.get();
}
void Alarm_Dev::EndRawUpdate() {
name_wrapper_.EndRawUpdate();
period_in_minutes_wrapper_.EndRawUpdate();
}
AlarmCreateInfo_Dev::AlarmCreateInfo_Dev()
: when_wrapper_(&storage_->when, NOT_OWNED),
delay_in_minutes_wrapper_(&storage_->delay_in_minutes, NOT_OWNED),
period_in_minutes_wrapper_(&storage_->period_in_minutes, NOT_OWNED) {
}
AlarmCreateInfo_Dev::AlarmCreateInfo_Dev(const AlarmCreateInfo_Dev& other)
: when_wrapper_(&storage_->when, NOT_OWNED),
delay_in_minutes_wrapper_(&storage_->delay_in_minutes, NOT_OWNED),
period_in_minutes_wrapper_(&storage_->period_in_minutes, NOT_OWNED) {
operator=(other);
}
AlarmCreateInfo_Dev::AlarmCreateInfo_Dev(
const PP_Alarms_AlarmCreateInfo_Dev& other)
: when_wrapper_(&storage_->when, NOT_OWNED),
delay_in_minutes_wrapper_(&storage_->delay_in_minutes, NOT_OWNED),
period_in_minutes_wrapper_(&storage_->period_in_minutes, NOT_OWNED) {
operator=(other);
}
AlarmCreateInfo_Dev::AlarmCreateInfo_Dev(
PP_Alarms_AlarmCreateInfo_Dev* storage,
NotOwned)
: storage_(storage, NOT_OWNED),
when_wrapper_(&storage_->when, NOT_OWNED),
delay_in_minutes_wrapper_(&storage_->delay_in_minutes, NOT_OWNED),
period_in_minutes_wrapper_(&storage_->period_in_minutes, NOT_OWNED) {
}
AlarmCreateInfo_Dev::~AlarmCreateInfo_Dev() {
}
AlarmCreateInfo_Dev& AlarmCreateInfo_Dev::operator=(
const AlarmCreateInfo_Dev& other) {
return operator=(*other.storage_);
}
AlarmCreateInfo_Dev& AlarmCreateInfo_Dev::operator=(
const PP_Alarms_AlarmCreateInfo_Dev& other) {
if (storage_.get() == &other)
return *this;
when_wrapper_ = other.when;
delay_in_minutes_wrapper_ = other.delay_in_minutes;
period_in_minutes_wrapper_ = other.period_in_minutes;
return *this;
}
bool AlarmCreateInfo_Dev::is_when_set() const {
return when_wrapper_.is_set();
}
void AlarmCreateInfo_Dev::unset_when() {
when_wrapper_.unset();
}
double AlarmCreateInfo_Dev::when() const {
return when_wrapper_.get();
}
void AlarmCreateInfo_Dev::set_when(double value) {
when_wrapper_.set(value);
}
bool AlarmCreateInfo_Dev::is_delay_in_minutes_set() const {
return delay_in_minutes_wrapper_.is_set();
}
void AlarmCreateInfo_Dev::unset_delay_in_minutes() {
delay_in_minutes_wrapper_.unset();
}
double AlarmCreateInfo_Dev::delay_in_minutes() const {
return delay_in_minutes_wrapper_.get();
}
void AlarmCreateInfo_Dev::set_delay_in_minutes(double value) {
delay_in_minutes_wrapper_.set(value);
}
bool AlarmCreateInfo_Dev::is_period_in_minutes_set() const {
return period_in_minutes_wrapper_.is_set();
}
void AlarmCreateInfo_Dev::unset_period_in_minutes() {
period_in_minutes_wrapper_.unset();
}
double AlarmCreateInfo_Dev::period_in_minutes() const {
return period_in_minutes_wrapper_.get();
}
void AlarmCreateInfo_Dev::set_period_in_minutes(double value) {
period_in_minutes_wrapper_.set(value);
}
const PP_Alarms_AlarmCreateInfo_Dev* AlarmCreateInfo_Dev::ToStruct() const {
return storage_.get();
}
PP_Alarms_AlarmCreateInfo_Dev* AlarmCreateInfo_Dev::StartRawUpdate() {
when_wrapper_.StartRawUpdate();
delay_in_minutes_wrapper_.StartRawUpdate();
period_in_minutes_wrapper_.StartRawUpdate();
return storage_.get();
}
void AlarmCreateInfo_Dev::EndRawUpdate() {
when_wrapper_.EndRawUpdate();
delay_in_minutes_wrapper_.EndRawUpdate();
period_in_minutes_wrapper_.EndRawUpdate();
}
Alarms_Dev::Alarms_Dev(const InstanceHandle& instance) : instance_(instance) {
}
Alarms_Dev::~Alarms_Dev() {
}
void Alarms_Dev::Create(const Optional<std::string>& name,
const AlarmCreateInfo_Dev& alarm_info) {
if (!has_interface<PPB_Alarms_Dev_0_1>())
return;
internal::ToCTypeConverter<Optional<std::string> > name_converter(name);
internal::ToCTypeConverter<AlarmCreateInfo_Dev> alarm_info_converter(
alarm_info);
return get_interface<PPB_Alarms_Dev_0_1>()->Create(
instance_.pp_instance(),
name_converter.ToCInput(),
alarm_info_converter.ToCInput());
}
int32_t Alarms_Dev::Get(const Optional<std::string>& name,
const GetCallback& callback) {
if (!has_interface<PPB_Alarms_Dev_0_1>())
return callback.MayForce(PP_ERROR_NOINTERFACE);
internal::ToCTypeConverter<Optional<std::string> > name_converter(name);
return get_interface<PPB_Alarms_Dev_0_1>()->Get(
instance_.pp_instance(),
name_converter.ToCInput(),
callback.output(),
callback.pp_completion_callback());
}
int32_t Alarms_Dev::GetAll(const GetAllCallback& callback) {
if (!has_interface<PPB_Alarms_Dev_0_1>())
return callback.MayForce(PP_ERROR_NOINTERFACE);
return get_interface<PPB_Alarms_Dev_0_1>()->GetAll(
instance_.pp_instance(),
callback.output(),
internal::ArrayAllocator::Get(),
callback.pp_completion_callback());
}
void Alarms_Dev::Clear(const Optional<std::string>& name) {
if (!has_interface<PPB_Alarms_Dev_0_1>())
return;
internal::ToCTypeConverter<Optional<std::string> > name_converter(name);
return get_interface<PPB_Alarms_Dev_0_1>()->Clear(
instance_.pp_instance(),
name_converter.ToCInput());
}
void Alarms_Dev::ClearAll() {
if (!has_interface<PPB_Alarms_Dev_0_1>())
return;
return get_interface<PPB_Alarms_Dev_0_1>()->ClearAll(
instance_.pp_instance());
}
} // namespace alarms
} // namespace pp
// Copyright 2014 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_ALARMS_DEV_H_
#define PPAPI_CPP_DEV_ALARMS_DEV_H_
#include <string>
#include "ppapi/c/dev/ppb_alarms_dev.h"
#include "ppapi/cpp/dev/may_own_ptr_dev.h"
#include "ppapi/cpp/dev/optional_dev.h"
#include "ppapi/cpp/dev/string_wrapper_dev.h"
#include "ppapi/cpp/dev/struct_wrapper_output_traits_dev.h"
#include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/output_traits.h"
namespace pp {
template <typename T>
class CompletionCallbackWithOutput;
template <typename T>
class Array;
namespace alarms {
// Data types ------------------------------------------------------------------
class Alarm_Dev {
public:
typedef PP_Alarms_Alarm_Dev CType;
typedef PP_Alarms_Alarm_Array_Dev CArrayType;
Alarm_Dev();
Alarm_Dev(const Alarm_Dev& other);
explicit Alarm_Dev(const PP_Alarms_Alarm_Dev& other);
// Creates an accessor to |storage| but doesn't take ownership of the memory.
// |storage| must live longer than this object. The contents pointed to by
// |storage| is managed by this object.
// Used by Pepper internal implementation.
Alarm_Dev(PP_Alarms_Alarm_Dev* storage, NotOwned);
~Alarm_Dev();
Alarm_Dev& operator=(const Alarm_Dev& other);
Alarm_Dev& operator=(const PP_Alarms_Alarm_Dev& other);
std::string name() const;
void set_name(const std::string& value);
double scheduled_time() const;
void set_scheduled_time(double value);
bool is_period_in_minutes_set() const;
void unset_period_in_minutes();
double period_in_minutes() const;
void set_period_in_minutes(double value);
const PP_Alarms_Alarm_Dev* ToStruct() const;
// The returned pointer is still owned by this object. And after it is used,
// EndRawUpdate() must be called.
PP_Alarms_Alarm_Dev* StartRawUpdate();
void EndRawUpdate();
private:
internal::MayOwnPtr<PP_Alarms_Alarm_Dev> storage_;
internal::StringWrapper name_wrapper_;
Optional<double> period_in_minutes_wrapper_;
};
class AlarmCreateInfo_Dev {
public:
typedef PP_Alarms_AlarmCreateInfo_Dev CType;
AlarmCreateInfo_Dev();
AlarmCreateInfo_Dev(const AlarmCreateInfo_Dev& other);
explicit AlarmCreateInfo_Dev(const PP_Alarms_AlarmCreateInfo_Dev& other);
// Creates an accessor to |storage| but doesn't take ownership of the memory.
// |storage| must live longer than this object. The contents pointed to by
// |storage| is managed by this object.
// Used by Pepper internal implementation.
AlarmCreateInfo_Dev(PP_Alarms_AlarmCreateInfo_Dev* storage, NotOwned);
~AlarmCreateInfo_Dev();
AlarmCreateInfo_Dev& operator=(const AlarmCreateInfo_Dev& other);
AlarmCreateInfo_Dev& operator=(const PP_Alarms_AlarmCreateInfo_Dev& other);
bool is_when_set() const;
void unset_when();
double when() const;
void set_when(double value);
bool is_delay_in_minutes_set() const;
void unset_delay_in_minutes();
double delay_in_minutes() const;
void set_delay_in_minutes(double value);
bool is_period_in_minutes_set() const;
void unset_period_in_minutes();
double period_in_minutes() const;
void set_period_in_minutes(double value);
const PP_Alarms_AlarmCreateInfo_Dev* ToStruct() const;
// The returned pointer is still owned by this object. And after it is used,
// EndRawUpdate() must be called.
PP_Alarms_AlarmCreateInfo_Dev* StartRawUpdate();
void EndRawUpdate();
private:
internal::MayOwnPtr<PP_Alarms_AlarmCreateInfo_Dev> storage_;
Optional<double> when_wrapper_;
Optional<double> delay_in_minutes_wrapper_;
Optional<double> period_in_minutes_wrapper_;
};
// Functions -------------------------------------------------------------------
class Alarms_Dev {
public:
explicit Alarms_Dev(const InstanceHandle& instance);
~Alarms_Dev();
void Create(const Optional<std::string>& name,
const AlarmCreateInfo_Dev& alarm_info);
typedef CompletionCallbackWithOutput<Alarm_Dev> GetCallback;
int32_t Get(const Optional<std::string>& name, const GetCallback& callback);
typedef CompletionCallbackWithOutput<Array<Alarm_Dev> > GetAllCallback;
int32_t GetAll(const GetAllCallback& callback);
void Clear(const Optional<std::string>& name);
void ClearAll();
private:
InstanceHandle instance_;
};
// Events ----------------------------------------------------------------------
// TODO(yzshen): add onAlarm event.
} // namespace alarms
namespace internal {
template <>
struct CallbackOutputTraits<alarms::Alarm_Dev>
: public internal::StructWrapperOutputTraits<alarms::Alarm_Dev> {
};
template <>
struct CallbackOutputTraits<alarms::AlarmCreateInfo_Dev>
: public internal::StructWrapperOutputTraits<alarms::AlarmCreateInfo_Dev> {
};
} // namespace internal
} // namespace pp
#endif // PPAPI_CPP_DEV_ALARMS_DEV_H_
// Copyright 2014 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_ARRAY_DEV_H_
#define PPAPI_CPP_DEV_ARRAY_DEV_H_
#include <cstdlib>
#include <vector>
#include "ppapi/cpp/dev/may_own_ptr_dev.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/output_traits.h"
namespace pp {
template <typename T>
class Array;
namespace internal {
class ArrayAllocator {
public:
// Allocates memory and zero-fills it.
static void* Alloc(uint32_t count, uint32_t size) {
if (count == 0 || size == 0)
return NULL;
return calloc(count, size);
}
static void Free(void* mem) { free(mem); }
static PP_ArrayOutput Get() {
PP_ArrayOutput array_output = { &ArrayAllocator::GetDataBuffer, NULL };
return array_output;
}
private:
static void* GetDataBuffer(void* /* user_data */,
uint32_t element_count,
uint32_t element_size) {
return Alloc(element_count, element_size);
}
};
// A specialization of CallbackOutputTraits for pp::Array parameters.
template <typename T>
struct CallbackOutputTraits<Array<T> > {
typedef typename Array<T>::CArrayType* APIArgType;
typedef Array<T> StorageType;
// Returns the underlying C struct of |t|, which can be passed to the browser
// as an output parameter.
static inline APIArgType StorageToAPIArg(StorageType& t) {
return t.StartRawUpdate();
}
// For each |t| passed into StorageToAPIArg(), this method must be called
// exactly once in order to match StartRawUpdate() and EndRawUpdate().
static inline Array<T>& StorageToPluginArg(StorageType& t) {
t.EndRawUpdate();
return t;
}
static inline void Initialize(StorageType* /* t */) {}
};
} // namespace internal
// Generic array for struct wrappers.
template <class T>
class Array {
public:
typedef typename T::CArrayType CArrayType;
typedef typename T::CType CType;
Array() {}
explicit Array(uint32_t size) { Reset(size); }
// Creates an accessor to |storage| but doesn't take ownership of the memory.
// |storage| must live longer than this object.
//
// Although this object doesn't own the memory of |storage|, it manages the
// memory pointed to by |storage->elements| and resets |storage| to empty when
// destructed.
Array(CArrayType* storage, NotOwned) : storage_(storage, NOT_OWNED) {
CreateWrappers();
}
Array(const Array<T>& other) { DeepCopy(*other.storage_); }
~Array() { Reset(0); }
Array<T>& operator=(const Array<T>& other) {
return operator=(*other.storage_);
}
Array<T>& operator=(const CArrayType& other) {
if (storage_.get() == &other)
return *this;
Reset(0);
DeepCopy(other);
return *this;
}
uint32_t size() const { return storage_->size; }
T& operator[](uint32_t index) {
PP_DCHECK(storage_->size == wrappers_.size());
PP_DCHECK(index < storage_->size);
PP_DCHECK(wrappers_[index]->ToStruct() == storage_->elements + index);
return *wrappers_[index];
}
const T& operator[](uint32_t index) const {
PP_DCHECK(storage_->size == wrappers_.size());
PP_DCHECK(index < storage_->size);
PP_DCHECK(wrappers_[index]->ToStruct() == storage_->elements + index);
return *wrappers_[index];
}
// Clears the existing contents of the array, and resets it to |size| elements
// of default value.
void Reset(uint32_t size) {
// Wrappers must be destroyed before we free |storage_->elements|, because
// they may try to access the memory in their destructors.
DeleteWrappers();
internal::ArrayAllocator::Free(storage_->elements);
storage_->elements = NULL;
storage_->size = size;
if (size > 0) {
storage_->elements = static_cast<CType*>(
internal::ArrayAllocator::Alloc(size, sizeof(CType)));
}
CreateWrappers();
}
// Sets the underlying C array struct to empty before returning it.
CArrayType* StartRawUpdate() {
Reset(0);
return storage_.get();
}
void EndRawUpdate() { CreateWrappers(); }
private:
void DeepCopy(const CArrayType& other) {
storage_->size = other.size;
if (storage_->size > 0) {
storage_->elements = static_cast<CType*>(
internal::ArrayAllocator::Alloc(storage_->size, sizeof(CType)));
}
CreateWrappers();
for (size_t i = 0; i < storage_->size; ++i)
wrappers_[i] = other.elements[i];
}
void DeleteWrappers() {
for (typename std::vector<T*>::iterator iter = wrappers_.begin();
iter != wrappers_.end();
++iter) {
delete *iter;
}
wrappers_.clear();
}
void CreateWrappers() {
PP_DCHECK(wrappers_.empty());
uint32_t size = storage_->size;
if (size == 0)
return;
wrappers_.reserve(size);
for (size_t i = 0; i < size; ++i)
wrappers_.push_back(new T(&storage_->elements[i], NOT_OWNED));
}
internal::MayOwnPtr<CArrayType> storage_;
std::vector<T*> wrappers_;
};
} // namespace pp
#endif // PPAPI_CPP_DEV_ARRAY_DEV_H_
// Copyright 2014 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_MAY_OWN_PTR_DEV_H_
#define PPAPI_CPP_DEV_MAY_OWN_PTR_DEV_H_
#include "ppapi/cpp/logging.h"
namespace pp {
// An annotation used along with a pointer parameter of a function to indicate
// that the function doesn't take ownership of the pointer.
enum NotOwned {
NOT_OWNED
};
namespace internal {
// MayOwnPtr keeps track of whether it has ownership of the pointer it holds,
// and deletes the pointer on destruction if it does.
template <typename T>
class MayOwnPtr {
public:
// Creates and owns a T instance.
// NOTE: "()" after T is important to do zero-initialization for POD types.
MayOwnPtr() : value_(new T()), owned_(true) {}
// It doesn't take ownership of |value|, therefore |value| must live longer
// than this object.
MayOwnPtr(T* value, NotOwned) : value_(value), owned_(false) {
PP_DCHECK(value);
}
~MayOwnPtr() {
if (owned_)
delete value_;
}
const T* get() const { return value_; }
T* get() { return value_; }
const T& operator*() const { return *value_; }
T& operator*() { return *value_; }
const T* operator->() const { return value_; }
T* operator->() { return value_; }
private:
// Disallow copying and assignment.
MayOwnPtr(const MayOwnPtr<T>&);
MayOwnPtr<T>& operator=(const MayOwnPtr<T>&);
T* value_;
bool owned_;
};
} // namespace internal
} // namespace pp
#endif // PPAPI_CPP_DEV_MAY_OWN_PTR_DEV_H_
// Copyright 2014 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_OPTIONAL_DEV_H_
#define PPAPI_CPP_DEV_OPTIONAL_DEV_H_
#include "ppapi/c/dev/pp_optional_structs_dev.h"
#include "ppapi/c/pp_bool.h"
#include "ppapi/cpp/dev/may_own_ptr_dev.h"
#include "ppapi/cpp/dev/string_wrapper_dev.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/var.h"
namespace pp {
template <typename T>
class Optional;
template <>
class Optional<double> {
public:
typedef const PP_Optional_Double_Dev* CInputType;
Optional() {}
Optional(double value) { set(value); }
// Creates an accessor to |storage| but doesn't take ownership of the memory.
// |storage| must live longer than this object.
Optional(PP_Optional_Double_Dev* storage, NotOwned)
: storage_(storage, NOT_OWNED) {
}
Optional(const Optional<double>& other) { *storage_ = *other.storage_; }
Optional<double>& operator=(const Optional<double>& other) {
return operator=(*other.storage_);
}
Optional<double>& operator=(const PP_Optional_Double_Dev& other) {
if (storage_.get() == &other)
return *this;
*storage_ = other;
return *this;
}
bool is_set() const { return PP_ToBool(storage_->is_set); }
void unset() { storage_->is_set = PP_FALSE; }
double get() const {
PP_DCHECK(is_set());
return storage_->value;
}
void set(double value) {
storage_->value = value;
storage_->is_set = PP_TRUE;
}
const PP_Optional_Double_Dev* ToCInput() const { return storage_.get(); }
PP_Optional_Double_Dev* StartRawUpdate() { return storage_.get(); }
void EndRawUpdate() {}
private:
internal::MayOwnPtr<PP_Optional_Double_Dev> storage_;
};
template <>
class Optional<std::string> {
public:
typedef const PP_Var& CInputType;
Optional() {}
Optional(const std::string& value) : wrapper_(value) {}
Optional(const char* value) : wrapper_(value) {}
// Creates an accessor to |storage| but doesn't take ownership of the memory.
// |storage| must live longer than this object.
//
// Although this object doesn't own the memory of |storage|, it manages the
// ref count and sets the var to undefined when destructed.
Optional(PP_Var* storage, NotOwned) : wrapper_(storage, NOT_OWNED) {}
Optional(const Optional<std::string>& other) : wrapper_(other.wrapper_) {}
Optional<std::string>& operator=(const Optional<std::string>& other) {
wrapper_ = other.wrapper_;
return *this;
}
Optional<std::string>& operator=(const PP_Var& other) {
wrapper_ = other;
return *this;
}
bool is_set() const { return wrapper_.is_set(); }
void unset() { wrapper_.unset(); }
std::string get() const { return wrapper_.get(); }
void set(const std::string& value) { wrapper_.set(value); }
const PP_Var& ToCInput() const { return wrapper_.ToVar(); }
PP_Var* StartRawUpdate() { return wrapper_.StartRawUpdate(); }
void EndRawUpdate() { wrapper_.EndRawUpdate(); }
private:
internal::OptionalStringWrapper wrapper_;
};
} // namespace pp
#endif // PPAPI_CPP_DEV_OPTIONAL_DEV_H_
// Copyright 2014 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/string_wrapper_dev.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/var.h"
namespace pp {
namespace internal {
OptionalStringWrapper::OptionalStringWrapper() {
}
OptionalStringWrapper::OptionalStringWrapper(const std::string& value) {
*storage_ = Var(value).Detach();
}
OptionalStringWrapper::OptionalStringWrapper(PP_Var* storage, NotOwned)
: storage_(storage, NOT_OWNED) {
PP_DCHECK(storage_->type == PP_VARTYPE_STRING ||
storage_->type == PP_VARTYPE_UNDEFINED);
}
OptionalStringWrapper::OptionalStringWrapper(
const OptionalStringWrapper& other) {
// Add one ref.
*storage_ = Var(*other.storage_).Detach();
}
OptionalStringWrapper::~OptionalStringWrapper() {
unset();
}
OptionalStringWrapper& OptionalStringWrapper::operator=(
const OptionalStringWrapper& other) {
return operator=(*other.storage_);
}
OptionalStringWrapper& OptionalStringWrapper::operator=(const PP_Var& other) {
if (storage_.get() == &other)
return *this;
Var auto_release(PASS_REF, *storage_);
// Add one ref.
*storage_ = Var(other).Detach();
return *this;
}
bool OptionalStringWrapper::is_set() const {
PP_DCHECK(storage_->type == PP_VARTYPE_STRING ||
storage_->type == PP_VARTYPE_UNDEFINED);
return storage_->type == PP_VARTYPE_STRING;
}
void OptionalStringWrapper::unset() {
Var auto_release(PASS_REF, *storage_);
*storage_ = PP_MakeUndefined();
}
std::string OptionalStringWrapper::get() const {
// TODO(yzshen): consider adding a cache.
Var var(*storage_);
if (var.is_string()) {
return var.AsString();
} else {
PP_NOTREACHED();
return std::string();
}
}
void OptionalStringWrapper::set(const std::string& value) {
Var auto_release(PASS_REF, *storage_);
*storage_ = Var(value).Detach();
}
PP_Var* OptionalStringWrapper::StartRawUpdate() {
unset();
return storage_.get();
}
void OptionalStringWrapper::EndRawUpdate() {
PP_DCHECK(storage_->type == PP_VARTYPE_STRING ||
storage_->type == PP_VARTYPE_UNDEFINED);
}
StringWrapper::StringWrapper() : storage_(std::string()) {
}
StringWrapper::StringWrapper(const std::string& value) : storage_(value) {
}
StringWrapper::StringWrapper(PP_Var* storage, NotOwned)
: storage_(storage, NOT_OWNED) {
if (!storage_.is_set())
storage_.set(std::string());
}
StringWrapper::StringWrapper(const StringWrapper& other)
: storage_(other.storage_) {
}
StringWrapper::~StringWrapper() {
}
StringWrapper& StringWrapper::operator=(const StringWrapper& other) {
storage_ = other.storage_;
return *this;
}
StringWrapper& StringWrapper::operator=(const PP_Var& other) {
PP_DCHECK(other.type == PP_VARTYPE_STRING);
storage_ = other;
return *this;
}
PP_Var* StringWrapper::StartRawUpdate() {
return storage_.StartRawUpdate();
}
void StringWrapper::EndRawUpdate() {
storage_.EndRawUpdate();
if (!storage_.is_set())
storage_.set(std::string());
}
} // namespace internal
} // namespace pp
// Copyright 2014 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_STRING_WRAPPER_DEV_H_
#define PPAPI_CPP_DEV_STRING_WRAPPER_DEV_H_
#include <string>
#include "ppapi/c/pp_var.h"
#include "ppapi/cpp/dev/may_own_ptr_dev.h"
namespace pp {
namespace internal {
// An optional string backed by a PP_Var. When the string is not set, the type
// of the PP_Var is set to PP_VARTYPE_UNDEFINED; otherwise, it is set to
// PP_VARTYPE_STRING.
class OptionalStringWrapper {
public:
OptionalStringWrapper();
explicit OptionalStringWrapper(const std::string& value);
// Creates an accessor to |storage| but doesn't take ownership of the memory.
// |storage| must live longer than this object.
//
// Although this object doesn't own the memory of |storage|, it manages the
// ref count and sets the var to undefined when destructed.
OptionalStringWrapper(PP_Var* storage, NotOwned);
OptionalStringWrapper(const OptionalStringWrapper& other);
~OptionalStringWrapper();
OptionalStringWrapper& operator=(const OptionalStringWrapper& other);
OptionalStringWrapper& operator=(const PP_Var& other);
bool is_set() const;
void unset();
std::string get() const;
void set(const std::string& value);
const PP_Var& ToVar() const { return *storage_; }
// Sets the underlying PP_Var to undefined before returning it.
PP_Var* StartRawUpdate();
void EndRawUpdate();
private:
MayOwnPtr<PP_Var> storage_;
};
// A string backed by a PP_Var whose type is PP_VARTYPE_STRING.
class StringWrapper {
public:
StringWrapper();
explicit StringWrapper(const std::string& value);
// Creates an accessor to |storage| but doesn't take ownership of the memory.
// |storage| must live longer than this object.
//
// Although this object doesn't own the memory of |storage|, it manages the
// ref count and sets the var to undefined when destructed.
StringWrapper(PP_Var* storage, NotOwned);
StringWrapper(const StringWrapper& other);
~StringWrapper();
StringWrapper& operator=(const StringWrapper& other);
StringWrapper& operator=(const PP_Var& other);
std::string get() const { return storage_.get(); }
void set(const std::string& value) { return storage_.set(value); }
const PP_Var& ToVar() const { return storage_.ToVar(); }
// Sets the underlying PP_Var to undefined before returning it.
PP_Var* StartRawUpdate();
// If the underlying PP_Var wasn't updated to a valid string, sets it to an
// empty string.
void EndRawUpdate();
private:
OptionalStringWrapper storage_;
};
} // namespace internal
} // namespace pp
#endif // PPAPI_CPP_DEV_STRING_WRAPPER_DEV_H_
// Copyright 2014 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_STRUCT_WRAPPER_OUTPUT_TRAITS_DEV_
#define PPAPI_CPP_DEV_STRUCT_WRAPPER_OUTPUT_TRAITS_DEV_
namespace pp {
namespace internal {
// This class is used as the base class for CallbackOutputTraits specialized for
// C struct wrappers.
template <typename T>
struct StructWrapperOutputTraits {
typedef typename T::CType* APIArgType;
typedef T StorageType;
// Returns the underlying C struct of |t|, which can be passed to the browser
// as an output parameter.
static inline APIArgType StorageToAPIArg(StorageType& t) {
return t.StartRawUpdate();
}
// For each |t| passed into StorageToAPIArg(), this method must be called
// exactly once in order to match StartRawUpdate() and EndRawUpdate().
static inline T& StorageToPluginArg(StorageType& t) {
t.EndRawUpdate();
return t;
}
static inline void Initialize(StorageType* /* t */) {}
};
} // namespace internal
} // namespace pp
#endif // PPAPI_CPP_DEV_STRUCT_WRAPPER_OUTPUT_TRAITS_DEV_
// Copyright 2014 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_TO_C_TYPE_CONVERTER_DEV_H_
#define PPAPI_CPP_DEV_TO_C_TYPE_CONVERTER_DEV_H_
#include <string>
#include "ppapi/c/pp_var.h"
#include "ppapi/cpp/dev/optional_dev.h"
#include "ppapi/cpp/dev/string_wrapper_dev.h"
namespace pp {
namespace internal {
// ToCTypeConverter is used to convert C++ API input parameters into C API input
// parameters.
// Generic converter for C struct wrappers.
template <typename T>
class ToCTypeConverter {
public:
explicit ToCTypeConverter(const T& object) : wrapper_(object) {}
~ToCTypeConverter() {}
const typename T::CType* ToCInput() const { return wrapper_.ToStruct(); }
private:
// Disallow copying and assignment.
ToCTypeConverter(const ToCTypeConverter<T>&);
ToCTypeConverter<T>& operator=(const ToCTypeConverter<T>&);
const T& wrapper_;
};
template <>
class ToCTypeConverter<std::string> {
public:
explicit ToCTypeConverter(const std::string& object) : wrapper_(object) {}
~ToCTypeConverter() {}
const PP_Var& ToCInput() const { return wrapper_.ToVar(); }
private:
// Disallow copying and assignment.
ToCTypeConverter(const ToCTypeConverter<std::string>&);
ToCTypeConverter<std::string>& operator=(
const ToCTypeConverter<std::string>&);
StringWrapper wrapper_;
};
template <>
class ToCTypeConverter<double> {
public:
explicit ToCTypeConverter(double object) : storage_(object) {}
~ToCTypeConverter() {}
double ToCInput() const { return storage_; }
private:
// Disallow copying and assignment.
ToCTypeConverter(const ToCTypeConverter<double>&);
ToCTypeConverter<double>& operator=(const ToCTypeConverter<double>&);
double storage_;
};
template <typename T>
class ToCTypeConverter<Optional<T> > {
public:
explicit ToCTypeConverter(const Optional<T>& object) : wrapper_(object) {}
~ToCTypeConverter() {}
typename Optional<T>::CInputType ToCInput() const {
return wrapper_.ToCInput();
}
private:
// Disallow copying and assignment.
ToCTypeConverter(const ToCTypeConverter<Optional<T> >&);
ToCTypeConverter<Optional<T> >& operator=(
const ToCTypeConverter<Optional<T> >&);
const Optional<T>& wrapper_;
};
} // namespace internal
} // namespace pp
#endif // PPAPI_CPP_DEV_TO_C_TYPE_CONVERTER_DEV_H_
......@@ -239,6 +239,9 @@
'cpp/websocket.h',
# Dev interfaces.
'cpp/dev/alarms_dev.cc',
'cpp/dev/alarms_dev.h',
'cpp/dev/array_dev.h',
'cpp/dev/audio_input_dev.cc',
'cpp/dev/audio_input_dev.h',
'cpp/dev/buffer_dev.cc',
......@@ -259,8 +262,10 @@
'cpp/dev/graphics_2d_dev.h',
'cpp/dev/ime_input_event_dev.cc',
'cpp/dev/ime_input_event_dev.h',
'cpp/dev/may_own_ptr_dev.h',
'cpp/dev/memory_dev.cc',
'cpp/dev/memory_dev.h',
'cpp/dev/optional_dev.h',
'cpp/dev/printing_dev.cc',
'cpp/dev/printing_dev.h',
'cpp/dev/resource_array_dev.cc',
......@@ -269,8 +274,12 @@
'cpp/dev/scrollbar_dev.h',
'cpp/dev/selection_dev.cc',
'cpp/dev/selection_dev.h',
'cpp/dev/string_wrapper_dev.cc',
'cpp/dev/string_wrapper_dev.h',
'cpp/dev/struct_wrapper_output_traits_dev.h',
'cpp/dev/text_input_dev.cc',
'cpp/dev/text_input_dev.h',
'cpp/dev/to_c_type_converter_dev.h',
'cpp/dev/truetype_font_dev.cc',
'cpp/dev/truetype_font_dev.h',
'cpp/dev/url_util_dev.cc',
......
......@@ -12,6 +12,8 @@
#include "ppapi/cpp/audio_config.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/core.h"
#include "ppapi/cpp/dev/alarms_dev.h"
#include "ppapi/cpp/dev/array_dev.h"
#include "ppapi/cpp/dev/buffer_dev.h"
#include "ppapi/cpp/dev/device_ref_dev.h"
#include "ppapi/cpp/dev/file_chooser_dev.h"
......@@ -19,13 +21,18 @@
#include "ppapi/cpp/dev/font_dev.h"
#include "ppapi/cpp/dev/graphics_2d_dev.h"
#include "ppapi/cpp/dev/ime_input_event_dev.h"
#include "ppapi/cpp/dev/may_own_ptr_dev.h"
#include "ppapi/cpp/dev/memory_dev.h"
#include "ppapi/cpp/dev/optional_dev.h"
#include "ppapi/cpp/dev/printing_dev.h"
#include "ppapi/cpp/dev/resource_array_dev.h"
#include "ppapi/cpp/dev/scriptable_object_deprecated.h"
#include "ppapi/cpp/dev/scrollbar_dev.h"
#include "ppapi/cpp/dev/selection_dev.h"
#include "ppapi/cpp/dev/string_wrapper_dev.h"
#include "ppapi/cpp/dev/struct_wrapper_output_traits_dev.h"
#include "ppapi/cpp/dev/text_input_dev.h"
#include "ppapi/cpp/dev/to_c_type_converter_dev.h"
#include "ppapi/cpp/dev/url_util_dev.h"
#include "ppapi/cpp/dev/video_decoder_dev.h"
#include "ppapi/cpp/dev/view_dev.h"
......
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