blob: 443535f8247d94422f10d6a9db4d38dc1df6b4b0 [file] [log] [blame]
[email protected]fb5c4742014-05-01 09:05:271// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]d754cbb02013-08-12 17:51:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]fb5c4742014-05-01 09:05:275#include "extensions/renderer/activity_log_converter_strategy.h"
[email protected]fd46e7a2014-04-16 17:59:476
dchengf6f80662016-04-20 20:26:047#include <memory>
8
[email protected]fd46e7a2014-04-16 17:59:479#include "base/logging.h"
[email protected]d754cbb02013-08-12 17:51:3610#include "base/values.h"
[email protected]fd46e7a2014-04-16 17:59:4711#include "v8/include/v8.h"
[email protected]d754cbb02013-08-12 17:51:3612
13namespace extensions {
14
[email protected]fd46e7a2014-04-16 17:59:4715namespace {
16
[email protected]fd46e7a2014-04-16 17:59:4717// 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]").
dchengf6f80662016-04-20 20:26:0420std::unique_ptr<base::Value> SummarizeV8Value(v8::Isolate* isolate,
21 v8::Local<v8::Object> object) {
jochendfd8ec12015-11-30 11:33:4222 v8::TryCatch try_catch(isolate);
[email protected]fd46e7a2014-04-16 17:59:4723 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]9c47471e2013-11-28 14:41:2127 name =
28 v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "Function"));
[email protected]fd46e7a2014-04-16 17:59:4729 v8::Local<v8::Value> fname =
tfarinaf85316f2015-04-29 17:03:4030 v8::Local<v8::Function>::Cast(object)->GetName();
31 if (fname->IsString() && v8::Local<v8::String>::Cast(fname)->Length()) {
[email protected]82438f22013-11-28 11:00:0032 name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, " "));
tfarinaf85316f2015-04-29 17:03:4033 name = v8::String::Concat(name, v8::Local<v8::String>::Cast(fname));
[email protected]82438f22013-11-28 11:00:0034 name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "()"));
[email protected]d754cbb02013-08-12 17:51:3635 }
36 } else {
[email protected]fd46e7a2014-04-16 17:59:4737 name = v8::String::Concat(name, object->GetConstructorName());
[email protected]d754cbb02013-08-12 17:51:3638 }
[email protected]82438f22013-11-28 11:00:0039 name = v8::String::Concat(name, v8::String::NewFromUtf8(isolate, "]"));
[email protected]fd46e7a2014-04-16 17:59:4740
41 if (try_catch.HasCaught()) {
dchengf6f80662016-04-20 20:26:0442 return std::unique_ptr<base::Value>(
jdoerrie122c4da2017-03-06 11:12:0443 new base::Value("[JS Execution Exception]"));
[email protected]fd46e7a2014-04-16 17:59:4744 }
45
dchengf6f80662016-04-20 20:26:0446 return std::unique_ptr<base::Value>(
jdoerrie122c4da2017-03-06 11:12:0447 new base::Value(std::string(*v8::String::Utf8Value(name))));
[email protected]fd46e7a2014-04-16 17:59:4748}
49
50} // namespace
51
[email protected]b0ac9702014-07-18 12:55:4352ActivityLogConverterStrategy::ActivityLogConverterStrategy() {}
[email protected]fd46e7a2014-04-16 17:59:4753
54ActivityLogConverterStrategy::~ActivityLogConverterStrategy() {}
55
56bool ActivityLogConverterStrategy::FromV8Object(
tfarinaf85316f2015-04-29 17:03:4057 v8::Local<v8::Object> value,
dcheng0232f572016-05-27 17:47:4458 std::unique_ptr<base::Value>* out,
[email protected]fd46e7a2014-04-16 17:59:4759 v8::Isolate* isolate,
60 const FromV8ValueCallback& callback) const {
61 return FromV8Internal(value, out, isolate, callback);
62}
63
64bool ActivityLogConverterStrategy::FromV8Array(
tfarinaf85316f2015-04-29 17:03:4065 v8::Local<v8::Array> value,
dcheng0232f572016-05-27 17:47:4466 std::unique_ptr<base::Value>* out,
[email protected]fd46e7a2014-04-16 17:59:4767 v8::Isolate* isolate,
68 const FromV8ValueCallback& callback) const {
69 return FromV8Internal(value, out, isolate, callback);
70}
71
72bool ActivityLogConverterStrategy::FromV8Internal(
tfarinaf85316f2015-04-29 17:03:4073 v8::Local<v8::Object> value,
dcheng0232f572016-05-27 17:47:4474 std::unique_ptr<base::Value>* out,
[email protected]fd46e7a2014-04-16 17:59:4775 v8::Isolate* isolate,
76 const FromV8ValueCallback& callback) const {
dcheng0232f572016-05-27 17:47:4477 *out = SummarizeV8Value(isolate, value);
[email protected]fd46e7a2014-04-16 17:59:4778
[email protected]d754cbb02013-08-12 17:51:3679 return true;
80}
81
82} // namespace extensions