Chromium Code Reviews| 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 "components/crx_file/id_util.h" | 9 #include "components/crx_file/id_util.h" |
| 10 #include "extensions/common/extension.h" | 10 #include "extensions/common/extension.h" |
| 11 #include "extensions/common/extension_builder.h" | 11 #include "extensions/common/extension_builder.h" |
| 12 #include "extensions/common/extension_messages.h" | 12 #include "extensions/common/extension_messages.h" |
| 13 #include "extensions/common/manifest.h" | 13 #include "extensions/common/manifest.h" |
| 14 #include "extensions/common/permissions/permissions_data.h" | 14 #include "extensions/common/permissions/permissions_data.h" |
| 15 #include "extensions/common/value_builder.h" | 15 #include "extensions/common/value_builder.h" |
| 16 #include "extensions/renderer/api_binding_test.h" | 16 #include "extensions/renderer/api_binding_test.h" |
| 17 #include "extensions/renderer/api_binding_test_util.h" | 17 #include "extensions/renderer/api_binding_test_util.h" |
| 18 #include "extensions/renderer/module_system.h" | |
| 19 #include "extensions/renderer/safe_builtins.h" | |
| 18 #include "extensions/renderer/script_context.h" | 20 #include "extensions/renderer/script_context.h" |
| 19 #include "extensions/renderer/script_context_set.h" | 21 #include "extensions/renderer/script_context_set.h" |
| 22 #include "extensions/renderer/string_source_map.h" | |
| 23 #include "extensions/renderer/test_v8_extension_configuration.h" | |
| 20 | 24 |
| 21 namespace extensions { | 25 namespace extensions { |
| 22 | 26 |
| 23 namespace { | 27 namespace { |
| 24 | 28 |
| 25 // Creates an extension with the given |name| and |permissions|. | 29 // Creates an extension with the given |name| and |permissions|. |
| 26 scoped_refptr<Extension> CreateExtension( | 30 scoped_refptr<Extension> CreateExtension( |
| 27 const std::string& name, | 31 const std::string& name, |
| 28 const std::vector<std::string>& permissions) { | 32 const std::vector<std::string>& permissions) { |
| 29 DictionaryBuilder manifest; | 33 DictionaryBuilder manifest; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 45 } | 49 } |
| 46 | 50 |
| 47 } // namespace | 51 } // namespace |
| 48 | 52 |
| 49 class NativeExtensionBindingsSystemUnittest : public APIBindingTest { | 53 class NativeExtensionBindingsSystemUnittest : public APIBindingTest { |
| 50 public: | 54 public: |
| 51 NativeExtensionBindingsSystemUnittest() {} | 55 NativeExtensionBindingsSystemUnittest() {} |
| 52 ~NativeExtensionBindingsSystemUnittest() override {} | 56 ~NativeExtensionBindingsSystemUnittest() override {} |
| 53 | 57 |
| 54 protected: | 58 protected: |
| 59 v8::ExtensionConfiguration* GetV8ExtensionConfiguration() override { | |
| 60 return TestV8ExtensionConfiguration::GetConfiguration(); | |
| 61 } | |
| 62 | |
| 55 void SetUp() override { | 63 void SetUp() override { |
| 56 script_context_set_ = base::MakeUnique<ScriptContextSet>(&extension_ids_); | 64 script_context_set_ = base::MakeUnique<ScriptContextSet>(&extension_ids_); |
| 57 bindings_system_ = base::MakeUnique<NativeExtensionBindingsSystem>( | 65 bindings_system_ = base::MakeUnique<NativeExtensionBindingsSystem>( |
| 58 base::Bind(&NativeExtensionBindingsSystemUnittest::MockSendIPC, | 66 base::Bind(&NativeExtensionBindingsSystemUnittest::MockSendIPC, |
| 59 base::Unretained(this))); | 67 base::Unretained(this))); |
| 60 APIBindingTest::SetUp(); | 68 APIBindingTest::SetUp(); |
| 61 } | 69 } |
| 62 | 70 |
| 63 void TearDown() override { | 71 void TearDown() override { |
| 64 for (const auto& context : raw_script_contexts_) | 72 for (const auto& context : raw_script_contexts_) |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 80 last_params_.user_gesture = params.user_gesture; | 88 last_params_.user_gesture = params.user_gesture; |
| 81 last_params_.worker_thread_id = params.worker_thread_id; | 89 last_params_.worker_thread_id = params.worker_thread_id; |
| 82 last_params_.service_worker_version_id = params.service_worker_version_id; | 90 last_params_.service_worker_version_id = params.service_worker_version_id; |
| 83 } | 91 } |
| 84 | 92 |
| 85 ScriptContext* CreateScriptContext(v8::Local<v8::Context> v8_context, | 93 ScriptContext* CreateScriptContext(v8::Local<v8::Context> v8_context, |
| 86 Extension* extension, | 94 Extension* extension, |
| 87 Feature::Context context_type) { | 95 Feature::Context context_type) { |
| 88 auto script_context = base::MakeUnique<ScriptContext>( | 96 auto script_context = base::MakeUnique<ScriptContext>( |
| 89 v8_context, nullptr, extension, context_type, extension, context_type); | 97 v8_context, nullptr, extension, context_type, extension, context_type); |
| 98 script_context->set_module_system( | |
| 99 base::MakeUnique<ModuleSystem>(script_context.get(), source_map())); | |
| 90 ScriptContext* raw_script_context = script_context.get(); | 100 ScriptContext* raw_script_context = script_context.get(); |
| 91 raw_script_contexts_.push_back(raw_script_context); | 101 raw_script_contexts_.push_back(raw_script_context); |
| 92 script_context_set_->AddForTesting(std::move(script_context)); | 102 script_context_set_->AddForTesting(std::move(script_context)); |
| 93 return raw_script_context; | 103 return raw_script_context; |
| 94 } | 104 } |
| 95 | 105 |
| 96 void RegisterExtension(const ExtensionId& id) { extension_ids_.insert(id); } | 106 void RegisterExtension(const ExtensionId& id) { extension_ids_.insert(id); } |
| 97 | 107 |
| 98 NativeExtensionBindingsSystem* bindings_system() { | 108 NativeExtensionBindingsSystem* bindings_system() { |
| 99 return bindings_system_.get(); | 109 return bindings_system_.get(); |
| 100 } | 110 } |
| 101 const ExtensionHostMsg_Request_Params& last_params() { return last_params_; } | 111 const ExtensionHostMsg_Request_Params& last_params() { return last_params_; } |
| 112 StringSourceMap* source_map() { return &source_map_; } | |
| 102 | 113 |
| 103 private: | 114 private: |
| 104 ExtensionIdSet extension_ids_; | 115 ExtensionIdSet extension_ids_; |
| 105 std::unique_ptr<ScriptContextSet> script_context_set_; | 116 std::unique_ptr<ScriptContextSet> script_context_set_; |
| 106 std::vector<ScriptContext*> raw_script_contexts_; | 117 std::vector<ScriptContext*> raw_script_contexts_; |
| 107 std::unique_ptr<NativeExtensionBindingsSystem> bindings_system_; | 118 std::unique_ptr<NativeExtensionBindingsSystem> bindings_system_; |
| 108 ExtensionHostMsg_Request_Params last_params_; | 119 ExtensionHostMsg_Request_Params last_params_; |
| 120 StringSourceMap source_map_; | |
| 109 | 121 |
| 110 DISALLOW_COPY_AND_ASSIGN(NativeExtensionBindingsSystemUnittest); | 122 DISALLOW_COPY_AND_ASSIGN(NativeExtensionBindingsSystemUnittest); |
| 111 }; | 123 }; |
| 112 | 124 |
| 113 TEST_F(NativeExtensionBindingsSystemUnittest, Basic) { | 125 TEST_F(NativeExtensionBindingsSystemUnittest, Basic) { |
| 114 scoped_refptr<Extension> extension = | 126 scoped_refptr<Extension> extension = |
| 115 CreateExtension("foo", {"idle", "power"}); | 127 CreateExtension("foo", {"idle", "power"}); |
| 116 RegisterExtension(extension->id()); | 128 RegisterExtension(extension->id()); |
| 117 | 129 |
| 118 v8::HandleScope handle_scope(isolate()); | 130 v8::HandleScope handle_scope(isolate()); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 253 V8ValueFromScriptSource(context, "chrome.idle"); | 265 V8ValueFromScriptSource(context, "chrome.idle"); |
| 254 ASSERT_FALSE(second_idle_object.IsEmpty()); | 266 ASSERT_FALSE(second_idle_object.IsEmpty()); |
| 255 EXPECT_TRUE(second_idle_object->IsUndefined()); | 267 EXPECT_TRUE(second_idle_object->IsUndefined()); |
| 256 // ... and also one that wasn't. | 268 // ... and also one that wasn't. |
| 257 v8::Local<v8::Value> power_object = | 269 v8::Local<v8::Value> power_object = |
| 258 V8ValueFromScriptSource(context, "chrome.power"); | 270 V8ValueFromScriptSource(context, "chrome.power"); |
| 259 ASSERT_FALSE(power_object.IsEmpty()); | 271 ASSERT_FALSE(power_object.IsEmpty()); |
| 260 EXPECT_TRUE(power_object->IsUndefined()); | 272 EXPECT_TRUE(power_object->IsUndefined()); |
| 261 } | 273 } |
| 262 | 274 |
| 275 // Tests that traditional custom bindings can be used with the native bindings | |
| 276 // system. | |
| 277 TEST_F(NativeExtensionBindingsSystemUnittest, TestBridgingToJSCustomBindings) { | |
| 278 // Custom binding code. This basically utilizes the interface in binding.js in | |
| 279 // order to test backwards compatibility. | |
| 280 const char kCustomBinding[] = | |
| 281 "apiBridge.registerCustomHook(api => {\n" | |
| 282 " api.apiFunctions.setHandleRequest('queryState',\n" | |
|
jbroman
2016/12/20 20:51:35
Do we need more exhaustive coverage than this one
Devlin
2016/12/20 22:20:19
Made this test more thorough. Note also that a lo
| |
| 283 " (time, callback) => {\n" | |
| 284 " this.timeArg = time;\n" | |
| 285 " callback('active');\n" | |
| 286 " });\n" | |
| 287 "});\n"; | |
| 288 | |
| 289 source_map()->RegisterModule("idle", kCustomBinding); | |
| 290 | |
| 291 scoped_refptr<Extension> extension = CreateExtension("foo", {"idle"}); | |
| 292 RegisterExtension(extension->id()); | |
| 293 | |
| 294 v8::HandleScope handle_scope(isolate()); | |
| 295 v8::Local<v8::Context> context = ContextLocal(); | |
| 296 | |
| 297 ScriptContext* script_context = CreateScriptContext( | |
| 298 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); | |
| 299 script_context->set_url(extension->url()); | |
| 300 | |
| 301 bindings_system()->UpdateBindingsForContext(script_context); | |
| 302 | |
| 303 { | |
| 304 // Call the function correctly. | |
| 305 const char kCallIdleQueryState[] = | |
| 306 "(function() {\n" | |
| 307 " chrome.idle.queryState(30, function(state) {\n" | |
| 308 " this.responseState = state;\n" | |
| 309 " });\n" | |
| 310 "});"; | |
| 311 | |
| 312 v8::Local<v8::Function> call_idle_query_state = | |
| 313 FunctionFromString(context, kCallIdleQueryState); | |
| 314 RunFunctionOnGlobal(call_idle_query_state, context, 0, nullptr); | |
| 315 } | |
| 316 | |
| 317 // We need to check two pieces: first, that the custom handler was called | |
| 318 // with the proper arguments.... | |
| 319 std::unique_ptr<base::Value> result_value = | |
| 320 GetBaseValuePropertyFromObject(context->Global(), context, "timeArg"); | |
| 321 ASSERT_TRUE(result_value); | |
| 322 EXPECT_EQ("30", ValueToString(*result_value)); | |
| 323 | |
| 324 // ...and second, that the callback was called with the proper result. | |
| 325 result_value = GetBaseValuePropertyFromObject(context->Global(), context, | |
| 326 "responseState"); | |
| 327 ASSERT_TRUE(result_value); | |
| 328 EXPECT_EQ("\"active\"", ValueToString(*result_value)); | |
| 329 } | |
| 330 | |
| 263 } // namespace extensions | 331 } // namespace extensions |
| OLD | NEW |