Home | History | Annotate | Download | only in extensions
      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.
      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"
      9 #include "chrome/renderer/extensions/activity_log_converter_strategy.h"
     10 #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 
     14 using content::V8ValueConverter;
     15 
     16 namespace extensions {
     17 
     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));
     23 }
     24 
     25 // static
     26 void APIActivityLogger::LogAPICall(
     27     const v8::FunctionCallbackInfo<v8::Value>& args) {
     28   LogInternal(APICALL, args);
     29 }
     30 
     31 // static
     32 void APIActivityLogger::LogEvent(
     33     const v8::FunctionCallbackInfo<v8::Value>& args) {
     34   LogInternal(EVENT, args);
     35 }
     36 
     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());
     45 
     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 = "";
     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());
     58     ActivityLogConverterStrategy strategy;
     59     converter->SetFunctionAllowed(true);
     60     converter->SetStrategy(&strategy);
     61     scoped_ptr<ListValue> arg_list(new 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()));
     66     }
     67     params.arguments.Swap(arg_list.get());
     68   }
     69 
     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));
     76   }
     77 }
     78 
     79 }  // namespace extensions
     80