Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: extensions/renderer/native_extension_bindings_system_unittest.cc

Issue 2575173002: [Extensions Bindings] Add a bridge to use current custom bindings (Closed)
Patch Set: jbroman II Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/crx_file/id_util.h" 10 #include "components/crx_file/id_util.h"
10 #include "extensions/common/extension.h" 11 #include "extensions/common/extension.h"
11 #include "extensions/common/extension_builder.h" 12 #include "extensions/common/extension_builder.h"
12 #include "extensions/common/extension_messages.h" 13 #include "extensions/common/extension_messages.h"
13 #include "extensions/common/manifest.h" 14 #include "extensions/common/manifest.h"
14 #include "extensions/common/permissions/permissions_data.h" 15 #include "extensions/common/permissions/permissions_data.h"
15 #include "extensions/common/value_builder.h" 16 #include "extensions/common/value_builder.h"
16 #include "extensions/renderer/api_binding_test.h" 17 #include "extensions/renderer/api_binding_test.h"
17 #include "extensions/renderer/api_binding_test_util.h" 18 #include "extensions/renderer/api_binding_test_util.h"
19 #include "extensions/renderer/module_system.h"
20 #include "extensions/renderer/safe_builtins.h"
18 #include "extensions/renderer/script_context.h" 21 #include "extensions/renderer/script_context.h"
19 #include "extensions/renderer/script_context_set.h" 22 #include "extensions/renderer/script_context_set.h"
23 #include "extensions/renderer/string_source_map.h"
24 #include "extensions/renderer/test_v8_extension_configuration.h"
20 25
21 namespace extensions { 26 namespace extensions {
22 27
23 namespace { 28 namespace {
24 29
25 // Creates an extension with the given |name| and |permissions|. 30 // Creates an extension with the given |name| and |permissions|.
26 scoped_refptr<Extension> CreateExtension( 31 scoped_refptr<Extension> CreateExtension(
27 const std::string& name, 32 const std::string& name,
28 const std::vector<std::string>& permissions) { 33 const std::vector<std::string>& permissions) {
29 DictionaryBuilder manifest; 34 DictionaryBuilder manifest;
(...skipping 15 matching lines...) Expand all
45 } 50 }
46 51
47 } // namespace 52 } // namespace
48 53
49 class NativeExtensionBindingsSystemUnittest : public APIBindingTest { 54 class NativeExtensionBindingsSystemUnittest : public APIBindingTest {
50 public: 55 public:
51 NativeExtensionBindingsSystemUnittest() {} 56 NativeExtensionBindingsSystemUnittest() {}
52 ~NativeExtensionBindingsSystemUnittest() override {} 57 ~NativeExtensionBindingsSystemUnittest() override {}
53 58
54 protected: 59 protected:
60 v8::ExtensionConfiguration* GetV8ExtensionConfiguration() override {
61 return TestV8ExtensionConfiguration::GetConfiguration();
62 }
63
55 void SetUp() override { 64 void SetUp() override {
56 script_context_set_ = base::MakeUnique<ScriptContextSet>(&extension_ids_); 65 script_context_set_ = base::MakeUnique<ScriptContextSet>(&extension_ids_);
57 bindings_system_ = base::MakeUnique<NativeExtensionBindingsSystem>( 66 bindings_system_ = base::MakeUnique<NativeExtensionBindingsSystem>(
58 base::Bind(&NativeExtensionBindingsSystemUnittest::MockSendIPC, 67 base::Bind(&NativeExtensionBindingsSystemUnittest::MockSendIPC,
59 base::Unretained(this))); 68 base::Unretained(this)));
60 APIBindingTest::SetUp(); 69 APIBindingTest::SetUp();
61 } 70 }
62 71
63 void TearDown() override { 72 void TearDown() override {
64 for (const auto& context : raw_script_contexts_) 73 for (const auto& context : raw_script_contexts_)
(...skipping 15 matching lines...) Expand all
80 last_params_.user_gesture = params.user_gesture; 89 last_params_.user_gesture = params.user_gesture;
81 last_params_.worker_thread_id = params.worker_thread_id; 90 last_params_.worker_thread_id = params.worker_thread_id;
82 last_params_.service_worker_version_id = params.service_worker_version_id; 91 last_params_.service_worker_version_id = params.service_worker_version_id;
83 } 92 }
84 93
85 ScriptContext* CreateScriptContext(v8::Local<v8::Context> v8_context, 94 ScriptContext* CreateScriptContext(v8::Local<v8::Context> v8_context,
86 Extension* extension, 95 Extension* extension,
87 Feature::Context context_type) { 96 Feature::Context context_type) {
88 auto script_context = base::MakeUnique<ScriptContext>( 97 auto script_context = base::MakeUnique<ScriptContext>(
89 v8_context, nullptr, extension, context_type, extension, context_type); 98 v8_context, nullptr, extension, context_type, extension, context_type);
99 script_context->set_module_system(
100 base::MakeUnique<ModuleSystem>(script_context.get(), source_map()));
90 ScriptContext* raw_script_context = script_context.get(); 101 ScriptContext* raw_script_context = script_context.get();
91 raw_script_contexts_.push_back(raw_script_context); 102 raw_script_contexts_.push_back(raw_script_context);
92 script_context_set_->AddForTesting(std::move(script_context)); 103 script_context_set_->AddForTesting(std::move(script_context));
93 return raw_script_context; 104 return raw_script_context;
94 } 105 }
95 106
96 void RegisterExtension(const ExtensionId& id) { extension_ids_.insert(id); } 107 void RegisterExtension(const ExtensionId& id) { extension_ids_.insert(id); }
97 108
98 NativeExtensionBindingsSystem* bindings_system() { 109 NativeExtensionBindingsSystem* bindings_system() {
99 return bindings_system_.get(); 110 return bindings_system_.get();
100 } 111 }
101 const ExtensionHostMsg_Request_Params& last_params() { return last_params_; } 112 const ExtensionHostMsg_Request_Params& last_params() { return last_params_; }
113 StringSourceMap* source_map() { return &source_map_; }
102 114
103 private: 115 private:
104 ExtensionIdSet extension_ids_; 116 ExtensionIdSet extension_ids_;
105 std::unique_ptr<ScriptContextSet> script_context_set_; 117 std::unique_ptr<ScriptContextSet> script_context_set_;
106 std::vector<ScriptContext*> raw_script_contexts_; 118 std::vector<ScriptContext*> raw_script_contexts_;
107 std::unique_ptr<NativeExtensionBindingsSystem> bindings_system_; 119 std::unique_ptr<NativeExtensionBindingsSystem> bindings_system_;
108 ExtensionHostMsg_Request_Params last_params_; 120 ExtensionHostMsg_Request_Params last_params_;
121 StringSourceMap source_map_;
109 122
110 DISALLOW_COPY_AND_ASSIGN(NativeExtensionBindingsSystemUnittest); 123 DISALLOW_COPY_AND_ASSIGN(NativeExtensionBindingsSystemUnittest);
111 }; 124 };
112 125
113 TEST_F(NativeExtensionBindingsSystemUnittest, Basic) { 126 TEST_F(NativeExtensionBindingsSystemUnittest, Basic) {
114 scoped_refptr<Extension> extension = 127 scoped_refptr<Extension> extension =
115 CreateExtension("foo", {"idle", "power"}); 128 CreateExtension("foo", {"idle", "power"});
116 RegisterExtension(extension->id()); 129 RegisterExtension(extension->id());
117 130
118 v8::HandleScope handle_scope(isolate()); 131 v8::HandleScope handle_scope(isolate());
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 V8ValueFromScriptSource(context, "chrome.idle"); 266 V8ValueFromScriptSource(context, "chrome.idle");
254 ASSERT_FALSE(second_idle_object.IsEmpty()); 267 ASSERT_FALSE(second_idle_object.IsEmpty());
255 EXPECT_TRUE(second_idle_object->IsUndefined()); 268 EXPECT_TRUE(second_idle_object->IsUndefined());
256 // ... and also one that wasn't. 269 // ... and also one that wasn't.
257 v8::Local<v8::Value> power_object = 270 v8::Local<v8::Value> power_object =
258 V8ValueFromScriptSource(context, "chrome.power"); 271 V8ValueFromScriptSource(context, "chrome.power");
259 ASSERT_FALSE(power_object.IsEmpty()); 272 ASSERT_FALSE(power_object.IsEmpty());
260 EXPECT_TRUE(power_object->IsUndefined()); 273 EXPECT_TRUE(power_object->IsUndefined());
261 } 274 }
262 275
276 // Tests that traditional custom bindings can be used with the native bindings
277 // system.
278 TEST_F(NativeExtensionBindingsSystemUnittest, TestBridgingToJSCustomBindings) {
279 // Custom binding code. This basically utilizes the interface in binding.js in
280 // order to test backwards compatibility.
281 const char kCustomBinding[] =
282 "apiBridge.registerCustomHook((api, extensionId, contextType) => {\n"
283 " api.apiFunctions.setHandleRequest('queryState',\n"
284 " (time, callback) => {\n"
285 " this.timeArg = time;\n"
286 " callback('active');\n"
287 " });\n"
288 " this.hookedExtensionId = extensionId;\n"
289 " this.hookedContextType = contextType;\n"
290 " api.compiledApi.hookedApiProperty = 'someProperty';\n"
291 "});\n";
292
293 source_map()->RegisterModule("idle", kCustomBinding);
294
295 scoped_refptr<Extension> extension = CreateExtension("foo", {"idle"});
296 RegisterExtension(extension->id());
297
298 v8::HandleScope handle_scope(isolate());
299 v8::Local<v8::Context> context = ContextLocal();
300
301 ScriptContext* script_context = CreateScriptContext(
302 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT);
303 script_context->set_url(extension->url());
304
305 bindings_system()->UpdateBindingsForContext(script_context);
306
307 {
308 // Call the function correctly.
309 const char kCallIdleQueryState[] =
310 "(function() {\n"
311 " chrome.idle.queryState(30, function(state) {\n"
312 " this.responseState = state;\n"
313 " });\n"
314 "});";
315
316 v8::Local<v8::Function> call_idle_query_state =
317 FunctionFromString(context, kCallIdleQueryState);
318 RunFunctionOnGlobal(call_idle_query_state, context, 0, nullptr);
319 }
320
321 // To start, check that the properties we set when running the hooks are
322 // correct. We do this after calling the function because the API objects (and
323 // thus the hooks) are set up lazily.
324 std::unique_ptr<base::Value> property =
325 GetBaseValuePropertyFromObject(context->Global(), context,
326 "hookedExtensionId");
327 ASSERT_TRUE(property);
328 EXPECT_EQ(base::StringPrintf("\"%s\"", extension->id().c_str()),
329 ValueToString(*property));
330 property =
lazyboy 2016/12/21 19:49:58 (optional nit) This pattern of pulling a property
Devlin 2016/12/22 01:49:10 Yeah, I was debating even putting this in the util
331 GetBaseValuePropertyFromObject(context->Global(), context,
332 "hookedContextType");
333 ASSERT_TRUE(property);
334 EXPECT_EQ("\"BLESSED_EXTENSION\"", ValueToString(*property));
335 v8::Local<v8::Value> idle_api =
336 V8ValueFromScriptSource(context, "chrome.idle");
337 ASSERT_FALSE(idle_api.IsEmpty());
338 ASSERT_TRUE(idle_api->IsObject());
339 property =
340 GetBaseValuePropertyFromObject(idle_api.As<v8::Object>(), context,
341 "hookedApiProperty");
342 ASSERT_TRUE(property);
343 EXPECT_EQ("\"someProperty\"", ValueToString(*property));
344
345 // Next, we need to check two pieces: first, that the custom handler was
346 // called with the proper arguments....
347 std::unique_ptr<base::Value> result_value =
348 GetBaseValuePropertyFromObject(context->Global(), context, "timeArg");
349 ASSERT_TRUE(result_value);
350 EXPECT_EQ("30", ValueToString(*result_value));
351
352 // ...and second, that the callback was called with the proper result.
353 result_value = GetBaseValuePropertyFromObject(context->Global(), context,
354 "responseState");
355 ASSERT_TRUE(result_value);
356 EXPECT_EQ("\"active\"", ValueToString(*result_value));
357 }
358
263 } // namespace extensions 359 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698