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/api_bindings_system.h" | 5 #include "extensions/renderer/api_bindings_system.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 " }]" | 46 " }]" |
47 " }, {" | 47 " }, {" |
48 " 'name': 'functionWithRefAndCallback'," | 48 " 'name': 'functionWithRefAndCallback'," |
49 " 'parameters': [{" | 49 " 'parameters': [{" |
50 " 'name': 'ref'," | 50 " 'name': 'ref'," |
51 " '$ref': 'objRef'" | 51 " '$ref': 'objRef'" |
52 " }, {" | 52 " }, {" |
53 " 'name': 'callback'," | 53 " 'name': 'callback'," |
54 " 'type': 'function'" | 54 " 'type': 'function'" |
55 " }]" | 55 " }]" |
| 56 " }]," |
| 57 " 'events': [{" |
| 58 " 'name': 'alphaEvent'" |
56 " }]" | 59 " }]" |
57 "}"; | 60 "}"; |
58 | 61 |
59 // Another fake API for testing. | 62 // Another fake API for testing. |
60 const char kBetaAPIName[] = "beta"; | 63 const char kBetaAPIName[] = "beta"; |
61 const char kBetaAPISpec[] = | 64 const char kBetaAPISpec[] = |
62 "{" | 65 "{" |
63 " 'functions': [{" | 66 " 'functions': [{" |
64 " 'name': 'simpleFunc'," | 67 " 'name': 'simpleFunc'," |
65 " 'parameters': [{'name': 'int', 'type': 'integer'}]" | 68 " 'parameters': [{'name': 'int', 'type': 'integer'}]" |
(...skipping 20 matching lines...) Expand all Loading... |
86 ASSERT_FALSE(last_request_); | 89 ASSERT_FALSE(last_request_); |
87 last_request_ = std::move(request); | 90 last_request_ = std::move(request); |
88 } | 91 } |
89 | 92 |
90 protected: | 93 protected: |
91 APIBindingsSystemTestBase() {} | 94 APIBindingsSystemTestBase() {} |
92 void SetUp() override; | 95 void SetUp() override; |
93 | 96 |
94 void TearDown() override { | 97 void TearDown() override { |
95 bindings_system_.reset(); | 98 bindings_system_.reset(); |
| 99 |
| 100 v8::Global<v8::Context> weak_context(instance_->isolate(), context_); |
| 101 weak_context.SetWeak(); |
| 102 |
96 holder_.reset(); | 103 holder_.reset(); |
97 gin::V8Test::TearDown(); | 104 |
| 105 // NOTE: We explicitly do NOT call gin::V8Test::TearDown() here because we |
| 106 // do intermittent validation by doing a garbage collection after context |
| 107 // destruction and ensuring the context is fully released (which wouldn't |
| 108 // happen in cycles). |
| 109 // TODO(devlin): It might be time to move off V8Test if we're doing this. |
| 110 { |
| 111 v8::HandleScope handle_scope(instance_->isolate()); |
| 112 v8::Local<v8::Context>::New(instance_->isolate(), context_)->Exit(); |
| 113 context_.Reset(); |
| 114 } |
| 115 |
| 116 // Garbage collect everything so that we find any issues where we might be |
| 117 // double-freeing. |
| 118 // '5' is a magic number stolen from Blink; arbitrarily large enough to |
| 119 // hopefully clean up all the various paths. |
| 120 for (int i = 0; i < 5; i++) { |
| 121 instance_->isolate()->RequestGarbageCollectionForTesting( |
| 122 v8::Isolate::kFullGarbageCollection); |
| 123 } |
| 124 |
| 125 ASSERT_TRUE(weak_context.IsEmpty()); |
| 126 |
| 127 instance_->isolate()->Exit(); |
| 128 instance_.reset(); |
98 } | 129 } |
99 | 130 |
100 // Checks that |last_request_| exists and was provided with the | 131 // Checks that |last_request_| exists and was provided with the |
101 // |expected_name| and |expected_arguments|. | 132 // |expected_name| and |expected_arguments|. |
102 void ValidateLastRequest(const std::string& expected_name, | 133 void ValidateLastRequest(const std::string& expected_name, |
103 const std::string& expected_arguments); | 134 const std::string& expected_arguments); |
104 | 135 |
105 const APIBindingsSystem::Request* last_request() const { | 136 const APIBindingsSystem::Request* last_request() const { |
106 return last_request_.get(); | 137 return last_request_.get(); |
107 } | 138 } |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 base::ListValue()); | 294 base::ListValue()); |
264 | 295 |
265 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( | 296 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( |
266 context->Global(), context, "callbackArguments"); | 297 context->Global(), context, "callbackArguments"); |
267 ASSERT_TRUE(result); | 298 ASSERT_TRUE(result); |
268 EXPECT_EQ("[]", ValueToString(*result)); | 299 EXPECT_EQ("[]", ValueToString(*result)); |
269 reset_last_request(); | 300 reset_last_request(); |
270 } | 301 } |
271 | 302 |
272 { | 303 { |
| 304 // Test an event registration -> event occurrence. |
| 305 const char kTestCall[] = |
| 306 "obj.alphaEvent.addListener(function() {\n" |
| 307 " this.eventArguments = Array.from(arguments);\n" |
| 308 "});\n"; |
| 309 CallFunctionOnObject(context, alpha_api, kTestCall); |
| 310 |
| 311 const char kResponseArgsJson[] = "['response',1,{'key':42}]"; |
| 312 std::unique_ptr<base::ListValue> expected_args = |
| 313 ListValueFromString(kResponseArgsJson); |
| 314 bindings_system()->FireEventInContext("alpha.alphaEvent", context, |
| 315 *expected_args); |
| 316 |
| 317 std::unique_ptr<base::Value> result = GetBaseValuePropertyFromObject( |
| 318 context->Global(), context, "eventArguments"); |
| 319 ASSERT_TRUE(result); |
| 320 EXPECT_EQ(ReplaceSingleQuotes(kResponseArgsJson), ValueToString(*result)); |
| 321 } |
| 322 |
| 323 { |
273 // Test a call -> response on the second API. | 324 // Test a call -> response on the second API. |
274 const char kTestCall[] = "obj.simpleFunc(2)"; | 325 const char kTestCall[] = "obj.simpleFunc(2)"; |
275 CallFunctionOnObject(context, beta_api, kTestCall); | 326 CallFunctionOnObject(context, beta_api, kTestCall); |
276 ValidateLastRequest("beta.simpleFunc", "[2]"); | 327 ValidateLastRequest("beta.simpleFunc", "[2]"); |
277 EXPECT_TRUE(last_request()->request_id.empty()); | 328 EXPECT_TRUE(last_request()->request_id.empty()); |
278 reset_last_request(); | 329 reset_last_request(); |
279 } | 330 } |
280 } | 331 } |
281 | 332 |
282 // An implementation using real API schemas. | 333 // An implementation using real API schemas. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 reset_last_request(); // Just to not pollute future results. | 460 reset_last_request(); // Just to not pollute future results. |
410 } | 461 } |
411 | 462 |
412 { | 463 { |
413 // The queryState() param has a minimum of 15. | 464 // The queryState() param has a minimum of 15. |
414 const char kTestCall[] = "chrome.idle.queryState(10, function() {});"; | 465 const char kTestCall[] = "chrome.idle.queryState(10, function() {});"; |
415 ExecuteScriptAndExpectError(context, kTestCall, kError); | 466 ExecuteScriptAndExpectError(context, kTestCall, kError); |
416 EXPECT_FALSE(last_request()); | 467 EXPECT_FALSE(last_request()); |
417 reset_last_request(); // Just to not pollute future results. | 468 reset_last_request(); // Just to not pollute future results. |
418 } | 469 } |
| 470 |
| 471 { |
| 472 const char kTestCall[] = |
| 473 "chrome.idle.onStateChanged.addListener(state => {\n" |
| 474 " this.idleState = state;\n" |
| 475 "});\n"; |
| 476 ExecuteScript(context, kTestCall); |
| 477 v8::Local<v8::Value> v8_result = |
| 478 GetPropertyFromObject(context->Global(), context, "idleState"); |
| 479 EXPECT_TRUE(v8_result->IsUndefined()); |
| 480 bindings_system()->FireEventInContext("idle.onStateChanged", context, |
| 481 *ListValueFromString("['active']")); |
| 482 |
| 483 std::unique_ptr<base::Value> result = |
| 484 GetBaseValuePropertyFromObject(context->Global(), context, "idleState"); |
| 485 ASSERT_TRUE(result); |
| 486 EXPECT_EQ("\"active\"", ValueToString(*result)); |
| 487 } |
419 } | 488 } |
420 | 489 |
421 } // namespace extensions | 490 } // namespace extensions |
OLD | NEW |