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 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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; |
60 const base::BinaryValue* binary_value = | 60 if (value->GetSize() < sizeof(Header)) |
61 reinterpret_cast<const base::BinaryValue*>(value); | |
62 if (binary_value->GetSize() < sizeof(Header)) | |
63 return false; | 61 return false; |
64 base::Pickle pickle(binary_value->GetBuffer(), binary_value->GetSize()); | 62 base::Pickle pickle(value->GetBuffer(), value->GetSize()); |
65 // Broken binary value: payload or header size is wrong | 63 // Broken binary value: payload or header size is wrong |
66 if (!pickle.data() || pickle.size() - pickle.payload_size() != sizeof(Header)) | 64 if (!pickle.data() || pickle.size() - pickle.payload_size() != sizeof(Header)) |
67 return false; | 65 return false; |
68 Header* header = pickle.headerT<Header>(); | 66 Header* header = pickle.headerT<Header>(); |
69 return (header->magic == kHeaderMagic && | 67 return (header->magic == kHeaderMagic && |
70 header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE); | 68 header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE); |
71 } | 69 } |
72 | 70 |
73 // static | 71 // static |
74 std::unique_ptr<const GinJavaBridgeValue> GinJavaBridgeValue::FromValue( | 72 std::unique_ptr<const GinJavaBridgeValue> GinJavaBridgeValue::FromValue( |
75 const base::Value* value) { | 73 const base::Value* value) { |
76 return std::unique_ptr<const GinJavaBridgeValue>( | 74 return std::unique_ptr<const GinJavaBridgeValue>( |
77 value->IsType(base::Value::Type::BINARY) | 75 value->IsType(base::Value::Type::BINARY) ? new GinJavaBridgeValue(value) |
78 ? new GinJavaBridgeValue( | 76 : NULL); |
79 reinterpret_cast<const base::BinaryValue*>(value)) | |
80 : NULL); | |
81 } | 77 } |
82 | 78 |
83 GinJavaBridgeValue::Type GinJavaBridgeValue::GetType() const { | 79 GinJavaBridgeValue::Type GinJavaBridgeValue::GetType() const { |
84 const Header* header = pickle_.headerT<Header>(); | 80 const Header* header = pickle_.headerT<Header>(); |
85 DCHECK(header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE); | 81 DCHECK(header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE); |
86 return static_cast<Type>(header->type); | 82 return static_cast<Type>(header->type); |
87 } | 83 } |
88 | 84 |
89 bool GinJavaBridgeValue::IsType(Type type) const { | 85 bool GinJavaBridgeValue::IsType(Type type) const { |
90 return GetType() == type; | 86 return GetType() == type; |
(...skipping 29 matching lines...) Expand all Loading... |
120 DCHECK(ContainsGinJavaBridgeValue(value)); | 116 DCHECK(ContainsGinJavaBridgeValue(value)); |
121 } | 117 } |
122 | 118 |
123 std::unique_ptr<base::BinaryValue> | 119 std::unique_ptr<base::BinaryValue> |
124 GinJavaBridgeValue::SerializeToBinaryValue() { | 120 GinJavaBridgeValue::SerializeToBinaryValue() { |
125 return base::BinaryValue::CreateWithCopiedBuffer( | 121 return base::BinaryValue::CreateWithCopiedBuffer( |
126 reinterpret_cast<const char*>(pickle_.data()), pickle_.size()); | 122 reinterpret_cast<const char*>(pickle_.data()), pickle_.size()); |
127 } | 123 } |
128 | 124 |
129 } // namespace content | 125 } // namespace content |
OLD | NEW |