OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 5 #include <string> |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "chrome/common/extensions/extension_messages.h" | 7 #include "chrome/common/extensions/extension_messages.h" |
8 #include "chrome/renderer/chrome_render_process_observer.h" | 8 #include "chrome/renderer/chrome_render_process_observer.h" |
9 #include "chrome/renderer/extensions/api_activity_logger.h" | 9 #include "chrome/renderer/extensions/api_activity_logger.h" |
10 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
11 #include "content/public/renderer/v8_value_converter.h" | 11 #include "content/public/renderer/v8_value_converter.h" |
12 | 12 |
13 using content::V8ValueConverter; | 13 using content::V8ValueConverter; |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 APIActivityLogger::APIActivityLogger( | 17 APIActivityLogger::APIActivityLogger( |
18 Dispatcher* dispatcher, v8::Handle<v8::Context> v8_context) | 18 Dispatcher* dispatcher, v8::Handle<v8::Context> v8_context) |
19 : ChromeV8Extension(dispatcher, v8_context) { | 19 : ChromeV8Extension(dispatcher, v8_context) { |
20 RouteFunction("LogActivity", base::Bind(&APIActivityLogger::LogActivity)); | 20 RouteFunction("LogEvent", base::Bind(&APIActivityLogger::LogEvent)); |
21 RouteFunction("LogAPICall", base::Bind(&APIActivityLogger::LogAPICall)); | |
21 } | 22 } |
22 | 23 |
23 // static | 24 // static |
24 v8::Handle<v8::Value> APIActivityLogger::LogActivity( | 25 v8::Handle<v8::Value> APIActivityLogger::LogAPICall(const v8::Arguments& args) { |
25 const v8::Arguments& args) { | 26 LogInternal(kApiCall, args); |
27 return v8::Undefined(); | |
28 } | |
29 | |
30 // static | |
31 v8::Handle<v8::Value> APIActivityLogger::LogEvent(const v8::Arguments& args) { | |
32 LogInternal(kEvent, args); | |
33 return v8::Undefined(); | |
34 } | |
35 | |
36 // static | |
37 void APIActivityLogger::LogInternal(const CallType call_type, | |
38 const v8::Arguments& args) { | |
26 DCHECK_GT(args.Length(), 2); | 39 DCHECK_GT(args.Length(), 2); |
27 DCHECK(args[0]->IsString()); | 40 DCHECK(args[0]->IsString()); |
28 DCHECK(args[1]->IsString()); | 41 DCHECK(args[1]->IsString()); |
29 DCHECK(args[2]->IsArray()); | 42 DCHECK(args[2]->IsArray()); |
30 | 43 |
31 // Get the simple values. | |
32 std::string ext_id = *v8::String::AsciiValue(args[0]->ToString()); | 44 std::string ext_id = *v8::String::AsciiValue(args[0]->ToString()); |
33 ExtensionHostMsg_APIAction_Params params; | 45 ExtensionHostMsg_APIActionOrEvent_Params params; |
34 params.api_call = *v8::String::AsciiValue(args[1]->ToString()); | 46 params.api_call = *v8::String::AsciiValue(args[1]->ToString()); |
35 if (args.Length() == 4) // Extras are optional. | 47 if (args.Length() == 4) // Extras are optional. |
36 params.extra = *v8::String::AsciiValue(args[3]->ToString()); | 48 params.extra = *v8::String::AsciiValue(args[3]->ToString()); |
37 else | 49 else |
38 params.extra = ""; | 50 params.extra = ""; |
39 | 51 |
40 // Get the array of api call arguments. | 52 // Get the array of api call arguments. |
41 v8::Local<v8::Array> arg_array = v8::Local<v8::Array>::Cast(args[2]); | 53 v8::Local<v8::Array> arg_array = v8::Local<v8::Array>::Cast(args[2]); |
42 if (arg_array->Length() > 0) { | 54 if (arg_array->Length() > 0) { |
43 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 55 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
44 scoped_ptr<ListValue> arg_list(new ListValue()); | 56 scoped_ptr<ListValue> arg_list(new ListValue()); |
45 for (size_t i = 0; i < arg_array->Length(); ++i) { | 57 for (size_t i = 0; i < arg_array->Length(); ++i) { |
46 arg_list->Set(i, | 58 arg_list->Set(i, |
47 converter->FromV8Value(arg_array->Get(i), | 59 converter->FromV8Value(arg_array->Get(i), |
48 v8::Context::GetCurrent())); | 60 v8::Context::GetCurrent())); |
49 } | 61 } |
50 params.arguments.Swap(arg_list.get()); | 62 params.arguments.Swap(arg_list.get()); |
51 } | 63 } |
52 | 64 |
53 content::RenderThread::Get()->Send( | 65 if (call_type == kApiCall) |
Matt Perry
2013/04/12 01:03:17
nit: use braces for multi-line ifs
felt
2013/04/12 02:07:21
Done.
| |
54 new ExtensionHostMsg_AddAPIActionToActivityLog(ext_id, params)); | 66 content::RenderThread::Get()->Send( |
67 new ExtensionHostMsg_AddAPIActionToActivityLog(ext_id, params)); | |
68 else if (call_type == kEvent) | |
69 content::RenderThread::Get()->Send( | |
70 new ExtensionHostMsg_AddEventToActivityLog(ext_id, params)); | |
71 } | |
55 | 72 |
56 return v8::Undefined(); | |
57 } | |
58 | 73 |
59 } // namespace extensions | 74 } // namespace extensions |
60 | 75 |
OLD | NEW |