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 20 matching lines...) Expand all Loading... |
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 bindings_system_->DidCreateScriptContext(raw_script_context); | 144 bindings_system_->DidCreateScriptContext(raw_script_context); |
141 return raw_script_context; | 145 return raw_script_context; |
142 } | 146 } |
143 | 147 |
| 148 void DisposeMainScriptContext() { |
| 149 v8::Local<v8::Context> context = ContextLocal(); |
| 150 auto iter = |
| 151 std::find_if(raw_script_contexts_.begin(), raw_script_contexts_.end(), |
| 152 [context](ScriptContext* script_context) { |
| 153 return script_context->v8_context() == context; |
| 154 }); |
| 155 ASSERT_TRUE(iter != raw_script_contexts_.end()); |
| 156 bindings_system_->WillReleaseScriptContext(*iter); |
| 157 DisposeContext(); |
| 158 script_context_set_->Remove(*iter); |
| 159 raw_script_contexts_.erase(iter); |
| 160 } |
| 161 |
144 void RegisterExtension(const ExtensionId& id) { extension_ids_.insert(id); } | 162 void RegisterExtension(const ExtensionId& id) { extension_ids_.insert(id); } |
145 | 163 |
146 void InitEventChangeHandler() { | 164 void InitEventChangeHandler() { |
147 event_change_handler_ = base::MakeUnique<MockEventChangeHandler>(); | 165 event_change_handler_ = base::MakeUnique<MockEventChangeHandler>(); |
148 } | 166 } |
149 | 167 |
150 NativeExtensionBindingsSystem* bindings_system() { | 168 NativeExtensionBindingsSystem* bindings_system() { |
151 return bindings_system_.get(); | 169 return bindings_system_.get(); |
152 } | 170 } |
153 const ExtensionHostMsg_Request_Params& last_params() { return last_params_; } | 171 const ExtensionHostMsg_Request_Params& last_params() { return last_params_; } |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); | 372 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); |
355 script_context->set_url(extension->url()); | 373 script_context->set_url(extension->url()); |
356 | 374 |
357 bindings_system()->UpdateBindingsForContext(script_context); | 375 bindings_system()->UpdateBindingsForContext(script_context); |
358 | 376 |
359 v8::Local<v8::Value> first_idle_object = | 377 v8::Local<v8::Value> first_idle_object = |
360 V8ValueFromScriptSource(context, "chrome.idle"); | 378 V8ValueFromScriptSource(context, "chrome.idle"); |
361 ASSERT_FALSE(first_idle_object.IsEmpty()); | 379 ASSERT_FALSE(first_idle_object.IsEmpty()); |
362 EXPECT_TRUE(first_idle_object->IsObject()); | 380 EXPECT_TRUE(first_idle_object->IsObject()); |
363 | 381 |
364 DisposeContext(); | 382 DisposeMainScriptContext(); |
365 | 383 |
366 // Check an API that was instantiated.... | 384 // Check an API that was instantiated.... |
367 v8::Local<v8::Value> second_idle_object = | 385 v8::Local<v8::Value> second_idle_object = |
368 V8ValueFromScriptSource(context, "chrome.idle"); | 386 V8ValueFromScriptSource(context, "chrome.idle"); |
369 ASSERT_FALSE(second_idle_object.IsEmpty()); | 387 ASSERT_FALSE(second_idle_object.IsEmpty()); |
370 EXPECT_TRUE(second_idle_object->IsUndefined()); | 388 EXPECT_TRUE(second_idle_object->IsUndefined()); |
371 // ... and also one that wasn't. | 389 // ... and also one that wasn't. |
372 v8::Local<v8::Value> power_object = | 390 v8::Local<v8::Value> power_object = |
373 V8ValueFromScriptSource(context, "chrome.power"); | 391 V8ValueFromScriptSource(context, "chrome.power"); |
374 ASSERT_FALSE(power_object.IsEmpty()); | 392 ASSERT_FALSE(power_object.IsEmpty()); |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 const std::vector<std::string> kKeys = {"get", "set", "remove", "clear", | 743 const std::vector<std::string> kKeys = {"get", "set", "remove", "clear", |
726 "getBytesInUse"}; | 744 "getBytesInUse"}; |
727 for (const auto& key : kKeys) { | 745 for (const auto& key : kKeys) { |
728 v8::Local<v8::String> v8_key = gin::StringToV8(isolate(), key); | 746 v8::Local<v8::String> v8_key = gin::StringToV8(isolate(), key); |
729 EXPECT_TRUE(local_object->HasOwnProperty(context, v8_key).FromJust()) | 747 EXPECT_TRUE(local_object->HasOwnProperty(context, v8_key).FromJust()) |
730 << key; | 748 << key; |
731 } | 749 } |
732 } | 750 } |
733 | 751 |
734 } // namespace extensions | 752 } // namespace extensions |
OLD | NEW |