Devlin Cronin | c605569 | 2017-07-12 17:54:40 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "extensions/renderer/api_activity_logger.h" |
| 6 | |
Gabriel Charette | 078e366 | 2017-08-28 22:59:04 | [diff] [blame] | 7 | #include "base/run_loop.h" |
Devlin Cronin | c605569 | 2017-07-12 17:54:40 | [diff] [blame] | 8 | #include "content/public/test/mock_render_thread.h" |
Devlin Cronin | c89373f | 2017-08-21 22:08:33 | [diff] [blame] | 9 | #include "extensions/common/extension_builder.h" |
Devlin Cronin | c605569 | 2017-07-12 17:54:40 | [diff] [blame] | 10 | #include "extensions/common/extension_messages.h" |
| 11 | #include "extensions/common/features/feature.h" |
Devlin Cronin | c605569 | 2017-07-12 17:54:40 | [diff] [blame] | 12 | #include "extensions/renderer/bindings/api_binding_test.h" |
| 13 | #include "extensions/renderer/bindings/api_binding_test_util.h" |
| 14 | #include "extensions/renderer/script_context.h" |
| 15 | #include "extensions/renderer/script_context_set.h" |
| 16 | #include "extensions/renderer/test_extensions_renderer_client.h" |
| 17 | #include "ipc/ipc_message.h" |
| 18 | #include "ipc/ipc_test_sink.h" |
| 19 | |
| 20 | namespace extensions { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class ScopedAllowActivityLogging { |
| 25 | public: |
| 26 | ScopedAllowActivityLogging() { APIActivityLogger::set_log_for_testing(true); } |
| 27 | |
| 28 | ~ScopedAllowActivityLogging() { |
| 29 | APIActivityLogger::set_log_for_testing(false); |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | DISALLOW_COPY_AND_ASSIGN(ScopedAllowActivityLogging); |
| 34 | }; |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | using ActivityLoggerTest = APIBindingTest; |
| 39 | |
| 40 | // Regression test for crbug.com/740866. |
| 41 | TEST_F(ActivityLoggerTest, DontCrashOnUnconvertedValues) { |
| 42 | content::MockRenderThread mock_render_thread; |
| 43 | TestExtensionsRendererClient client; |
| 44 | std::set<ExtensionId> extension_ids; |
| 45 | ScriptContextSet script_context_set(&extension_ids); |
| 46 | |
| 47 | ScopedAllowActivityLogging scoped_allow_activity_logging; |
| 48 | |
| 49 | v8::HandleScope handle_scope(isolate()); |
| 50 | v8::Local<v8::Context> context = MainContext(); |
| 51 | |
Devlin Cronin | c89373f | 2017-08-21 22:08:33 | [diff] [blame] | 52 | scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build(); |
Devlin Cronin | c605569 | 2017-07-12 17:54:40 | [diff] [blame] | 53 | extension_ids.insert(extension->id()); |
| 54 | const Feature::Context kContextType = Feature::BLESSED_EXTENSION_CONTEXT; |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 55 | script_context_set.AddForTesting(std::make_unique<ScriptContext>( |
Devlin Cronin | c605569 | 2017-07-12 17:54:40 | [diff] [blame] | 56 | context, nullptr, extension.get(), kContextType, extension.get(), |
| 57 | kContextType)); |
| 58 | |
| 59 | std::vector<v8::Local<v8::Value>> args = {v8::Undefined(isolate())}; |
| 60 | |
| 61 | IPC::TestSink& sink = mock_render_thread.sink(); |
| 62 | sink.ClearMessages(); |
| 63 | |
| 64 | APIActivityLogger::LogAPICall(context, "someApiMethod", args); |
| 65 | |
| 66 | ASSERT_EQ(1u, sink.message_count()); |
| 67 | const IPC::Message* message = sink.GetMessageAt(0u); |
Hans Wennborg | 4b99b0f | 2017-12-07 17:01:28 | [diff] [blame] | 68 | ASSERT_EQ( |
| 69 | static_cast<uint32_t>(ExtensionHostMsg_AddAPIActionToActivityLog::ID), |
| 70 | message->type()); |
Devlin Cronin | c605569 | 2017-07-12 17:54:40 | [diff] [blame] | 71 | ExtensionHostMsg_AddAPIActionToActivityLog::Param full_params; |
| 72 | ASSERT_TRUE( |
| 73 | ExtensionHostMsg_AddAPIActionToActivityLog::Read(message, &full_params)); |
| 74 | std::string extension_id = std::get<0>(full_params); |
jdoerrie | cc9f573 | 2017-08-23 14:12:30 | [diff] [blame] | 75 | ExtensionHostMsg_APIActionOrEvent_Params params; |
| 76 | params.api_call = std::get<1>(full_params).api_call; |
| 77 | params.arguments = |
| 78 | base::ListValue(std::get<1>(full_params).arguments.GetList()); |
| 79 | params.extra = std::get<1>(full_params).extra; |
Devlin Cronin | c605569 | 2017-07-12 17:54:40 | [diff] [blame] | 80 | EXPECT_EQ(extension->id(), extension_id); |
| 81 | ASSERT_EQ(1u, params.arguments.GetList().size()); |
| 82 | EXPECT_EQ(base::Value::Type::NONE, params.arguments.GetList()[0].type()); |
| 83 | |
| 84 | ScriptContext* script_context = script_context_set.GetByV8Context(context); |
| 85 | script_context_set.Remove(script_context); |
| 86 | base::RunLoop().RunUntilIdle(); // Let script context destruction complete. |
| 87 | } |
| 88 | |
| 89 | } // namespace extensions |