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 | |
| 7 | #include "base/memory/ptr_util.h" |
| 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; |
| 55 | script_context_set.AddForTesting(base::MakeUnique<ScriptContext>( |
| 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); |
| 68 | ASSERT_EQ(ExtensionHostMsg_AddAPIActionToActivityLog::ID, message->type()); |
| 69 | ExtensionHostMsg_AddAPIActionToActivityLog::Param full_params; |
| 70 | ASSERT_TRUE( |
| 71 | ExtensionHostMsg_AddAPIActionToActivityLog::Read(message, &full_params)); |
| 72 | std::string extension_id = std::get<0>(full_params); |
| 73 | ExtensionHostMsg_APIActionOrEvent_Params params = std::get<1>(full_params); |
| 74 | EXPECT_EQ(extension->id(), extension_id); |
| 75 | ASSERT_EQ(1u, params.arguments.GetList().size()); |
| 76 | EXPECT_EQ(base::Value::Type::NONE, params.arguments.GetList()[0].type()); |
| 77 | |
| 78 | ScriptContext* script_context = script_context_set.GetByV8Context(context); |
| 79 | script_context_set.Remove(script_context); |
| 80 | base::RunLoop().RunUntilIdle(); // Let script context destruction complete. |
| 81 | } |
| 82 | |
| 83 | } // namespace extensions |