blob: 08eb53e26493857fda7d04470d57f714f30dc05e [file] [log] [blame]
[email protected]e6893672014-05-01 17:29:131// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]323722d2013-03-26 07:21:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avi2d124c02015-12-23 06:36:425#include <stddef.h>
6
[email protected]323722d2013-03-26 07:21:077#include <string>
[email protected]bcd9580f2014-04-17 19:17:598
[email protected]323722d2013-03-26 07:21:079#include "base/bind.h"
mek87e0ab52015-02-13 01:18:2610#include "content/public/child/v8_value_converter.h"
[email protected]323722d2013-03-26 07:21:0711#include "content/public/renderer/render_thread.h"
[email protected]fb820c02014-03-13 15:07:0812#include "extensions/common/extension_messages.h"
[email protected]fb5c4742014-05-01 09:05:2713#include "extensions/renderer/activity_log_converter_strategy.h"
[email protected]e6893672014-05-01 17:29:1314#include "extensions/renderer/api_activity_logger.h"
rdevlin.cronin6fba7ec2016-06-24 16:15:0515#include "extensions/renderer/dispatcher.h"
[email protected]bcd9580f2014-04-17 19:17:5916#include "extensions/renderer/script_context.h"
[email protected]323722d2013-03-26 07:21:0717
18using content::V8ValueConverter;
19
20namespace extensions {
21
rdevlin.cronin6fba7ec2016-06-24 16:15:0522APIActivityLogger::APIActivityLogger(ScriptContext* context,
23 Dispatcher* dispatcher)
24 : ObjectBackedNativeHandler(context), dispatcher_(dispatcher) {
25 RouteFunction("LogEvent", base::Bind(&APIActivityLogger::LogEvent,
26 base::Unretained(this)));
27 RouteFunction("LogAPICall", base::Bind(&APIActivityLogger::LogAPICall,
28 base::Unretained(this)));
[email protected]323722d2013-03-26 07:21:0729}
30
rdevlin.cronin6fba7ec2016-06-24 16:15:0531APIActivityLogger::~APIActivityLogger() {}
32
[email protected]323722d2013-03-26 07:21:0733// static
[email protected]d8c5fbb2013-06-14 11:35:2534void APIActivityLogger::LogAPICall(
35 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]302e4ad2013-04-12 22:56:4336 LogInternal(APICALL, args);
[email protected]302e4ad2013-04-12 22:56:4337}
38
39// static
[email protected]d8c5fbb2013-06-14 11:35:2540void APIActivityLogger::LogEvent(
41 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]302e4ad2013-04-12 22:56:4342 LogInternal(EVENT, args);
[email protected]302e4ad2013-04-12 22:56:4343}
44
45// static
[email protected]d8c5fbb2013-06-14 11:35:2546void APIActivityLogger::LogInternal(
47 const CallType call_type,
48 const v8::FunctionCallbackInfo<v8::Value>& args) {
rdevlin.cronin415b73b2015-11-13 01:14:4749 CHECK_GT(args.Length(), 2);
50 CHECK(args[0]->IsString());
51 CHECK(args[1]->IsString());
52 CHECK(args[2]->IsArray());
[email protected]323722d2013-03-26 07:21:0753
rdevlin.cronin6fba7ec2016-06-24 16:15:0554 if (!dispatcher_->activity_logging_enabled())
55 return;
56
[email protected]95c6b3012013-12-02 14:30:3157 std::string ext_id = *v8::String::Utf8Value(args[0]);
[email protected]302e4ad2013-04-12 22:56:4358 ExtensionHostMsg_APIActionOrEvent_Params params;
[email protected]95c6b3012013-12-02 14:30:3159 params.api_call = *v8::String::Utf8Value(args[1]);
[email protected]323722d2013-03-26 07:21:0760 if (args.Length() == 4) // Extras are optional.
[email protected]95c6b3012013-12-02 14:30:3161 params.extra = *v8::String::Utf8Value(args[3]);
[email protected]323722d2013-03-26 07:21:0762 else
63 params.extra = "";
64
65 // Get the array of api call arguments.
66 v8::Local<v8::Array> arg_array = v8::Local<v8::Array>::Cast(args[2]);
67 if (arg_array->Length() > 0) {
dchengf6f80662016-04-20 20:26:0468 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]d754cbb02013-08-12 17:51:3669 ActivityLogConverterStrategy strategy;
70 converter->SetFunctionAllowed(true);
71 converter->SetStrategy(&strategy);
dchengf6f80662016-04-20 20:26:0472 std::unique_ptr<base::ListValue> arg_list(new base::ListValue());
[email protected]323722d2013-03-26 07:21:0773 for (size_t i = 0; i < arg_array->Length(); ++i) {
[email protected]e6893672014-05-01 17:29:1374 arg_list->Set(
75 i,
76 converter->FromV8Value(arg_array->Get(i),
77 args.GetIsolate()->GetCurrentContext()));
[email protected]323722d2013-03-26 07:21:0778 }
79 params.arguments.Swap(arg_list.get());
80 }
81
[email protected]302e4ad2013-04-12 22:56:4382 if (call_type == APICALL) {
83 content::RenderThread::Get()->Send(
84 new ExtensionHostMsg_AddAPIActionToActivityLog(ext_id, params));
85 } else if (call_type == EVENT) {
86 content::RenderThread::Get()->Send(
87 new ExtensionHostMsg_AddEventToActivityLog(ext_id, params));
88 }
[email protected]323722d2013-03-26 07:21:0789}
90
[email protected]323722d2013-03-26 07:21:0791} // namespace extensions