[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 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> |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 6 | |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 7 | #include "base/bind.h" |
mek | 87e0ab5 | 2015-02-13 01:18:26 | [diff] [blame] | 8 | #include "content/public/child/v8_value_converter.h" |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 9 | #include "content/public/renderer/render_thread.h" |
[email protected] | fb820c0 | 2014-03-13 15:07:08 | [diff] [blame] | 10 | #include "extensions/common/extension_messages.h" |
[email protected] | fb5c474 | 2014-05-01 09:05:27 | [diff] [blame] | 11 | #include "extensions/renderer/activity_log_converter_strategy.h" |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 12 | #include "extensions/renderer/api_activity_logger.h" |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 13 | #include "extensions/renderer/script_context.h" |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 14 | |
| 15 | using content::V8ValueConverter; |
| 16 | |
| 17 | namespace extensions { |
| 18 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 19 | APIActivityLogger::APIActivityLogger(ScriptContext* context) |
| 20 | : ObjectBackedNativeHandler(context) { |
[email protected] | 302e4ad | 2013-04-12 22:56:43 | [diff] [blame] | 21 | RouteFunction("LogEvent", base::Bind(&APIActivityLogger::LogEvent)); |
| 22 | RouteFunction("LogAPICall", base::Bind(&APIActivityLogger::LogAPICall)); |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | // static |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 26 | void APIActivityLogger::LogAPICall( |
| 27 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 302e4ad | 2013-04-12 22:56:43 | [diff] [blame] | 28 | LogInternal(APICALL, args); |
[email protected] | 302e4ad | 2013-04-12 22:56:43 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // static |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 32 | void APIActivityLogger::LogEvent( |
| 33 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 302e4ad | 2013-04-12 22:56:43 | [diff] [blame] | 34 | LogInternal(EVENT, args); |
[email protected] | 302e4ad | 2013-04-12 22:56:43 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // static |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 38 | void APIActivityLogger::LogInternal( |
| 39 | const CallType call_type, |
| 40 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 41 | DCHECK_GT(args.Length(), 2); |
| 42 | DCHECK(args[0]->IsString()); |
| 43 | DCHECK(args[1]->IsString()); |
| 44 | DCHECK(args[2]->IsArray()); |
| 45 | |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 46 | std::string ext_id = *v8::String::Utf8Value(args[0]); |
[email protected] | 302e4ad | 2013-04-12 22:56:43 | [diff] [blame] | 47 | ExtensionHostMsg_APIActionOrEvent_Params params; |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 48 | params.api_call = *v8::String::Utf8Value(args[1]); |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 49 | if (args.Length() == 4) // Extras are optional. |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 50 | params.extra = *v8::String::Utf8Value(args[3]); |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 51 | 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] | d754cbb0 | 2013-08-12 17:51:36 | [diff] [blame] | 58 | ActivityLogConverterStrategy strategy; |
| 59 | converter->SetFunctionAllowed(true); |
| 60 | converter->SetStrategy(&strategy); |
[email protected] | e53a7f29 | 2013-12-23 21:43:38 | [diff] [blame] | 61 | scoped_ptr<base::ListValue> arg_list(new base::ListValue()); |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 62 | for (size_t i = 0; i < arg_array->Length(); ++i) { |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 63 | arg_list->Set( |
| 64 | i, |
| 65 | converter->FromV8Value(arg_array->Get(i), |
| 66 | args.GetIsolate()->GetCurrentContext())); |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 67 | } |
| 68 | params.arguments.Swap(arg_list.get()); |
| 69 | } |
| 70 | |
[email protected] | 302e4ad | 2013-04-12 22:56:43 | [diff] [blame] | 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)); |
| 77 | } |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | 323722d | 2013-03-26 07:21:07 | [diff] [blame] | 80 | } // namespace extensions |