blob: e78c27452f15e2eba36d8c8bce72c436834d069f [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"
[email protected]bcd9580f2014-04-17 19:17:5915#include "extensions/renderer/script_context.h"
[email protected]323722d2013-03-26 07:21:0716
17using content::V8ValueConverter;
18
19namespace extensions {
20
[email protected]bcd9580f2014-04-17 19:17:5921APIActivityLogger::APIActivityLogger(ScriptContext* context)
22 : ObjectBackedNativeHandler(context) {
[email protected]302e4ad2013-04-12 22:56:4323 RouteFunction("LogEvent", base::Bind(&APIActivityLogger::LogEvent));
24 RouteFunction("LogAPICall", base::Bind(&APIActivityLogger::LogAPICall));
[email protected]323722d2013-03-26 07:21:0725}
26
27// static
[email protected]d8c5fbb2013-06-14 11:35:2528void APIActivityLogger::LogAPICall(
29 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]302e4ad2013-04-12 22:56:4330 LogInternal(APICALL, args);
[email protected]302e4ad2013-04-12 22:56:4331}
32
33// static
[email protected]d8c5fbb2013-06-14 11:35:2534void APIActivityLogger::LogEvent(
35 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]302e4ad2013-04-12 22:56:4336 LogInternal(EVENT, args);
[email protected]302e4ad2013-04-12 22:56:4337}
38
39// static
[email protected]d8c5fbb2013-06-14 11:35:2540void APIActivityLogger::LogInternal(
41 const CallType call_type,
42 const v8::FunctionCallbackInfo<v8::Value>& args) {
rdevlin.cronin415b73b2015-11-13 01:14:4743 CHECK_GT(args.Length(), 2);
44 CHECK(args[0]->IsString());
45 CHECK(args[1]->IsString());
46 CHECK(args[2]->IsArray());
[email protected]323722d2013-03-26 07:21:0747
[email protected]95c6b3012013-12-02 14:30:3148 std::string ext_id = *v8::String::Utf8Value(args[0]);
[email protected]302e4ad2013-04-12 22:56:4349 ExtensionHostMsg_APIActionOrEvent_Params params;
[email protected]95c6b3012013-12-02 14:30:3150 params.api_call = *v8::String::Utf8Value(args[1]);
[email protected]323722d2013-03-26 07:21:0751 if (args.Length() == 4) // Extras are optional.
[email protected]95c6b3012013-12-02 14:30:3152 params.extra = *v8::String::Utf8Value(args[3]);
[email protected]323722d2013-03-26 07:21:0753 else
54 params.extra = "";
55
56 // Get the array of api call arguments.
57 v8::Local<v8::Array> arg_array = v8::Local<v8::Array>::Cast(args[2]);
58 if (arg_array->Length() > 0) {
59 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]d754cbb02013-08-12 17:51:3660 ActivityLogConverterStrategy strategy;
61 converter->SetFunctionAllowed(true);
62 converter->SetStrategy(&strategy);
[email protected]e53a7f292013-12-23 21:43:3863 scoped_ptr<base::ListValue> arg_list(new base::ListValue());
[email protected]323722d2013-03-26 07:21:0764 for (size_t i = 0; i < arg_array->Length(); ++i) {
[email protected]e6893672014-05-01 17:29:1365 arg_list->Set(
66 i,
67 converter->FromV8Value(arg_array->Get(i),
68 args.GetIsolate()->GetCurrentContext()));
[email protected]323722d2013-03-26 07:21:0769 }
70 params.arguments.Swap(arg_list.get());
71 }
72
[email protected]302e4ad2013-04-12 22:56:4373 if (call_type == APICALL) {
74 content::RenderThread::Get()->Send(
75 new ExtensionHostMsg_AddAPIActionToActivityLog(ext_id, params));
76 } else if (call_type == EVENT) {
77 content::RenderThread::Get()->Send(
78 new ExtensionHostMsg_AddEventToActivityLog(ext_id, params));
79 }
[email protected]323722d2013-03-26 07:21:0780}
81
[email protected]323722d2013-03-26 07:21:0782} // namespace extensions