| Index: extensions/renderer/api_binding_unittest.cc
|
| diff --git a/extensions/renderer/api_binding_unittest.cc b/extensions/renderer/api_binding_unittest.cc
|
| index e8d8aa3edf0ac6edc41b76615fcb8068b4a58b84..eee8ff435040c120869a8ba26fdc37fbc680dab8 100644
|
| --- a/extensions/renderer/api_binding_unittest.cc
|
| +++ b/extensions/renderer/api_binding_unittest.cc
|
| @@ -69,7 +69,7 @@ const char kFunctions[] =
|
| " }]"
|
| "}]";
|
|
|
| -bool AllowAllAPIs(const std::string& name) {
|
| +bool AllowAllFeatures(v8::Local<v8::Context> context, const std::string& name) {
|
| return true;
|
| }
|
|
|
| @@ -148,6 +148,11 @@ class APIBindingUnittest : public APIBindingTest {
|
| create_custom_type_ = callback;
|
| }
|
|
|
| + void SetAvailabilityCallback(
|
| + const APIBinding::AvailabilityCallback& callback) {
|
| + availability_callback_ = callback;
|
| + }
|
| +
|
| void InitializeBinding() {
|
| if (!binding_hooks_) {
|
| binding_hooks_ = base::MakeUnique<APIBindingHooks>(
|
| @@ -155,14 +160,16 @@ class APIBindingUnittest : public APIBindingTest {
|
| }
|
| if (binding_hooks_delegate_)
|
| binding_hooks_->SetDelegate(std::move(binding_hooks_delegate_));
|
| + if (!availability_callback_)
|
| + availability_callback_ = base::Bind(&AllowAllFeatures);
|
| event_handler_ = base::MakeUnique<APIEventHandler>(
|
| base::Bind(&RunFunctionOnGlobalAndIgnoreResult),
|
| base::Bind(&OnEventListenersChanged));
|
| binding_ = base::MakeUnique<APIBinding>(
|
| kBindingName, binding_functions_.get(), binding_types_.get(),
|
| binding_events_.get(), binding_properties_.get(), create_custom_type_,
|
| - std::move(binding_hooks_), &type_refs_, request_handler_.get(),
|
| - event_handler_.get());
|
| + availability_callback_, std::move(binding_hooks_), &type_refs_,
|
| + request_handler_.get(), event_handler_.get());
|
| EXPECT_EQ(!binding_types_.get(), type_refs_.empty());
|
| }
|
|
|
| @@ -230,6 +237,7 @@ class APIBindingUnittest : public APIBindingTest {
|
| std::unique_ptr<APIBindingHooks> binding_hooks_;
|
| std::unique_ptr<APIBindingHooksDelegate> binding_hooks_delegate_;
|
| APIBinding::CreateCustomType create_custom_type_;
|
| + APIBinding::AvailabilityCallback availability_callback_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(APIBindingUnittest);
|
| };
|
| @@ -271,8 +279,7 @@ TEST_F(APIBindingUnittest, TestEmptyAPI) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
| EXPECT_EQ(
|
| 0u,
|
| binding_object->GetOwnPropertyNames(context).ToLocalChecked()->Length());
|
| @@ -287,8 +294,7 @@ TEST_F(APIBindingUnittest, TestBasicAPICalls) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| // Argument parsing is tested primarily in APISignature and ArgumentSpec
|
| // tests, so do a few quick sanity checks...
|
| @@ -338,8 +344,7 @@ TEST_F(APIBindingUnittest, EnumValues) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| const char kExpected[] =
|
| "{'ALPHA':'alpha','CAMEL_CASE':'camelCase','HYPHEN_ATED':'Hyphen-ated',"
|
| @@ -365,8 +370,7 @@ TEST_F(APIBindingUnittest, EnumWithEmptyEntry) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| EXPECT_EQ(
|
| "{\"\":\"\",\"OTHER\":\"other\"}",
|
| @@ -413,8 +417,7 @@ TEST_F(APIBindingUnittest, TypeRefsTest) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| // Parsing in general is tested in APISignature and ArgumentSpec tests, but
|
| // we test that the binding a) correctly finds the definitions, and b) accepts
|
| @@ -456,12 +459,8 @@ TEST_F(APIBindingUnittest, RestrictedAPIs) {
|
| const char kEvents[] =
|
| "[{'name': 'allowedEvent'}, {'name': 'restrictedEvent'}]";
|
| SetEvents(kEvents);
|
| - InitializeBinding();
|
| -
|
| - v8::HandleScope handle_scope(isolate());
|
| - v8::Local<v8::Context> context = MainContext();
|
| -
|
| - auto is_available = [](const std::string& name) {
|
| + auto is_available = [](v8::Local<v8::Context> context,
|
| + const std::string& name) {
|
| std::set<std::string> allowed = {"test.allowedOne", "test.allowedTwo",
|
| "test.allowedEvent"};
|
| std::set<std::string> restricted = {
|
| @@ -469,9 +468,14 @@ TEST_F(APIBindingUnittest, RestrictedAPIs) {
|
| EXPECT_TRUE(allowed.count(name) || restricted.count(name)) << name;
|
| return allowed.count(name) != 0;
|
| };
|
| + SetAvailabilityCallback(base::Bind(is_available));
|
| +
|
| + InitializeBinding();
|
| +
|
| + v8::HandleScope handle_scope(isolate());
|
| + v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(is_available));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| auto is_defined = [&binding_object, context](const std::string& name) {
|
| v8::Local<v8::Value> val =
|
| @@ -498,8 +502,7 @@ TEST_F(APIBindingUnittest, TestEventCreation) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| // Event behavior is tested in the APIEventHandler unittests as well as the
|
| // APIBindingsSystem tests, so we really only need to check that the events
|
| @@ -536,8 +539,7 @@ TEST_F(APIBindingUnittest, TestProperties) {
|
|
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
| EXPECT_EQ("17",
|
| GetStringPropertyFromObject(binding_object, context, "prop1"));
|
| EXPECT_EQ(R"({"subprop1":"some value","subprop2":true})",
|
| @@ -588,8 +590,7 @@ TEST_F(APIBindingUnittest, TestRefProperties) {
|
|
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
| EXPECT_EQ(R"({"alphaProp":"alphaVal"})",
|
| GetStringPropertyFromObject(binding_object, context, "alpha"));
|
| EXPECT_EQ(
|
| @@ -604,8 +605,7 @@ TEST_F(APIBindingUnittest, TestDisposedContext) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| v8::Local<v8::Function> func =
|
| FunctionFromString(context, "(function(obj) { obj.oneString('foo'); })");
|
| @@ -625,10 +625,8 @@ TEST_F(APIBindingUnittest, MultipleContexts) {
|
| SetFunctions(kFunctions);
|
| InitializeBinding();
|
|
|
| - v8::Local<v8::Object> binding_object_a =
|
| - binding()->CreateInstance(context_a, base::Bind(&AllowAllAPIs));
|
| - v8::Local<v8::Object> binding_object_b =
|
| - binding()->CreateInstance(context_b, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object_a = binding()->CreateInstance(context_a);
|
| + v8::Local<v8::Object> binding_object_b = binding()->CreateInstance(context_b);
|
|
|
| ExpectPass(context_a, binding_object_a, "obj.oneString('foo');", "['foo']",
|
| false);
|
| @@ -668,8 +666,7 @@ TEST_F(APIBindingUnittest, TestCustomHooks) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| // First try calling the oneString() method, which has a custom hook
|
| // installed.
|
| @@ -709,8 +706,7 @@ TEST_F(APIBindingUnittest, TestJSCustomHook) {
|
| SetHooks(std::move(hooks));
|
| InitializeBinding();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| // First try calling with an invalid invocation. An error should be raised and
|
| // the hook should never have been called, since the arguments didn't match.
|
| @@ -765,8 +761,7 @@ TEST_F(APIBindingUnittest, TestUpdateArgumentsPreValidate) {
|
| SetFunctions(kFunctions);
|
| InitializeBinding();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| // Call the method with a hook. Since the hook updates arguments before
|
| // validation, we should be able to pass in invalid arguments and still
|
| @@ -827,8 +822,7 @@ TEST_F(APIBindingUnittest, TestThrowInUpdateArgumentsPreValidate) {
|
| SetFunctions(kFunctions);
|
| InitializeBinding();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| v8::Local<v8::Function> function =
|
| FunctionFromString(context,
|
| @@ -869,8 +863,7 @@ TEST_F(APIBindingUnittest, TestReturningResultFromCustomJSHook) {
|
| SetFunctions(kFunctions);
|
| InitializeBinding();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| v8::Local<v8::Function> function =
|
| FunctionFromString(context,
|
| @@ -930,8 +923,7 @@ TEST_F(APIBindingUnittest, TestThrowingFromCustomJSHook) {
|
| SetFunctions(kFunctions);
|
| InitializeBinding();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| v8::Local<v8::Function> function =
|
| FunctionFromString(context,
|
| @@ -980,8 +972,7 @@ TEST_F(APIBindingUnittest,
|
| SetFunctions(kFunctions);
|
| InitializeBinding();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| {
|
| // Test an invocation that we expect to throw an exception.
|
| @@ -1036,8 +1027,7 @@ TEST_F(APIBindingUnittest, TestUpdateArgumentsPostValidate) {
|
| SetFunctions(kFunctions);
|
| InitializeBinding();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| // Try calling the method with an invalid signature. Since it's invalid, we
|
| // should never enter the hook.
|
| @@ -1088,8 +1078,7 @@ TEST_F(APIBindingUnittest, TestUpdateArgumentsPostValidateViolatingSchema) {
|
| SetFunctions(kFunctions);
|
| InitializeBinding();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| // Call the method with a valid signature. The hook should be entered and
|
| // manipulate the arguments.
|
| @@ -1104,8 +1093,7 @@ TEST_F(APIBindingUnittest, TestUserGestures) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| v8::Local<v8::Function> function =
|
| FunctionFromString(context, "(function(obj) { obj.oneString('foo');})");
|
| @@ -1155,8 +1143,7 @@ TEST_F(APIBindingUnittest, FilteredEvents) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| const char kAddFilteredListener[] =
|
| "(function(evt) {\n"
|
| @@ -1209,8 +1196,7 @@ TEST_F(APIBindingUnittest, HooksTemplateInitializer) {
|
| v8::HandleScope handle_scope(isolate());
|
| v8::Local<v8::Context> context = MainContext();
|
|
|
| - v8::Local<v8::Object> binding_object =
|
| - binding()->CreateInstance(context, base::Bind(&AllowAllAPIs));
|
| + v8::Local<v8::Object> binding_object = binding()->CreateInstance(context);
|
|
|
| // The extra property should be present on the binding object.
|
| EXPECT_EQ("42", GetStringPropertyFromObject(binding_object, context,
|
|
|