Move extension_messages.h to extensions/common.
[chromium-blink-merge.git] / chrome / renderer / extensions / api_activity_logger.cc
blob80f25bdebf95a9df77eec52f244b7d5d3de53726
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.
5 #include <string>
6 #include "base/bind.h"
7 #include "chrome/renderer/chrome_render_process_observer.h"
8 #include "chrome/renderer/extensions/activity_log_converter_strategy.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"
12 #include "extensions/common/extension_messages.h"
14 using content::V8ValueConverter;
16 namespace extensions {
18 APIActivityLogger::APIActivityLogger(
19 Dispatcher* dispatcher, ChromeV8Context* context)
20 : ChromeV8Extension(dispatcher, 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(i,
64 converter->FromV8Value(arg_array->Get(i),
65 args.GetIsolate()->GetCurrentContext()));
67 params.arguments.Swap(arg_list.get());
70 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));
79 } // namespace extensions