OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "extensions/renderer/api_binding_test_util.h" | 5 #include "extensions/renderer/api_binding_test_util.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 base::StringPiece str) { | 67 base::StringPiece str) { |
68 return base::DictionaryValue::From(ValueFromString(str)); | 68 return base::DictionaryValue::From(ValueFromString(str)); |
69 } | 69 } |
70 | 70 |
71 std::string ValueToString(const base::Value& value) { | 71 std::string ValueToString(const base::Value& value) { |
72 std::string json; | 72 std::string json; |
73 EXPECT_TRUE(base::JSONWriter::Write(value, &json)); | 73 EXPECT_TRUE(base::JSONWriter::Write(value, &json)); |
74 return json; | 74 return json; |
75 } | 75 } |
76 | 76 |
| 77 std::string V8ToString(v8::Local<v8::Value> value, |
| 78 v8::Local<v8::Context> context) { |
| 79 if (value.IsEmpty()) |
| 80 return "empty"; |
| 81 if (value->IsNull()) |
| 82 return "null"; |
| 83 if (value->IsUndefined()) |
| 84 return "undefined"; |
| 85 if (value->IsFunction()) |
| 86 return "function"; |
| 87 std::unique_ptr<base::Value> json = V8ToBaseValue(value, context); |
| 88 if (!json) |
| 89 return "unserializable"; |
| 90 return ValueToString(*json); |
| 91 } |
| 92 |
77 v8::Local<v8::Value> V8ValueFromScriptSource(v8::Local<v8::Context> context, | 93 v8::Local<v8::Value> V8ValueFromScriptSource(v8::Local<v8::Context> context, |
78 base::StringPiece source) { | 94 base::StringPiece source) { |
79 v8::MaybeLocal<v8::Script> maybe_script = v8::Script::Compile( | 95 v8::MaybeLocal<v8::Script> maybe_script = v8::Script::Compile( |
80 context, gin::StringToV8(context->GetIsolate(), source)); | 96 context, gin::StringToV8(context->GetIsolate(), source)); |
81 v8::Local<v8::Script> script; | 97 v8::Local<v8::Script> script; |
82 if (!maybe_script.ToLocal(&script)) | 98 if (!maybe_script.ToLocal(&script)) |
83 return v8::Local<v8::Value>(); | 99 return v8::Local<v8::Value>(); |
84 return script->Run(); | 100 return script->Run(); |
85 } | 101 } |
86 | 102 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 std::unique_ptr<base::Value> GetBaseValuePropertyFromObject( | 197 std::unique_ptr<base::Value> GetBaseValuePropertyFromObject( |
182 v8::Local<v8::Object> object, | 198 v8::Local<v8::Object> object, |
183 v8::Local<v8::Context> context, | 199 v8::Local<v8::Context> context, |
184 base::StringPiece key) { | 200 base::StringPiece key) { |
185 return V8ToBaseValue(GetPropertyFromObject(object, context, key), context); | 201 return V8ToBaseValue(GetPropertyFromObject(object, context, key), context); |
186 } | 202 } |
187 | 203 |
188 std::string GetStringPropertyFromObject(v8::Local<v8::Object> object, | 204 std::string GetStringPropertyFromObject(v8::Local<v8::Object> object, |
189 v8::Local<v8::Context> context, | 205 v8::Local<v8::Context> context, |
190 base::StringPiece key) { | 206 base::StringPiece key) { |
191 v8::Local<v8::Value> v8_val = GetPropertyFromObject(object, context, key); | 207 return V8ToString(GetPropertyFromObject(object, context, key), context); |
192 if (v8_val.IsEmpty()) | |
193 return "empty"; | |
194 if (v8_val->IsNull()) | |
195 return "null"; | |
196 if (v8_val->IsUndefined()) | |
197 return "undefined"; | |
198 if (v8_val->IsFunction()) | |
199 return "function"; | |
200 std::unique_ptr<base::Value> json_prop = V8ToBaseValue(v8_val, context); | |
201 DCHECK(json_prop) << key; | |
202 return ValueToString(*json_prop); | |
203 } | 208 } |
204 | 209 |
205 } // namespace extensions | 210 } // namespace extensions |
OLD | NEW |