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 #include "base/memory/ptr_util.h" | |
8 | |
9 namespace content { | 7 namespace content { |
10 | 8 |
11 namespace { | 9 namespace { |
12 | 10 |
13 // 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 |
14 // GinJavaBridgeValue from a random BinaryValue. GinJavaBridgeValue is not | 12 // GinJavaBridgeValue from a random BinaryValue. GinJavaBridgeValue is not |
15 // intended for scenarios where with BinaryValues are being used for anything | 13 // intended for scenarios where with BinaryValues are being used for anything |
16 // else than holding GinJavaBridgeValues. If a need for such scenario ever | 14 // else than holding GinJavaBridgeValues. If a need for such scenario ever |
17 // 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 |
18 // to wrap raw BinaryValues. | 16 // to wrap raw BinaryValues. |
19 const uint32_t kHeaderMagic = 0xBEEFCAFE; | 17 const uint32_t kHeaderMagic = 0xBEEFCAFE; |
20 | 18 |
21 #pragma pack(push, 4) | 19 #pragma pack(push, 4) |
22 struct Header : public base::Pickle::Header { | 20 struct Header : public base::Pickle::Header { |
23 uint32_t magic; | 21 uint32_t magic; |
24 int32_t type; | 22 int32_t type; |
25 }; | 23 }; |
26 #pragma pack(pop) | 24 #pragma pack(pop) |
27 | 25 |
28 } | 26 } |
29 | 27 |
30 // static | 28 // static |
31 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateUndefinedValue() { | 29 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateUndefinedValue() { |
32 GinJavaBridgeValue gin_value(TYPE_UNDEFINED); | 30 GinJavaBridgeValue gin_value(TYPE_UNDEFINED); |
33 return base::WrapUnique(gin_value.SerializeToBinaryValue()); | 31 return gin_value.SerializeToBinaryValue(); |
34 } | 32 } |
35 | 33 |
36 // static | 34 // static |
37 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateNonFiniteValue( | 35 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateNonFiniteValue( |
38 float in_value) { | 36 float in_value) { |
39 GinJavaBridgeValue gin_value(TYPE_NONFINITE); | 37 GinJavaBridgeValue gin_value(TYPE_NONFINITE); |
40 gin_value.pickle_.WriteFloat(in_value); | 38 gin_value.pickle_.WriteFloat(in_value); |
41 return base::WrapUnique(gin_value.SerializeToBinaryValue()); | 39 return gin_value.SerializeToBinaryValue(); |
42 } | 40 } |
43 | 41 |
44 // static | 42 // static |
45 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateNonFiniteValue( | 43 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateNonFiniteValue( |
46 double in_value) { | 44 double in_value) { |
47 return CreateNonFiniteValue(static_cast<float>(in_value)); | 45 return CreateNonFiniteValue(static_cast<float>(in_value)); |
48 } | 46 } |
49 | 47 |
50 // static | 48 // static |
51 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateObjectIDValue( | 49 std::unique_ptr<base::BinaryValue> GinJavaBridgeValue::CreateObjectIDValue( |
52 int32_t in_value) { | 50 int32_t in_value) { |
53 GinJavaBridgeValue gin_value(TYPE_OBJECT_ID); | 51 GinJavaBridgeValue gin_value(TYPE_OBJECT_ID); |
54 gin_value.pickle_.WriteInt(in_value); | 52 gin_value.pickle_.WriteInt(in_value); |
55 return base::WrapUnique(gin_value.SerializeToBinaryValue()); | 53 return gin_value.SerializeToBinaryValue(); |
56 } | 54 } |
57 | 55 |
58 // static | 56 // static |
59 bool GinJavaBridgeValue::ContainsGinJavaBridgeValue(const base::Value* value) { | 57 bool GinJavaBridgeValue::ContainsGinJavaBridgeValue(const base::Value* value) { |
60 if (!value->IsType(base::Value::TYPE_BINARY)) | 58 if (!value->IsType(base::Value::TYPE_BINARY)) |
61 return false; | 59 return false; |
62 const base::BinaryValue* binary_value = | 60 const base::BinaryValue* binary_value = |
63 reinterpret_cast<const base::BinaryValue*>(value); | 61 reinterpret_cast<const base::BinaryValue*>(value); |
64 if (binary_value->GetSize() < sizeof(Header)) | 62 if (binary_value->GetSize() < sizeof(Header)) |
65 return false; | 63 return false; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 Header* header = pickle_.headerT<Header>(); | 113 Header* header = pickle_.headerT<Header>(); |
116 header->magic = kHeaderMagic; | 114 header->magic = kHeaderMagic; |
117 header->type = type; | 115 header->type = type; |
118 } | 116 } |
119 | 117 |
120 GinJavaBridgeValue::GinJavaBridgeValue(const base::BinaryValue* value) | 118 GinJavaBridgeValue::GinJavaBridgeValue(const base::BinaryValue* value) |
121 : pickle_(value->GetBuffer(), value->GetSize()) { | 119 : pickle_(value->GetBuffer(), value->GetSize()) { |
122 DCHECK(ContainsGinJavaBridgeValue(value)); | 120 DCHECK(ContainsGinJavaBridgeValue(value)); |
123 } | 121 } |
124 | 122 |
125 base::BinaryValue* GinJavaBridgeValue::SerializeToBinaryValue() { | 123 std::unique_ptr<base::BinaryValue> |
| 124 GinJavaBridgeValue::SerializeToBinaryValue() { |
126 return base::BinaryValue::CreateWithCopiedBuffer( | 125 return base::BinaryValue::CreateWithCopiedBuffer( |
127 reinterpret_cast<const char*>(pickle_.data()), pickle_.size()); | 126 reinterpret_cast<const char*>(pickle_.data()), pickle_.size()); |
128 } | 127 } |
129 | 128 |
130 } // namespace content | 129 } // namespace content |
OLD | NEW |