blob: a6b95af98c90eda87d4519ae2d26f58cf43fb797 [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]b6708d032013-07-14 08:18:2223 RouteFunction("LogBlockedCall",
24 base::Bind(&APIActivityLogger::LogBlockedCallWrapper));
[email protected]323722d2013-03-26 07:21:0725}
26
27// static
[email protected]d8c5fbb2013-06-14 11:35:2528void APIActivityLogger::LogAPICall(
29 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]302e4ad2013-04-12 22:56:4330 LogInternal(APICALL, args);
[email protected]302e4ad2013-04-12 22:56:4331}
32
33// static
[email protected]d8c5fbb2013-06-14 11:35:2534void APIActivityLogger::LogEvent(
35 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]302e4ad2013-04-12 22:56:4336 LogInternal(EVENT, args);
[email protected]302e4ad2013-04-12 22:56:4337}
38
39// static
[email protected]d8c5fbb2013-06-14 11:35:2540void APIActivityLogger::LogInternal(
41 const CallType call_type,
42 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]323722d2013-03-26 07:21:0743 DCHECK_GT(args.Length(), 2);
44 DCHECK(args[0]->IsString());
45 DCHECK(args[1]->IsString());
46 DCHECK(args[2]->IsArray());
47
[email protected]b6708d032013-07-14 08:18:2248 std::string ext_id = *v8::String::AsciiValue(args[0]);
[email protected]302e4ad2013-04-12 22:56:4349 ExtensionHostMsg_APIActionOrEvent_Params params;
[email protected]b6708d032013-07-14 08:18:2250 params.api_call = *v8::String::AsciiValue(args[1]);
[email protected]323722d2013-03-26 07:21:0751 if (args.Length() == 4) // Extras are optional.
[email protected]b6708d032013-07-14 08:18:2252 params.extra = *v8::String::AsciiValue(args[3]);
[email protected]323722d2013-03-26 07:21:0753 else
54 params.extra = "";
55
56 // Get the array of api call arguments.
57 v8::Local<v8::Array> arg_array = v8::Local<v8::Array>::Cast(args[2]);
58 if (arg_array->Length() > 0) {
59 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]d754cbb02013-08-12 17:51:3660 ActivityLogConverterStrategy strategy;
61 converter->SetFunctionAllowed(true);
62 converter->SetStrategy(&strategy);
[email protected]323722d2013-03-26 07:21:0763 scoped_ptr<ListValue> arg_list(new ListValue());
64 for (size_t i = 0; i < arg_array->Length(); ++i) {
65 arg_list->Set(i,
66 converter->FromV8Value(arg_array->Get(i),
67 v8::Context::GetCurrent()));
68 }
69 params.arguments.Swap(arg_list.get());
70 }
71
[email protected]302e4ad2013-04-12 22:56:4372 if (call_type == APICALL) {
73 content::RenderThread::Get()->Send(
74 new ExtensionHostMsg_AddAPIActionToActivityLog(ext_id, params));
75 } else if (call_type == EVENT) {
76 content::RenderThread::Get()->Send(
77 new ExtensionHostMsg_AddEventToActivityLog(ext_id, params));
78 }
[email protected]323722d2013-03-26 07:21:0779}
80
[email protected]78216e12013-05-17 01:11:2581// static
[email protected]b6708d032013-07-14 08:18:2282void APIActivityLogger::LogBlockedCallWrapper(
83 const v8::FunctionCallbackInfo<v8::Value>& args) {
84 DCHECK_EQ(args.Length(), 3);
85 DCHECK(args[0]->IsString());
86 DCHECK(args[1]->IsString());
87 DCHECK(args[2]->IsNumber());
88 int result;
89 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
90 converter->FromV8Value(args[2],
91 v8::Context::GetCurrent())->GetAsInteger(&result);
92 LogBlockedCall(*v8::String::AsciiValue(args[0]),
93 *v8::String::AsciiValue(args[1]),
94 static_cast<Feature::AvailabilityResult>(result));
95}
96
97// static
[email protected]78216e12013-05-17 01:11:2598void APIActivityLogger::LogBlockedCall(const std::string& extension_id,
[email protected]b6708d032013-07-14 08:18:2299 const std::string& function_name,
100 Feature::AvailabilityResult result) {
101 // We don't really want to bother logging if it isn't permission related.
102 if (result == Feature::INVALID_MIN_MANIFEST_VERSION ||
103 result == Feature::INVALID_MAX_MANIFEST_VERSION ||
104 result == Feature::UNSUPPORTED_CHANNEL)
105 return;
[email protected]78216e12013-05-17 01:11:25106 content::RenderThread::Get()->Send(
107 new ExtensionHostMsg_AddBlockedCallToActivityLog(extension_id,
108 function_name));
109}
110
[email protected]302e4ad2013-04-12 22:56:43111
[email protected]323722d2013-03-26 07:21:07112} // namespace extensions