Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: content/common/android/gin_java_bridge_value.cc

Issue 252393004: [Android] Add GinJavaBridgeValue for Gin Java Bridge (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/common/android/gin_java_bridge_value.h"
6
7 namespace content {
8
9 namespace {
10
11 // The magic value is only used to prevent accidental attempts of reading
12 // GinJavaBridgeValue from a random BinaryValue. GinJavaBridgeValue is not
13 // intended for scenarios where with BinaryValues are being used for anything
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
16 // to wrap raw BinaryValues.
17 const uint32 kHeaderMagic = 0xBEEFCAFE;
18
19 #pragma pack(push, 4)
20 struct Header : public Pickle::Header {
21 uint32 magic;
22 int32 type;
23 };
24 #pragma pack(pop)
25
26 }
27
28 // static
29 base::BinaryValue* GinJavaBridgeValue::CreateUndefinedValue() {
30 GinJavaBridgeValue gin_value(TYPE_UNDEFINED);
31 return gin_value.SerializeToBinaryValue();
32 }
33
34 // static
35 base::BinaryValue* GinJavaBridgeValue::CreateNonFiniteValue(float in_value) {
36 GinJavaBridgeValue gin_value(TYPE_NONFINITE);
37 gin_value.pickle_.WriteFloat(in_value);
38 return gin_value.SerializeToBinaryValue();
39 }
40
41 // static
42 base::BinaryValue* GinJavaBridgeValue::CreateNonFiniteValue(double in_value) {
43 return CreateNonFiniteValue(static_cast<float>(in_value));
44 }
45
46 // static
47 base::BinaryValue* GinJavaBridgeValue::CreateObjectIDValue(int32 in_value) {
48 GinJavaBridgeValue gin_value(TYPE_OBJECT_ID);
49 gin_value.pickle_.WriteInt(in_value);
50 return gin_value.SerializeToBinaryValue();
51 }
52
53 // static
54 bool GinJavaBridgeValue::ContainsGinJavaBridgeValue(const base::Value* value) {
55 if (!value->IsType(base::Value::TYPE_BINARY))
56 return false;
57 const base::BinaryValue* binary_value =
58 reinterpret_cast<const base::BinaryValue*>(value);
59 if (binary_value->GetSize() < sizeof(Header))
60 return false;
61 Pickle pickle(binary_value->GetBuffer(), binary_value->GetSize());
62 // Broken binary value: payload or header size is wrong
63 if (!pickle.data() || pickle.size() - pickle.payload_size() != sizeof(Header))
64 return false;
65 Header* header = pickle.headerT<Header>();
66 return (header->magic == kHeaderMagic &&
67 header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE);
68 }
69
70 // static
71 const GinJavaBridgeValue* GinJavaBridgeValue::FromValue(
72 const base::Value* value) {
73 if (!value->IsType(base::Value::TYPE_BINARY))
74 return NULL;
75 return new GinJavaBridgeValue(
76 reinterpret_cast<const base::BinaryValue*>(value));
77 }
78
79 GinJavaBridgeValue::Type GinJavaBridgeValue::GetType() const {
80 const Header* header = pickle_.headerT<Header>();
81 DCHECK(header->type >= TYPE_FIRST_VALUE && header->type < TYPE_LAST_VALUE);
82 return static_cast<Type>(header->type);
83 }
84
85 bool GinJavaBridgeValue::IsType(Type type) const {
86 return GetType() == type;
87 }
88
89 bool GinJavaBridgeValue::GetAsNonFinite(float* out_value) const {
90 if (GetType() == TYPE_NONFINITE) {
91 PickleIterator iter(pickle_);
92 return iter.ReadFloat(out_value);
93 } else {
94 return false;
95 }
96 }
97
98 bool GinJavaBridgeValue::GetAsObjectID(int32* out_object_id) const {
99 if (GetType() == TYPE_OBJECT_ID) {
100 PickleIterator iter(pickle_);
101 return iter.ReadInt(out_object_id);
102 } else {
103 return false;
104 }
105 }
106
107 GinJavaBridgeValue::GinJavaBridgeValue(Type type) :
108 pickle_(sizeof(Header)) {
109 Header* header = pickle_.headerT<Header>();
110 header->magic = kHeaderMagic;
111 header->type = type;
112 }
113
114 GinJavaBridgeValue::GinJavaBridgeValue(const base::BinaryValue* value)
115 : pickle_(value->GetBuffer(), value->GetSize()) {
116 DCHECK(ContainsGinJavaBridgeValue(value));
117 }
118
119 base::BinaryValue* GinJavaBridgeValue::SerializeToBinaryValue() {
120 return base::BinaryValue::CreateWithCopiedBuffer(
121 reinterpret_cast<const char*>(pickle_.data()), pickle_.size());
122 }
123
124 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698