OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/module_system_test.h" | 5 #include "extensions/renderer/module_system_test.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
18 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
19 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
20 #include "base/path_service.h" | 20 #include "base/path_service.h" |
21 #include "base/strings/string_piece.h" | 21 #include "base/strings/string_piece.h" |
22 #include "extensions/common/extension_paths.h" | 22 #include "extensions/common/extension_paths.h" |
23 #include "extensions/renderer/logging_native_handler.h" | 23 #include "extensions/renderer/logging_native_handler.h" |
24 #include "extensions/renderer/object_backed_native_handler.h" | 24 #include "extensions/renderer/object_backed_native_handler.h" |
25 #include "extensions/renderer/safe_builtins.h" | 25 #include "extensions/renderer/safe_builtins.h" |
26 #include "extensions/renderer/source_map.h" | 26 #include "extensions/renderer/string_source_map.h" |
| 27 #include "extensions/renderer/test_v8_extension_configuration.h" |
27 #include "extensions/renderer/utils_native_handler.h" | 28 #include "extensions/renderer/utils_native_handler.h" |
28 #include "ui/base/resource/resource_bundle.h" | 29 #include "ui/base/resource/resource_bundle.h" |
29 | 30 |
30 namespace extensions { | 31 namespace extensions { |
31 namespace { | 32 namespace { |
32 | 33 |
33 class FailsOnException : public ModuleSystem::ExceptionHandler { | 34 class FailsOnException : public ModuleSystem::ExceptionHandler { |
34 public: | 35 public: |
35 FailsOnException() : ModuleSystem::ExceptionHandler(nullptr) {} | 36 FailsOnException() : ModuleSystem::ExceptionHandler(nullptr) {} |
36 void HandleUncaughtException(const v8::TryCatch& try_catch) override { | 37 void HandleUncaughtException(const v8::TryCatch& try_catch) override { |
37 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); | 38 FAIL() << "Uncaught exception: " << CreateExceptionString(try_catch); |
38 } | 39 } |
39 }; | 40 }; |
40 | 41 |
41 class V8ExtensionConfigurator { | |
42 public: | |
43 V8ExtensionConfigurator() | |
44 : safe_builtins_(SafeBuiltins::CreateV8Extension()), | |
45 names_(1, safe_builtins_->name()), | |
46 configuration_( | |
47 new v8::ExtensionConfiguration(static_cast<int>(names_.size()), | |
48 names_.data())) { | |
49 v8::RegisterExtension(safe_builtins_.get()); | |
50 } | |
51 | |
52 v8::ExtensionConfiguration* GetConfiguration() { | |
53 return configuration_.get(); | |
54 } | |
55 | |
56 private: | |
57 std::unique_ptr<v8::Extension> safe_builtins_; | |
58 std::vector<const char*> names_; | |
59 std::unique_ptr<v8::ExtensionConfiguration> configuration_; | |
60 }; | |
61 | |
62 base::LazyInstance<V8ExtensionConfigurator>::Leaky g_v8_extension_configurator = | |
63 LAZY_INSTANCE_INITIALIZER; | |
64 | |
65 } // namespace | 42 } // namespace |
66 | 43 |
67 // Native JS functions for doing asserts. | 44 // Native JS functions for doing asserts. |
68 class ModuleSystemTestEnvironment::AssertNatives | 45 class ModuleSystemTestEnvironment::AssertNatives |
69 : public ObjectBackedNativeHandler { | 46 : public ObjectBackedNativeHandler { |
70 public: | 47 public: |
71 explicit AssertNatives(ScriptContext* context) | 48 explicit AssertNatives(ScriptContext* context) |
72 : ObjectBackedNativeHandler(context), | 49 : ObjectBackedNativeHandler(context), |
73 assertion_made_(false), | 50 assertion_made_(false), |
74 failed_(false) { | 51 failed_(false) { |
(...skipping 18 matching lines...) Expand all Loading... |
93 CHECK_EQ(1, args.Length()); | 70 CHECK_EQ(1, args.Length()); |
94 assertion_made_ = true; | 71 assertion_made_ = true; |
95 failed_ = failed_ || args[0]->ToBoolean(args.GetIsolate())->Value(); | 72 failed_ = failed_ || args[0]->ToBoolean(args.GetIsolate())->Value(); |
96 } | 73 } |
97 | 74 |
98 private: | 75 private: |
99 bool assertion_made_; | 76 bool assertion_made_; |
100 bool failed_; | 77 bool failed_; |
101 }; | 78 }; |
102 | 79 |
103 // Source map that operates on std::strings. | |
104 class ModuleSystemTestEnvironment::StringSourceMap : public SourceMap { | |
105 public: | |
106 StringSourceMap() {} | |
107 ~StringSourceMap() override {} | |
108 | |
109 v8::Local<v8::String> GetSource(v8::Isolate* isolate, | |
110 const std::string& name) const override { | |
111 const auto& source_map_iter = source_map_.find(name); | |
112 if (source_map_iter == source_map_.end()) | |
113 return v8::Local<v8::String>(); | |
114 return v8::String::NewFromUtf8(isolate, source_map_iter->second.c_str()); | |
115 } | |
116 | |
117 bool Contains(const std::string& name) const override { | |
118 return source_map_.count(name); | |
119 } | |
120 | |
121 void RegisterModule(const std::string& name, const std::string& source) { | |
122 CHECK_EQ(0u, source_map_.count(name)) << "Module " << name << " not found"; | |
123 source_map_[name] = source; | |
124 } | |
125 | |
126 private: | |
127 std::map<std::string, std::string> source_map_; | |
128 }; | |
129 | |
130 ModuleSystemTestEnvironment::ModuleSystemTestEnvironment(v8::Isolate* isolate) | 80 ModuleSystemTestEnvironment::ModuleSystemTestEnvironment(v8::Isolate* isolate) |
131 : isolate_(isolate), | 81 : isolate_(isolate), |
132 context_holder_(new gin::ContextHolder(isolate_)), | 82 context_holder_(new gin::ContextHolder(isolate_)), |
133 handle_scope_(isolate_), | 83 handle_scope_(isolate_), |
134 source_map_(new StringSourceMap()) { | 84 source_map_(new StringSourceMap()) { |
135 context_holder_->SetContext(v8::Context::New( | 85 context_holder_->SetContext(v8::Context::New( |
136 isolate, g_v8_extension_configurator.Get().GetConfiguration())); | 86 isolate, TestV8ExtensionConfiguration::GetConfiguration())); |
137 context_.reset(new ScriptContext(context_holder_->context(), | 87 context_.reset(new ScriptContext(context_holder_->context(), |
138 nullptr, // WebFrame | 88 nullptr, // WebFrame |
139 nullptr, // Extension | 89 nullptr, // Extension |
140 Feature::BLESSED_EXTENSION_CONTEXT, | 90 Feature::BLESSED_EXTENSION_CONTEXT, |
141 nullptr, // Effective Extension | 91 nullptr, // Effective Extension |
142 Feature::BLESSED_EXTENSION_CONTEXT)); | 92 Feature::BLESSED_EXTENSION_CONTEXT)); |
143 context_->v8_context()->Enter(); | 93 context_->v8_context()->Enter(); |
144 assert_natives_ = new AssertNatives(context_.get()); | 94 assert_natives_ = new AssertNatives(context_.get()); |
145 | 95 |
146 { | 96 { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 205 |
256 void ModuleSystemTest::ExpectNoAssertionsMade() { | 206 void ModuleSystemTest::ExpectNoAssertionsMade() { |
257 should_assertions_be_made_ = false; | 207 should_assertions_be_made_ = false; |
258 } | 208 } |
259 | 209 |
260 void ModuleSystemTest::RunResolvedPromises() { | 210 void ModuleSystemTest::RunResolvedPromises() { |
261 v8::MicrotasksScope::PerformCheckpoint(isolate_); | 211 v8::MicrotasksScope::PerformCheckpoint(isolate_); |
262 } | 212 } |
263 | 213 |
264 } // namespace extensions | 214 } // namespace extensions |
OLD | NEW |