blob: 826abb0c89daee41eecd0261a1a62fd68d8ecafb [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
5#include <string>
[email protected]bcd9580f2014-04-17 19:17:596
[email protected]323722d2013-03-26 07:21:077#include "base/bind.h"
mek87e0ab52015-02-13 01:18:268#include "content/public/child/v8_value_converter.h"
[email protected]323722d2013-03-26 07:21:079#include "content/public/renderer/render_thread.h"
[email protected]fb820c02014-03-13 15:07:0810#include "extensions/common/extension_messages.h"
[email protected]fb5c4742014-05-01 09:05:2711#include "extensions/renderer/activity_log_converter_strategy.h"
[email protected]e6893672014-05-01 17:29:1312#include "extensions/renderer/api_activity_logger.h"
[email protected]bcd9580f2014-04-17 19:17:5913#include "extensions/renderer/script_context.h"
[email protected]323722d2013-03-26 07:21:0714
15using content::V8ValueConverter;
16
17namespace extensions {
18
[email protected]bcd9580f2014-04-17 19:17:5919APIActivityLogger::APIActivityLogger(ScriptContext* context)
20 : ObjectBackedNativeHandler(context) {
[email protected]302e4ad2013-04-12 22:56:4321 RouteFunction("LogEvent", base::Bind(&APIActivityLogger::LogEvent));
22 RouteFunction("LogAPICall", base::Bind(&APIActivityLogger::LogAPICall));
[email protected]323722d2013-03-26 07:21:0723}
24
25// static
[email protected]d8c5fbb2013-06-14 11:35:2526void APIActivityLogger::LogAPICall(
27 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]302e4ad2013-04-12 22:56:4328 LogInternal(APICALL, args);
[email protected]302e4ad2013-04-12 22:56:4329}
30
31// static
[email protected]d8c5fbb2013-06-14 11:35:2532void APIActivityLogger::LogEvent(
33 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]302e4ad2013-04-12 22:56:4334 LogInternal(EVENT, args);
[email protected]302e4ad2013-04-12 22:56:4335}
36
37// static
[email protected]d8c5fbb2013-06-14 11:35:2538void APIActivityLogger::LogInternal(
39 const CallType call_type,
40 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]323722d2013-03-26 07:21:0741 DCHECK_GT(args.Length(), 2);
42 DCHECK(args[0]->IsString());
43 DCHECK(args[1]->IsString());
44 DCHECK(args[2]->IsArray());
45
[email protected]95c6b3012013-12-02 14:30:3146 std::string ext_id = *v8::String::Utf8Value(args[0]);
[email protected]302e4ad2013-04-12 22:56:4347 ExtensionHostMsg_APIActionOrEvent_Params params;
[email protected]95c6b3012013-12-02 14:30:3148 params.api_call = *v8::String::Utf8Value(args[1]);
[email protected]323722d2013-03-26 07:21:0749 if (args.Length() == 4) // Extras are optional.
[email protected]95c6b3012013-12-02 14:30:3150 params.extra = *v8::String::Utf8Value(args[3]);
[email protected]323722d2013-03-26 07:21:0751 else
52 params.extra = "";
53
54 // Get the array of api call arguments.
55 v8::Local<v8::Array> arg_array = v8::Local<v8::Array>::Cast(args[2]);
56 if (arg_array->Length() > 0) {
57 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]d754cbb02013-08-12 17:51:3658 ActivityLogConverterStrategy strategy;
59 converter->SetFunctionAllowed(true);
60 converter->SetStrategy(&strategy);
[email protected]e53a7f292013-12-23 21:43:3861 scoped_ptr<base::ListValue> arg_list(new base::ListValue());
[email protected]323722d2013-03-26 07:21:0762 for (size_t i = 0; i < arg_array->Length(); ++i) {
[email protected]e6893672014-05-01 17:29:1363 arg_list->Set(
64 i,
65 converter->FromV8Value(arg_array->Get(i),
66 args.GetIsolate()->GetCurrentContext()));
[email protected]323722d2013-03-26 07:21:0767 }
68 params.arguments.Swap(arg_list.get());
69 }
70
[email protected]302e4ad2013-04-12 22:56:4371 if (call_type == APICALL) {
72 content::RenderThread::Get()->Send(
73 new ExtensionHostMsg_AddAPIActionToActivityLog(ext_id, params));
74 } else if (call_type == EVENT) {
75 content::RenderThread::Get()->Send(
76 new ExtensionHostMsg_AddEventToActivityLog(ext_id, params));
77 }
[email protected]323722d2013-03-26 07:21:0778}
79
[email protected]323722d2013-03-26 07:21:0780} // namespace extensions