blob: 4bd1b981545e620f41bccafc9756f0d095264ac0 [file] [log] [blame]
[email protected]323722d2013-03-26 07:21:071// Copyright (c) 2013 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 <string>
6#include "base/bind.h"
7#include "chrome/common/extensions/extension_messages.h"
8#include "chrome/renderer/chrome_render_process_observer.h"
[email protected]d754cbb02013-08-12 17:51:369#include "chrome/renderer/extensions/activity_log_converter_strategy.h"
[email protected]323722d2013-03-26 07:21:0710#include "chrome/renderer/extensions/api_activity_logger.h"
11#include "content/public/renderer/render_thread.h"
12#include "content/public/renderer/v8_value_converter.h"
13
14using content::V8ValueConverter;
15
16namespace extensions {
17
18APIActivityLogger::APIActivityLogger(
[email protected]9a598442013-06-04 16:39:1219 Dispatcher* dispatcher, ChromeV8Context* context)
20 : ChromeV8Extension(dispatcher, 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) {
63 arg_list->Set(i,
64 converter->FromV8Value(arg_array->Get(i),
[email protected]95c6b3012013-12-02 14:30:3165 args.GetIsolate()->GetCurrentContext()));
[email protected]323722d2013-03-26 07:21:0766 }
67 params.arguments.Swap(arg_list.get());
68 }
69
[email protected]302e4ad2013-04-12 22:56:4370 if (call_type == APICALL) {
71 content::RenderThread::Get()->Send(
72 new ExtensionHostMsg_AddAPIActionToActivityLog(ext_id, params));
73 } else if (call_type == EVENT) {
74 content::RenderThread::Get()->Send(
75 new ExtensionHostMsg_AddEventToActivityLog(ext_id, params));
76 }
[email protected]323722d2013-03-26 07:21:0777}
78
[email protected]323722d2013-03-26 07:21:0779} // namespace extensions