123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- // Copyright 2016 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "extensions/renderer/bindings/api_binding_test_util.h"
- #include "base/json/json_reader.h"
- #include "base/json/json_writer.h"
- #include "base/strings/string_piece.h"
- #include "base/strings/string_util.h"
- #include "base/values.h"
- #include "content/public/renderer/v8_value_converter.h"
- #include "gin/converter.h"
- namespace extensions {
- namespace {
- // Common call function implementation. Calls the given |function| with the
- // specified |receiver| and arguments. If the call succeeds (doesn't throw an
- // error), populates |out_value| with the returned result. If the call does
- // throw, populates |out_error| with the thrown error.
- // Returns true if the function runs without throwing an error.
- bool RunFunctionImpl(v8::Local<v8::Function> function,
- v8::Local<v8::Context> context,
- v8::Local<v8::Value> receiver,
- int argc,
- v8::Local<v8::Value> argv[],
- v8::Local<v8::Value>* out_value,
- std::string* out_error) {
- v8::TryCatch try_catch(context->GetIsolate());
- v8::MaybeLocal<v8::Value> maybe_result =
- function->Call(context, receiver, argc, argv);
- if (try_catch.HasCaught()) {
- *out_error =
- gin::V8ToString(context->GetIsolate(), try_catch.Message()->Get());
- return false;
- }
- v8::Local<v8::Value> result;
- if (!maybe_result.ToLocal(&result)) {
- *out_error = "Could not convert result to v8::Local.";
- return false;
- }
- *out_value = result;
- return true;
- }
- } // namespace
- std::string ReplaceSingleQuotes(base::StringPiece str) {
- std::string result;
- base::ReplaceChars(str, "'", "\"", &result);
- return result;
- }
- base::Value ValueFromString(base::StringPiece str) {
- absl::optional<base::Value> value =
- base::JSONReader::Read(ReplaceSingleQuotes(str));
- if (!value) {
- ADD_FAILURE() << "Failed to parse " << str;
- return base::Value();
- }
- return std::move(value.value());
- }
- base::Value::List ListValueFromString(base::StringPiece str) {
- base::Value value = ValueFromString(str);
- if (value.is_none()) {
- return base::Value::List();
- }
- if (!value.is_list()) {
- ADD_FAILURE() << "Not a list: " << str;
- return base::Value::List();
- }
- return std::move(value.GetList());
- }
- base::Value::Dict DictValueFromString(base::StringPiece str) {
- base::Value value = ValueFromString(str);
- if (value.is_none()) {
- return base::Value::Dict();
- }
- if (!value.is_dict()) {
- ADD_FAILURE() << "Not a dict: " << str;
- return base::Value::Dict();
- }
- return std::move(value.GetDict());
- }
- std::unique_ptr<base::Value> DeprecatedValueFromString(base::StringPiece str) {
- std::unique_ptr<base::Value> value =
- base::JSONReader::ReadDeprecated(ReplaceSingleQuotes(str));
- EXPECT_TRUE(value) << str;
- return value;
- }
- std::unique_ptr<base::ListValue> DeprecatedListValueFromString(
- base::StringPiece str) {
- return base::ListValue::From(DeprecatedValueFromString(str));
- }
- std::unique_ptr<base::DictionaryValue> DeprecatedDictionaryValueFromString(
- base::StringPiece str) {
- return base::DictionaryValue::From(DeprecatedValueFromString(str));
- }
- std::string ValueToString(const base::Value& value) {
- std::string json;
- EXPECT_TRUE(base::JSONWriter::Write(value, &json));
- return json;
- }
- std::string V8ToString(v8::Local<v8::Value> value,
- v8::Local<v8::Context> context) {
- if (value.IsEmpty())
- return "empty";
- if (value->IsNull())
- return "null";
- if (value->IsUndefined())
- return "undefined";
- if (value->IsFunction())
- return "function";
- std::unique_ptr<base::Value> json = V8ToBaseValue(value, context);
- if (!json)
- return "unserializable";
- return ValueToString(*json);
- }
- v8::Local<v8::Value> V8ValueFromScriptSource(v8::Local<v8::Context> context,
- base::StringPiece source) {
- v8::MaybeLocal<v8::Script> maybe_script = v8::Script::Compile(
- context, gin::StringToV8(context->GetIsolate(), source));
- v8::Local<v8::Script> script;
- if (!maybe_script.ToLocal(&script))
- return v8::Local<v8::Value>();
- return script->Run(context).ToLocalChecked();
- }
- v8::Local<v8::Function> FunctionFromString(v8::Local<v8::Context> context,
- base::StringPiece source) {
- v8::Local<v8::Value> value = V8ValueFromScriptSource(context, source);
- v8::Local<v8::Function> function;
- EXPECT_TRUE(gin::ConvertFromV8(context->GetIsolate(), value, &function));
- return function;
- }
- std::unique_ptr<base::Value> V8ToBaseValue(v8::Local<v8::Value> value,
- v8::Local<v8::Context> context) {
- return content::V8ValueConverter::Create()->FromV8Value(value, context);
- }
- v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
- v8::Local<v8::Context> context,
- v8::Local<v8::Value> receiver,
- int argc,
- v8::Local<v8::Value> argv[]) {
- std::string error;
- v8::Local<v8::Value> result;
- EXPECT_TRUE(
- RunFunctionImpl(function, context, receiver, argc, argv, &result, &error))
- << error;
- EXPECT_FALSE(result.IsEmpty());
- return result;
- }
- v8::Local<v8::Value> RunFunction(v8::Local<v8::Function> function,
- v8::Local<v8::Context> context,
- int argc,
- v8::Local<v8::Value> argv[]) {
- return RunFunction(function, context, v8::Undefined(context->GetIsolate()),
- argc, argv);
- }
- v8::Local<v8::Value> RunFunctionOnGlobal(v8::Local<v8::Function> function,
- v8::Local<v8::Context> context,
- int argc,
- v8::Local<v8::Value> argv[]) {
- return RunFunction(function, context, context->Global(), argc, argv);
- }
- void RunFunctionOnGlobalAndIgnoreResult(v8::Local<v8::Function> function,
- v8::Local<v8::Context> context,
- int argc,
- v8::Local<v8::Value> argv[]) {
- RunFunction(function, context, context->Global(), argc, argv);
- }
- v8::Global<v8::Value> RunFunctionOnGlobalAndReturnHandle(
- v8::Local<v8::Function> function,
- v8::Local<v8::Context> context,
- int argc,
- v8::Local<v8::Value> argv[]) {
- return v8::Global<v8::Value>(
- context->GetIsolate(),
- RunFunction(function, context, context->Global(), argc, argv));
- }
- void RunFunctionAndExpectError(v8::Local<v8::Function> function,
- v8::Local<v8::Context> context,
- v8::Local<v8::Value> receiver,
- int argc,
- v8::Local<v8::Value> argv[],
- const std::string& expected_error) {
- std::string error;
- v8::Local<v8::Value> result;
- EXPECT_FALSE(RunFunctionImpl(function, context, receiver, argc, argv, &result,
- &error));
- EXPECT_TRUE(result.IsEmpty());
- EXPECT_EQ(expected_error, error);
- }
- void RunFunctionAndExpectError(v8::Local<v8::Function> function,
- v8::Local<v8::Context> context,
- int argc,
- v8::Local<v8::Value> argv[],
- const std::string& expected_error) {
- RunFunctionAndExpectError(function, context,
- v8::Undefined(context->GetIsolate()), argc, argv,
- expected_error);
- }
- v8::Local<v8::Value> GetPropertyFromObject(v8::Local<v8::Object> object,
- v8::Local<v8::Context> context,
- base::StringPiece key) {
- v8::Local<v8::Value> result;
- EXPECT_TRUE(object->Get(context, gin::StringToV8(context->GetIsolate(), key))
- .ToLocal(&result));
- return result;
- }
- std::unique_ptr<base::Value> GetBaseValuePropertyFromObject(
- v8::Local<v8::Object> object,
- v8::Local<v8::Context> context,
- base::StringPiece key) {
- return V8ToBaseValue(GetPropertyFromObject(object, context, key), context);
- }
- std::string GetStringPropertyFromObject(v8::Local<v8::Object> object,
- v8::Local<v8::Context> context,
- base::StringPiece key) {
- return V8ToString(GetPropertyFromObject(object, context, key), context);
- }
- bool ValueTypeChecker<v8::Function>::IsType(v8::Local<v8::Value> value) {
- return value->IsFunction();
- }
- bool ValueTypeChecker<v8::Object>::IsType(v8::Local<v8::Value> value) {
- return value->IsObject();
- }
- bool ValueTypeChecker<v8::Promise>::IsType(v8::Local<v8::Value> value) {
- return value->IsPromise();
- }
- bool ValueTypeChecker<v8::Array>::IsType(v8::Local<v8::Value> value) {
- return value->IsArray();
- }
- } // namespace extensions
|