1 // 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.
7 #include "chrome/common/extensions/extension_messages.h"
8 #include "chrome/renderer/chrome_render_process_observer.h"
9 #include "chrome/renderer/extensions/api_activity_logger.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "content/public/renderer/v8_value_converter.h"
13 using content::V8ValueConverter
;
15 namespace extensions
{
17 APIActivityLogger::APIActivityLogger(
18 Dispatcher
* dispatcher
, v8::Handle
<v8::Context
> v8_context
)
19 : ChromeV8Extension(dispatcher
, v8_context
) {
20 RouteFunction("LogEvent", base::Bind(&APIActivityLogger::LogEvent
));
21 RouteFunction("LogAPICall", base::Bind(&APIActivityLogger::LogAPICall
));
25 v8::Handle
<v8::Value
> APIActivityLogger::LogAPICall(const v8::Arguments
& args
) {
26 LogInternal(APICALL
, args
);
27 return v8::Undefined();
31 v8::Handle
<v8::Value
> APIActivityLogger::LogEvent(const v8::Arguments
& args
) {
32 LogInternal(EVENT
, args
);
33 return v8::Undefined();
37 void APIActivityLogger::LogInternal(const CallType call_type
,
38 const v8::Arguments
& args
) {
39 DCHECK_GT(args
.Length(), 2);
40 DCHECK(args
[0]->IsString());
41 DCHECK(args
[1]->IsString());
42 DCHECK(args
[2]->IsArray());
44 std::string ext_id
= *v8::String::AsciiValue(args
[0]->ToString());
45 ExtensionHostMsg_APIActionOrEvent_Params params
;
46 params
.api_call
= *v8::String::AsciiValue(args
[1]->ToString());
47 if (args
.Length() == 4) // Extras are optional.
48 params
.extra
= *v8::String::AsciiValue(args
[3]->ToString());
52 // Get the array of api call arguments.
53 v8::Local
<v8::Array
> arg_array
= v8::Local
<v8::Array
>::Cast(args
[2]);
54 if (arg_array
->Length() > 0) {
55 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
56 scoped_ptr
<ListValue
> arg_list(new ListValue());
57 for (size_t i
= 0; i
< arg_array
->Length(); ++i
) {
59 converter
->FromV8Value(arg_array
->Get(i
),
60 v8::Context::GetCurrent()));
62 params
.arguments
.Swap(arg_list
.get());
65 if (call_type
== APICALL
) {
66 content::RenderThread::Get()->Send(
67 new ExtensionHostMsg_AddAPIActionToActivityLog(ext_id
, params
));
68 } else if (call_type
== EVENT
) {
69 content::RenderThread::Get()->Send(
70 new ExtensionHostMsg_AddEventToActivityLog(ext_id
, params
));
75 } // namespace extensions