blob: 04d29ff44a3d0d05eba3b8cb99f33606ccd6f542 [file] [log] [blame]
Devlin Croninc6055692017-07-12 17:54:401// 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 Croninc89373f2017-08-21 22:08:339#include "extensions/common/extension_builder.h"
Devlin Croninc6055692017-07-12 17:54:4010#include "extensions/common/extension_messages.h"
11#include "extensions/common/features/feature.h"
Devlin Croninc6055692017-07-12 17:54:4012#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
20namespace extensions {
21
22namespace {
23
24class 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
38using ActivityLoggerTest = APIBindingTest;
39
40// Regression test for crbug.com/740866.
41TEST_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 Croninc89373f2017-08-21 22:08:3352 scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
Devlin Croninc6055692017-07-12 17:54:4053 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);
jdoerriecc9f5732017-08-23 14:12:3073 ExtensionHostMsg_APIActionOrEvent_Params params;
74 params.api_call = std::get<1>(full_params).api_call;
75 params.arguments =
76 base::ListValue(std::get<1>(full_params).arguments.GetList());
77 params.extra = std::get<1>(full_params).extra;
Devlin Croninc6055692017-07-12 17:54:4078 EXPECT_EQ(extension->id(), extension_id);
79 ASSERT_EQ(1u, params.arguments.GetList().size());
80 EXPECT_EQ(base::Value::Type::NONE, params.arguments.GetList()[0].type());
81
82 ScriptContext* script_context = script_context_set.GetByV8Context(context);
83 script_context_set.Remove(script_context);
84 base::RunLoop().RunUntilIdle(); // Let script context destruction complete.
85}
86
87} // namespace extensions