Home | History | Annotate | Download | only in extensions
      1 // Copyright 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 "chrome/renderer/extensions/activity_log_converter_strategy.h"
      6 #include "base/values.h"
      7 
      8 namespace extensions {
      9 
     10 bool ActivityLogConverterStrategy::FromV8Object(v8::Handle<v8::Object> value,
     11                                                 base::Value** out,
     12                                                 v8::Isolate* isolate) const {
     13   return FromV8ObjectInternal(value, out, isolate);
     14 }
     15 
     16 bool ActivityLogConverterStrategy::FromV8Array(v8::Handle<v8::Array> value,
     17                                                base::Value** out,
     18                                                v8::Isolate* isolate) const {
     19   return FromV8ObjectInternal(value, out, isolate);
     20 }
     21 
     22 bool ActivityLogConverterStrategy::FromV8ObjectInternal(
     23     v8::Handle<v8::Object> value,
     24     base::Value** out,
     25     v8::Isolate* isolate) const {
     26 
     27   // Handle JSObject.
     28   // We cannot use value->Get(key/index) as there may be a getter method,
     29   // accessor, interceptor/handler, or access check callback set on the
     30   // property. If that it is the case, any of those may invoke JS code that
     31   // may result on logging extension activity events caused by value conversion
     32   // rather than extension itself.
     33 
     34   // V8 arrays are handled here in the same way as other JSObjects as they may
     35   // also have getter methods, accessor, interceptor/handler, and access check
     36   // callback.
     37 
     38   v8::Handle<v8::String> name = v8::String::NewFromUtf8(isolate, "[");
     39   if (value->IsFunction()) {
     40     name =
     41         v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "Function"));
     42     v8::Handle<v8::Value> fname =
     43         v8::Handle<v8::Function>::Cast(value)->GetName();
     44     if (fname->IsString() && v8::Handle<v8::String>::Cast(fname)->Length()) {
     45       name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, " "));
     46       name = v8::String::Concat(name, v8::Handle<v8::String>::Cast(fname));
     47       name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "()"));
     48     }
     49   } else {
     50     name = v8::String::Concat(name, value->GetConstructorName());
     51   }
     52   name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "]"));
     53   *out = new base::StringValue(std::string(*v8::String::Utf8Value(name)));
     54   // Prevent V8ValueConverter from further processing this object.
     55   return true;
     56 }
     57 
     58 }  // namespace extensions
     59