OLD | NEW |
(Empty) | |
| 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 "base/values.h" |
| 6 #include "chrome/renderer/extensions/activity_log_value_converter.h" |
| 7 |
| 8 namespace extensions { |
| 9 |
| 10 ActivityLogValueConverter::ActivityLogValueConverter() |
| 11 : v8_value_converter_(content::V8ValueConverter::create()) { |
| 12 } |
| 13 |
| 14 ActivityLogValueConverter::~ActivityLogValueConverter() {} |
| 15 |
| 16 base::Value* ActivityLogValueConverter::FromV8Value( |
| 17 v8::Handle<v8::Value> value, |
| 18 v8::Handle<v8::Context> context) const { |
| 19 |
| 20 if (!value->IsObject()) |
| 21 return v8_value_converter_->FromV8Value(value, context); |
| 22 |
| 23 // Handle JSObject. |
| 24 // We cannot use value->Get(key/index) as there may be an accessor installed |
| 25 // on the key/index. If that is the case, V8ValueConverter will execute the |
| 26 // getter function which may interfere with the JS execution environment in |
| 27 // the way unexpected by a developer. Moreover, executing getter functions may |
| 28 // lead to recursive logging by ActivityLog when the getter accesses |
| 29 // properties or issues calls on a JS object that happens to be a wrapper of a |
| 30 // DOM object. V8 API does not allow to check if a property has a getter |
| 31 // method installed. One way to do it is by using getOwnPropertyDescriptor |
| 32 // method of the JS object; however, the function may be redefined and return |
| 33 // bogus values. Apart from accessors, a JS object may have an interceptor |
| 34 // installed which is not a problem as it can be tested with the |
| 35 // HasNamedLookupInterceptor and HasIndexedLookupInterceptor V8 API calls. |
| 36 |
| 37 v8::Handle<v8::String> name = v8::String::New("["); |
| 38 |
| 39 if (value->IsFunction()) { |
| 40 name = v8::String::Concat(name, v8::String::New("Function")); |
| 41 v8::Handle<v8::Value> fname = |
| 42 v8::Handle<v8::Function>::Cast(value)->GetName(); |
| 43 if (fname->IsString() && v8::Handle<v8::String>::Cast(fname)->Length()) { |
| 44 name = v8::String::Concat(name, v8::String::New(" ")); |
| 45 name = v8::String::Concat(name, v8::Handle<v8::String>::Cast(fname)); |
| 46 name = v8::String::Concat(name, v8::String::New("()")); |
| 47 } |
| 48 } else { |
| 49 name = v8::String::Concat(name, value->ToObject()->GetConstructorName()); |
| 50 } |
| 51 |
| 52 name = v8::String::Concat(name, v8::String::New("]")); |
| 53 v8::String::Utf8Value utf8(name); |
| 54 return new base::StringValue(std::string(*utf8, utf8.length())); |
| 55 } |
| 56 |
| 57 } // namespace extensions |
| 58 |
OLD | NEW |