| 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.h" | 5 #include "extensions/renderer/api_binding.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "extensions/renderer/api_binding_hooks.h" | 11 #include "extensions/renderer/api_binding_hooks.h" |
| 12 #include "extensions/renderer/api_binding_hooks_test_delegate.h" | 12 #include "extensions/renderer/api_binding_hooks_test_delegate.h" |
| 13 #include "extensions/renderer/api_binding_test.h" | 13 #include "extensions/renderer/api_binding_test.h" |
| 14 #include "extensions/renderer/api_binding_test_util.h" | 14 #include "extensions/renderer/api_binding_test_util.h" |
| 15 #include "extensions/renderer/api_event_handler.h" | 15 #include "extensions/renderer/api_event_handler.h" |
| 16 #include "extensions/renderer/api_invocation_errors.h" | 16 #include "extensions/renderer/api_invocation_errors.h" |
| 17 #include "extensions/renderer/api_request_handler.h" | 17 #include "extensions/renderer/api_request_handler.h" |
| 18 #include "extensions/renderer/api_type_reference_map.h" | 18 #include "extensions/renderer/api_type_reference_map.h" |
| 19 #include "extensions/renderer/binding_access_checker.h" |
| 19 #include "gin/arguments.h" | 20 #include "gin/arguments.h" |
| 20 #include "gin/converter.h" | 21 #include "gin/converter.h" |
| 21 #include "gin/public/context_holder.h" | 22 #include "gin/public/context_holder.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 24 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
| 24 #include "v8/include/v8.h" | 25 #include "v8/include/v8.h" |
| 25 | 26 |
| 26 namespace extensions { | 27 namespace extensions { |
| 27 | 28 |
| 28 using namespace api_errors; | 29 using namespace api_errors; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 APIBindingTest::SetUp(); | 96 APIBindingTest::SetUp(); |
| 96 request_handler_ = base::MakeUnique<APIRequestHandler>( | 97 request_handler_ = base::MakeUnique<APIRequestHandler>( |
| 97 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), | 98 base::Bind(&APIBindingUnittest::OnFunctionCall, base::Unretained(this)), |
| 98 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 99 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 99 APILastError(APILastError::GetParent(), | 100 APILastError(APILastError::GetParent(), |
| 100 APILastError::AddConsoleError())); | 101 APILastError::AddConsoleError())); |
| 101 } | 102 } |
| 102 | 103 |
| 103 void TearDown() override { | 104 void TearDown() override { |
| 104 DisposeAllContexts(); | 105 DisposeAllContexts(); |
| 106 access_checker_.reset(); |
| 105 request_handler_.reset(); | 107 request_handler_.reset(); |
| 106 event_handler_.reset(); | 108 event_handler_.reset(); |
| 107 binding_.reset(); | 109 binding_.reset(); |
| 108 APIBindingTest::TearDown(); | 110 APIBindingTest::TearDown(); |
| 109 } | 111 } |
| 110 | 112 |
| 111 void OnWillDisposeContext(v8::Local<v8::Context> context) override { | 113 void OnWillDisposeContext(v8::Local<v8::Context> context) override { |
| 112 event_handler_->InvalidateContext(context); | 114 event_handler_->InvalidateContext(context); |
| 113 request_handler_->InvalidateContext(context); | 115 request_handler_->InvalidateContext(context); |
| 114 } | 116 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 142 std::unique_ptr<APIBindingHooksDelegate> hooks_delegate) { | 144 std::unique_ptr<APIBindingHooksDelegate> hooks_delegate) { |
| 143 binding_hooks_delegate_ = std::move(hooks_delegate); | 145 binding_hooks_delegate_ = std::move(hooks_delegate); |
| 144 ASSERT_TRUE(binding_hooks_delegate_); | 146 ASSERT_TRUE(binding_hooks_delegate_); |
| 145 } | 147 } |
| 146 | 148 |
| 147 void SetCreateCustomType(const APIBinding::CreateCustomType& callback) { | 149 void SetCreateCustomType(const APIBinding::CreateCustomType& callback) { |
| 148 create_custom_type_ = callback; | 150 create_custom_type_ = callback; |
| 149 } | 151 } |
| 150 | 152 |
| 151 void SetAvailabilityCallback( | 153 void SetAvailabilityCallback( |
| 152 const APIBinding::AvailabilityCallback& callback) { | 154 const BindingAccessChecker::AvailabilityCallback& callback) { |
| 153 availability_callback_ = callback; | 155 availability_callback_ = callback; |
| 154 } | 156 } |
| 155 | 157 |
| 156 void InitializeBinding() { | 158 void InitializeBinding() { |
| 157 if (!binding_hooks_) { | 159 if (!binding_hooks_) { |
| 158 binding_hooks_ = base::MakeUnique<APIBindingHooks>( | 160 binding_hooks_ = base::MakeUnique<APIBindingHooks>( |
| 159 kBindingName, binding::RunJSFunctionSync()); | 161 kBindingName, binding::RunJSFunctionSync()); |
| 160 } | 162 } |
| 161 if (binding_hooks_delegate_) | 163 if (binding_hooks_delegate_) |
| 162 binding_hooks_->SetDelegate(std::move(binding_hooks_delegate_)); | 164 binding_hooks_->SetDelegate(std::move(binding_hooks_delegate_)); |
| 163 if (!availability_callback_) | 165 if (!availability_callback_) |
| 164 availability_callback_ = base::Bind(&AllowAllFeatures); | 166 availability_callback_ = base::Bind(&AllowAllFeatures); |
| 165 event_handler_ = base::MakeUnique<APIEventHandler>( | 167 event_handler_ = base::MakeUnique<APIEventHandler>( |
| 166 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), | 168 base::Bind(&RunFunctionOnGlobalAndIgnoreResult), |
| 167 base::Bind(&RunFunctionOnGlobalAndReturnHandle), | 169 base::Bind(&RunFunctionOnGlobalAndReturnHandle), |
| 168 base::Bind(&OnEventListenersChanged)); | 170 base::Bind(&OnEventListenersChanged)); |
| 171 access_checker_ = |
| 172 base::MakeUnique<BindingAccessChecker>(availability_callback_); |
| 169 binding_ = base::MakeUnique<APIBinding>( | 173 binding_ = base::MakeUnique<APIBinding>( |
| 170 kBindingName, binding_functions_.get(), binding_types_.get(), | 174 kBindingName, binding_functions_.get(), binding_types_.get(), |
| 171 binding_events_.get(), binding_properties_.get(), create_custom_type_, | 175 binding_events_.get(), binding_properties_.get(), create_custom_type_, |
| 172 availability_callback_, std::move(binding_hooks_), &type_refs_, | 176 std::move(binding_hooks_), &type_refs_, request_handler_.get(), |
| 173 request_handler_.get(), event_handler_.get()); | 177 event_handler_.get(), access_checker_.get()); |
| 174 EXPECT_EQ(!binding_types_.get(), type_refs_.empty()); | 178 EXPECT_EQ(!binding_types_.get(), type_refs_.empty()); |
| 175 } | 179 } |
| 176 | 180 |
| 177 void ExpectPass(v8::Local<v8::Object> object, | 181 void ExpectPass(v8::Local<v8::Object> object, |
| 178 const std::string& script_source, | 182 const std::string& script_source, |
| 179 const std::string& expected_json_arguments_single_quotes, | 183 const std::string& expected_json_arguments_single_quotes, |
| 180 bool expect_callback) { | 184 bool expect_callback) { |
| 181 ExpectPass(MainContext(), object, script_source, | 185 ExpectPass(MainContext(), object, script_source, |
| 182 expected_json_arguments_single_quotes, expect_callback); | 186 expected_json_arguments_single_quotes, expect_callback); |
| 183 } | 187 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 const std::string& script_source, | 226 const std::string& script_source, |
| 223 bool should_pass, | 227 bool should_pass, |
| 224 const std::string& expected_json_arguments, | 228 const std::string& expected_json_arguments, |
| 225 bool expect_callback, | 229 bool expect_callback, |
| 226 const std::string& expected_error); | 230 const std::string& expected_error); |
| 227 | 231 |
| 228 std::unique_ptr<APIRequestHandler::Request> last_request_; | 232 std::unique_ptr<APIRequestHandler::Request> last_request_; |
| 229 std::unique_ptr<APIBinding> binding_; | 233 std::unique_ptr<APIBinding> binding_; |
| 230 std::unique_ptr<APIEventHandler> event_handler_; | 234 std::unique_ptr<APIEventHandler> event_handler_; |
| 231 std::unique_ptr<APIRequestHandler> request_handler_; | 235 std::unique_ptr<APIRequestHandler> request_handler_; |
| 236 std::unique_ptr<BindingAccessChecker> access_checker_; |
| 232 APITypeReferenceMap type_refs_; | 237 APITypeReferenceMap type_refs_; |
| 233 | 238 |
| 234 std::unique_ptr<base::ListValue> binding_functions_; | 239 std::unique_ptr<base::ListValue> binding_functions_; |
| 235 std::unique_ptr<base::ListValue> binding_events_; | 240 std::unique_ptr<base::ListValue> binding_events_; |
| 236 std::unique_ptr<base::ListValue> binding_types_; | 241 std::unique_ptr<base::ListValue> binding_types_; |
| 237 std::unique_ptr<base::DictionaryValue> binding_properties_; | 242 std::unique_ptr<base::DictionaryValue> binding_properties_; |
| 238 std::unique_ptr<APIBindingHooks> binding_hooks_; | 243 std::unique_ptr<APIBindingHooks> binding_hooks_; |
| 239 std::unique_ptr<APIBindingHooksDelegate> binding_hooks_delegate_; | 244 std::unique_ptr<APIBindingHooksDelegate> binding_hooks_delegate_; |
| 240 APIBinding::CreateCustomType create_custom_type_; | 245 APIBinding::CreateCustomType create_custom_type_; |
| 241 APIBinding::AvailabilityCallback availability_callback_; | 246 BindingAccessChecker::AvailabilityCallback availability_callback_; |
| 242 | 247 |
| 243 DISALLOW_COPY_AND_ASSIGN(APIBindingUnittest); | 248 DISALLOW_COPY_AND_ASSIGN(APIBindingUnittest); |
| 244 }; | 249 }; |
| 245 | 250 |
| 246 void APIBindingUnittest::RunTest(v8::Local<v8::Context> context, | 251 void APIBindingUnittest::RunTest(v8::Local<v8::Context> context, |
| 247 v8::Local<v8::Object> object, | 252 v8::Local<v8::Object> object, |
| 248 const std::string& script_source, | 253 const std::string& script_source, |
| 249 bool should_pass, | 254 bool should_pass, |
| 250 const std::string& expected_json_arguments, | 255 const std::string& expected_json_arguments, |
| 251 bool expect_callback, | 256 bool expect_callback, |
| (...skipping 1016 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1268 | 1273 |
| 1269 // The extra property should be present on the binding object. | 1274 // The extra property should be present on the binding object. |
| 1270 EXPECT_EQ("42", GetStringPropertyFromObject(binding_object, context, | 1275 EXPECT_EQ("42", GetStringPropertyFromObject(binding_object, context, |
| 1271 "hookedProperty")); | 1276 "hookedProperty")); |
| 1272 // Sanity check: other values should still be there. | 1277 // Sanity check: other values should still be there. |
| 1273 EXPECT_EQ("function", | 1278 EXPECT_EQ("function", |
| 1274 GetStringPropertyFromObject(binding_object, context, "oneString")); | 1279 GetStringPropertyFromObject(binding_object, context, "oneString")); |
| 1275 } | 1280 } |
| 1276 | 1281 |
| 1277 } // namespace extensions | 1282 } // namespace extensions |
| OLD | NEW |