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/native_extension_bindings_system.h" | 5 #include "extensions/renderer/native_extension_bindings_system.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "components/crx_file/id_util.h" | 10 #include "components/crx_file/id_util.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 script_context_set_ = base::MakeUnique<ScriptContextSet>(&extension_ids_); | 92 script_context_set_ = base::MakeUnique<ScriptContextSet>(&extension_ids_); |
93 bindings_system_ = base::MakeUnique<NativeExtensionBindingsSystem>( | 93 bindings_system_ = base::MakeUnique<NativeExtensionBindingsSystem>( |
94 base::Bind(&NativeExtensionBindingsSystemUnittest::MockSendRequestIPC, | 94 base::Bind(&NativeExtensionBindingsSystemUnittest::MockSendRequestIPC, |
95 base::Unretained(this)), | 95 base::Unretained(this)), |
96 base::Bind(&NativeExtensionBindingsSystemUnittest::MockSendListenerIPC, | 96 base::Bind(&NativeExtensionBindingsSystemUnittest::MockSendListenerIPC, |
97 base::Unretained(this))); | 97 base::Unretained(this))); |
98 APIBindingTest::SetUp(); | 98 APIBindingTest::SetUp(); |
99 } | 99 } |
100 | 100 |
101 void TearDown() override { | 101 void TearDown() override { |
102 for (auto* context : raw_script_contexts_) | 102 event_change_handler_.reset(); |
| 103 |
| 104 for (auto* context : raw_script_contexts_) { |
| 105 bindings_system_->WillReleaseScriptContext(context); |
103 script_context_set_->Remove(context); | 106 script_context_set_->Remove(context); |
| 107 } |
104 base::RunLoop().RunUntilIdle(); | 108 base::RunLoop().RunUntilIdle(); |
105 script_context_set_.reset(); | 109 script_context_set_.reset(); |
106 bindings_system_.reset(); | 110 bindings_system_.reset(); |
107 APIBindingTest::TearDown(); | 111 APIBindingTest::TearDown(); |
108 } | 112 } |
109 | 113 |
110 void MockSendRequestIPC(ScriptContext* context, | 114 void MockSendRequestIPC(ScriptContext* context, |
111 const ExtensionHostMsg_Request_Params& params) { | 115 const ExtensionHostMsg_Request_Params& params) { |
112 last_params_.name = params.name; | 116 last_params_.name = params.name; |
113 last_params_.arguments.Swap(params.arguments.CreateDeepCopy().get()); | 117 last_params_.arguments.Swap(params.arguments.CreateDeepCopy().get()); |
(...skipping 19 matching lines...) Expand all Loading... |
133 auto script_context = base::MakeUnique<ScriptContext>( | 137 auto script_context = base::MakeUnique<ScriptContext>( |
134 v8_context, nullptr, extension, context_type, extension, context_type); | 138 v8_context, nullptr, extension, context_type, extension, context_type); |
135 script_context->set_module_system( | 139 script_context->set_module_system( |
136 base::MakeUnique<ModuleSystem>(script_context.get(), source_map())); | 140 base::MakeUnique<ModuleSystem>(script_context.get(), source_map())); |
137 ScriptContext* raw_script_context = script_context.get(); | 141 ScriptContext* raw_script_context = script_context.get(); |
138 raw_script_contexts_.push_back(raw_script_context); | 142 raw_script_contexts_.push_back(raw_script_context); |
139 script_context_set_->AddForTesting(std::move(script_context)); | 143 script_context_set_->AddForTesting(std::move(script_context)); |
140 return raw_script_context; | 144 return raw_script_context; |
141 } | 145 } |
142 | 146 |
| 147 void DisposeMainScriptContext() { |
| 148 v8::Local<v8::Context> context = ContextLocal(); |
| 149 auto iter = |
| 150 std::find_if(raw_script_contexts_.begin(), raw_script_contexts_.end(), |
| 151 [context](ScriptContext* script_context) { |
| 152 return script_context->v8_context() == context; |
| 153 }); |
| 154 ASSERT_TRUE(iter != raw_script_contexts_.end()); |
| 155 bindings_system_->WillReleaseScriptContext(*iter); |
| 156 DisposeContext(); |
| 157 script_context_set_->Remove(*iter); |
| 158 raw_script_contexts_.erase(iter); |
| 159 } |
| 160 |
143 void RegisterExtension(const ExtensionId& id) { extension_ids_.insert(id); } | 161 void RegisterExtension(const ExtensionId& id) { extension_ids_.insert(id); } |
144 | 162 |
145 void InitEventChangeHandler() { | 163 void InitEventChangeHandler() { |
146 event_change_handler_ = base::MakeUnique<MockEventChangeHandler>(); | 164 event_change_handler_ = base::MakeUnique<MockEventChangeHandler>(); |
147 } | 165 } |
148 | 166 |
149 NativeExtensionBindingsSystem* bindings_system() { | 167 NativeExtensionBindingsSystem* bindings_system() { |
150 return bindings_system_.get(); | 168 return bindings_system_.get(); |
151 } | 169 } |
152 const ExtensionHostMsg_Request_Params& last_params() { return last_params_; } | 170 const ExtensionHostMsg_Request_Params& last_params() { return last_params_; } |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); | 371 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); |
354 script_context->set_url(extension->url()); | 372 script_context->set_url(extension->url()); |
355 | 373 |
356 bindings_system()->UpdateBindingsForContext(script_context); | 374 bindings_system()->UpdateBindingsForContext(script_context); |
357 | 375 |
358 v8::Local<v8::Value> first_idle_object = | 376 v8::Local<v8::Value> first_idle_object = |
359 V8ValueFromScriptSource(context, "chrome.idle"); | 377 V8ValueFromScriptSource(context, "chrome.idle"); |
360 ASSERT_FALSE(first_idle_object.IsEmpty()); | 378 ASSERT_FALSE(first_idle_object.IsEmpty()); |
361 EXPECT_TRUE(first_idle_object->IsObject()); | 379 EXPECT_TRUE(first_idle_object->IsObject()); |
362 | 380 |
363 DisposeContext(); | 381 DisposeMainScriptContext(); |
364 | 382 |
365 // Check an API that was instantiated.... | 383 // Check an API that was instantiated.... |
366 v8::Local<v8::Value> second_idle_object = | 384 v8::Local<v8::Value> second_idle_object = |
367 V8ValueFromScriptSource(context, "chrome.idle"); | 385 V8ValueFromScriptSource(context, "chrome.idle"); |
368 ASSERT_FALSE(second_idle_object.IsEmpty()); | 386 ASSERT_FALSE(second_idle_object.IsEmpty()); |
369 EXPECT_TRUE(second_idle_object->IsUndefined()); | 387 EXPECT_TRUE(second_idle_object->IsUndefined()); |
370 // ... and also one that wasn't. | 388 // ... and also one that wasn't. |
371 v8::Local<v8::Value> power_object = | 389 v8::Local<v8::Value> power_object = |
372 V8ValueFromScriptSource(context, "chrome.power"); | 390 V8ValueFromScriptSource(context, "chrome.power"); |
373 ASSERT_FALSE(power_object.IsEmpty()); | 391 ASSERT_FALSE(power_object.IsEmpty()); |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
724 const std::vector<std::string> kKeys = {"get", "set", "remove", "clear", | 742 const std::vector<std::string> kKeys = {"get", "set", "remove", "clear", |
725 "getBytesInUse"}; | 743 "getBytesInUse"}; |
726 for (const auto& key : kKeys) { | 744 for (const auto& key : kKeys) { |
727 v8::Local<v8::String> v8_key = gin::StringToV8(isolate(), key); | 745 v8::Local<v8::String> v8_key = gin::StringToV8(isolate(), key); |
728 EXPECT_TRUE(local_object->HasOwnProperty(context, v8_key).FromJust()) | 746 EXPECT_TRUE(local_object->HasOwnProperty(context, v8_key).FromJust()) |
729 << key; | 747 << key; |
730 } | 748 } |
731 } | 749 } |
732 | 750 |
733 } // namespace extensions | 751 } // namespace extensions |
OLD | NEW |