Fix crashing tests on pre-KK devices.
[chromium-blink-merge.git] / extensions / renderer / api_activity_logger.cc
blob826abb0c89daee41eecd0261a1a62fd68d8ecafb
1 // Copyright 2014 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.
5 #include <string>
7 #include "base/bind.h"
8 #include "content/public/child/v8_value_converter.h"
9 #include "content/public/renderer/render_thread.h"
10 #include "extensions/common/extension_messages.h"
11 #include "extensions/renderer/activity_log_converter_strategy.h"
12 #include "extensions/renderer/api_activity_logger.h"
13 #include "extensions/renderer/script_context.h"
15 using content::V8ValueConverter;
17 namespace extensions {
19 APIActivityLogger::APIActivityLogger(ScriptContext* context)
20 : ObjectBackedNativeHandler(context) {
21 RouteFunction("LogEvent", base::Bind(&APIActivityLogger::LogEvent));
22 RouteFunction("LogAPICall", base::Bind(&APIActivityLogger::LogAPICall));
25 // static
26 void APIActivityLogger::LogAPICall(
27 const v8::FunctionCallbackInfo<v8::Value>& args) {
28 LogInternal(APICALL, args);
31 // static
32 void APIActivityLogger::LogEvent(
33 const v8::FunctionCallbackInfo<v8::Value>& args) {
34 LogInternal(EVENT, args);
37 // static
38 void APIActivityLogger::LogInternal(
39 const CallType call_type,
40 const v8::FunctionCallbackInfo<v8::Value>& args) {
41 DCHECK_GT(args.Length(), 2);
42 DCHECK(args[0]->IsString());
43 DCHECK(args[1]->IsString());
44 DCHECK(args[2]->IsArray());
46 std::string ext_id = *v8::String::Utf8Value(args[0]);
47 ExtensionHostMsg_APIActionOrEvent_Params params;
48 params.api_call = *v8::String::Utf8Value(args[1]);
49 if (args.Length() == 4) // Extras are optional.
50 params.extra = *v8::String::Utf8Value(args[3]);
51 else
52 params.extra = "";
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());
58 ActivityLogConverterStrategy strategy;
59 converter->SetFunctionAllowed(true);
60 converter->SetStrategy(&strategy);
61 scoped_ptr<base::ListValue> arg_list(new base::ListValue());
62 for (size_t i = 0; i < arg_array->Length(); ++i) {
63 arg_list->Set(
65 converter->FromV8Value(arg_array->Get(i),
66 args.GetIsolate()->GetCurrentContext()));
68 params.arguments.Swap(arg_list.get());
71 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));
80 } // namespace extensions