api_binding_test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "extensions/renderer/bindings/api_binding_test.h"
  5. #include "base/threading/thread_task_runner_handle.h"
  6. #include "gin/array_buffer.h"
  7. #include "gin/public/context_holder.h"
  8. #include "gin/public/isolate_holder.h"
  9. #include "gin/v8_initializer.h"
  10. namespace extensions {
  11. APIBindingTest::APIBindingTest() {}
  12. APIBindingTest::~APIBindingTest() {}
  13. v8::ExtensionConfiguration* APIBindingTest::GetV8ExtensionConfiguration() {
  14. return nullptr;
  15. }
  16. void APIBindingTest::SetUp() {
  17. test_js_runner_ = CreateTestJSRunner();
  18. // Much of this initialization is stolen from the somewhat-similar
  19. // gin::V8Test.
  20. #ifdef V8_USE_EXTERNAL_STARTUP_DATA
  21. gin::V8Initializer::LoadV8Snapshot();
  22. #endif
  23. gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
  24. gin::ArrayBufferAllocator::SharedInstance());
  25. isolate_holder_ = std::make_unique<gin::IsolateHolder>(
  26. base::ThreadTaskRunnerHandle::Get(),
  27. gin::IsolateHolder::IsolateType::kTest);
  28. isolate()->Enter();
  29. v8::HandleScope handle_scope(isolate());
  30. v8::Local<v8::Context> context =
  31. v8::Context::New(isolate(), GetV8ExtensionConfiguration());
  32. context->Enter();
  33. main_context_holder_ = std::make_unique<gin::ContextHolder>(isolate());
  34. main_context_holder_->SetContext(context);
  35. }
  36. void APIBindingTest::TearDown() {
  37. if (main_context_holder_ || !additional_context_holders_.empty()) {
  38. DisposeAllContexts();
  39. } else {
  40. // The context was already deleted (as through DisposeContext()), but we
  41. // still need to garbage collect.
  42. RunGarbageCollection();
  43. }
  44. isolate()->Exit();
  45. isolate_holder_.reset();
  46. test_js_runner_.reset();
  47. }
  48. void APIBindingTest::DisposeAllContexts() {
  49. auto dispose_and_check_context =
  50. [this](std::unique_ptr<gin::ContextHolder>& holder, bool exit) {
  51. // Check for leaks - a weak handle to a context is invalidated on
  52. // context destruction, so resetting the context should reset the
  53. // handle.
  54. v8::Global<v8::Context> weak_context;
  55. {
  56. v8::HandleScope handle_scope(isolate());
  57. v8::Local<v8::Context> context = holder->context();
  58. weak_context.Reset(isolate(), context);
  59. weak_context.SetWeak();
  60. OnWillDisposeContext(context);
  61. if (exit)
  62. context->Exit();
  63. }
  64. holder.reset();
  65. // Garbage collect everything so that we find any issues where we might
  66. // be double-freeing.
  67. RunGarbageCollection();
  68. // The context should have been deleted.
  69. // (ASSERT_TRUE is not used, so that Isolate::Exit is still called.)
  70. EXPECT_TRUE(weak_context.IsEmpty());
  71. };
  72. for (auto& context_holder : additional_context_holders_)
  73. dispose_and_check_context(context_holder, false);
  74. additional_context_holders_.clear();
  75. if (main_context_holder_)
  76. dispose_and_check_context(main_context_holder_, true);
  77. }
  78. v8::Local<v8::Context> APIBindingTest::AddContext() {
  79. auto holder = std::make_unique<gin::ContextHolder>(isolate());
  80. v8::Local<v8::Context> context =
  81. v8::Context::New(isolate(), GetV8ExtensionConfiguration());
  82. holder->SetContext(context);
  83. additional_context_holders_.push_back(std::move(holder));
  84. return context;
  85. }
  86. v8::Local<v8::Context> APIBindingTest::MainContext() {
  87. return main_context_holder_->context();
  88. }
  89. void APIBindingTest::DisposeContext(v8::Local<v8::Context> context) {
  90. if (main_context_holder_ && context == main_context_holder_->context()) {
  91. context->Exit();
  92. OnWillDisposeContext(context);
  93. main_context_holder_.reset();
  94. return;
  95. }
  96. auto iter = std::find_if(
  97. additional_context_holders_.begin(), additional_context_holders_.end(),
  98. [context](const std::unique_ptr<gin::ContextHolder>& holder) {
  99. return holder->context() == context;
  100. });
  101. ASSERT_TRUE(iter != additional_context_holders_.end())
  102. << "Could not find context";
  103. OnWillDisposeContext(context);
  104. additional_context_holders_.erase(iter);
  105. }
  106. void APIBindingTest::RunGarbageCollection() {
  107. // '5' is a magic number stolen from Blink; arbitrarily large enough to
  108. // hopefully clean up all the various paths.
  109. for (int i = 0; i < 5; ++i) {
  110. isolate()->RequestGarbageCollectionForTesting(
  111. v8::Isolate::kFullGarbageCollection);
  112. }
  113. }
  114. std::unique_ptr<TestJSRunner::Scope> APIBindingTest::CreateTestJSRunner() {
  115. return std::make_unique<TestJSRunner::Scope>(
  116. std::make_unique<TestJSRunner>());
  117. }
  118. v8::Isolate* APIBindingTest::isolate() {
  119. return isolate_holder_->isolate();
  120. }
  121. } // namespace extensions