[email protected] | fb5c474 | 2014-05-01 09:05:27 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | d754cbb0 | 2013-08-12 17:51:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | fb5c474 | 2014-05-01 09:05:27 | [diff] [blame] | 5 | #include "extensions/renderer/activity_log_converter_strategy.h" |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 6 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | d754cbb0 | 2013-08-12 17:51:36 | [diff] [blame] | 10 | #include "base/values.h" |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 11 | #include "v8/include/v8.h" |
[email protected] | d754cbb0 | 2013-08-12 17:51:36 | [diff] [blame] | 12 | |
| 13 | namespace extensions { |
| 14 | |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 15 | namespace { |
| 16 | |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 17 | // Summarize a V8 value. This performs a shallow conversion in all cases, and |
| 18 | // returns only a string with a description of the value (e.g., |
| 19 | // "[HTMLElement]"). |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 20 | std::unique_ptr<base::Value> SummarizeV8Value(v8::Isolate* isolate, |
| 21 | v8::Local<v8::Object> object) { |
jochen | dfd8ec1 | 2015-11-30 11:33:42 | [diff] [blame] | 22 | v8::TryCatch try_catch(isolate); |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 23 | v8::Isolate::DisallowJavascriptExecutionScope scope( |
| 24 | isolate, v8::Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); |
| 25 | v8::Local<v8::String> name = v8::String::NewFromUtf8(isolate, "["); |
| 26 | if (object->IsFunction()) { |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 27 | name = |
| 28 | v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "Function")); |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 29 | v8::Local<v8::Value> fname = |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 30 | v8::Local<v8::Function>::Cast(object)->GetName(); |
| 31 | if (fname->IsString() && v8::Local<v8::String>::Cast(fname)->Length()) { |
[email protected] | 82438f2 | 2013-11-28 11:00:00 | [diff] [blame] | 32 | name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, " ")); |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 33 | name = v8::String::Concat(name, v8::Local<v8::String>::Cast(fname)); |
[email protected] | 82438f2 | 2013-11-28 11:00:00 | [diff] [blame] | 34 | name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "()")); |
[email protected] | d754cbb0 | 2013-08-12 17:51:36 | [diff] [blame] | 35 | } |
| 36 | } else { |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 37 | name = v8::String::Concat(name, object->GetConstructorName()); |
[email protected] | d754cbb0 | 2013-08-12 17:51:36 | [diff] [blame] | 38 | } |
[email protected] | 82438f2 | 2013-11-28 11:00:00 | [diff] [blame] | 39 | name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "]")); |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 40 | |
| 41 | if (try_catch.HasCaught()) { |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 42 | return std::unique_ptr<base::Value>( |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 43 | new base::Value("[JS Execution Exception]")); |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 44 | } |
| 45 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 46 | return std::unique_ptr<base::Value>( |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame^] | 47 | new base::Value(std::string(*v8::String::Utf8Value(name)))); |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
[email protected] | b0ac970 | 2014-07-18 12:55:43 | [diff] [blame] | 52 | ActivityLogConverterStrategy::ActivityLogConverterStrategy() {} |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 53 | |
| 54 | ActivityLogConverterStrategy::~ActivityLogConverterStrategy() {} |
| 55 | |
| 56 | bool ActivityLogConverterStrategy::FromV8Object( |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 57 | v8::Local<v8::Object> value, |
dcheng | 0232f57 | 2016-05-27 17:47:44 | [diff] [blame] | 58 | std::unique_ptr<base::Value>* out, |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 59 | v8::Isolate* isolate, |
| 60 | const FromV8ValueCallback& callback) const { |
| 61 | return FromV8Internal(value, out, isolate, callback); |
| 62 | } |
| 63 | |
| 64 | bool ActivityLogConverterStrategy::FromV8Array( |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 65 | v8::Local<v8::Array> value, |
dcheng | 0232f57 | 2016-05-27 17:47:44 | [diff] [blame] | 66 | std::unique_ptr<base::Value>* out, |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 67 | v8::Isolate* isolate, |
| 68 | const FromV8ValueCallback& callback) const { |
| 69 | return FromV8Internal(value, out, isolate, callback); |
| 70 | } |
| 71 | |
| 72 | bool ActivityLogConverterStrategy::FromV8Internal( |
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 73 | v8::Local<v8::Object> value, |
dcheng | 0232f57 | 2016-05-27 17:47:44 | [diff] [blame] | 74 | std::unique_ptr<base::Value>* out, |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 75 | v8::Isolate* isolate, |
| 76 | const FromV8ValueCallback& callback) const { |
dcheng | 0232f57 | 2016-05-27 17:47:44 | [diff] [blame] | 77 | *out = SummarizeV8Value(isolate, value); |
[email protected] | fd46e7a | 2014-04-16 17:59:47 | [diff] [blame] | 78 | |
[email protected] | d754cbb0 | 2013-08-12 17:51:36 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | } // namespace extensions |