summaryrefslogtreecommitdiffstats
path: root/ppapi
diff options
context:
space:
mode:
authormgiuca@chromium.org <mgiuca@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-11 03:45:41 +0000
committermgiuca@chromium.org <mgiuca@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-11 03:45:41 +0000
commitd2d95aebafc2477ae93238e1c4aaec9b9b6d6bd1 (patch)
treebb263daf3a609c3159634506b87e76df46d7b674 /ppapi
parentf42e611e88b164225b30873424f867825c32e49f (diff)
downloadchromium_src-d2d95aebafc2477ae93238e1c4aaec9b9b6d6bd1.zip
chromium_src-d2d95aebafc2477ae93238e1c4aaec9b9b6d6bd1.tar.gz
chromium_src-d2d95aebafc2477ae93238e1c4aaec9b9b6d6bd1.tar.bz2
[PPAPI] pp::Var uses the latest version of the PPB_Var API.
It previously used version 1.0 even if version 1.1 was available. BUG=342250 Review URL: https://codereview.chromium.org/158563002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@250314 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r--ppapi/cpp/var.cc64
1 files changed, 42 insertions, 22 deletions
diff --git a/ppapi/cpp/var.cc b/ppapi/cpp/var.cc
index 1108e05..cb8172c 100644
--- a/ppapi/cpp/var.cc
+++ b/ppapi/cpp/var.cc
@@ -39,9 +39,8 @@ inline bool NeedsRefcounting(const PP_Var& var) {
return var.type > PP_VARTYPE_DOUBLE;
}
-// This helper function detects whether PPB_Var version 1.1 is available. If so,
-// it uses it to create a PP_Var for the given string. Otherwise it falls back
-// to PPB_Var version 1.0.
+// This helper function uses the latest available version of VarFromUtf8. Note
+// that version 1.0 of this method has a different API to later versions.
PP_Var VarFromUtf8Helper(const char* utf8_str, uint32_t len) {
if (has_interface<PPB_Var_1_1>()) {
return get_interface<PPB_Var_1_1>()->VarFromUtf8(utf8_str, len);
@@ -49,9 +48,34 @@ PP_Var VarFromUtf8Helper(const char* utf8_str, uint32_t len) {
return get_interface<PPB_Var_1_0>()->VarFromUtf8(Module::Get()->pp_module(),
utf8_str,
len);
- } else {
- return PP_MakeNull();
}
+ return PP_MakeNull();
+}
+
+// This helper function uses the latest available version of AddRef.
+// Returns true on success, false if no appropriate interface was available.
+bool AddRefHelper(const PP_Var& var) {
+ if (has_interface<PPB_Var_1_1>()) {
+ get_interface<PPB_Var_1_1>()->AddRef(var);
+ return true;
+ } else if (has_interface<PPB_Var_1_0>()) {
+ get_interface<PPB_Var_1_0>()->AddRef(var);
+ return true;
+ }
+ return false;
+}
+
+// This helper function uses the latest available version of Release.
+// Returns true on success, false if no appropriate interface was available.
+bool ReleaseHelper(const PP_Var& var) {
+ if (has_interface<PPB_Var_1_1>()) {
+ get_interface<PPB_Var_1_1>()->Release(var);
+ return true;
+ } else if (has_interface<PPB_Var_1_0>()) {
+ get_interface<PPB_Var_1_0>()->Release(var);
+ return true;
+ }
+ return false;
}
} // namespace
@@ -106,9 +130,7 @@ Var::Var(const PP_Var& var) {
var_ = var;
is_managed_ = true;
if (NeedsRefcounting(var_)) {
- if (has_interface<PPB_Var_1_0>())
- get_interface<PPB_Var_1_0>()->AddRef(var_);
- else
+ if (!AddRefHelper(var_))
var_.type = PP_VARTYPE_NULL;
}
}
@@ -117,18 +139,14 @@ Var::Var(const Var& other) {
var_ = other.var_;
is_managed_ = true;
if (NeedsRefcounting(var_)) {
- if (has_interface<PPB_Var_1_0>())
- get_interface<PPB_Var_1_0>()->AddRef(var_);
- else
+ if (!AddRefHelper(var_))
var_.type = PP_VARTYPE_NULL;
}
}
Var::~Var() {
- if (NeedsRefcounting(var_) &&
- is_managed_ &&
- has_interface<PPB_Var_1_0>())
- get_interface<PPB_Var_1_0>()->Release(var_);
+ if (NeedsRefcounting(var_) && is_managed_)
+ ReleaseHelper(var_);
}
Var& Var::operator=(const Var& other) {
@@ -143,12 +161,10 @@ Var& Var::operator=(const Var& other) {
bool old_is_managed = is_managed_;
is_managed_ = true;
if (NeedsRefcounting(other.var_)) {
- // Assume we already has_interface<PPB_Var_1_0> for refcounted vars or else
- // we couldn't have created them in the first place.
- get_interface<PPB_Var_1_0>()->AddRef(other.var_);
+ AddRefHelper(other.var_);
}
if (NeedsRefcounting(var_) && old_is_managed)
- get_interface<PPB_Var_1_0>()->Release(var_);
+ ReleaseHelper(var_);
var_ = other.var_;
return *this;
@@ -212,10 +228,14 @@ std::string Var::AsString() const {
return std::string();
}
- if (!has_interface<PPB_Var_1_0>())
- return std::string();
uint32_t len;
- const char* str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len);
+ const char* str;
+ if (has_interface<PPB_Var_1_1>())
+ str = get_interface<PPB_Var_1_1>()->VarToUtf8(var_, &len);
+ else if (has_interface<PPB_Var_1_0>())
+ str = get_interface<PPB_Var_1_0>()->VarToUtf8(var_, &len);
+ else
+ return std::string();
return std::string(str, len);
}