Commit 21d14e47 authored by mnaganov@chromium.org's avatar mnaganov@chromium.org

[Android] Add GinJavaBridgeValue for Gin Java Bridge

Gin Java Bridge passes values between Java and JS using base::Value.
Since there are some kinds of data that can't be represented in JSON,
we use base::BinaryValue that holds GinJavaBridgeValue. And we don't
use base::BinaryValue for anything else (this is enforced by our
Strategy for V8ValueConverter).

BUG=355644
R=bulach@chromium.org, jochen@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@266527 0039d316-1c4b-4281-b951-d872f2087c98
parent eeaafd0e
// 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 "content/common/android/gin_java_bridge_value.h"
namespace content {
namespace {
// The magic value is only used to prevent accidental attempts of reading
// GinJavaBridgeValue from a random BinaryValue. GinJavaBridgeValue is not
// intended for scenarios where with BinaryValues are being used for anything
// else than holding GinJavaBridgeValues. If a need for such scenario ever
// emerges, the best solution would be to extend GinJavaBridgeValue to be able
// to wrap raw BinaryValues.
const uint32 kHeaderMagic = 0xBEEFCAFE;
#pragma pack(push, 4)
struct Header : public Pickle::Header {
uint32 magic;
int32 type;
};
#pragma pack(pop)
}
// static
scoped_ptr<base::BinaryValue> GinJavaBridgeValue::CreateUndefinedValue() {
GinJavaBridgeValue gin_value(TYPE_UNDEFINED);
return make_scoped_ptr(gin_value.SerializeToBinaryValue());
}
// static
scoped_ptr<base::BinaryValue> GinJavaBridgeValue::CreateNonFiniteValue(
float in_value) {
GinJavaBridgeValue gin_value(TYPE_NONFINITE);
gin_value.pickle_.WriteFloat(in_value);
return make_scoped_ptr(gin_value.SerializeToBinaryValue());
}
// static
scoped_ptr<base::BinaryValue> GinJavaBridgeValue::CreateNonFiniteValue(
double in_value) {
return CreateNonFiniteValue(static_cast<float>(in_value)).Pass();
}
// static
scoped_ptr<base::BinaryValue> GinJavaBridgeValue::CreateObjectIDValue(
int32 in_value) {
GinJavaBridgeValue gin_value(TYPE_OBJECT_ID);
gin_value.pickle_.WriteInt(in_value);
return make_scoped_ptr(gin_value.SerializeToBinaryValue());
}
// static
bool GinJavaBridgeValue::ContainsGinJavaBridgeValue(const base::Value* value) {
if (!value->IsType(base::Value::TYPE_BINARY))
return false;
const base::BinaryValue* binary_value =
reinterpret_cast<const base::BinaryValue*>(value);
if (binary_value->GetSize() < sizeof(Header))
return false;
Pickle pickle(binary_value->GetBuffer(), binary_value->GetSize());
// Broken binary value: payload or header size is wrong
if (!pickle.data() || pickle.size() - pickle.payload_size() != sizeof(Header))
return false;
Header* header = pickle.headerT<Header>();
return (header->magic == kHeaderMagic &&
header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE);
}
// static
scoped_ptr<const GinJavaBridgeValue> GinJavaBridgeValue::FromValue(
const base::Value* value) {
return scoped_ptr<const GinJavaBridgeValue>(
value->IsType(base::Value::TYPE_BINARY)
? new GinJavaBridgeValue(
reinterpret_cast<const base::BinaryValue*>(value))
: NULL);
}
GinJavaBridgeValue::Type GinJavaBridgeValue::GetType() const {
const Header* header = pickle_.headerT<Header>();
DCHECK(header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE);
return static_cast<Type>(header->type);
}
bool GinJavaBridgeValue::IsType(Type type) const {
return GetType() == type;
}
bool GinJavaBridgeValue::GetAsNonFinite(float* out_value) const {
if (GetType() == TYPE_NONFINITE) {
PickleIterator iter(pickle_);
return iter.ReadFloat(out_value);
} else {
return false;
}
}
bool GinJavaBridgeValue::GetAsObjectID(int32* out_object_id) const {
if (GetType() == TYPE_OBJECT_ID) {
PickleIterator iter(pickle_);
return iter.ReadInt(out_object_id);
} else {
return false;
}
}
GinJavaBridgeValue::GinJavaBridgeValue(Type type) :
pickle_(sizeof(Header)) {
Header* header = pickle_.headerT<Header>();
header->magic = kHeaderMagic;
header->type = type;
}
GinJavaBridgeValue::GinJavaBridgeValue(const base::BinaryValue* value)
: pickle_(value->GetBuffer(), value->GetSize()) {
DCHECK(ContainsGinJavaBridgeValue(value));
}
base::BinaryValue* GinJavaBridgeValue::SerializeToBinaryValue() {
return base::BinaryValue::CreateWithCopiedBuffer(
reinterpret_cast<const char*>(pickle_.data()), pickle_.size());
}
} // namespace content
// 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 CONTENT_COMMON_ANDROID_GIN_JAVA_BRIDGE_VALUE_H_
#define CONTENT_COMMON_ANDROID_GIN_JAVA_BRIDGE_VALUE_H_
#include "base/memory/scoped_ptr.h"
#include "base/pickle.h"
#include "base/values.h"
#include "content/common/content_export.h"
// In Java Bridge, we need to pass some kinds of values that can't
// be put into base::Value. And since base::Value is not extensible,
// we transfer these special values via base::BinaryValue.
namespace content {
class GinJavaBridgeValue {
public:
enum Type {
TYPE_FIRST_VALUE = 0,
// JavaScript 'undefined'
TYPE_UNDEFINED = 0,
// JavaScript NaN and Infinity
TYPE_NONFINITE,
// Bridge Object ID
TYPE_OBJECT_ID,
TYPE_LAST_VALUE
};
// Serialization
CONTENT_EXPORT static scoped_ptr<base::BinaryValue> CreateUndefinedValue();
CONTENT_EXPORT static scoped_ptr<base::BinaryValue> CreateNonFiniteValue(
float in_value);
CONTENT_EXPORT static scoped_ptr<base::BinaryValue> CreateNonFiniteValue(
double in_value);
CONTENT_EXPORT static scoped_ptr<base::BinaryValue> CreateObjectIDValue(
int32 in_value);
// De-serialization
CONTENT_EXPORT static bool ContainsGinJavaBridgeValue(
const base::Value* value);
CONTENT_EXPORT static scoped_ptr<const GinJavaBridgeValue> FromValue(
const base::Value* value);
CONTENT_EXPORT Type GetType() const;
CONTENT_EXPORT bool IsType(Type type) const;
CONTENT_EXPORT bool GetAsNonFinite(float* out_value) const;
CONTENT_EXPORT bool GetAsObjectID(int32* out_object_id) const;
private:
explicit GinJavaBridgeValue(Type type);
explicit GinJavaBridgeValue(const base::BinaryValue* value);
base::BinaryValue* SerializeToBinaryValue();
Pickle pickle_;
DISALLOW_COPY_AND_ASSIGN(GinJavaBridgeValue);
};
} // namespace content
#endif // CONTENT_COMMON_ANDROID_GIN_JAVA_BRIDGE_VALUE_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 "base/basictypes.h"
#include "base/float_util.h"
#include "base/memory/scoped_ptr.h"
#include "content/common/android/gin_java_bridge_value.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
class GinJavaBridgeValueTest : public testing::Test {
};
TEST_F(GinJavaBridgeValueTest, BasicValues) {
float native_float;
int32 native_object_id;
scoped_ptr<base::BinaryValue> undefined(
GinJavaBridgeValue::CreateUndefinedValue());
ASSERT_TRUE(undefined.get());
EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(undefined.get()));
scoped_ptr<const GinJavaBridgeValue> undefined_value(
GinJavaBridgeValue::FromValue(undefined.get()));
ASSERT_TRUE(undefined_value.get());
EXPECT_TRUE(undefined_value->IsType(GinJavaBridgeValue::TYPE_UNDEFINED));
EXPECT_FALSE(undefined_value->GetAsNonFinite(&native_float));
EXPECT_FALSE(undefined_value->GetAsObjectID(&native_object_id));
scoped_ptr<base::BinaryValue> float_infinity(
GinJavaBridgeValue::CreateNonFiniteValue(
std::numeric_limits<float>::infinity()));
ASSERT_TRUE(float_infinity.get());
EXPECT_TRUE(
GinJavaBridgeValue::ContainsGinJavaBridgeValue(float_infinity.get()));
scoped_ptr<const GinJavaBridgeValue> float_infinity_value(
GinJavaBridgeValue::FromValue(float_infinity.get()));
ASSERT_TRUE(float_infinity_value.get());
EXPECT_TRUE(float_infinity_value->IsType(GinJavaBridgeValue::TYPE_NONFINITE));
EXPECT_TRUE(float_infinity_value->GetAsNonFinite(&native_float));
EXPECT_FALSE(base::IsFinite(native_float));
EXPECT_FALSE(base::IsNaN(native_float));
EXPECT_FALSE(undefined_value->GetAsObjectID(&native_object_id));
scoped_ptr<base::BinaryValue> double_infinity(
GinJavaBridgeValue::CreateNonFiniteValue(
std::numeric_limits<double>::infinity()));
ASSERT_TRUE(double_infinity.get());
EXPECT_TRUE(
GinJavaBridgeValue::ContainsGinJavaBridgeValue(double_infinity.get()));
scoped_ptr<const GinJavaBridgeValue> double_infinity_value(
GinJavaBridgeValue::FromValue(double_infinity.get()));
ASSERT_TRUE(double_infinity_value.get());
EXPECT_TRUE(
double_infinity_value->IsType(GinJavaBridgeValue::TYPE_NONFINITE));
EXPECT_TRUE(double_infinity_value->GetAsNonFinite(&native_float));
EXPECT_FALSE(base::IsFinite(native_float));
EXPECT_FALSE(base::IsNaN(native_float));
EXPECT_FALSE(undefined_value->GetAsObjectID(&native_object_id));
scoped_ptr<base::BinaryValue> object_id(
GinJavaBridgeValue::CreateObjectIDValue(42));
ASSERT_TRUE(object_id.get());
EXPECT_TRUE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(object_id.get()));
scoped_ptr<const GinJavaBridgeValue> object_id_value(
GinJavaBridgeValue::FromValue(object_id.get()));
ASSERT_TRUE(object_id_value.get());
EXPECT_TRUE(object_id_value->IsType(GinJavaBridgeValue::TYPE_OBJECT_ID));
EXPECT_TRUE(object_id_value->GetAsObjectID(&native_object_id));
EXPECT_EQ(42, native_object_id);
EXPECT_FALSE(undefined_value->GetAsNonFinite(&native_float));
}
TEST_F(GinJavaBridgeValueTest, BrokenValues) {
scoped_ptr<base::Value> non_binary(new base::FundamentalValue(42));
EXPECT_FALSE(
GinJavaBridgeValue::ContainsGinJavaBridgeValue(non_binary.get()));
const char dummy_data[] = "\000\001\002\003\004\005\006\007\010\011\012\013";
scoped_ptr<base::BinaryValue> broken_binary(
base::BinaryValue::CreateWithCopiedBuffer(dummy_data,
sizeof(dummy_data)));
EXPECT_FALSE(
GinJavaBridgeValue::ContainsGinJavaBridgeValue(broken_binary.get()));
}
} // namespace
...@@ -133,6 +133,8 @@ ...@@ -133,6 +133,8 @@
'common/android/common_jni_registrar.h', 'common/android/common_jni_registrar.h',
'common/android/device_telephony_info.cc', 'common/android/device_telephony_info.cc',
'common/android/device_telephony_info.h', 'common/android/device_telephony_info.h',
'common/android/gin_java_bridge_value.cc',
'common/android/gin_java_bridge_value.h',
'common/android/hash_set.cc', 'common/android/hash_set.cc',
'common/android/hash_set.h', 'common/android/hash_set.h',
'common/android/surface_texture_lookup.cc', 'common/android/surface_texture_lookup.cc',
......
...@@ -604,6 +604,7 @@ ...@@ -604,6 +604,7 @@
'child/webcrypto/shared_crypto_unittest.cc', 'child/webcrypto/shared_crypto_unittest.cc',
'child/worker_task_runner_unittest.cc', 'child/worker_task_runner_unittest.cc',
'common/android/address_parser_unittest.cc', 'common/android/address_parser_unittest.cc',
'common/android/gin_java_bridge_value_unittest.cc',
'common/cc_messages_unittest.cc', 'common/cc_messages_unittest.cc',
'common/common_param_traits_unittest.cc', 'common/common_param_traits_unittest.cc',
'common/cursors/webcursor_unittest.cc', 'common/cursors/webcursor_unittest.cc',
......
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