| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/android/gin_java_bridge_value.h" | 5 #include "content/common/android/gin_java_bridge_value.h" |
| 6 | 6 |
| 7 namespace content { | 7 namespace content { |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| 11 // The magic value is only used to prevent accidental attempts of reading | 11 // The magic value is only used to prevent accidental attempts of reading |
| 12 // GinJavaBridgeValue from a random BinaryValue. GinJavaBridgeValue is not | 12 // GinJavaBridgeValue from a random BinaryValue. GinJavaBridgeValue is not |
| 13 // intended for scenarios where with BinaryValues are being used for anything | 13 // intended for scenarios where with BinaryValues are being used for anything |
| 14 // else than holding GinJavaBridgeValues. If a need for such scenario ever | 14 // else than holding GinJavaBridgeValues. If a need for such scenario ever |
| 15 // emerges, the best solution would be to extend GinJavaBridgeValue to be able | 15 // emerges, the best solution would be to extend GinJavaBridgeValue to be able |
| 16 // to wrap raw BinaryValues. | 16 // to wrap raw BinaryValues. |
| 17 const uint32_t kHeaderMagic = 0xBEEFCAFE; | 17 const uint32_t kHeaderMagic = 0xBEEFCAFE; |
| 18 | 18 |
| 19 #pragma pack(push, 4) | 19 #pragma pack(push, 4) |
| 20 struct Header : public base::Pickle::Header { | 20 struct Header : public base::Pickle::Header { |
| 21 uint32_t magic; | 21 uint32_t magic; |
| 22 int32_t type; | 22 int32_t type; |
| 23 }; | 23 }; |
| 24 #pragma pack(pop) | 24 #pragma pack(pop) |
| 25 | 25 |
| 26 } | 26 } |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateUndefinedValue() { | 29 std::unique_ptr<base::Value> GinJavaBridgeValue::CreateUndefinedValue() { |
| 30 GinJavaBridgeValue gin_value(TYPE_UNDEFINED); | 30 GinJavaBridgeValue gin_value(TYPE_UNDEFINED); |
| 31 return gin_value.SerializeToBinaryValue(); | 31 return gin_value.SerializeToBinaryValue(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 // static | 34 // static |
| 35 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateNonFiniteValue( | 35 std::unique_ptr<base::Value> GinJavaBridgeValue::CreateNonFiniteValue( |
| 36 float in_value) { | 36 float in_value) { |
| 37 GinJavaBridgeValue gin_value(TYPE_NONFINITE); | 37 GinJavaBridgeValue gin_value(TYPE_NONFINITE); |
| 38 gin_value.pickle_.WriteFloat(in_value); | 38 gin_value.pickle_.WriteFloat(in_value); |
| 39 return gin_value.SerializeToBinaryValue(); | 39 return gin_value.SerializeToBinaryValue(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 // static | 42 // static |
| 43 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateNonFiniteValue( | 43 std::unique_ptr<base::Value> GinJavaBridgeValue::CreateNonFiniteValue( |
| 44 double in_value) { | 44 double in_value) { |
| 45 return CreateNonFiniteValue(static_cast<float>(in_value)); | 45 return CreateNonFiniteValue(static_cast<float>(in_value)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 // static | 48 // static |
| 49 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateObjectIDValue( | 49 std::unique_ptr<base::Value> GinJavaBridgeValue::CreateObjectIDValue( |
| 50 int32_t in_value) { | 50 int32_t in_value) { |
| 51 GinJavaBridgeValue gin_value(TYPE_OBJECT_ID); | 51 GinJavaBridgeValue gin_value(TYPE_OBJECT_ID); |
| 52 gin_value.pickle_.WriteInt(in_value); | 52 gin_value.pickle_.WriteInt(in_value); |
| 53 return gin_value.SerializeToBinaryValue(); | 53 return gin_value.SerializeToBinaryValue(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // static | 56 // static |
| 57 bool GinJavaBridgeValue::ContainsGinJavaBridgeValue(const base::Value* value) { | 57 bool GinJavaBridgeValue::ContainsGinJavaBridgeValue(const base::Value* value) { |
| 58 if (!value->IsType(base::Value::Type::BINARY)) | 58 if (!value->IsType(base::Value::Type::BINARY)) |
| 59 return false; | 59 return false; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 GinJavaBridgeValue::GinJavaBridgeValue(Type type) : | 107 GinJavaBridgeValue::GinJavaBridgeValue(Type type) : |
| 108 pickle_(sizeof(Header)) { | 108 pickle_(sizeof(Header)) { |
| 109 Header* header = pickle_.headerT<Header>(); | 109 Header* header = pickle_.headerT<Header>(); |
| 110 header->magic = kHeaderMagic; | 110 header->magic = kHeaderMagic; |
| 111 header->type = type; | 111 header->type = type; |
| 112 } | 112 } |
| 113 | 113 |
| 114 GinJavaBridgeValue::GinJavaBridgeValue(const base::BinaryValue* value) | 114 GinJavaBridgeValue::GinJavaBridgeValue(const base::Value* value) |
| 115 : pickle_(value->GetBuffer(), value->GetSize()) { | 115 : pickle_(value->GetBuffer(), value->GetSize()) { |
| 116 DCHECK(ContainsGinJavaBridgeValue(value)); | 116 DCHECK(ContainsGinJavaBridgeValue(value)); |
| 117 } | 117 } |
| 118 | 118 |
| 119 std::unique_ptr<base::BinaryValue> | 119 std::unique_ptr<base::Value> GinJavaBridgeValue::SerializeToBinaryValue() { |
| 120 GinJavaBridgeValue::SerializeToBinaryValue() { | 120 return base::Value::CreateWithCopiedBuffer( |
| 121 return base::BinaryValue::CreateWithCopiedBuffer( | |
| 122 reinterpret_cast<const char*>(pickle_.data()), pickle_.size()); | 121 reinterpret_cast<const char*>(pickle_.data()), pickle_.size()); |
| 123 } | 122 } |
| 124 | 123 |
| 125 } // namespace content | 124 } // namespace content |
| OLD | NEW |