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

Side by Side Diff: extensions/renderer/api_binding_test_util.cc

Issue 2947463002: [Extensions Bindings] Add a bindings/ subdirectory under renderer (Closed)
Patch Set: . Created 3 years, 6 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
« no previous file with comments | « extensions/renderer/api_binding_test_util.h ('k') | extensions/renderer/api_binding_types.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "extensions/renderer/api_binding_test_util.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/strings/string_util.h"
10 #include "base/values.h"
11 #include "content/public/child/v8_value_converter.h"
12 #include "gin/converter.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace extensions {
16
17 namespace {
18
19 // Common call function implementation. Calls the given |function| with the
20 // specified |receiver| and arguments. If the call succeeds (doesn't throw an
21 // error), populates |out_value| with the returned result. If the call does
22 // throw, populates |out_error| with the thrown error.
23 // Returns true if the function runs without throwing an error.
24 bool RunFunctionImpl(v8::Local<v8::Function> function,
25 v8::Local<v8::Context> context,
26 v8::Local<v8::Value> receiver,
27 int argc,
28 v8::Local<v8::Value> argv[],
29 v8::Local<v8::Value>* out_value,
30 std::string* out_error) {
31 v8::TryCatch try_catch(context->GetIsolate());
32 v8::MaybeLocal<v8::Value> maybe_result =
33 function->Call(context, receiver, argc, argv);
34 if (try_catch.HasCaught()) {
35 *out_error = gin::V8ToString(try_catch.Message()->Get());
36 return false;
37 }
38 v8::Local<v8::Value> result;
39 if (!maybe_result.ToLocal(&result)) {
40 *out_error = "Could not convert result to v8::Local.";
41 return false;
42 }
43 *out_value = result;
44 return true;
45 }
46
47 } // namespace
48
49 std::string ReplaceSingleQuotes(base::StringPiece str) {
50 std::string result;
51 base::ReplaceChars(str.as_string(), "'", "\"", &result);
52 return result;
53 }
54
55 std::unique_ptr<base::Value> ValueFromString(base::StringPiece str) {
56 std::unique_ptr<base::Value> value =
57 base::JSONReader::Read(ReplaceSingleQuotes(str));
58 EXPECT_TRUE(value) << str;
59 return value;
60 }
61
62 std::unique_ptr<base::ListValue> ListValueFromString(base::StringPiece str) {
63 return base::ListValue::From(ValueFromString(str));
64 }
65
66 std::unique_ptr<base::DictionaryValue> DictionaryValueFromString(
67 base::StringPiece str) {
68 return base::DictionaryValue::From(ValueFromString(str));
69 }
70
71 std::string ValueToString(const base::Value& value) {
72 std::string json;
73 EXPECT_TRUE(base::JSONWriter::Write(value, &json));
74 return json;
75 }
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
93 v8::Local<v8::Value> V8ValueFromScriptSource(v8::Local<v8::Context> context,
94 base::StringPiece source) {
95 v8::MaybeLocal<v8::Script> maybe_script = v8::Script::Compile(
96 context, gin::StringToV8(context->GetIsolate(), source));
97 v8::Local<v8::Script> script;
98 if (!maybe_script.ToLocal(&script))
99 return v8::Local<v8::Value>();
100 return script->Run();
101 }
102
103 v8::Local<v8::Function> FunctionFromString(v8::Local<v8::Context> context,
104 base::StringPiece source) {
105 v8::Local<v8::Value> value = V8ValueFromScriptSource(context, source);
106 v8::Local<v8::Function> function;
107 EXPECT_TRUE(gin::ConvertFromV8(context->GetIsolate(), value, &function));
108 return function;
109 }
110
111 std::unique_ptr<base::Value> V8ToBaseValue(v8::Local<v8::Value> value,
112 v8::Local<v8::Context> context) {
113 return content::V8ValueConverter::Create()->FromV8Value(value, context);
114 }
115
116 v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
117 v8::Local<v8::Context> context,
118 v8::Local<v8::Value> receiver,
119 int argc,
120 v8::Local<v8::Value> argv[]) {
121 std::string error;
122 v8::Local<v8::Value> result;
123 EXPECT_TRUE(
124 RunFunctionImpl(function, context, receiver, argc, argv, &result, &error))
125 << error;
126 EXPECT_FALSE(result.IsEmpty());
127 return result;
128 }
129
130 v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
131 v8::Local<v8::Context> context,
132 int argc,
133 v8::Local<v8::Value> argv[]) {
134 return RunFunction(function, context, v8::Undefined(context->GetIsolate()),
135 argc, argv);
136 }
137
138 v8::Local<v8::Value> RunFunctionOnGlobal(v8::Local<v8::Function> function,
139 v8::Local<v8::Context> context,
140 int argc,
141 v8::Local<v8::Value> argv[]) {
142 return RunFunction(function, context, context->Global(), argc, argv);
143 }
144
145 void RunFunctionOnGlobalAndIgnoreResult(v8::Local<v8::Function> function,
146 v8::Local<v8::Context> context,
147 int argc,
148 v8::Local<v8::Value> argv[]) {
149 RunFunction(function, context, context->Global(), argc, argv);
150 }
151
152 v8::Global<v8::Value> RunFunctionOnGlobalAndReturnHandle(
153 v8::Local<v8::Function> function,
154 v8::Local<v8::Context> context,
155 int argc,
156 v8::Local<v8::Value> argv[]) {
157 return v8::Global<v8::Value>(
158 context->GetIsolate(),
159 RunFunction(function, context, context->Global(), argc, argv));
160 }
161
162 void RunFunctionAndExpectError(v8::Local<v8::Function> function,
163 v8::Local<v8::Context> context,
164 v8::Local<v8::Value> receiver,
165 int argc,
166 v8::Local<v8::Value> argv[],
167 const std::string& expected_error) {
168 std::string error;
169 v8::Local<v8::Value> result;
170 EXPECT_FALSE(RunFunctionImpl(function, context, receiver, argc, argv, &result,
171 &error));
172 EXPECT_TRUE(result.IsEmpty());
173 EXPECT_EQ(expected_error, error);
174 }
175
176 void RunFunctionAndExpectError(v8::Local<v8::Function> function,
177 v8::Local<v8::Context> context,
178 int argc,
179 v8::Local<v8::Value> argv[],
180 const std::string& expected_error) {
181 RunFunctionAndExpectError(function, context,
182 v8::Undefined(context->GetIsolate()), argc, argv,
183 expected_error);
184 }
185
186 v8::Local<v8::Value> GetPropertyFromObject(v8::Local<v8::Object> object,
187 v8::Local<v8::Context> context,
188 base::StringPiece key) {
189 v8::Local<v8::Value> result;
190 EXPECT_TRUE(object->Get(context, gin::StringToV8(context->GetIsolate(), key))
191 .ToLocal(&result));
192 return result;
193 }
194
195 std::unique_ptr<base::Value> GetBaseValuePropertyFromObject(
196 v8::Local<v8::Object> object,
197 v8::Local<v8::Context> context,
198 base::StringPiece key) {
199 return V8ToBaseValue(GetPropertyFromObject(object, context, key), context);
200 }
201
202 std::string GetStringPropertyFromObject(v8::Local<v8::Object> object,
203 v8::Local<v8::Context> context,
204 base::StringPiece key) {
205 return V8ToString(GetPropertyFromObject(object, context, key), context);
206 }
207
208 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/api_binding_test_util.h ('k') | extensions/renderer/api_binding_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698