| 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 kHeaderMagic = 0xBEEFCAFE; | 17 const uint32 kHeaderMagic = 0xBEEFCAFE; |
| 18 | 18 |
| 19 #pragma pack(push, 4) | 19 #pragma pack(push, 4) |
| 20 struct Header : public Pickle::Header { | 20 struct Header : public base::Pickle::Header { |
| 21 uint32 magic; | 21 uint32 magic; |
| 22 int32 type; | 22 int32 type; |
| 23 }; | 23 }; |
| 24 #pragma pack(pop) | 24 #pragma pack(pop) |
| 25 | 25 |
| 26 } | 26 } |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 scoped_ptr<base::BinaryValue> GinJavaBridgeValue::CreateUndefinedValue() { | 29 scoped_ptr<base::BinaryValue> GinJavaBridgeValue::CreateUndefinedValue() { |
| 30 GinJavaBridgeValue gin_value(TYPE_UNDEFINED); | 30 GinJavaBridgeValue gin_value(TYPE_UNDEFINED); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 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; |
| 60 const base::BinaryValue* binary_value = | 60 const base::BinaryValue* binary_value = |
| 61 reinterpret_cast<const base::BinaryValue*>(value); | 61 reinterpret_cast<const base::BinaryValue*>(value); |
| 62 if (binary_value->GetSize() < sizeof(Header)) | 62 if (binary_value->GetSize() < sizeof(Header)) |
| 63 return false; | 63 return false; |
| 64 Pickle pickle(binary_value->GetBuffer(), binary_value->GetSize()); | 64 base::Pickle pickle(binary_value->GetBuffer(), binary_value->GetSize()); |
| 65 // Broken binary value: payload or header size is wrong | 65 // Broken binary value: payload or header size is wrong |
| 66 if (!pickle.data() || pickle.size() - pickle.payload_size() != sizeof(Header)) | 66 if (!pickle.data() || pickle.size() - pickle.payload_size() != sizeof(Header)) |
| 67 return false; | 67 return false; |
| 68 Header* header = pickle.headerT<Header>(); | 68 Header* header = pickle.headerT<Header>(); |
| 69 return (header->magic == kHeaderMagic && | 69 return (header->magic == kHeaderMagic && |
| 70 header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE); | 70 header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // static | 73 // static |
| 74 scoped_ptr<const GinJavaBridgeValue> GinJavaBridgeValue::FromValue( | 74 scoped_ptr<const GinJavaBridgeValue> GinJavaBridgeValue::FromValue( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 85 DCHECK(header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE); | 85 DCHECK(header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE); |
| 86 return static_cast<Type>(header->type); | 86 return static_cast<Type>(header->type); |
| 87 } | 87 } |
| 88 | 88 |
| 89 bool GinJavaBridgeValue::IsType(Type type) const { | 89 bool GinJavaBridgeValue::IsType(Type type) const { |
| 90 return GetType() == type; | 90 return GetType() == type; |
| 91 } | 91 } |
| 92 | 92 |
| 93 bool GinJavaBridgeValue::GetAsNonFinite(float* out_value) const { | 93 bool GinJavaBridgeValue::GetAsNonFinite(float* out_value) const { |
| 94 if (GetType() == TYPE_NONFINITE) { | 94 if (GetType() == TYPE_NONFINITE) { |
| 95 PickleIterator iter(pickle_); | 95 base::PickleIterator iter(pickle_); |
| 96 return iter.ReadFloat(out_value); | 96 return iter.ReadFloat(out_value); |
| 97 } else { | 97 } else { |
| 98 return false; | 98 return false; |
| 99 } | 99 } |
| 100 } | 100 } |
| 101 | 101 |
| 102 bool GinJavaBridgeValue::GetAsObjectID(int32* out_object_id) const { | 102 bool GinJavaBridgeValue::GetAsObjectID(int32* out_object_id) const { |
| 103 if (GetType() == TYPE_OBJECT_ID) { | 103 if (GetType() == TYPE_OBJECT_ID) { |
| 104 PickleIterator iter(pickle_); | 104 base::PickleIterator iter(pickle_); |
| 105 return iter.ReadInt(out_object_id); | 105 return iter.ReadInt(out_object_id); |
| 106 } else { | 106 } else { |
| 107 return false; | 107 return false; |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 | 110 |
| 111 GinJavaBridgeValue::GinJavaBridgeValue(Type type) : | 111 GinJavaBridgeValue::GinJavaBridgeValue(Type type) : |
| 112 pickle_(sizeof(Header)) { | 112 pickle_(sizeof(Header)) { |
| 113 Header* header = pickle_.headerT<Header>(); | 113 Header* header = pickle_.headerT<Header>(); |
| 114 header->magic = kHeaderMagic; | 114 header->magic = kHeaderMagic; |
| 115 header->type = type; | 115 header->type = type; |
| 116 } | 116 } |
| 117 | 117 |
| 118 GinJavaBridgeValue::GinJavaBridgeValue(const base::BinaryValue* value) | 118 GinJavaBridgeValue::GinJavaBridgeValue(const base::BinaryValue* value) |
| 119 : pickle_(value->GetBuffer(), value->GetSize()) { | 119 : pickle_(value->GetBuffer(), value->GetSize()) { |
| 120 DCHECK(ContainsGinJavaBridgeValue(value)); | 120 DCHECK(ContainsGinJavaBridgeValue(value)); |
| 121 } | 121 } |
| 122 | 122 |
| 123 base::BinaryValue* GinJavaBridgeValue::SerializeToBinaryValue() { | 123 base::BinaryValue* GinJavaBridgeValue::SerializeToBinaryValue() { |
| 124 return base::BinaryValue::CreateWithCopiedBuffer( | 124 return base::BinaryValue::CreateWithCopiedBuffer( |
| 125 reinterpret_cast<const char*>(pickle_.data()), pickle_.size()); | 125 reinterpret_cast<const char*>(pickle_.data()), pickle_.size()); |
| 126 } | 126 } |
| 127 | 127 |
| 128 } // namespace content | 128 } // namespace content |
| OLD | NEW |