OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/api_activity_logger.h" | |
6 | |
5 #include <stddef.h> | 7 #include <stddef.h> |
6 | 8 |
7 #include <string> | 9 #include <string> |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "content/public/child/v8_value_converter.h" | 12 #include "content/public/child/v8_value_converter.h" |
11 #include "content/public/renderer/render_thread.h" | 13 #include "content/public/renderer/render_thread.h" |
12 #include "extensions/common/extension_messages.h" | 14 #include "extensions/common/extension_messages.h" |
13 #include "extensions/renderer/activity_log_converter_strategy.h" | 15 #include "extensions/renderer/activity_log_converter_strategy.h" |
14 #include "extensions/renderer/api_activity_logger.h" | |
15 #include "extensions/renderer/dispatcher.h" | 16 #include "extensions/renderer/dispatcher.h" |
17 #include "extensions/renderer/extensions_renderer_client.h" | |
16 #include "extensions/renderer/script_context.h" | 18 #include "extensions/renderer/script_context.h" |
17 | 19 |
18 namespace extensions { | 20 namespace extensions { |
19 | 21 |
20 APIActivityLogger::APIActivityLogger(ScriptContext* context, | 22 APIActivityLogger::APIActivityLogger(ScriptContext* context, |
21 Dispatcher* dispatcher) | 23 Dispatcher* dispatcher) |
22 : ObjectBackedNativeHandler(context), dispatcher_(dispatcher) { | 24 : ObjectBackedNativeHandler(context), dispatcher_(dispatcher) { |
23 RouteFunction("LogEvent", base::Bind(&APIActivityLogger::LogEvent, | 25 RouteFunction("LogEvent", base::Bind(&APIActivityLogger::LogForJS, |
24 base::Unretained(this))); | 26 base::Unretained(this), EVENT)); |
25 RouteFunction("LogAPICall", base::Bind(&APIActivityLogger::LogAPICall, | 27 RouteFunction("LogAPICall", base::Bind(&APIActivityLogger::LogForJS, |
26 base::Unretained(this))); | 28 base::Unretained(this), APICALL)); |
27 } | 29 } |
28 | 30 |
29 APIActivityLogger::~APIActivityLogger() {} | 31 APIActivityLogger::~APIActivityLogger() {} |
30 | 32 |
31 // static | 33 // static |
32 void APIActivityLogger::LogAPICall( | 34 void APIActivityLogger::LogAPICall( |
33 const v8::FunctionCallbackInfo<v8::Value>& args) { | 35 v8::Local<v8::Context> context, |
34 LogInternal(APICALL, args); | 36 const std::string& call_name, |
37 const std::vector<v8::Local<v8::Value>>& arguments) { | |
38 const Dispatcher* dispatcher = | |
39 ExtensionsRendererClient::Get()->GetDispatcher(); | |
40 if (!dispatcher || // dispatcher can be null in unittests. | |
41 !dispatcher->activity_logging_enabled()) { | |
42 return; | |
43 } | |
44 | |
45 ScriptContext* script_context = | |
46 ScriptContextSet::GetContextByV8Context(context); | |
47 auto value_args = base::MakeUnique<base::ListValue>(); | |
48 std::unique_ptr<content::V8ValueConverter> converter = | |
49 content::V8ValueConverter::Create(); | |
50 ActivityLogConverterStrategy strategy; | |
51 converter->SetFunctionAllowed(true); | |
52 converter->SetStrategy(&strategy); | |
53 value_args->Reserve(arguments.size()); | |
54 for (const auto& arg : arguments) | |
55 value_args->Append(converter->FromV8Value(arg, context)); | |
jbroman
2017/06/30 19:05:48
How important is it that these are correct? It see
Devlin
2017/07/06 17:09:57
Nice catch. This is a bit fun.
Having perfectly-
jbroman
2017/07/06 20:48:45
Yeah, in my mind an ideal correct version looks so
| |
56 | |
57 LogInternal(APICALL, script_context->GetExtensionID(), call_name, | |
58 std::move(value_args), std::string()); | |
35 } | 59 } |
36 | 60 |
37 // static | 61 void APIActivityLogger::LogForJS( |
38 void APIActivityLogger::LogEvent( | |
39 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
40 LogInternal(EVENT, args); | |
41 } | |
42 | |
43 // static | |
44 void APIActivityLogger::LogInternal( | |
45 const CallType call_type, | 62 const CallType call_type, |
46 const v8::FunctionCallbackInfo<v8::Value>& args) { | 63 const v8::FunctionCallbackInfo<v8::Value>& args) { |
47 CHECK_GT(args.Length(), 2); | 64 CHECK_GT(args.Length(), 2); |
48 CHECK(args[0]->IsString()); | 65 CHECK(args[0]->IsString()); |
49 CHECK(args[1]->IsString()); | 66 CHECK(args[1]->IsString()); |
50 CHECK(args[2]->IsArray()); | 67 CHECK(args[2]->IsArray()); |
51 | 68 |
52 if (!dispatcher_->activity_logging_enabled()) | 69 if (!dispatcher_->activity_logging_enabled()) |
53 return; | 70 return; |
54 | 71 |
55 std::string ext_id = *v8::String::Utf8Value(args[0]); | 72 v8::Isolate* isolate = args.GetIsolate(); |
56 ExtensionHostMsg_APIActionOrEvent_Params params; | 73 v8::HandleScope handle_scope(isolate); |
57 params.api_call = *v8::String::Utf8Value(args[1]); | 74 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
58 if (args.Length() == 4) // Extras are optional. | |
59 params.extra = *v8::String::Utf8Value(args[3]); | |
60 else | |
61 params.extra = ""; | |
62 | 75 |
63 // Get the array of api call arguments. | 76 std::string extension_id = *v8::String::Utf8Value(args[0]); |
77 std::string call_name = *v8::String::Utf8Value(args[1]); | |
78 std::string extra; | |
79 if (args.Length() == 4) { // Extras are optional. | |
80 CHECK(args[3]->IsString()); | |
81 extra = *v8::String::Utf8Value(args[3]); | |
82 } | |
83 | |
84 // Get the array of call arguments. | |
85 auto arguments = base::MakeUnique<base::ListValue>(); | |
64 v8::Local<v8::Array> arg_array = v8::Local<v8::Array>::Cast(args[2]); | 86 v8::Local<v8::Array> arg_array = v8::Local<v8::Array>::Cast(args[2]); |
65 if (arg_array->Length() > 0) { | 87 if (arg_array->Length() > 0) { |
88 arguments->Reserve(arg_array->Length()); | |
66 std::unique_ptr<content::V8ValueConverter> converter = | 89 std::unique_ptr<content::V8ValueConverter> converter = |
67 content::V8ValueConverter::Create(); | 90 content::V8ValueConverter::Create(); |
68 ActivityLogConverterStrategy strategy; | 91 ActivityLogConverterStrategy strategy; |
69 converter->SetFunctionAllowed(true); | 92 converter->SetFunctionAllowed(true); |
70 converter->SetStrategy(&strategy); | 93 converter->SetStrategy(&strategy); |
71 std::unique_ptr<base::ListValue> arg_list(new base::ListValue()); | 94 for (size_t i = 0; i < arg_array->Length(); ++i) |
72 for (size_t i = 0; i < arg_array->Length(); ++i) { | 95 arguments->Append(converter->FromV8Value(arg_array->Get(i), context)); |
73 arg_list->Set( | |
74 i, | |
75 converter->FromV8Value(arg_array->Get(i), | |
76 args.GetIsolate()->GetCurrentContext())); | |
77 } | |
78 params.arguments.Swap(arg_list.get()); | |
79 } | 96 } |
80 | 97 |
98 LogInternal(call_type, extension_id, call_name, std::move(arguments), extra); | |
99 } | |
100 | |
101 // static | |
102 void APIActivityLogger::LogInternal(const CallType call_type, | |
103 const std::string& extension_id, | |
104 const std::string& call_name, | |
105 std::unique_ptr<base::ListValue> arguments, | |
106 const std::string& extra) { | |
107 ExtensionHostMsg_APIActionOrEvent_Params params; | |
108 params.api_call = call_name; | |
109 params.arguments.Swap(arguments.get()); | |
110 params.extra = extra; | |
81 if (call_type == APICALL) { | 111 if (call_type == APICALL) { |
82 content::RenderThread::Get()->Send( | 112 content::RenderThread::Get()->Send( |
83 new ExtensionHostMsg_AddAPIActionToActivityLog(ext_id, params)); | 113 new ExtensionHostMsg_AddAPIActionToActivityLog(extension_id, params)); |
84 } else if (call_type == EVENT) { | 114 } else if (call_type == EVENT) { |
85 content::RenderThread::Get()->Send( | 115 content::RenderThread::Get()->Send( |
86 new ExtensionHostMsg_AddEventToActivityLog(ext_id, params)); | 116 new ExtensionHostMsg_AddEventToActivityLog(extension_id, params)); |
87 } | 117 } |
88 } | 118 } |
89 | 119 |
90 } // namespace extensions | 120 } // namespace extensions |
OLD | NEW |